Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SandNetNode.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  * SandNetNode.h
17  * Header file for the SandNetNode class
18  * Copyright (C) 2005-2009 Simon Newton
19  */
20 
21 #ifndef PLUGINS_SANDNET_SANDNETNODE_H_
22 #define PLUGINS_SANDNET_SANDNETNODE_H_
23 
24 #include <map>
25 #include <string>
26 #include <vector>
27 #include <utility>
28 #include "ola/Callback.h"
29 #include "ola/DmxBuffer.h"
30 #include "ola/network/IPV4Address.h"
31 #include "ola/network/InterfacePicker.h"
32 #include "ola/network/Socket.h"
34 #include "plugins/sandnet/SandNetPackets.h"
35 
36 namespace ola {
37 namespace plugin {
38 namespace sandnet {
39 
42 
43 class SandNetNode {
44  public:
45  typedef enum {
46  SANDNET_PORT_MODE_DISABLED,
47  SANDNET_PORT_MODE_OUT,
48  SANDNET_PORT_MODE_IN,
49  SANDNET_PORT_MODE_MOUT,
50  SANDNET_PORT_MODE_MIN
51  } sandnet_port_type;
52 
53  explicit SandNetNode(const string &preferred_ip);
54  ~SandNetNode();
55 
56  const ola::network::Interface &GetInterface() const {
57  return m_interface;
58  }
59 
60  void SetName(const string &name) {
61  m_node_name = name;
62  }
63  bool Start();
64  bool Stop();
65  std::vector<UDPSocket*> GetSockets();
66  void SocketReady(UDPSocket *socket);
67 
68  bool SetHandler(uint8_t group,
69  uint8_t universe,
70  DmxBuffer *buffer,
71  Callback0<void> *closure);
72  bool RemoveHandler(uint8_t group, uint8_t universe);
73 
74  bool SetPortParameters(uint8_t port_id, sandnet_port_type type,
75  uint8_t group, uint8_t universe);
76  bool SendAdvertisement();
77  bool SendDMX(uint8_t port_id, const DmxBuffer &buffer);
78 
79  private:
80  typedef struct {
81  uint8_t group;
82  uint8_t universe;
83  sandnet_port_type type;
84  } sandnet_port;
85 
86  typedef struct {
87  DmxBuffer *buffer;
88  Callback0<void> *closure;
89  } universe_handler;
90 
91  typedef std::pair<uint8_t, uint8_t> group_universe_pair;
92  typedef std::map<group_universe_pair, universe_handler> universe_handlers;
93 
94  bool InitNetwork();
95 
96  bool HandleCompressedDMX(const sandnet_compressed_dmx &dmx_packet,
97  unsigned int size);
98 
99  bool HandleDMX(const sandnet_dmx &dmx_packet,
100  unsigned int size);
101  bool SendUncompressedDMX(uint8_t port_id, const DmxBuffer &buffer);
102  bool SendPacket(const sandnet_packet &packet,
103  unsigned int size,
104  bool is_control = false);
105 
106  bool m_running;
107  string m_node_name;
108  string m_preferred_ip;
109 
110  sandnet_port m_ports[SANDNET_MAX_PORTS];
111  universe_handlers m_handlers;
112  ola::network::Interface m_interface;
113  UDPSocket m_control_socket;
114  UDPSocket m_data_socket;
115  ola::dmx::RunLengthEncoder m_encoder;
116  IPV4Address m_control_addr;
117  IPV4Address m_data_addr;
118 
119  static const uint16_t CONTROL_PORT;
120  static const uint16_t DATA_PORT;
121  static const char CONTROL_ADDRESS[];
122  static const char DATA_ADDRESS[];
123  static const char DEFAULT_NODE_NAME[];
124  static const uint32_t FIRMWARE_VERSION = 0x00050501;
125 };
126 } // namespace sandnet
127 } // namespace plugin
128 } // namespace ola
129 #endif // PLUGINS_SANDNET_SANDNETNODE_H_