Debian Codegraph @

interfaces/qextserialport/qextserialenumerator_osx.cpp

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