Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 61 additions & 65 deletions src/platforms/hosted/bmp_libusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,33 +65,35 @@ typedef struct debugger_device {
} debugger_device_s;

/* Create the list of debuggers BMDA works with */
debugger_device_s debugger_devices[] = {
static const debugger_device_s debugger_devices[] = {
{VENDOR_ID_BMP, PRODUCT_ID_BMP, BMP_TYPE_BMP, bmp_read_product_version, "Black Magic Probe"},
{VENDOR_ID_STLINK, PRODUCT_ID_STLINKV2, BMP_TYPE_STLINK_V2, stlinkv2_read_serial, "ST-Link v2"},
{VENDOR_ID_STLINK, PRODUCT_ID_STLINKV21, BMP_TYPE_STLINK_V2, NULL, "ST-Link v2.1"},
{VENDOR_ID_STLINK, PRODUCT_ID_STLINKV21_MSD, BMP_TYPE_STLINK_V2, NULL, "ST-Link v2.1 MSD"},
{VENDOR_ID_STLINK, PRODUCT_ID_STLINKV3_NO_MSD, BMP_TYPE_STLINK_V2, NULL, "ST-Link v2.1 No MSD"},
{VENDOR_ID_STLINK, PRODUCT_ID_STLINKV3, BMP_TYPE_STLINK_V2, NULL, "ST-Link v3"},
{VENDOR_ID_STLINK, PRODUCT_ID_STLINKV3E, BMP_TYPE_STLINK_V2, NULL, "ST-Link v3E"},
{VENDOR_ID_SEGGER, PRODUCT_ID_UNKNOWN, BMP_TYPE_JLINK, NULL, "Segger JLink"},
{VENDOR_ID_SEGGER, PRODUCT_ID_ANY, BMP_TYPE_JLINK, NULL, "Segger JLink"},
{VENDOR_ID_FTDI, PRODUCT_ID_FTDI_FT2232, BMP_TYPE_FTDI, NULL, "FTDI FT2232"},
{VENDOR_ID_FTDI, PRODUCT_ID_FTDI_FT4232, BMP_TYPE_FTDI, NULL, "FTDI FT4232"},
{VENDOR_ID_FTDI, PRODUCT_ID_FTDI_FT232, BMP_TYPE_FTDI, NULL, "FTDI FT232"},
{0, 0, BMP_TYPE_NONE, NULL, ""},
};

bmp_type_t get_type_from_vid_pid(const uint16_t probe_vid, const uint16_t probe_pid)
const debugger_device_s *get_debugger_device_from_vid_pid(const uint16_t probe_vid, const uint16_t probe_pid)
{
bmp_type_t probe_type = BMP_TYPE_NONE;
/* Segger probe PIDs are unknown, if we have a Segger probe force the type to JLink */
if (probe_vid == VENDOR_ID_SEGGER)
return BMP_TYPE_JLINK;

for (size_t index = 0; debugger_devices[index].type != BMP_TYPE_NONE; index++) {
if (debugger_devices[index].vendor == probe_vid && debugger_devices[index].product == probe_pid)
return debugger_devices[index].type;
/* Iterate over the list excluding the last entry (BMP_TYPE_NONE) */
for (size_t index = 0; index < ARRAY_LENGTH(debugger_devices) - 1U; index++) {
/* Check for a vendor id match */
if (debugger_devices[index].vendor != probe_vid)
continue;

/* If the map product id is "any", then we accept all products ids, otherwise check for a match */
if (debugger_devices[index].product == PRODUCT_ID_ANY || probe_pid == debugger_devices[index].product)
return &debugger_devices[index];
}
return probe_type;
/* Return the last entry in the list (BMP_TYPE_NONE) */
return &debugger_devices[ARRAY_LENGTH(debugger_devices) - 1U];
}

void bmp_ident(bmp_info_s *info)
Expand All @@ -116,7 +118,7 @@ void libusb_exit_function(bmp_info_s *info)

static char *get_device_descriptor_string(libusb_device_handle *handle, uint16_t string_index)
{
char read_string[128] = {0};
char read_string[128U] = {0};
if (string_index != 0) {
const int result =
libusb_get_string_descriptor_ascii(handle, string_index, (uint8_t *)read_string, sizeof(read_string));
Expand Down Expand Up @@ -148,8 +150,8 @@ void bmp_read_product_version(libusb_device_descriptor_s *device_descriptor, lib

while (start_of_version[0] == ' ' && start_of_version != *product)
--start_of_version;
start_of_version[1] = '\0';
start_of_version += 2;
start_of_version[1U] = '\0';
start_of_version += 2U;
while (start_of_version[0] == ' ')
++start_of_version;
*version = strdup(start_of_version);
Expand All @@ -167,7 +169,7 @@ void stlinkv2_read_serial(libusb_device_descriptor_s *device_descriptor, libusb_
(void)manufacturer;
(void)version;
/* libusb_get_string_descriptor requires a byte buffer, but returns char16_t's */
char16_t raw_serial[64] = {0};
char16_t raw_serial[64U] = {0};
const int raw_length = libusb_get_string_descriptor(
handle, device_descriptor->iSerialNumber, 0x0409U, (uint8_t *)raw_serial, sizeof(raw_serial));
if (raw_length < LIBUSB_SUCCESS)
Expand All @@ -177,11 +179,11 @@ void stlinkv2_read_serial(libusb_device_descriptor_s *device_descriptor, libusb_
* Re-encode the resulting chunk of data as hex, skipping the first char16_t which
* contains the header of the string descriptor
*/
char encoded_serial[128] = {0};
char encoded_serial[128U] = {0};
for (size_t offset = 0; offset < (size_t)raw_length - 2U; offset += 2U) {
uint8_t digit = raw_serial[1 + (offset / 2U)];
encoded_serial[offset + 0] = hex_digit(digit >> 4U);
encoded_serial[offset + 1] = hex_digit(digit & 0x0fU);
encoded_serial[offset + 1U] = hex_digit(digit & 0x0fU);
}
*serial = strdup(encoded_serial);
}
Expand Down Expand Up @@ -289,7 +291,7 @@ void orbtrace_read_version(libusb_device *device, libusb_device_handle *handle,
const size_t version_len = strlen(version);
if (begins_with(version, version_len, "Version")) {
/* Chop off the "Version: " prefix */
memmove(version, version + 9, version_len - 8);
memmove(version, version + 9U, version_len - 8);
break;
}
}
Expand All @@ -300,7 +302,7 @@ void orbtrace_read_version(libusb_device *device, libusb_device_handle *handle,
static void process_cmsis_interface(const libusb_device_descriptor_s *const device_descriptor,
libusb_device *const device, libusb_device_handle *const handle, probe_info_s **probe_list)
{
char read_string[128];
char read_string[128U];
char *version;
if (device_descriptor->idVendor == VENDOR_ID_ORBCODE && device_descriptor->idProduct == PRODUCT_ID_ORBTRACE) {
orbtrace_read_version(device, handle, read_string, sizeof(read_string));
Expand Down Expand Up @@ -394,7 +396,7 @@ static void check_cmsis_interface_type(libusb_device *const device, bmp_info_s *
/* If we've found an interface without a description string, ignore it */
if (descriptor->iInterface == 0)
continue;
char interface_string[128];
char interface_string[128U];
/* Read out the string */
if (libusb_get_string_descriptor_ascii(
handle, descriptor->iInterface, (unsigned char *)interface_string, sizeof(interface_string)) < 0)
Expand Down Expand Up @@ -425,51 +427,45 @@ static void check_cmsis_interface_type(libusb_device *const device, bmp_info_s *
static bool process_vid_pid_table_probe(
libusb_device_descriptor_s *device_descriptor, libusb_device *device, probe_info_s **probe_list)
{
bool probe_added = false;
for (size_t index = 0; debugger_devices[index].type != BMP_TYPE_NONE; ++index) {
/* Check for a match, skip the entry if we don't get one */
if (device_descriptor->idVendor != debugger_devices[index].vendor ||
(device_descriptor->idProduct != debugger_devices[index].product &&
debugger_devices[index].product != PRODUCT_ID_UNKNOWN))
continue;
/* Check for a match */
const debugger_device_s *const debugger_device =
get_debugger_device_from_vid_pid(device_descriptor->idVendor, device_descriptor->idProduct);
if (debugger_device->type == BMP_TYPE_NONE)
return false;

libusb_device_handle *handle = NULL;
/* Try to open the device */
if (libusb_open(device, &handle) != LIBUSB_SUCCESS)
break;

const bmp_type_t probe_type = get_type_from_vid_pid(device_descriptor->idVendor, device_descriptor->idProduct);
char *product = NULL;
char *manufacturer = NULL;
char *serial = NULL;
char *version = NULL;
/*
* If the probe has a custom string reader available, use it first.
*
* This will read and process any strings that need special work, e.g., extracting
* a version string from a product string (BMP native)
*/
if (debugger_devices[index].function != NULL)
debugger_devices[index].function(
device_descriptor, device, handle, &product, &manufacturer, &serial, &version);

/* Now read any strings that have not been set by a custom reader function */
if (product == NULL)
product = get_device_descriptor_string(handle, device_descriptor->iProduct);
if (manufacturer == NULL)
manufacturer = get_device_descriptor_string(handle, device_descriptor->iManufacturer);
if (serial == NULL)
serial = get_device_descriptor_string(handle, device_descriptor->iSerialNumber);

if (version == NULL)
version = strdup("---");

*probe_list = probe_info_add_by_id(*probe_list, probe_type, device, device_descriptor->idVendor,
device_descriptor->idProduct, manufacturer, product, serial, version);
probe_added = true;
libusb_close(handle);
}
return probe_added;
libusb_device_handle *handle = NULL;
/* Try to open the device */
if (libusb_open(device, &handle) != LIBUSB_SUCCESS)
return false;

char *product = NULL;
char *manufacturer = NULL;
char *serial = NULL;
char *version = NULL;
/*
* If the probe has a custom string reader available, use it first.
*
* This will read and process any strings that need special work, e.g., extracting
* a version string from a product string (BMP native)
*/
if (debugger_device->function != NULL)
debugger_device->function(device_descriptor, device, handle, &product, &manufacturer, &serial, &version);

/* Now read any strings that have not been set by a custom reader function */
if (product == NULL)
product = get_device_descriptor_string(handle, device_descriptor->iProduct);
if (manufacturer == NULL)
manufacturer = get_device_descriptor_string(handle, device_descriptor->iManufacturer);
if (serial == NULL)
serial = get_device_descriptor_string(handle, device_descriptor->iSerialNumber);

if (version == NULL)
version = strdup("---");

*probe_list = probe_info_add_by_id(*probe_list, debugger_device->type, device, device_descriptor->idVendor,
device_descriptor->idProduct, manufacturer, product, serial, version);
libusb_close(handle);
return true;
}

static const probe_info_s *scan_for_devices(bmp_info_s *info)
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/hosted/jlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ bool jlink_init(void)
return false;
info.usb_link = link;
link->context = info.libusb_ctx;
int result = libusb_open(info.libusb_dev, &link->device_handle);
const int result = libusb_open(info.libusb_dev, &link->device_handle);
if (result != LIBUSB_SUCCESS) {
DEBUG_ERROR("libusb_open() failed (%d): %s\n", result, libusb_error_name(result));
return false;
Expand Down
Loading