Skip to content

Commit

Permalink
Report whether other devices are sharing the USB bus.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinling committed Sep 26, 2024
1 parent 17f3943 commit e27b459
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
17 changes: 17 additions & 0 deletions host/hackrf-tools/src/hackrf_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,23 @@ int main(void)
hackrf_error_name(result),
result);
}

result = hackrf_device_list_bus_sharing(list, i);
if (result < 0) {
fprintf(stderr,
"hackrf_device_list_bus_sharing() failed: %s (%d)\n",
hackrf_error_name(result),
result);
} else if (result == 0) {
printf("This device is on its own USB bus.\n");
} else if (result == 1) {
printf("There is 1 other device on the same USB bus.\n");
printf("You may have problems at high sample rates.\n");
} else {
printf("There are %d other devices on the same USB bus.\n",
result);
printf("You may have problems at high sample rates.\n");
}
}

hackrf_device_list_free(list);
Expand Down
24 changes: 24 additions & 0 deletions host/libhackrf/src/hackrf.c
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,30 @@ int ADDCALL hackrf_device_list_open(
return hackrf_open_setup(usb_device, device);
}

int ADDCALL hackrf_device_list_bus_sharing(hackrf_device_list_t* list, int idx)
{
libusb_device *usb_dev, *hackrf_dev;
uint8_t hackrf_bus;
int other_device_count = 0;
int i;
if (list == NULL || list->usb_devices == NULL || list->usb_device_index == NULL ||
idx < 0 || idx > list->devicecount) {
return HACKRF_ERROR_INVALID_PARAM;
}
hackrf_dev = list->usb_devices[list->usb_device_index[idx]];
hackrf_bus = libusb_get_bus_number(hackrf_dev);
for (i = 0; i < list->usb_devicecount; i++) {
usb_dev = (libusb_device*) list->usb_devices[i];
// Don't count the HackRF, devices on other buses, or the root hub.
if (usb_dev != hackrf_dev &&
libusb_get_bus_number(usb_dev) == hackrf_bus &&
libusb_get_parent(usb_dev) != NULL) {
other_device_count++;
}
}
return other_device_count;
}

int ADDCALL hackrf_set_transceiver_mode(
hackrf_device* device,
hackrf_transceiver_mode value)
Expand Down
12 changes: 12 additions & 0 deletions host/libhackrf/src/hackrf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,18 @@ extern ADDAPI int ADDCALL hackrf_device_list_open(
int idx,
hackrf_device** device);

/**
* Check if a listed HackRF device is sharing its USB bus with other devices.
*
* @param[in] list device list to query
* @param[in] idx index of the HackRF device in the list
* @return The number of devices sharing the USB bus with the specified HackRF, or a negative error code from @ref hackrf_error
*
*/
extern ADDAPI int ADDCALL hackrf_device_list_bus_sharing(
hackrf_device_list_t* list,
int idx);

/**
* Free a previously allocated @ref hackrf_device_list list.
* @param[in] list list to free
Expand Down

0 comments on commit e27b459

Please sign in to comment.