File: E131Device.cpp

package info (click to toggle)
ola 0.10.9.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,440 kB
  • sloc: cpp: 132,294; python: 14,445; javascript: 7,109; sh: 4,713; ansic: 2,189; java: 518; xml: 253; makefile: 175
file content (253 lines) | stat: -rw-r--r-- 7,526 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * E131Device.cpp
 * An E1.31 device
 * Copyright (C) 2007 Simon Newton
 *
 * Ids 0-3 : Input ports (recv dmx)
 * Ids 4-7 : Output ports (send dmx)
 */

#include <google/protobuf/service.h>
#include <google/protobuf/stubs/common.h>
#include <iostream>
#include <set>
#include <string>
#include <vector>

#include "common/rpc/RpcController.h"
#include "ola/CallbackRunner.h"
#include "ola/Logging.h"
#include "ola/network/NetworkUtils.h"
#include "olad/Plugin.h"
#include "olad/PluginAdaptor.h"
#include "olad/Preferences.h"
#include "plugins/e131/E131Device.h"
#include "plugins/e131/E131Port.h"
#include "libs/acn/E131Node.h"

namespace ola {
namespace plugin {
namespace e131 {

const char E131Device::DEVICE_NAME[] = "E1.31 (DMX over ACN)";

using ola::acn::E131Node;
using ola::rpc::RpcController;
using std::ostringstream;
using std::set;
using std::string;
using std::vector;

/*
 * Create a new device
 */
E131Device::E131Device(Plugin *owner,
                       const ola::acn::CID &cid,
                       string ip_addr,
                       PluginAdaptor *plugin_adaptor,
                       const E131DeviceOptions &options)
    : Device(owner, DEVICE_NAME),
      m_plugin_adaptor(plugin_adaptor),
      m_options(options),
      m_ip_addr(ip_addr),
      m_cid(cid) {
}


/*
 * Start this device
 */
bool E131Device::StartHook() {
  m_node.reset(new E131Node(m_plugin_adaptor, m_ip_addr, m_options, m_cid));

  if (!m_node->Start()) {
    m_node.reset();
    DeleteAllPorts();
    return false;
  }

  ostringstream str;
  str << DEVICE_NAME << " [" << m_node->GetInterface().ip_address << "]";
  SetName(str.str());

  for (unsigned int i = 0; i < m_options.input_ports; i++) {
    E131InputPort *input_port = new E131InputPort(
        this, i, m_node.get(), m_plugin_adaptor);
    AddPort(input_port);
    m_input_ports.push_back(input_port);
  }

  for (unsigned int i = 0; i < m_options.output_ports; i++) {
    E131OutputPort *output_port = new E131OutputPort(
        this, i, m_node.get());
    AddPort(output_port);
    m_output_ports.push_back(output_port);
  }

  m_plugin_adaptor->AddReadDescriptor(m_node->GetSocket());
  return true;
}


/*
 * Stop this device
 */
void E131Device::PrePortStop() {
  m_plugin_adaptor->RemoveReadDescriptor(m_node->GetSocket());
}


/*
 * Stop this device
 */
void E131Device::PostPortStop() {
  m_node->Stop();
  m_node.reset();
}


/*
 * Handle device config messages
 * @param controller An RpcController
 * @param request the request data
 * @param response the response to return
 * @param done the closure to call once the request is complete
 */
void E131Device::Configure(RpcController *controller,
                           const string &request,
                           string *response,
                           ConfigureCallback *done) {
  CallbackRunner<ConfigureCallback> runner(done);
  Request request_pb;
  if (!request_pb.ParseFromString(request)) {
    controller->SetFailed("Invalid Request");
    return;
  }

  switch (request_pb.type()) {
    case ola::plugin::e131::Request::E131_PORT_INFO:
      HandlePortStatusRequest(response);
      break;
    case ola::plugin::e131::Request::E131_PREVIEW_MODE:
      HandlePreviewMode(&request_pb, response);
      break;
    case ola::plugin::e131::Request::E131_SOURCES_LIST:
      HandleSourceListRequest(&request_pb, response);
      break;
    default:
      controller->SetFailed("Invalid Request");
  }
}


/*
 * Handle a preview mode request.
 */
void E131Device::HandlePreviewMode(const Request *request, string *response) {
  if (request->has_preview_mode()) {
    const ola::plugin::e131::PreviewModeRequest preview_request =
      request->preview_mode();

    unsigned int port_id = preview_request.port_id();
    bool preview_mode = preview_request.preview_mode();

    if (preview_request.input_port()) {
      E131InputPort *e131_port = GetE131InputPort(port_id);
      if (e131_port) {
        // TODO(simon): figure out what to do here
        (void) e131_port;
      }
    } else {
      E131OutputPort *e131_port = GetE131OutputPort(port_id);
      if (e131_port) {
        e131_port->SetPreviewMode(preview_mode);
      }
    }
  }
  HandlePortStatusRequest(response);
}


/*
 * Handle an options request
 */
void E131Device::HandlePortStatusRequest(string *response) {
  ola::plugin::e131::Reply reply;
  reply.set_type(ola::plugin::e131::Reply::E131_PORT_INFO);
  ola::plugin::e131::PortInfoReply *port_reply = reply.mutable_port_info();

  vector<E131InputPort*>::iterator input_iter = m_input_ports.begin();
  for (; input_iter != m_input_ports.end(); ++input_iter) {
    ola::plugin::e131::InputPortInfo *input_port =
      port_reply->add_input_port();
    input_port->set_port_id((*input_iter)->PortId());
    input_port->set_preview_mode(m_options.ignore_preview);
  }

  vector<E131OutputPort*>::iterator output_iter = m_output_ports.begin();
  for (; output_iter != m_output_ports.end(); ++output_iter) {
    ola::plugin::e131::OutputPortInfo *output_port =
      port_reply->add_output_port();
    output_port->set_port_id((*output_iter)->PortId());
    output_port->set_preview_mode((*output_iter)->PreviewMode());
  }
  reply.SerializeToString(response);
}

void E131Device::HandleSourceListRequest(const Request *request,
                                         string *response) {
  typedef std::vector<E131Node::KnownController> KnownControllerList;
  (void) request;
  ola::plugin::e131::Reply reply;
  reply.set_type(ola::plugin::e131::Reply::E131_SOURCES_LIST);
  ola::plugin::e131::SourceListReply *sources_reply =
      reply.mutable_source_list();

  if (!m_options.enable_draft_discovery) {
    sources_reply->set_unsupported(true);
  } else {
    sources_reply->set_unsupported(false);
    KnownControllerList controllers;
    m_node->GetKnownControllers(&controllers);

    KnownControllerList::const_iterator iter = controllers.begin();
    for (; iter != controllers.end(); ++iter) {
      ola::plugin::e131::SourceEntry *entry = sources_reply->add_source();
      entry->set_cid(iter->cid.ToString());
      entry->set_ip_address(iter->ip_address.ToString());
      entry->set_source_name(iter->source_name);

      set<uint16_t>::const_iterator uni_iter = iter->universes.begin();
      for (; uni_iter != iter->universes.end(); ++uni_iter) {
        entry->add_universe(*uni_iter);
      }
    }
  }

  reply.SerializeToString(response);
}

E131InputPort *E131Device::GetE131InputPort(unsigned int port_id) {
  return (port_id < m_input_ports.size()) ? m_input_ports[port_id] : NULL;
}

E131OutputPort *E131Device::GetE131OutputPort(unsigned int port_id) {
  return (port_id < m_output_ports.size()) ? m_output_ports[port_id] : NULL;
}
}  // namespace e131
}  // namespace plugin
}  // namespace ola