Open Lighting Architecture  0.9.0
 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 class ShowNetNode {
38  public:
39  explicit ShowNetNode(const std::string &ip_address);
40  virtual ~ShowNetNode();
41 
42  bool Start();
43  bool Stop();
44  void SetName(const std::string &name);
45 
46  bool SendDMX(unsigned int universe, const ola::DmxBuffer &buffer);
47  bool SetHandler(unsigned int universe,
48  DmxBuffer *buffer,
49  ola::Callback0<void> *handler);
50  bool RemoveHandler(unsigned int universe);
51 
52  const ola::network::Interface &GetInterface() const {
53  return m_interface;
54  }
55 
56  ola::network::UDPSocket* GetSocket() { return m_socket; }
57  void SocketReady();
58 
59  static const uint16_t SHOWNET_MAX_UNIVERSES = 8;
60 
61  friend class ShowNetNodeTest;
62 
63  private:
64  typedef struct {
65  DmxBuffer *buffer;
66  Callback0<void> *closure;
67  } universe_handler;
68 
69  bool m_running;
70  uint16_t m_packet_count;
71  std::string m_node_name;
72  std::string m_preferred_ip;
73  std::map<unsigned int, universe_handler> m_handlers;
74  ola::network::Interface m_interface;
76  ola::network::UDPSocket *m_socket;
77 
78  ShowNetNode(const ShowNetNode&);
79  ShowNetNode& operator=(const ShowNetNode&);
80  bool HandlePacket(const shownet_data_packet &packet, unsigned int size);
81  unsigned int PopulatePacket(shownet_data_packet *packet,
82  unsigned int universe,
83  const DmxBuffer &buffer);
84  bool InitNetwork();
85  inline uint8_t ShortGetHigh(uint16_t x) const { return (0xff00 & x) >> 8; }
86  inline uint8_t ShortGetLow(uint16_t x) const { return 0x00ff & x; }
87 
88  static const uint16_t SHOWNET_PORT = 2501;
89  static const uint8_t SHOWNET_ID_HIGH = 0x80;
90  static const uint8_t SHOWNET_ID_LOW = 0x8f;
91  static const int MAGIC_INDEX_OFFSET = 11;
92 };
93 } // namespace shownet
94 } // namespace plugin
95 } // namespace ola
96 #endif // PLUGINS_SHOWNET_SHOWNETNODE_H_