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
|
/*
* 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.
*
* StageProfiPlugin.cpp
* The StageProfi plugin for ola
* Copyright (C) 2006 Simon Newton
*/
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <vector>
#include "ola/Logging.h"
#include "olad/PluginAdaptor.h"
#include "olad/Preferences.h"
#include "plugins/stageprofi/StageProfiDevice.h"
#include "plugins/stageprofi/StageProfiPlugin.h"
namespace ola {
namespace plugin {
namespace stageprofi {
using ola::io::ConnectedDescriptor;
using std::string;
using std::vector;
const char StageProfiPlugin::STAGEPROFI_DEVICE_PATH[] = "/dev/ttyUSB0";
const char StageProfiPlugin::STAGEPROFI_DEVICE_NAME[] = "StageProfi Device";
const char StageProfiPlugin::PLUGIN_NAME[] = "StageProfi";
const char StageProfiPlugin::PLUGIN_PREFIX[] = "stageprofi";
const char StageProfiPlugin::DEVICE_KEY[] = "device";
/*
* Start the plugin
*
* Multiple devices now supported
*/
bool StageProfiPlugin::StartHook() {
vector<string> device_names;
vector<string>::iterator it;
StageProfiDevice *device;
// fetch device listing
device_names = m_preferences->GetMultipleValue(DEVICE_KEY);
for (it = device_names.begin(); it != device_names.end(); ++it) {
if (it->empty())
continue;
device = new StageProfiDevice(this, STAGEPROFI_DEVICE_NAME, *it);
if (!device->Start()) {
delete device;
continue;
}
m_plugin_adaptor->AddReadDescriptor(device->GetSocket());
m_plugin_adaptor->RegisterDevice(device);
m_devices.push_back(device);
}
return true;
}
/*
* Stop the plugin
* @return true on success, false on failure
*/
bool StageProfiPlugin::StopHook() {
vector<StageProfiDevice*>::iterator iter;
for (iter = m_devices.begin(); iter != m_devices.end(); ++iter) {
m_plugin_adaptor->RemoveReadDescriptor((*iter)->GetSocket());
DeleteDevice(*iter);
}
m_devices.clear();
return true;
}
/*
* Return the description for this plugin
*/
string StageProfiPlugin::Description() const {
return
"StageProfi Plugin\n"
"----------------------------\n"
"\n"
"This plugin creates devices with one output port.\n"
"\n"
"--- Config file : ola-stageprofi.conf ---\n"
"\n"
"device = /dev/ttyUSB0\n"
"device = 192.168.1.250\n"
"The device to use either as a path for the USB version or an IP address\n"
"for the LAN version. Multiple devices are supported.\n"
"\n";
}
/*
* Called when the file descriptor is closed.
*/
int StageProfiPlugin::SocketClosed(ConnectedDescriptor *socket) {
vector<StageProfiDevice*>::iterator iter;
for (iter = m_devices.begin(); iter != m_devices.end(); ++iter) {
if ((*iter)->GetSocket() == socket)
break;
}
if (iter == m_devices.end()) {
OLA_WARN << "unknown fd";
return -1;
}
DeleteDevice(*iter);
m_devices.erase(iter);
return 0;
}
/*
* load the plugin prefs and default to sensible values
*
*/
bool StageProfiPlugin::SetDefaultPreferences() {
if (!m_preferences)
return false;
bool save = false;
save |= m_preferences->SetDefaultValue(DEVICE_KEY, StringValidator(),
STAGEPROFI_DEVICE_PATH);
if (save)
m_preferences->Save();
if (m_preferences->GetValue(DEVICE_KEY).empty())
return false;
return true;
}
/*
* Cleanup a single device
*/
void StageProfiPlugin::DeleteDevice(StageProfiDevice *device) {
m_plugin_adaptor->UnregisterDevice(device);
device->Stop();
delete device;
}
} // namespace stageprofi
} // namespace plugin
} // namespace ola
|