-int altos_list_next(struct altos_device *device)
-{
- while (current < num) {
- struct libusb_device_descriptor descriptor;
- libusb_device *usb_device = list[current++];
-
- if (libusb_get_device_descriptor(usb_device, &descriptor) == 0) {
- if (descriptor.idVendor == 0xfffe)
- {
- libusb_device_handle *handle;
- if (libusb_open(usb_device, &handle) == 0) {
- char serial_number[256];
- libusb_get_string_descriptor_ascii(handle, descriptor.iProduct,
- device->product,
- sizeof(device->product));
- libusb_get_string_descriptor_ascii(handle, descriptor.iSerialNumber,
- serial_number,
- sizeof (serial_number));
- libusb_close(handle);
- device->serial = atoi(serial_number);
- device->device = usb_device;
- return 1;
- }
- }
+PUBLIC int
+altos_list_next(struct altos_list *list, struct altos_device *device)
+{
+ SP_DEVINFO_DATA dev_info_data;
+ char port[128];
+ DWORD port_len;
+ char location[256];
+ char symbolic[256];
+ DWORD symbolic_len;
+ HKEY dev_key;
+ int vid, pid;
+ int serial;
+ HRESULT result;
+ DWORD location_type;
+ DWORD location_len;
+
+ dev_info_data.cbSize = sizeof (SP_DEVINFO_DATA);
+ while(SetupDiEnumDeviceInfo(list->dev_info, list->index,
+ &dev_info_data))
+ {
+ list->index++;
+
+ dev_key = SetupDiOpenDevRegKey(list->dev_info, &dev_info_data,
+ DICS_FLAG_GLOBAL, 0, DIREG_DEV,
+ KEY_READ);
+ if (dev_key == INVALID_HANDLE_VALUE) {
+ printf("cannot open device registry key\n");
+ continue;
+ }
+
+ /* Fetch symbolic name for this device and parse out
+ * the vid/pid/serial info */
+ symbolic_len = sizeof(symbolic);
+ result = RegQueryValueEx(dev_key, "SymbolicName", NULL, NULL,
+ symbolic, &symbolic_len);
+ if (result != 0) {
+ printf("cannot find SymbolicName value\n");
+ RegCloseKey(dev_key);
+ continue;
+ }
+ vid = pid = serial = 0;
+ sscanf(symbolic + sizeof("\\??\\USB#VID_") - 1,
+ "%04X", &vid);
+ sscanf(symbolic + sizeof("\\??\\USB#VID_XXXX&PID_") - 1,
+ "%04X", &pid);
+ sscanf(symbolic + sizeof("\\??\\USB#VID_XXXX&PID_XXXX#") - 1,
+ "%d", &serial);
+ if (!USB_IS_ALTUSMETRUM(vid, pid)) {
+ printf("Not Altus Metrum symbolic name: %s\n",
+ symbolic);
+ RegCloseKey(dev_key);
+ continue;
+ }
+
+ /* Fetch the com port name */
+ port_len = sizeof (port);
+ result = RegQueryValueEx(dev_key, "PortName", NULL, NULL,
+ port, &port_len);
+ RegCloseKey(dev_key);
+ if (result != 0) {
+ printf("failed to get PortName\n");
+ continue;
+ }
+
+ /* Fetch the 'location information' which is the device name,
+ * at least on XP */
+ location_len = sizeof (location);
+ if(!SetupDiGetDeviceRegistryProperty(list->dev_info,
+ &dev_info_data,
+ SPDRP_LOCATION_INFORMATION,
+ &location_type,
+ (BYTE *)location,
+ sizeof(location),
+ &location_len))
+ {
+ printf("Failed to get location\n");
+ continue;