Open Lighting Architecture  0.9.5
 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 #if HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 
30 #include "ola/base/Macro.h"
31 #include "ola/thread/Thread.h"
32 
33 namespace ola {
34 namespace plugin {
35 namespace usbdmx {
36 
57  public:
62  explicit LibUsbThread(libusb_context *context)
63  : m_context(context),
64  m_term(false) {
65  }
66 
70  virtual ~LibUsbThread() {}
71 
75  virtual bool Init() {
76  return true;
77  }
78 
82  virtual void Shutdown() {}
83 
89  void *Run();
90 
94  virtual void OpenHandle() = 0;
95 
99  virtual void CloseHandle(libusb_device_handle *handle) = 0;
100 
101  protected:
108  void SetTerminate() {
109  ola::thread::MutexLocker locker(&m_term_mutex);
110  m_term = true;
111  }
112 
116  void LaunchThread();
117 
121  void JoinThread();
122 
127  libusb_context* Context() const { return m_context; }
128 
129  private:
130  libusb_context *m_context;
131  bool m_term; // GUARDED_BY(m_term_mutex)
132  ola::thread::Mutex m_term_mutex;
133 };
134 
135 #if HAVE_LIBUSB_HOTPLUG_API
136 
140 class LibUsbHotplugThread : public LibUsbThread {
141  public:
152  LibUsbHotplugThread(libusb_context *context,
153  libusb_hotplug_callback_fn callback_fn,
154  void *user_data);
155 
156  bool Init();
157 
158  void Shutdown();
159 
160  void OpenHandle() {}
161 
162  void CloseHandle(libusb_device_handle *handle);
163 
164  private:
165  libusb_hotplug_callback_handle m_hotplug_handle;
166  libusb_hotplug_callback_fn m_callback_fn;
167  void *m_user_data;
168 
169  DISALLOW_COPY_AND_ASSIGN(LibUsbHotplugThread);
170 };
171 
172 #endif
173 
183  public:
192  explicit LibUsbSimpleThread(libusb_context *context)
193  : LibUsbThread(context),
194  m_device_count(0) {
195  }
196 
197  void OpenHandle();
198  void CloseHandle(libusb_device_handle *handle);
199 
200  private:
201  unsigned int m_device_count;
202 
203  DISALLOW_COPY_AND_ASSIGN(LibUsbSimpleThread);
204 };
205 } // namespace usbdmx
206 } // namespace plugin
207 } // namespace ola
208 #endif // PLUGINS_USBDMX_LIBUSBTHREAD_H_