Open Lighting Architecture
 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"
28 #include "ola/network/IPV4Address.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 
39 
40 // the node types
41 typedef enum {
42  ESPNET_NODE_TYPE_SINGLE_OUT = 0x0001, // ip to dmx
43  ESPNET_NODE_TYPE_SINGLE_IN = 0x0002, // dmx to ip
44  ESPNET_NODE_TYPE_RS232 = 0x0060,
45  ESPNET_NODE_TYPE_IO = 0x0061, // multi universe
46  ESPNET_NODE_TYPE_LONWORKS = 0x0100,
47 } espnet_node_type;
48 
49 enum { ESPNET_MAX_UNIVERSES = 512 };
50 
51 using std::string;
52 
53 class EspNetNode {
54  public:
55  explicit EspNetNode(const string &ip_address);
56  virtual ~EspNetNode();
57 
58  bool Start();
59  bool Stop();
60 
61  const ola::network::Interface &GetInterface() const {
62  return m_interface;
63  }
64 
65  void SetName(const string &name) { m_node_name = name; }
66  void SetType(espnet_node_type type) { m_type = type; }
67  void SetUniverse(uint8_t universe) { m_universe = universe; }
68 
69  // IO methods
70  ola::network::UDPSocket* GetSocket() { return &m_socket; }
71  void SocketReady();
72 
73  // DMX Receiving methods
74  bool SetHandler(uint8_t universe, DmxBuffer *buffer,
75  ola::Callback0<void> *handler);
76  bool RemoveHandler(uint8_t universe);
77 
78  // Sending methods
79  bool SendPoll(bool full_poll = false);
80  bool SendDMX(uint8_t universe, const ola::DmxBuffer &buffer);
81 
82  private:
83  typedef struct {
84  DmxBuffer *buffer;
85  Callback0<void> *closure;
86  } universe_handler;
87 
88  EspNetNode(const EspNetNode&);
89  EspNetNode& operator=(const EspNetNode&);
90  bool InitNetwork();
91  void HandlePoll(const espnet_poll_t &poll, ssize_t length,
92  const IPV4Address &source);
93  void HandleReply(const espnet_poll_reply_t &reply,
94  ssize_t length,
95  const IPV4Address &source);
96  void HandleAck(const espnet_ack_t &ack, ssize_t length,
97  const IPV4Address &source);
98  void HandleData(const espnet_data_t &data, ssize_t length,
99  const IPV4Address &source);
100 
101  bool SendEspPoll(const IPV4Address &dst, bool full);
102  bool SendEspAck(const IPV4Address &dst,
103  uint8_t status,
104  uint8_t crc);
105  bool SendEspPollReply(const IPV4Address &dst);
106  bool SendEspData(const IPV4Address &dst,
107  uint8_t universe,
108  const DmxBuffer &buffer);
109  bool SendPacket(const IPV4Address &dst,
110  const espnet_packet_union_t &packet,
111  unsigned int size);
112 
113  bool m_running;
114  uint8_t m_options;
115  uint8_t m_tos;
116  uint8_t m_ttl;
117  uint8_t m_universe;
118  espnet_node_type m_type;
119  string m_node_name;
120  string m_preferred_ip;
121  std::map<uint8_t, universe_handler> m_handlers;
122  ola::network::Interface m_interface;
123  ola::network::UDPSocket m_socket;
124  RunLengthDecoder m_decoder;
125 
126  static const char NODE_NAME[];
127  static const uint8_t DEFAULT_OPTIONS = 0;
128  static const uint8_t DEFAULT_TOS = 0;
129  static const uint8_t DEFAULT_TTL = 4;
130  static const uint8_t FIRMWARE_VERSION = 1;
131  static const uint8_t SWITCH_SETTINGS = 0;
132  static const uint16_t ESPNET_PORT = 3333;
133  static const uint8_t DATA_RAW = 1;
134  static const uint8_t DATA_PAIRS = 2;
135  static const uint8_t DATA_RLE = 4;
136  static const uint8_t START_CODE = 0;
137 };
138 } // namespace espnet
139 } // namespace plugin
140 } // namespace ola
141 #endif // PLUGINS_ESPNET_ESPNETNODE_H_