Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qextserialenumerator_win.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /****************************************************************************
  2. ** Copyright (c) 2000-2003 Wayne Roth
  3. ** Copyright (c) 2004-2007 Stefan Sander
  4. ** Copyright (c) 2007 Michal Policht
  5. ** Copyright (c) 2008 Brandon Fosdick
  6. ** Copyright (c) 2009-2010 Liam Staskawicz
  7. ** Copyright (c) 2011 Debao Zhang
  8. ** All right reserved.
  9. ** Web: http://code.google.com/p/qextserialport/
  10. **
  11. ** Permission is hereby granted, free of charge, to any person obtaining
  12. ** a copy of this software and associated documentation files (the
  13. ** "Software"), to deal in the Software without restriction, including
  14. ** without limitation the rights to use, copy, modify, merge, publish,
  15. ** distribute, sublicense, and/or sell copies of the Software, and to
  16. ** permit persons to whom the Software is furnished to do so, subject to
  17. ** the following conditions:
  18. **
  19. ** The above copyright notice and this permission notice shall be
  20. ** included in all copies or substantial portions of the Software.
  21. **
  22. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. **
  30. ****************************************************************************/
  31. #include "qextserialenumerator.h"
  32. #include "qextserialenumerator_p.h"
  33. #include <QtCore/QDebug>
  34. #include <QtCore/QMetaType>
  35. #include <QtCore/QRegExp>
  36. #include <objbase.h>
  37. #include <initguid.h>
  38. #include <setupapi.h>
  39. #include <dbt.h>
  40. #include "qextserialport.h"
  41. #ifdef QT_GUI_LIB
  42. #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
  43. #include <QtGui/QWidget>
  44. class QextSerialRegistrationWidget : public QWidget
  45. #else
  46. #include <QtGui/QWindow>
  47. class QextSerialRegistrationWidget : public QWindow
  48. #endif
  49. {
  50. public:
  51. QextSerialRegistrationWidget(QextSerialEnumeratorPrivate* qese) {
  52. this->qese = qese;
  53. }
  54. ~QextSerialRegistrationWidget() {}
  55. protected:
  56. #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
  57. bool winEvent( MSG* message, long* result ) {
  58. #else
  59. bool nativeEvent(const QByteArray & /*eventType*/, void *msg, long *result) {
  60. MSG *message = static_cast<MSG*>(msg);
  61. #endif
  62. if ( message->message == WM_DEVICECHANGE ) {
  63. qese->onDeviceChanged(message->wParam, message->lParam );
  64. *result = 1;
  65. return true;
  66. }
  67. return false;
  68. }
  69. private:
  70. QextSerialEnumeratorPrivate* qese;
  71. };
  72. #endif // QT_GUI_LIB
  73. void QextSerialEnumeratorPrivate::platformSpecificInit()
  74. {
  75. #ifdef QT_GUI_LIB
  76. notificationWidget = 0;
  77. #endif // QT_GUI_LIB
  78. }
  79. /*!
  80. default
  81. */
  82. void QextSerialEnumeratorPrivate::platformSpecificDestruct()
  83. {
  84. #ifdef QT_GUI_LIB
  85. if( notificationWidget )
  86. delete notificationWidget;
  87. #endif
  88. }
  89. // see http://msdn.microsoft.com/en-us/library/windows/hardware/ff553426(v=vs.85).aspx
  90. // for list of GUID classes
  91. #ifndef GUID_DEVCLASS_PORTS
  92. DEFINE_GUID(GUID_DEVCLASS_PORTS, 0x4D36E978, 0xE325, 0x11CE, 0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 );
  93. #endif
  94. /* Gordon Schumacher's macros for TCHAR -> QString conversions and vice versa */
  95. #ifdef UNICODE
  96. #define QStringToTCHAR(x) (wchar_t*) x.utf16()
  97. #define PQStringToTCHAR(x) (wchar_t*) x->utf16()
  98. #define TCHARToQString(x) QString::fromUtf16((ushort*)(x))
  99. #define TCHARToQStringN(x,y) QString::fromUtf16((ushort*)(x),(y))
  100. #else
  101. #define QStringToTCHAR(x) x.local8Bit().constData()
  102. #define PQStringToTCHAR(x) x->local8Bit().constData()
  103. #define TCHARToQString(x) QString::fromLocal8Bit((char*)(x))
  104. #define TCHARToQStringN(x,y) QString::fromLocal8Bit((char*)(x),(y))
  105. #endif /*UNICODE*/
  106. /*!
  107. \internal
  108. Get value of specified property from the registry.
  109. \a key handle to an open key.
  110. \a property property name.
  111. return property value.
  112. */
  113. static QString getRegKeyValue(HKEY key, LPCTSTR property)
  114. {
  115. DWORD size = 0;
  116. DWORD type;
  117. ::RegQueryValueEx(key, property, NULL, NULL, NULL, & size);
  118. BYTE* buff = new BYTE[size];
  119. QString result;
  120. if(::RegQueryValueEx(key, property, NULL, &type, buff, & size) == ERROR_SUCCESS )
  121. result = TCHARToQString(buff);
  122. ::RegCloseKey(key);
  123. delete [] buff;
  124. return result;
  125. }
  126. /*!
  127. \internal
  128. Get specific property from registry.
  129. \a devInfo pointer to the device information set that contains the interface
  130. and its underlying device. Returned by SetupDiGetClassDevs() function.
  131. \a devData pointer to an SP_DEVINFO_DATA structure that defines the device instance.
  132. this is returned by SetupDiGetDeviceInterfaceDetail() function.
  133. \a property registry property. One of defined SPDRP_* constants.
  134. return property string.
  135. */
  136. static QString getDeviceProperty(HDEVINFO devInfo, PSP_DEVINFO_DATA devData, DWORD property)
  137. {
  138. DWORD buffSize = 0;
  139. ::SetupDiGetDeviceRegistryProperty(devInfo, devData, property, NULL, NULL, 0, & buffSize);
  140. BYTE* buff = new BYTE[buffSize];
  141. ::SetupDiGetDeviceRegistryProperty(devInfo, devData, property, NULL, buff, buffSize, NULL);
  142. QString result = TCHARToQString(buff);
  143. delete [] buff;
  144. return result;
  145. }
  146. /*!
  147. \internal
  148. */
  149. static bool getDeviceDetailsWin( QextPortInfo* portInfo, HDEVINFO devInfo, PSP_DEVINFO_DATA devData
  150. , WPARAM wParam = DBT_DEVICEARRIVAL)
  151. {
  152. portInfo->friendName = getDeviceProperty(devInfo, devData, SPDRP_FRIENDLYNAME);
  153. if( wParam == DBT_DEVICEARRIVAL)
  154. portInfo->physName = getDeviceProperty(devInfo, devData, SPDRP_PHYSICAL_DEVICE_OBJECT_NAME);
  155. portInfo->enumName = getDeviceProperty(devInfo, devData, SPDRP_ENUMERATOR_NAME);
  156. QString hardwareIDs = getDeviceProperty(devInfo, devData, SPDRP_HARDWAREID);
  157. HKEY devKey = ::SetupDiOpenDevRegKey(devInfo, devData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
  158. portInfo->portName = getRegKeyValue(devKey, TEXT("PortName"));
  159. QRegExp idRx(QLatin1String("VID_(\\w+)&PID_(\\w+)"));
  160. if(hardwareIDs.toUpper().contains(idRx)) {
  161. bool dummy;
  162. portInfo->vendorID = idRx.cap(1).toInt(&dummy, 16);
  163. portInfo->productID = idRx.cap(2).toInt(&dummy, 16);
  164. //qDebug() << "got vid:" << vid << "pid:" << pid;
  165. }
  166. return true;
  167. }
  168. /*!
  169. \internal
  170. */
  171. static void enumerateDevicesWin( const GUID & guid, QList<QextPortInfo>* infoList )
  172. {
  173. HDEVINFO devInfo;
  174. if( (devInfo = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT)) != INVALID_HANDLE_VALUE) {
  175. SP_DEVINFO_DATA devInfoData;
  176. devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  177. for(int i = 0; ::SetupDiEnumDeviceInfo(devInfo, i, &devInfoData); i++) {
  178. QextPortInfo info;
  179. info.productID = info.vendorID = 0;
  180. getDeviceDetailsWin( &info, devInfo, &devInfoData );
  181. infoList->append(info);
  182. }
  183. ::SetupDiDestroyDeviceInfoList(devInfo);
  184. }
  185. }
  186. static bool lessThan(const QextPortInfo &s1, const QextPortInfo &s2)
  187. {
  188. if (s1.portName.startsWith(QLatin1String("COM"))
  189. && s2.portName.startsWith(QLatin1String("COM"))) {
  190. return s1.portName.mid(3).toInt()<s2.portName.mid(3).toInt();
  191. }
  192. return s1.portName < s2.portName;
  193. }
  194. /*!
  195. Get list of ports.
  196. return list of ports currently available in the system.
  197. */
  198. QList<QextPortInfo> QextSerialEnumeratorPrivate::getPorts_sys()
  199. {
  200. QList<QextPortInfo> ports;
  201. enumerateDevicesWin(GUID_DEVCLASS_PORTS, &ports);
  202. qSort(ports.begin(), ports.end(), lessThan);
  203. return ports;
  204. }
  205. /*
  206. Enable event-driven notifications of board discovery/removal.
  207. */
  208. bool QextSerialEnumeratorPrivate::setUpNotifications_sys(bool setup)
  209. {
  210. #ifndef QT_GUI_LIB
  211. Q_UNUSED(setup)
  212. QESP_WARNING("QextSerialEnumerator: GUI not enabled - can't register for device notifications.");
  213. return false;
  214. #else
  215. Q_Q(QextSerialEnumerator);
  216. if(setup && notificationWidget) //already setup
  217. return true;
  218. notificationWidget = new QextSerialRegistrationWidget(this);
  219. DEV_BROADCAST_DEVICEINTERFACE dbh;
  220. ::ZeroMemory(&dbh, sizeof(dbh));
  221. dbh.dbcc_size = sizeof(dbh);
  222. dbh.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  223. ::CopyMemory(&dbh.dbcc_classguid, &GUID_DEVCLASS_PORTS, sizeof(GUID));
  224. if(::RegisterDeviceNotification((HWND)notificationWidget->winId(), &dbh, DEVICE_NOTIFY_WINDOW_HANDLE ) == NULL) {
  225. QESP_WARNING() << "RegisterDeviceNotification failed:" << GetLastError();
  226. return false;
  227. }
  228. // setting up notifications doesn't tell us about devices already connected
  229. // so get those manually
  230. foreach(QextPortInfo port, getPorts_sys())
  231. Q_EMIT q->deviceDiscovered(port);
  232. return true;
  233. #endif // QT_GUI_LIB
  234. }
  235. LRESULT QextSerialEnumeratorPrivate::onDeviceChanged( WPARAM wParam, LPARAM lParam )
  236. {
  237. if (DBT_DEVICEARRIVAL == wParam || DBT_DEVICEREMOVECOMPLETE == wParam ) {
  238. PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam;
  239. if(pHdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE ) {
  240. PDEV_BROADCAST_DEVICEINTERFACE pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)pHdr;
  241. // delimiters are different across APIs...change to backslash. ugh.
  242. QString deviceID = TCHARToQString(pDevInf->dbcc_name).toUpper().replace(QLatin1String("#"), QLatin1String("\\"));
  243. matchAndDispatchChangedDevice(deviceID, GUID_DEVCLASS_PORTS, wParam);
  244. }
  245. }
  246. return 0;
  247. }
  248. bool QextSerialEnumeratorPrivate::matchAndDispatchChangedDevice(const QString & deviceID, const GUID & guid, WPARAM wParam)
  249. {
  250. Q_Q(QextSerialEnumerator);
  251. bool rv = false;
  252. DWORD dwFlag = (DBT_DEVICEARRIVAL == wParam) ? DIGCF_PRESENT : DIGCF_ALLCLASSES;
  253. HDEVINFO devInfo;
  254. if( (devInfo = SetupDiGetClassDevs(&guid,NULL,NULL,dwFlag)) != INVALID_HANDLE_VALUE ) {
  255. SP_DEVINFO_DATA spDevInfoData;
  256. spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  257. for(int i=0; SetupDiEnumDeviceInfo(devInfo, i, &spDevInfoData); i++) {
  258. DWORD nSize=0 ;
  259. TCHAR buf[MAX_PATH];
  260. if ( SetupDiGetDeviceInstanceId(devInfo, &spDevInfoData, buf, MAX_PATH, &nSize) &&
  261. deviceID.contains(TCHARToQString(buf))) { // we found a match
  262. rv = true;
  263. QextPortInfo info;
  264. info.productID = info.vendorID = 0;
  265. getDeviceDetailsWin( &info, devInfo, &spDevInfoData, wParam );
  266. if( wParam == DBT_DEVICEARRIVAL )
  267. Q_EMIT q->deviceDiscovered(info);
  268. else if( wParam == DBT_DEVICEREMOVECOMPLETE )
  269. Q_EMIT q->deviceRemoved(info);
  270. break;
  271. }
  272. }
  273. SetupDiDestroyDeviceInfoList(devInfo);
  274. }
  275. return rv;
  276. }