📄 usb_osx.c
字号:
kr = IOCreatePlugInInterfaceForService(device, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugin, &score); if ((kr != 0) || (plugin == NULL)) { ERR("Unable to create a plug-in (%08x)\n", kr); goto error; } // Now create the device interface. result = (*plugin)->QueryInterface(plugin, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID) &dev); if ((result != 0) || (dev == NULL)) { ERR("Couldn't create a device interface (%08x)\n", (int) result); goto error; } /* * We don't need the intermediate interface after the device interface * is created. */ IODestroyPlugInInterface(plugin); // So, we have a device, finally. Grab its vitals. kr = (*dev)->GetDeviceVendor(dev, &handle->info.dev_vendor); if (kr != 0) { ERR("GetDeviceVendor"); goto error; } kr = (*dev)->GetDeviceProduct(dev, &handle->info.dev_product); if (kr != 0) { ERR("GetDeviceProduct"); goto error; } kr = (*dev)->GetDeviceClass(dev, &handle->info.dev_class); if (kr != 0) { ERR("GetDeviceClass"); goto error; } kr = (*dev)->GetDeviceSubClass(dev, &handle->info.dev_subclass); if (kr != 0) { ERR("GetDeviceSubClass"); goto error; } kr = (*dev)->GetDeviceProtocol(dev, &handle->info.dev_protocol); if (kr != 0) { ERR("GetDeviceProtocol"); goto error; } kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex); if (serialIndex > 0) { IOUSBDevRequest req; UInt16 buffer[256]; req.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice); req.bRequest = kUSBRqGetDescriptor; req.wValue = (kUSBStringDesc << 8) | serialIndex; req.wIndex = 0; req.pData = buffer; req.wLength = sizeof(buffer); kr = (*dev)->DeviceRequest(dev, &req); if (kr == kIOReturnSuccess && req.wLenDone > 0) { int i, count; // skip first word, and copy the rest to the serial string, changing shorts to bytes. count = (req.wLenDone - 1) / 2; for (i = 0; i < count; i++) handle->info.serial_number[i] = buffer[i + 1]; handle->info.serial_number[i] = 0; } } else { // device has no serial number handle->info.serial_number[0] = 0; } if (try_interfaces(dev, handle)) { goto error; } (*dev)->Release(dev); return 0; error: if (dev != NULL) { (*dev)->Release(dev); } return -1; }/** Initializes the USB system. Returns 0 on success, -1 on error. */static int init_usb(ifc_match_func callback, usb_handle **handle) { int ret = -1; CFMutableDictionaryRef matchingDict; kern_return_t result; io_iterator_t iterator; usb_handle h; h.success = 0; h.callback = callback; /* * Create our matching dictionary to find appropriate devices. * IOServiceAddMatchingNotification consumes the reference, so we * do not need to release it. */ matchingDict = IOServiceMatching(kIOUSBDeviceClassName); if (matchingDict == NULL) { ERR("Couldn't create USB matching dictionary.\n"); return -1; } result = IOServiceGetMatchingServices( kIOMasterPortDefault, matchingDict, &iterator); if (result != 0) { ERR("Could not create iterator."); return -1; } for (;;) { if (! IOIteratorIsValid(iterator)) { /* * Apple documentation advises resetting the iterator if * it should become invalid during iteration. */ IOIteratorReset(iterator); continue; } io_service_t device = IOIteratorNext(iterator); if (device == 0) { break; } usb_ifc_info info; if (try_device(device, &h) != 0) { IOObjectRelease(device); ret = -1; break; } if (h.success) { *handle = calloc(1, sizeof(usb_handle)); memcpy(*handle, &h, sizeof(usb_handle)); ret = 0; break; } IOObjectRelease(device); } IOObjectRelease(iterator); return ret;}/* * Definitions of this file's public functions. */usb_handle *usb_open(ifc_match_func callback) { usb_handle *handle = NULL; if (init_usb(callback, &handle) < 0) { /* Something went wrong initializing USB. */ return NULL; } return handle;}int usb_close(usb_handle *h) { /* TODO: Something better here? */ return 0;}int usb_read(usb_handle *h, void *data, int len) { IOReturn result; UInt32 numBytes = len; if (len == 0) { return 0; } if (h == NULL) { return -1; } if (h->interface == NULL) { ERR("usb_read interface was null\n"); return -1; } if (h->bulkIn == 0) { ERR("bulkIn endpoint not assigned\n"); return -1; } result = (*h->interface)->ReadPipe( h->interface, h->bulkIn, data, &numBytes); if (result == 0) { return (int) numBytes; } else { ERR("usb_read failed with status %x\n", result); } return -1;}int usb_write(usb_handle *h, const void *data, int len) { IOReturn result; if (len == 0) { return 0; } if (h == NULL) { return -1; } if (h->interface == NULL) { ERR("usb_write interface was null\n"); return -1; } if (h->bulkOut == 0) { ERR("bulkOut endpoint not assigned\n"); return -1; } result = (*h->interface)->WritePipe( h->interface, h->bulkOut, (void *)data, len); #if 0 if ((result == 0) && (h->zero_mask)) { /* we need 0-markers and our transfer */ if(!(len & h->zero_mask)) { result = (*h->interface)->WritePipe( h->interface, h->bulkOut, (void *)data, 0); } } #endif if (result != 0) { ERR("usb_write failed with status %x\n", result); return -1; } return len;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -