Debian Codegraph @

olad/plugin_api/Device.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 * Device.cpp
17 * Base implementation of the device class.
18 * Copyright (C) 2005 Simon Newton
19 */
20 #include <stdlib.h>
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <map>
25 #include <string>
26 #include <utility>
27 #include <vector>
28
29 #include "common/rpc/RpcController.h"
30 #include "common/rpc/RpcService.h"
31 #include "ola/Logging.h"
32 #include "ola/stl/STLUtils.h"
33 #include "olad/Device.h"
34 #include "olad/Plugin.h"
35 #include "olad/Port.h"
36 #include "olad/Universe.h"
37
38 namespace ola {
39
40 using std::map;
41 using std::pair;
42 using std::string;
43 using std::vector;
44
45 /*
46 * Create a new device
47 * @param owner the plugin that owns this device
48 * @param name a nice name for this device
49 */
50 Device::Device(AbstractPlugin *owner, const string &name)
51 : AbstractDevice(),
52 m_enabled(false),
53 m_owner(owner),
54 m_name(name) {
55 }
56
57
58 /*
59 * Stop this device
60 */
61 Device::~Device() {
62 // we can't call stop from here because it uses virtual methods
63 if (m_enabled)
64 OLA_FATAL << "Device " << m_name << " wasn't stopped before deleting, " <<
65 "this represents a serious programming error.";
66 }
67
68
69 /*
70 * Start the Device
71 */
72 bool Device::Start() {
73 if (m_enabled)
74 return true;
75
76 bool ret = StartHook();
77 if (ret)
78 m_enabled = true;
79 return ret;
80 }
81
82
83 /*
84 * Stop this device
85 */
86 bool Device::Stop() {
87 if (!m_enabled)
88 return true;
89
90 PrePortStop();
91 DeleteAllPorts();
92 PostPortStop();
93
94 m_enabled = false;
95 return true;
96 }
97
98
99 /*
100 * Add a port to this device
101 * @param port the port to add
102 * @return true on success, false if the port already exists
103 */
104 bool Device::AddPort(InputPort *port) {
105 return GenericAddPort(port, &m_input_ports);
106 }
107
108
109 /*
110 * Add a port to this device
111 * @param port the port to add
112 * @return 0 on success, non 0 on failure
113 */
114 bool Device::AddPort(OutputPort *port) {
115 return GenericAddPort(port, &m_output_ports);
116 }
117
118 void Device::InputPorts(vector<InputPort*> *ports) const {
119 STLValues(m_input_ports, ports);
120 }
121
122
123 void Device::OutputPorts(vector<OutputPort*> *ports) const {
124 STLValues(m_output_ports, ports);
125 }
126
127 /*
128 * Returns the InputPort with the id port_id
129 */
130 InputPort *Device::GetInputPort(unsigned int port_id) const {
131 return STLFindOrNull(m_input_ports, port_id);
132 }
133
134
135 /*
136 * Returns the OutputPort with the id port_id
137 */
138 OutputPort *Device::GetOutputPort(unsigned int port_id) const {
139 return STLFindOrNull(m_output_ports, port_id);
140 }
141
142
143 /*
144 * Delete all ports and clear the port list
145 */
146 void Device::DeleteAllPorts() {
147 map<unsigned int, InputPort*>::iterator input_iter;
148 map<unsigned int, OutputPort*>::iterator output_iter;
149 for (input_iter = m_input_ports.begin(); input_iter != m_input_ports.end();
150 ++input_iter) {
151 GenericDeletePort(input_iter->second);
152 }
153 for (output_iter = m_output_ports.begin();
154 output_iter != m_output_ports.end(); ++output_iter) {
155 GenericDeletePort(output_iter->second);
156 }
157 m_input_ports.clear();
158 m_output_ports.clear();
159 }
160
161
162 /*
163 * Returns a unique id for this device
164 */
165 string Device::UniqueId() const {
166 if (m_unique_id.empty()) {
167 if (!Owner()) {
168 OLA_WARN << "Device: " << Name() << " missing owner";
169 return "";
170 }
171
172 std::ostringstream str;
173 str << Owner()->Id() << "-" << DeviceId();
174 m_unique_id = str.str();
175 }
176 return m_unique_id;
177 }
178
179 /*
180 * Device Config request
181 */
182 void Device::Configure(ola::rpc::RpcController *controller,
183 const string &request,
184 string *response,
185 ConfigureCallback *done) {
186 controller->SetFailed("Not Implemented");
187 done->Run();
188 (void) request;
189 (void) response;
190 }
191
192
193 template<class PortClass>
194 bool Device::GenericAddPort(PortClass *port,
195 map<unsigned int, PortClass*> *port_map) {
196 if (!port)
197 return false;
198
199 if (!STLInsertIfNotPresent(port_map, port->PortId(), port)) {
200 OLA_WARN << "Attempt to insert a port but this port id is already "
201 << "associated with a different port.";
202 }
203 return true;
204 }
205
206
207 template <class PortClass>
208 void Device::GenericDeletePort(PortClass *port) {
209 Universe *universe = port->GetUniverse();
210 if (universe)
211 universe->RemovePort(port);
212 delete port;
213 }
214 } // namespace ola