Open Lighting Architecture 0.10.9
Loading...
Searching...
No Matches
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
33namespace ola {
34namespace 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:
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
139class 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 LibUsbHotplugThread(const LibUsbHotplugThread &) = delete;
169 const LibUsbHotplugThread &operator=(const LibUsbHotplugThread &) = delete;
170};
171
172#endif // HAVE_LIBUSB_HOTPLUG_API
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 LibUsbSimpleThread(const LibUsbSimpleThread &) = delete;
204 const LibUsbSimpleThread &operator=(const LibUsbSimpleThread &) = delete;
205};
206} // namespace usb
207} // namespace ola
208#endif // LIBS_USB_LIBUSBTHREAD_H_
Helper macros.
Definition Mutex.h:41
Definition Mutex.h:64
Definition Thread.h:52
The non-hotplug version of LibUsbThread.
Definition LibUsbThread.h:182
void OpenHandle()
This must be called whenever libusb_open() is called.
Definition LibUsbThread.cpp:112
LibUsbSimpleThread(libusb_context *context)
Create a new LibUsbHotplugThread.
Definition LibUsbThread.h:192
void CloseHandle(libusb_device_handle *handle)
This must be called whenever libusb_close() is called.
Definition LibUsbThread.cpp:119
The base class for the dedicated libusb thread.
Definition LibUsbThread.h:55
void JoinThread()
Join the libusb thread.
Definition LibUsbThread.cpp:54
void SetTerminate()
Indicate that the libusb thread should terminate.
Definition LibUsbThread.h:107
virtual void CloseHandle(libusb_device_handle *handle)=0
This must be called whenever libusb_close() is called.
virtual ~LibUsbThread()
Destructor.
Definition LibUsbThread.h:69
virtual void OpenHandle()=0
This must be called whenever libusb_open() is called.
LibUsbThread(libusb_context *context)
Base constructor.
Definition LibUsbThread.h:61
void LaunchThread()
Start the libusb thread.
Definition LibUsbThread.cpp:49
virtual bool Init()
Initialize the thread.
Definition LibUsbThread.h:74
void * Run()
The entry point to the libusb thread.
Definition LibUsbThread.cpp:34
virtual void Shutdown()
Shutdown the thread.
Definition LibUsbThread.h:81
libusb_context * Context() const
Return the libusb_context this thread uses.
Definition LibUsbThread.h:126
The namespace containing all OLA symbols.
Definition Credentials.cpp:44