Open Lighting Architecture  0.9.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LibUsbThread.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  * LibUsbThread.h
17  * The thread for asynchronous libusb communication.
18  * Copyright (C) 2014 Simon Newton
19  */
20 
21 #ifndef PLUGINS_USBDMX_LIBUSBTHREAD_H_
22 #define PLUGINS_USBDMX_LIBUSBTHREAD_H_
23 
24 #include <libusb.h>
25 
26 #include "ola/base/Macro.h"
27 #include "ola/thread/Thread.h"
28 
29 namespace ola {
30 namespace plugin {
31 namespace usbdmx {
32 
53  public:
58  explicit LibUsbThread(libusb_context *context)
59  : m_context(context),
60  m_term(false) {
61  }
62 
66  virtual ~LibUsbThread() {}
67 
71  virtual bool Init() {
72  return true;
73  }
74 
78  virtual void Shutdown() {}
79 
85  void *Run();
86 
90  virtual void OpenHandle() = 0;
91 
95  virtual void CloseHandle(libusb_device_handle *handle) = 0;
96 
97  protected:
104  void SetTerminate() {
105  ola::thread::MutexLocker locker(&m_term_mutex);
106  m_term = true;
107  }
108 
112  void LaunchThread();
113 
117  void JoinThread();
118 
123  libusb_context* Context() const { return m_context; }
124 
125  private:
126  libusb_context *m_context;
127  bool m_term; // GUARDED_BY(m_term_mutex)
128  ola::thread::Mutex m_term_mutex;
129 };
130 
131 #if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000102)
132 
136 class LibUsbHotplugThread : public LibUsbThread {
137  public:
148  LibUsbHotplugThread(libusb_context *context,
149  libusb_hotplug_callback_fn callback_fn,
150  void *user_data);
151 
152  bool Init();
153 
154  void Shutdown();
155 
156  void OpenHandle() {}
157 
158  void CloseHandle(libusb_device_handle *handle);
159 
160  private:
161  libusb_hotplug_callback_handle m_hotplug_handle;
162  libusb_hotplug_callback_fn m_callback_fn;
163  void *m_user_data;
164 
165  DISALLOW_COPY_AND_ASSIGN(LibUsbHotplugThread);
166 };
167 
168 #endif
169 
179  public:
188  explicit LibUsbSimpleThread(libusb_context *context)
189  : LibUsbThread(context),
190  m_device_count(0) {
191  }
192 
193  void OpenHandle();
194  void CloseHandle(libusb_device_handle *handle);
195 
196  private:
197  unsigned int m_device_count;
198 
199  DISALLOW_COPY_AND_ASSIGN(LibUsbSimpleThread);
200 };
201 } // namespace usbdmx
202 } // namespace plugin
203 } // namespace ola
204 #endif // PLUGINS_USBDMX_LIBUSBTHREAD_H_