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