Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 LIBS_USB_LIBUSBTHREAD_H_
22 #define LIBS_USB_LIBUSBTHREAD_H_
23 
24 #include <libusb.h>
25 
26 #if HAVE_CONFIG_H
27 #include <config.h>
28 #endif // HAVE_CONFIG_H
29 
30 #include "ola/base/Macro.h"
31 #include "ola/thread/Thread.h"
32 
33 namespace ola {
34 namespace usb {
35 
56  public:
61  explicit LibUsbThread(libusb_context *context)
62  : m_context(context),
63  m_term(false) {
64  }
65 
69  virtual ~LibUsbThread() {}
70 
74  virtual bool Init() {
75  return true;
76  }
77 
81  virtual void Shutdown() {}
82 
88  void *Run();
89 
93  virtual void OpenHandle() = 0;
94 
98  virtual void CloseHandle(libusb_device_handle *handle) = 0;
99 
100  protected:
107  void SetTerminate() {
108  ola::thread::MutexLocker locker(&m_term_mutex);
109  m_term = true;
110  }
111 
115  void LaunchThread();
116 
120  void JoinThread();
121 
126  libusb_context* Context() const { return m_context; }
127 
128  private:
129  libusb_context *m_context;
130  bool m_term; // GUARDED_BY(m_term_mutex)
131  ola::thread::Mutex m_term_mutex;
132 };
133 
134 #if HAVE_LIBUSB_HOTPLUG_API
135 
139 class LibUsbHotplugThread : public LibUsbThread {
140  public:
151  LibUsbHotplugThread(libusb_context *context,
152  libusb_hotplug_callback_fn callback_fn,
153  void *user_data);
154 
155  bool Init();
156 
157  void Shutdown();
158 
159  void OpenHandle() {}
160 
161  void CloseHandle(libusb_device_handle *handle);
162 
163  private:
164  libusb_hotplug_callback_handle m_hotplug_handle;
165  libusb_hotplug_callback_fn m_callback_fn;
166  void *m_user_data;
167 
168  DISALLOW_COPY_AND_ASSIGN(LibUsbHotplugThread);
169 };
170 
171 #endif // HAVE_LIBUSB_HOTPLUG_API
172 
182  public:
191  explicit LibUsbSimpleThread(libusb_context *context)
192  : LibUsbThread(context),
193  m_device_count(0) {
194  }
195 
196  void OpenHandle();
197  void CloseHandle(libusb_device_handle *handle);
198 
199  private:
200  unsigned int m_device_count;
201 
202  DISALLOW_COPY_AND_ASSIGN(LibUsbSimpleThread);
203 };
204 } // namespace usb
205 } // namespace ola
206 #endif // LIBS_USB_LIBUSBTHREAD_H_
void LaunchThread()
Start the libusb thread.
Definition: LibUsbThread.cpp:49
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Creates dummy copy constructor and assignment operator declarations.
Definition: Macro.h:44
void * Run()
The entry point to the libusb thread.
Definition: LibUsbThread.cpp:34
virtual void OpenHandle()=0
This must be called whenever libusb_open() is called.
void JoinThread()
Join the libusb thread.
Definition: LibUsbThread.cpp:54
void OpenHandle()
This must be called whenever libusb_open() is called.
Definition: LibUsbThread.cpp:112
libusb_context * Context() const
Return the libusb_context this thread uses.
Definition: LibUsbThread.h:126
Definition: Mutex.h:57
Definition: Thread.h:52
LibUsbSimpleThread(libusb_context *context)
Create a new LibUsbHotplugThread.
Definition: LibUsbThread.h:191
virtual ~LibUsbThread()
Destructor.
Definition: LibUsbThread.h:69
The non-hotplug version of LibUsbThread.
Definition: LibUsbThread.h:181
The base class for the dedicated libusb thread.
Definition: LibUsbThread.h:55
LibUsbThread(libusb_context *context)
Base constructor.
Definition: LibUsbThread.h:61
void SetTerminate()
Indicate that the libusb thread should terminate.
Definition: LibUsbThread.h:107
Helper macros.
virtual void CloseHandle(libusb_device_handle *handle)=0
This must be called whenever libusb_close() is called.
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
void CloseHandle(libusb_device_handle *handle)
This must be called whenever libusb_close() is called.
Definition: LibUsbThread.cpp:119
virtual bool Init()
Initialize the thread.
Definition: LibUsbThread.h:74
Definition: Mutex.h:35
virtual void Shutdown()
Shutdown the thread.
Definition: LibUsbThread.h:81