Open Lighting Architecture  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AsyncPluginImpl.h
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  * AsyncPluginImpl.h
17  * The asynchronous libusb implementation.
18  * Copyright (C) 2014 Simon Newton
19  */
20 
21 #ifndef PLUGINS_USBDMX_ASYNCPLUGINIMPL_H_
22 #define PLUGINS_USBDMX_ASYNCPLUGINIMPL_H_
23 
24 #include <libusb.h>
25 
26 #if HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 
30 #include <map>
31 #include <memory>
32 #include <set>
33 #include <string>
34 #include <utility>
35 #include <vector>
36 
37 #include "ola/base/Macro.h"
38 #include "ola/thread/Mutex.h"
39 #include "ola/thread/Thread.h"
40 #include "plugins/usbdmx/PluginImplInterface.h"
41 #include "plugins/usbdmx/SyncronizedWidgetObserver.h"
42 #include "plugins/usbdmx/WidgetFactory.h"
43 
44 namespace ola {
45 
46 class Device;
47 
48 namespace plugin {
49 namespace usbdmx {
50 
55  public:
64  AsyncPluginImpl(PluginAdaptor *plugin_adaptor,
65  Plugin *plugin,
66  unsigned int debug_level);
67  ~AsyncPluginImpl();
68 
69  bool Start();
70  bool Stop();
71 
72  #ifdef HAVE_LIBUSB_HOTPLUG_API
73 
83  void HotPlugEvent(struct libusb_device *dev,
84  libusb_hotplug_event event);
85  #endif
86 
87  bool NewWidget(class AnymauDMX *widget);
88  bool NewWidget(class EurolitePro *widget);
89  bool NewWidget(class JaRuleWidget *widget);
90  bool NewWidget(class ScanlimeFadecandy *widget);
91  bool NewWidget(class Sunlite *widget);
92  bool NewWidget(class VellemanK8062 *widget);
93 
94  void WidgetRemoved(class AnymauDMX *widget);
95  void WidgetRemoved(class EurolitePro *widget);
96  void WidgetRemoved(class JaRuleWidget *widget);
97  void WidgetRemoved(class ScanlimeFadecandy *widget);
98  void WidgetRemoved(class Sunlite *widget);
99  void WidgetRemoved(class VellemanK8062 *widget);
100 
101  private:
102  typedef std::vector<class WidgetFactory*> WidgetFactories;
103  typedef std::map<struct libusb_device*, WidgetFactory*> USBDeviceToFactoryMap;
104  typedef std::map<class Widget*, Device*> WidgetToDeviceMap;
105  typedef std::pair<uint8_t, uint8_t> USBDeviceID;
106  typedef std::map<USBDeviceID, struct libusb_device*> USBDeviceIDs;
107 
108  PluginAdaptor* const m_plugin_adaptor;
109  Plugin* const m_plugin;
110  const unsigned int m_debug_level;
111 
112  SyncronizedWidgetObserver m_widget_observer;
113 
114  libusb_context *m_context;
115  bool m_use_hotplug;
116  ola::thread::Mutex m_mutex;
117  bool m_suppress_hotplug_events; // GUARDED_BY(m_mutex);
118  std::auto_ptr<class LibUsbThread> m_usb_thread;
119  std::auto_ptr<class AsyncronousLibUsbAdaptor> m_usb_adaptor;
120 
121  WidgetFactories m_widget_factories;
122  USBDeviceToFactoryMap m_device_factory_map;
123  WidgetToDeviceMap m_widget_device_map;
124 
125  // Members used if hotplug is not supported
126  ola::thread::timeout_id m_scan_timeout;
127  USBDeviceIDs m_seen_usb_devices;
128 
129  bool HotplugSupported();
130  bool USBDeviceAdded(libusb_device *device);
131  void USBDeviceRemoved(libusb_device *device);
132 
133  bool StartAndRegisterDevice(class Widget *widget, Device *device);
134  void RemoveWidget(class Widget *widget);
135 
136  bool ScanUSBDevices();
137 
138  DISALLOW_COPY_AND_ASSIGN(AsyncPluginImpl);
139 };
140 } // namespace usbdmx
141 } // namespace plugin
142 } // namespace ola
143 #endif // PLUGINS_USBDMX_ASYNCPLUGINIMPL_H_