Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mkellner committed Aug 8, 2023
2 parents 6240ce8 + 83566a0 commit c692d33
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build/makefiles/win/serial2xsbug.mak
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions tools/mcconfig/nmake.nrf52.mk
Expand Up @@ -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 =
Expand Down
10 changes: 9 additions & 1 deletion tools/mcmanifest.js
Expand Up @@ -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)
Expand All @@ -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;
}
Expand Down
13 changes: 5 additions & 8 deletions tools/nodered2mcu.js
Expand Up @@ -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(".")),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand Down
69 changes: 66 additions & 3 deletions 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.
*
Expand All @@ -19,6 +19,8 @@
*/

#include "serial2xsbug.h"
#include <windows.h>
#include <Setupapi.h>

static void fxCountMachines(txSerialTool self);
static void fxProgrammingModeSerial(txSerialTool self);
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion xs/makefiles/win/xsl.mak
Expand Up @@ -89,7 +89,7 @@ C_OPTIONS = $(C_OPTIONS) \
C_OPTIONS = $(C_OPTIONS) \
/D NDEBUG \
/Fp$(TMP_DIR_RLS)\ \
/Od \
/O2 \
/W0
!ENDIF

Expand All @@ -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 = \
Expand Down

0 comments on commit c692d33

Please sign in to comment.