Debian Codegraph @

olad/plugin_api/Port.cpp

raw
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.cpp
17 * Base implementation of the Port class.
18 * Copyright (C) 2005 Simon Newton
19 *
20 * Unfortunately this file contains a lot of code duplication.
21 */
22
23 #include <memory>
24 #include <string>
25 #include <vector>
26 #include "ola/Logging.h"
27 #include "ola/rdm/UIDSet.h"
28 #include "olad/Device.h"
29 #include "olad/Port.h"
30 #include "olad/PortBroker.h"
31
32 namespace ola {
33
34 using std::auto_ptr;
35 using std::string;
36 using std::vector;
37
38 BasicInputPort::BasicInputPort(AbstractDevice *parent,
39 unsigned int port_id,
40 const PluginAdaptor *plugin_adaptor,
41 bool supports_rdm):
42 m_port_id(port_id),
43 m_priority(ola::dmx::SOURCE_PRIORITY_DEFAULT),
44 m_priority_mode(PRIORITY_MODE_STATIC),
45 m_port_string(""),
46 m_universe(NULL),
47 m_device(parent),
48 m_plugin_adaptor(plugin_adaptor),
49 m_supports_rdm(supports_rdm) {
50 }
51
52 bool BasicInputPort::SetUniverse(Universe *new_universe) {
53 Universe *old_universe = GetUniverse();
54 if (old_universe == new_universe)
55 return true;
56
57 if (PreSetUniverse(old_universe, new_universe)) {
58 m_universe = new_universe;
59 PostSetUniverse(old_universe, new_universe);
60 return true;
61 }
62 return false;
63 }
64
65 string BasicInputPort::UniqueId() const {
66 if (m_port_string.empty()) {
67 std::ostringstream str;
68 if (m_device)
69 str << m_device->UniqueId() << "-I-" << m_port_id;
70 m_port_string = str.str();
71 }
72 return m_port_string;
73 }
74
75 bool BasicInputPort::SetPriority(uint8_t priority) {
76 if (priority > ola::dmx::SOURCE_PRIORITY_MAX)
77 return false;
78
79 m_priority = priority;
80 return true;
81 }
82
83 void BasicInputPort::DmxChanged() {
84 if (GetUniverse()) {
85 const DmxBuffer &buffer = ReadDMX();
86 uint8_t priority = (PriorityCapability() == CAPABILITY_FULL &&
87 GetPriorityMode() == PRIORITY_MODE_INHERIT ?
88 InheritedPriority() :
89 GetPriority());
90 m_dmx_source.UpdateData(buffer, *m_plugin_adaptor->WakeUpTime(), priority);
91 GetUniverse()->PortDataChanged(this);
92 }
93 }
94
95 void BasicInputPort::HandleRDMRequest(ola::rdm::RDMRequest *request_ptr,
96 ola::rdm::RDMCallback *callback) {
97 auto_ptr<ola::rdm::RDMRequest> request(request_ptr);
98 if (m_universe) {
99 m_plugin_adaptor->GetPortBroker()->SendRDMRequest(
100 this,
101 m_universe,
102 request.release(),
103 callback);
104 } else {
105 ola::rdm::RunRDMCallback(callback, ola::rdm::RDM_FAILED_TO_SEND);
106 }
107 }
108
109 void BasicInputPort::TriggerRDMDiscovery(
110 ola::rdm::RDMDiscoveryCallback *on_complete,
111 bool full) {
112 if (m_universe) {
113 m_universe->RunRDMDiscovery(on_complete, full);
114 } else {
115 ola::rdm::UIDSet uids;
116 on_complete->Run(uids);
117 }
118 }
119
120 BasicOutputPort::BasicOutputPort(AbstractDevice *parent,
121 unsigned int port_id,
122 bool start_rdm_discovery_on_patch,
123 bool supports_rdm):
124 m_port_id(port_id),
125 m_discover_on_patch(start_rdm_discovery_on_patch),
126 m_priority(ola::dmx::SOURCE_PRIORITY_DEFAULT),
127 m_priority_mode(PRIORITY_MODE_INHERIT),
128 m_port_string(""),
129 m_universe(NULL),
130 m_device(parent),
131 m_supports_rdm(supports_rdm) {
132 }
133
134 bool BasicOutputPort::SetUniverse(Universe *new_universe) {
135 Universe *old_universe = GetUniverse();
136 if (old_universe == new_universe)
137 return true;
138
139 if (PreSetUniverse(old_universe, new_universe)) {
140 m_universe = new_universe;
141 PostSetUniverse(old_universe, new_universe);
142 if (m_discover_on_patch)
143 RunIncrementalDiscovery(
144 NewSingleCallback(this, &BasicOutputPort::UpdateUIDs));
145 return true;
146 }
147 return false;
148 }
149
150 string BasicOutputPort::UniqueId() const {
151 if (m_port_string.empty()) {
152 std::ostringstream str;
153 if (m_device)
154 str << m_device->UniqueId() << "-O-" << m_port_id;
155 m_port_string = str.str();
156 }
157 return m_port_string;
158 }
159
160 bool BasicOutputPort::SetPriority(uint8_t priority) {
161 if (priority > ola::dmx::SOURCE_PRIORITY_MAX)
162 return false;
163
164 m_priority = priority;
165 return true;
166 }
167
168 void BasicOutputPort::SendRDMRequest(ola::rdm::RDMRequest *request_ptr,
169 ola::rdm::RDMCallback *callback) {
170 auto_ptr<ola::rdm::RDMRequest> request(request_ptr);
171
172 // broadcasts go to every port
173 if (request->DestinationUID().IsBroadcast()) {
174 ola::rdm::RunRDMCallback(callback, ola::rdm::RDM_WAS_BROADCAST);
175 } else {
176 OLA_WARN << "In base HandleRDMRequest, something has gone wrong with RDM"
177 << " request routing";
178 ola::rdm::RunRDMCallback(callback, ola::rdm::RDM_FAILED_TO_SEND);
179 }
180 }
181
182 void BasicOutputPort::RunFullDiscovery(
183 ola::rdm::RDMDiscoveryCallback *on_complete) {
184 ola::rdm::UIDSet uids;
185 on_complete->Run(uids);
186 }
187
188 void BasicOutputPort::RunIncrementalDiscovery(
189 ola::rdm::RDMDiscoveryCallback *on_complete) {
190 ola::rdm::UIDSet uids;
191 on_complete->Run(uids);
192 }
193
194 void BasicOutputPort::UpdateUIDs(const ola::rdm::UIDSet &uids) {
195 Universe *universe = GetUniverse();
196 if (universe)
197 universe->NewUIDList(this, uids);
198 }
199
200 template<class PortClass>
201 bool IsInputPort() {
202 return true;
203 }
204
205 template<>
206 bool IsInputPort<OutputPort>() {
207 return false;
208 }
209
210 template<>
211 bool IsInputPort<InputPort>() {
212 return true;
213 }
214 } // namespace ola