diff --git a/build/makefiles/win/serial2xsbug.mak b/build/makefiles/win/serial2xsbug.mak index 57d2e98872..75dfcdb49b 100644 --- a/build/makefiles/win/serial2xsbug.mak +++ b/build/makefiles/win/serial2xsbug.mak @@ -59,7 +59,7 @@ C_OPTIONS = $(C_OPTIONS) \ /W0 !ENDIF -LIBRARIES = ws2_32.lib advapi32.lib comctl32.lib comdlg32.lib gdi32.lib kernel32.lib user32.lib +LIBRARIES = ws2_32.lib advapi32.lib comctl32.lib comdlg32.lib gdi32.lib kernel32.lib user32.lib Setupapi.lib LINK_OPTIONS = /incremental:no /nologo /subsystem:console !IF "$(GOAL)"=="debug" diff --git a/tools/mcconfig/nmake.nrf52.mk b/tools/mcconfig/nmake.nrf52.mk index 40aab539c6..744cd06c68 100644 --- a/tools/mcconfig/nmake.nrf52.mk +++ b/tools/mcconfig/nmake.nrf52.mk @@ -103,8 +103,8 @@ ECHO_GIT_AND_SIZE = $(PLATFORM_DIR)\config\echoGitTagAndSizeWindows.bat $(TMP_DI DO_XSBUG = tasklist /nh /fi "imagename eq xsbug.exe" | find /i "xsbug.exe" > nul || (start $(MODDABLE_TOOLS_DIR)\xsbug.exe) KILL_SERIAL_2_XSBUG =-tasklist /nh /fi "imagename eq serial2xsbug.exe" | (find /i "serial2xsbug.exe" > nul) && taskkill /f /t /im "serial2xsbug.exe" >nul 2>&1 WAIT_FOR_NEW_SERIAL = $(PLATFORM_DIR)\config\waitForNewSerialWindows.bat 1 $(UF2_VOLUME_NAME) $(TMP_DIR)\_port.tmp $(M4_VID) $(M4_PID) -SERIAL_2_XSBUG = echo Starting serial2xsbug. Type Ctrl-C twice after debugging app. && for /F "tokens=1" %%i in ( $(TMP_DIR)\_port.tmp ) do @$(MODDABLE_TOOLS_DIR)\serial2xsbug %%i $(DEBUGGER_SPEED) 8N1 -dtr -NORESTART = -norestart +SERIAL_2_XSBUG = echo Starting serial2xsbug. Type Ctrl-C twice after debugging app. && $(MODDABLE_TOOLS_DIR)\serial2xsbug $(M4_VID):$(M4_PID) $(DEBUGGER_SPEED) 8N1 -dtr +NORESTART = !ELSE DO_XSBUG = KILL_SERIAL_2_XSBUG = diff --git a/tools/mcmanifest.js b/tools/mcmanifest.js index df2148afc2..f48f67b293 100644 --- a/tools/mcmanifest.js +++ b/tools/mcmanifest.js @@ -1873,6 +1873,14 @@ export class Tool extends TOOL { this.currentDirectory = manifest.directory; const source = this.resolveFilePath(this.resolveVariable(module.source) + ".json"); const flows = JSON.parse(this.readFileString(source)); + + let credentials; + if (source.toLowerCase().endsWith(".json")) { + const path = source.slice(0, -5) + "_cred_mcu.json"; + if (this.resolveFilePath(path)) + credentials = JSON.parse(this.readFileString(path)).credentials; + } + flows.forEach((node, i) => { let manifest; if (node.moddable_manifest) @@ -1884,7 +1892,7 @@ export class Tool extends TOOL { switch (node.type) { case "tls-config": - if (node.ca || node.credentials?.cadata) + if (node.ca || credentials?.[node.id]?.cadata) this.nodeRedExtracts.set(`${node.id}-ca.der`, source) break; } diff --git a/tools/nodered2mcu.js b/tools/nodered2mcu.js index 87019dfc25..06bbfcfcee 100644 --- a/tools/nodered2mcu.js +++ b/tools/nodered2mcu.js @@ -123,7 +123,7 @@ export default class extends TOOL { } if (this.extract) { - const data = this.extractOne(flows, this.extract); + const data = this.extractOne(flows, this.extract, credentials); const parts = { directory: this.outputDirectory, name: this.extract.substring(0, this.extract.lastIndexOf(".")), @@ -1094,12 +1094,9 @@ export default class extends TOOL { break; case "mqtt": - break; - case "mqtts": - throw new Error("MQTT TLS unimplemented") break; - + default: // Node-RED ignores all unrecognized schemes. break; @@ -1946,7 +1943,7 @@ export default class extends TOOL { return `"${name}", ${value}`; return `"${name}"`; } - extractOne(flows, what) { + extractOne(flows, what, credentials) { const parts = what.substring(0, what.lastIndexOf(".")).split("-"); let data; @@ -1960,8 +1957,8 @@ export default class extends TOOL { case "ca": if (config.ca) data = this.readFileString(config.ca); - else if (config.credentials?.cadata) - data = config.credentials.cadata; + else if (credentials?.[config.id]?.cadata) + data = credentials?.[config.id].cadata; if (data) { data = Transform.pemToDER(data); diff --git a/tools/serial2xsbug/serial2xsbug_win.c b/tools/serial2xsbug/serial2xsbug_win.c index b077ef7c3a..4b99ed6b8c 100644 --- a/tools/serial2xsbug/serial2xsbug_win.c +++ b/tools/serial2xsbug/serial2xsbug_win.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2020 Moddable Tech, Inc. + * Copyright (c) 2016-2023 Moddable Tech, Inc. * * This file is part of the Moddable SDK Tools. * @@ -19,6 +19,8 @@ */ #include "serial2xsbug.h" +#include +#include static void fxCountMachines(txSerialTool self); static void fxProgrammingModeSerial(txSerialTool self); @@ -326,6 +328,56 @@ static void fxSignalHandler(int s) SetEvent(self->events[0]); } +static int fxFindPortFromVIDPID(int VID, int PID) +{ + HDEVINFO info; + SP_DEVINFO_DATA data; + DEVPROPTYPE propertyType; + DWORD index = 0; + DWORD size = 0; + char buffer[1024] = {0}; + int portNum = -1; + + char searchName[25]; + sprintf(searchName, "VID_%04X&PID_%04X&", VID, PID); + + info = SetupDiGetClassDevs(NULL, "USB", NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT); + if (info == INVALID_HANDLE_VALUE) + return -1; + + data.cbSize = sizeof(SP_DEVINFO_DATA); + + while (SetupDiEnumDeviceInfo(info, index, &data)) { + index++; + + if (SetupDiGetDeviceRegistryProperty(info, &data, SPDRP_HARDWAREID, &propertyType, buffer, sizeof(buffer), &size)) { + if ( !strstr(buffer, searchName)) + continue; + + HKEY key = SetupDiOpenDevRegKey(info, &data, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ); + + if (key == INVALID_HANDLE_VALUE) + continue; + + char portName[20]; + DWORD type = 0; + + if ( (RegQueryValueEx(key, "PortName", NULL, &type, portName, &size) == ERROR_SUCCESS) && type == REG_SZ) { + if ( strncmp( portName, "COM", 3) == 0) { + portNum = atoi( portName + 3); + } + } + + RegCloseKey(key); + } + } + + if (info) + SetupDiDestroyDeviceInfoList(info); + + return portNum; +} + int main(int argc, char* argv[]) { txSerialTool self = &tool; @@ -334,8 +386,19 @@ int main(int argc, char* argv[]) int result = fxArguments(self, argc, argv); if (result) return result; - strcpy(path, "\\\\.\\"); - strcat(path, self->path); + + if (!(self->vendorID) || !(self->productID)) { + strcpy(path, "\\\\.\\"); + strcat(path, self->path); + } else { + int port = fxFindPortFromVIDPID(self->vendorID, self->productID); + if (port == -1) { + fprintf(stderr, "Could not find USB COM Port with VID 0x%04X and ID 0x%04X\n", self->vendorID, self->productID); + exit(1); + } + sprintf(path, "\\\\.\\COM%d", port); + } + self->path = path; self->serialConnection = INVALID_HANDLE_VALUE; if (setjmp(self->_jmp_buf) == 0) { diff --git a/xs/makefiles/win/xsl.mak b/xs/makefiles/win/xsl.mak index 57315d4d3c..baf5799125 100644 --- a/xs/makefiles/win/xsl.mak +++ b/xs/makefiles/win/xsl.mak @@ -89,7 +89,7 @@ C_OPTIONS = $(C_OPTIONS) \ C_OPTIONS = $(C_OPTIONS) \ /D NDEBUG \ /Fp$(TMP_DIR_RLS)\ \ - /Od \ + /O2 \ /W0 !ENDIF @@ -98,6 +98,8 @@ LIBRARIES = ws2_32.lib advapi32.lib comctl32.lib comdlg32.lib gdi32.lib kernel32 LINK_OPTIONS = /incremental:no /nologo /subsystem:console !IF "$(GOAL)"=="debug" LINK_OPTIONS = $(LINK_OPTIONS) /debug +!ELSE +LINK_OPTIONS = $(LINK_OPTIONS) /OPT:NOICF !ENDIF OBJECTS = \