Skip to content

Commit

Permalink
Add VID and PID device target filters
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-pecserke committed Jun 3, 2023
1 parent d33a555 commit ec92534
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ struct _settings {
uint32_t binary_start = FLASH_START;
int bus=-1;
int address=-1;
int vid = -1;
int pid = -1;
string serial;
uint32_t offset = 0;
uint32_t from = 0;
Expand Down Expand Up @@ -302,13 +304,17 @@ auto device_selection =
+ (option("--address") & integer("addr").min_value(1).max_value(127).set(settings.address)
.if_missing([] { return "missing address"; }))
% "Filter devices by USB device address"
).min(0).doc_non_optional(true) +
(
(option("--serial") & value("serial").set(settings.serial)
+ (option("--vid") & hex("vid").min_value(0).max_value(0xffff).set(settings.vid)
.if_missing([] { return "missing vendor id"; }))
% "Filter devices by vendor id"
+ (option("--pid") & hex("pid").min_value(0).max_value(0xffff).set(settings.pid)
.if_missing([] { return "missing product id"; }))
% "Filter devices by product id"
+ (option("--serial") & value("serial").set(settings.serial)
.if_missing([] { return "missing serial id"; }))
% "Filter devices by serial id"
#if !defined(_WIN32)
+ option('f', "--force").set(settings.force)
+ option('f', "--force").set(settings.force)
% "Force a device not in BOOTSEL mode but running compatible code to reset so the command can be executed. "
"After executing the command (unless the command itself is a 'reboot') "
"the device will be rebooted back to application mode"
Expand Down Expand Up @@ -1602,16 +1608,28 @@ string missing_device_string(bool wasRetry) {
oss << 's';
}
oss << " in BOOTSEL mode";

if (settings.vid != -1 && settings.pid != -1) {
oss << " with ID '" << std::hex << settings.vid << ':' << std::hex << settings.pid << '\'';
} else if (settings.vid != -1) {
oss << " with Vendor ID '" << std::hex << settings.vid << '\'';
} else if (settings.pid != -1) {
oss << " with Product ID '" << std::hex << settings.pid << '\'';
}

if (!settings.serial.empty()) {
oss << " with serial ID '" << settings.serial << '\'';
}

oss << " was found";

if (settings.bus != -1) {
oss << " at bus " << settings.bus;
}
if (settings.address != -1) {
oss << (settings.bus == -1 ? " with" : ",") << " address " << settings.address;
}

oss << '.';

return oss.str();
Expand Down Expand Up @@ -2205,6 +2223,16 @@ string get_usb_device_serial(libusb_device *device, libusb_device_handle *handle
return "";
}

int get_usb_device_vid_pid(libusb_device *device, uint16_t &vid, uint16_t &pid) {
struct libusb_device_descriptor desc;
int ret = libusb_get_device_descriptor(device, &desc);
if (ret) {
return -1;
}

return 0;
}

int main(int argc, char **argv) {
libusb_context *ctx = nullptr;

Expand Down Expand Up @@ -2250,6 +2278,12 @@ int main(int argc, char **argv) {
for (libusb_device **dev = devs; *dev; dev++) {
if (settings.bus != -1 && settings.bus != libusb_get_bus_number(*dev)) continue;
if (settings.address != -1 && settings.address != libusb_get_device_address(*dev)) continue;
if (settings.vid != -1 || settings.pid != -1) {
uint16_t vid, pid;
int ret = get_usb_device_vid_pid(*dev, vid, pid);
if (settings.vid != -1 && (ret || settings.vid != vid)) continue;
if (settings.pid != -1 && (ret || settings.pid != vid)) continue;
}
libusb_device_handle *handle = nullptr;
auto result = picoboot_open_device(*dev, &handle);
if (handle) {
Expand Down

0 comments on commit ec92534

Please sign in to comment.