mirror of
https://github.com/greatscottgadgets/hackrf.git
synced 2026-03-14 03:08:56 +01:00
Serial number firmware and host-side changes. Very hacky at this point.
Among the TODOs: * Refactor obtaining LPC serial number and chip ID into separate API and header/source files. Remove from main(). * Create a usb_set_serial_number_descriptor() or similar function to be called before USB stack is started. * Ensure USB serial number descriptor is valid even if code forgets to initialize the serial number before the USB stack is started. May be as simple as providing default initializer for usb_descriptor_string_serial_number[]. * Create a #define/constant for the usb_descriptor_string_serial_number length. * Identify what's causing intermittent crashes in hackrf_transfer when no serial number is specified. I'm probably misusing getopt. * Permit serial number without leading zeros so you don't have to type as much. * Add support for serial number argument in other hackrf_* tools. * Provide libhackrf support for enumerating multiple HackRFs, so that hackrf_info can list all devices. May require an additional libhackrf function, outside of hackrf_open(). ...and anything else that makes this less of a hack.
This commit is contained in:
committed by
Heikki Hannikainen
parent
50a6622ff8
commit
9dbe967bf2
@@ -260,7 +260,10 @@ int ADDCALL hackrf_exit(void)
|
||||
return HACKRF_SUCCESS;
|
||||
}
|
||||
|
||||
int ADDCALL hackrf_open(hackrf_device** device)
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int ADDCALL hackrf_open(const char* const desired_serial_number, hackrf_device** device)
|
||||
{
|
||||
int result;
|
||||
libusb_device_handle* usb_device;
|
||||
@@ -271,13 +274,61 @@ int ADDCALL hackrf_open(hackrf_device** 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<list_length; i++) {
|
||||
struct libusb_device_descriptor device_descriptor;
|
||||
libusb_get_device_descriptor(devices[i], &device_descriptor);
|
||||
if( device_descriptor.idVendor == hackrf_usb_vid ) {
|
||||
if( (device_descriptor.idProduct == hackrf_one_usb_pid) || (device_descriptor.idProduct == hackrf_jawbreaker_usb_pid) ) {
|
||||
printf("%4x:%4x", device_descriptor.idVendor, device_descriptor.idProduct);
|
||||
if( desired_serial_number != NULL ) {
|
||||
const uint_fast8_t serial_descriptor_index = device_descriptor.iSerialNumber;
|
||||
if( serial_descriptor_index > 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);
|
||||
|
||||
// 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;
|
||||
|
||||
Reference in New Issue
Block a user