Open Lighting Architecture  Latest Git
HotplugAgent.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  * HotplugAgent.h
17  * Handles auto-detection of USB devices.
18  * Copyright (C) 2015 Simon Newton
19  */
20 
21 #ifndef LIBS_USB_HOTPLUGAGENT_H_
22 #define LIBS_USB_HOTPLUGAGENT_H_
23 
24 #include <libusb.h>
25 #include <ola/Callback.h>
26 #include <ola/thread/PeriodicThread.h>
27 
28 #include <map>
29 #include <memory>
30 
31 #include "libs/usb/LibUsbAdaptor.h"
32 #include "libs/usb/LibUsbThread.h"
33 #include "libs/usb/Types.h"
34 
35 namespace ola {
36 namespace usb {
37 
45 class HotplugAgent {
46  public:
47  enum EventType {
50  };
51 
62 
69  HotplugAgent(NotificationCallback* notification_cb,
70  int debug_level);
71 
77  ~HotplugAgent();
78 
87 
92  bool Init();
93 
99  bool Start();
100 
106  void HaltNotifications();
107 
115  bool Stop();
116 
117  #ifdef HAVE_LIBUSB_HOTPLUG_API
118 
128  void HotPlugEvent(struct libusb_device *dev,
129  libusb_hotplug_event event);
130  #endif // HAVE_LIBUSB_HOTPLUG_API
131 
132  private:
133  typedef std::map<USBDeviceID, struct libusb_device*> DeviceMap;
134 
135  std::auto_ptr<NotificationCallback> const m_notification_cb;
136  const int m_debug_level;
137  bool m_use_hotplug;
138  libusb_context *m_context;
139  std::auto_ptr<ola::usb::LibUsbThread> m_usb_thread;
140  std::auto_ptr<ola::usb::AsynchronousLibUsbAdaptor> m_usb_adaptor;
141  std::auto_ptr<ola::thread::PeriodicThread> m_scanner_thread;
142 
143  ola::thread::Mutex m_mutex;
144  bool m_suppress_hotplug_events; // GUARDED_BY(m_mutex);
145 
146  // In hotplug mode, this is guarded by m_mutex while
147  // m_suppress_hotplug_events is false.
148  // In non-hotplug mode, this is only accessed from the scanner thread, unless
149  // the thread is no longer running in which case it's accessed from the main
150  // thread during cleanup
151  DeviceMap m_devices;
152 
153  bool HotplugSupported();
154  bool ScanUSBDevices();
155 
156  DISALLOW_COPY_AND_ASSIGN(HotplugAgent);
157 };
158 } // namespace usb
159 } // namespace ola
160 #endif // LIBS_USB_HOTPLUGAGENT_H_
~HotplugAgent()
Destructor.
Definition: HotplugAgent.cpp:69
HotplugAgent(NotificationCallback *notification_cb, int debug_level)
Create a new HotplugAgent.
Definition: HotplugAgent.cpp:60
AsynchronousLibUsbAdaptor * GetUSBAdaptor() const
Get the AsynchronousLibUsbAdaptor to use.
Definition: HotplugAgent.cpp:75
ola::Callback2< void, EventType, struct libusb_device * > NotificationCallback
Called when a USB device has been added or removed.
Definition: HotplugAgent.h:61
Detects when USB devices are added or removed.
Definition: HotplugAgent.h:45
EventType
Definition: HotplugAgent.h:47
The device ws removed.
Definition: HotplugAgent.h:49
bool Start()
Start the hotplug agent.
Definition: HotplugAgent.cpp:110
bool Stop()
Stop the HotplugAgent.
Definition: HotplugAgent.cpp:144
A 2 argument callback which can be called multiple times.
Definition: Callback.h:1895
void HaltNotifications()
Prevent any further notifications from occurring.
Definition: HotplugAgent.cpp:129
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
bool Init()
Initialize the hotplug agent.
Definition: HotplugAgent.cpp:79
A LibUsbAdaptor for use with Asynchronous widgets.
Definition: LibUsbAdaptor.h:563
The device was added.
Definition: HotplugAgent.h:48
Definition: Mutex.h:41