Skip to content

Commit

Permalink
Correct config sizes for read/write old config
Browse files Browse the repository at this point in the history
  • Loading branch information
fchorney committed Apr 16, 2024
1 parent feb0e7e commit 095f5b7
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions sdk/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ const OLD_CONFIG_INIT = [
export class SMXConfig {
public config: Decoded<typeof smx_config_t>;
private oldConfig: Decoded<typeof smx_old_config_t> | null = null;
private oldConfigSize: number | null = null;
private firmwareVersion: number;

/**
Expand All @@ -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);
}
}
Expand All @@ -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));
}

/**
Expand Down

0 comments on commit 095f5b7

Please sign in to comment.