Skip to content

Commit

Permalink
feat: rework NVM access, support incremental NVM modification over se…
Browse files Browse the repository at this point in the history
…rial (#7153)
  • Loading branch information
AlCalzone authored Sep 20, 2024
1 parent c21f137 commit 0b79b29
Show file tree
Hide file tree
Showing 67 changed files with 6,169 additions and 2,502 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/error/ZWaveError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export enum ZWaveErrorCodes {
NVM_InvalidFormat,
/** Not enough space in the NVM */
NVM_NoSpace,
/** The NVM hasn't been opened yet */
NVM_NotOpen,

CC_Invalid = 300,
CC_NoNodeID,
Expand Down
10 changes: 5 additions & 5 deletions packages/nvmedit/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ void yargs
const buffer = await fs.readFile(argv.in);
let json: any;
try {
json = nvmToJSON(buffer, argv.verbose);
json = await nvmToJSON(buffer, argv.verbose);
} catch (e) {
try {
json = nvm500ToJSON(buffer);
json = await nvm500ToJSON(buffer);
} catch (ee) {
console.error(e);
process.exit(1);
Expand Down Expand Up @@ -118,8 +118,8 @@ Create a backup of the target stick, use the nvm2json command to convert it to J
}

const nvm = versionIs500
? jsonToNVM500(json, protocolVersion)
: jsonToNVM(json, protocolVersion);
? await jsonToNVM500(json, protocolVersion)
: await jsonToNVM(json, protocolVersion);
await fs.writeFile(argv.out, nvm);
console.error(`NVM (binary) written to ${argv.out}`);

Expand Down Expand Up @@ -217,7 +217,7 @@ Create a backup of the target stick, use the nvm2json command to convert it to J
async (argv) => {
const source = await fs.readFile(argv.source);
const target = await fs.readFile(argv.target);
const output = migrateNVM(source, target);
const output = await migrateNVM(source, target);
await fs.writeFile(argv.out, output);
console.error(`Converted NVM written to ${argv.out}`);

Expand Down
23 changes: 13 additions & 10 deletions packages/nvmedit/src/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type { NVM500JSON } from "./nvm500/NVMParser";
for (const file of files) {
test(`${suite} -> ${file}`, async (t) => {
const data = await fs.readFile(path.join(fixturesDir, file));
const json = nvmToJSON(data);
const json = await nvmToJSON(data);
t.snapshot(json);
});
}
Expand All @@ -36,14 +36,14 @@ import type { NVM500JSON } from "./nvm500/NVMParser";

for (const file of files) {
test(`${suite} -> ${file}`, async (t) => {
const jsonInput: Required<NVMJSON> = await fs.readJson(
const jsonInput: NVMJSON = await fs.readJson(
path.join(fixturesDir, file),
);
const nvm = jsonToNVM(
const nvm = await jsonToNVM(
jsonInput,
jsonInput.controller.applicationVersion,
);
const jsonOutput = nvmToJSON(nvm);
const jsonOutput = await nvmToJSON(nvm);
// @ts-expect-error
if (!("meta" in jsonInput)) delete jsonOutput.meta;

Expand All @@ -66,8 +66,8 @@ import type { NVM500JSON } from "./nvm500/NVMParser";
const nvmIn = await fs.readFile(path.join(fixturesDir, file));

const version = /_(\d+\.\d+\.\d+)[_.]/.exec(file)![1];
const json = nvmToJSON(nvmIn);
const nvmOut = jsonToNVM(json, version);
const json = await nvmToJSON(nvmIn);
const nvmOut = await jsonToNVM(json, version);

t.deepEqual(nvmOut, nvmIn);
});
Expand All @@ -83,7 +83,7 @@ import type { NVM500JSON } from "./nvm500/NVMParser";
for (const file of files) {
test(`${suite} -> ${file}`, async (t) => {
const data = await fs.readFile(path.join(fixturesDir, file));
const json = nvm500ToJSON(data);
const json = await nvm500ToJSON(data);
t.snapshot(json);
});
}
Expand Down Expand Up @@ -119,8 +119,11 @@ import type { NVM500JSON } from "./nvm500/NVMParser";
const nvmIn = await fs.readFile(path.join(fixturesDir, file));

// const lib = /_(static|bridge)_/.exec(file)![1];
const json = nvm500ToJSON(nvmIn);
const nvmOut = jsonToNVM500(json, json.controller.protocolVersion);
const json = await nvm500ToJSON(nvmIn);
const nvmOut = await jsonToNVM500(
json,
json.controller.protocolVersion,
);

t.deepEqual(nvmOut, nvmIn);
});
Expand Down Expand Up @@ -190,7 +193,7 @@ test("700 to 700 migration shortcut", async (t) => {
const nvmTarget = await fs.readFile(
path.join(fixturesDir, "ctrlr_backup_700_7.16_1.bin"),
);
const converted = migrateNVM(nvmSource, nvmTarget);
const converted = await migrateNVM(nvmSource, nvmTarget);

t.deepEqual(converted, nvmSource);
});
1 change: 1 addition & 0 deletions packages/nvmedit/src/convert.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -6984,6 +6984,7 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
{
applicationFileFormat: 4,
controller: {
applicationData: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
applicationName: null,
Expand Down
Binary file modified packages/nvmedit/src/convert.test.ts.snap
Binary file not shown.
Loading

0 comments on commit 0b79b29

Please sign in to comment.