Open Lighting Architecture  0.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Port.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  * Port.h
17  * Header file for the Port classes
18  * Copyright (C) 2005 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLAD_PORT_H_
22 #define INCLUDE_OLAD_PORT_H_
23 
24 #include <ola/DmxBuffer.h>
25 #include <ola/base/Macro.h>
26 #include <ola/rdm/RDMCommand.h>
28 #include <ola/timecode/TimeCode.h>
29 #include <olad/DmxSource.h>
30 #include <olad/PluginAdaptor.h>
31 #include <olad/PortConstants.h>
32 #include <olad/Universe.h>
33 
34 #include <string>
35 
36 namespace ola {
37 
38 class AbstractDevice;
39 
40 /*
41  * The base port class, all ports inherit from this.
42  */
43 class Port {
44  public:
45  virtual ~Port() {}
46 
47  // return the id of the port within this deivce
48  virtual unsigned int PortId() const = 0;
49 
50  // Return the device which owns this port
51  virtual AbstractDevice *GetDevice() const = 0;
52 
53  // return a short description of this port
54  virtual std::string Description() const = 0;
55 
56  // bind this port to a universe
57  virtual bool SetUniverse(Universe *universe) = 0;
58 
59  // return the universe that this port is bound to or NULL
60  virtual Universe *GetUniverse() const = 0;
61 
62  // Return a globally unique id of this port. This is used to preserve port
63  // universe bindings. An empty string means we don't preserve settings.
64  virtual std::string UniqueId() const = 0;
65 
66  // this tells us what sort of priority capabilities this port has
67  virtual port_priority_capability PriorityCapability() const = 0;
68 
69  virtual bool SetPriority(uint8_t priority) = 0;
70  virtual uint8_t GetPriority() const = 0;
71 
72  virtual void SetPriorityMode(port_priority_mode mode) = 0;
73  virtual port_priority_mode GetPriorityMode() const = 0;
74 
75  // If this port supports RDM or not
76  virtual bool SupportsRDM() const = 0;
77 };
78 
79 
80 /*
81  * The Input Port interface, for ports that provide push data into the OLA
82  * system.
83  */
84 class InputPort: public Port {
85  public:
86  virtual ~InputPort() {}
87 
88  // signal the port that the DMX data has changed
89  virtual void DmxChanged() = 0;
90 
91  // Get the current data
92  virtual const DmxSource &SourceData() const = 0;
93 
94  // Handle RDMRequests, ownership of the request object is transferred
95  virtual void HandleRDMRequest(const ola::rdm::RDMRequest *request,
96  ola::rdm::RDMCallback *callback) = 0;
97 };
98 
99 
100 /*
101  * The Output Port interface, for ports that send data from the OLA system.
102  */
104  public:
105  virtual ~OutputPort() {}
106 
107  // Write dmx data to this port
108  virtual bool WriteDMX(const DmxBuffer &buffer, uint8_t priority) = 0;
109 
110  // Called if the universe name changes
111  virtual void UniverseNameChanged(const std::string &new_name) = 0;
112 
113  // Methods from DiscoverableRDMControllerInterface
114  // Ownership of the request object is transferred
115  virtual void SendRDMRequest(const ola::rdm::RDMRequest *request,
116  ola::rdm::RDMCallback *callback) = 0;
117  virtual void RunFullDiscovery(
118  ola::rdm::RDMDiscoveryCallback *on_complete) = 0;
119  virtual void RunIncrementalDiscovery(
120  ola::rdm::RDMDiscoveryCallback *on_complete) = 0;
121 
122  // timecode support
123  virtual bool SupportsTimeCode() const = 0;
124  virtual bool SendTimeCode(const ola::timecode::TimeCode &timecode) = 0;
125 };
126 
127 
128 /*
129  * A Implementation of InputPort, provides the basic functionality which saves
130  * the plugin implementations from having to do it.
131  */
132 class BasicInputPort: public InputPort {
133  public:
135  unsigned int port_id,
136  const PluginAdaptor *plugin_adaptor,
137  bool supports_rdm = false);
138 
139  unsigned int PortId() const { return m_port_id; }
140  AbstractDevice *GetDevice() const { return m_device; }
141  bool SetUniverse(Universe *universe);
142  Universe *GetUniverse() const { return m_universe; }
143  virtual std::string UniqueId() const;
144  bool SetPriority(uint8_t priority);
145  uint8_t GetPriority() const { return m_priority; }
146  void SetPriorityMode(port_priority_mode mode) { m_priority_mode = mode; }
147  port_priority_mode GetPriorityMode() const { return m_priority_mode; }
148  void DmxChanged();
149  const DmxSource &SourceData() const { return m_dmx_source; }
150 
151  // rdm methods, the child class provides HandleRDMResponse
152  void HandleRDMRequest(const ola::rdm::RDMRequest *request,
153  ola::rdm::RDMCallback *callback);
154  void TriggerRDMDiscovery(ola::rdm::RDMDiscoveryCallback *on_complete,
155  bool full = true);
156 
157  port_priority_capability PriorityCapability() const {
158  return SupportsPriorities() ? CAPABILITY_FULL : CAPABILITY_STATIC;
159  }
160 
161  // subclasses override these
162  // Read the dmx data.
163  virtual const DmxBuffer &ReadDMX() const = 0;
164 
165  // Get the inherited priority
166  virtual uint8_t InheritedPriority() const {
168  }
169 
170  // override this to cancel the SetUniverse operation.
171  virtual bool PreSetUniverse(Universe *, Universe *) { return true; }
172 
173  virtual void PostSetUniverse(Universe *, Universe *) {}
174 
175  virtual bool SupportsRDM() const { return m_supports_rdm; }
176 
177  protected:
178  // indicates whether this port supports priorities, default to no
179  virtual bool SupportsPriorities() const { return false; }
180 
181  private:
182  const unsigned int m_port_id;
183  uint8_t m_priority;
184  port_priority_mode m_priority_mode;
185  mutable std::string m_port_string;
186  Universe *m_universe; // the universe this port belongs to
187  AbstractDevice *m_device;
188  DmxSource m_dmx_source;
189  const PluginAdaptor *m_plugin_adaptor;
190  bool m_supports_rdm;
191 
192  DISALLOW_COPY_AND_ASSIGN(BasicInputPort);
193 };
194 
195 
196 /*
197  * An implementation of an OutputPort.
198  */
200  public:
202  unsigned int port_id,
203  bool start_rdm_discovery_on_patch = false,
204  bool supports_rdm = false);
205 
206  unsigned int PortId() const { return m_port_id; }
207  AbstractDevice *GetDevice() const { return m_device; }
208  bool SetUniverse(Universe *universe);
209  Universe *GetUniverse() const { return m_universe; }
210  std::string UniqueId() const;
211  bool SetPriority(uint8_t priority);
212  uint8_t GetPriority() const { return m_priority; }
213  void SetPriorityMode(port_priority_mode mode) { m_priority_mode = mode; }
214  port_priority_mode GetPriorityMode() const { return m_priority_mode; }
215 
216  virtual void UniverseNameChanged(const std::string &new_name) {
217  (void) new_name;
218  }
219 
220  port_priority_capability PriorityCapability() const {
221  return SupportsPriorities() ? CAPABILITY_FULL : CAPABILITY_NONE;
222  }
223 
224  // DiscoverableRDMControllerInterface methods
225  virtual void SendRDMRequest(const ola::rdm::RDMRequest *request,
226  ola::rdm::RDMCallback *callback);
227  virtual void RunFullDiscovery(
228  ola::rdm::RDMDiscoveryCallback *on_complete);
229  virtual void RunIncrementalDiscovery(
230  ola::rdm::RDMDiscoveryCallback *on_complete);
231 
232  // TimeCode
233  virtual bool SupportsTimeCode() const {
234  return false;
235  }
236 
237  virtual bool SendTimeCode(const ola::timecode::TimeCode &) {
238  return true; // no op
239  }
240 
241  // Subclasses can override this to cancel the SetUniverse operation.
242  virtual bool PreSetUniverse(Universe *, Universe *) { return true; }
243  virtual void PostSetUniverse(Universe *, Universe *) { }
244 
245  virtual bool SupportsRDM() const { return m_supports_rdm; }
246 
247  protected:
248  // indicates whether this port supports priorities, default to no
249  virtual bool SupportsPriorities() const { return false; }
250  void UpdateUIDs(const ola::rdm::UIDSet &uids);
251 
252  private:
253  const unsigned int m_port_id;
254  const bool m_discover_on_patch;
255  uint8_t m_priority;
256  port_priority_mode m_priority_mode;
257  mutable std::string m_port_string;
258  Universe *m_universe; // the universe this port belongs to
259  AbstractDevice *m_device;
260  bool m_supports_rdm;
261 
262  DISALLOW_COPY_AND_ASSIGN(BasicOutputPort);
263 };
264 
265 
266 /*
267  * This allows switching based on Port type.
268  */
269 template<class PortClass>
270 bool IsInputPort();
271 
272 template<>
273 bool IsInputPort<OutputPort>();
274 } // namespace ola
275 #endif // INCLUDE_OLAD_PORT_H_