@zwave-js/nvmedit 14.0.0 → 14.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/build/cjs/cli.js +1 -1
  2. package/build/cjs/cli.js.map +1 -1
  3. package/build/cjs/convert.js +1 -1
  4. package/build/cjs/convert.js.map +1 -1
  5. package/build/cjs/index.d.ts +1 -1
  6. package/build/cjs/index.js +4 -8
  7. package/build/cjs/index.js.map +2 -2
  8. package/build/cjs/index_browser.d.ts +12 -0
  9. package/build/cjs/index_browser.js +42 -0
  10. package/build/cjs/index_browser.js.map +7 -0
  11. package/build/cjs/index_safe.d.ts +1 -0
  12. package/build/cjs/index_safe.js +3 -0
  13. package/build/cjs/index_safe.js.map +2 -2
  14. package/build/cjs/lib/NVM3.js +17 -17
  15. package/build/cjs/lib/NVM3.js.map +3 -3
  16. package/build/cjs/lib/nvm3/adapter.js +7 -7
  17. package/build/cjs/lib/nvm3/adapter.js.map +3 -3
  18. package/build/cjs/lib/nvm500/EntryParsers.js +1 -2
  19. package/build/cjs/lib/nvm500/EntryParsers.js.map +2 -2
  20. package/build/cjs/lib/nvm500/adapter.js +1 -1
  21. package/build/cjs/lib/nvm500/adapter.js.map +1 -1
  22. package/build/esm/cli.js +1 -1
  23. package/build/esm/cli.js.map +1 -1
  24. package/build/esm/convert.js +1 -1
  25. package/build/esm/convert.js.map +1 -1
  26. package/build/esm/index.d.ts +1 -1
  27. package/build/esm/index.d.ts.map +1 -1
  28. package/build/esm/index.js +1 -1
  29. package/build/esm/index.js.map +1 -1
  30. package/build/esm/index_browser.d.ts +12 -0
  31. package/build/esm/index_browser.d.ts.map +1 -0
  32. package/build/esm/index_browser.js +6 -0
  33. package/build/esm/index_browser.js.map +1 -0
  34. package/build/esm/index_safe.d.ts +1 -0
  35. package/build/esm/index_safe.d.ts.map +1 -1
  36. package/build/esm/index_safe.js +1 -0
  37. package/build/esm/index_safe.js.map +1 -1
  38. package/build/esm/lib/NVM3.js +2 -2
  39. package/build/esm/lib/NVM3.js.map +1 -1
  40. package/build/esm/lib/nvm3/adapter.js +3 -3
  41. package/build/esm/lib/nvm3/adapter.js.map +1 -1
  42. package/build/esm/lib/nvm500/EntryParsers.d.ts.map +1 -1
  43. package/build/esm/lib/nvm500/EntryParsers.js +1 -2
  44. package/build/esm/lib/nvm500/EntryParsers.js.map +1 -1
  45. package/build/esm/lib/nvm500/adapter.js +1 -1
  46. package/build/esm/lib/nvm500/adapter.js.map +1 -1
  47. package/package.json +6 -5
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var index_browser_exports = {};
20
+ __export(index_browser_exports, {
21
+ BufferedNVMReader: () => import_BufferedNVMReader.BufferedNVMReader,
22
+ FragmentType: () => import_consts.FragmentType,
23
+ NVMAccess: () => import_definitions.NVMAccess,
24
+ ObjectType: () => import_consts.ObjectType,
25
+ PageStatus: () => import_consts.PageStatus,
26
+ PageWriteSize: () => import_consts.PageWriteSize
27
+ });
28
+ module.exports = __toCommonJS(index_browser_exports);
29
+ var import_reflect_metadata = require("reflect-metadata");
30
+ var import_definitions = require("./lib/common/definitions.js");
31
+ var import_BufferedNVMReader = require("./lib/io/BufferedNVMReader.js");
32
+ var import_consts = require("./lib/nvm3/consts.js");
33
+ // Annotate the CommonJS export names for ESM import in node:
34
+ 0 && (module.exports = {
35
+ BufferedNVMReader,
36
+ FragmentType,
37
+ NVMAccess,
38
+ ObjectType,
39
+ PageStatus,
40
+ PageWriteSize
41
+ });
42
+ //# sourceMappingURL=index_browser.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index_browser.ts"],
4
+ "sourcesContent": ["/* @forbiddenImports external */\n\nimport \"reflect-metadata\";\n\nexport type {\n\tNVMJSON,\n\tNVMJSONController,\n\tNVMJSONControllerRFConfig,\n\tNVMJSONNode,\n\tNVMJSONNodeWithInfo,\n\tNVMJSONVirtualNode,\n} from \"./convert.js\";\nexport type { NVM3EraseOptions, NVM3Meta } from \"./lib/NVM3.js\";\nexport type { NVM500EraseOptions, NVM500Info } from \"./lib/NVM500.js\";\nexport { NVMAccess } from \"./lib/common/definitions.js\";\nexport type {\n\tControllerNVMProperty,\n\tControllerNVMPropertyToDataType,\n\tLRNodeNVMProperty,\n\tLRNodeNVMPropertyToDataType,\n\tNVM,\n\tNVMAdapter,\n\tNVMIO,\n\tNVMProperty,\n\tNVMPropertyToDataType,\n\tNodeNVMProperty,\n\tNodeNVMPropertyToDataType,\n} from \"./lib/common/definitions.js\";\nexport { BufferedNVMReader } from \"./lib/io/BufferedNVMReader.js\";\nexport {\n\tFragmentType,\n\tObjectType,\n\tPageStatus,\n\tPageWriteSize,\n} from \"./lib/nvm3/consts.js\";\nexport type { NVM3Object } from \"./lib/nvm3/object.js\";\nexport type { NVM3Page, NVM3PageHeader } from \"./lib/nvm3/page.js\";\nexport type {\n\tNVM500JSON,\n\tNVM500JSONController,\n\tNVM500JSONControllerRFConfig,\n\tNVM500JSONNode,\n\tNVM500JSONNodeWithInfo,\n\tNVM500JSONVirtualNode,\n\tNVM500Meta,\n} from \"./nvm500/NVMParser.js\";\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;AAEA,8BAAO;AAYP,yBAA0B;AAc1B,+BAAkC;AAClC,oBAKO;",
6
+ "names": []
7
+ }
@@ -4,6 +4,7 @@ export type { NVM3EraseOptions, NVM3Meta } from "./lib/NVM3.js";
4
4
  export type { NVM500EraseOptions, NVM500Info } from "./lib/NVM500.js";
5
5
  export { NVMAccess } from "./lib/common/definitions.js";
6
6
  export type { ControllerNVMProperty, ControllerNVMPropertyToDataType, LRNodeNVMProperty, LRNodeNVMPropertyToDataType, NVM, NVMAdapter, NVMIO, NVMProperty, NVMPropertyToDataType, NodeNVMProperty, NodeNVMPropertyToDataType, } from "./lib/common/definitions.js";
7
+ export { BufferedNVMReader } from "./lib/io/BufferedNVMReader.js";
7
8
  export { FragmentType, ObjectType, PageStatus, PageWriteSize, } from "./lib/nvm3/consts.js";
8
9
  export type { NVM3Object } from "./lib/nvm3/object.js";
9
10
  export type { NVM3Page, NVM3PageHeader } from "./lib/nvm3/page.js";
