static int make_path(IOHIDDeviceRef device, char *buf, size_t len) { int res; unsigned short vid, pid; char transport[32]; int32_t location; buf[0] = '\0'; res = get_string_property_utf8( device, CFSTR(kIOHIDTransportKey), transport, sizeof(transport)); if (!res) return -1; location = get_location_id(device); vid = get_vendor_id(device); pid = get_product_id(device); res = snprintf(buf, len, "%s_%04hx_%04hx_%x", transport, vid, pid, location); buf[len-1] = '\0'; return res+1; }
int main(void) { IOHIDManagerRef mgr; int i; mgr = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); IOHIDManagerSetDeviceMatching(mgr, NULL); IOHIDManagerOpen(mgr, kIOHIDOptionsTypeNone); CFSetRef device_set = IOHIDManagerCopyDevices(mgr); if (device_set==NULL) { return 0; } CFIndex num_devices = CFSetGetCount(device_set); IOHIDDeviceRef *device_array = calloc(num_devices, sizeof(IOHIDDeviceRef)); CFSetGetValues(device_set, (const void **) device_array); for (i = 0; i < num_devices; i++) { IOHIDDeviceRef dev = device_array[i]; printf("Device: %p\n", dev); printf(" %04hx %04hx\n", get_vendor_id(dev), get_product_id(dev)); wchar_t serial[256], buf[256]; char cbuf[256]; get_serial_number(dev, serial, 256); printf(" Serial: %ls\n", serial); printf(" Loc: %ld\n", get_location_id(dev)); get_transport(dev, buf, 256); printf(" Trans: %ls\n", buf); make_path(dev, cbuf, 256); printf(" Path: %s\n", cbuf); } return 0; }
int get_string_from_cache(char *buf, size_t size, libusb_device *dev, unsigned int referrer) { CFStringRef property; unsigned int location_id; io_service_t service; unsigned int typeID; /* either the base of the number, or 0 for string */ unsigned int child; /* if it's a child property, indicate here */ if (size < 1) return LIBUSB_SUCCESS; *buf = 0; child = 0; typeID = 0; switch (referrer) { case LIBUSB_DEVICE_B_MAXPACKETSIZE0: typeID = 16; property = CFSTR("bMaxPacketSize0"); break; case LIBUSB_DEVICE_ID_PRODUCT: typeID = 16; property = CFSTR("idProduct"); break; case LIBUSB_DEVICE_ID_VENDOR: typeID = 16; property = CFSTR("idVendor"); break; case LIBUSB_DEVICE_BCD_USB: typeID = 16; property = CFSTR("bcdUSB"); break; case LIBUSB_DEVICE_S_MANUFACTURER: typeID = 0; property = CFSTR("USB Vendor Name"); break; case LIBUSB_DEVICE_S_PRODUCT: typeID = 0; property = CFSTR("USB Product Name"); break; case LIBUSB_DEVICE_S_SERIALNUMBER: typeID = 0; property = CFSTR("USB Serial Number"); break; case LIBUSB_DEVICE_BCD_DEVICE: typeID = 16; property = CFSTR("bcdDevice"); break; case LIBUSB_CONFIG_B_CONFIGURATIONVALUE: child = 1; typeID = 16; property = CFSTR("bConfigurationValue"); break; case LIBUSB_INTERFACE_B_INTERFACENUMBER: child = 1; typeID = 16; property = CFSTR("bInterfaceNumber"); break; case LIBUSB_INTERFACE_B_ALTERNATESETTING: child = 1; typeID = 16; property = CFSTR("bAlternateSetting"); break; case LIBUSB_INTERFACE_B_NUMENDPOINTS: child = 1; typeID = 16; property = CFSTR("bNumEndpoints"); break; case LIBUSB_INTERFACE_B_INTERFACECLASS: child = 1; typeID = 16; property = CFSTR("bInterfaceClass"); break; case LIBUSB_INTERFACE_B_INTERFACESUBCLASS: child = 1; typeID = 16; property = CFSTR("bInterfaceSubClass"); break; case LIBUSB_INTERFACE_B_INTERFACEPROTOCOL: child = 1; typeID = 16; property = CFSTR("bInterfaceProtocol"); break; case LIBUSB_DEVICE_B_DEVICECLASS: typeID = 16; property = CFSTR("bDeviceClass"); break; case LIBUSB_DEVICE_B_DEVICEPROTOCOL: typeID = 16; property = CFSTR("bDeviceProtocol"); break; case LIBUSB_DEVICE_B_DEVICESUBCLASS: typeID = 16; property = CFSTR("bDeviceSubClass"); break; case LIBUSB_CONFIG_BM_ATTRIBUTES: typeID = 16; property = CFSTR("bmAttributes"); break; case LIBUSB_CONFIG_B_MAXPOWER: typeID = 16; property = CFSTR("Requested Power"); break; case LIBUSB_DEVICE_B_NUMCONFIGURATIONS: typeID = 16; property = CFSTR("bNumConfigurations"); break; case LIBUSB_CONFIG_B_NUMINTERFACES: typeID = 16; property = CFSTR("bNumInterfaces"); break; case LIBUSB_CONFIG_S_CONFIGURATION: typeID = 0; property = CFSTR("Configuration"); break; case LIBUSB_HUB_N_NBRPORTS: typeID = 16; property = CFSTR("Ports"); break; case LIBUSB_HUB_B_HUBCONTRCURRENT: typeID = 16; property = CFSTR("Bus Power Available"); break; } location_id = get_location_id(dev); darwin_get_service_from_location_id (location_id, &service); if (typeID == 0) { darwin_get_ioreg_string(buf, size, service, property); } else { if (typeID == 10) { snprintf(buf, size, "%d", (UInt32) GetSInt32CFProperty(service, property)); } else if (typeID == 16) { snprintf(buf, size,"%x", (UInt32) GetSInt32CFProperty(service, property)); } } return LIBUSB_SUCCESS; }
int get_string_from_cache(char *buf, size_t size, libusb_device *dev, unsigned int referrer) { /* referrer is something like LIBUSB_DEVICE_BCDUSB */ /* return is length of string in buf */ const char *cacheID; int fd, r; unsigned int location_id; char path[MY_PATH_MAX]; if (size < 1) return LIBUSB_SUCCESS; *buf = 0; path[0] = '\0'; location_id = get_location_id(dev); linux_get_device_info_path(path, sizeof(path), location_id); snprintf(path + strlen(path), size - strlen(path), "%s", "/"); switch (referrer) { case LIBUSB_HUB_N_NBRPORTS: cacheID = "/maxchild"; break; case LIBUSB_DEVICE_BCD_DEVICE: cacheID = "/bcdDevice"; break; case LIBUSB_CONFIG_B_CONFIGURATIONVALUE: cacheID = "/bConfigurationValue"; break; case LIBUSB_DEVICE_B_DEVICECLASS: cacheID = "/bDeviceClass"; break; case LIBUSB_DEVICE_B_DEVICEPROTOCOL: cacheID = "/bDeviceProtocol"; break; case LIBUSB_DEVICE_B_DEVICESUBCLASS: cacheID = "/bDeviceSubClass"; break; case LIBUSB_CONFIG_BM_ATTRIBUTES: cacheID = "/bmAttributes"; break; case LIBUSB_DEVICE_B_MAXPACKETSIZE0: cacheID = "/bMaxPacketSize0"; break; case LIBUSB_CONFIG_B_MAXPOWER: cacheID = "/bMaxPower"; break; case LIBUSB_DEVICE_B_NUMCONFIGURATIONS: cacheID = "/bNumConfigurations"; break; case LIBUSB_CONFIG_B_NUMINTERFACES: cacheID = "/bNumInterfaces"; break; case LIBUSB_CONFIG_S_CONFIGURATION: cacheID = "/configuration"; break; case LIBUSB_DEVICE_ID_PRODUCT: cacheID = "/idProduct"; break; case LIBUSB_DEVICE_ID_VENDOR: cacheID = "/idVendor"; break; case LIBUSB_DEVICE_BCD_USB: cacheID = "/version"; break; case LIBUSB_DEVICE_S_MANUFACTURER: cacheID = "/manufacturer"; break; case LIBUSB_DEVICE_S_PRODUCT: cacheID = "/product"; break; case LIBUSB_DEVICE_S_SERIALNUMBER: cacheID = "/serial"; break; case LIBUSB_ENDPOINT_B_ENDPOINTADDRESS: cacheID = "/ep00/bEndpointAddress"; break; case LIBUSB_ENDPOINT_B_INTERVAL: cacheID = "/ep00/bInterval"; break; case LIBUSB_ENDPOINT_B_LENGTH: cacheID = "/ep00/bLength"; break; case LIBUSB_ENDPOINT_BM_ATTRIBUTES: cacheID = "/ep00/bmAttributes"; break; case LIBUSB_ENDPOINT_W_MAXPACKETSIZE: cacheID = "/ep00/wMaxPacketSize"; break; /* hex */ } strncat(path, cacheID, sizeof(path) - strlen(path) - 1); fd = open(path, O_RDONLY); if (fd < 0) return 0; r = read(fd, buf, size - 1); close(fd); if (r < 0) return 0; /* buf has \n appended */ if (r < 2) return 0; buf[r - 1] = '\0'; return strlen(buf); }