Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ShowNetNode.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  * ShowNetNode.h
17  * Header file for the ShowNetNode class
18  * Copyright (C) 2005-2009 Simon Newton
19  */
20 
21 #ifndef PLUGINS_SHOWNET_SHOWNETNODE_H_
22 #define PLUGINS_SHOWNET_SHOWNETNODE_H_
23 
24 #include <string>
25 #include <map>
26 #include "ola/Callback.h"
27 #include "ola/DmxBuffer.h"
29 #include "ola/network/InterfacePicker.h"
30 #include "ola/network/Socket.h"
31 #include "plugins/shownet/ShowNetPackets.h"
32 
33 namespace ola {
34 namespace plugin {
35 namespace shownet {
36 
37 using std::string;
38 
39 class ShowNetNode {
40  public:
41  explicit ShowNetNode(const string &ip_address);
42  virtual ~ShowNetNode();
43 
44  bool Start();
45  bool Stop();
46  void SetName(const string &name);
47 
48  bool SendDMX(unsigned int universe, const ola::DmxBuffer &buffer);
49  bool SetHandler(unsigned int universe,
50  DmxBuffer *buffer,
51  ola::Callback0<void> *handler);
52  bool RemoveHandler(unsigned int universe);
53 
54  const ola::network::Interface &GetInterface() const {
55  return m_interface;
56  }
57 
58  ola::network::UDPSocket* GetSocket() { return m_socket; }
59  void SocketReady();
60 
61  static const uint16_t SHOWNET_MAX_UNIVERSES = 8;
62 
63  friend class ShowNetNodeTest;
64 
65  private:
66  typedef struct {
67  DmxBuffer *buffer;
68  Callback0<void> *closure;
69  } universe_handler;
70 
71  bool m_running;
72  uint16_t m_packet_count;
73  string m_node_name;
74  string m_preferred_ip;
75  std::map<unsigned int, universe_handler> m_handlers;
76  ola::network::Interface m_interface;
78  ola::network::UDPSocket *m_socket;
79 
80  ShowNetNode(const ShowNetNode&);
81  ShowNetNode& operator=(const ShowNetNode&);
82  bool HandlePacket(const shownet_data_packet &packet, unsigned int size);
83  unsigned int PopulatePacket(shownet_data_packet *packet,
84  unsigned int universe,
85  const DmxBuffer &buffer);
86  bool InitNetwork();
87  inline uint8_t ShortGetHigh(uint16_t x) const { return (0xff00 & x) >> 8; }
88  inline uint8_t ShortGetLow(uint16_t x) const { return 0x00ff & x; }
89 
90  static const uint16_t SHOWNET_PORT = 2501;
91  static const uint8_t SHOWNET_ID_HIGH = 0x80;
92  static const uint8_t SHOWNET_ID_LOW = 0x8f;
93  static const int MAGIC_INDEX_OFFSET = 11;
94 };
95 } // namespace shownet
96 } // namespace plugin
97 } // namespace ola
98 #endif // PLUGINS_SHOWNET_SHOWNETNODE_H_