Skip to content

Commit

Permalink
fix: detect bootloader in more difficult cases
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Oct 27, 2024
1 parent 9acbad5 commit 93091f5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
22 changes: 19 additions & 3 deletions packages/serial/src/serialport/ZWaveSerialPortBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,25 @@ export class ZWaveSerialPortBase extends PassThrough {
const str = (data as Buffer).toString("ascii")
// like .trim(), but including null bytes
.replaceAll(/^[\s\0]+|[\s\0]+$/g, "");
this.mode = str.startsWith(bootloaderMenuPreamble)
? ZWaveSerialMode.Bootloader
: ZWaveSerialMode.SerialAPI;

if (str.startsWith(bootloaderMenuPreamble)) {
// We're sure we're in bootloader mode
this.mode = ZWaveSerialMode.Bootloader;
} else if (
(data as Buffer).every((b) =>
b === 0x00
|| b === 0x0a
|| b === 0x0d
|| (b >= 0x20 && b <= 0x7e)
) && (data as Buffer).some((b) => b >= 0x20 && b <= 0x7e)
) {
// Only printable line breaks, null bytes and at least one printable ASCII character
// --> We're pretty sure we're in bootloader mode
this.mode = ZWaveSerialMode.Bootloader;
} else {
// We're in Serial API mode
this.mode = ZWaveSerialMode.SerialAPI;
}
}

// On Windows, writing to the parsers immediately seems to lag the event loop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ integrationTest(
"The bootloader is detected when received in smaller chunks",
{
// Reproduction for issue #7316
// debug: true,
debug: true,

Check failure on line 9 in packages/zwave-js/src/lib/test/driver/bootloaderDetection.test.ts

View workflow job for this annotation

GitHub Actions / lint (18)

Comment out the `debug` flag when not actively debugging

additionalDriverOptions: {
allowBootloaderOnly: true,
Expand All @@ -19,12 +19,13 @@ integrationTest(
// ctrl.length === 1
// && (ctrl[0] === MessageHeaders.NAK || ctrl[0] === 0x32)
// ) {
// I've seen logs with as few as 5 bytes in the first chunk
self.serial.emitData(
Buffer.from("\0\r\nGecko Bootloa", "ascii"),
Buffer.from("\0\r\nGeck", "ascii"),
);
await wait(20);
self.serial.emitData(Buffer.from(
`der v2.05.01
`o Bootloader v2.05.01
1. upload gbl
2. run
3. ebl info
Expand Down

0 comments on commit 93091f5

Please sign in to comment.