From 095f5b705288793c273c07e73c7e9cbb4d8311bf Mon Sep 17 00:00:00 2001 From: Fernando Chorney Date: Tue, 16 Apr 2024 17:34:20 -0500 Subject: [PATCH] Correct config sizes for read/write old config --- sdk/commands/config.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/sdk/commands/config.ts b/sdk/commands/config.ts index 55c9a73..1193632 100644 --- a/sdk/commands/config.ts +++ b/sdk/commands/config.ts @@ -343,6 +343,7 @@ const OLD_CONFIG_INIT = [ export class SMXConfig { public config: Decoded; private oldConfig: Decoded | null = null; + private oldConfigSize: number | null = null; private firmwareVersion: number; /** @@ -356,8 +357,12 @@ export class SMXConfig { if (this.firmwareVersion >= 5) { this.config = smx_config_t.decode(data.slice(2, -1), true); } else { + this.oldConfigSize = data[1]; console.log("Reading Old Config"); - this.oldConfig = smx_old_config_t.decode(data.slice(2, -1), true); + + const slicedData = data.slice(2, -1); + const oldData = Uint8Array.from({ length: 250 }, (_, i) => slicedData[i] ?? 0); + this.oldConfig = smx_old_config_t.decode(oldData, true); this.config = this.convertOldToNew(this.oldConfig); } } @@ -370,7 +375,14 @@ export class SMXConfig { if (!this.oldConfig) throw new ReferenceError("Can not encode old config as it is null"); console.log("Writing Old Config"); this.convertNewToOld(); - return Array.from(new Uint8Array(smx_old_config_t.encode(this.oldConfig, true).buffer)); + + const encodedConfig = smx_old_config_t.encode(this.oldConfig, true); + // If the old config data is less than 128 Bytes, only send the first 128 bytes + if (this.oldConfigSize && this.oldConfigSize <= 128) { + return Array.from(new Uint8Array(encodedConfig.buffer.slice(0, 128))); + } + + return Array.from(new Uint8Array(encodedConfig.buffer)); } /**