From fca8593d9216f6901019b08ee1d320dcc2f6d552 Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Sat, 1 Aug 2020 22:31:26 +0200 Subject: [PATCH] serial_win: Find BMP comport by serial number. --- src/platforms/hosted/platform.c | 3 -- src/platforms/hosted/platform.h | 3 ++ src/platforms/pc/serial_win.c | 50 ++++++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/platforms/hosted/platform.c b/src/platforms/hosted/platform.c index 631fa49..0fd486a 100644 --- a/src/platforms/hosted/platform.c +++ b/src/platforms/hosted/platform.c @@ -37,9 +37,6 @@ #include "jlink.h" #include "cmsis_dap.h" -#define VENDOR_ID_BMP 0x1d50 -#define PRODUCT_ID_BMP 0x6018 - #define VENDOR_ID_STLINK 0x0483 #define PRODUCT_ID_STLINK_MASK 0xffe0 #define PRODUCT_ID_STLINK_GROUP 0x3740 diff --git a/src/platforms/hosted/platform.h b/src/platforms/hosted/platform.h index b0e5043..e36d7b9 100644 --- a/src/platforms/hosted/platform.h +++ b/src/platforms/hosted/platform.h @@ -14,6 +14,9 @@ void platform_buffer_flush(void); #define SET_IDLE_STATE(x) #define SET_RUN_STATE(x) +#define VENDOR_ID_BMP 0x1d50 +#define PRODUCT_ID_BMP 0x6018 + typedef enum bmp_type_s { BMP_TYPE_NONE = 0, BMP_TYPE_BMP, diff --git a/src/platforms/pc/serial_win.c b/src/platforms/pc/serial_win.c index 4b02e36..4290428 100644 --- a/src/platforms/pc/serial_win.c +++ b/src/platforms/pc/serial_win.c @@ -23,17 +23,59 @@ #include "remote.h" #include "cl_utils.h" -HANDLE hComm; +static HANDLE hComm; + +static char *find_bmp_by_serial(const char *serial) +{ + char regpath[258]; + /* First find the containers of the BMP comports */ + sprintf(regpath, + "SYSTEM\\CurrentControlSet\\Enum\\USB\\VID_%04X&PID_%04X\\%s", + VENDOR_ID_BMP, PRODUCT_ID_BMP, serial); + HKEY hkeySection; + LSTATUS res; + res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &hkeySection); + if (res != ERROR_SUCCESS) + return NULL; + BYTE prefix[128]; + DWORD maxlen = sizeof(prefix); + res = RegQueryValueEx(hkeySection, "ParentIdPrefix", NULL, NULL, prefix, + &maxlen); + RegCloseKey(hkeySection); + if (res != ERROR_SUCCESS) + return NULL; + printf("prefix %s\n", prefix); + sprintf(regpath, + "SYSTEM\\CurrentControlSet\\Enum\\USB\\VID_%04X&PID_%04X&MI_00\\%s" + "&0000\\Device Parameters", + VENDOR_ID_BMP, PRODUCT_ID_BMP, prefix); + printf("%s\n", regpath); + res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, KEY_READ, &hkeySection); + if (res != ERROR_SUCCESS) { + printf("Failuere\n"); + return NULL; + } + BYTE port[128]; + maxlen = sizeof(port); + res = RegQueryValueEx(hkeySection, "PortName", NULL, NULL, port, &maxlen); + RegCloseKey(hkeySection); + if (res != ERROR_SUCCESS) + return NULL; + printf("Portname %s\n", port); + return strdup((char*)port); +} int serial_open(BMP_CL_OPTIONS_t *cl_opts, char * serial) { (void) serial; /* FIXME: Does Windows allow open with USB serial no? */ + char device[256]; + if (!cl_opts->opt_device) + cl_opts->opt_device = find_bmp_by_serial(serial); if (!cl_opts->opt_device) { - DEBUG_WARN("Specify the serial device to use!\n"); + DEBUG_WARN("Unexpected problems finding the device!\n"); return -1; } - char device[256]; - if (strstr(device, "\\\\.\\")) { + if (strstr(cl_opts->opt_device, "\\\\.\\")) { strncpy(device, cl_opts->opt_device, sizeof(device) - 1); } else { strcpy(device, "\\\\.\\");