Open Lighting Architecture  Latest Git
OSCNode.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  * OSCNode.h
17  * A DMX orientated, C++ wrapper around liblo.
18  * Copyright (C) 2012 Simon Newton
19  */
20 
21 #ifndef PLUGINS_OSC_OSCNODE_H_
22 #define PLUGINS_OSC_OSCNODE_H_
23 
24 #include <lo/lo.h>
25 #include <ola/DmxBuffer.h>
26 #include <ola/ExportMap.h>
27 #include <ola/base/Macro.h>
28 #include <ola/io/Descriptor.h>
29 #include <ola/io/SelectServerInterface.h>
31 #include <stdint.h>
32 #include <map>
33 #include <memory>
34 #include <string>
35 #include <vector>
36 #include "plugins/osc/OSCTarget.h"
37 
38 namespace ola {
39 namespace plugin {
40 namespace osc {
41 
70 class OSCNode {
71  public:
72  // The different data formats we can send in.
73  enum DataFormat {
74  FORMAT_BLOB,
75  FORMAT_INT_ARRAY,
76  FORMAT_INT_INDIVIDUAL,
77  FORMAT_FLOAT_ARRAY,
78  FORMAT_FLOAT_INDIVIDUAL,
79  };
80 
81  // The options for the OSCNode object.
82  struct OSCNodeOptions {
83  uint16_t listen_port; // UDP port to listen on
84 
85  OSCNodeOptions() : listen_port(DEFAULT_OSC_PORT) {}
86  };
87 
88  // The callback run when we receive new DMX data.
90 
92  ola::ExportMap *export_map,
93  const OSCNodeOptions &options);
94  ~OSCNode();
95 
96  bool Init();
97  void Stop();
98 
99  // Sending methods
100  void AddTarget(unsigned int group, const OSCTarget &target);
101  bool RemoveTarget(unsigned int group, const OSCTarget &target);
102  bool SendData(unsigned int group, DataFormat data_format,
103  const ola::DmxBuffer &data);
104 
105  // Receiving methods
106  bool RegisterAddress(const std::string &osc_address, DMXCallback *callback);
107 
108  // Called by the liblo handlers.
109  void SetUniverse(const std::string &osc_address, const uint8_t *data,
110  unsigned int size);
111  void SetSlot(const std::string &osc_address, uint16_t slot, uint8_t value);
112 
113  // The port OSC is listening on.
114  uint16_t ListeningPort() const;
115 
116  private:
117  class NodeOSCTarget {
118  public:
119  explicit NodeOSCTarget(const OSCTarget &target);
120  ~NodeOSCTarget();
121 
122  bool operator==(const NodeOSCTarget &other) const {
123  return (socket_address == other.socket_address &&
124  osc_address == other.osc_address);
125  }
126 
127  bool operator==(const OSCTarget &other) const {
128  return (socket_address == other.socket_address &&
129  osc_address == other.osc_address);
130  }
131 
132  ola::network::IPV4SocketAddress socket_address;
133  std::string osc_address;
134  lo_address liblo_address;
135 
136  private:
137  DISALLOW_COPY_AND_ASSIGN(NodeOSCTarget);
138  };
139 
140  typedef std::vector<NodeOSCTarget*> OSCTargetVector;
141 
142  struct OSCOutputGroup {
143  OSCTargetVector targets;
144  DmxBuffer dmx; // holds the last values.
145  };
146 
147  struct OSCInputGroup {
148  explicit OSCInputGroup(DMXCallback *callback) : callback(callback) {}
149 
150  DmxBuffer dmx;
151  std::auto_ptr<DMXCallback> callback;
152  };
153 
154  typedef std::map<unsigned int, OSCOutputGroup*> OutputGroupMap;
155  typedef std::map<std::string, OSCInputGroup*> InputUniverseMap;
156 
157  struct SlotMessage {
158  unsigned int slot;
159  lo_message message;
160  };
161 
163  const uint16_t m_listen_port;
164  std::auto_ptr<ola::io::UnmanagedFileDescriptor> m_descriptor;
165  lo_server m_osc_server;
166  OutputGroupMap m_output_map;
167  InputUniverseMap m_input_map;
168 
169  void DescriptorReady();
170  bool SendBlob(const DmxBuffer &data, const OSCTargetVector &targets);
171  bool SendIndividualFloats(const DmxBuffer &data,
172  OSCOutputGroup *group);
173  bool SendIndividualInts(const DmxBuffer &data,
174  OSCOutputGroup *group);
175  bool SendIntArray(const DmxBuffer &data,
176  const OSCTargetVector &targets);
177  bool SendFloatArray(const DmxBuffer &data,
178  const OSCTargetVector &targets);
179  bool SendMessageToTargets(lo_message message,
180  const OSCTargetVector &targets);
181  bool SendIndividualMessages(const DmxBuffer &data,
182  OSCOutputGroup *group,
183  const std::string &osc_type);
184 
185  static const uint16_t DEFAULT_OSC_PORT = 7770;
186  static const char OSC_PORT_VARIABLE[];
187 };
188 } // namespace osc
189 } // namespace plugin
190 } // namespace ola
191 #endif // PLUGINS_OSC_OSCNODE_H_
Represents Socket Addresses.
bool RemoveTarget(unsigned int group, const OSCTarget &target)
Definition: OSCNode.cpp:342
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Creates dummy copy constructor and assignment operator declarations.
Definition: Macro.h:44
Definition: OSCTarget.h:31
A container for the exported variables.
Definition: ExportMap.h:324
~OSCNode()
Definition: OSCNode.cpp:227
bool RegisterAddress(const std::string &osc_address, DMXCallback *callback)
Definition: OSCNode.cpp:408
Used to hold a single universe of DMX data.
Definition: DmxBuffer.h:49
Export variables on the http server.
void SetUniverse(const std::string &osc_address, const uint8_t *data, unsigned int size)
Definition: OSCNode.cpp:438
Definition: OSCNode.h:70
A class used to hold a single universe of DMX data.
The interface for the SelectServer.
Definition: SelectServerInterface.h:42
OSCNode(ola::io::SelectServerInterface *ss, ola::ExportMap *export_map, const OSCNodeOptions &options)
Definition: OSCNode.cpp:209
void SetSlot(const std::string &osc_address, uint16_t slot, uint8_t value)
Definition: OSCNode.cpp:456
void AddTarget(unsigned int group, const OSCTarget &target)
Definition: OSCNode.cpp:309
Helper macros.
bool SendData(unsigned int group, DataFormat data_format, const ola::DmxBuffer &data)
Definition: OSCNode.cpp:371
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
uint16_t ListeningPort() const
Definition: OSCNode.cpp:472
An IPv4 SocketAddress.
Definition: SocketAddress.h:78
void Stop()
Definition: OSCNode.cpp:274
A 1 argument callback which can be called multiple times.
Definition: Callback.h:992