-
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathdarwin_list.cpp
More file actions
343 lines (271 loc) · 11.1 KB
/
darwin_list.cpp
File metadata and controls
343 lines (271 loc) · 11.1 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "./darwin_list.h"
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/usb/IOUSBLib.h>
#include <IOKit/serial/IOSerialKeys.h>
#if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4)
#include <sys/ioctl.h>
#include <IOKit/serial/ioss.h>
#endif
#include <string>
#include <list>
uv_mutex_t list_mutex;
Boolean lockInitialised = FALSE;
Napi::Value List(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
// callback
if (!info[0].IsFunction()) {
Napi::TypeError::New(env, "First argument must be a function").ThrowAsJavaScriptException();
return env.Null();
}
Napi::Function callback = info[0].As<Napi::Function>();
ListBaton* baton = new ListBaton(callback);
snprintf(baton->errorString, sizeof(baton->errorString), "");
baton->Queue();
return env.Undefined();
}
void setIfNotEmpty(Napi::Object item, std::string key, const char *value) {
Napi::Env env = item.Env();
Napi::String v8key = Napi::String::New(env, key);
if (strlen(value) > 0) {
(item).Set(v8key, Napi::String::New(env, value));
} else {
(item).Set(v8key, env.Undefined());
}
}
// Function prototypes
static kern_return_t FindModems(io_iterator_t *matchingServices);
static io_service_t GetUsbDevice(io_service_t service);
static stDeviceListItem* GetSerialDevices();
static kern_return_t FindModems(io_iterator_t *matchingServices) {
kern_return_t kernResult;
CFMutableDictionaryRef classesToMatch;
classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
if (classesToMatch != NULL) {
CFDictionarySetValue(classesToMatch,
CFSTR(kIOSerialBSDTypeKey),
CFSTR(kIOSerialBSDAllTypes));
}
kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatch, matchingServices);
return kernResult;
}
static io_service_t GetUsbDevice(io_service_t service) {
IOReturn status;
io_iterator_t iterator = 0;
io_service_t device = 0;
if (!service) {
return device;
}
status = IORegistryEntryCreateIterator(service,
kIOServicePlane,
(kIORegistryIterateParents | kIORegistryIterateRecursively),
&iterator);
if (status == kIOReturnSuccess) {
io_service_t currentService;
while ((currentService = IOIteratorNext(iterator)) && device == 0) {
io_name_t serviceName;
status = IORegistryEntryGetNameInPlane(currentService, kIOServicePlane, serviceName);
if (status == kIOReturnSuccess && IOObjectConformsTo(currentService, kIOUSBDeviceClassName)) {
device = currentService;
} else {
// Release the service object which is no longer needed
(void) IOObjectRelease(currentService);
}
}
// Release the iterator
(void) IOObjectRelease(iterator);
}
return device;
}
static void ExtractUsbInformation(stSerialDevice *serialDevice, IOUSBDeviceInterface **deviceInterface) {
kern_return_t kernResult;
UInt32 locationID;
kernResult = (*deviceInterface)->GetLocationID(deviceInterface, &locationID);
if (KERN_SUCCESS == kernResult) {
snprintf(serialDevice->locationId, sizeof(serialDevice->locationId), "%08x", locationID);
}
UInt16 vendorID;
kernResult = (*deviceInterface)->GetDeviceVendor(deviceInterface, &vendorID);
if (KERN_SUCCESS == kernResult) {
snprintf(serialDevice->vendorId, sizeof(serialDevice->vendorId), "%04x", vendorID);
}
UInt16 productID;
kernResult = (*deviceInterface)->GetDeviceProduct(deviceInterface, &productID);
if (KERN_SUCCESS == kernResult) {
snprintf(serialDevice->productId, sizeof(serialDevice->productId), "%04x", productID);
}
}
static stDeviceListItem* GetSerialDevices() {
char bsdPath[MAXPATHLEN];
io_iterator_t serialPortIterator;
FindModems(&serialPortIterator);
kern_return_t kernResult = KERN_FAILURE;
Boolean modemFound = false;
// Initialize the returned path
*bsdPath = '\0';
stDeviceListItem* devices = NULL;
stDeviceListItem* lastDevice = NULL;
int length = 0;
io_service_t modemService;
while ((modemService = IOIteratorNext(serialPortIterator))) {
CFTypeRef bsdPathAsCFString;
bsdPathAsCFString = IORegistryEntrySearchCFProperty(
modemService,
kIOServicePlane,
CFSTR(kIODialinDeviceKey),
kCFAllocatorDefault,
kIORegistryIterateRecursively);
if (bsdPathAsCFString) {
Boolean result;
// Convert the path from a CFString to a C (NUL-terminated)
result = CFStringGetCString((CFStringRef) bsdPathAsCFString,
bsdPath,
sizeof(bsdPath),
kCFStringEncodingUTF8);
CFRelease(bsdPathAsCFString);
if (result) {
stDeviceListItem *deviceListItem = reinterpret_cast<stDeviceListItem*>( malloc(sizeof(stDeviceListItem)));
stSerialDevice *serialDevice = &(deviceListItem->value);
snprintf(serialDevice->port, sizeof(serialDevice->port), "%s", bsdPath);
memset(serialDevice->locationId, 0, sizeof(serialDevice->locationId));
memset(serialDevice->vendorId, 0, sizeof(serialDevice->vendorId));
memset(serialDevice->productId, 0, sizeof(serialDevice->productId));
serialDevice->manufacturer[0] = '\0';
serialDevice->product[0] = '\0';
serialDevice->serialNumber[0] = '\0';
deviceListItem->next = NULL;
deviceListItem->length = &length;
if (devices == NULL) {
devices = deviceListItem;
} else {
lastDevice->next = deviceListItem;
}
lastDevice = deviceListItem;
length++;
modemFound = true;
kernResult = KERN_SUCCESS;
uv_mutex_lock(&list_mutex);
io_service_t device = GetUsbDevice(modemService);
if (device) {
CFStringRef manufacturerAsCFString = (CFStringRef) IORegistryEntryCreateCFProperty(device,
CFSTR(kUSBVendorString),
kCFAllocatorDefault,
0);
if (manufacturerAsCFString) {
Boolean result;
char manufacturer[MAXPATHLEN];
// Convert from a CFString to a C (NUL-terminated)
result = CFStringGetCString(manufacturerAsCFString,
manufacturer,
sizeof(manufacturer),
kCFStringEncodingUTF8);
if (result) {
snprintf(serialDevice->manufacturer, sizeof(serialDevice->manufacturer), "%s", manufacturer);
}
CFRelease(manufacturerAsCFString);
}
CFStringRef productAsCFString = (CFStringRef) IORegistryEntryCreateCFProperty(device,
CFSTR(kUSBProductString),
kCFAllocatorDefault,
0);
if (productAsCFString) {
Boolean result;
char product[MAXPATHLEN];
// Convert from a CFString to a C (NUL-terminated)
result = CFStringGetCString(productAsCFString,
product,
sizeof(product),
kCFStringEncodingUTF8);
if (result) {
snprintf(serialDevice->product, sizeof(serialDevice->product), "%s", product);
}
CFRelease(productAsCFString);
}
CFStringRef serialNumberAsCFString = (CFStringRef) IORegistryEntrySearchCFProperty(device,
kIOServicePlane,
CFSTR(kUSBSerialNumberString),
kCFAllocatorDefault,
kIORegistryIterateRecursively);
if (serialNumberAsCFString) {
Boolean result;
char serialNumber[MAXPATHLEN];
// Convert from a CFString to a C (NUL-terminated)
result = CFStringGetCString(serialNumberAsCFString,
serialNumber,
sizeof(serialNumber),
kCFStringEncodingUTF8);
if (result) {
snprintf(serialDevice->serialNumber, sizeof(serialDevice->serialNumber), "%s", serialNumber);
}
CFRelease(serialNumberAsCFString);
}
IOCFPlugInInterface **plugInInterface = NULL;
SInt32 score;
HRESULT res;
IOUSBDeviceInterface **deviceInterface = NULL;
kernResult = IOCreatePlugInInterfaceForService(device, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID,
&plugInInterface, &score);
if ((kIOReturnSuccess == kernResult) && plugInInterface) {
// Use the plugin interface to retrieve the device interface.
res = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
reinterpret_cast<LPVOID*> (&deviceInterface));
// Now done with the plugin interface.
(*plugInInterface)->Release(plugInInterface);
if (!res && deviceInterface != NULL) {
// Extract the desired Information
ExtractUsbInformation(serialDevice, deviceInterface);
// Release the Interface
(*deviceInterface)->Release(deviceInterface);
}
}
// Release the device
(void) IOObjectRelease(device);
}
uv_mutex_unlock(&list_mutex);
}
}
// Release the io_service_t now that we are done with it.
(void) IOObjectRelease(modemService);
}
IOObjectRelease(serialPortIterator); // Release the iterator.
return devices;
}
void ListBaton::Execute() {
if (!lockInitialised) {
uv_mutex_init(&list_mutex);
lockInitialised = TRUE;
}
stDeviceListItem* devices = GetSerialDevices();
if (devices != NULL && *(devices->length) > 0) {
stDeviceListItem* next = devices;
for (int i = 0, len = *(devices->length); i < len; i++) {
stSerialDevice device = (* next).value;
ListResultItem* resultItem = new ListResultItem();
resultItem->path = device.port;
if (*device.locationId) {
resultItem->locationId = device.locationId;
}
if (*device.vendorId) {
resultItem->vendorId = device.vendorId;
}
if (*device.productId) {
resultItem->productId = device.productId;
}
if (*device.manufacturer) {
resultItem->manufacturer = device.manufacturer;
}
if (*device.product) {
resultItem->product = device.product;
}
if (*device.serialNumber) {
resultItem->serialNumber = device.serialNumber;
}
results.push_back(resultItem);
stDeviceListItem* current = next;
if (next->next != NULL) {
next = next->next;
}
free(current);
}
}
}