diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 6e2bd30d..4bf5fd7b 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -263,6 +263,60 @@ int ADDCALL hackrf_exit(void) #include #include +libusb_device_handle* hackrf_open_usb(const char* const desired_serial_number) +{ + libusb_device_handle* usb_device = NULL; + libusb_device** devices = NULL; + const ssize_t list_length = libusb_get_device_list(g_libusb_context, &devices); + + printf("Number of USB devices: %ld\n", list_length); + + for (ssize_t i=0; i 0 ) { + if( libusb_open(devices[i], &usb_device) != 0 ) { + usb_device = NULL; + continue; + } + char serial_number[64]; + const int serial_number_length = libusb_get_string_descriptor_ascii(usb_device, serial_descriptor_index, (unsigned char*)serial_number, sizeof(serial_number)); + if( serial_number_length == 32 ) { + serial_number[32] = 0; + printf(" %s", serial_number); + if( strncmp(serial_number, desired_serial_number, 32) == 0 ) { + printf(" match\n"); + break; + } else { + printf(" skip\n"); + libusb_close(usb_device); + usb_device = NULL; + } + } else { + printf(" error\n"); + libusb_close(usb_device); + usb_device = NULL; + } + } + } else { + printf(" default\n"); + libusb_open(devices[i], &usb_device); + break; + } + } + } + } + + libusb_free_device_list(devices, 1); + + return usb_device; +} + int ADDCALL hackrf_open(const char* const desired_serial_number, hackrf_device** device) { int result; @@ -273,62 +327,19 @@ int ADDCALL hackrf_open(const char* const desired_serial_number, hackrf_device** { return HACKRF_ERROR_INVALID_PARAM; } - - -libusb_device** devices = NULL; -const ssize_t list_length = libusb_get_device_list(g_libusb_context, &devices); -printf("All devices: %ld\n", list_length); -for(ssize_t i=0; i 0 ) { - if( libusb_open(devices[i], &usb_device) != 0 ) { - usb_device = NULL; - continue; - } - char serial_number[64]; - const int serial_number_length = libusb_get_string_descriptor_ascii(usb_device, serial_descriptor_index, (unsigned char*)serial_number, sizeof(serial_number)); - if( serial_number_length == 32 ) { - serial_number[32] = 0; - printf(" %s", serial_number); - if( strncmp(serial_number, desired_serial_number, 32) == 0 ) { - printf(" match\n"); - break; - } else { - printf(" skip\n"); - libusb_close(usb_device); - usb_device = NULL; - } - } else { - printf(" error\n"); - libusb_close(usb_device); - usb_device = NULL; - } - } - } else { - printf(" default\n"); - libusb_open(devices[i], &usb_device); - break; - } - } + + if( desired_serial_number ) + { + usb_device = hackrf_open_usb(desired_serial_number); + } else { + usb_device = libusb_open_device_with_vid_pid(g_libusb_context, hackrf_usb_vid, hackrf_one_usb_pid); } -} -libusb_free_device_list(devices, 1); - - // TODO: Do proper scanning of available devices, searching for - // unit serial number (if specified?). -/* - usb_device = libusb_open_device_with_vid_pid(g_libusb_context, hackrf_usb_vid, hackrf_one_usb_pid); + if( usb_device == NULL ) { usb_device = libusb_open_device_with_vid_pid(g_libusb_context, hackrf_usb_vid, hackrf_jawbreaker_usb_pid); } -*/ + if( usb_device == NULL ) { return HACKRF_ERROR_NOT_FOUND;