-
Notifications
You must be signed in to change notification settings - Fork 0
/
qextserialenumerator_win.cpp
303 lines (274 loc) · 10.7 KB
/
qextserialenumerator_win.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
/****************************************************************************
** Copyright (c) 2000-2003 Wayne Roth
** Copyright (c) 2004-2007 Stefan Sander
** Copyright (c) 2007 Michal Policht
** Copyright (c) 2008 Brandon Fosdick
** Copyright (c) 2009-2010 Liam Staskawicz
** Copyright (c) 2011 Debao Zhang
** All right reserved.
** Web: http://code.google.com/p/qextserialport/
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#include "qextserialenumerator.h"
#include "qextserialenumerator_p.h"
#include <QtCore/QDebug>
#include <QtCore/QMetaType>
#include <QtCore/QRegExp>
#include <algorithm>
#include <objbase.h>
#include <initguid.h>
#include <setupapi.h>
#include <dbt.h>
#ifdef QT_GUI_LIB
/*!
\internal
\class QextSerialRegistrationWidget
Internal window which is used to receive device arrvial and removal message.
*/
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
#include <QtGui/QWidget>
class QextSerialRegistrationWidget : public QWidget
#else
#include <QtGui/QWindow>
class QextSerialRegistrationWidget : public QWindow
#endif
{
public:
QextSerialRegistrationWidget(QextSerialEnumeratorPrivate *qese) {
this->qese = qese;
}
~QextSerialRegistrationWidget() {}
protected:
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
bool winEvent(MSG *message, long *result) {
#else
bool nativeEvent(const QByteArray & /*eventType*/, void *msg, long *result) {
MSG *message = static_cast<MSG *>(msg);
#endif
if (message->message == WM_DEVICECHANGE) {
qese->onDeviceChanged(message->wParam, message->lParam);
*result = 1;
return true;
}
return false;
}
private:
QextSerialEnumeratorPrivate *qese;
};
#endif // QT_GUI_LIB
void QextSerialEnumeratorPrivate::init_sys()
{
#ifdef QT_GUI_LIB
notificationWidget = 0;
#endif // QT_GUI_LIB
}
/*!
default
*/
void QextSerialEnumeratorPrivate::destroy_sys()
{
#ifdef QT_GUI_LIB
if (notificationWidget)
delete notificationWidget;
#endif
}
#ifndef GUID_DEVINTERFACE_COMPORT
DEFINE_GUID(GUID_DEVINTERFACE_COMPORT, 0x86e0d1e0L, 0x8089, 0x11d0, 0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73);
#endif
/*!
\internal
Get value of specified property from the registry.
\a key handle to an open key.
\a property property name.
return property value.
*/
static QString getRegKeyValue(HKEY key, LPCTSTR property)
{
DWORD size = 0;
DWORD type;
if (::RegQueryValueEx(key, property, NULL, NULL, NULL, &size) != ERROR_SUCCESS)
return QString();
BYTE *buff = new BYTE[size];
QString result;
if (::RegQueryValueEx(key, property, NULL, &type, buff, &size) == ERROR_SUCCESS)
result = QString::fromUtf16(reinterpret_cast<ushort *>(buff));
delete [] buff;
return result;
}
/*!
\internal
Get specific property from registry.
\a devInfoSet pointer to the device information set that contains the interface
and its underlying device. Returned by SetupDiGetClassDevs() function.
\a devInfoData pointer to an SP_DEVINFO_DATA structure that defines the device instance.
this is returned by SetupDiGetDeviceInterfaceDetail() function.
\a property registry property. One of defined SPDRP_* constants.
return property string.
*/
static QString getDeviceRegistryProperty(HDEVINFO devInfoSet, PSP_DEVINFO_DATA devInfoData, DWORD property)
{
DWORD buffSize = 0;
::SetupDiGetDeviceRegistryProperty(devInfoSet, devInfoData, property, NULL, NULL, 0, &buffSize);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return QString();
BYTE *buff = new BYTE[buffSize];
::SetupDiGetDeviceRegistryProperty(devInfoSet, devInfoData, property, NULL, buff, buffSize, NULL);
QString result = QString::fromUtf16(reinterpret_cast<ushort *>(buff));
delete [] buff;
return result;
}
/*!
\internal
*/
static bool getDeviceDetailsInformation(QextPortInfo *portInfo, HDEVINFO devInfoSet, PSP_DEVINFO_DATA devInfoData
, WPARAM wParam = DBT_DEVICEARRIVAL)
{
portInfo->friendName = getDeviceRegistryProperty(devInfoSet, devInfoData, SPDRP_FRIENDLYNAME);
if (wParam == DBT_DEVICEARRIVAL)
portInfo->physName = getDeviceRegistryProperty(devInfoSet, devInfoData, SPDRP_PHYSICAL_DEVICE_OBJECT_NAME);
portInfo->enumName = getDeviceRegistryProperty(devInfoSet, devInfoData, SPDRP_ENUMERATOR_NAME);
HKEY devKey = ::SetupDiOpenDevRegKey(devInfoSet, devInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
portInfo->portName = getRegKeyValue(devKey, TEXT("PortName"));
::RegCloseKey(devKey);
QString hardwareIDs = getDeviceRegistryProperty(devInfoSet, devInfoData, SPDRP_HARDWAREID);
QRegExp idRx(QLatin1String("VID_(\\w+)&PID_(\\w+)"));
if (hardwareIDs.toUpper().contains(idRx)) {
bool dummy;
portInfo->vendorID = idRx.cap(1).toInt(&dummy, 16);
portInfo->productID = idRx.cap(2).toInt(&dummy, 16);
//qDebug() << "got vid:" << vid << "pid:" << pid;
}
return true;
}
/*!
\internal
*/
static void enumerateDevices(const GUID &guid, QList<QextPortInfo> *infoList)
{
HDEVINFO devInfoSet = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (devInfoSet != INVALID_HANDLE_VALUE) {
SP_DEVINFO_DATA devInfoData;
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for(int i = 0; ::SetupDiEnumDeviceInfo(devInfoSet, i, &devInfoData); i++) {
QextPortInfo info;
info.productID = info.vendorID = 0;
getDeviceDetailsInformation(&info, devInfoSet, &devInfoData);
infoList->append(info);
}
::SetupDiDestroyDeviceInfoList(devInfoSet);
}
}
static bool lessThan(const QextPortInfo &s1, const QextPortInfo &s2)
{
if (s1.portName.startsWith(QLatin1String("COM"))
&& s2.portName.startsWith(QLatin1String("COM"))) {
return s1.portName.mid(3).toInt()<s2.portName.mid(3).toInt();
}
return s1.portName < s2.portName;
}
/*!
Get list of ports.
return list of ports currently available in the system.
*/
QList<QextPortInfo> QextSerialEnumeratorPrivate::getPorts_sys()
{
QList<QextPortInfo> ports;
enumerateDevices(GUID_DEVINTERFACE_COMPORT, &ports);
std::sort(ports.begin(), ports.end(), lessThan);
return ports;
}
/*
Enable event-driven notifications of board discovery/removal.
*/
bool QextSerialEnumeratorPrivate::setUpNotifications_sys(bool setup)
{
#ifndef QT_GUI_LIB
Q_UNUSED(setup)
QESP_WARNING("QextSerialEnumerator: GUI not enabled - can't register for device notifications.");
return false;
#else
Q_Q(QextSerialEnumerator);
if (setup && notificationWidget) //already setup
return true;
notificationWidget = new QextSerialRegistrationWidget(this);
DEV_BROADCAST_DEVICEINTERFACE dbh;
::ZeroMemory(&dbh, sizeof(dbh));
dbh.dbcc_size = sizeof(dbh);
dbh.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
// dbh.dbcc_classguid = GUID_DEVCLASS_PORTS; //Ignored in such case
DWORD flags = DEVICE_NOTIFY_WINDOW_HANDLE|DEVICE_NOTIFY_ALL_INTERFACE_CLASSES;
if (::RegisterDeviceNotification((HWND)notificationWidget->winId(), &dbh, flags) == NULL) {
QESP_WARNING() << "RegisterDeviceNotification failed:" << GetLastError();
return false;
}
// setting up notifications doesn't tell us about devices already connected
// so get those manually
foreach (QextPortInfo port, getPorts_sys())
Q_EMIT q->deviceDiscovered(port);
return true;
#endif // QT_GUI_LIB
}
#ifdef QT_GUI_LIB
LRESULT QextSerialEnumeratorPrivate::onDeviceChanged(WPARAM wParam, LPARAM lParam)
{
if (DBT_DEVICEARRIVAL == wParam || DBT_DEVICEREMOVECOMPLETE == wParam) {
PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam;
if (pHdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
PDEV_BROADCAST_DEVICEINTERFACE pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)pHdr;
// delimiters are different across APIs...change to backslash. ugh.
QString deviceID = QString::fromUtf16(reinterpret_cast<ushort *>(pDevInf->dbcc_name));
deviceID = deviceID.toUpper().replace(QLatin1String("#"), QLatin1String("\\"));
matchAndDispatchChangedDevice(deviceID, GUID_DEVINTERFACE_COMPORT, wParam);
}
}
return 0;
}
bool QextSerialEnumeratorPrivate::matchAndDispatchChangedDevice(const QString &deviceID, const GUID &guid, WPARAM wParam)
{
Q_Q(QextSerialEnumerator);
bool rv = false;
DWORD dwFlag = (DBT_DEVICEARRIVAL == wParam) ? DIGCF_PRESENT : DIGCF_PROFILE;
HDEVINFO devInfoSet = SetupDiGetClassDevs(&guid, NULL, NULL, dwFlag | DIGCF_DEVICEINTERFACE);
if (devInfoSet != INVALID_HANDLE_VALUE) {
SP_DEVINFO_DATA spDevInfoData;
spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for(int i=0; SetupDiEnumDeviceInfo(devInfoSet, i, &spDevInfoData); i++) {
DWORD nSize = 0;
TCHAR buf[MAX_PATH];
if (SetupDiGetDeviceInstanceId(devInfoSet, &spDevInfoData, buf, MAX_PATH, &nSize)
&& deviceID.contains(QString::fromUtf16(reinterpret_cast<ushort *>(buf)))) { // we found a match
rv = true;
QextPortInfo info;
info.productID = info.vendorID = 0;
getDeviceDetailsInformation(&info, devInfoSet, &spDevInfoData, wParam);
if (wParam == DBT_DEVICEARRIVAL)
Q_EMIT q->deviceDiscovered(info);
else if (wParam == DBT_DEVICEREMOVECOMPLETE)
Q_EMIT q->deviceRemoved(info);
break;
}
}
SetupDiDestroyDeviceInfoList(devInfoSet);
}
return rv;
}
#endif //QT_GUI_LIB