diff --git a/host/hackrf-tools/src/hackrf_cpldjtag.c b/host/hackrf-tools/src/hackrf_cpldjtag.c index c2120990..b76c9349 100644 --- a/host/hackrf-tools/src/hackrf_cpldjtag.c +++ b/host/hackrf-tools/src/hackrf_cpldjtag.c @@ -164,7 +164,7 @@ int main(int argc, char** argv) return EXIT_FAILURE; } - result = hackrf_open(serial_number, &device); + result = hackrf_open_by_serial(serial_number, &device); if (result != HACKRF_SUCCESS) { fprintf(stderr, "hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); diff --git a/host/hackrf-tools/src/hackrf_max2837.c b/host/hackrf-tools/src/hackrf_max2837.c index 81ebea65..d8fb75f5 100644 --- a/host/hackrf-tools/src/hackrf_max2837.c +++ b/host/hackrf-tools/src/hackrf_max2837.c @@ -130,7 +130,7 @@ int main(int argc, char** argv) { return -1; } - result = hackrf_open(NULL, &device); + result = hackrf_open(&device); if( result ) { printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); return -1; diff --git a/host/hackrf-tools/src/hackrf_rffc5071.c b/host/hackrf-tools/src/hackrf_rffc5071.c index 00e48d85..c9630dd4 100644 --- a/host/hackrf-tools/src/hackrf_rffc5071.c +++ b/host/hackrf-tools/src/hackrf_rffc5071.c @@ -131,7 +131,7 @@ int main(int argc, char** argv) { return -1; } - result = hackrf_open(NULL, &device); + result = hackrf_open(&device); if( result ) { printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); return -1; diff --git a/host/hackrf-tools/src/hackrf_si5351c.c b/host/hackrf-tools/src/hackrf_si5351c.c index 0db251e2..41146f2c 100644 --- a/host/hackrf-tools/src/hackrf_si5351c.c +++ b/host/hackrf-tools/src/hackrf_si5351c.c @@ -195,7 +195,7 @@ int main(int argc, char** argv) { return -1; } - result = hackrf_open(NULL, &device); + result = hackrf_open(&device); if( result ) { printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); return -1; diff --git a/host/hackrf-tools/src/hackrf_spiflash.c b/host/hackrf-tools/src/hackrf_spiflash.c index d58c41ee..f210f370 100644 --- a/host/hackrf-tools/src/hackrf_spiflash.c +++ b/host/hackrf-tools/src/hackrf_spiflash.c @@ -219,7 +219,7 @@ int main(int argc, char** argv) return EXIT_FAILURE; } - result = hackrf_open(serial_number, &device); + result = hackrf_open_by_serial(serial_number, &device); if (result != HACKRF_SUCCESS) { fprintf(stderr, "hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); diff --git a/host/hackrf-tools/src/hackrf_transfer.c b/host/hackrf-tools/src/hackrf_transfer.c index 780b2d47..b76ae631 100644 --- a/host/hackrf-tools/src/hackrf_transfer.c +++ b/host/hackrf-tools/src/hackrf_transfer.c @@ -782,7 +782,7 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - result = hackrf_open(serial_number, &device); + result = hackrf_open_by_serial(serial_number, &device); if( result != HACKRF_SUCCESS ) { printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); usage(); diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 42d69c83..063ad11f 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -447,7 +447,7 @@ static int hackrf_open_setup(libusb_device_handle* usb_device, hackrf_device** d return HACKRF_SUCCESS; } -int ADDCALL hackrf_open(const char* const desired_serial_number, hackrf_device** device) +int ADDCALL hackrf_open(hackrf_device** device) { libusb_device_handle* usb_device; @@ -456,16 +456,11 @@ int ADDCALL hackrf_open(const char* const desired_serial_number, hackrf_device** return HACKRF_ERROR_INVALID_PARAM; } - if( desired_serial_number ) + usb_device = libusb_open_device_with_vid_pid(g_libusb_context, hackrf_usb_vid, hackrf_one_usb_pid); + + if( usb_device == NULL ) { - 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); - - if( usb_device == NULL ) - { - usb_device = libusb_open_device_with_vid_pid(g_libusb_context, hackrf_usb_vid, hackrf_jawbreaker_usb_pid); - } + usb_device = libusb_open_device_with_vid_pid(g_libusb_context, hackrf_usb_vid, hackrf_jawbreaker_usb_pid); } if( usb_device == NULL ) @@ -476,6 +471,30 @@ int ADDCALL hackrf_open(const char* const desired_serial_number, hackrf_device** return hackrf_open_setup(usb_device, device); } +int ADDCALL hackrf_open_by_serial(const char* const desired_serial_number, hackrf_device** device) +{ + libusb_device_handle* usb_device; + + if( desired_serial_number == NULL ) + { + return hackrf_open(device); + } + + if( device == NULL ) + { + return HACKRF_ERROR_INVALID_PARAM; + } + + usb_device = hackrf_open_usb(desired_serial_number); + + if( usb_device == NULL ) + { + return HACKRF_ERROR_NOT_FOUND; + } + + return hackrf_open_setup(usb_device, device); +} + int ADDCALL hackrf_device_list_open(hackrf_device_list_t *list, int idx, hackrf_device** device) { libusb_device_handle* usb_device; diff --git a/host/libhackrf/src/hackrf.h b/host/libhackrf/src/hackrf.h index 08afe3c9..d75ee24b 100644 --- a/host/libhackrf/src/hackrf.h +++ b/host/libhackrf/src/hackrf.h @@ -117,7 +117,8 @@ extern ADDAPI hackrf_device_list_t* ADDCALL hackrf_device_list(); extern ADDAPI int ADDCALL hackrf_device_list_open(hackrf_device_list_t *list, int idx, hackrf_device** device); extern ADDAPI void ADDCALL hackrf_device_list_free(hackrf_device_list_t *list); -extern ADDAPI int ADDCALL hackrf_open(const char* const desired_serial_number, hackrf_device** device); +extern ADDAPI int ADDCALL hackrf_open(hackrf_device** device); +extern ADDAPI int ADDCALL hackrf_open_by_serial(const char* const desired_serial_number, hackrf_device** device); extern ADDAPI int ADDCALL hackrf_close(hackrf_device* device); extern ADDAPI int ADDCALL hackrf_start_rx(hackrf_device* device, hackrf_sample_block_cb_fn callback, void* rx_ctx);