From 180c084e9c72d97c7275f802b506111fce503139 Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Wed, 17 Jun 2026 17:42:24 +0100 Subject: [PATCH] Utilise str_set_vid_pid_serno() in usb_hidapi.c --- src/usb_hidapi.c | 46 ++++++++-------------------------------------- 1 file changed, 8 insertions(+), 38 deletions(-) diff --git a/src/usb_hidapi.c b/src/usb_hidapi.c index d71972e1..d3a019af 100644 --- a/src/usb_hidapi.c +++ b/src/usb_hidapi.c @@ -37,54 +37,24 @@ #include "usbdevs.h" -/* - * The "baud" parameter is meaningless for USB devices, so we reuse it to pass - * the desired USB device ID. - */ static int usbhid_open(const char *port, union pinfo pinfo, union filedescriptor *fd) { hid_device *dev = NULL; - char serno[64], *s; - const char *serp, *vidp, *pidp; + char serno[64] = {0}; unsigned char usbbuf[USBDEV_MAX_XFER_3 + 1]; if(fd->usb.max_xfer == 0) fd->usb.max_xfer = USBDEV_MAX_XFER_3; - /* - * The syntax for usb devices is defined as: - * - * -P usb:vid:pid - * -P usb:serialnumber - * -P usb - * - * First check for a valid vid:pid pair, then see if there is a serial number - * passed here. The serial number might contain colons which are removed; - * comparison is right-to-left, so only the least significant nibbles need to - * be specified. - */ - - if((vidp = strchr(port, ':')) && (pidp = strchr(vidp + 1, ':'))) { - int vid, pid; - - if(sscanf(vidp + 1, "%x", &vid) == 1 && sscanf(pidp + 1, "%x", &pid) == 1) { - if((dev = hid_open(vid, pid, NULL))) { - pmsg_notice2("USB device with VID: 0x%04x and PID: 0x%04x\n", vid, pid); - pinfo.usbinfo.vid = vid; - pinfo.usbinfo.pid = pid; - } - } + // Override/set pid, vid and/or serno from -P usb[[:[]:]:] + if(str_set_vid_pid_serno(port, &pinfo.usbinfo.vid, &pinfo.usbinfo.pid, -1, serno, sizeof serno) < 0) { + pmsg_error("parsing error in -P %s\n", port); + return -1; } - if(!dev && (serp = vidp) && *++serp) { - // First, get a copy of the serial number w/out colons - for(s = serno; *serp && s < serno + sizeof serno - 1; serp++) - if(*serp != ':') - *s++ = *serp; - *s = 0; - + if(*serno) { wchar_t wserno[sizeof serno] = { 0 }; mbstowcs(wserno, serno, sizeof serno); - size_t serlen = s - serno; + size_t serlen = strlen(serno); /* * Now, try finding all devices matching VID:PID, and compare their serial @@ -123,7 +93,7 @@ static int usbhid_open(const char *port, union pinfo pinfo, union filedescriptor pmsg_error("found device, but hid_open_path() failed\n"); return -1; } - } else if(!dev) { + } else { dev = hid_open(pinfo.usbinfo.vid, pinfo.usbinfo.pid, NULL); if(dev == NULL) { pmsg_notice2("USB device with VID: 0x%04x and PID: 0x%04x not found\n", pinfo.usbinfo.vid, pinfo.usbinfo.pid);