Skip to content

Commit

Permalink
usb.c refactoring request: remove getenv("STLINK_DEVICE")
Browse files Browse the repository at this point in the history
There is no enironment variable "STLINK_DEVICE" in user system, and program do not set it. So I removed all code which works with it.
  • Loading branch information
hydroconstructor committed Jan 6, 2022
1 parent ab5c47b commit 4132973
Showing 1 changed file with 2 additions and 34 deletions.
36 changes: 2 additions & 34 deletions src/stlink-lib/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1134,33 +1134,8 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect,
#endif

libusb_device **list = NULL;
// TODO: We should use ssize_t and use it as a counter if > 0.
// As per libusb API: ssize_t libusb_get_device_list (libusb_context *ctx, libusb_device ***list)
int cnt = (int)libusb_get_device_list(slu->libusb_ctx, &list);
ssize_t cnt = libusb_get_device_list(slu->libusb_ctx, &list);
struct libusb_device_descriptor desc;
int devBus = 0;
int devAddr = 0;

// TODO: Reading a environment variable in a usb open function is not very nice, this should
// be refactored and moved into the CLI tools, and instead of giving USB_BUS:USB_ADDR a real
// stlink serial string should be passed to this function. Probably people are using this
// but this is very odd because as programmer can change to multiple busses and it is better
// to detect them based on serial.
char *device = getenv("STLINK_DEVICE");

if (device) {
char *c = strchr(device, ':');

if (c == NULL) {
WLOG("STLINK_DEVICE must be <USB_BUS>:<USB_ADDR> format\n");
goto on_error;
}

devBus = atoi(device);
*c++ = 0;
devAddr = atoi(c);
ILOG("bus %03d dev %03d\n", devBus, devAddr);
}

while (cnt-- > 0) {
struct libusb_device_handle *handle;
Expand All @@ -1169,13 +1144,6 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect,

if (desc.idVendor != STLINK_USB_VID_ST) { continue; }

if (devBus && devAddr) {
if ((libusb_get_bus_number(list[cnt]) != devBus) ||
(libusb_get_device_address(list[cnt]) != devAddr)) {
continue;
}
}

ret = libusb_open(list[cnt], &handle);

if (ret) { continue; } // could not open device
Expand All @@ -1202,7 +1170,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect,
}

if (cnt < 0) {
WLOG ("Couldn't find %s ST-Link devices\n", (devBus && devAddr) ? "matched" : "any");
WLOG ("Couldn't find any ST-Link devices\n");
libusb_free_device_list(list, 1);
goto on_error;
} else {
Expand Down

0 comments on commit 4132973

Please sign in to comment.