@@ -18,6 +18,7 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var index_safe_exports = {};
20
20
  __export(index_safe_exports, {
21
+ BufferedNVMReader: () => import_BufferedNVMReader.BufferedNVMReader,
21
22
  FragmentType: () => import_consts.FragmentType,
22
23
  NVMAccess: () => import_definitions.NVMAccess,
23
24
  ObjectType: () => import_consts.ObjectType,
@@ -27,9 +28,11 @@ __export(index_safe_exports, {
27
28
  module.exports = __toCommonJS(index_safe_exports);
28
29
  var import_reflect_metadata = require("reflect-metadata");
29
30
  var import_definitions = require("./lib/common/definitions.js");
31
+ var import_BufferedNVMReader = require("./lib/io/BufferedNVMReader.js");
30
32
  var import_consts = require("./lib/nvm3/consts.js");
31
33
  // Annotate the CommonJS export names for ESM import in node:
32
34
  0 && (module.exports = {
35
+ BufferedNVMReader,
33
36
  FragmentType,
34
37
  NVMAccess,
35
38
  ObjectType,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/index_safe.ts"],
4
- "sourcesContent": ["/* @forbiddenImports external */\n\nimport \"reflect-metadata\";\n\nexport type {\n\tNVMJSON,\n\tNVMJSONController,\n\tNVMJSONControllerRFConfig,\n\tNVMJSONNode,\n\tNVMJSONNodeWithInfo,\n\tNVMJSONVirtualNode,\n} from \"./convert.js\";\nexport type { NVM3EraseOptions, NVM3Meta } from \"./lib/NVM3.js\";\nexport type { NVM500EraseOptions, NVM500Info } from \"./lib/NVM500.js\";\nexport { NVMAccess } from \"./lib/common/definitions.js\";\nexport type {\n\tControllerNVMProperty,\n\tControllerNVMPropertyToDataType,\n\tLRNodeNVMProperty,\n\tLRNodeNVMPropertyToDataType,\n\tNVM,\n\tNVMAdapter,\n\tNVMIO,\n\tNVMProperty,\n\tNVMPropertyToDataType,\n\tNodeNVMProperty,\n\tNodeNVMPropertyToDataType,\n} from \"./lib/common/definitions.js\";\nexport {\n\tFragmentType,\n\tObjectType,\n\tPageStatus,\n\tPageWriteSize,\n} from \"./lib/nvm3/consts.js\";\nexport type { NVM3Object } from \"./lib/nvm3/object.js\";\nexport type { NVM3Page, NVM3PageHeader } from \"./lib/nvm3/page.js\";\nexport type {\n\tNVM500JSON,\n\tNVM500JSONController,\n\tNVM500JSONControllerRFConfig,\n\tNVM500JSONNode,\n\tNVM500JSONNodeWithInfo,\n\tNVM500JSONVirtualNode,\n\tNVM500Meta,\n} from \"./nvm500/NVMParser.js\";\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;AAEA,8BAAO;AAYP,yBAA0B;AAc1B,oBAKO;",
4
+ "sourcesContent": ["/* @forbiddenImports external */\n\nimport \"reflect-metadata\";\n\nexport type {\n\tNVMJSON,\n\tNVMJSONController,\n\tNVMJSONControllerRFConfig,\n\tNVMJSONNode,\n\tNVMJSONNodeWithInfo,\n\tNVMJSONVirtualNode,\n} from \"./convert.js\";\nexport type { NVM3EraseOptions, NVM3Meta } from \"./lib/NVM3.js\";\nexport type { NVM500EraseOptions, NVM500Info } from \"./lib/NVM500.js\";\nexport { NVMAccess } from \"./lib/common/definitions.js\";\nexport type {\n\tControllerNVMProperty,\n\tControllerNVMPropertyToDataType,\n\tLRNodeNVMProperty,\n\tLRNodeNVMPropertyToDataType,\n\tNVM,\n\tNVMAdapter,\n\tNVMIO,\n\tNVMProperty,\n\tNVMPropertyToDataType,\n\tNodeNVMProperty,\n\tNodeNVMPropertyToDataType,\n} from \"./lib/common/definitions.js\";\nexport { BufferedNVMReader } from \"./lib/io/BufferedNVMReader.js\";\nexport {\n\tFragmentType,\n\tObjectType,\n\tPageStatus,\n\tPageWriteSize,\n} from \"./lib/nvm3/consts.js\";\nexport type { NVM3Object } from \"./lib/nvm3/object.js\";\nexport type { NVM3Page, NVM3PageHeader } from \"./lib/nvm3/page.js\";\nexport type {\n\tNVM500JSON,\n\tNVM500JSONController,\n\tNVM500JSONControllerRFConfig,\n\tNVM500JSONNode,\n\tNVM500JSONNodeWithInfo,\n\tNVM500JSONVirtualNode,\n\tNVM500Meta,\n} from \"./nvm500/NVMParser.js\";\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;AAEA,8BAAO;AAYP,yBAA0B;AAc1B,+BAAkC;AAClC,oBAKO;",
6
6
  "names": []
7
7
  }
@@ -21,8 +21,8 @@ __export(NVM3_exports, {
21
21
  NVM3: () => NVM3
22
22
  });
23
23
  module.exports = __toCommonJS(NVM3_exports);
24
- var import_core = require("@zwave-js/core");
25
- var import_shared = require("@zwave-js/shared");
24
+ var import_safe = require("@zwave-js/core/safe");
25
+ var import_safe2 = require("@zwave-js/shared/safe");
26
26
  var import_definitions = require("./common/definitions.js");
27
27
  var import_utils = require("./common/utils.js");
28
28
  var import_consts = require("./nvm3/consts.js");
@@ -208,7 +208,7 @@ class NVM3 {
208
208
  }
209
209
  if (!parts?.length || !complete || objType == void 0)
210
210
  return;
211
- return import_shared.Bytes.concat(parts);
211
+ return import_safe2.Bytes.concat(parts);
212
212
  }
213
213
  async writeObjects(objects) {
214
214
  const section = this.getNVMSectionForFile(objects[0].key);
@@ -220,9 +220,9 @@ class NVM3 {
220
220
  const toPreserve = [...section.objectLocations].filter(([, pageIndex]) => pageIndex === section.currentPage).map(([fileID]) => page.objects.findLast((h) => h.key === fileID)).filter((h) => h != void 0).filter((h) => h.type !== import_consts.ObjectType.Deleted);
221
221
  for (const header of toPreserve) {
222
222
  const data = await this.get(header.key);
223
- console.error(`Need to preserve object ${(0, import_shared.num2hex)(header.key)}
223
+ console.error(`Need to preserve object ${(0, import_safe2.num2hex)(header.key)}
224
224
  page index: ${section.currentPage}
225
- object type: ${(0, import_shared.getEnumMemberName)(import_consts.ObjectType, header.type)}
225
+ object type: ${(0, import_safe2.getEnumMemberName)(import_consts.ObjectType, header.type)}
226
226
  data: ${data != void 0 ? `${data.length} bytes` : "(no data)"}`);
227
227
  objects.push({
228
228
  key: header.key,
@@ -330,11 +330,11 @@ class NVM3 {
330
330
  const maxPageSize = sharedFileSystem ? import_consts.FLASH_MAX_PAGE_SIZE_800 : import_consts.FLASH_MAX_PAGE_SIZE_700;
331
331
  const pageSize = Math.min(options?.pageSize ?? maxPageSize, maxPageSize);
332
332
  if (this._io.size % pageSize !== 0) {
333
- throw new import_core.ZWaveError(`Invalid page size. NVM size ${this._io.size} must be a multiple of the page size ${pageSize}.`, import_core.ZWaveErrorCodes.Argument_Invalid);
333
+ throw new import_safe.ZWaveError(`Invalid page size. NVM size ${this._io.size} must be a multiple of the page size ${pageSize}.`, import_safe.ZWaveErrorCodes.Argument_Invalid);
334
334
  } else if (!sharedFileSystem && import_consts.ZWAVE_APPLICATION_NVM_SIZE % pageSize !== 0) {
335
- throw new import_core.ZWaveError(`Invalid page size. The application NVM size ${import_consts.ZWAVE_APPLICATION_NVM_SIZE} must be a multiple of the page size ${pageSize}.`, import_core.ZWaveErrorCodes.Argument_Invalid);
335
+ throw new import_safe.ZWaveError(`Invalid page size. The application NVM size ${import_consts.ZWAVE_APPLICATION_NVM_SIZE} must be a multiple of the page size ${pageSize}.`, import_safe.ZWaveErrorCodes.Argument_Invalid);
336
336
  } else if (!sharedFileSystem && (this._io.size - import_consts.ZWAVE_APPLICATION_NVM_SIZE) % pageSize !== 0) {
337
- throw new import_core.ZWaveError(`Invalid page size. The protocol NVM size ${this._io.size - import_consts.ZWAVE_APPLICATION_NVM_SIZE} must be a multiple of the page size ${pageSize}.`, import_core.ZWaveErrorCodes.Argument_Invalid);
337
+ throw new import_safe.ZWaveError(`Invalid page size. The protocol NVM size ${this._io.size - import_consts.ZWAVE_APPLICATION_NVM_SIZE} must be a multiple of the page size ${pageSize}.`, import_safe.ZWaveErrorCodes.Argument_Invalid);
338
338
  }
339
339
  await this.ensureWritable();
340
340
  const applicationPages = [];
@@ -393,9 +393,9 @@ class NVM3 {
393
393
  }
394
394
  async function readPageHeader(io, offset) {
395
395
  if (offset > io.size - import_consts.NVM3_PAGE_HEADER_SIZE) {
396
- throw new import_core.ZWaveError("Incomplete page in buffer!", import_core.ZWaveErrorCodes.NVM_InvalidFormat);
396
+ throw new import_safe.ZWaveError("Incomplete page in buffer!", import_safe.ZWaveErrorCodes.NVM_InvalidFormat);
397
397
  }
398
- const buffer = import_shared.Bytes.view((await io.read(offset, import_consts.NVM3_PAGE_HEADER_SIZE)).buffer);
398
+ const buffer = import_safe2.Bytes.view((await io.read(offset, import_consts.NVM3_PAGE_HEADER_SIZE)).buffer);
399
399
  const { version, eraseCount } = tryGetVersionAndEraseCount(buffer);
400
400
  const status = buffer.readUInt32LE(12);
401
401
  const devInfo = buffer.readUInt16LE(16);
@@ -417,10 +417,10 @@ async function readPageHeader(io, offset) {
417
417
  }
418
418
  }
419
419
  if (pageSize > 65535) {
420
- throw new import_core.ZWaveError("Could not determine page size!", import_core.ZWaveErrorCodes.NVM_InvalidFormat);
420
+ throw new import_safe.ZWaveError("Could not determine page size!", import_safe.ZWaveErrorCodes.NVM_InvalidFormat);
421
421
  }
422
422
  if (io.size < offset + pageSize) {
423
- throw new import_core.ZWaveError(`NVM contains incomplete page at offset ${(0, import_shared.num2hex)(offset)}!`, import_core.ZWaveErrorCodes.NVM_InvalidFormat);
423
+ throw new import_safe.ZWaveError(`NVM contains incomplete page at offset ${(0, import_safe2.num2hex)(offset)}!`, import_safe.ZWaveErrorCodes.NVM_InvalidFormat);
424
424
  }
425
425
  const formatInfo = buffer.readUInt16LE(18);
426
426
  const encrypted = !(formatInfo & 1);
@@ -437,14 +437,14 @@ async function readPageHeader(io, offset) {
437
437
  };
438
438
  }
439
439
  function tryGetVersionAndEraseCount(header) {
440
- const buffer = import_shared.Bytes.view(header);
440
+ const buffer = import_safe2.Bytes.view(header);
441
441
  const version = buffer.readUInt16LE(0);
442
442
  const magic = buffer.readUInt16LE(2);
443
443
  if (magic !== import_consts.NVM3_PAGE_MAGIC) {
444
- throw new import_core.ZWaveError("Not a valid NVM3 page!", import_core.ZWaveErrorCodes.NVM_InvalidFormat);
444
+ throw new import_safe.ZWaveError("Not a valid NVM3 page!", import_safe.ZWaveErrorCodes.NVM_InvalidFormat);
445
445
  }
446
446
  if (version !== 1) {
447
- throw new import_core.ZWaveError(`Unsupported NVM3 page version: ${version}`, import_core.ZWaveErrorCodes.NVM_NotSupported);
447
+ throw new import_safe.ZWaveError(`Unsupported NVM3 page version: ${version}`, import_safe.ZWaveErrorCodes.NVM_NotSupported);
448
448
  }
449
449
  let eraseCount = buffer.readUInt32LE(4);
450
450
  const eraseCountCode = eraseCount >>> import_consts.NVM3_PAGE_COUNTER_SIZE;
@@ -455,7 +455,7 @@ function tryGetVersionAndEraseCount(header) {
455
455
  eraseCountInv &= import_consts.NVM3_PAGE_COUNTER_MASK;
456
456
  (0, import_utils2.validateBergerCode)(eraseCountInv, eraseCountInvCode, import_consts.NVM3_PAGE_COUNTER_SIZE);
457
457
  if (eraseCount !== (~eraseCountInv & import_consts.NVM3_PAGE_COUNTER_MASK)) {
458
- throw new import_core.ZWaveError("Invalid erase count!", import_core.ZWaveErrorCodes.NVM_InvalidFormat);
458
+ throw new import_safe.ZWaveError("Invalid erase count!", import_safe.ZWaveErrorCodes.NVM_InvalidFormat);
459
459
  }
460
460
  return { version, eraseCount };
461
461
  }
@@ -498,7 +498,7 @@ async function readObjectHeader(io, offset) {
498
498
  (0, import_utils2.validateBergerCodeMulti)([hdr1], import_consts.NVM3_CODE_SMALL_SHIFT);
499
499
  }
500
500
  if (io.size < offset + headerSize + fragmentSize) {
501
- throw new import_core.ZWaveError(`NVM contains incomplete object at offset ${(0, import_shared.num2hex)(offset)}!`, import_core.ZWaveErrorCodes.NVM_InvalidFormat);
501
+ throw new import_safe.ZWaveError(`NVM contains incomplete object at offset ${(0, import_safe2.num2hex)(offset)}!`, import_safe.ZWaveErrorCodes.NVM_InvalidFormat);
502
502
  }
503
503
  const alignedFragmentSize = (0, import_object.getAlignedSize)(fragmentSize);
504
504
  const alignedSize = headerSize + alignedFragmentSize;
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/lib/NVM3.ts"],
4
- "sourcesContent": ["import { ZWaveError, ZWaveErrorCodes } from \"@zwave-js/core\";\nimport { Bytes, getEnumMemberName, num2hex } from \"@zwave-js/shared\";\nimport { type NVM, NVMAccess, type NVMIO } from \"./common/definitions.js\";\nimport {\n\tnvmReadBuffer,\n\tnvmReadUInt32LE,\n\tnvmWriteBuffer,\n} from \"./common/utils.js\";\nimport {\n\tFLASH_MAX_PAGE_SIZE_700,\n\tFLASH_MAX_PAGE_SIZE_800,\n\tFragmentType,\n\tNVM3_CODE_LARGE_SHIFT,\n\tNVM3_CODE_SMALL_SHIFT,\n\tNVM3_COUNTER_SIZE,\n\tNVM3_MAX_OBJ_SIZE_SMALL,\n\tNVM3_OBJ_FRAGTYPE_MASK,\n\tNVM3_OBJ_FRAGTYPE_SHIFT,\n\tNVM3_OBJ_HEADER_SIZE_LARGE,\n\tNVM3_OBJ_KEY_MASK,\n\tNVM3_OBJ_KEY_SHIFT,\n\tNVM3_OBJ_LARGE_LEN_MASK,\n\tNVM3_OBJ_TYPE_MASK,\n\tNVM3_PAGE_COUNTER_MASK,\n\tNVM3_PAGE_COUNTER_SIZE,\n\tNVM3_PAGE_HEADER_SIZE,\n\tNVM3_PAGE_MAGIC,\n\tObjectType,\n\tPageStatus,\n\tPageWriteSize,\n\tZWAVE_APPLICATION_NVM_SIZE,\n} from \"./nvm3/consts.js\";\nimport {\n\tApplicationVersionFile800ID,\n\ttype NVMSection,\n\tgetNVMSectionByFileID,\n} from \"./nvm3/files/index.js\";\nimport {\n\ttype NVM3Object,\n\ttype NVM3ObjectHeader,\n\tfragmentLargeObject,\n\tgetAlignedSize,\n\tgetObjectHeader,\n\tgetRequiredSpace,\n\tserializeObject,\n} from \"./nvm3/object.js\";\nimport {\n\ttype NVM3PageHeader,\n\tpageSizeFromBits,\n\tserializePageHeader,\n} from \"./nvm3/page.js\";\nimport { validateBergerCode, validateBergerCodeMulti } from \"./nvm3/utils.js\";\n\n// TODO: Possible optimizations:\n// Investigate if there is a better way to determine whether the NVM\n// uses a shared FS or not. The current implementation scans all objects\n// to find the 800 series application version file.\n// Alternatively, we could simply check if each page starts with an object header.\n// If yes, read the objects lazily when needed. If not, remember that the page is empty.\n\nexport type NVM3PageInfo = NVM3PageHeader & {\n\tobjects: NVM3ObjectHeader[];\n};\n\nexport interface NVM3SectionInfo {\n\tpages: NVM3PageInfo[];\n\t/** The index of the current page */\n\tcurrentPage: number;\n\t/** The next byte to write in the current page */\n\toffsetInPage: number;\n\t/** A map of file IDs and page indizes in which their last copy resides */\n\tobjectLocations: Map<number, number>;\n}\n\nexport type NVM3FileSystemInfo = {\n\tisSharedFileSystem: true;\n\tsections: Record<\"all\", NVM3SectionInfo>;\n} | {\n\tisSharedFileSystem: false;\n\tsections: Record<NVMSection, NVM3SectionInfo>;\n};\n\nexport interface NVM3Meta {\n\tsharedFileSystem: boolean;\n\tpageSize: number;\n\tdeviceFamily: number;\n\twriteSize: PageWriteSize;\n\tmemoryMapped: boolean;\n}\n\nexport type NVM3EraseOptions = Partial<NVM3Meta>;\n\nexport class NVM3 implements NVM<number, Uint8Array> {\n\tpublic constructor(io: NVMIO) {\n\t\tthis._io = io;\n\t}\n\n\tprivate _io: NVMIO;\n\tprivate _access: NVMAccess = NVMAccess.None;\n\n\tprivate _info: NVM3FileSystemInfo | undefined;\n\tpublic get info(): NVM3FileSystemInfo | undefined {\n\t\treturn this._info;\n\t}\n\n\tprivate async ensureReadable(): Promise<void> {\n\t\tif (\n\t\t\tthis._access === NVMAccess.Read\n\t\t\t|| this._access === NVMAccess.ReadWrite\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tif (this._access === NVMAccess.Write) {\n\t\t\tawait this._io.close();\n\t\t}\n\t\tthis._access = await this._io.open(NVMAccess.Read);\n\t}\n\n\tprivate async ensureWritable(): Promise<void> {\n\t\tif (\n\t\t\tthis._access === NVMAccess.Write\n\t\t\t|| this._access === NVMAccess.ReadWrite\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tif (this._access === NVMAccess.Read) {\n\t\t\tawait this._io.close();\n\t\t}\n\t\tthis._access = await this._io.open(NVMAccess.Write);\n\t}\n\n\tpublic async init(): Promise<NVM3FileSystemInfo> {\n\t\tawait this.ensureReadable();\n\n\t\tlet pageOffset = 0;\n\t\t// Determine NVM size, scan pages\n\t\tconst pages: NVM3PageInfo[] = [];\n\t\tlet isSharedFileSystem = false;\n\t\twhile (pageOffset < this._io.size) {\n\t\t\t// console.debug(\n\t\t\t// \t`NVM3 init() - reading page header at offset ${\n\t\t\t// \t\tnum2hex(pageOffset)\n\t\t\t// \t}`,\n\t\t\t// );\n\t\t\tconst header = await readPageHeader(this._io, pageOffset);\n\t\t\tpages.push({\n\t\t\t\t...header,\n\t\t\t\tobjects: [],\n\t\t\t});\n\t\t\tpageOffset += header.pageSize;\n\t\t}\n\n\t\t// Scan each page for objects\n\t\tfor (const page of pages) {\n\t\t\t// Scan objects in this page\n\t\t\tlet objectOffset = page.offset + NVM3_PAGE_HEADER_SIZE;\n\t\t\tconst nextPageOffset = page.offset + page.pageSize;\n\t\t\twhile (objectOffset < nextPageOffset) {\n\t\t\t\t// console.debug(\n\t\t\t\t// \t`NVM3 init() - reading object header. page offset ${\n\t\t\t\t// \t\tnum2hex(page.offset)\n\t\t\t\t// \t}, object offset ${num2hex(objectOffset)}`,\n\t\t\t\t// );\n\t\t\t\tconst objectHeader = await readObjectHeader(\n\t\t\t\t\tthis._io,\n\t\t\t\t\tobjectOffset,\n\t\t\t\t);\n\t\t\t\tif (objectHeader) {\n\t\t\t\t\tpage.objects.push(objectHeader);\n\t\t\t\t\tobjectOffset += objectHeader.alignedSize;\n\n\t\t\t\t\t// Detect the 800 series shared protocol & application NVM file system\n\t\t\t\t\t// by looking for the 800 series application version file\n\t\t\t\t\tif (objectHeader.key === ApplicationVersionFile800ID) {\n\t\t\t\t\t\tisSharedFileSystem = true;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\t// Reached the end of the data in this page\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// By convention, we only use the applicationPages in that case\n\t\tlet applicationPages: NVM3PageInfo[];\n\t\tlet protocolPages: NVM3PageInfo[];\n\n\t\tif (isSharedFileSystem) {\n\t\t\tapplicationPages = pages;\n\t\t\tprotocolPages = [];\n\t\t} else {\n\t\t\tapplicationPages = pages.filter(\n\t\t\t\t(p) => p.offset < ZWAVE_APPLICATION_NVM_SIZE,\n\t\t\t);\n\t\t\tprotocolPages = pages.filter(\n\t\t\t\t(p) => p.offset >= ZWAVE_APPLICATION_NVM_SIZE,\n\t\t\t);\n\t\t}\n\n\t\t// NVM3 layouts pages in a ring buffer. Pages are written from front to back, then occupied pages\n\t\t// are erased and overwritten. Pages at the start of the memory section may have an erase count that's 1 higher\n\t\t// than the pages at the end.\n\t\tconst pageInfoToSectionInfo = (\n\t\t\tpages: NVM3PageInfo[],\n\t\t): NVM3SectionInfo => {\n\t\t\t// Find the current page, which is either:\n\t\t\t// - The last page with the high erase count that contains an object\n\t\t\tconst maxEraseCount = Math.max(...pages.map((p) => p.eraseCount));\n\t\t\tlet currentPageIndex = pages.findLastIndex((p) =>\n\t\t\t\tp.eraseCount === maxEraseCount && p.objects.length > 0\n\t\t\t);\n\t\t\t// - or if there is none, the last page with the lower erase count that contains an object\n\t\t\tif (currentPageIndex === -1) {\n\t\t\t\tcurrentPageIndex = pages.findLastIndex((p) =>\n\t\t\t\t\tp.objects.length > 0\n\t\t\t\t);\n\t\t\t}\n\t\t\t// - Or if no objects exist at all, the beginning of the section\n\t\t\tif (currentPageIndex === -1) currentPageIndex = 0;\n\n\t\t\t// Find the next free byte of the current page\n\t\t\tconst currentPage = pages[currentPageIndex];\n\t\t\tlet offset = NVM3_PAGE_HEADER_SIZE;\n\t\t\tfor (const object of currentPage.objects) {\n\t\t\t\toffset += object.alignedSize;\n\t\t\t}\n\n\t\t\tconst objectLocations = new Map<number, number>();\n\t\t\tfor (let i = 0; i < pages.length; i++) {\n\t\t\t\tconst page = pages[i];\n\t\t\t\tfor (const object of page.objects) {\n\t\t\t\t\tconst location = objectLocations.get(object.key);\n\t\t\t\t\tif (location == undefined) {\n\t\t\t\t\t\t// Object seen for the first time, remember the page it is in\n\t\t\t\t\t\tobjectLocations.set(object.key, i);\n\t\t\t\t\t} else if (\n\t\t\t\t\t\t(object.fragmentType === FragmentType.None\n\t\t\t\t\t\t\t|| object.fragmentType === FragmentType.First)\n\t\t\t\t\t\t&& (page.eraseCount >= pages[location].eraseCount)\n\t\t\t\t\t) {\n\t\t\t\t\t\t// Object was seen before. Only remember it if it is the only\n\t\t\t\t\t\t// or first fragment and the object appears in a later location\n\t\t\t\t\t\t// of the ring buffer\n\t\t\t\t\t\tobjectLocations.set(object.key, i);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tpages,\n\t\t\t\toffsetInPage: offset,\n\t\t\t\tcurrentPage: currentPageIndex,\n\t\t\t\tobjectLocations,\n\t\t\t};\n\t\t};\n\n\t\tif (isSharedFileSystem) {\n\t\t\tthis._info = {\n\t\t\t\tisSharedFileSystem: true,\n\t\t\t\tsections: {\n\t\t\t\t\tall: pageInfoToSectionInfo(applicationPages),\n\t\t\t\t},\n\t\t\t};\n\t\t} else {\n\t\t\tthis._info = {\n\t\t\t\tisSharedFileSystem: false,\n\t\t\t\tsections: {\n\t\t\t\t\tapplication: pageInfoToSectionInfo(applicationPages),\n\t\t\t\t\tprotocol: pageInfoToSectionInfo(protocolPages),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\treturn this._info;\n\t}\n\n\tprivate getNVMSectionForFile(fileId: number): NVM3SectionInfo {\n\t\t// Determine which ring buffer to read in\n\t\treturn this._info!.isSharedFileSystem\n\t\t\t? this._info!.sections.all\n\t\t\t: this._info!.sections[getNVMSectionByFileID(fileId)];\n\t}\n\n\tpublic async has(fileId: number): Promise<boolean> {\n\t\tthis._info ??= await this.init();\n\n\t\t// Determine which ring buffer to read in\n\t\tconst section = this.getNVMSectionForFile(fileId);\n\n\t\treturn section.objectLocations.has(fileId);\n\t}\n\n\tpublic readObjectData(object: NVM3ObjectHeader): Promise<Uint8Array> {\n\t\treturn nvmReadBuffer(\n\t\t\tthis._io,\n\t\t\tobject.offset + object.headerSize,\n\t\t\tobject.fragmentSize,\n\t\t);\n\t}\n\n\tpublic async get(fileId: number): Promise<Uint8Array | undefined> {\n\t\tthis._info ??= await this.init();\n\n\t\t// Determine which ring buffer to read in\n\t\tconst section = this.getNVMSectionForFile(fileId);\n\n\t\tconst pages = section.pages;\n\n\t\t// TODO: There should be no need for scanning, since we know the object locations after init().\n\n\t\t// Start scanning backwards through the pages ring buffer, starting with the current page\n\t\tlet parts: Uint8Array[] | undefined;\n\t\tlet complete = false;\n\t\tlet objType: ObjectType | undefined;\n\t\tconst resetFragments = () => {\n\t\t\t// if (parts?.length) {\n\t\t\t// \tconsole.debug(\"Resetting fragmented object\");\n\t\t\t// }\n\t\t\tparts = undefined;\n\t\t\tcomplete = false;\n\t\t};\n\t\tpages: for (let offset = 0; offset < pages.length; offset++) {\n\t\t\tconst index = (section.currentPage - offset + pages.length)\n\t\t\t\t% pages.length;\n\t\t\tconst page = pages[index];\n\t\t\t// console.debug(\n\t\t\t// \t`NVM3.get(${fileId}): scanning page ${index} at offset ${\n\t\t\t// \t\tnum2hex(page.offset)\n\t\t\t// \t}`,\n\t\t\t// );\n\t\t\t// Scan objects in this page, read backwards.\n\t\t\t// The last non-deleted object wins\n\t\t\tobjects: for (let j = page.objects.length - 1; j >= 0; j--) {\n\t\t\t\tconst object = page.objects[j];\n\n\t\t\t\tconst readObject = () => this.readObjectData(object);\n\n\t\t\t\tif (object.key !== fileId) {\n\t\t\t\t\t// Reset any fragmented objects when encountering a different key\n\t\t\t\t\tresetFragments();\n\t\t\t\t\tcontinue objects;\n\t\t\t\t}\n\n\t\t\t\tif (object.type === ObjectType.Deleted) {\n\t\t\t\t\t// Last action for this object was a deletion. There is no data.\n\t\t\t\t\treturn;\n\t\t\t\t} else if (object.fragmentType === FragmentType.None) {\n\t\t\t\t\t// console.debug(\n\t\t\t\t\t// \t`NVM3.get(${fileId}): found complete object - header offset ${\n\t\t\t\t\t// \t\tnum2hex(object.offset)\n\t\t\t\t\t// \t}, content offset ${\n\t\t\t\t\t// \t\tnum2hex(object.offset + object.headerSize)\n\t\t\t\t\t// \t}, length ${object.fragmentSize}`,\n\t\t\t\t\t// );\n\t\t\t\t\t// This is a complete object\n\t\t\t\t\tparts = [await readObject()];\n\t\t\t\t\tobjType = object.type;\n\t\t\t\t\tcomplete = true;\n\t\t\t\t\tbreak pages;\n\t\t\t\t} else if (object.fragmentType === FragmentType.Last) {\n\t\t\t\t\t// console.debug(\n\t\t\t\t\t// \t`NVM3.get(${fileId}): found LAST fragment - header offset ${\n\t\t\t\t\t// \t\tnum2hex(object.offset)\n\t\t\t\t\t// \t}, content offset ${\n\t\t\t\t\t// \t\tnum2hex(object.offset + object.headerSize)\n\t\t\t\t\t// \t}, length ${object.fragmentSize}`,\n\t\t\t\t\t// );\n\t\t\t\t\tparts = [await readObject()];\n\t\t\t\t\tobjType = object.type;\n\t\t\t\t\tcomplete = false;\n\t\t\t\t} else if (object.fragmentType === FragmentType.Next) {\n\t\t\t\t\tif (parts?.length && objType === object.type) {\n\t\t\t\t\t\t// console.debug(\n\t\t\t\t\t\t// \t`NVM3.get(${fileId}): found NEXT fragment - header offset ${\n\t\t\t\t\t\t// \t\tnum2hex(object.offset)\n\t\t\t\t\t\t// \t}, content offset ${\n\t\t\t\t\t\t// \t\tnum2hex(object.offset + object.headerSize)\n\t\t\t\t\t\t// \t}, length ${object.fragmentSize}`,\n\t\t\t\t\t\t// );\n\t\t\t\t\t\tparts.unshift(await readObject());\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// This shouldn't be here\n\t\t\t\t\t\tresetFragments();\n\t\t\t\t\t}\n\t\t\t\t} else if (object.fragmentType === FragmentType.First) {\n\t\t\t\t\tif (parts?.length && objType === object.type) {\n\t\t\t\t\t\t// console.debug(\n\t\t\t\t\t\t// \t`NVM3.get(${fileId}): found FIRST fragment - header offset ${\n\t\t\t\t\t\t// \t\tnum2hex(object.offset)\n\t\t\t\t\t\t// \t}, content offset ${\n\t\t\t\t\t\t// \t\tnum2hex(object.offset + object.headerSize)\n\t\t\t\t\t\t// \t}, length ${object.fragmentSize}`,\n\t\t\t\t\t\t// );\n\t\t\t\t\t\tparts.unshift(await readObject());\n\t\t\t\t\t\tcomplete = true;\n\t\t\t\t\t\tbreak pages;\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// This shouldn't be here\n\t\t\t\t\t\tresetFragments();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (!parts?.length || !complete || objType == undefined) return;\n\n\t\treturn Bytes.concat(parts);\n\t}\n\n\tprivate async writeObjects(objects: NVM3Object[]): Promise<void> {\n\t\tconst section = this.getNVMSectionForFile(objects[0].key);\n\n\t\tlet page = section.pages[section.currentPage];\n\t\tlet remainingSpace = page.pageSize\n\t\t\t- NVM3_PAGE_HEADER_SIZE\n\t\t\t- section.offsetInPage;\n\n\t\t// TODO: See if we can avoid double writes on a page change\n\n\t\t/** Moves to the next page and erases it if necessary */\n\t\tconst nextPage = async () => {\n\t\t\tsection.currentPage = (section.currentPage + 1)\n\t\t\t\t% section.pages.length;\n\t\t\tpage = section.pages[section.currentPage];\n\n\t\t\t// Find headers of objects that need to be preserved\n\t\t\tconst toPreserve = [...section.objectLocations].filter((\n\t\t\t\t[, pageIndex],\n\t\t\t) => pageIndex === section.currentPage)\n\t\t\t\t.map(([fileID]) =>\n\t\t\t\t\tpage.objects.findLast((h) => h.key === fileID)\n\t\t\t\t)\n\t\t\t\t.filter((h) => h != undefined)\n\t\t\t\t.filter((h) => h.type !== ObjectType.Deleted);\n\t\t\t// And add the objects to the TODO list\n\t\t\tfor (const header of toPreserve) {\n\t\t\t\tconst data = await this.get(header.key);\n\t\t\t\tconsole.error(`Need to preserve object ${num2hex(header.key)}\n page index: ${section.currentPage}\n object type: ${getEnumMemberName(ObjectType, header.type)}\n data: ${data != undefined ? `${data.length} bytes` : \"(no data)\"}`);\n\t\t\t\tobjects.push({\n\t\t\t\t\tkey: header.key,\n\t\t\t\t\ttype: header.type,\n\t\t\t\t\tfragmentType: FragmentType.None,\n\t\t\t\t\tdata,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (page.objects.length > 0) {\n\t\t\t\t// The page needs to be erased\n\t\t\t\tpage.eraseCount++;\n\t\t\t\tpage.objects = [];\n\n\t\t\t\tconst pageHeaderBuffer = serializePageHeader(page);\n\t\t\t\tconst pageBuffer = new Uint8Array(page.pageSize).fill(0xff);\n\t\t\t\tpageBuffer.set(pageHeaderBuffer, 0);\n\n\t\t\t\tawait nvmWriteBuffer(this._io, page.offset, pageBuffer);\n\t\t\t}\n\n\t\t\tsection.offsetInPage = NVM3_PAGE_HEADER_SIZE;\n\t\t\tremainingSpace = page.pageSize - NVM3_PAGE_HEADER_SIZE;\n\t\t};\n\n\t\t// Go through the list of objects and write all of them to the NVM\n\t\tfor (const object of objects) {\n\t\t\tconst isLargeObject = object.type === ObjectType.DataLarge\n\t\t\t\t|| object.type === ObjectType.CounterLarge;\n\n\t\t\tlet fragments: NVM3Object[] | undefined;\n\n\t\t\tif (isLargeObject) {\n\t\t\t\t// Large objects may be fragmented\n\n\t\t\t\t// We need to start a new page, if the remaining space is not enough for\n\t\t\t\t// the object header plus additional data\n\t\t\t\tif (remainingSpace <= NVM3_OBJ_HEADER_SIZE_LARGE) {\n\t\t\t\t\tawait nextPage();\n\t\t\t\t}\n\n\t\t\t\tfragments = fragmentLargeObject(\n\t\t\t\t\tobject as any,\n\t\t\t\t\tremainingSpace,\n\t\t\t\t\tpage.pageSize - NVM3_PAGE_HEADER_SIZE,\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\t// Small objects cannot be fragmented. If they don't fit,\n\t\t\t\t// they need to go on the next page.\n\t\t\t\tconst requiredSpace = getRequiredSpace(object);\n\t\t\t\tif (requiredSpace > remainingSpace) {\n\t\t\t\t\tawait nextPage();\n\t\t\t\t}\n\t\t\t\tfragments = [object];\n\t\t\t}\n\n\t\t\t// Write each fragment to the NVM. If there are multiple fragments,\n\t\t\t// each one but the first needs to be written at the beginning of a new page\n\t\t\tfor (let i = 0; i < fragments.length; i++) {\n\t\t\t\tif (i > 0) await nextPage();\n\t\t\t\tconst fragment = fragments[i];\n\n\t\t\t\tconst objBuffer = serializeObject(fragment);\n\t\t\t\tconst objOffset = page.offset + section.offsetInPage;\n\t\t\t\tawait this._io.write(objOffset, objBuffer);\n\t\t\t\tconst requiredSpace = getRequiredSpace(fragment);\n\t\t\t\tsection.offsetInPage += requiredSpace;\n\t\t\t\tremainingSpace -= requiredSpace;\n\n\t\t\t\t// Remember which objects exist in this page\n\t\t\t\tpage.objects.push(getObjectHeader(object, objOffset));\n\n\t\t\t\t// And remember where this object lives\n\t\t\t\tif (object.type === ObjectType.Deleted) {\n\t\t\t\t\tsection.objectLocations.delete(object.key);\n\t\t\t\t} else if (\n\t\t\t\t\tfragment.fragmentType === FragmentType.None\n\t\t\t\t\t|| fragment.fragmentType === FragmentType.First\n\t\t\t\t) {\n\t\t\t\t\tsection.objectLocations.set(\n\t\t\t\t\t\tfragment.key,\n\t\t\t\t\t\tsection.currentPage,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic async set(property: number, value: Uint8Array): Promise<void> {\n\t\tif (!this._info) await this.init();\n\t\tawait this.ensureWritable();\n\n\t\tawait this.writeObjects([{\n\t\t\tkey: property,\n\t\t\ttype: value.length <= NVM3_MAX_OBJ_SIZE_SMALL\n\t\t\t\t? ObjectType.DataSmall\n\t\t\t\t: ObjectType.DataLarge,\n\t\t\t// writeObject deals with fragmentation\n\t\t\tfragmentType: FragmentType.None,\n\t\t\tdata: value,\n\t\t}]);\n\t}\n\n\t/** Writes multiple values to the NVM at once. `null` / `undefined` cause the value to be deleted */\n\tpublic async setMany(\n\t\tvalues: [number, Uint8Array | null | undefined][],\n\t): Promise<void> {\n\t\tif (!this._info) await this.init();\n\t\tawait this.ensureWritable();\n\n\t\t// Group objects by their NVM section\n\t\tconst objectsBySection = new Map<\n\t\t\tnumber, /* offset */\n\t\t\t[number, Uint8Array | null | undefined][]\n\t\t>();\n\t\tfor (const [key, value] of values) {\n\t\t\tconst sectionOffset =\n\t\t\t\tthis.getNVMSectionForFile(key).pages[0].offset;\n\t\t\tif (!objectsBySection.has(sectionOffset)) {\n\t\t\t\tobjectsBySection.set(sectionOffset, []);\n\t\t\t}\n\t\t\tobjectsBySection.get(sectionOffset)!.push([key, value]);\n\t\t}\n\n\t\t// And call writeObjects for each group\n\t\tfor (const objectGroups of objectsBySection.values()) {\n\t\t\tawait this.writeObjects(\n\t\t\t\tobjectGroups.map(([key, value]) => (value\n\t\t\t\t\t? {\n\t\t\t\t\t\tkey,\n\t\t\t\t\t\ttype: value.length <= NVM3_MAX_OBJ_SIZE_SMALL\n\t\t\t\t\t\t\t? ObjectType.DataSmall\n\t\t\t\t\t\t\t: ObjectType.DataLarge,\n\t\t\t\t\t\t// writeObject deals with fragmentation\n\t\t\t\t\t\tfragmentType: FragmentType.None,\n\t\t\t\t\t\tdata: value,\n\t\t\t\t\t}\n\t\t\t\t\t: {\n\t\t\t\t\t\tkey,\n\t\t\t\t\t\ttype: ObjectType.Deleted,\n\t\t\t\t\t\tfragmentType: FragmentType.None,\n\t\t\t\t\t})\n\t\t\t\t),\n\t\t\t);\n\t\t}\n\t}\n\n\tpublic async delete(property: number): Promise<void> {\n\t\tif (!this._info) await this.init();\n\t\tawait this.ensureWritable();\n\n\t\tawait this.writeObjects([{\n\t\t\tkey: property,\n\t\t\ttype: ObjectType.Deleted,\n\t\t\tfragmentType: FragmentType.None,\n\t\t}]);\n\t}\n\n\tpublic async erase(options?: NVM3EraseOptions): Promise<void> {\n\t\tconst {\n\t\t\tdeviceFamily = 2047,\n\t\t\twriteSize = PageWriteSize.WRITE_SIZE_16,\n\t\t\tmemoryMapped = true,\n\t\t\tsharedFileSystem = false,\n\t\t} = options ?? {};\n\t\tconst maxPageSize = sharedFileSystem\n\t\t\t? FLASH_MAX_PAGE_SIZE_800\n\t\t\t: FLASH_MAX_PAGE_SIZE_700;\n\t\tconst pageSize = Math.min(\n\t\t\toptions?.pageSize ?? maxPageSize,\n\t\t\tmaxPageSize,\n\t\t);\n\n\t\t// Make sure we won't be writing incomplete pages\n\t\tif (this._io.size % pageSize !== 0) {\n\t\t\tthrow new ZWaveError(\n\t\t\t\t`Invalid page size. NVM size ${this._io.size} must be a multiple of the page size ${pageSize}.`,\n\t\t\t\tZWaveErrorCodes.Argument_Invalid,\n\t\t\t);\n\t\t} else if (\n\t\t\t!sharedFileSystem && ZWAVE_APPLICATION_NVM_SIZE % pageSize !== 0\n\t\t) {\n\t\t\tthrow new ZWaveError(\n\t\t\t\t`Invalid page size. The application NVM size ${ZWAVE_APPLICATION_NVM_SIZE} must be a multiple of the page size ${pageSize}.`,\n\t\t\t\tZWaveErrorCodes.Argument_Invalid,\n\t\t\t);\n\t\t} else if (\n\t\t\t!sharedFileSystem\n\t\t\t&& (this._io.size - ZWAVE_APPLICATION_NVM_SIZE) % pageSize !== 0\n\t\t) {\n\t\t\tthrow new ZWaveError(\n\t\t\t\t`Invalid page size. The protocol NVM size ${\n\t\t\t\t\tthis._io.size\n\t\t\t\t\t- ZWAVE_APPLICATION_NVM_SIZE\n\t\t\t\t} must be a multiple of the page size ${pageSize}.`,\n\t\t\t\tZWaveErrorCodes.Argument_Invalid,\n\t\t\t);\n\t\t}\n\n\t\tawait this.ensureWritable();\n\n\t\t// Create empty pages, write them to the NVM\n\t\tconst applicationPages: NVM3PageInfo[] = [];\n\t\tconst protocolPages: NVM3PageInfo[] = [];\n\n\t\tconst numPages = this._io.size / pageSize;\n\t\tfor (let i = 0; i < numPages; i++) {\n\t\t\tconst offset = i * pageSize;\n\t\t\tconst pageBuffer = new Uint8Array(pageSize).fill(0xff);\n\t\t\tconst pageHeader: NVM3PageHeader = {\n\t\t\t\toffset,\n\t\t\t\tversion: 0x01,\n\t\t\t\teraseCount: 0,\n\t\t\t\tencrypted: false,\n\t\t\t\tdeviceFamily,\n\t\t\t\tmemoryMapped,\n\t\t\t\tpageSize,\n\t\t\t\tstatus: PageStatus.OK,\n\t\t\t\twriteSize,\n\t\t\t};\n\t\t\tpageBuffer.set(serializePageHeader(pageHeader), 0);\n\t\t\tawait nvmWriteBuffer(this._io, offset, pageBuffer);\n\n\t\t\tif (sharedFileSystem || offset < ZWAVE_APPLICATION_NVM_SIZE) {\n\t\t\t\tapplicationPages.push({ ...pageHeader, objects: [] });\n\t\t\t} else {\n\t\t\t\tprotocolPages.push({ ...pageHeader, objects: [] });\n\t\t\t}\n\t\t}\n\n\t\t// Remember the pages we just created for further use\n\t\tthis._info = sharedFileSystem\n\t\t\t? {\n\t\t\t\tisSharedFileSystem: true,\n\t\t\t\tsections: {\n\t\t\t\t\tall: {\n\t\t\t\t\t\tcurrentPage: 0,\n\t\t\t\t\t\tobjectLocations: new Map(),\n\t\t\t\t\t\toffsetInPage: NVM3_PAGE_HEADER_SIZE,\n\t\t\t\t\t\tpages: applicationPages,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t}\n\t\t\t: {\n\t\t\t\tisSharedFileSystem: false,\n\t\t\t\tsections: {\n\t\t\t\t\tapplication: {\n\t\t\t\t\t\tcurrentPage: 0,\n\t\t\t\t\t\tobjectLocations: new Map(),\n\t\t\t\t\t\toffsetInPage: NVM3_PAGE_HEADER_SIZE,\n\t\t\t\t\t\tpages: applicationPages,\n\t\t\t\t\t},\n\t\t\t\t\tprotocol: {\n\t\t\t\t\t\tcurrentPage: 0,\n\t\t\t\t\t\tobjectLocations: new Map(),\n\t\t\t\t\t\toffsetInPage: NVM3_PAGE_HEADER_SIZE,\n\t\t\t\t\t\tpages: protocolPages,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t}\n}\n\nasync function readPageHeader(\n\tio: NVMIO,\n\toffset: number,\n): Promise<NVM3PageHeader> {\n\tif (offset > io.size - NVM3_PAGE_HEADER_SIZE) {\n\t\tthrow new ZWaveError(\n\t\t\t\"Incomplete page in buffer!\",\n\t\t\tZWaveErrorCodes.NVM_InvalidFormat,\n\t\t);\n\t}\n\n\tconst buffer = Bytes.view(\n\t\t(await io.read(offset, NVM3_PAGE_HEADER_SIZE)).buffer,\n\t);\n\n\tconst { version, eraseCount } = tryGetVersionAndEraseCount(buffer);\n\n\t// Page status\n\tconst status = buffer.readUInt32LE(12);\n\n\tconst devInfo = buffer.readUInt16LE(16);\n\tconst deviceFamily = devInfo & 0x7ff;\n\tconst writeSize = (devInfo >> 11) & 0b1;\n\tconst memoryMapped = !!((devInfo >> 12) & 0b1);\n\tlet pageSize = pageSizeFromBits((devInfo >> 13) & 0b111);\n\n\tif (pageSize > 0xffff) {\n\t\t// Some controllers have no valid info in the page size bits, resulting\n\t\t// in an impossibly large page size. To try and figure out the actual page\n\t\t// size without knowing the hardware, we scan the buffer for the next valid\n\t\t// page start.\n\t\tfor (let exponent = 0; exponent < 0b111; exponent++) {\n\t\t\tconst testPageSize = pageSizeFromBits(exponent);\n\t\t\tconst nextOffset = offset + testPageSize;\n\t\t\tif (\n\t\t\t\t// exactly end of NVM OR\n\t\t\t\tio.size === nextOffset\n\t\t\t\t// next page\n\t\t\t\t|| await isValidPageHeaderAtOffset(io, nextOffset)\n\t\t\t) {\n\t\t\t\tpageSize = testPageSize;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tif (pageSize > 0xffff) {\n\t\tthrow new ZWaveError(\n\t\t\t\"Could not determine page size!\",\n\t\t\tZWaveErrorCodes.NVM_InvalidFormat,\n\t\t);\n\t}\n\n\tif (io.size < offset + pageSize) {\n\t\tthrow new ZWaveError(\n\t\t\t`NVM contains incomplete page at offset ${num2hex(offset)}!`,\n\t\t\tZWaveErrorCodes.NVM_InvalidFormat,\n\t\t);\n\t}\n\n\tconst formatInfo = buffer.readUInt16LE(18);\n\tconst encrypted = !(formatInfo & 0b1);\n\n\treturn {\n\t\toffset,\n\t\tversion,\n\t\teraseCount,\n\t\tstatus,\n\t\tencrypted,\n\t\tpageSize,\n\t\twriteSize,\n\t\tmemoryMapped,\n\t\tdeviceFamily,\n\t};\n}\n\nfunction tryGetVersionAndEraseCount(\n\theader: Uint8Array,\n): { version: number; eraseCount: number } {\n\tconst buffer = Bytes.view(header);\n\tconst version = buffer.readUInt16LE(0);\n\tconst magic = buffer.readUInt16LE(2);\n\tif (magic !== NVM3_PAGE_MAGIC) {\n\t\tthrow new ZWaveError(\n\t\t\t\"Not a valid NVM3 page!\",\n\t\t\tZWaveErrorCodes.NVM_InvalidFormat,\n\t\t);\n\t}\n\tif (version !== 0x01) {\n\t\tthrow new ZWaveError(\n\t\t\t`Unsupported NVM3 page version: ${version}`,\n\t\t\tZWaveErrorCodes.NVM_NotSupported,\n\t\t);\n\t}\n\n\t// The erase counter is saved twice, once normally, once inverted\n\tlet eraseCount = buffer.readUInt32LE(4);\n\tconst eraseCountCode = eraseCount >>> NVM3_PAGE_COUNTER_SIZE;\n\teraseCount &= NVM3_PAGE_COUNTER_MASK;\n\tvalidateBergerCode(eraseCount, eraseCountCode, NVM3_PAGE_COUNTER_SIZE);\n\n\tlet eraseCountInv = buffer.readUInt32LE(8);\n\tconst eraseCountInvCode = eraseCountInv >>> NVM3_PAGE_COUNTER_SIZE;\n\teraseCountInv &= NVM3_PAGE_COUNTER_MASK;\n\tvalidateBergerCode(\n\t\teraseCountInv,\n\t\teraseCountInvCode,\n\t\tNVM3_PAGE_COUNTER_SIZE,\n\t);\n\n\tif (eraseCount !== (~eraseCountInv & NVM3_PAGE_COUNTER_MASK)) {\n\t\tthrow new ZWaveError(\n\t\t\t\"Invalid erase count!\",\n\t\t\tZWaveErrorCodes.NVM_InvalidFormat,\n\t\t);\n\t}\n\n\treturn { version, eraseCount };\n}\n\nasync function isValidPageHeaderAtOffset(\n\tio: NVMIO,\n\toffset: number,\n): Promise<boolean> {\n\tif (offset > io.size - NVM3_PAGE_HEADER_SIZE) {\n\t\treturn false;\n\t}\n\n\tconst { buffer } = await io.read(offset, NVM3_PAGE_HEADER_SIZE);\n\n\ttry {\n\t\ttryGetVersionAndEraseCount(buffer);\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n}\n\nasync function readObjectHeader(\n\tio: NVMIO,\n\toffset: number,\n): Promise<NVM3ObjectHeader | undefined> {\n\tlet headerSize = 4;\n\tconst hdr1 = await nvmReadUInt32LE(io, offset);\n\n\t// Skip over blank page areas\n\tif (hdr1 === 0xffffffff) return;\n\n\tconst key = (hdr1 >> NVM3_OBJ_KEY_SHIFT) & NVM3_OBJ_KEY_MASK;\n\tlet objType: ObjectType = hdr1 & NVM3_OBJ_TYPE_MASK;\n\tlet fragmentSize = 0;\n\tlet hdr2: number | undefined;\n\tconst isLarge = objType === ObjectType.DataLarge\n\t\t|| objType === ObjectType.CounterLarge;\n\tif (isLarge) {\n\t\thdr2 = await nvmReadUInt32LE(io, offset + 4);\n\t\theaderSize += 4;\n\t\tfragmentSize = hdr2 & NVM3_OBJ_LARGE_LEN_MASK;\n\t} else if (objType > ObjectType.DataSmall) {\n\t\t// In small objects with data, the length and object type are stored in the same value\n\t\tfragmentSize = objType - ObjectType.DataSmall;\n\t\tobjType = ObjectType.DataSmall;\n\t} else if (objType === ObjectType.CounterSmall) {\n\t\tfragmentSize = NVM3_COUNTER_SIZE;\n\t}\n\n\tconst fragmentType: FragmentType = isLarge\n\t\t? (hdr1 >>> NVM3_OBJ_FRAGTYPE_SHIFT) & NVM3_OBJ_FRAGTYPE_MASK\n\t\t: FragmentType.None;\n\n\tif (isLarge) {\n\t\tvalidateBergerCodeMulti([hdr1, hdr2!], 32 + NVM3_CODE_LARGE_SHIFT);\n\t} else {\n\t\tvalidateBergerCodeMulti([hdr1], NVM3_CODE_SMALL_SHIFT);\n\t}\n\n\tif (io.size < offset + headerSize + fragmentSize) {\n\t\tthrow new ZWaveError(\n\t\t\t`NVM contains incomplete object at offset ${num2hex(offset)}!`,\n\t\t\tZWaveErrorCodes.NVM_InvalidFormat,\n\t\t);\n\t}\n\n\tconst alignedFragmentSize = getAlignedSize(fragmentSize);\n\tconst alignedSize = headerSize + alignedFragmentSize;\n\n\treturn {\n\t\tkey,\n\t\toffset,\n\t\ttype: objType,\n\t\tfragmentType,\n\t\theaderSize,\n\t\tfragmentSize,\n\t\talignedSize,\n\t};\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;;;;;AAAA,kBAA4C;AAC5C,oBAAkD;AAClD,yBAAgD;AAChD,mBAIO;AACP,oBAuBO;AACP,mBAIO;AACP,oBAQO;AACP,kBAIO;AACP,IAAAA,gBAA4D;AAyCtD,MAAO,KAAI;EAChB,YAAmB,IAAS;AAC3B,SAAK,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B;AAC3C,MAAI,UAAsB,OAAO;AACjC,MAAI,eAAe;AACnB,MAAI;AACJ,QAAM,UAAU,YAAY,yBAAW,aACnC,YAAY,yBAAW;AAC3B,MAAI,SAAS;AACZ,WAAO,UAAM,8BAAgB,IAAI,SAAS,CAAC;AAC3C,kBAAc;AACd,mBAAe,OAAO;EACvB,WAAW,UAAU,yBAAW,WAAW;AAE1C,mBAAe,UAAU,yBAAW;AACpC,cAAU,yBAAW;EACtB,WAAW,YAAY,yBAAW,cAAc;AAC/C,mBAAe;EAChB;AAEA,QAAM,eAA6B,UAC/B,SAAS,wCAA2B,uCACrC,2BAAa;AAEhB,MAAI,SAAS;AACZ,+CAAwB,CAAC,MAAM,IAAK,GAAG,KAAK,mCAAqB;EAClE,OAAO;AACN,+CAAwB,CAAC,IAAI,GAAG,mCAAqB;EACtD;AAEA,MAAI,GAAG,OAAO,SAAS,aAAa,cAAc;AACjD,UAAM,IAAI,uBACT,gDAA4C,uBAAQ,MAAM,CAAC,KAC3D,4BAAgB,iBAAiB;EAEnC;AAEA,QAAM,0BAAsB,8BAAe,YAAY;AACvD,QAAM,cAAc,aAAa;AAEjC,SAAO;IACN;IACA;IACA,MAAM;IACN;IACA;IACA;IACA;;AAEF;",
6
- "names": ["import_utils", "pages"]
4
+ "sourcesContent": ["import { ZWaveError, ZWaveErrorCodes } from \"@zwave-js/core/safe\";\nimport { Bytes, getEnumMemberName, num2hex } from \"@zwave-js/shared/safe\";\nimport { type NVM, NVMAccess, type NVMIO } from \"./common/definitions.js\";\nimport {\n\tnvmReadBuffer,\n\tnvmReadUInt32LE,\n\tnvmWriteBuffer,\n} from \"./common/utils.js\";\nimport {\n\tFLASH_MAX_PAGE_SIZE_700,\n\tFLASH_MAX_PAGE_SIZE_800,\n\tFragmentType,\n\tNVM3_CODE_LARGE_SHIFT,\n\tNVM3_CODE_SMALL_SHIFT,\n\tNVM3_COUNTER_SIZE,\n\tNVM3_MAX_OBJ_SIZE_SMALL,\n\tNVM3_OBJ_FRAGTYPE_MASK,\n\tNVM3_OBJ_FRAGTYPE_SHIFT,\n\tNVM3_OBJ_HEADER_SIZE_LARGE,\n\tNVM3_OBJ_KEY_MASK,\n\tNVM3_OBJ_KEY_SHIFT,\n\tNVM3_OBJ_LARGE_LEN_MASK,\n\tNVM3_OBJ_TYPE_MASK,\n\tNVM3_PAGE_COUNTER_MASK,\n\tNVM3_PAGE_COUNTER_SIZE,\n\tNVM3_PAGE_HEADER_SIZE,\n\tNVM3_PAGE_MAGIC,\n\tObjectType,\n\tPageStatus,\n\tPageWriteSize,\n\tZWAVE_APPLICATION_NVM_SIZE,\n} from \"./nvm3/consts.js\";\nimport {\n\tApplicationVersionFile800ID,\n\ttype NVMSection,\n\tgetNVMSectionByFileID,\n} from \"./nvm3/files/index.js\";\nimport {\n\ttype NVM3Object,\n\ttype NVM3ObjectHeader,\n\tfragmentLargeObject,\n\tgetAlignedSize,\n\tgetObjectHeader,\n\tgetRequiredSpace,\n\tserializeObject,\n} from \"./nvm3/object.js\";\nimport {\n\ttype NVM3PageHeader,\n\tpageSizeFromBits,\n\tserializePageHeader,\n} from \"./nvm3/page.js\";\nimport { validateBergerCode, validateBergerCodeMulti } from \"./nvm3/utils.js\";\n\n// TODO: Possible optimizations:\n// Investigate if there is a better way to determine whether the NVM\n// uses a shared FS or not. The current implementation scans all objects\n// to find the 800 series application version file.\n// Alternatively, we could simply check if each page starts with an object header.\n// If yes, read the objects lazily when needed. If not, remember that the page is empty.\n\nexport type NVM3PageInfo = NVM3PageHeader & {\n\tobjects: NVM3ObjectHeader[];\n};\n\nexport interface NVM3SectionInfo {\n\tpages: NVM3PageInfo[];\n\t/** The index of the current page */\n\tcurrentPage: number;\n\t/** The next byte to write in the current page */\n\toffsetInPage: number;\n\t/** A map of file IDs and page indizes in which their last copy resides */\n\tobjectLocations: Map<number, number>;\n}\n\nexport type NVM3FileSystemInfo = {\n\tisSharedFileSystem: true;\n\tsections: Record<\"all\", NVM3SectionInfo>;\n} | {\n\tisSharedFileSystem: false;\n\tsections: Record<NVMSection, NVM3SectionInfo>;\n};\n\nexport interface NVM3Meta {\n\tsharedFileSystem: boolean;\n\tpageSize: number;\n\tdeviceFamily: number;\n\twriteSize: PageWriteSize;\n\tmemoryMapped: boolean;\n}\n\nexport type NVM3EraseOptions = Partial<NVM3Meta>;\n\nexport class NVM3 implements NVM<number, Uint8Array> {\n\tpublic constructor(io: NVMIO) {\n\t\tthis._io = io;\n\t}\n\n\tprivate _io: NVMIO;\n\tprivate _access: NVMAccess = NVMAccess.None;\n\n\tprivate _info: NVM3FileSystemInfo | undefined;\n\tpublic get info(): NVM3FileSystemInfo | undefined {\n\t\treturn this._info;\n\t}\n\n\tprivate async ensureReadable(): Promise<void> {\n\t\tif (\n\t\t\tthis._access === NVMAccess.Read\n\t\t\t|| this._access === NVMAccess.ReadWrite\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tif (this._access === NVMAccess.Write) {\n\t\t\tawait this._io.close();\n\t\t}\n\t\tthis._access = await this._io.open(NVMAccess.Read);\n\t}\n\n\tprivate async ensureWritable(): Promise<void> {\n\t\tif (\n\t\t\tthis._access === NVMAccess.Write\n\t\t\t|| this._access === NVMAccess.ReadWrite\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t\tif (this._access === NVMAccess.Read) {\n\t\t\tawait this._io.close();\n\t\t}\n\t\tthis._access = await this._io.open(NVMAccess.Write);\n\t}\n\n\tpublic async init(): Promise<NVM3FileSystemInfo> {\n\t\tawait this.ensureReadable();\n\n\t\tlet pageOffset = 0;\n\t\t// Determine NVM size, scan pages\n\t\tconst pages: NVM3PageInfo[] = [];\n\t\tlet isSharedFileSystem = false;\n\t\twhile (pageOffset < this._io.size) {\n\t\t\t// console.debug(\n\t\t\t// \t`NVM3 init() - reading page header at offset ${\n\t\t\t// \t\tnum2hex(pageOffset)\n\t\t\t// \t}`,\n\t\t\t// );\n\t\t\tconst header = await readPageHeader(this._io, pageOffset);\n\t\t\tpages.push({\n\t\t\t\t...header,\n\t\t\t\tobjects: [],\n\t\t\t});\n\t\t\tpageOffset += header.pageSize;\n\t\t}\n\n\t\t// Scan each page for objects\n\t\tfor (const page of pages) {\n\t\t\t// Scan objects in this page\n\t\t\tlet objectOffset = page.offset + NVM3_PAGE_HEADER_SIZE;\n\t\t\tconst nextPageOffset = page.offset + page.pageSize;\n\t\t\twhile (objectOffset < nextPageOffset) {\n\t\t\t\t// console.debug(\n\t\t\t\t// \t`NVM3 init() - reading object header. page offset ${\n\t\t\t\t// \t\tnum2hex(page.offset)\n\t\t\t\t// \t}, object offset ${num2hex(objectOffset)}`,\n\t\t\t\t// );\n\t\t\t\tconst objectHeader = await readObjectHeader(\n\t\t\t\t\tthis._io,\n\t\t\t\t\tobjectOffset,\n\t\t\t\t);\n\t\t\t\tif (objectHeader) {\n\t\t\t\t\tpage.objects.push(objectHeader);\n\t\t\t\t\tobjectOffset += objectHeader.alignedSize;\n\n\t\t\t\t\t// Detect the 800 series shared protocol & application NVM file system\n\t\t\t\t\t// by looking for the 800 series application version file\n\t\t\t\t\tif (objectHeader.key === ApplicationVersionFile800ID) {\n\t\t\t\t\t\tisSharedFileSystem = true;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\t// Reached the end of the data in this page\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// By convention, we only use the applicationPages in that case\n\t\tlet applicationPages: NVM3PageInfo[];\n\t\tlet protocolPages: NVM3PageInfo[];\n\n\t\tif (isSharedFileSystem) {\n\t\t\tapplicationPages = pages;\n\t\t\tprotocolPages = [];\n\t\t} else {\n\t\t\tapplicationPages = pages.filter(\n\t\t\t\t(p) => p.offset < ZWAVE_APPLICATION_NVM_SIZE,\n\t\t\t);\n\t\t\tprotocolPages = pages.filter(\n\t\t\t\t(p) => p.offset >= ZWAVE_APPLICATION_NVM_SIZE,\n\t\t\t);\n\t\t}\n\n\t\t// NVM3 layouts pages in a ring buffer. Pages are written from front to back, then occupied pages\n\t\t// are erased and overwritten. Pages at the start of the memory section may have an erase count that's 1 higher\n\t\t// than the pages at the end.\n\t\tconst pageInfoToSectionInfo = (\n\t\t\tpages: NVM3PageInfo[],\n\t\t): NVM3SectionInfo => {\n\t\t\t// Find the current page, which is either:\n\t\t\t// - The last page with the high erase count that contains an object\n\t\t\tconst maxEraseCount = Math.max(...pages.map((p) => p.eraseCount));\n\t\t\tlet currentPageIndex = pages.findLastIndex((p) =>\n\t\t\t\tp.eraseCount === maxEraseCount && p.objects.length > 0\n\t\t\t);\n\t\t\t// - or if there is none, the last page with the lower erase count that contains an object\n\t\t\tif (currentPageIndex === -1) {\n\t\t\t\tcurrentPageIndex = pages.findLastIndex((p) =>\n\t\t\t\t\tp.objects.length > 0\n\t\t\t\t);\n\t\t\t}\n\t\t\t// - Or if no objects exist at all, the beginning of the section\n\t\t\tif (currentPageIndex === -1) currentPageIndex = 0;\n\n\t\t\t// Find the next free byte of the current page\n\t\t\tconst currentPage = pages[currentPageIndex];\n\t\t\tlet offset = NVM3_PAGE_HEADER_SIZE;\n\t\t\tfor (const object of currentPage.objects) {\n\t\t\t\toffset += object.alignedSize;\n\t\t\t}\n\n\t\t\tconst objectLocations = new Map<number, number>();\n\t\t\tfor (let i = 0; i < pages.length; i++) {\n\t\t\t\tconst page = pages[i];\n\t\t\t\tfor (const object of page.objects) {\n\t\t\t\t\tconst location = objectLocations.get(object.key);\n\t\t\t\t\tif (location == undefined) {\n\t\t\t\t\t\t// Object seen for the first time, remember the page it is in\n\t\t\t\t\t\tobjectLocations.set(object.key, i);\n\t\t\t\t\t} else if (\n\t\t\t\t\t\t(object.fragmentType === FragmentType.None\n\t\t\t\t\t\t\t|| object.fragmentType === FragmentType.First)\n\t\t\t\t\t\t&& (page.eraseCount >= pages[location].eraseCount)\n\t\t\t\t\t) {\n\t\t\t\t\t\t// Object was seen before. Only remember it if it is the only\n\t\t\t\t\t\t// or first fragment and the object appears in a later location\n\t\t\t\t\t\t// of the ring buffer\n\t\t\t\t\t\tobjectLocations.set(object.key, i);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tpages,\n\t\t\t\toffsetInPage: offset,\n\t\t\t\tcurrentPage: currentPageIndex,\n\t\t\t\tobjectLocations,\n\t\t\t};\n\t\t};\n\n\t\tif (isSharedFileSystem) {\n\t\t\tthis._info = {\n\t\t\t\tisSharedFileSystem: true,\n\t\t\t\tsections: {\n\t\t\t\t\tall: pageInfoToSectionInfo(applicationPages),\n\t\t\t\t},\n\t\t\t};\n\t\t} else {\n\t\t\tthis._info = {\n\t\t\t\tisSharedFileSystem: false,\n\t\t\t\tsections: {\n\t\t\t\t\tapplication: pageInfoToSectionInfo(applicationPages),\n\t\t\t\t\tprotocol: pageInfoToSectionInfo(protocolPages),\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\treturn this._info;\n\t}\n\n\tprivate getNVMSectionForFile(fileId: number): NVM3SectionInfo {\n\t\t// Determine which ring buffer to read in\n\t\treturn this._info!.isSharedFileSystem\n\t\t\t? this._info!.sections.all\n\t\t\t: this._info!.sections[getNVMSectionByFileID(fileId)];\n\t}\n\n\tpublic async has(fileId: number): Promise<boolean> {\n\t\tthis._info ??= await this.init();\n\n\t\t// Determine which ring buffer to read in\n\t\tconst section = this.getNVMSectionForFile(fileId);\n\n\t\treturn section.objectLocations.has(fileId);\n\t}\n\n\tpublic readObjectData(object: NVM3ObjectHeader): Promise<Uint8Array> {\n\t\treturn nvmReadBuffer(\n\t\t\tthis._io,\n\t\t\tobject.offset + object.headerSize,\n\t\t\tobject.fragmentSize,\n\t\t);\n\t}\n\n\tpublic async get(fileId: number): Promise<Uint8Array | undefined> {\n\t\tthis._info ??= await this.init();\n\n\t\t// Determine which ring buffer to read in\n\t\tconst section = this.getNVMSectionForFile(fileId);\n\n\t\tconst pages = section.pages;\n\n\t\t// TODO: There should be no need for scanning, since we know the object locations after init().\n\n\t\t// Start scanning backwards through the pages ring buffer, starting with the current page\n\t\tlet parts: Uint8Array[] | undefined;\n\t\tlet complete = false;\n\t\tlet objType: ObjectType | undefined;\n\t\tconst resetFragments = () => {\n\t\t\t// if (parts?.length) {\n\t\t\t// \tconsole.debug(\"Resetting fragmented object\");\n\t\t\t// }\n\t\t\tparts = undefined;\n\t\t\tcomplete = false;\n\t\t};\n\t\tpages: for (let offset = 0; offset < pages.length; offset++) {\n\t\t\tconst index = (section.currentPage - offset + pages.length)\n\t\t\t\t% pages.length;\n\t\t\tconst page = pages[index];\n\t\t\t// console.debug(\n\t\t\t// \t`NVM3.get(${fileId}): scanning page ${index} at offset ${\n\t\t\t// \t\tnum2hex(page.offset)\n\t\t\t// \t}`,\n\t\t\t// );\n\t\t\t// Scan objects in this page, read backwards.\n\t\t\t// The last non-deleted object wins\n\t\t\tobjects: for (let j = page.objects.length - 1; j >= 0; j--) {\n\t\t\t\tconst object = page.objects[j];\n\n\t\t\t\tconst readObject = () => this.readObjectData(object);\n\n\t\t\t\tif (object.key !== fileId) {\n\t\t\t\t\t// Reset any fragmented objects when encountering a different key\n\t\t\t\t\tresetFragments();\n\t\t\t\t\tcontinue objects;\n\t\t\t\t}\n\n\t\t\t\tif (object.type === ObjectType.Deleted) {\n\t\t\t\t\t// Last action for this object was a deletion. There is no data.\n\t\t\t\t\treturn;\n\t\t\t\t} else if (object.fragmentType === FragmentType.None) {\n\t\t\t\t\t// console.debug(\n\t\t\t\t\t// \t`NVM3.get(${fileId}): found complete object - header offset ${\n\t\t\t\t\t// \t\tnum2hex(object.offset)\n\t\t\t\t\t// \t}, content offset ${\n\t\t\t\t\t// \t\tnum2hex(object.offset + object.headerSize)\n\t\t\t\t\t// \t}, length ${object.fragmentSize}`,\n\t\t\t\t\t// );\n\t\t\t\t\t// This is a complete object\n\t\t\t\t\tparts = [await readObject()];\n\t\t\t\t\tobjType = object.type;\n\t\t\t\t\tcomplete = true;\n\t\t\t\t\tbreak pages;\n\t\t\t\t} else if (object.fragmentType === FragmentType.Last) {\n\t\t\t\t\t// console.debug(\n\t\t\t\t\t// \t`NVM3.get(${fileId}): found LAST fragment - header offset ${\n\t\t\t\t\t// \t\tnum2hex(object.offset)\n\t\t\t\t\t// \t}, content offset ${\n\t\t\t\t\t// \t\tnum2hex(object.offset + object.headerSize)\n\t\t\t\t\t// \t}, length ${object.fragmentSize}`,\n\t\t\t\t\t// );\n\t\t\t\t\tparts = [await readObject()];\n\t\t\t\t\tobjType = object.type;\n\t\t\t\t\tcomplete = false;\n\t\t\t\t} else if (object.fragmentType === FragmentType.Next) {\n\t\t\t\t\tif (parts?.length && objType === object.type) {\n\t\t\t\t\t\t// console.debug(\n\t\t\t\t\t\t// \t`NVM3.get(${fileId}): found NEXT fragment - header offset ${\n\t\t\t\t\t\t// \t\tnum2hex(object.offset)\n\t\t\t\t\t\t// \t}, content offset ${\n\t\t\t\t\t\t// \t\tnum2hex(object.offset + object.headerSize)\n\t\t\t\t\t\t// \t}, length ${object.fragmentSize}`,\n\t\t\t\t\t\t// );\n\t\t\t\t\t\tparts.unshift(await readObject());\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// This shouldn't be here\n\t\t\t\t\t\tresetFragments();\n\t\t\t\t\t}\n\t\t\t\t} else if (object.fragmentType === FragmentType.First) {\n\t\t\t\t\tif (parts?.length && objType === object.type) {\n\t\t\t\t\t\t// console.debug(\n\t\t\t\t\t\t// \t`NVM3.get(${fileId}): found FIRST fragment - header offset ${\n\t\t\t\t\t\t// \t\tnum2hex(object.offset)\n\t\t\t\t\t\t// \t}, content offset ${\n\t\t\t\t\t\t// \t\tnum2hex(object.offset + object.headerSize)\n\t\t\t\t\t\t// \t}, length ${object.fragmentSize}`,\n\t\t\t\t\t\t// );\n\t\t\t\t\t\tparts.unshift(await readObject());\n\t\t\t\t\t\tcomplete = true;\n\t\t\t\t\t\tbreak pages;\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// This shouldn't be here\n\t\t\t\t\t\tresetFragments();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (!parts?.length || !complete || objType == undefined) return;\n\n\t\treturn Bytes.concat(parts);\n\t}\n\n\tprivate async writeObjects(objects: NVM3Object[]): Promise<void> {\n\t\tconst section = this.getNVMSectionForFile(objects[0].key);\n\n\t\tlet page = section.pages[section.currentPage];\n\t\tlet remainingSpace = page.pageSize\n\t\t\t- NVM3_PAGE_HEADER_SIZE\n\t\t\t- section.offsetInPage;\n\n\t\t// TODO: See if we can avoid double writes on a page change\n\n\t\t/** Moves to the next page and erases it if necessary */\n\t\tconst nextPage = async () => {\n\t\t\tsection.currentPage = (section.currentPage + 1)\n\t\t\t\t% section.pages.length;\n\t\t\tpage = section.pages[section.currentPage];\n\n\t\t\t// Find headers of objects that need to be preserved\n\t\t\tconst toPreserve = [...section.objectLocations].filter((\n\t\t\t\t[, pageIndex],\n\t\t\t) => pageIndex === section.currentPage)\n\t\t\t\t.map(([fileID]) =>\n\t\t\t\t\tpage.objects.findLast((h) => h.key === fileID)\n\t\t\t\t)\n\t\t\t\t.filter((h) => h != undefined)\n\t\t\t\t.filter((h) => h.type !== ObjectType.Deleted);\n\t\t\t// And add the objects to the TODO list\n\t\t\tfor (const header of toPreserve) {\n\t\t\t\tconst data = await this.get(header.key);\n\t\t\t\tconsole.error(`Need to preserve object ${num2hex(header.key)}\n page index: ${section.currentPage}\n object type: ${getEnumMemberName(ObjectType, header.type)}\n data: ${data != undefined ? `${data.length} bytes` : \"(no data)\"}`);\n\t\t\t\tobjects.push({\n\t\t\t\t\tkey: header.key,\n\t\t\t\t\ttype: header.type,\n\t\t\t\t\tfragmentType: FragmentType.None,\n\t\t\t\t\tdata,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tif (page.objects.length > 0) {\n\t\t\t\t// The page needs to be erased\n\t\t\t\tpage.eraseCount++;\n\t\t\t\tpage.objects = [];\n\n\t\t\t\tconst pageHeaderBuffer = serializePageHeader(page);\n\t\t\t\tconst pageBuffer = new Uint8Array(page.pageSize).fill(0xff);\n\t\t\t\tpageBuffer.set(pageHeaderBuffer, 0);\n\n\t\t\t\tawait nvmWriteBuffer(this._io, page.offset, pageBuffer);\n\t\t\t}\n\n\t\t\tsection.offsetInPage = NVM3_PAGE_HEADER_SIZE;\n\t\t\tremainingSpace = page.pageSize - NVM3_PAGE_HEADER_SIZE;\n\t\t};\n\n\t\t// Go through the list of objects and write all of them to the NVM\n\t\tfor (const object of objects) {\n\t\t\tconst isLargeObject = object.type === ObjectType.DataLarge\n\t\t\t\t|| object.type === ObjectType.CounterLarge;\n\n\t\t\tlet fragments: NVM3Object[] | undefined;\n\n\t\t\tif (isLargeObject) {\n\t\t\t\t// Large objects may be fragmented\n\n\t\t\t\t// We need to start a new page, if the remaining space is not enough for\n\t\t\t\t// the object header plus additional data\n\t\t\t\tif (remainingSpace <= NVM3_OBJ_HEADER_SIZE_LARGE) {\n\t\t\t\t\tawait nextPage();\n\t\t\t\t}\n\n\t\t\t\tfragments = fragmentLargeObject(\n\t\t\t\t\tobject as any,\n\t\t\t\t\tremainingSpace,\n\t\t\t\t\tpage.pageSize - NVM3_PAGE_HEADER_SIZE,\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\t// Small objects cannot be fragmented. If they don't fit,\n\t\t\t\t// they need to go on the next page.\n\t\t\t\tconst requiredSpace = getRequiredSpace(object);\n\t\t\t\tif (requiredSpace > remainingSpace) {\n\t\t\t\t\tawait nextPage();\n\t\t\t\t}\n\t\t\t\tfragments = [object];\n\t\t\t}\n\n\t\t\t// Write each fragment to the NVM. If there are multiple fragments,\n\t\t\t// each one but the first needs to be written at the beginning of a new page\n\t\t\tfor (let i = 0; i < fragments.length; i++) {\n\t\t\t\tif (i > 0) await nextPage();\n\t\t\t\tconst fragment = fragments[i];\n\n\t\t\t\tconst objBuffer = serializeObject(fragment);\n\t\t\t\tconst objOffset = page.offset + section.offsetInPage;\n\t\t\t\tawait this._io.write(objOffset, objBuffer);\n\t\t\t\tconst requiredSpace = getRequiredSpace(fragment);\n\t\t\t\tsection.offsetInPage += requiredSpace;\n\t\t\t\tremainingSpace -= requiredSpace;\n\n\t\t\t\t// Remember which objects exist in this page\n\t\t\t\tpage.objects.push(getObjectHeader(object, objOffset));\n\n\t\t\t\t// And remember where this object lives\n\t\t\t\tif (object.type === ObjectType.Deleted) {\n\t\t\t\t\tsection.objectLocations.delete(object.key);\n\t\t\t\t} else if (\n\t\t\t\t\tfragment.fragmentType === FragmentType.None\n\t\t\t\t\t|| fragment.fragmentType === FragmentType.First\n\t\t\t\t) {\n\t\t\t\t\tsection.objectLocations.set(\n\t\t\t\t\t\tfragment.key,\n\t\t\t\t\t\tsection.currentPage,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic async set(property: number, value: Uint8Array): Promise<void> {\n\t\tif (!this._info) await this.init();\n\t\tawait this.ensureWritable();\n\n\t\tawait this.writeObjects([{\n\t\t\tkey: property,\n\t\t\ttype: value.length <= NVM3_MAX_OBJ_SIZE_SMALL\n\t\t\t\t? ObjectType.DataSmall\n\t\t\t\t: ObjectType.DataLarge,\n\t\t\t// writeObject deals with fragmentation\n\t\t\tfragmentType: FragmentType.None,\n\t\t\tdata: value,\n\t\t}]);\n\t}\n\n\t/** Writes multiple values to the NVM at once. `null` / `undefined` cause the value to be deleted */\n\tpublic async setMany(\n\t\tvalues: [number, Uint8Array | null | undefined][],\n\t): Promise<void> {\n\t\tif (!this._info) await this.init();\n\t\tawait this.ensureWritable();\n\n\t\t// Group objects by their NVM section\n\t\tconst objectsBySection = new Map<\n\t\t\tnumber, /* offset */\n\t\t\t[number, Uint8Array | null | undefined][]\n\t\t>();\n\t\tfor (const [key, value] of values) {\n\t\t\tconst sectionOffset =\n\t\t\t\tthis.getNVMSectionForFile(key).pages[0].offset;\n\t\t\tif (!objectsBySection.has(sectionOffset)) {\n\t\t\t\tobjectsBySection.set(sectionOffset, []);\n\t\t\t}\n\t\t\tobjectsBySection.get(sectionOffset)!.push([key, value]);\n\t\t}\n\n\t\t// And call writeObjects for each group\n\t\tfor (const objectGroups of objectsBySection.values()) {\n\t\t\tawait this.writeObjects(\n\t\t\t\tobjectGroups.map(([key, value]) => (value\n\t\t\t\t\t? {\n\t\t\t\t\t\tkey,\n\t\t\t\t\t\ttype: value.length <= NVM3_MAX_OBJ_SIZE_SMALL\n\t\t\t\t\t\t\t? ObjectType.DataSmall\n\t\t\t\t\t\t\t: ObjectType.DataLarge,\n\t\t\t\t\t\t// writeObject deals with fragmentation\n\t\t\t\t\t\tfragmentType: FragmentType.None,\n\t\t\t\t\t\tdata: value,\n\t\t\t\t\t}\n\t\t\t\t\t: {\n\t\t\t\t\t\tkey,\n\t\t\t\t\t\ttype: ObjectType.Deleted,\n\t\t\t\t\t\tfragmentType: FragmentType.None,\n\t\t\t\t\t})\n\t\t\t\t),\n\t\t\t);\n\t\t}\n\t}\n\n\tpublic async delete(property: number): Promise<void> {\n\t\tif (!this._info) await this.init();\n\t\tawait this.ensureWritable();\n\n\t\tawait this.writeObjects([{\n\t\t\tkey: property,\n\t\t\ttype: ObjectType.Deleted,\n\t\t\tfragmentType: FragmentType.None,\n\t\t}]);\n\t}\n\n\tpublic async erase(options?: NVM3EraseOptions): Promise<void> {\n\t\tconst {\n\t\t\tdeviceFamily = 2047,\n\t\t\twriteSize = PageWriteSize.WRITE_SIZE_16,\n\t\t\tmemoryMapped = true,\n\t\t\tsharedFileSystem = false,\n\t\t} = options ?? {};\n\t\tconst maxPageSize = sharedFileSystem\n\t\t\t? FLASH_MAX_PAGE_SIZE_800\n\t\t\t: FLASH_MAX_PAGE_SIZE_700;\n\t\tconst pageSize = Math.min(\n\t\t\toptions?.pageSize ?? maxPageSize,\n\t\t\tmaxPageSize,\n\t\t);\n\n\t\t// Make sure we won't be writing incomplete pages\n\t\tif (this._io.size % pageSize !== 0) {\n\t\t\tthrow new ZWaveError(\n\t\t\t\t`Invalid page size. NVM size ${this._io.size} must be a multiple of the page size ${pageSize}.`,\n\t\t\t\tZWaveErrorCodes.Argument_Invalid,\n\t\t\t);\n\t\t} else if (\n\t\t\t!sharedFileSystem && ZWAVE_APPLICATION_NVM_SIZE % pageSize !== 0\n\t\t) {\n\t\t\tthrow new ZWaveError(\n\t\t\t\t`Invalid page size. The application NVM size ${ZWAVE_APPLICATION_NVM_SIZE} must be a multiple of the page size ${pageSize}.`,\n\t\t\t\tZWaveErrorCodes.Argument_Invalid,\n\t\t\t);\n\t\t} else if (\n\t\t\t!sharedFileSystem\n\t\t\t&& (this._io.size - ZWAVE_APPLICATION_NVM_SIZE) % pageSize !== 0\n\t\t) {\n\t\t\tthrow new ZWaveError(\n\t\t\t\t`Invalid page size. The protocol NVM size ${\n\t\t\t\t\tthis._io.size\n\t\t\t\t\t- ZWAVE_APPLICATION_NVM_SIZE\n\t\t\t\t} must be a multiple of the page size ${pageSize}.`,\n\t\t\t\tZWaveErrorCodes.Argument_Invalid,\n\t\t\t);\n\t\t}\n\n\t\tawait this.ensureWritable();\n\n\t\t// Create empty pages, write them to the NVM\n\t\tconst applicationPages: NVM3PageInfo[] = [];\n\t\tconst protocolPages: NVM3PageInfo[] = [];\n\n\t\tconst numPages = this._io.size / pageSize;\n\t\tfor (let i = 0; i < numPages; i++) {\n\t\t\tconst offset = i * pageSize;\n\t\t\tconst pageBuffer = new Uint8Array(pageSize).fill(0xff);\n\t\t\tconst pageHeader: NVM3PageHeader = {\n\t\t\t\toffset,\n\t\t\t\tversion: 0x01,\n\t\t\t\teraseCount: 0,\n\t\t\t\tencrypted: false,\n\t\t\t\tdeviceFamily,\n\t\t\t\tmemoryMapped,\n\t\t\t\tpageSize,\n\t\t\t\tstatus: PageStatus.OK,\n\t\t\t\twriteSize,\n\t\t\t};\n\t\t\tpageBuffer.set(serializePageHeader(pageHeader), 0);\n\t\t\tawait nvmWriteBuffer(this._io, offset, pageBuffer);\n\n\t\t\tif (sharedFileSystem || offset < ZWAVE_APPLICATION_NVM_SIZE) {\n\t\t\t\tapplicationPages.push({ ...pageHeader, objects: [] });\n\t\t\t} else {\n\t\t\t\tprotocolPages.push({ ...pageHeader, objects: [] });\n\t\t\t}\n\t\t}\n\n\t\t// Remember the pages we just created for further use\n\t\tthis._info = sharedFileSystem\n\t\t\t? {\n\t\t\t\tisSharedFileSystem: true,\n\t\t\t\tsections: {\n\t\t\t\t\tall: {\n\t\t\t\t\t\tcurrentPage: 0,\n\t\t\t\t\t\tobjectLocations: new Map(),\n\t\t\t\t\t\toffsetInPage: NVM3_PAGE_HEADER_SIZE,\n\t\t\t\t\t\tpages: applicationPages,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t}\n\t\t\t: {\n\t\t\t\tisSharedFileSystem: false,\n\t\t\t\tsections: {\n\t\t\t\t\tapplication: {\n\t\t\t\t\t\tcurrentPage: 0,\n\t\t\t\t\t\tobjectLocations: new Map(),\n\t\t\t\t\t\toffsetInPage: NVM3_PAGE_HEADER_SIZE,\n\t\t\t\t\t\tpages: applicationPages,\n\t\t\t\t\t},\n\t\t\t\t\tprotocol: {\n\t\t\t\t\t\tcurrentPage: 0,\n\t\t\t\t\t\tobjectLocations: new Map(),\n\t\t\t\t\t\toffsetInPage: NVM3_PAGE_HEADER_SIZE,\n\t\t\t\t\t\tpages: protocolPages,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t}\n}\n\nasync function readPageHeader(\n\tio: NVMIO,\n\toffset: number,\n): Promise<NVM3PageHeader> {\n\tif (offset > io.size - NVM3_PAGE_HEADER_SIZE) {\n\t\tthrow new ZWaveError(\n\t\t\t\"Incomplete page in buffer!\",\n\t\t\tZWaveErrorCodes.NVM_InvalidFormat,\n\t\t);\n\t}\n\n\tconst buffer = Bytes.view(\n\t\t(await io.read(offset, NVM3_PAGE_HEADER_SIZE)).buffer,\n\t);\n\n\tconst { version, eraseCount } = tryGetVersionAndEraseCount(buffer);\n\n\t// Page status\n\tconst status = buffer.readUInt32LE(12);\n\n\tconst devInfo = buffer.readUInt16LE(16);\n\tconst deviceFamily = devInfo & 0x7ff;\n\tconst writeSize = (devInfo >> 11) & 0b1;\n\tconst memoryMapped = !!((devInfo >> 12) & 0b1);\n\tlet pageSize = pageSizeFromBits((devInfo >> 13) & 0b111);\n\n\tif (pageSize > 0xffff) {\n\t\t// Some controllers have no valid info in the page size bits, resulting\n\t\t// in an impossibly large page size. To try and figure out the actual page\n\t\t// size without knowing the hardware, we scan the buffer for the next valid\n\t\t// page start.\n\t\tfor (let exponent = 0; exponent < 0b111; exponent++) {\n\t\t\tconst testPageSize = pageSizeFromBits(exponent);\n\t\t\tconst nextOffset = offset + testPageSize;\n\t\t\tif (\n\t\t\t\t// exactly end of NVM OR\n\t\t\t\tio.size === nextOffset\n\t\t\t\t// next page\n\t\t\t\t|| await isValidPageHeaderAtOffset(io, nextOffset)\n\t\t\t) {\n\t\t\t\tpageSize = testPageSize;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\tif (pageSize > 0xffff) {\n\t\tthrow new ZWaveError(\n\t\t\t\"Could not determine page size!\",\n\t\t\tZWaveErrorCodes.NVM_InvalidFormat,\n\t\t);\n\t}\n\n\tif (io.size < offset + pageSize) {\n\t\tthrow new ZWaveError(\n\t\t\t`NVM contains incomplete page at offset ${num2hex(offset)}!`,\n\t\t\tZWaveErrorCodes.NVM_InvalidFormat,\n\t\t);\n\t}\n\n\tconst formatInfo = buffer.readUInt16LE(18);\n\tconst encrypted = !(formatInfo & 0b1);\n\n\treturn {\n\t\toffset,\n\t\tversion,\n\t\teraseCount,\n\t\tstatus,\n\t\tencrypted,\n\t\tpageSize,\n\t\twriteSize,\n\t\tmemoryMapped,\n\t\tdeviceFamily,\n\t};\n}\n\nfunction tryGetVersionAndEraseCount(\n\theader: Uint8Array,\n): { version: number; eraseCount: number } {\n\tconst buffer = Bytes.view(header);\n\tconst version = buffer.readUInt16LE(0);\n\tconst magic = buffer.readUInt16LE(2);\n\tif (magic !== NVM3_PAGE_MAGIC) {\n\t\tthrow new ZWaveError(\n\t\t\t\"Not a valid NVM3 page!\",\n\t\t\tZWaveErrorCodes.NVM_InvalidFormat,\n\t\t);\n\t}\n\tif (version !== 0x01) {\n\t\tthrow new ZWaveError(\n\t\t\t`Unsupported NVM3 page version: ${version}`,\n\t\t\tZWaveErrorCodes.NVM_NotSupported,\n\t\t);\n\t}\n\n\t// The erase counter is saved twice, once normally, once inverted\n\tlet eraseCount = buffer.readUInt32LE(4);\n\tconst eraseCountCode = eraseCount >>> NVM3_PAGE_COUNTER_SIZE;\n\teraseCount &= NVM3_PAGE_COUNTER_MASK;\n\tvalidateBergerCode(eraseCount, eraseCountCode, NVM3_PAGE_COUNTER_SIZE);\n\n\tlet eraseCountInv = buffer.readUInt32LE(8);\n\tconst eraseCountInvCode = eraseCountInv >>> NVM3_PAGE_COUNTER_SIZE;\n\teraseCountInv &= NVM3_PAGE_COUNTER_MASK;\n\tvalidateBergerCode(\n\t\teraseCountInv,\n\t\teraseCountInvCode,\n\t\tNVM3_PAGE_COUNTER_SIZE,\n\t);\n\n\tif (eraseCount !== (~eraseCountInv & NVM3_PAGE_COUNTER_MASK)) {\n\t\tthrow new ZWaveError(\n\t\t\t\"Invalid erase count!\",\n\t\t\tZWaveErrorCodes.NVM_InvalidFormat,\n\t\t);\n\t}\n\n\treturn { version, eraseCount };\n}\n\nasync function isValidPageHeaderAtOffset(\n\tio: NVMIO,\n\toffset: number,\n): Promise<boolean> {\n\tif (offset > io.size - NVM3_PAGE_HEADER_SIZE) {\n\t\treturn false;\n\t}\n\n\tconst { buffer } = await io.read(offset, NVM3_PAGE_HEADER_SIZE);\n\n\ttry {\n\t\ttryGetVersionAndEraseCount(buffer);\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n}\n\nasync function readObjectHeader(\n\tio: NVMIO,\n\toffset: number,\n): Promise<NVM3ObjectHeader | undefined> {\n\tlet headerSize = 4;\n\tconst hdr1 = await nvmReadUInt32LE(io, offset);\n\n\t// Skip over blank page areas\n\tif (hdr1 === 0xffffffff) return;\n\n\tconst key = (hdr1 >> NVM3_OBJ_KEY_SHIFT) & NVM3_OBJ_KEY_MASK;\n\tlet objType: ObjectType = hdr1 & NVM3_OBJ_TYPE_MASK;\n\tlet fragmentSize = 0;\n\tlet hdr2: number | undefined;\n\tconst isLarge = objType === ObjectType.DataLarge\n\t\t|| objType === ObjectType.CounterLarge;\n\tif (isLarge) {\n\t\thdr2 = await nvmReadUInt32LE(io, offset + 4);\n\t\theaderSize += 4;\n\t\tfragmentSize = hdr2 & NVM3_OBJ_LARGE_LEN_MASK;\n\t} else if (objType > ObjectType.DataSmall) {\n\t\t// In small objects with data, the length and object type are stored in the same value\n\t\tfragmentSize = objType - ObjectType.DataSmall;\n\t\tobjType = ObjectType.DataSmall;\n\t} else if (objType === ObjectType.CounterSmall) {\n\t\tfragmentSize = NVM3_COUNTER_SIZE;\n\t}\n\n\tconst fragmentType: FragmentType = isLarge\n\t\t? (hdr1 >>> NVM3_OBJ_FRAGTYPE_SHIFT) & NVM3_OBJ_FRAGTYPE_MASK\n\t\t: FragmentType.None;\n\n\tif (isLarge) {\n\t\tvalidateBergerCodeMulti([hdr1, hdr2!], 32 + NVM3_CODE_LARGE_SHIFT);\n\t} else {\n\t\tvalidateBergerCodeMulti([hdr1], NVM3_CODE_SMALL_SHIFT);\n\t}\n\n\tif (io.size < offset + headerSize + fragmentSize) {\n\t\tthrow new ZWaveError(\n\t\t\t`NVM contains incomplete object at offset ${num2hex(offset)}!`,\n\t\t\tZWaveErrorCodes.NVM_InvalidFormat,\n\t\t);\n\t}\n\n\tconst alignedFragmentSize = getAlignedSize(fragmentSize);\n\tconst alignedSize = headerSize + alignedFragmentSize;\n\n\treturn {\n\t\tkey,\n\t\toffset,\n\t\ttype: objType,\n\t\tfragmentType,\n\t\theaderSize,\n\t\tfragmentSize,\n\t\talignedSize,\n\t};\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;;;;;AAAA,kBAA4C;AAC5C,IAAAA,eAAkD;AAClD,yBAAgD;AAChD,m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A,QAAc;AAEd,MAAI,aAAa;AACjB,QAAM,OAAO,UAAM,8BAAgB,IAAI,MAAM;AAG7C,MAAI,SAAS;AAAY;AAEzB,QAAM,MAAO,QAAQ,mCAAsB;AAC3C,MAAI,UAAsB,OAAO;AACjC,MAAI,eAAe;AACnB,MAAI;AACJ,QAAM,UAAU,YAAY,yBAAW,aACnC,YAAY,yBAAW;AAC3B,MAAI,SAAS;AACZ,WAAO,UAAM,8BAAgB,IAAI,SAAS,CAAC;AAC3C,kBAAc;AACd,mBAAe,OAAO;EACvB,WAAW,UAAU,yBAAW,WAAW;AAE1C,mBAAe,UAAU,yBAAW;AACpC,cAAU,yBAAW;EACtB,WAAW,YAAY,yBAAW,cAAc;AAC/C,mBAAe;EAChB;AAEA,QAAM,eAA6B,UAC/B,SAAS,wCAA2B,uCACrC,2BAAa;AAEhB,MAAI,SAAS;AACZ,+CAAwB,CAAC,MAAM,IAAK,GAAG,KAAK,mCAAqB;EAClE,OAAO;AACN,+CAAwB,CAAC,IAAI,GAAG,mCAAqB;EACtD;AAEA,MAAI,GAAG,OAAO,SAAS,aAAa,cAAc;AACjD,UAAM,IAAI,uBACT,gDAA4C,sBAAQ,MAAM,CAAC,KAC3D,4BAAgB,iBAAiB;EAEnC;AAEA,QAAM,0BAAsB,8BAAe,YAAY;AACvD,QAAM,cAAc,aAAa;AAEjC,SAAO;IACN;IACA;IACA,MAAM;IACN;IACA;IACA;IACA;;AAEF;",
6
+ "names": ["import_safe", "import_utils", "pages"]
7
7
  }
@@ -21,9 +21,9 @@ __export(adapter_exports, {
21
21
  NVM3Adapter: () => NVM3Adapter
22
22
  });
23
23
  module.exports = __toCommonJS(adapter_exports);
24
- var import_core = require("@zwave-js/core");
25
- var import_shared = require("@zwave-js/shared");
26
- var import_helpers = require("alcalzone-shared/helpers/index.js");
24
+ var import_safe = require("@zwave-js/core/safe");
25
+ var import_safe2 = require("@zwave-js/shared/safe");
26
+ var import_helpers = require("alcalzone-shared/helpers");
27
27
  var import_consts = require("../../consts.js");
28
28
  var import_files = require("./files/index.js");
29
29
  const DEFAULT_FILE_VERSION = "7.0.0";
@@ -105,7 +105,7 @@ class NVM3Adapter {
105
105
  async _expectFile(fileId, skipInit = false) {
106
106
  const file = await this._getFile(fileId, skipInit);
107
107
  if (!file) {
108
- throw new import_core.ZWaveError(`NVM file ${(0, import_shared.num2hex)(fileId)} not found`, import_core.ZWaveErrorCodes.NVM_ObjectNotFound);
108
+ throw new import_safe.ZWaveError(`NVM file ${(0, import_safe2.num2hex)(fileId)} not found`, import_safe.ZWaveErrorCodes.NVM_ObjectNotFound);
109
109
  }
110
110
  return file;
111
111
  }
@@ -151,7 +151,7 @@ class NVM3Adapter {
151
151
  const file = file700 ?? file800;
152
152
  if (!file) {
153
153
  if (required) {
154
- throw new import_core.ZWaveError("ApplicationVersionFile not found!", import_core.ZWaveErrorCodes.NVM_ObjectNotFound);
154
+ throw new import_safe.ZWaveError("ApplicationVersionFile not found!", import_safe.ZWaveErrorCodes.NVM_ObjectNotFound);
155
155
  } else {
156
156
  return;
157
157
  }
@@ -343,7 +343,7 @@ class NVM3Adapter {
343
343
  }
344
344
  async setControllerNVMProperty(property, value) {
345
345
  function failFileMissing() {
346
- throw new import_core.ZWaveError("Cannot set property in NVM for non-existing file", import_core.ZWaveErrorCodes.NVM_ObjectNotFound);
346
+ throw new import_safe.ZWaveError("Cannot set property in NVM for non-existing file", import_safe.ZWaveErrorCodes.NVM_ObjectNotFound);
347
347
  }
348
348
  const expectFile = async (fileId) => {
349
349
  const file = await this._getFile(fileId);
@@ -374,7 +374,7 @@ class NVM3Adapter {
374
374
  const file800 = await this._getFile(import_files.ApplicationVersionFile800ID);
375
375
  const file = file700 ?? file800;
376
376
  if (!file) {
377
- throw new import_core.ZWaveError("ApplicationVersionFile not found!", import_core.ZWaveErrorCodes.NVM_ObjectNotFound);
377
+ throw new import_safe.ZWaveError("ApplicationVersionFile not found!", import_safe.ZWaveErrorCodes.NVM_ObjectNotFound);
378
378
  }
379
379
  const [major, minor, patch] = value.split(".").map((part) => parseInt(part, 10));
380
380
  file.major = major;