Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions plugins/usbdmx/AnymauDMXFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ bool AnymauDMXFactory::DeviceAdded(
WidgetObserver *observer,
libusb_device *usb_device,
const struct libusb_device_descriptor &descriptor) {
if (descriptor.idVendor != VENDOR_ID || descriptor.idProduct != PRODUCT_ID ||
HasDevice(usb_device)) {
if (descriptor.idVendor != VENDOR_ID || descriptor.idProduct != PRODUCT_ID) {
return false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come we've dropped the test for this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

De-duping is done in the hotplug agent now so each factory doesn't need to deal with it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay

}

Expand Down Expand Up @@ -83,7 +82,7 @@ bool AnymauDMXFactory::DeviceAdded(
} else {
widget = new SynchronousAnymauDMX(m_adaptor, usb_device, info.serial);
}
return AddWidget(observer, usb_device, widget);
return AddWidget(observer, widget);
}
} // namespace usbdmx
} // namespace plugin
Expand Down
151 changes: 84 additions & 67 deletions plugins/usbdmx/AsyncPluginImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "ola/StringUtils.h"
#include "ola/stl/STLUtils.h"
#include "ola/strings/Format.h"
#include "ola/thread/Future.h"
#include "ola/util/Deleter.h"
#include "olad/PluginAdaptor.h"

