Debian Codegraph @

olad/plugin_api/DeviceManager.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 * DeviceManager.cpp
17 * Implementation of the Device Manager, this object tracks what devices are in
18 * use.
19 * Copyright (C) 2005 Simon Newton
20 */
21
22 #include "olad/plugin_api/DeviceManager.h"
23
24 #include <stdio.h>
25 #include <errno.h>
26 #include <map>
27 #include <set>
28 #include <string>
29 #include <vector>
30
31 #include "ola/Logging.h"
32 #include "ola/StringUtils.h"
33 #include "ola/stl/STLUtils.h"
34 #include "olad/Port.h"
35 #include "olad/plugin_api/PortManager.h"
36
37 namespace ola {
38
39 using std::map;
40 using std::set;
41 using std::string;
42 using std::vector;
43
44 const unsigned int DeviceManager::MISSING_DEVICE_ALIAS = 0;
45 const char DeviceManager::PORT_PREFERENCES[] = "port";
46 const char DeviceManager::PRIORITY_VALUE_SUFFIX[] = "_priority_value";
47 const char DeviceManager::PRIORITY_MODE_SUFFIX[] = "_priority_mode";
48
49 bool operator <(const device_alias_pair& left,
50 const device_alias_pair &right) {
51 if (left.alias < right.alias)
52 return true;
53 return false;
54 }
55
56 DeviceManager::DeviceManager(PreferencesFactory *prefs_factory,
57 PortManager *port_manager)
58 : m_port_preferences(NULL),
59 m_port_manager(port_manager),
60 m_next_device_alias(FIRST_DEVICE_ALIAS) {
61 if (prefs_factory) {
62 m_port_preferences = prefs_factory->NewPreference(PORT_PREFERENCES);
63 m_port_preferences->Load();
64 }
65 }
66
67 DeviceManager::~DeviceManager() {
68 if (m_port_preferences) {
69 m_port_preferences->Save();
70 }
71 }
72
73 bool DeviceManager::RegisterDevice(AbstractDevice *device) {
74 if (!device) {
75 return false;
76 }
77
78 const string device_id = device->UniqueId();
79
80 if (device_id.empty()) {
81 OLA_WARN << "Device: " << device->Name() << " is missing UniqueId";
82 return false;
83 }
84
85 // See if we already have an alias for this device.
86 unsigned int alias;
87 DeviceIdMap::iterator iter = m_devices.find(device_id);
88 if (iter != m_devices.end()) {
89 if (iter->second.device) {
90 // already registered
91 OLA_INFO << "Device " << device_id << " is already registered";
92 return false;
93 } else {
94 // was previously registered, reuse alias
95 alias = iter->second.alias;
96 iter->second.device = device;
97 }
98 } else {
99 alias = m_next_device_alias++;
100 device_alias_pair pair(alias, device);
101 STLReplace(&m_devices, device_id, pair);
102 }
103
104 STLReplace(&m_alias_map, alias, device);
105 OLA_INFO << "Installed device: " << device->Name() << ":"
106 << device->UniqueId();
107
108 vector<InputPort*> input_ports;
109 device->InputPorts(&input_ports);
110 RestorePortSettings(input_ports);
111
112 vector<OutputPort*> output_ports;
113 device->OutputPorts(&output_ports);
114 RestorePortSettings(output_ports);
115
116 // look for timecode ports and add them to the set
117 vector<OutputPort*>::const_iterator output_iter = output_ports.begin();
118 for (; output_iter != output_ports.end(); ++output_iter) {
119 if ((*output_iter)->SupportsTimeCode()) {
120 m_timecode_ports.insert(*output_iter);
121 }
122 }
123
124 return true;
125 }
126
127 bool DeviceManager::UnregisterDevice(const string &device_id) {
128 device_alias_pair *pair = STLFind(&m_devices, device_id);
129 if (!pair || !pair->device) {
130 OLA_WARN << "Device " << device_id << "not found";
131 return false;
132 }
133
134 ReleaseDevice(pair->device);
135 STLRemove(&m_alias_map, pair->alias);
136
137 pair->device = NULL;
138 return true;
139 }
140
141 bool DeviceManager::UnregisterDevice(const AbstractDevice *device) {
142 if (!device) {
143 return false;
144 }
145
146 string device_id = device->UniqueId();
147 if (device_id.empty()) {
148 return false;
149 }
150
151 return UnregisterDevice(device_id);
152 }
153
154 unsigned int DeviceManager::DeviceCount() const {
155 return m_alias_map.size();
156 }
157
158 vector<device_alias_pair> DeviceManager::Devices() const {
159 vector<device_alias_pair> result;
160 map<string, device_alias_pair>::const_iterator iter;
161 for (iter = m_devices.begin(); iter != m_devices.end(); ++iter) {
162 if (iter->second.device) {
163 result.push_back(iter->second);
164 }
165 }
166 return result;
167 }
168
169 AbstractDevice *DeviceManager::GetDevice(unsigned int alias) const {
170 return STLFindOrNull(m_alias_map, alias);
171 }
172
173 device_alias_pair DeviceManager::GetDevice(const string &unique_id) const {
174 const device_alias_pair *result = STLFind(&m_devices, unique_id);
175 if (result) {
176 return *result;
177 } else {
178 return device_alias_pair(MISSING_DEVICE_ALIAS, NULL);
179 }
180 }
181
182 void DeviceManager::UnregisterAllDevices() {
183 DeviceIdMap::iterator iter;
184 for (iter = m_devices.begin(); iter != m_devices.end(); ++iter) {
185 ReleaseDevice(iter->second.device);
186 iter->second.device = NULL;
187 }
188 m_alias_map.clear();
189 }
190
191 void DeviceManager::SendTimeCode(const ola::timecode::TimeCode &timecode) {
192 set<OutputPort*>::iterator iter = m_timecode_ports.begin();
193 for (; iter != m_timecode_ports.end(); iter++) {
194 (*iter)->SendTimeCode(timecode);
195 }
196 }
197
198 /*
199 * Save the port universe patchings for a device
200 * @param device the device to save the settings for
201 */
202 void DeviceManager::ReleaseDevice(const AbstractDevice *device) {
203 if (!m_port_preferences || !device) {
204 return;
205 }
206
207 vector<InputPort*> input_ports;
208 vector<OutputPort*> output_ports;
209 device->InputPorts(&input_ports);
210 device->OutputPorts(&output_ports);
211 SavePortPatchings(input_ports);
212 SavePortPatchings(output_ports);
213
214 vector<InputPort*>::const_iterator input_iter = input_ports.begin();
215 for (; input_iter != input_ports.end(); ++input_iter) {
216 SavePortPriority(**input_iter);
217 }
218
219 vector<OutputPort*>::const_iterator output_iter = output_ports.begin();
220 for (; output_iter != output_ports.end(); ++output_iter) {
221 SavePortPriority(**output_iter);
222
223 // remove from the timecode port set
224 STLRemove(&m_timecode_ports, *output_iter);
225 }
226 }
227
228
229 /*
230 * Save the patching information for a list of ports.
231 */
232 template <class PortClass>
233 void DeviceManager::SavePortPatchings(const vector<PortClass*> &ports) const {
234 typename vector<PortClass*>::const_iterator iter = ports.begin();
235 while (iter != ports.end()) {
236 string port_id = (*iter)->UniqueId();
237 if (port_id.empty()) {
238 return;
239 }
240
241 if ((*iter)->GetUniverse()) {
242 m_port_preferences->SetValue(
243 port_id,
244 IntToString((*iter)->GetUniverse()->UniverseId()));
245 } else {
246 m_port_preferences->RemoveValue(port_id);
247 }
248 iter++;
249 }
250 }
251
252
253 /*
254 * Save the priorities for all ports on this device
255 */
256 void DeviceManager::SavePortPriority(const Port &port) const {
257 if (port.PriorityCapability() == CAPABILITY_NONE) {
258 return;
259 }
260
261 string port_id = port.UniqueId();
262 if (port_id.empty()) {
263 return;
264 }
265
266 m_port_preferences->SetValue(port_id + PRIORITY_VALUE_SUFFIX,
267 IntToString(port.GetPriority()));
268
269 if (port.PriorityCapability() == CAPABILITY_FULL) {
270 m_port_preferences->SetValue(port_id + PRIORITY_MODE_SUFFIX,
271 IntToString(port.GetPriorityMode()));
272 }
273 }
274
275
276 /*
277 * Restore the priority settings for a port
278 */
279 void DeviceManager::RestorePortPriority(Port *port) const {
280 if (port->PriorityCapability() == CAPABILITY_NONE) {
281 return;
282 }
283
284 string port_id = port->UniqueId();
285 if (port_id.empty()) {
286 return;
287 }
288
289 string priority_str = m_port_preferences->GetValue(
290 port_id + PRIORITY_VALUE_SUFFIX);
291 string priority_mode_str = m_port_preferences->GetValue(
292 port_id + PRIORITY_MODE_SUFFIX);
293
294 if (priority_str.empty() && priority_mode_str.empty()) {
295 return;
296 }
297
298 uint8_t priority, priority_mode;
299 // setting the priority to override mode first means we remember the over
300 // value even if it's in inherit mode
301 if (StringToInt(priority_str, &priority)) {
302 m_port_manager->SetPriorityStatic(port, priority);
303 }
304
305 if (StringToInt(priority_mode_str, &priority_mode)) {
306 if (priority_mode == PRIORITY_MODE_INHERIT) {
307 m_port_manager->SetPriorityInherit(port);
308 }
309 }
310 }
311
312
313 /*
314 * Restore the patching information for a port.
315 */
316 template <class PortClass>
317 void DeviceManager::RestorePortSettings(const vector<PortClass*> &ports) const {
318 if (!m_port_preferences) {
319 return;
320 }
321
322 typename vector<PortClass*>::const_iterator iter = ports.begin();
323 while (iter != ports.end()) {
324 RestorePortPriority(*iter);
325 PortClass *port = *iter;
326 iter++;
327
328 string port_id = port->UniqueId();
329 if (port_id.empty())
330 continue;
331
332 string uni_id = m_port_preferences->GetValue(port_id);
333 if (uni_id.empty())
334 continue;
335
336 errno = 0;
337 int id = static_cast<int>(strtol(uni_id.c_str(), NULL, 10));
338 if ((id == 0 && errno) || id < 0)
339 continue;
340
341 m_port_manager->PatchPort(port, id);
342 }
343 }
344 } // namespace ola