Open Lighting Architecture
 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  * Port.h
17  * Header file for the Port classes
18  * Copyright (C) 2005-2010 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/rdm/RDMCommand.h>
27 #include <ola/timecode/TimeCode.h>
28 #include <olad/DmxSource.h>
29 #include <olad/PluginAdaptor.h>
30 #include <olad/PortConstants.h>
31 #include <olad/Universe.h>
32 
33 #include <string>
34 #include <vector>
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 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 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 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 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 {
167  return DmxSource::PRIORITY_MIN;
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 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 
193  BasicInputPort& operator=(const BasicInputPort&);
194 };
195 
196 
197 /*
198  * An implementation of an OutputPort.
199  */
201  public:
203  unsigned int port_id,
204  bool start_rdm_discovery_on_patch = false,
205  bool supports_rdm = false);
206 
207  unsigned int PortId() const { return m_port_id; }
208  AbstractDevice *GetDevice() const { return m_device; }
209  bool SetUniverse(Universe *universe);
210  Universe *GetUniverse() const { return m_universe; }
211  string UniqueId() const;
212  bool SetPriority(uint8_t priority);
213  uint8_t GetPriority() const { return m_priority; }
214  void SetPriorityMode(port_priority_mode mode) { m_priority_mode = mode; }
215  port_priority_mode GetPriorityMode() const { return m_priority_mode; }
216 
217  virtual void UniverseNameChanged(const string &new_name) {
218  (void) new_name;
219  }
220 
221  port_priority_capability PriorityCapability() const {
222  return SupportsPriorities() ? CAPABILITY_FULL : CAPABILITY_NONE;
223  }
224 
225  // DiscoverableRDMControllerInterface methods
226  virtual void SendRDMRequest(const ola::rdm::RDMRequest *request,
227  ola::rdm::RDMCallback *callback);
228  virtual void RunFullDiscovery(
229  ola::rdm::RDMDiscoveryCallback *on_complete);
230  virtual void RunIncrementalDiscovery(
231  ola::rdm::RDMDiscoveryCallback *on_complete);
232 
233  // TimeCode
234  virtual bool SupportsTimeCode() const {
235  return false;
236  }
237 
238  virtual bool SendTimeCode(const ola::timecode::TimeCode &) {
239  return true; // no op
240  }
241 
242  // Subclasses can override this to cancel the SetUniverse operation.
243  virtual bool PreSetUniverse(Universe *, Universe *) { return true; }
244  virtual void PostSetUniverse(Universe *, Universe *) { }
245 
246  virtual bool SupportsRDM() const { return m_supports_rdm; }
247 
248  protected:
249  // indicates whether this port supports priorities, default to no
250  virtual bool SupportsPriorities() const { return false; }
251  void UpdateUIDs(const ola::rdm::UIDSet &uids);
252 
253  private:
254  const unsigned int m_port_id;
255  const bool m_discover_on_patch;
256  uint8_t m_priority;
257  port_priority_mode m_priority_mode;
258  mutable string m_port_string;
259  Universe *m_universe; // the universe this port belongs to
260  AbstractDevice *m_device;
261  bool m_supports_rdm;
262 
264  BasicOutputPort& operator=(const BasicOutputPort&);
265 };
266 
267 
268 /*
269  * This allows switching based on Port type.
270  */
271 template<class PortClass>
272 bool IsInputPort();
273 
274 template<>
275 bool IsInputPort<OutputPort>();
276 } // namespace ola
277 #endif // INCLUDE_OLAD_PORT_H_