#include "libs/usb/JaRuleWidget.h"
Expand All @@ -54,15 +56,42 @@ namespace usbdmx {
using ola::usb::HotplugAgent;
using ola::usb::JaRuleWidget;
using ola::usb::USBDeviceID;
using ola::NewSingleCallback;
using std::auto_ptr;
using ola::thread::Future;

class DeviceState {
public:
typedef ola::SingleUseCallback0<void> DeleterCallback;

DeviceState() : factory(NULL), ola_device(NULL), m_deleter(NULL) {}

void SetDeleteCallback(DeleterCallback *cb) {
m_deleter = cb;
}

void DeleteWidget() {
if (m_deleter) {
m_deleter->Run();
m_deleter = NULL;
}
}

WidgetFactory *factory; // The factory that owns this device.
Device *ola_device; // The OLA device that uses this USB device.

private:
DeleterCallback *m_deleter;
};

AsyncPluginImpl::AsyncPluginImpl(PluginAdaptor *plugin_adaptor,
Plugin *plugin,
unsigned int debug_level)
: m_plugin_adaptor(plugin_adaptor),
m_plugin(plugin),
m_debug_level(debug_level),
m_widget_observer(this, plugin_adaptor) {
m_widget_observer(this, plugin_adaptor),
m_usb_adaptor(NULL) {
}

AsyncPluginImpl::~AsyncPluginImpl() {
Expand All @@ -71,8 +100,7 @@ AsyncPluginImpl::~AsyncPluginImpl() {

bool AsyncPluginImpl::Start() {
auto_ptr<HotplugAgent> agent(new HotplugAgent(
NewCallback(this, &AsyncPluginImpl::DeviceEvent),
m_debug_level));
NewCallback(this, &AsyncPluginImpl::DeviceEvent), m_debug_level));

if (!agent->Init()) {
return false;
Expand Down Expand Up @@ -111,10 +139,13 @@ bool AsyncPluginImpl::Stop() {
// Now we're free to use m_device_map.
USBDeviceMap::iterator iter = m_device_map.begin();
for (; iter != m_device_map.end(); ++iter) {
if (iter->second->factory) {
iter->second->factory->DeviceRemoved(this, iter->second->usb_device);
iter->second->factory = NULL;
DeviceState *state = iter->second;
if (state->ola_device) {
m_plugin_adaptor->UnregisterDevice(state->ola_device);
state->ola_device->Stop();
delete state->ola_device;
}
state->DeleteWidget();
}
STLDeleteValues(&m_device_map);
STLDeleteElements(&m_widget_factories);
Expand All @@ -125,28 +156,28 @@ bool AsyncPluginImpl::Stop() {

bool AsyncPluginImpl::NewWidget(AnymauDMX *widget) {
return StartAndRegisterDevice(
widget->GetDeviceId(),
widget,
new GenericDevice(m_plugin, widget, "Anyma USB Device",
"anyma-" + widget->SerialNumber()));
}

bool AsyncPluginImpl::NewWidget(EurolitePro *widget) {
return StartAndRegisterDevice(
widget->GetDeviceId(),
widget,
new GenericDevice(m_plugin, widget, "EurolitePro USB Device",
"eurolite-" + widget->SerialNumber()));
}

bool AsyncPluginImpl::NewWidget(JaRuleWidget *widget) {
std::ostringstream str;
str << widget->ProductString() << " (" << widget->GetUID() << ")";
return StartAndRegisterDevice(widget->GetDeviceId(),
return StartAndRegisterDevice(widget,
new JaRuleDevice(m_plugin, widget, str.str()));
}

bool AsyncPluginImpl::NewWidget(ScanlimeFadecandy *widget) {
return StartAndRegisterDevice(
widget->GetDeviceId(),
widget,
new GenericDevice(
m_plugin, widget,
"Fadecandy USB Device (" + widget->SerialNumber() + ")",
Expand All @@ -155,57 +186,45 @@ bool AsyncPluginImpl::NewWidget(ScanlimeFadecandy *widget) {

bool AsyncPluginImpl::NewWidget(Sunlite *widget) {
return StartAndRegisterDevice(
widget->GetDeviceId(),
widget,
new GenericDevice(m_plugin, widget, "Sunlite USBDMX2 Device", "usbdmx2"));
}

bool AsyncPluginImpl::NewWidget(VellemanK8062 *widget) {
return StartAndRegisterDevice(
widget->GetDeviceId(),
widget,
new GenericDevice(m_plugin, widget, "Velleman USB Device", "velleman"));
}

void AsyncPluginImpl::WidgetRemoved(AnymauDMX *widget) {
RemoveWidget(widget->GetDeviceId());
}

void AsyncPluginImpl::WidgetRemoved(EurolitePro *widget) {
RemoveWidget(widget->GetDeviceId());
}

void AsyncPluginImpl::WidgetRemoved(JaRuleWidget *widget) {
RemoveWidget(widget->GetDeviceId());
}

void AsyncPluginImpl::WidgetRemoved(ScanlimeFadecandy *widget) {
RemoveWidget(widget->GetDeviceId());
}

void AsyncPluginImpl::WidgetRemoved(Sunlite *widget) {
RemoveWidget(widget->GetDeviceId());
}

void AsyncPluginImpl::WidgetRemoved(VellemanK8062 *widget) {
RemoveWidget(widget->GetDeviceId());
}

/**
* This is run in either the thread calling Start() or a hotplug thread,
* but not both at once.
*/
void AsyncPluginImpl::DeviceEvent(HotplugAgent::EventType event,
struct libusb_device *device) {
if (event == HotplugAgent::DEVICE_ADDED) {
USBDeviceAdded(device);
SetupUSBDevice(device);
} else {
USBDeviceID device_id = m_usb_adaptor->GetDeviceId(device);
DeviceState *state = STLFindOrNull(m_device_map, device_id);
if (state && state->factory) {
OLA_INFO << "USB device " << device_id << " removed";
state->factory->DeviceRemoved(&m_widget_observer, state->usb_device);
state->factory = NULL;
DeviceState *state;
if (!STLLookupAndRemove(&m_device_map, device_id, &state) || !state) {
return;
}
STLRemoveAndDelete(&m_device_map, device_id);

// At some point we'll need to notify the factory here. For instance in the

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a TODO

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't an actionable item. It's more of a 'someone will need to add code here sometime in the future'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we add more functionality elsewhere?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, if someone wants to make the f/w loading async, they'll need to add a call in here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. So does Sunlite only work in sync mode?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works in both, but the f/w loading is always synchronous.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

// Sunlite plugin, if we make the f/w load async we'll need to let the
// factory cancel the load.

// Unregister & delete the device in the main thread.
if (state->ola_device) {
Future<void> f;
m_plugin_adaptor->Execute(
NewSingleCallback(this, &AsyncPluginImpl::ShutdownDevice,
state->ola_device, &f));
f.Get();
state->ola_device = NULL;
}
state->DeleteWidget();
}
}

Expand All @@ -218,7 +237,7 @@ void AsyncPluginImpl::DeviceEvent(HotplugAgent::EventType event,
* This can be called from either the libusb thread or the main thread. However
* only one of those will be active at once, so we can avoid locking.
*/
void AsyncPluginImpl::USBDeviceAdded(libusb_device *usb_device) {
void AsyncPluginImpl::SetupUSBDevice(libusb_device *usb_device) {
USBDeviceID device_id = m_usb_adaptor->GetDeviceId(usb_device);
USBDeviceMap::iterator iter = STLLookupOrInsertNew(&m_device_map, device_id);

Expand All @@ -229,10 +248,6 @@ void AsyncPluginImpl::USBDeviceAdded(libusb_device *usb_device) {
return;
}

if (!state->usb_device) {
state->usb_device = usb_device;
}

struct libusb_device_descriptor descriptor;
libusb_get_device_descriptor(usb_device, &descriptor);

Expand All @@ -253,22 +268,17 @@ void AsyncPluginImpl::USBDeviceAdded(libusb_device *usb_device) {
}

/*
* @brief Signal widget / device addition.
* @param device_id The device id to add.
* @brief Called when a new OLA device is ready.
* @param widget The Widget used for this device.
* @param device The new olad device that uses this new widget.
*
* This is run within the main thread.
# */
bool AsyncPluginImpl::StartAndRegisterDevice(const USBDeviceID &device_id,
Device *device) {
if (!device->Start()) {
delete device;
return false;
}

DeviceState *state = STLFindOrNull(m_device_map, device_id);
*/
template <typename Widget>
bool AsyncPluginImpl::StartAndRegisterDevice(Widget *widget, Device *device) {
DeviceState *state = STLFindOrNull(m_device_map, widget->GetDeviceId());
if (!state) {
OLA_WARN << "Failed to find state for device " << device_id;
OLA_WARN << "Failed to find state for device " << widget->GetDeviceId();
delete device;
return false;
}
Expand All @@ -278,9 +288,17 @@ bool AsyncPluginImpl::StartAndRegisterDevice(const USBDeviceID &device_id,
m_plugin_adaptor->UnregisterDevice(state->ola_device);
state->ola_device->Stop();
delete state->ola_device;
state->ola_device = NULL;
}

if (!device->Start()) {
delete device;
return false;
}

m_plugin_adaptor->RegisterDevice(device);
state->ola_device = device;
state->SetDeleteCallback(ola::DeletePointerCallback(widget));
return true;
}

Expand All @@ -290,13 +308,12 @@ bool AsyncPluginImpl::StartAndRegisterDevice(const USBDeviceID &device_id,
*
* This is run within the main thread.
*/
void AsyncPluginImpl::RemoveWidget(const USBDeviceID &device_id) {
DeviceState *state = STLFindOrNull(m_device_map, device_id);
if (state && state->ola_device) {
m_plugin_adaptor->UnregisterDevice(state->ola_device);
state->ola_device->Stop();
delete state->ola_device;
state->ola_device = NULL;
void AsyncPluginImpl::ShutdownDevice(Device *device, Future<void> *f) {
m_plugin_adaptor->UnregisterDevice(device);
device->Stop();
delete device;
if (f) {
f->Set();
}
}
} // namespace usbdmx
Expand Down
29 changes: 7 additions & 22 deletions plugins/usbdmx/AsyncPluginImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
#include "libs/usb/HotplugAgent.h"

#include "ola/base/Macro.h"
#include "ola/thread/Mutex.h"
#include "ola/thread/Thread.h"
#include "ola/thread/Future.h"
#include "plugins/usbdmx/PluginImplInterface.h"
#include "plugins/usbdmx/SyncronizedWidgetObserver.h"
#include "plugins/usbdmx/WidgetFactory.h"
Expand Down Expand Up @@ -83,24 +82,9 @@ class AsyncPluginImpl: public PluginImplInterface, public WidgetObserver {
bool NewWidget(class Sunlite *widget);
bool NewWidget(class VellemanK8062 *widget);

void WidgetRemoved(class AnymauDMX *widget);
void WidgetRemoved(class EurolitePro *widget);
void WidgetRemoved(ola::usb::JaRuleWidget *widget);
void WidgetRemoved(class ScanlimeFadecandy *widget);
void WidgetRemoved(class Sunlite *widget);
void WidgetRemoved(class VellemanK8062 *widget);

private:
struct DeviceState {
DeviceState() : usb_device(NULL), factory(NULL), ola_device(NULL) {}

struct libusb_device *usb_device; // The underlying libusb device.
WidgetFactory *factory; // The factory that owns this device.
Device *ola_device; // The OLA device that uses this USB device.
};

typedef std::vector<class WidgetFactory*> WidgetFactories;
typedef std::map<ola::usb::USBDeviceID, DeviceState*> USBDeviceMap;
typedef std::map<ola::usb::USBDeviceID, class DeviceState*> USBDeviceMap;

PluginAdaptor* const m_plugin_adaptor;
Plugin* const m_plugin;
Expand All @@ -114,11 +98,12 @@ class AsyncPluginImpl: public PluginImplInterface, public WidgetObserver {

void DeviceEvent(ola::usb::HotplugAgent::EventType event,
struct libusb_device *device);
void USBDeviceAdded(libusb_device *device);
void SetupUSBDevice(libusb_device *device);

template <typename Widget>
bool StartAndRegisterDevice(Widget *widget, Device *device);

bool StartAndRegisterDevice(const ola::usb::USBDeviceID &device_id,
Device *device);
void RemoveWidget(const ola::usb::USBDeviceID &device_id);
void ShutdownDevice(Device *device, ola::thread::Future<void> *f);

DISALLOW_COPY_AND_ASSIGN(AsyncPluginImpl);
};
Expand Down
5 changes: 2 additions & 3 deletions plugins/usbdmx/EuroliteProFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ bool EuroliteProFactory::DeviceAdded(
WidgetObserver *observer,
libusb_device *usb_device,
const struct libusb_device_descriptor &descriptor) {
if (descriptor.idVendor != VENDOR_ID || descriptor.idProduct != PRODUCT_ID ||
HasDevice(usb_device)) {
if (descriptor.idVendor != VENDOR_ID || descriptor.idProduct != PRODUCT_ID) {
return false;
}

Expand Down Expand Up @@ -80,7 +79,7 @@ bool EuroliteProFactory::DeviceAdded(
widget = new SynchronousEurolitePro(m_adaptor, usb_device,
serial_str.str());
}
return AddWidget(observer, usb_device, widget);
return AddWidget(observer, widget);
}
} // namespace usbdmx
} // namespace plugin
Expand Down
5 changes: 2 additions & 3 deletions plugins/usbdmx/JaRuleFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ bool JaRuleFactory::DeviceAdded(
WidgetObserver *observer,
libusb_device *usb_device,
const struct libusb_device_descriptor &descriptor) {
if (descriptor.idVendor != VENDOR_ID || descriptor.idProduct != PRODUCT_ID ||
HasDevice(usb_device)) {
if (descriptor.idVendor != VENDOR_ID || descriptor.idProduct != PRODUCT_ID) {
return false;
}

Expand All @@ -53,7 +52,7 @@ bool JaRuleFactory::DeviceAdded(
OLA_INFO << "Found a new Ja Rule device";
std::auto_ptr<ola::usb::JaRuleWidget> widget(
new ola::usb::JaRuleWidget(m_ss, m_adaptor, usb_device));
return AddWidget(observer, usb_device, widget.release());
return AddWidget(observer, widget.release());
}
} // namespace usbdmx
} // namespace plugin
Expand Down
Loading