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_osx.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <IOKit/serial/IOSerialKeys.h>
  35. #include <CoreFoundation/CFNumber.h>
  36. #include <sys/param.h>
  37. void QextSerialEnumeratorPrivate::platformSpecificInit()
  38. {
  39. }
  40. void QextSerialEnumeratorPrivate::platformSpecificDestruct()
  41. {
  42. IONotificationPortDestroy( notificationPortRef );
  43. }
  44. // static
  45. QList<QextPortInfo> QextSerialEnumeratorPrivate::getPorts_sys()
  46. {
  47. QList<QextPortInfo> infoList;
  48. io_iterator_t serialPortIterator = 0;
  49. kern_return_t kernResult = KERN_FAILURE;
  50. CFMutableDictionaryRef matchingDictionary;
  51. // first try to get any serialbsd devices, then try any USBCDC devices
  52. if( !(matchingDictionary = IOServiceMatching(kIOSerialBSDServiceValue) ) ) {
  53. QESP_WARNING("IOServiceMatching returned a NULL dictionary.");
  54. return infoList;
  55. }
  56. CFDictionaryAddValue(matchingDictionary, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
  57. // then create the iterator with all the matching devices
  58. if( IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &serialPortIterator) != KERN_SUCCESS ) {
  59. qCritical() << "IOServiceGetMatchingServices failed, returned" << kernResult;
  60. return infoList;
  61. }
  62. iterateServicesOSX(serialPortIterator, infoList);
  63. IOObjectRelease(serialPortIterator);
  64. serialPortIterator = 0;
  65. if( !(matchingDictionary = IOServiceNameMatching("AppleUSBCDC")) ) {
  66. QESP_WARNING("IOServiceNameMatching returned a NULL dictionary.");
  67. return infoList;
  68. }
  69. if( IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &serialPortIterator) != KERN_SUCCESS ) {
  70. qCritical() << "IOServiceGetMatchingServices failed, returned" << kernResult;
  71. return infoList;
  72. }
  73. iterateServicesOSX(serialPortIterator, infoList);
  74. IOObjectRelease(serialPortIterator);
  75. return infoList;
  76. }
  77. void QextSerialEnumeratorPrivate::iterateServicesOSX(io_object_t service, QList<QextPortInfo> & infoList)
  78. {
  79. // Iterate through all modems found.
  80. io_object_t usbService;
  81. while( ( usbService = IOIteratorNext(service) ) )
  82. {
  83. QextPortInfo info;
  84. info.vendorID = 0;
  85. info.productID = 0;
  86. getServiceDetailsOSX( usbService, &info );
  87. infoList.append(info);
  88. }
  89. }
  90. bool QextSerialEnumeratorPrivate::getServiceDetailsOSX( io_object_t service, QextPortInfo* portInfo )
  91. {
  92. bool retval = true;
  93. CFTypeRef bsdPathAsCFString = NULL;
  94. CFTypeRef productNameAsCFString = NULL;
  95. CFTypeRef vendorIdAsCFNumber = NULL;
  96. CFTypeRef productIdAsCFNumber = NULL;
  97. // check the name of the modem's callout device
  98. bsdPathAsCFString = IORegistryEntryCreateCFProperty(service, CFSTR(kIOCalloutDeviceKey),
  99. kCFAllocatorDefault, 0);
  100. // wander up the hierarchy until we find the level that can give us the
  101. // vendor/product IDs and the product name, if available
  102. io_registry_entry_t parent;
  103. kern_return_t kernResult = IORegistryEntryGetParentEntry(service, kIOServicePlane, &parent);
  104. while( kernResult == KERN_SUCCESS && !vendorIdAsCFNumber && !productIdAsCFNumber )
  105. {
  106. if(!productNameAsCFString)
  107. productNameAsCFString = IORegistryEntrySearchCFProperty(parent,
  108. kIOServicePlane,
  109. CFSTR("Product Name"),
  110. kCFAllocatorDefault, 0);
  111. vendorIdAsCFNumber = IORegistryEntrySearchCFProperty(parent,
  112. kIOServicePlane,
  113. CFSTR(kUSBVendorID),
  114. kCFAllocatorDefault, 0);
  115. productIdAsCFNumber = IORegistryEntrySearchCFProperty(parent,
  116. kIOServicePlane,
  117. CFSTR(kUSBProductID),
  118. kCFAllocatorDefault, 0);
  119. io_registry_entry_t oldparent = parent;
  120. kernResult = IORegistryEntryGetParentEntry(parent, kIOServicePlane, &parent);
  121. IOObjectRelease(oldparent);
  122. }
  123. io_string_t ioPathName;
  124. IORegistryEntryGetPath( service, kIOServicePlane, ioPathName );
  125. portInfo->physName = ioPathName;
  126. if( bsdPathAsCFString )
  127. {
  128. char path[MAXPATHLEN];
  129. if( CFStringGetCString((CFStringRef)bsdPathAsCFString, path,
  130. PATH_MAX, kCFStringEncodingUTF8) )
  131. portInfo->portName = path;
  132. CFRelease(bsdPathAsCFString);
  133. }
  134. if(productNameAsCFString)
  135. {
  136. char productName[MAXPATHLEN];
  137. if( CFStringGetCString((CFStringRef)productNameAsCFString, productName,
  138. PATH_MAX, kCFStringEncodingUTF8) )
  139. portInfo->friendName = productName;
  140. CFRelease(productNameAsCFString);
  141. }
  142. if(vendorIdAsCFNumber)
  143. {
  144. SInt32 vID;
  145. if(CFNumberGetValue((CFNumberRef)vendorIdAsCFNumber, kCFNumberSInt32Type, &vID))
  146. portInfo->vendorID = vID;
  147. CFRelease(vendorIdAsCFNumber);
  148. }
  149. if(productIdAsCFNumber)
  150. {
  151. SInt32 pID;
  152. if(CFNumberGetValue((CFNumberRef)productIdAsCFNumber, kCFNumberSInt32Type, &pID))
  153. portInfo->productID = pID;
  154. CFRelease(productIdAsCFNumber);
  155. }
  156. IOObjectRelease(service);
  157. return retval;
  158. }
  159. // IOKit callbacks registered via setupNotifications()
  160. void deviceDiscoveredCallbackOSX( void *ctxt, io_iterator_t serialPortIterator )
  161. {
  162. QextSerialEnumeratorPrivate* d = (QextSerialEnumeratorPrivate*)ctxt;
  163. io_object_t serialService;
  164. while ((serialService = IOIteratorNext(serialPortIterator)))
  165. d->onDeviceDiscoveredOSX(serialService);
  166. }
  167. void deviceTerminatedCallbackOSX( void *ctxt, io_iterator_t serialPortIterator )
  168. {
  169. QextSerialEnumeratorPrivate* d = (QextSerialEnumeratorPrivate*)ctxt;
  170. io_object_t serialService;
  171. while ((serialService = IOIteratorNext(serialPortIterator)))
  172. d->onDeviceTerminatedOSX(serialService);
  173. }
  174. /*
  175. A device has been discovered via IOKit.
  176. Create a QextPortInfo if possible, and emit the signal indicating that we've found it.
  177. */
  178. void QextSerialEnumeratorPrivate::onDeviceDiscoveredOSX( io_object_t service )
  179. {
  180. Q_Q(QextSerialEnumerator);
  181. QextPortInfo info;
  182. info.vendorID = 0;
  183. info.productID = 0;
  184. if( getServiceDetailsOSX( service, &info ) )
  185. Q_EMIT q->deviceDiscovered( info );
  186. }
  187. /*
  188. Notification via IOKit that a device has been removed.
  189. Create a QextPortInfo if possible, and emit the signal indicating that it's gone.
  190. */
  191. void QextSerialEnumeratorPrivate::onDeviceTerminatedOSX( io_object_t service )
  192. {
  193. Q_Q(QextSerialEnumerator);
  194. QextPortInfo info;
  195. info.vendorID = 0;
  196. info.productID = 0;
  197. if( getServiceDetailsOSX( service, &info ) )
  198. Q_EMIT q->deviceRemoved( info );
  199. }
  200. /*
  201. Create matching dictionaries for the devices we want to get notifications for,
  202. and add them to the current run loop. Invoke the callbacks that will be responding
  203. to these notifications once to arm them, and discover any devices that
  204. are currently connected at the time notifications are setup.
  205. */
  206. bool QextSerialEnumeratorPrivate::setUpNotifications_sys(bool setup)
  207. {
  208. kern_return_t kernResult;
  209. mach_port_t masterPort;
  210. CFRunLoopSourceRef notificationRunLoopSource;
  211. CFMutableDictionaryRef classesToMatch;
  212. CFMutableDictionaryRef cdcClassesToMatch;
  213. io_iterator_t portIterator;
  214. kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
  215. if (KERN_SUCCESS != kernResult) {
  216. qDebug() << "IOMasterPort returned:" << kernResult;
  217. return false;
  218. }
  219. classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
  220. if (classesToMatch == NULL)
  221. qDebug("IOServiceMatching returned a NULL dictionary.");
  222. else
  223. CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
  224. if( !(cdcClassesToMatch = IOServiceNameMatching("AppleUSBCDC") ) ) {
  225. QESP_WARNING("couldn't create cdc matching dict");
  226. return false;
  227. }
  228. // Retain an additional reference since each call to IOServiceAddMatchingNotification consumes one.
  229. classesToMatch = (CFMutableDictionaryRef) CFRetain(classesToMatch);
  230. cdcClassesToMatch = (CFMutableDictionaryRef) CFRetain(cdcClassesToMatch);
  231. notificationPortRef = IONotificationPortCreate(masterPort);
  232. if(notificationPortRef == NULL) {
  233. qDebug("IONotificationPortCreate return a NULL IONotificationPortRef.");
  234. return false;
  235. }
  236. notificationRunLoopSource = IONotificationPortGetRunLoopSource(notificationPortRef);
  237. if (notificationRunLoopSource == NULL) {
  238. qDebug("IONotificationPortGetRunLoopSource returned NULL CFRunLoopSourceRef.");
  239. return false;
  240. }
  241. CFRunLoopAddSource(CFRunLoopGetCurrent(), notificationRunLoopSource, kCFRunLoopDefaultMode);
  242. kernResult = IOServiceAddMatchingNotification(notificationPortRef, kIOMatchedNotification, classesToMatch,
  243. deviceDiscoveredCallbackOSX, this, &portIterator);
  244. if (kernResult != KERN_SUCCESS) {
  245. qDebug() << "IOServiceAddMatchingNotification return:" << kernResult;
  246. return false;
  247. }
  248. // arm the callback, and grab any devices that are already connected
  249. deviceDiscoveredCallbackOSX( this, portIterator );
  250. kernResult = IOServiceAddMatchingNotification(notificationPortRef, kIOMatchedNotification, cdcClassesToMatch,
  251. deviceDiscoveredCallbackOSX, this, &portIterator);
  252. if (kernResult != KERN_SUCCESS) {
  253. qDebug() << "IOServiceAddMatchingNotification return:" << kernResult;
  254. return false;
  255. }
  256. // arm the callback, and grab any devices that are already connected
  257. deviceDiscoveredCallbackOSX( this, portIterator );
  258. kernResult = IOServiceAddMatchingNotification(notificationPortRef, kIOTerminatedNotification, classesToMatch,
  259. deviceTerminatedCallbackOSX, this, &portIterator);
  260. if (kernResult != KERN_SUCCESS) {
  261. qDebug() << "IOServiceAddMatchingNotification return:" << kernResult;
  262. return false;
  263. }
  264. // arm the callback, and clear any devices that are terminated
  265. deviceTerminatedCallbackOSX( this, portIterator );
  266. kernResult = IOServiceAddMatchingNotification(notificationPortRef, kIOTerminatedNotification, cdcClassesToMatch,
  267. deviceTerminatedCallbackOSX, this, &portIterator);
  268. if (kernResult != KERN_SUCCESS) {
  269. qDebug() << "IOServiceAddMatchingNotification return:" << kernResult;
  270. return false;
  271. }
  272. // arm the callback, and clear any devices that are terminated
  273. deviceTerminatedCallbackOSX( this, portIterator );
  274. return true;
  275. }