Open Lighting Architecture  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EspNetNode.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  * EspNetNode.h
17  * Header file for the EspNetNode class
18  * Copyright (C) 2005-2009 Simon Newton
19  */
20 
21 #ifndef PLUGINS_ESPNET_ESPNETNODE_H_
22 #define PLUGINS_ESPNET_ESPNETNODE_H_
23 
24 #include <string>
25 #include <map>
26 #include "ola/Callback.h"
27 #include "ola/DmxBuffer.h"
29 #include "ola/network/Interface.h"
30 #include "ola/network/Socket.h"
31 #include "plugins/espnet/EspNetPackets.h"
32 #include "plugins/espnet/RunLengthDecoder.h"
33 
34 namespace ola {
35 namespace plugin {
36 namespace espnet {
37 
38 // the node types
39 typedef enum {
40  ESPNET_NODE_TYPE_SINGLE_OUT = 0x0001, // ip to dmx
41  ESPNET_NODE_TYPE_SINGLE_IN = 0x0002, // dmx to ip
42  ESPNET_NODE_TYPE_RS232 = 0x0060,
43  ESPNET_NODE_TYPE_IO = 0x0061, // multi universe
44  ESPNET_NODE_TYPE_LONWORKS = 0x0100,
45 } espnet_node_type;
46 
47 enum { ESPNET_MAX_UNIVERSES = 512 };
48 
49 class EspNetNode {
50  public:
51  explicit EspNetNode(const std::string &ip_address);
52  virtual ~EspNetNode();
53 
54  bool Start();
55  bool Stop();
56 
57  const ola::network::Interface &GetInterface() const {
58  return m_interface;
59  }
60 
61  void SetName(const std::string &name) { m_node_name = name; }
62  void SetType(espnet_node_type type) { m_type = type; }
63  void SetUniverse(uint8_t universe) { m_universe = universe; }
64 
65  // IO methods
66  ola::network::UDPSocket* GetSocket() { return &m_socket; }
67  void SocketReady();
68 
69  // DMX Receiving methods
70  bool SetHandler(uint8_t universe, DmxBuffer *buffer,
71  ola::Callback0<void> *handler);
72  bool RemoveHandler(uint8_t universe);
73 
74  // Sending methods
75  bool SendPoll(bool full_poll = false);
76  bool SendDMX(uint8_t universe, const ola::DmxBuffer &buffer);
77 
78  private:
79  typedef struct {
80  DmxBuffer *buffer;
81  Callback0<void> *closure;
82  } universe_handler;
83 
84  EspNetNode(const EspNetNode&);
85  EspNetNode& operator=(const EspNetNode&);
86  bool InitNetwork();
87  void HandlePoll(const espnet_poll_t &poll, ssize_t length,
88  const ola::network::IPV4Address &source);
89  void HandleReply(const espnet_poll_reply_t &reply,
90  ssize_t length,
91  const ola::network::IPV4Address &source);
92  void HandleAck(const espnet_ack_t &ack, ssize_t length,
93  const ola::network::IPV4Address &source);
94  void HandleData(const espnet_data_t &data, ssize_t length,
95  const ola::network::IPV4Address &source);
96 
97  bool SendEspPoll(const ola::network::IPV4Address &dst, bool full);
98  bool SendEspAck(const ola::network::IPV4Address &dst,
99  uint8_t status,
100  uint8_t crc);
101  bool SendEspPollReply(const ola::network::IPV4Address &dst);
102  bool SendEspData(const ola::network::IPV4Address &dst,
103  uint8_t universe,
104  const DmxBuffer &buffer);
105  bool SendPacket(const ola::network::IPV4Address &dst,
106  const espnet_packet_union_t &packet,
107  unsigned int size);
108 
109  bool m_running;
110  uint8_t m_options;
111  uint8_t m_tos;
112  uint8_t m_ttl;
113  uint8_t m_universe;
114  espnet_node_type m_type;
115  std::string m_node_name;
116  std::string m_preferred_ip;
117  std::map<uint8_t, universe_handler> m_handlers;
118  ola::network::Interface m_interface;
119  ola::network::UDPSocket m_socket;
120  RunLengthDecoder m_decoder;
121 
122  static const char NODE_NAME[];
123  static const uint8_t DEFAULT_OPTIONS = 0;
124  static const uint8_t DEFAULT_TOS = 0;
125  static const uint8_t DEFAULT_TTL = 4;
126  static const uint8_t FIRMWARE_VERSION = 1;
127  static const uint8_t SWITCH_SETTINGS = 0;
128  static const uint16_t ESPNET_PORT = 3333;
129  static const uint8_t DATA_RAW = 1;
130  static const uint8_t DATA_PAIRS = 2;
131  static const uint8_t DATA_RLE = 4;
132  static const uint8_t START_CODE = 0;
133 };
134 } // namespace espnet
135 } // namespace plugin
136 } // namespace ola
137 #endif // PLUGINS_ESPNET_ESPNETNODE_H_