File: Dmx4LinuxPlugin.cpp

package info (click to toggle)
ola 0.9.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 21,340 kB
  • ctags: 23,021
  • sloc: cpp: 129,922; python: 12,265; sh: 11,778; makefile: 2,288; ansic: 1,775; java: 518; xml: 214
file content (305 lines) | stat: -rw-r--r-- 7,778 bytes parent folder | download
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * 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.
 *
 * Dmx4LinuxPlugin.cpp
 * The Dmx4Linux plugin for ola
 * Copyright (C) 2006 Simon Newton
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <dmx/dmxioctl.h>

#include <string>
#include <vector>

#include "ola/BaseTypes.h"
#include "ola/Logging.h"
#include "ola/StringUtils.h"
#include "olad/PluginAdaptor.h"
#include "olad/Preferences.h"

#include "plugins/dmx4linux/Dmx4LinuxDevice.h"
#include "plugins/dmx4linux/Dmx4LinuxPlugin.h"
#include "plugins/dmx4linux/Dmx4LinuxPort.h"
#include "plugins/dmx4linux/Dmx4LinuxSocket.h"


namespace ola {
namespace plugin {
namespace dmx4linux {

const char Dmx4LinuxPlugin::DMX4LINUX_OUT_DEVICE[] = "/dev/dmx";
const char Dmx4LinuxPlugin::DMX4LINUX_IN_DEVICE[] = "/dev/dmxin";
const char Dmx4LinuxPlugin::IN_DEV_KEY[] = "in_device";
const char Dmx4LinuxPlugin::OUT_DEV_KEY[] = "out_device";
const char Dmx4LinuxPlugin::PLUGIN_NAME[] = "Dmx4Linux";
const char Dmx4LinuxPlugin::PLUGIN_PREFIX[] = "dmx4linux";


Dmx4LinuxPlugin::~Dmx4LinuxPlugin() {
  CleanupDescriptors();
}


/*
 * Start the plugin
 */
bool Dmx4LinuxPlugin::StartHook() {
  if (!SetupDescriptors())
    return false;

  if (!SetupDevices()) {
    CleanupDescriptors();
    return false;
  }

  if (!m_devices.empty()) {
    m_in_descriptor->SetOnData(
        ola::NewCallback(this, &Dmx4LinuxPlugin::SocketReady));
    m_plugin_adaptor->AddReadDescriptor(m_in_descriptor);
    return true;
  } else {
    CleanupDescriptors();
    return false;
  }
}


/*
 * Stop the plugin
 * @return true on success, false on failure
 */
bool Dmx4LinuxPlugin::StopHook() {
  vector<Dmx4LinuxDevice*>::iterator it;
  m_plugin_adaptor->RemoveReadDescriptor(m_in_descriptor);

  for (it = m_devices.begin(); it != m_devices.end(); ++it) {
    m_plugin_adaptor->UnregisterDevice(*it);
    (*it)->Stop();
    delete *it;
  }
  CleanupDescriptors();
  m_devices.clear();
  m_in_ports.clear();
  return 0;
}


/*
 * Return the description for this plugin
 */
string Dmx4LinuxPlugin::Description() const {
    return
"DMX 4 Linux Plugin\n"
"----------------------------\n"
"\n"
"This plugin exposes DMX 4 Linux devices.\n"
"\n"
"--- Config file : ola-dmx4linux.conf ---\n"
"\n"
"in_device =  /dev/dmxin\n"
"out_device = /dev/dmx\n";
}


/*
 * Called when there is input for us
 */
int Dmx4LinuxPlugin::SocketReady() {
  vector<Dmx4LinuxInputPort*>::iterator iter;
  unsigned int data_read, offset;
  int ret;

  if (lseek(m_in_descriptor->ReadDescriptor(), 0, SEEK_SET) != 0) {
    OLA_WARN << "failed to seek: " << strerror(errno);
    return -1;
  }
  ret = m_in_descriptor->Receive(m_in_buffer,
                             DMX_UNIVERSE_SIZE * m_in_devices_count,
                             data_read);
  iter = m_in_ports.begin();
  offset = 0;
  while (offset < data_read && iter != m_in_ports.end()) {
    (*iter)->UpdateData(m_in_buffer + offset, data_read - offset);
    iter++;
  }
  return 0;
}


/*
 * load the plugin prefs and default to sensible values
 */
bool Dmx4LinuxPlugin::SetDefaultPreferences() {
  bool save = false;

  if (!m_preferences)
    return false;

  save |= m_preferences->SetDefaultValue(IN_DEV_KEY, StringValidator(),
                                         DMX4LINUX_IN_DEVICE);
  save |= m_preferences->SetDefaultValue(OUT_DEV_KEY, StringValidator(),
                                         DMX4LINUX_OUT_DEVICE);

  if (save)
    m_preferences->Save();

  if (m_preferences->GetValue(IN_DEV_KEY).empty() ||
      m_preferences->GetValue(OUT_DEV_KEY).empty())
    return false;

  m_in_dev = m_preferences->GetValue(IN_DEV_KEY);
  m_out_dev = m_preferences->GetValue(OUT_DEV_KEY);
  return true;
}


/*
 * Open the input and output fds
 */
bool Dmx4LinuxPlugin::SetupDescriptors() {
  if (!m_in_descriptor && !m_out_descriptor) {
    int fd = open(m_out_dev.c_str(), O_WRONLY);

    if (fd < 0) {
      OLA_WARN << "failed to open " << m_out_dev << " " << strerror(errno);
      return false;
    }
    m_out_descriptor = new Dmx4LinuxSocket(fd);

    fd = open(m_in_dev.c_str(), O_RDONLY | O_NONBLOCK);
    if (fd < 0) {
      OLA_WARN << "failed to open " << m_in_dev << " " << strerror(errno);
      CleanupDescriptors();
      return false;
    }
    m_in_descriptor = new Dmx4LinuxSocket(fd);
    return true;
  }
  return false;
}


/*
 * Close all fds
 */
int Dmx4LinuxPlugin::CleanupDescriptors() {
  if (m_in_descriptor) {
    delete m_in_descriptor;
    m_in_descriptor = NULL;
  }

  if (m_out_descriptor) {
    delete m_out_descriptor;
    m_out_descriptor = NULL;
  }

  if (m_in_buffer) {
    delete[] m_in_buffer;
    m_in_buffer = NULL;
  }
  return 0;
}


/*
 * setup a single device
 * @param family the dmx4linux family
 * @param d4l_uni the dmx4linux universe
 * @param dir   in|out
 */
bool Dmx4LinuxPlugin::SetupDevice(string family, int d4l_uni, int dir) {
  string device_id = IntToString((d4l_uni << 1) + dir);
  string name = string("dmx4linux_");
  name.append(family);
  name.append(dir ? "_in_" : "_out_");
  name.append(IntToString(d4l_uni));

  OLA_INFO << "Dmx4LinuxPlugin creates a device : name = " << name <<
      " / uni = " << d4l_uni << " / dir = " << dir;
  Dmx4LinuxDevice *dev = new Dmx4LinuxDevice(this, name, device_id);
  dev->Start();

  if (dir == DMX_DIRECTION_INPUT) {
    Dmx4LinuxInputPort *port = new Dmx4LinuxInputPort(
        dev,
        m_plugin_adaptor);
    m_in_ports.push_back(port);
    dev->AddPort(port);
  } else {
    Dmx4LinuxOutputPort *port = new Dmx4LinuxOutputPort(dev,
                                                        m_out_descriptor,
                                                        d4l_uni);
    dev->AddPort(port);
  }
  m_plugin_adaptor->RegisterDevice(dev);
  m_devices.push_back(dev);
  return true;
}


/*
 * Find all the devices connected and setup ports for them.
 */
bool Dmx4LinuxPlugin::SetupDevices() {
  struct dmx_capabilities cap;
  struct dmx_info info;

  if (ioctl(m_in_descriptor->ReadDescriptor(), DMX_IOCTL_GET_INFO, &info) < 0) {
    OLA_WARN << "failed to fetch universe list";
    return false;
  }

  if (info.max_in_universes > 0) {
    m_in_devices_count = info.max_in_universes;
    m_in_buffer = new uint8_t[DMX_UNIVERSE_SIZE * info.max_in_universes];
  }

  for (int i = 0; i < info.max_in_universes; i++) {
    cap.direction = DMX_DIRECTION_INPUT;
    cap.universe = i;

    if (ioctl(m_in_descriptor->ReadDescriptor(), DMX_IOCTL_GET_CAP, &cap) >=
        0) {
      if (cap.maxSlots > 0) {
        SetupDevice(cap.family, cap.universe, cap.direction);
      }
    }
  }

  for (int i = 0; i < info.max_out_universes; i++) {
    cap.direction = DMX_DIRECTION_OUTPUT;
    cap.universe = i;

    if (ioctl(m_in_descriptor->ReadDescriptor(), DMX_IOCTL_GET_CAP, &cap) >=
        0) {
      if (cap.maxSlots > 0) {
        SetupDevice(cap.family, cap.universe, cap.direction);
      }
    }
  }
  return true;
}
}  // namespace dmx4linux
}  // namespace plugin
}  // namespace ola