Open Lighting Architecture  0.9.4
 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 #include <map>
26 #include <memory>
27 #include <set>
28 #include <string>
29 #include <utility>
30 #include <vector>
31 
32 #include "ola/base/Macro.h"
33 #include "ola/thread/Mutex.h"
34 #include "ola/thread/Thread.h"
35 #include "plugins/usbdmx/PluginImplInterface.h"
36 #include "plugins/usbdmx/SyncronizedWidgetObserver.h"
37 #include "plugins/usbdmx/WidgetFactory.h"
38 
39 namespace ola {
40 
41 class Device;
42 
43 namespace plugin {
44 namespace usbdmx {
45 
46 #if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000102)
47 #define OLA_LIBUSB_HAS_HOTPLUG_API
48 #endif
49 
54  public:
63  AsyncPluginImpl(PluginAdaptor *plugin_adaptor,
64  Plugin *plugin,
65  unsigned int debug_level);
66  ~AsyncPluginImpl();
67 
68  bool Start();
69  bool Stop();
70 
71  #ifdef OLA_LIBUSB_HAS_HOTPLUG_API
72 
82  void HotPlugEvent(struct libusb_device *dev,
83  libusb_hotplug_event event);
84  #endif
85 
86  bool NewWidget(class AnymauDMX *widget);
87  bool NewWidget(class EurolitePro *widget);
88  bool NewWidget(class ScanlimeFadecandy *widget);
89  bool NewWidget(class Sunlite *widget);
90  bool NewWidget(class VellemanK8062 *widget);
91 
92  void WidgetRemoved(class AnymauDMX *widget);
93  void WidgetRemoved(class EurolitePro *widget);
94  void WidgetRemoved(class ScanlimeFadecandy *widget);
95  void WidgetRemoved(class Sunlite *widget);
96  void WidgetRemoved(class VellemanK8062 *widget);
97 
98  private:
99  typedef std::vector<class WidgetFactory*> WidgetFactories;
100  typedef std::map<struct libusb_device*, WidgetFactory*> USBDeviceToFactoryMap;
101  typedef std::map<class Widget*, Device*> WidgetToDeviceMap;
102  typedef std::pair<uint8_t, uint8_t> USBDeviceID;
103  typedef std::map<USBDeviceID, struct libusb_device*> USBDeviceIDs;
104 
105  PluginAdaptor* const m_plugin_adaptor;
106  Plugin* const m_plugin;
107  const unsigned int m_debug_level;
108 
109  SyncronizedWidgetObserver m_widget_observer;
110 
111  libusb_context *m_context;
112  bool m_use_hotplug;
113  ola::thread::Mutex m_mutex;
114  bool m_suppress_hotplug_events; // GUARDED_BY(m_mutex);
115  std::auto_ptr<class LibUsbThread> m_usb_thread;
116  std::auto_ptr<class LibUsbAdaptor> m_usb_adaptor;
117 
118  WidgetFactories m_widget_factories;
119  USBDeviceToFactoryMap m_device_factory_map;
120  WidgetToDeviceMap m_widget_device_map;
121 
122  // Members used if hotplug is not supported
123  ola::thread::timeout_id m_scan_timeout;
124  USBDeviceIDs m_seen_usb_devices;
125 
126  bool HotplugSupported();
127  bool USBDeviceAdded(libusb_device *device);
128  void USBDeviceRemoved(libusb_device *device);
129 
130  bool StartAndRegisterDevice(class Widget *widget, Device *device);
131  void RemoveWidget(class Widget *widget);
132 
133  bool ScanUSBDevices();
134 
135  DISALLOW_COPY_AND_ASSIGN(AsyncPluginImpl);
136 };
137 } // namespace usbdmx
138 } // namespace plugin
139 } // namespace ola
140 #endif // PLUGINS_USBDMX_ASYNCPLUGINIMPL_H_