-
Notifications
You must be signed in to change notification settings - Fork 161
/
qusbinfo.cpp
336 lines (273 loc) · 8.97 KB
/
qusbinfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "qusbinfo.h"
#include "qusbinfo_p.h"
#include <QThread>
#define DbgPrintError() qWarning("In %s, at %s:%d", Q_FUNC_INFO, __FILE__, __LINE__)
#define DbgPrintFuncName() \
if (m_log_level >= QUsbDevice::logDebug) \
qDebug() << "***[" << Q_FUNC_INFO << "]***"
#define DbgPrintCB() \
if (info->logLevel() >= QUsbDevice::logDebug) \
qDebug() << "***[" << Q_FUNC_INFO << "]***"
static libusb_hotplug_callback_handle callback_handle;
static int LIBUSB_CALL hotplugCallback(libusb_context *ctx,
libusb_device *device,
libusb_hotplug_event event,
void *user_data)
{
struct libusb_device_descriptor desc;
(void)ctx;
QUsbDevice::IdList device_list;
QUsbInfo *info = reinterpret_cast<QUsbInfo *>(user_data);
DbgPrintCB();
uint8_t bus = libusb_get_bus_number(device);
uint8_t port = libusb_get_port_number(device);
if (info->logLevel() >= QUsbDevice::logDebug)
qDebug("hotplugCallback");
(void)libusb_get_device_descriptor(device, &desc);
if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) {
// Add to list
emit info->deviceInserted({desc.idProduct, desc.idVendor, bus, port, desc.bDeviceClass, desc.bDeviceSubClass});
} else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) {
// Remove from list
emit info->deviceRemoved({desc.idProduct, desc.idVendor, bus, port, desc.bDeviceClass, desc.bDeviceSubClass});
} else {
if (info->logLevel() >= QUsbDevice::logWarning)
qWarning("Unhandled hotplug event %d", event);
return -1;
}
return 0;
}
QUsbInfoPrivate::QUsbInfoPrivate()
: m_has_hotplug(false), m_ctx(Q_NULLPTR), m_refresh_timer(new QTimer)
{
QThread *t = new QThread();
m_refresh_timer->moveToThread(t);
m_refresh_timer->setSingleShot(false);
m_refresh_timer->setInterval(250);
m_refresh_timer->connect(t, SIGNAL(started()), SLOT(start()));
m_refresh_timer->connect(t, SIGNAL(finished()), SLOT(stop()));
t->start();
}
QUsbInfoPrivate::~QUsbInfoPrivate()
{
m_refresh_timer->thread()->exit();
m_refresh_timer->thread()->wait();
m_refresh_timer->thread()->deleteLater();
m_refresh_timer->disconnect();
m_refresh_timer->deleteLater();
}
/*!
\class QUsbInfo
\brief This class handles hotplug and device detection.
\reentrant
\ingroup usb-main
\inmodule QtUsb
Handles USB events and searching.
Can be used to monitor events for a list of devices or all system devices.
\sa QUsbDevice
*/
/*!
\fn void QUsbInfo::deviceInserted(QUsbDevice::IdList list)
This is signal is emited when one or more new devices are detected, providing a \a list.
*/
/*!
\fn void QUsbInfo::deviceRemoved(QUsbDevice::IdList list)
This is signal is emited when one or more new devices are removed, providing a \a list.
*/
/*!
\property QUsbInfo::logLevel
\brief the log level for hotplug/detection.
*/
QUsbInfo::QUsbInfo(QObject *parent)
: QObject(*(new QUsbInfoPrivate), parent), d_dummy(Q_NULLPTR)
{
Q_D(QUsbInfo);
m_log_level = QUsbDevice::logInfo;
DbgPrintFuncName();
int rc;
qRegisterMetaType<QUsbDevice::Id>("QUsbDevice::DeviceFilter");
qRegisterMetaType<QUsbDevice::Config>("QUsbDevice::DeviceConfig");
qRegisterMetaType<QUsbDevice::IdList>("QUsbDevice::FilterList");
qRegisterMetaType<QUsbDevice::ConfigList>("QUsbDevice::ConfigList");
rc = libusb_init(&d->m_ctx);
if (rc < 0) {
libusb_exit(d->m_ctx);
qCritical("LibUsb Init Error %d", rc);
return;
}
libusb_set_debug(d->m_ctx, LIBUSB_LOG_LEVEL_WARNING);
// Populate list once
m_system_list = QUsbDevice::devices();
// Try hotplug first
d->m_has_hotplug = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) != 0;
if (d->m_has_hotplug) {
rc = libusb_hotplug_register_callback(d->m_ctx,
static_cast<libusb_hotplug_event>((LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)),
LIBUSB_HOTPLUG_NO_FLAGS,
LIBUSB_HOTPLUG_MATCH_ANY,
LIBUSB_HOTPLUG_MATCH_ANY,
LIBUSB_HOTPLUG_MATCH_ANY,
reinterpret_cast<libusb_hotplug_callback_fn>(hotplugCallback),
reinterpret_cast<void *>(this),
&callback_handle);
if (LIBUSB_SUCCESS != rc) {
libusb_exit(d->m_ctx);
qWarning("Error creating hotplug callback");
return;
}
}
connect(d->m_refresh_timer, SIGNAL(timeout()), this, SLOT(checkDevices()));
}
/*!
Unregister callbacks and close the usb context.
*/
QUsbInfo::~QUsbInfo()
{
Q_D(QUsbInfo);
DbgPrintFuncName();
libusb_hotplug_deregister_callback(d->m_ctx, callback_handle);
libusb_exit(d->m_ctx);
}
/*!
Check devices present in system.
This gets called by the internal timer.
*/
void QUsbInfo::checkDevices()
{
DbgPrintFuncName();
Q_D(QUsbInfo);
QUsbDevice::IdList list;
timeval t = { 0, 0 };
if (d->m_has_hotplug) {
libusb_handle_events_timeout_completed(d->m_ctx, &t, Q_NULLPTR);
} else {
list = QUsbDevice::devices();
monitorDevices(list);
}
}
/*!
Gets a list of devices present in the system.
*/
QUsbDevice::IdList QUsbInfo::getPresentDevices() const
{
DbgPrintFuncName();
QUsbDevice::IdList list;
QUsbDevice::Id filter;
/* Search the system list with our own list */
for (int i = 0; i < m_list.length(); i++) {
filter = m_list.at(i);
if (this->findDevice(filter, m_system_list) < 0) {
list.append(filter);
}
}
return list;
}
/*!
Check if \a id device is present.
Return bool true if present.
*/
bool QUsbInfo::isPresent(const QUsbDevice::Id &id) const
{
DbgPrintFuncName();
return this->findDevice(id, m_system_list) >= 0;
}
/*!
Add an \a id device to the list.
Returns false if device was already in the list, else true.
*/
bool QUsbInfo::addDevice(const QUsbDevice::Id &id)
{
DbgPrintFuncName();
if (this->findDevice(id, m_list) == -1) {
m_list.append(id);
return true;
}
return false;
}
/*!
\brief Remove \a id device from the list.
Return bool false if device was not in the list, else true.
*/
bool QUsbInfo::removeDevice(const QUsbDevice::Id &id)
{
DbgPrintFuncName();
const int pos = this->findDevice(id, m_list);
if (pos > 0) {
m_list.removeAt(pos);
return true;
}
return true;
}
/*!
Search an \a id device in a device \a list.
Return index of the filter, returns -1 if not found.
*/
int QUsbInfo::findDevice(const QUsbDevice::Id &id,
const QUsbDevice::IdList &list) const
{
DbgPrintFuncName();
for (int i = 0; i < list.length(); i++) {
const QUsbDevice::Id *d = &list.at(i);
if (d->pid == id.pid && d->vid == id.vid) {
if (id.bus == 0 && id.port == 0) // Ignore bus/port if both == 0
return i;
if ((id.bus && d->bus == id.bus) && (id.port && d->port == id.port)) // Take bus/port into account for filtering when set
return i;
}
}
return -1;
}
/*!
Set log \a level (only hotplug/detection).
*/
void QUsbInfo::setLogLevel(QUsbDevice::LogLevel level)
{
DbgPrintFuncName();
Q_D(QUsbInfo);
m_log_level = level;
if (m_log_level >= QUsbDevice::logDebug)
libusb_set_debug(d->m_ctx, LIBUSB_LOG_LEVEL_DEBUG);
else if (m_log_level >= QUsbDevice::logWarning)
libusb_set_debug(d->m_ctx, LIBUSB_LOG_LEVEL_WARNING);
else
libusb_set_debug(d->m_ctx, LIBUSB_LOG_LEVEL_ERROR);
}
/*!
Get current log level.
*/
QUsbDevice::LogLevel QUsbInfo::logLevel() const
{
return m_log_level;
}
/*!
Add a \a list to monitor.
*/
void QUsbInfo::monitorDevices(const QUsbDevice::IdList &list)
{
DbgPrintFuncName();
QUsbDevice::IdList inserted, removed;
QUsbDevice::Id filter;
for (int i = 0; i < list.length(); i++) {
filter = list.at(i);
if (this->findDevice(filter, m_system_list) < 0) {
// It's not in the old system list
inserted.append(filter);
}
}
for (int i = 0; i < m_system_list.length(); i++) {
filter = m_system_list.at(i);
if (this->findDevice(filter, list) < 0) {
// It's in the old system list but not in the current one
removed.append(filter);
}
}
for (int i = 0; i < inserted.length(); i++)
{
emit deviceInserted(inserted.at(i));
}
for (int i = 0; i < removed.length(); i++)
{
emit deviceRemoved(removed.at(i));
}
m_system_list = list;
}