Open Lighting Architecture  0.9.5
 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 ScanlimeFadecandy *widget);
90  bool NewWidget(class Sunlite *widget);
91  bool NewWidget(class VellemanK8062 *widget);
92 
93  void WidgetRemoved(class AnymauDMX *widget);
94  void WidgetRemoved(class EurolitePro *widget);
95  void WidgetRemoved(class ScanlimeFadecandy *widget);
96  void WidgetRemoved(class Sunlite *widget);
97  void WidgetRemoved(class VellemanK8062 *widget);
98 
99  private:
100  typedef std::vector<class WidgetFactory*> WidgetFactories;
101  typedef std::map<struct libusb_device*, WidgetFactory*> USBDeviceToFactoryMap;
102  typedef std::map<class Widget*, Device*> WidgetToDeviceMap;
103  typedef std::pair<uint8_t, uint8_t> USBDeviceID;
104  typedef std::map<USBDeviceID, struct libusb_device*> USBDeviceIDs;
105 
106  PluginAdaptor* const m_plugin_adaptor;
107  Plugin* const m_plugin;
108  const unsigned int m_debug_level;
109 
110  SyncronizedWidgetObserver m_widget_observer;
111 
112  libusb_context *m_context;
113  bool m_use_hotplug;
114  ola::thread::Mutex m_mutex;
115  bool m_suppress_hotplug_events; // GUARDED_BY(m_mutex);
116  std::auto_ptr<class LibUsbThread> m_usb_thread;
117  std::auto_ptr<class LibUsbAdaptor> m_usb_adaptor;
118 
119  WidgetFactories m_widget_factories;
120  USBDeviceToFactoryMap m_device_factory_map;
121  WidgetToDeviceMap m_widget_device_map;
122 
123  // Members used if hotplug is not supported
124  ola::thread::timeout_id m_scan_timeout;
125  USBDeviceIDs m_seen_usb_devices;
126 
127  bool HotplugSupported();
128  bool USBDeviceAdded(libusb_device *device);
129  void USBDeviceRemoved(libusb_device *device);
130 
131  bool StartAndRegisterDevice(class Widget *widget, Device *device);
132  void RemoveWidget(class Widget *widget);
133 
134  bool ScanUSBDevices();
135 
136  DISALLOW_COPY_AND_ASSIGN(AsyncPluginImpl);
137 };
138 } // namespace usbdmx
139 } // namespace plugin
140 } // namespace ola
141 #endif // PLUGINS_USBDMX_ASYNCPLUGINIMPL_H_