Typica is a free program for professional coffee roasters. https://typica.us
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

qextserialenumerator_win.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <algorithm>
  37. #include <objbase.h>
  38. #include <initguid.h>
  39. #include <setupapi.h>
  40. #include <dbt.h>
  41. #ifdef QT_GUI_LIB
  42. /*!
  43. \internal
  44. \class QextSerialRegistrationWidget
  45. Internal window which is used to receive device arrvial and removal message.
  46. */
  47. #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
  48. #include <QtGui/QWidget>
  49. class QextSerialRegistrationWidget : public QWidget
  50. #else
  51. #include <QtGui/QWindow>
  52. class QextSerialRegistrationWidget : public QWindow
  53. #endif
  54. {
  55. public:
  56. QextSerialRegistrationWidget(QextSerialEnumeratorPrivate *qese) {
  57. this->qese = qese;
  58. }
  59. ~QextSerialRegistrationWidget() {}
  60. protected:
  61. #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
  62. bool winEvent(MSG *message, long *result) {
  63. #else
  64. bool nativeEvent(const QByteArray & /*eventType*/, void *msg, long *result) {
  65. MSG *message = static_cast<MSG *>(msg);
  66. #endif
  67. if (message->message == WM_DEVICECHANGE) {
  68. qese->onDeviceChanged(message->wParam, message->lParam);
  69. *result = 1;
  70. return true;
  71. }
  72. return false;
  73. }
  74. private:
  75. QextSerialEnumeratorPrivate *qese;
  76. };
  77. #endif // QT_GUI_LIB
  78. void QextSerialEnumeratorPrivate::init_sys()
  79. {
  80. #ifdef QT_GUI_LIB
  81. notificationWidget = 0;
  82. #endif // QT_GUI_LIB
  83. }
  84. /*!
  85. default
  86. */
  87. void QextSerialEnumeratorPrivate::destroy_sys()
  88. {
  89. #ifdef QT_GUI_LIB
  90. if (notificationWidget)
  91. delete notificationWidget;
  92. #endif
  93. }
  94. #ifndef GUID_DEVINTERFACE_COMPORT
  95. DEFINE_GUID(GUID_DEVINTERFACE_COMPORT, 0x86e0d1e0L, 0x8089, 0x11d0, 0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73);
  96. #endif
  97. /*!
  98. \internal
  99. Get value of specified property from the registry.
  100. \a key handle to an open key.
  101. \a property property name.
  102. return property value.
  103. */
  104. static QString getRegKeyValue(HKEY key, LPCTSTR property)
  105. {
  106. DWORD size = 0;
  107. DWORD type;
  108. if (::RegQueryValueEx(key, property, NULL, NULL, NULL, &size) != ERROR_SUCCESS)
  109. return QString();
  110. BYTE *buff = new BYTE[size];
  111. QString result;
  112. if (::RegQueryValueEx(key, property, NULL, &type, buff, &size) == ERROR_SUCCESS)
  113. result = QString::fromUtf16(reinterpret_cast<ushort *>(buff));
  114. delete [] buff;
  115. return result;
  116. }
  117. /*!
  118. \internal
  119. Get specific property from registry.
  120. \a devInfoSet pointer to the device information set that contains the interface
  121. and its underlying device. Returned by SetupDiGetClassDevs() function.
  122. \a devInfoData pointer to an SP_DEVINFO_DATA structure that defines the device instance.
  123. this is returned by SetupDiGetDeviceInterfaceDetail() function.
  124. \a property registry property. One of defined SPDRP_* constants.
  125. return property string.
  126. */
  127. static QString getDeviceRegistryProperty(HDEVINFO devInfoSet, PSP_DEVINFO_DATA devInfoData, DWORD property)
  128. {
  129. DWORD buffSize = 0;
  130. ::SetupDiGetDeviceRegistryProperty(devInfoSet, devInfoData, property, NULL, NULL, 0, &buffSize);
  131. if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  132. return QString();
  133. BYTE *buff = new BYTE[buffSize];
  134. ::SetupDiGetDeviceRegistryProperty(devInfoSet, devInfoData, property, NULL, buff, buffSize, NULL);
  135. QString result = QString::fromUtf16(reinterpret_cast<ushort *>(buff));
  136. delete [] buff;
  137. return result;
  138. }
  139. /*!
  140. \internal
  141. */
  142. static bool getDeviceDetailsInformation(QextPortInfo *portInfo, HDEVINFO devInfoSet, PSP_DEVINFO_DATA devInfoData
  143. , WPARAM wParam = DBT_DEVICEARRIVAL)
  144. {
  145. portInfo->friendName = getDeviceRegistryProperty(devInfoSet, devInfoData, SPDRP_FRIENDLYNAME);
  146. if (wParam == DBT_DEVICEARRIVAL)
  147. portInfo->physName = getDeviceRegistryProperty(devInfoSet, devInfoData, SPDRP_PHYSICAL_DEVICE_OBJECT_NAME);
  148. portInfo->enumName = getDeviceRegistryProperty(devInfoSet, devInfoData, SPDRP_ENUMERATOR_NAME);
  149. HKEY devKey = ::SetupDiOpenDevRegKey(devInfoSet, devInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
  150. portInfo->portName = getRegKeyValue(devKey, TEXT("PortName"));
  151. ::RegCloseKey(devKey);
  152. QString hardwareIDs = getDeviceRegistryProperty(devInfoSet, devInfoData, SPDRP_HARDWAREID);
  153. QRegExp idRx(QLatin1String("VID_(\\w+)&PID_(\\w+)"));
  154. if (hardwareIDs.toUpper().contains(idRx)) {
  155. bool dummy;
  156. portInfo->vendorID = idRx.cap(1).toInt(&dummy, 16);
  157. portInfo->productID = idRx.cap(2).toInt(&dummy, 16);
  158. //qDebug() << "got vid:" << vid << "pid:" << pid;
  159. }
  160. return true;
  161. }
  162. /*!
  163. \internal
  164. */
  165. static void enumerateDevices(const GUID &guid, QList<QextPortInfo> *infoList)
  166. {
  167. HDEVINFO devInfoSet = ::SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  168. if (devInfoSet != INVALID_HANDLE_VALUE) {
  169. SP_DEVINFO_DATA devInfoData;
  170. devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  171. for(int i = 0; ::SetupDiEnumDeviceInfo(devInfoSet, i, &devInfoData); i++) {
  172. QextPortInfo info;
  173. info.productID = info.vendorID = 0;
  174. getDeviceDetailsInformation(&info, devInfoSet, &devInfoData);
  175. infoList->append(info);
  176. }
  177. ::SetupDiDestroyDeviceInfoList(devInfoSet);
  178. }
  179. }
  180. static bool lessThan(const QextPortInfo &s1, const QextPortInfo &s2)
  181. {
  182. if (s1.portName.startsWith(QLatin1String("COM"))
  183. && s2.portName.startsWith(QLatin1String("COM"))) {
  184. return s1.portName.mid(3).toInt()<s2.portName.mid(3).toInt();
  185. }
  186. return s1.portName < s2.portName;
  187. }
  188. /*!
  189. Get list of ports.
  190. return list of ports currently available in the system.
  191. */
  192. QList<QextPortInfo> QextSerialEnumeratorPrivate::getPorts_sys()
  193. {
  194. QList<QextPortInfo> ports;
  195. enumerateDevices(GUID_DEVINTERFACE_COMPORT, &ports);
  196. std::sort(ports.begin(), ports.end(), lessThan);
  197. return ports;
  198. }
  199. /*
  200. Enable event-driven notifications of board discovery/removal.
  201. */
  202. bool QextSerialEnumeratorPrivate::setUpNotifications_sys(bool setup)
  203. {
  204. #ifndef QT_GUI_LIB
  205. Q_UNUSED(setup)
  206. QESP_WARNING("QextSerialEnumerator: GUI not enabled - can't register for device notifications.");
  207. return false;
  208. #else
  209. Q_Q(QextSerialEnumerator);
  210. if (setup && notificationWidget) //already setup
  211. return true;
  212. notificationWidget = new QextSerialRegistrationWidget(this);
  213. DEV_BROADCAST_DEVICEINTERFACE dbh;
  214. ::ZeroMemory(&dbh, sizeof(dbh));
  215. dbh.dbcc_size = sizeof(dbh);
  216. dbh.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  217. // dbh.dbcc_classguid = GUID_DEVCLASS_PORTS; //Ignored in such case
  218. DWORD flags = DEVICE_NOTIFY_WINDOW_HANDLE|DEVICE_NOTIFY_ALL_INTERFACE_CLASSES;
  219. if (::RegisterDeviceNotification((HWND)notificationWidget->winId(), &dbh, flags) == NULL) {
  220. QESP_WARNING() << "RegisterDeviceNotification failed:" << GetLastError();
  221. return false;
  222. }
  223. // setting up notifications doesn't tell us about devices already connected
  224. // so get those manually
  225. foreach (QextPortInfo port, getPorts_sys())
  226. Q_EMIT q->deviceDiscovered(port);
  227. return true;
  228. #endif // QT_GUI_LIB
  229. }
  230. #ifdef QT_GUI_LIB
  231. LRESULT QextSerialEnumeratorPrivate::onDeviceChanged(WPARAM wParam, LPARAM lParam)
  232. {
  233. if (DBT_DEVICEARRIVAL == wParam || DBT_DEVICEREMOVECOMPLETE == wParam) {
  234. PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam;
  235. if (pHdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
  236. PDEV_BROADCAST_DEVICEINTERFACE pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)pHdr;
  237. // delimiters are different across APIs...change to backslash. ugh.
  238. QString deviceID = QString::fromUtf16(reinterpret_cast<ushort *>(pDevInf->dbcc_name));
  239. deviceID = deviceID.toUpper().replace(QLatin1String("#"), QLatin1String("\\"));
  240. matchAndDispatchChangedDevice(deviceID, GUID_DEVINTERFACE_COMPORT, wParam);
  241. }
  242. }
  243. return 0;
  244. }
  245. bool QextSerialEnumeratorPrivate::matchAndDispatchChangedDevice(const QString &deviceID, const GUID &guid, WPARAM wParam)
  246. {
  247. Q_Q(QextSerialEnumerator);
  248. bool rv = false;
  249. DWORD dwFlag = (DBT_DEVICEARRIVAL == wParam) ? DIGCF_PRESENT : DIGCF_PROFILE;
  250. HDEVINFO devInfoSet = SetupDiGetClassDevs(&guid, NULL, NULL, dwFlag | DIGCF_DEVICEINTERFACE);
  251. if (devInfoSet != INVALID_HANDLE_VALUE) {
  252. SP_DEVINFO_DATA spDevInfoData;
  253. spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  254. for(int i=0; SetupDiEnumDeviceInfo(devInfoSet, i, &spDevInfoData); i++) {
  255. DWORD nSize = 0;
  256. TCHAR buf[MAX_PATH];
  257. if (SetupDiGetDeviceInstanceId(devInfoSet, &spDevInfoData, buf, MAX_PATH, &nSize)
  258. && deviceID.contains(QString::fromUtf16(reinterpret_cast<ushort *>(buf)))) { // we found a match
  259. rv = true;
  260. QextPortInfo info;
  261. info.productID = info.vendorID = 0;
  262. getDeviceDetailsInformation(&info, devInfoSet, &spDevInfoData, wParam);
  263. if (wParam == DBT_DEVICEARRIVAL)
  264. Q_EMIT q->deviceDiscovered(info);
  265. else if (wParam == DBT_DEVICEREMOVECOMPLETE)
  266. Q_EMIT q->deviceRemoved(info);
  267. break;
  268. }
  269. }
  270. SetupDiDestroyDeviceInfoList(devInfoSet);
  271. }
  272. return rv;
  273. }
  274. #endif //QT_GUI_LIB