zigbee-herdsman 6.0.2 → 6.0.4

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 (252) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/package.json +9 -3
  3. package/.github/ISSUE_TEMPLATE/config.yml +0 -5
  4. package/.github/dependabot.yml +0 -22
  5. package/.github/workflows/ci.yml +0 -69
  6. package/.github/workflows/release-please.yml +0 -18
  7. package/.github/workflows/stale.yml +0 -20
  8. package/.github/workflows/typedoc.yaml +0 -47
  9. package/.release-please-manifest.json +0 -3
  10. package/.vscode/extensions.json +0 -3
  11. package/.vscode/settings.json +0 -11
  12. package/biome.json +0 -98
  13. package/examples/join-and-log.js +0 -24
  14. package/release-please-config.json +0 -9
  15. package/src/adapter/adapter.ts +0 -189
  16. package/src/adapter/adapterDiscovery.ts +0 -666
  17. package/src/adapter/const.ts +0 -12
  18. package/src/adapter/deconz/adapter/deconzAdapter.ts +0 -877
  19. package/src/adapter/deconz/driver/constants.ts +0 -246
  20. package/src/adapter/deconz/driver/driver.ts +0 -1540
  21. package/src/adapter/deconz/driver/frame.ts +0 -11
  22. package/src/adapter/deconz/driver/frameParser.ts +0 -753
  23. package/src/adapter/deconz/driver/parser.ts +0 -45
  24. package/src/adapter/deconz/driver/writer.ts +0 -22
  25. package/src/adapter/deconz/types.d.ts +0 -13
  26. package/src/adapter/ember/adapter/emberAdapter.ts +0 -2265
  27. package/src/adapter/ember/adapter/endpoints.ts +0 -86
  28. package/src/adapter/ember/adapter/oneWaitress.ts +0 -324
  29. package/src/adapter/ember/adapter/tokensManager.ts +0 -782
  30. package/src/adapter/ember/consts.ts +0 -178
  31. package/src/adapter/ember/enums.ts +0 -1746
  32. package/src/adapter/ember/ezsp/buffalo.ts +0 -1392
  33. package/src/adapter/ember/ezsp/consts.ts +0 -148
  34. package/src/adapter/ember/ezsp/enums.ts +0 -1114
  35. package/src/adapter/ember/ezsp/ezsp.ts +0 -9061
  36. package/src/adapter/ember/ezspError.ts +0 -10
  37. package/src/adapter/ember/types.ts +0 -866
  38. package/src/adapter/ember/uart/ash.ts +0 -1960
  39. package/src/adapter/ember/uart/consts.ts +0 -109
  40. package/src/adapter/ember/uart/enums.ts +0 -192
  41. package/src/adapter/ember/uart/parser.ts +0 -48
  42. package/src/adapter/ember/uart/queues.ts +0 -247
  43. package/src/adapter/ember/uart/writer.ts +0 -53
  44. package/src/adapter/ember/utils/initters.ts +0 -58
  45. package/src/adapter/ember/utils/math.ts +0 -73
  46. package/src/adapter/events.ts +0 -21
  47. package/src/adapter/ezsp/adapter/backup.ts +0 -109
  48. package/src/adapter/ezsp/adapter/ezspAdapter.ts +0 -614
  49. package/src/adapter/ezsp/driver/commands.ts +0 -2497
  50. package/src/adapter/ezsp/driver/consts.ts +0 -11
  51. package/src/adapter/ezsp/driver/driver.ts +0 -1002
  52. package/src/adapter/ezsp/driver/ezsp.ts +0 -802
  53. package/src/adapter/ezsp/driver/frame.ts +0 -101
  54. package/src/adapter/ezsp/driver/index.ts +0 -4
  55. package/src/adapter/ezsp/driver/multicast.ts +0 -78
  56. package/src/adapter/ezsp/driver/parser.ts +0 -81
  57. package/src/adapter/ezsp/driver/types/basic.ts +0 -201
  58. package/src/adapter/ezsp/driver/types/index.ts +0 -239
  59. package/src/adapter/ezsp/driver/types/named.ts +0 -2330
  60. package/src/adapter/ezsp/driver/types/struct.ts +0 -844
  61. package/src/adapter/ezsp/driver/uart.ts +0 -460
  62. package/src/adapter/ezsp/driver/utils/crc16ccitt.ts +0 -44
  63. package/src/adapter/ezsp/driver/utils/index.ts +0 -32
  64. package/src/adapter/ezsp/driver/writer.ts +0 -64
  65. package/src/adapter/index.ts +0 -3
  66. package/src/adapter/serialPort.ts +0 -58
  67. package/src/adapter/socketPortUtils.ts +0 -16
  68. package/src/adapter/tstype.ts +0 -78
  69. package/src/adapter/z-stack/adapter/adapter-backup.ts +0 -519
  70. package/src/adapter/z-stack/adapter/adapter-nv-memory.ts +0 -457
  71. package/src/adapter/z-stack/adapter/endpoints.ts +0 -57
  72. package/src/adapter/z-stack/adapter/manager.ts +0 -543
  73. package/src/adapter/z-stack/adapter/tstype.ts +0 -6
  74. package/src/adapter/z-stack/adapter/zStackAdapter.ts +0 -1190
  75. package/src/adapter/z-stack/constants/af.ts +0 -27
  76. package/src/adapter/z-stack/constants/common.ts +0 -285
  77. package/src/adapter/z-stack/constants/dbg.ts +0 -23
  78. package/src/adapter/z-stack/constants/index.ts +0 -11
  79. package/src/adapter/z-stack/constants/mac.ts +0 -128
  80. package/src/adapter/z-stack/constants/sapi.ts +0 -25
  81. package/src/adapter/z-stack/constants/sys.ts +0 -72
  82. package/src/adapter/z-stack/constants/util.ts +0 -82
  83. package/src/adapter/z-stack/constants/utils.ts +0 -14
  84. package/src/adapter/z-stack/constants/zdo.ts +0 -103
  85. package/src/adapter/z-stack/models/startup-options.ts +0 -13
  86. package/src/adapter/z-stack/structs/entries/address-manager-entry.ts +0 -44
  87. package/src/adapter/z-stack/structs/entries/address-manager-table.ts +0 -19
  88. package/src/adapter/z-stack/structs/entries/aps-link-key-data-entry.ts +0 -12
  89. package/src/adapter/z-stack/structs/entries/aps-link-key-data-table.ts +0 -21
  90. package/src/adapter/z-stack/structs/entries/aps-tc-link-key-entry.ts +0 -19
  91. package/src/adapter/z-stack/structs/entries/aps-tc-link-key-table.ts +0 -21
  92. package/src/adapter/z-stack/structs/entries/channel-list.ts +0 -8
  93. package/src/adapter/z-stack/structs/entries/has-configured.ts +0 -16
  94. package/src/adapter/z-stack/structs/entries/index.ts +0 -16
  95. package/src/adapter/z-stack/structs/entries/nib.ts +0 -66
  96. package/src/adapter/z-stack/structs/entries/nwk-key-descriptor.ts +0 -15
  97. package/src/adapter/z-stack/structs/entries/nwk-key.ts +0 -13
  98. package/src/adapter/z-stack/structs/entries/nwk-pan-id.ts +0 -8
  99. package/src/adapter/z-stack/structs/entries/nwk-sec-material-descriptor-entry.ts +0 -20
  100. package/src/adapter/z-stack/structs/entries/nwk-sec-material-descriptor-table.ts +0 -19
  101. package/src/adapter/z-stack/structs/entries/security-manager-entry.ts +0 -33
  102. package/src/adapter/z-stack/structs/entries/security-manager-table.ts +0 -22
  103. package/src/adapter/z-stack/structs/index.ts +0 -4
  104. package/src/adapter/z-stack/structs/serializable-memory-object.ts +0 -14
  105. package/src/adapter/z-stack/structs/struct.ts +0 -367
  106. package/src/adapter/z-stack/structs/table.ts +0 -198
  107. package/src/adapter/z-stack/unpi/constants.ts +0 -33
  108. package/src/adapter/z-stack/unpi/frame.ts +0 -62
  109. package/src/adapter/z-stack/unpi/index.ts +0 -4
  110. package/src/adapter/z-stack/unpi/parser.ts +0 -56
  111. package/src/adapter/z-stack/unpi/writer.ts +0 -21
  112. package/src/adapter/z-stack/utils/channel-list.ts +0 -40
  113. package/src/adapter/z-stack/utils/index.ts +0 -2
  114. package/src/adapter/z-stack/utils/network-options.ts +0 -26
  115. package/src/adapter/z-stack/znp/buffaloZnp.ts +0 -175
  116. package/src/adapter/z-stack/znp/definition.ts +0 -2713
  117. package/src/adapter/z-stack/znp/index.ts +0 -2
  118. package/src/adapter/z-stack/znp/parameterType.ts +0 -22
  119. package/src/adapter/z-stack/znp/tstype.ts +0 -44
  120. package/src/adapter/z-stack/znp/utils.ts +0 -10
  121. package/src/adapter/z-stack/znp/znp.ts +0 -342
  122. package/src/adapter/z-stack/znp/zpiObject.ts +0 -148
  123. package/src/adapter/zboss/adapter/zbossAdapter.ts +0 -526
  124. package/src/adapter/zboss/commands.ts +0 -1184
  125. package/src/adapter/zboss/consts.ts +0 -9
  126. package/src/adapter/zboss/driver.ts +0 -422
  127. package/src/adapter/zboss/enums.ts +0 -360
  128. package/src/adapter/zboss/frame.ts +0 -227
  129. package/src/adapter/zboss/reader.ts +0 -65
  130. package/src/adapter/zboss/types.ts +0 -0
  131. package/src/adapter/zboss/uart.ts +0 -428
  132. package/src/adapter/zboss/utils.ts +0 -58
  133. package/src/adapter/zboss/writer.ts +0 -49
  134. package/src/adapter/zigate/adapter/patchZdoBuffaloBE.ts +0 -27
  135. package/src/adapter/zigate/adapter/zigateAdapter.ts +0 -618
  136. package/src/adapter/zigate/driver/LICENSE +0 -17
  137. package/src/adapter/zigate/driver/buffaloZiGate.ts +0 -212
  138. package/src/adapter/zigate/driver/commandType.ts +0 -418
  139. package/src/adapter/zigate/driver/constants.ts +0 -150
  140. package/src/adapter/zigate/driver/frame.ts +0 -197
  141. package/src/adapter/zigate/driver/messageType.ts +0 -287
  142. package/src/adapter/zigate/driver/parameterType.ts +0 -32
  143. package/src/adapter/zigate/driver/ziGateObject.ts +0 -146
  144. package/src/adapter/zigate/driver/zigate.ts +0 -423
  145. package/src/adapter/zoh/adapter/utils.ts +0 -27
  146. package/src/adapter/zoh/adapter/zohAdapter.ts +0 -838
  147. package/src/buffalo/buffalo.ts +0 -342
  148. package/src/buffalo/index.ts +0 -1
  149. package/src/controller/controller.ts +0 -1022
  150. package/src/controller/database.ts +0 -124
  151. package/src/controller/events.ts +0 -52
  152. package/src/controller/greenPower.ts +0 -603
  153. package/src/controller/helpers/index.ts +0 -1
  154. package/src/controller/helpers/installCodes.ts +0 -107
  155. package/src/controller/helpers/request.ts +0 -96
  156. package/src/controller/helpers/requestQueue.ts +0 -125
  157. package/src/controller/helpers/zclFrameConverter.ts +0 -47
  158. package/src/controller/helpers/zclTransactionSequenceNumber.ts +0 -19
  159. package/src/controller/index.ts +0 -6
  160. package/src/controller/model/device.ts +0 -1249
  161. package/src/controller/model/endpoint.ts +0 -1105
  162. package/src/controller/model/entity.ts +0 -23
  163. package/src/controller/model/group.ts +0 -424
  164. package/src/controller/model/index.ts +0 -5
  165. package/src/controller/model/zigbeeEntity.ts +0 -30
  166. package/src/controller/touchlink.ts +0 -189
  167. package/src/controller/tstype.ts +0 -274
  168. package/src/index.ts +0 -12
  169. package/src/models/backup-storage-legacy.ts +0 -48
  170. package/src/models/backup-storage-unified.ts +0 -47
  171. package/src/models/backup.ts +0 -37
  172. package/src/models/index.ts +0 -5
  173. package/src/models/network-options.ts +0 -11
  174. package/src/utils/backup.ts +0 -152
  175. package/src/utils/index.ts +0 -5
  176. package/src/utils/logger.ts +0 -20
  177. package/src/utils/patchBigIntSerialization.ts +0 -8
  178. package/src/utils/queue.ts +0 -76
  179. package/src/utils/types.d.ts +0 -3
  180. package/src/utils/utils.ts +0 -19
  181. package/src/utils/wait.ts +0 -5
  182. package/src/utils/waitress.ts +0 -96
  183. package/src/zspec/consts.ts +0 -84
  184. package/src/zspec/enums.ts +0 -22
  185. package/src/zspec/index.ts +0 -3
  186. package/src/zspec/tstypes.ts +0 -18
  187. package/src/zspec/utils.ts +0 -247
  188. package/src/zspec/zcl/buffaloZcl.ts +0 -1220
  189. package/src/zspec/zcl/definition/cluster.ts +0 -5915
  190. package/src/zspec/zcl/definition/clusters-typegen.ts +0 -588
  191. package/src/zspec/zcl/definition/clusters-types.ts +0 -7331
  192. package/src/zspec/zcl/definition/consts.ts +0 -24
  193. package/src/zspec/zcl/definition/enums.ts +0 -203
  194. package/src/zspec/zcl/definition/foundation.ts +0 -329
  195. package/src/zspec/zcl/definition/manufacturerCode.ts +0 -729
  196. package/src/zspec/zcl/definition/status.ts +0 -69
  197. package/src/zspec/zcl/definition/tstype.ts +0 -377
  198. package/src/zspec/zcl/index.ts +0 -11
  199. package/src/zspec/zcl/utils.ts +0 -321
  200. package/src/zspec/zcl/zclFrame.ts +0 -356
  201. package/src/zspec/zcl/zclHeader.ts +0 -102
  202. package/src/zspec/zcl/zclStatusError.ts +0 -10
  203. package/src/zspec/zdo/buffaloZdo.ts +0 -2336
  204. package/src/zspec/zdo/definition/clusters.ts +0 -722
  205. package/src/zspec/zdo/definition/consts.ts +0 -16
  206. package/src/zspec/zdo/definition/enums.ts +0 -99
  207. package/src/zspec/zdo/definition/status.ts +0 -105
  208. package/src/zspec/zdo/definition/tstypes.ts +0 -1062
  209. package/src/zspec/zdo/index.ts +0 -7
  210. package/src/zspec/zdo/utils.ts +0 -76
  211. package/src/zspec/zdo/zdoStatusError.ts +0 -10
  212. package/test/adapter/adapter.test.ts +0 -1062
  213. package/test/adapter/ember/ash.test.ts +0 -337
  214. package/test/adapter/ember/consts.ts +0 -131
  215. package/test/adapter/ember/emberAdapter.test.ts +0 -3449
  216. package/test/adapter/ember/ezsp.test.ts +0 -385
  217. package/test/adapter/ember/ezspBuffalo.test.ts +0 -93
  218. package/test/adapter/ember/ezspError.test.ts +0 -12
  219. package/test/adapter/ember/math.test.ts +0 -206
  220. package/test/adapter/ezsp/frame.test.ts +0 -30
  221. package/test/adapter/ezsp/uart.test.ts +0 -181
  222. package/test/adapter/z-stack/adapter.test.ts +0 -3984
  223. package/test/adapter/z-stack/constants.test.ts +0 -33
  224. package/test/adapter/z-stack/structs.test.ts +0 -115
  225. package/test/adapter/z-stack/unpi.test.ts +0 -213
  226. package/test/adapter/z-stack/znp.test.ts +0 -1284
  227. package/test/adapter/zboss/fixZdoResponse.test.ts +0 -179
  228. package/test/adapter/zigate/patchZdoBuffaloBE.test.ts +0 -81
  229. package/test/adapter/zigate/zdo.test.ts +0 -187
  230. package/test/adapter/zoh/utils.test.ts +0 -36
  231. package/test/adapter/zoh/zohAdapter.test.ts +0 -1307
  232. package/test/benchOptions.ts +0 -14
  233. package/test/buffalo.test.ts +0 -431
  234. package/test/controller.bench.ts +0 -214
  235. package/test/controller.test.ts +0 -8702
  236. package/test/greenpower.test.ts +0 -1408
  237. package/test/mockAdapters.ts +0 -65
  238. package/test/mockDevices.ts +0 -598
  239. package/test/requests.bench.ts +0 -229
  240. package/test/testUtils.ts +0 -20
  241. package/test/tsconfig.json +0 -9
  242. package/test/utils/math.ts +0 -19
  243. package/test/utils.test.ts +0 -279
  244. package/test/vitest.config.mts +0 -26
  245. package/test/zcl.test.ts +0 -2831
  246. package/test/zspec/utils.test.ts +0 -68
  247. package/test/zspec/zcl/buffalo.test.ts +0 -1374
  248. package/test/zspec/zcl/frame.test.ts +0 -960
  249. package/test/zspec/zcl/utils.test.ts +0 -273
  250. package/test/zspec/zdo/buffalo.test.ts +0 -1850
  251. package/test/zspec/zdo/utils.test.ts +0 -241
  252. package/tsconfig.json +0 -24
@@ -1,782 +0,0 @@
1
- /* v8 ignore start */
2
-
3
- import {logger} from "../../../utils/logger";
4
- import {BLANK_EUI64} from "../../../zspec";
5
- import {SecManFlag, SecManKeyType, SLStatus} from "../enums";
6
- import {EMBER_ENCRYPTION_KEY_SIZE, EUI64_SIZE} from "../ezsp/consts";
7
- import {EzspValueId} from "../ezsp/enums";
8
- import type {Ezsp} from "../ezsp/ezsp";
9
- import type {EmberTokenData, SecManKey} from "../types";
10
- import {initSecurityManagerContext} from "../utils/initters";
11
-
12
- const NS = "zh:ember:tokens";
13
-
14
- //------------------------------------------------------------------------------
15
- // Definitions for stack tokens.
16
- // protocol\zigbee\stack\config\token-stack.h
17
-
18
- /**
19
- * Creator Codes
20
- *
21
- * The CREATOR is used as a distinct identifier tag for the token.
22
- *
23
- * The CREATOR is necessary because the token name is defined differently depending on the hardware platform.
24
- * Therefore, the CREATOR ensures that token definitions and data stay tagged and known.
25
- * The only requirement is that each creator definition must be unique.
26
- * See hal/micro/token.h for a more complete explanation.
27
- *
28
- */
29
- // STACK CREATORS
30
- const CREATOR_STACK_NVDATA_VERSION = 0xff01;
31
- const CREATOR_STACK_BOOT_COUNTER = 0xe263;
32
- const CREATOR_STACK_NONCE_COUNTER = 0xe563;
33
- const CREATOR_STACK_ANALYSIS_REBOOT = 0xe162;
34
- const CREATOR_STACK_KEYS = 0xeb79;
35
- const CREATOR_STACK_NODE_DATA = 0xee64;
36
- const CREATOR_STACK_CLASSIC_DATA = 0xe364;
37
- const CREATOR_STACK_ALTERNATE_KEY = 0xe475;
38
- const CREATOR_STACK_APS_FRAME_COUNTER = 0xe123;
39
- const CREATOR_STACK_TRUST_CENTER = 0xe124;
40
- const CREATOR_STACK_NETWORK_MANAGEMENT = 0xe125;
41
- const CREATOR_STACK_PARENT_INFO = 0xe126;
42
- const CREATOR_STACK_PARENT_ADDITIONAL_INFO = 0xe127;
43
- const CREATOR_STACK_MULTI_PHY_NWK_INFO = 0xe128;
44
- const CREATOR_STACK_MIN_RECEIVED_RSSI = 0xe129;
45
- // Restored EUI64
46
- const CREATOR_STACK_RESTORED_EUI64 = 0xe12a;
47
-
48
- // MULTI-NETWORK STACK CREATORS
49
- const CREATOR_MULTI_NETWORK_STACK_KEYS = 0xe210;
50
- const CREATOR_MULTI_NETWORK_STACK_NODE_DATA = 0xe211;
51
- const CREATOR_MULTI_NETWORK_STACK_ALTERNATE_KEY = 0xe212;
52
- const CREATOR_MULTI_NETWORK_STACK_TRUST_CENTER = 0xe213;
53
- const CREATOR_MULTI_NETWORK_STACK_NETWORK_MANAGEMENT = 0xe214;
54
- const CREATOR_MULTI_NETWORK_STACK_PARENT_INFO = 0xe215;
55
-
56
- // A temporary solution for multi-network nwk counters:
57
- // This counter will be used on the network with index 1.
58
- const CREATOR_MULTI_NETWORK_STACK_NONCE_COUNTER = 0xe220;
59
- const CREATOR_MULTI_NETWORK_STACK_PARENT_ADDITIONAL_INFO = 0xe221;
60
-
61
- // GP stack tokens.
62
- const CREATOR_STACK_GP_DATA = 0xe258;
63
- const CREATOR_STACK_GP_PROXY_TABLE = 0xe259;
64
- const CREATOR_STACK_GP_SINK_TABLE = 0xe25a;
65
- const CREATOR_STACK_GP_INCOMING_FC = 0xe25b;
66
- const CREATOR_STACK_GP_INCOMING_FC_IN_SINK = 0xe25c;
67
- // APP CREATORS
68
- const CREATOR_STACK_BINDING_TABLE = 0xe274;
69
- const CREATOR_STACK_CHILD_TABLE = 0xff0d;
70
- const CREATOR_STACK_KEY_TABLE = 0xe456;
71
- const CREATOR_STACK_CERTIFICATE_TABLE = 0xe500;
72
- const CREATOR_STACK_ZLL_DATA = 0xe501;
73
- const CREATOR_STACK_ZLL_SECURITY = 0xe502;
74
- const CREATOR_STACK_ADDITIONAL_CHILD_DATA = 0xe503;
75
-
76
- /**
77
- * NVM3 Object Keys
78
- *
79
- * The NVM3 object key is used as a distinct identifier tag for a token stored in NVM3.
80
- *
81
- * Every token must have a defined NVM3 object key and the object key must be unique.
82
- * The object key defined must be in the following format:
83
- *
84
- * NVM3KEY_tokenname where tokenname is the name of the token without NVM3KEY_ or TOKEN_ prefix.
85
- *
86
- */
87
- // NVM3KEY domain base keys
88
- // const NVM3KEY_DOMAIN_USER = 0x00000;
89
- const NVM3KEY_DOMAIN_ZIGBEE = 0x10000;
90
- // const NVM3KEY_DOMAIN_COMMON = 0x80000;
91
-
92
- // STACK KEYS
93
- const NVM3KEY_STACK_NVDATA_VERSION = NVM3KEY_DOMAIN_ZIGBEE | 0xff01;
94
- const NVM3KEY_STACK_BOOT_COUNTER = NVM3KEY_DOMAIN_ZIGBEE | 0xe263;
95
- const NVM3KEY_STACK_NONCE_COUNTER = NVM3KEY_DOMAIN_ZIGBEE | 0xe563;
96
- const NVM3KEY_STACK_ANALYSIS_REBOOT = NVM3KEY_DOMAIN_ZIGBEE | 0xe162;
97
- const NVM3KEY_STACK_KEYS = NVM3KEY_DOMAIN_ZIGBEE | 0xeb79;
98
- const NVM3KEY_STACK_NODE_DATA = NVM3KEY_DOMAIN_ZIGBEE | 0xee64;
99
- const NVM3KEY_STACK_CLASSIC_DATA = NVM3KEY_DOMAIN_ZIGBEE | 0xe364;
100
- const NVM3KEY_STACK_ALTERNATE_KEY = NVM3KEY_DOMAIN_ZIGBEE | 0xe475;
101
- const NVM3KEY_STACK_APS_FRAME_COUNTER = NVM3KEY_DOMAIN_ZIGBEE | 0xe123;
102
- const NVM3KEY_STACK_TRUST_CENTER = NVM3KEY_DOMAIN_ZIGBEE | 0xe124;
103
- const NVM3KEY_STACK_NETWORK_MANAGEMENT = NVM3KEY_DOMAIN_ZIGBEE | 0xe125;
104
- const NVM3KEY_STACK_PARENT_INFO = NVM3KEY_DOMAIN_ZIGBEE | 0xe126;
105
- const NVM3KEY_STACK_PARENT_ADDITIONAL_INFO = NVM3KEY_DOMAIN_ZIGBEE | 0xe127;
106
- const NVM3KEY_STACK_MULTI_PHY_NWK_INFO = NVM3KEY_DOMAIN_ZIGBEE | 0xe128;
107
- const NVM3KEY_STACK_MIN_RECEIVED_RSSI = NVM3KEY_DOMAIN_ZIGBEE | 0xe129;
108
- // Restored EUI64
109
- const NVM3KEY_STACK_RESTORED_EUI64 = NVM3KEY_DOMAIN_ZIGBEE | 0xe12a;
110
-
111
- // MULTI-NETWORK STACK KEYS
112
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved.
113
- const NVM3KEY_MULTI_NETWORK_STACK_KEYS = NVM3KEY_DOMAIN_ZIGBEE | 0x0000;
114
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved.
115
- const NVM3KEY_MULTI_NETWORK_STACK_NODE_DATA = NVM3KEY_DOMAIN_ZIGBEE | 0x0080;
116
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved.
117
- const NVM3KEY_MULTI_NETWORK_STACK_ALTERNATE_KEY = NVM3KEY_DOMAIN_ZIGBEE | 0x0100;
118
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved.
119
- const NVM3KEY_MULTI_NETWORK_STACK_TRUST_CENTER = NVM3KEY_DOMAIN_ZIGBEE | 0x0180;
120
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved.
121
- const NVM3KEY_MULTI_NETWORK_STACK_NETWORK_MANAGEMENT = NVM3KEY_DOMAIN_ZIGBEE | 0x0200;
122
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved.
123
- const NVM3KEY_MULTI_NETWORK_STACK_PARENT_INFO = NVM3KEY_DOMAIN_ZIGBEE | 0x0280;
124
-
125
- // Temporary solution for multi-network nwk counters:
126
- // This counter will be used on the network with index 1.
127
- const NVM3KEY_MULTI_NETWORK_STACK_NONCE_COUNTER = NVM3KEY_DOMAIN_ZIGBEE | 0xe220;
128
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved
129
- const NVM3KEY_MULTI_NETWORK_STACK_PARENT_ADDITIONAL_INFO = NVM3KEY_DOMAIN_ZIGBEE | 0x0300;
130
-
131
- // GP stack tokens.
132
- const NVM3KEY_STACK_GP_DATA = NVM3KEY_DOMAIN_ZIGBEE | 0xe258;
133
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved.
134
- const NVM3KEY_STACK_GP_PROXY_TABLE = NVM3KEY_DOMAIN_ZIGBEE | 0x0380;
135
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved.
136
- const NVM3KEY_STACK_GP_SINK_TABLE = NVM3KEY_DOMAIN_ZIGBEE | 0x0400;
137
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved
138
- const NVM3KEY_STACK_GP_INCOMING_FC = NVM3KEY_DOMAIN_ZIGBEE | 0x0480;
139
-
140
- // APP KEYS
141
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved.
142
- const NVM3KEY_STACK_BINDING_TABLE = NVM3KEY_DOMAIN_ZIGBEE | 0x0500;
143
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved.
144
- const NVM3KEY_STACK_CHILD_TABLE = NVM3KEY_DOMAIN_ZIGBEE | 0x0580;
145
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved.
146
- const NVM3KEY_STACK_KEY_TABLE = NVM3KEY_DOMAIN_ZIGBEE | 0x0600;
147
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved.
148
- const NVM3KEY_STACK_CERTIFICATE_TABLE = NVM3KEY_DOMAIN_ZIGBEE | 0x0680;
149
- const NVM3KEY_STACK_ZLL_DATA = NVM3KEY_DOMAIN_ZIGBEE | 0xe501;
150
- const NVM3KEY_STACK_ZLL_SECURITY = NVM3KEY_DOMAIN_ZIGBEE | 0xe502;
151
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved.
152
- const NVM3KEY_STACK_ADDITIONAL_CHILD_DATA = NVM3KEY_DOMAIN_ZIGBEE | 0x0700;
153
-
154
- // This key is used for an indexed token and the subsequent 0x7F keys are also reserved
155
- const NVM3KEY_STACK_GP_INCOMING_FC_IN_SINK = NVM3KEY_DOMAIN_ZIGBEE | 0x0780;
156
-
157
- // XXX: comment out in prod, along with debug token prints
158
- // const DEBUG_TOKEN_STRINGS = {
159
- // [NVM3KEY_STACK_NVDATA_VERSION]: 'NVM3KEY_STACK_NVDATA_VERSION',
160
- // [NVM3KEY_STACK_BOOT_COUNTER]: 'NVM3KEY_STACK_BOOT_COUNTER',
161
- // [NVM3KEY_STACK_NONCE_COUNTER]: 'NVM3KEY_STACK_NONCE_COUNTER',
162
- // [NVM3KEY_STACK_ANALYSIS_REBOOT]: 'NVM3KEY_STACK_ANALYSIS_REBOOT',
163
- // [NVM3KEY_STACK_KEYS]: 'NVM3KEY_STACK_KEYS',
164
- // [NVM3KEY_STACK_NODE_DATA]: 'NVM3KEY_STACK_NODE_DATA',
165
- // [NVM3KEY_STACK_CLASSIC_DATA]: 'NVM3KEY_STACK_CLASSIC_DATA',
166
- // [NVM3KEY_STACK_ALTERNATE_KEY]: 'NVM3KEY_STACK_ALTERNATE_KEY',
167
- // [NVM3KEY_STACK_APS_FRAME_COUNTER]: 'NVM3KEY_STACK_APS_FRAME_COUNTER',
168
- // [NVM3KEY_STACK_TRUST_CENTER]: 'NVM3KEY_STACK_TRUST_CENTER',
169
- // [NVM3KEY_STACK_NETWORK_MANAGEMENT]: 'NVM3KEY_STACK_NETWORK_MANAGEMENT',
170
- // [NVM3KEY_STACK_PARENT_INFO]: 'NVM3KEY_STACK_PARENT_INFO',
171
- // [NVM3KEY_STACK_PARENT_ADDITIONAL_INFO]: 'NVM3KEY_STACK_PARENT_ADDITIONAL_INFO',
172
- // [NVM3KEY_STACK_MULTI_PHY_NWK_INFO]: 'NVM3KEY_STACK_MULTI_PHY_NWK_INFO',
173
- // [NVM3KEY_STACK_MIN_RECEIVED_RSSI]: 'NVM3KEY_STACK_MIN_RECEIVED_RSSI',
174
- // [NVM3KEY_STACK_RESTORED_EUI64]: 'NVM3KEY_STACK_RESTORED_EUI64',
175
- // [NVM3KEY_MULTI_NETWORK_STACK_KEYS]: 'NVM3KEY_MULTI_NETWORK_STACK_KEYS',
176
- // [NVM3KEY_MULTI_NETWORK_STACK_NODE_DATA]: 'NVM3KEY_MULTI_NETWORK_STACK_NODE_DATA',
177
- // [NVM3KEY_MULTI_NETWORK_STACK_ALTERNATE_KEY]: 'NVM3KEY_MULTI_NETWORK_STACK_ALTERNATE_KEY',
178
- // [NVM3KEY_MULTI_NETWORK_STACK_TRUST_CENTER]: 'NVM3KEY_MULTI_NETWORK_STACK_TRUST_CENTER',
179
- // [NVM3KEY_MULTI_NETWORK_STACK_NETWORK_MANAGEMENT]: 'NVM3KEY_MULTI_NETWORK_STACK_NETWORK_MANAGEMENT',
180
- // [NVM3KEY_MULTI_NETWORK_STACK_PARENT_INFO]: 'NVM3KEY_MULTI_NETWORK_STACK_PARENT_INFO',
181
- // [NVM3KEY_MULTI_NETWORK_STACK_NONCE_COUNTER]: 'NVM3KEY_MULTI_NETWORK_STACK_NONCE_COUNTER',
182
- // [NVM3KEY_MULTI_NETWORK_STACK_PARENT_ADDITIONAL_INFO]: 'NVM3KEY_MULTI_NETWORK_STACK_PARENT_ADDITIONAL_INFO',
183
- // [NVM3KEY_STACK_GP_DATA]: 'NVM3KEY_STACK_GP_DATA',
184
- // [NVM3KEY_STACK_GP_PROXY_TABLE]: 'NVM3KEY_STACK_GP_PROXY_TABLE',
185
- // [NVM3KEY_STACK_GP_SINK_TABLE]: 'NVM3KEY_STACK_GP_SINK_TABLE',
186
- // [NVM3KEY_STACK_GP_INCOMING_FC]: 'NVM3KEY_STACK_GP_INCOMING_FC',
187
- // [NVM3KEY_STACK_BINDING_TABLE]: 'NVM3KEY_STACK_BINDING_TABLE',
188
- // [NVM3KEY_STACK_CHILD_TABLE]: 'NVM3KEY_STACK_CHILD_TABLE',
189
- // [NVM3KEY_STACK_KEY_TABLE]: 'NVM3KEY_STACK_KEY_TABLE',
190
- // [NVM3KEY_STACK_CERTIFICATE_TABLE]: 'NVM3KEY_STACK_CERTIFICATE_TABLE',
191
- // [NVM3KEY_STACK_ZLL_DATA]: 'NVM3KEY_STACK_ZLL_DATA',
192
- // [NVM3KEY_STACK_ZLL_SECURITY]: 'NVM3KEY_STACK_ZLL_SECURITY',
193
- // [NVM3KEY_STACK_ADDITIONAL_CHILD_DATA]: 'NVM3KEY_STACK_ADDITIONAL_CHILD_DATA',
194
- // [NVM3KEY_STACK_GP_INCOMING_FC_IN_SINK]: 'NVM3KEY_STACK_GP_INCOMING_FC_IN_SINK',
195
- // };
196
-
197
- /**
198
- * The current version number of the stack tokens.
199
- * MSB is the version, LSB is a complement.
200
- *
201
- * See hal/micro/token.h for a more complete explanation.
202
- */
203
- // const CURRENT_STACK_TOKEN_VERSION = 0x03fc;
204
-
205
- /** 8-byte IEEE + 16-byte Key + 1-byte info */
206
- // const KEY_TABLE_ENTRY_SIZE = 25;
207
- // const KEY_ENTRY_IEEE_OFFSET = 0;
208
- /** first 4 bytes may point to PSA ID if data[KEY_ENTRY_INFO_OFFSET] & KEY_TABLE_ENTRY_HAS_PSA_ID */
209
- const KEY_ENTRY_KEY_DATA_OFFSET = 8;
210
- // const KEY_ENTRY_INFO_OFFSET = 24;
211
-
212
- /** uint16_t */
213
- const CREATORS: number[] = [
214
- CREATOR_STACK_NVDATA_VERSION,
215
- CREATOR_STACK_BOOT_COUNTER,
216
- CREATOR_STACK_NONCE_COUNTER,
217
- CREATOR_STACK_ANALYSIS_REBOOT,
218
- CREATOR_STACK_KEYS,
219
- CREATOR_STACK_NODE_DATA,
220
- CREATOR_STACK_CLASSIC_DATA,
221
- CREATOR_STACK_ALTERNATE_KEY,
222
- CREATOR_STACK_APS_FRAME_COUNTER,
223
- CREATOR_STACK_TRUST_CENTER,
224
- CREATOR_STACK_NETWORK_MANAGEMENT,
225
- CREATOR_STACK_PARENT_INFO,
226
- CREATOR_STACK_PARENT_ADDITIONAL_INFO,
227
- CREATOR_STACK_MULTI_PHY_NWK_INFO,
228
- CREATOR_STACK_MIN_RECEIVED_RSSI,
229
- CREATOR_STACK_RESTORED_EUI64,
230
- CREATOR_MULTI_NETWORK_STACK_KEYS,
231
- CREATOR_MULTI_NETWORK_STACK_NODE_DATA,
232
- CREATOR_MULTI_NETWORK_STACK_ALTERNATE_KEY,
233
- CREATOR_MULTI_NETWORK_STACK_TRUST_CENTER,
234
- CREATOR_MULTI_NETWORK_STACK_NETWORK_MANAGEMENT,
235
- CREATOR_MULTI_NETWORK_STACK_PARENT_INFO,
236
- CREATOR_MULTI_NETWORK_STACK_NONCE_COUNTER,
237
- CREATOR_MULTI_NETWORK_STACK_PARENT_ADDITIONAL_INFO,
238
- CREATOR_STACK_GP_DATA,
239
- CREATOR_STACK_GP_PROXY_TABLE,
240
- CREATOR_STACK_GP_SINK_TABLE,
241
- CREATOR_STACK_GP_INCOMING_FC,
242
- CREATOR_STACK_GP_INCOMING_FC_IN_SINK,
243
- CREATOR_STACK_BINDING_TABLE,
244
- CREATOR_STACK_CHILD_TABLE,
245
- CREATOR_STACK_KEY_TABLE,
246
- CREATOR_STACK_CERTIFICATE_TABLE,
247
- CREATOR_STACK_ZLL_DATA,
248
- CREATOR_STACK_ZLL_SECURITY,
249
- CREATOR_STACK_ADDITIONAL_CHILD_DATA,
250
- ];
251
-
252
- /** uint32_t */
253
- const NVM3KEYS: number[] = [
254
- NVM3KEY_STACK_NVDATA_VERSION,
255
- NVM3KEY_STACK_BOOT_COUNTER,
256
- NVM3KEY_STACK_NONCE_COUNTER,
257
- NVM3KEY_STACK_ANALYSIS_REBOOT,
258
- NVM3KEY_STACK_KEYS,
259
- NVM3KEY_STACK_NODE_DATA,
260
- NVM3KEY_STACK_CLASSIC_DATA,
261
- NVM3KEY_STACK_ALTERNATE_KEY,
262
- NVM3KEY_STACK_APS_FRAME_COUNTER,
263
- NVM3KEY_STACK_TRUST_CENTER,
264
- NVM3KEY_STACK_NETWORK_MANAGEMENT,
265
- NVM3KEY_STACK_PARENT_INFO,
266
- NVM3KEY_STACK_PARENT_ADDITIONAL_INFO,
267
- NVM3KEY_STACK_MULTI_PHY_NWK_INFO,
268
- NVM3KEY_STACK_MIN_RECEIVED_RSSI,
269
- NVM3KEY_STACK_RESTORED_EUI64,
270
- NVM3KEY_MULTI_NETWORK_STACK_KEYS,
271
- NVM3KEY_MULTI_NETWORK_STACK_NODE_DATA,
272
- NVM3KEY_MULTI_NETWORK_STACK_ALTERNATE_KEY,
273
- NVM3KEY_MULTI_NETWORK_STACK_TRUST_CENTER,
274
- NVM3KEY_MULTI_NETWORK_STACK_NETWORK_MANAGEMENT,
275
- NVM3KEY_MULTI_NETWORK_STACK_PARENT_INFO,
276
- NVM3KEY_MULTI_NETWORK_STACK_NONCE_COUNTER,
277
- NVM3KEY_MULTI_NETWORK_STACK_PARENT_ADDITIONAL_INFO,
278
- NVM3KEY_STACK_GP_DATA,
279
- NVM3KEY_STACK_GP_PROXY_TABLE,
280
- NVM3KEY_STACK_GP_SINK_TABLE,
281
- NVM3KEY_STACK_GP_INCOMING_FC,
282
- NVM3KEY_STACK_BINDING_TABLE,
283
- NVM3KEY_STACK_CHILD_TABLE,
284
- NVM3KEY_STACK_KEY_TABLE,
285
- NVM3KEY_STACK_CERTIFICATE_TABLE,
286
- NVM3KEY_STACK_ZLL_DATA,
287
- NVM3KEY_STACK_ZLL_SECURITY,
288
- NVM3KEY_STACK_ADDITIONAL_CHILD_DATA,
289
- NVM3KEY_STACK_GP_INCOMING_FC_IN_SINK,
290
- ];
291
-
292
- const BLANK_EUI64_BUF = Buffer.from(BLANK_EUI64.substring(2) /*take out 0x*/, "hex");
293
-
294
- // biome-ignore lint/complexity/noStaticOnlyClass: TODO to functions
295
- export class EmberTokensManager {
296
- /**
297
- * Host-only API to check whether the NCP uses key storage.
298
- *
299
- * @returns false if keys are in classic key storage, and true if they are located in PSA key storage.
300
- */
301
- public static async ncpUsesPSAKeyStorage(ezsp: Ezsp): Promise<boolean> {
302
- const [status, valueLength, value] = await ezsp.ezspGetValue(EzspValueId.KEY_STORAGE_VERSION, 1);
303
-
304
- if (status !== SLStatus.OK || valueLength < 1) {
305
- throw new Error(`[TOKENS] Error retrieving key storage version, status=${SLStatus[status]}.`);
306
- }
307
-
308
- return value[0] === 1;
309
- }
310
-
311
- /**
312
- * Matcher for Zigbeed tokens.
313
- * @param nvm3Key
314
- * @returns
315
- */
316
- public static getCreatorFromNvm3Key(nvm3Key: number): number {
317
- for (let i = 0; i < NVM3KEYS.length; i++) {
318
- if (NVM3KEYS[i] === nvm3Key) {
319
- return CREATORS[i];
320
- }
321
- }
322
-
323
- return 0xffff;
324
- }
325
-
326
- /**
327
- * Saves tokens. Only for NVM3-based NCP.
328
- *
329
- * The binary file format to save the tokens are
330
- *
331
- * Number of Tokens (1 byte)
332
- * Token0 (4 bytes) Token0Size(1 byte) Token0ArraySize(1 byte) Token0Data(Token0Size * Token0ArraySize)
333
- * :
334
- * :
335
- * TokenM (4 bytes) TokenMSize(1 byte) TokenMArraySize(1 byte) TokenMData(TokenMSize * TokenMArraySize)
336
- *
337
- * @param localEui64 Used in place of blank `restoredEui64` keys
338
- *
339
- * @return Saved tokens buffer or null.
340
- */
341
- public static async saveTokens(ezsp: Ezsp, localEui64: Buffer): Promise<Buffer | undefined> {
342
- logger.info("[TOKENS] Saving tokens...", NS);
343
- const tokenCount = await ezsp.ezspGetTokenCount();
344
-
345
- if (tokenCount) {
346
- const chunks: Buffer[] = [Buffer.from([tokenCount])]; // 1 byte
347
- // returns 1 if NCP has secure key storage (where these tokens do not store the key data).
348
- // Don't compile for scripted test or any non-host code due to linker issues.
349
- const hasSecureStorage: boolean = await EmberTokensManager.ncpUsesPSAKeyStorage(ezsp);
350
-
351
- logger.debug(`[TOKENS] Saving ${tokenCount} tokens, ${hasSecureStorage ? "with" : "without"} secure storage.`, NS);
352
-
353
- for (let i = 0; i < tokenCount; i++) {
354
- const [tiStatus, tokenInfo] = await ezsp.ezspGetTokenInfo(i);
355
- let writeOffset = 0;
356
-
357
- if (tiStatus === SLStatus.OK) {
358
- const outputToken = Buffer.alloc(4 + 1 + 1 + tokenInfo.size * tokenInfo.arraySize);
359
- outputToken.writeUInt32LE(tokenInfo.nvm3Key, writeOffset); // 4 bytes
360
- writeOffset += 4;
361
- outputToken.writeUInt8(tokenInfo.size, writeOffset++); // 1 byte
362
- outputToken.writeUInt8(tokenInfo.arraySize, writeOffset++); // 1 byte
363
-
364
- for (let arrayIndex = 0; arrayIndex < tokenInfo.arraySize; arrayIndex++) {
365
- const [tdStatus, tokenData] = await ezsp.ezspGetTokenData(tokenInfo.nvm3Key, arrayIndex);
366
-
367
- if (tdStatus === SLStatus.OK) {
368
- if (hasSecureStorage) {
369
- // Populate keys into tokenData because tokens do not contain them with secure key storage
370
- await EmberTokensManager.saveKeysToData(ezsp, tokenData, tokenInfo.nvm3Key, arrayIndex);
371
-
372
- // ensure the token data was retrieved properly, length should match the size announced by the token info
373
- if (tokenData.data.length !== tokenInfo.size) {
374
- logger.error(
375
- `[TOKENS] Mismatch in token data size; got ${tokenData.data.length}, expected ${tokenInfo.size}.`,
376
- NS,
377
- );
378
- }
379
- }
380
-
381
- // logger.debug(`[TOKENS] TOKEN nvm3Key=${DEBUG_TOKEN_STRINGS[tokenInfo.nvm3Key]} size=${tokenInfo.size} `
382
- // + `arraySize=${tokenInfo.arraySize} token=${tokenData.data.toString('hex')}`, NS);
383
-
384
- // Check the Key to see if the token to save is restoredEui64, in that case
385
- // check if it is blank, then save the node EUI64 in its place, else save the value
386
- // received from the API. Once it saves, during restore process the set token will
387
- // simply write the restoredEUI64 and the node will start to use that.
388
- if (
389
- tokenInfo.nvm3Key === NVM3KEY_STACK_RESTORED_EUI64 &&
390
- tokenData.size === EUI64_SIZE &&
391
- tokenData.data.equals(BLANK_EUI64_BUF)
392
- ) {
393
- // Special case : Save the node EUI64 on the restoredEui64 token while saving.
394
- tokenData.data.set(localEui64);
395
- logger.debug("[TOKENS] Saved node EUI64 in place of blank RESTORED EUI64.", NS);
396
- }
397
-
398
- outputToken.set(tokenData.data, writeOffset);
399
- writeOffset += tokenData.size;
400
- } else {
401
- logger.error(`[TOKENS] Failed to get token data at index ${arrayIndex} with status=${SLStatus[tdStatus]}.`, NS);
402
- }
403
- }
404
-
405
- chunks.push(outputToken);
406
- } else {
407
- logger.error(`[TOKENS] Failed to get token info at index ${i} with status=${SLStatus[tiStatus]}.`, NS);
408
- }
409
- }
410
-
411
- return Buffer.concat(chunks);
412
- }
413
-
414
- // ezspGetTokenCount == 0 OR (ezspGetTokenInfo|ezspGetTokenData|ezspSetTokenData return LIBRARY_NOT_PRESENT)
415
- // ezspTokenFactoryReset will do nothing.
416
- logger.error("[TOKENS] Saving tokens not supported by adapter (not NVM3-based).", NS);
417
-
418
- return undefined;
419
- }
420
-
421
- /**
422
- * Restores tokens. Only for NVM3-based NCP.
423
- * XXX: If a previous backup from an NVM3 NCP is attempted on a non-NVM3 NCP,
424
- * it should just fail (LIBRARY_NOT_PRESENT all on token-related functions).
425
- *
426
- * @see EmberTokensManager.saveTokens() for format
427
- *
428
- * @return SLStatus status code
429
- */
430
- public static async restoreTokens(ezsp: Ezsp, inBuffer: Buffer): Promise<SLStatus> {
431
- if (!inBuffer?.length) {
432
- throw new Error("[TOKENS] Restore tokens buffer empty.");
433
- }
434
-
435
- logger.info("[TOKENS] Restoring tokens...", NS);
436
-
437
- let readOffset = 0;
438
- const inTokenCount = inBuffer.readUInt8(readOffset++);
439
- const hasSecureStorage: boolean = await EmberTokensManager.ncpUsesPSAKeyStorage(ezsp);
440
-
441
- logger.debug(`[TOKENS] Restoring ${inTokenCount} tokens, ${hasSecureStorage ? "with" : "without"} secure storage.`, NS);
442
-
443
- for (let i = 0; i < inTokenCount; i++) {
444
- const [tiStatus, tokenInfo] = await ezsp.ezspGetTokenInfo(i);
445
-
446
- if (tiStatus === SLStatus.OK) {
447
- const nvm3Key = inBuffer.readUInt32LE(readOffset); // 4 bytes Token Key/Creator
448
- readOffset += 4;
449
- const size = inBuffer.readUInt8(readOffset++); // 1 byte token size
450
- const arraySize = inBuffer.readUInt8(readOffset++); // 1 byte array size.
451
-
452
- for (let arrayIndex = 0; arrayIndex < arraySize; arrayIndex++) {
453
- const tokenData: EmberTokenData = {
454
- data: inBuffer.subarray(readOffset, readOffset + size),
455
- size,
456
- };
457
-
458
- if (hasSecureStorage) {
459
- // do not keep keys in classic key storage upon restoration
460
- await EmberTokensManager.restoreKeysFromData(ezsp, tokenData, tokenInfo.nvm3Key, arrayIndex);
461
- }
462
-
463
- const status = await ezsp.ezspSetTokenData(nvm3Key, arrayIndex, tokenData);
464
-
465
- if (status !== SLStatus.OK) {
466
- logger.error(`[TOKENS] Failed to set token data for key "${nvm3Key}" with status=${SLStatus[status]}.`, NS);
467
- }
468
-
469
- readOffset += tokenData.size;
470
- }
471
- } else {
472
- logger.error(`[TOKENS] Failed to get token info at index ${i} with status=${SLStatus[tiStatus]}.`, NS);
473
- }
474
- }
475
-
476
- return SLStatus.OK;
477
- }
478
-
479
- /**
480
- * Secure key storage needs to export the keys first so backup file has them.
481
- *
482
- * @param tokenData EmberTokenData* [IN/OUT]
483
- * @param nvm3Key uint32_t
484
- * @param index uint8_t
485
- * @returns
486
- */
487
- public static async saveKeysToData(ezsp: Ezsp, tokenData: EmberTokenData, nvm3Key: number, index: number): Promise<SLStatus> {
488
- let status: SLStatus = SLStatus.OK;
489
- const context = initSecurityManagerContext();
490
- let plaintextKey: SecManKey;
491
-
492
- if (nvm3Key === NVM3KEY_STACK_KEYS) {
493
- // typedef struct {
494
- // uint8_t networkKey[16]; // ignored if using Secure Key Storage (but moved to PSA and cleared if upgrade code is run)
495
- // uint8_t activeKeySeqNum;
496
- // } tokTypeStackKeys;
497
-
498
- context.coreKeyType = SecManKeyType.NETWORK;
499
- context.keyIndex = 0;
500
-
501
- [status, plaintextKey] = await ezsp.ezspExportKey(context);
502
-
503
- tokenData.data.set(plaintextKey.contents, 0); // at beginning
504
- } else if (nvm3Key === NVM3KEY_STACK_ALTERNATE_KEY) {
505
- // typedef struct {
506
- // uint8_t networkKey[16]; // ignored if using Secure Key Storage (but moved to PSA and cleared if upgrade code is run)
507
- // uint8_t activeKeySeqNum;
508
- // } tokTypeStackKeys;
509
-
510
- context.coreKeyType = SecManKeyType.NETWORK;
511
- context.keyIndex = 1;
512
-
513
- [status, plaintextKey] = await ezsp.ezspExportKey(context);
514
-
515
- tokenData.data.set(plaintextKey.contents, 0); // at beginning
516
- } else if (nvm3Key === NVM3KEY_STACK_TRUST_CENTER) {
517
- // typedef struct {
518
- // uint16_t mode;
519
- // uint8_t eui64[8];
520
- // uint8_t key[16]; // ignored if (mode & TRUST_CENTER_KEY_LIVES_IN_PSA)
521
- // } tokTypeStackTrustCenter;
522
-
523
- context.coreKeyType = SecManKeyType.TC_LINK;
524
-
525
- [status, plaintextKey] = await ezsp.ezspExportKey(context);
526
-
527
- tokenData.data.set(plaintextKey.contents, 2 + EUI64_SIZE); // uint16_t+uint8_t[8]
528
- } else if (nvm3Key === NVM3KEY_STACK_KEY_TABLE) {
529
- // typedef uint8_t tokTypeStackKeyTable[25];
530
-
531
- context.coreKeyType = SecManKeyType.APP_LINK;
532
- context.keyIndex = index;
533
- //this must be set to export a specific link key from table
534
- context.flags |= SecManFlag.KEY_INDEX_IS_VALID;
535
-
536
- [status, plaintextKey] = await ezsp.ezspExportKey(context);
537
-
538
- tokenData.data.set(plaintextKey.contents, KEY_ENTRY_KEY_DATA_OFFSET); // end part of uint8_t[25]
539
- } else if (nvm3Key === NVM3KEY_STACK_GP_PROXY_TABLE) {
540
- // typedef struct {
541
- // uint8_t status;
542
- // uint32_t options;
543
- // //EmberGpAddress gpd;
544
- // uint8_t gpAddress[8];
545
- // uint8_t endpoint;
546
- // //uint16_t assignedAlias;
547
- // uint8_t securityOptions;
548
- // uint8_t gpdKey[16]; // ignored if using Secure Key Storage (but moved to PSA and cleared if upgrade code is run)
549
- // //EmberGpSinkListEntry sinkList[2];
550
- // uint8_t sinkType[2];
551
- // uint8_t sinkEUI[2][8];
552
- // //uint16_t sinkNodeId[2];
553
- // } tokTypeStackGpProxyTableEntry;
554
-
555
- context.coreKeyType = SecManKeyType.GREEN_POWER_PROXY_TABLE_KEY;
556
- context.keyIndex = index;
557
-
558
- [status, plaintextKey] = await ezsp.ezspExportKey(context);
559
-
560
- tokenData.data.set(plaintextKey.contents, 1 + 4 + 8 + 1 + 1); // uint8_t+uint32_t+uint8_t[8]+uint8_t+uint8_t
561
- } else if (nvm3Key === NVM3KEY_STACK_GP_SINK_TABLE) {
562
- // typedef struct {
563
- // uint8_t status;
564
- // uint16_t options;
565
- // //EmberGpAddress gpd;
566
- // uint8_t gpAddress[8];
567
- // uint8_t endpoint;
568
- // uint8_t securityOptions;
569
- // uint8_t gpdKey[16]; // ignored if using Secure Key Storage (but moved to PSA and cleared if upgrade code is run)
570
- // uint8_t sinkType[2];
571
- // uint16_t groupList[2][2];
572
- // uint32_t securityFrameCounter; // This is no more used, Incoming FC for gpd in a separate Token to control its update.
573
- // uint16_t assignedAlias;
574
- // uint8_t deviceId;
575
- // uint8_t groupcastRadius;
576
- // } tokTypeStackGpSinkTableEntry;
577
-
578
- context.coreKeyType = SecManKeyType.GREEN_POWER_SINK_TABLE_KEY;
579
- context.keyIndex = index;
580
-
581
- [status, plaintextKey] = await ezsp.ezspExportKey(context);
582
-
583
- tokenData.data.set(plaintextKey.contents, 1 + 2 + 8 + 1 + 1); // uint8_t+uint16_t+uint8_t[8]+uint8_t+uint8_t
584
- } else if (nvm3Key === NVM3KEY_STACK_ZLL_SECURITY) {
585
- // typedef struct {
586
- // uint32_t bitmask;
587
- // uint8_t keyIndex;
588
- // uint8_t encryptionKey[EMBER_ENCRYPTION_KEY_SIZE];
589
- // uint8_t preconfiguredKey[EMBER_ENCRYPTION_KEY_SIZE];
590
- // } EmberTokTypeStackZllSecurity;
591
-
592
- context.coreKeyType = SecManKeyType.ZLL_ENCRYPTION_KEY;
593
-
594
- [status, plaintextKey] = await ezsp.ezspExportKey(context);
595
-
596
- tokenData.data.set(plaintextKey.contents, 4 + 1); // uint32_t+uint8_t
597
-
598
- context.coreKeyType = SecManKeyType.ZLL_PRECONFIGURED_KEY;
599
-
600
- [status, plaintextKey] = await ezsp.ezspExportKey(context);
601
-
602
- tokenData.data.set(plaintextKey.contents, 4 + 1 + EMBER_ENCRYPTION_KEY_SIZE); // uint32_t+uint8_t+uint8_t[EMBER_ENCRYPTION_KEY_SIZE]
603
- } else {
604
- //nothing needs to be done for non-key tokens
605
- }
606
-
607
- return status;
608
- }
609
-
610
- /**
611
- *
612
- * @param data_s EmberTokenData*
613
- * @param nvm3Key uint32_t
614
- * @param index uint8_t
615
- * @returns
616
- *
617
- * @from sli_zigbee_af_trust_center_backup_restore_keys_from_data
618
- */
619
- public static async restoreKeysFromData(ezsp: Ezsp, tokenData: EmberTokenData, nvm3Key: number, index: number): Promise<SLStatus> {
620
- let status: SLStatus = SLStatus.OK;
621
- const context = initSecurityManagerContext();
622
-
623
- const plaintextKey: SecManKey = {contents: Buffer.alloc(0)};
624
-
625
- if (nvm3Key === NVM3KEY_STACK_KEYS) {
626
- // typedef struct {
627
- // uint8_t networkKey[16]; // ignored if using Secure Key Storage (but moved to PSA and cleared if upgrade code is run)
628
- // uint8_t activeKeySeqNum;
629
- // } tokTypeStackKeys;
630
-
631
- context.coreKeyType = SecManKeyType.NETWORK;
632
- context.keyIndex = 0;
633
- plaintextKey.contents = tokenData.data.subarray(0, EMBER_ENCRYPTION_KEY_SIZE); // at beginning
634
-
635
- status = await ezsp.ezspImportKey(context, plaintextKey);
636
- } else if (nvm3Key === NVM3KEY_STACK_ALTERNATE_KEY) {
637
- // typedef struct {
638
- // uint8_t networkKey[16]; // ignored if using Secure Key Storage (but moved to PSA and cleared if upgrade code is run)
639
- // uint8_t activeKeySeqNum;
640
- // } tokTypeStackKeys;
641
-
642
- context.coreKeyType = SecManKeyType.NETWORK;
643
- context.keyIndex = 1;
644
- plaintextKey.contents = tokenData.data.subarray(0, EMBER_ENCRYPTION_KEY_SIZE); // at beginning
645
-
646
- status = await ezsp.ezspImportKey(context, plaintextKey);
647
- } else if (nvm3Key === NVM3KEY_STACK_TRUST_CENTER) {
648
- // typedef struct {
649
- // uint16_t mode;
650
- // uint8_t eui64[8];
651
- // uint8_t key[16]; // ignored if (mode & TRUST_CENTER_KEY_LIVES_IN_PSA)
652
- // } tokTypeStackTrustCenter;
653
-
654
- context.coreKeyType = SecManKeyType.TC_LINK;
655
- const s = 2 + EUI64_SIZE;
656
- plaintextKey.contents = tokenData.data.subarray(s, s + EMBER_ENCRYPTION_KEY_SIZE); // uint16_t+uint8_t[8]
657
-
658
- status = await ezsp.ezspImportKey(context, plaintextKey);
659
- } else if (nvm3Key === NVM3KEY_STACK_KEY_TABLE) {
660
- // typedef uint8_t tokTypeStackKeyTable[25];
661
-
662
- context.coreKeyType = SecManKeyType.APP_LINK;
663
- context.keyIndex = index;
664
- context.flags |= SecManFlag.KEY_INDEX_IS_VALID;
665
- plaintextKey.contents = tokenData.data.subarray(KEY_ENTRY_KEY_DATA_OFFSET, KEY_ENTRY_KEY_DATA_OFFSET + EMBER_ENCRYPTION_KEY_SIZE); // end part of uint8_t[25]
666
-
667
- status = await ezsp.ezspImportKey(context, plaintextKey);
668
- } else if (nvm3Key === NVM3KEY_STACK_GP_PROXY_TABLE) {
669
- // typedef struct {
670
- // uint8_t status;
671
- // uint32_t options;
672
- // //EmberGpAddress gpd;
673
- // uint8_t gpAddress[8];
674
- // uint8_t endpoint;
675
- // //uint16_t assignedAlias;
676
- // uint8_t securityOptions;
677
- // uint8_t gpdKey[16]; // ignored if using Secure Key Storage (but moved to PSA and cleared if upgrade code is run)
678
- // //EmberGpSinkListEntry sinkList[2];
679
- // uint8_t sinkType[2];
680
- // uint8_t sinkEUI[2][8];
681
- // //uint16_t sinkNodeId[2];
682
- // } tokTypeStackGpProxyTableEntry;
683
-
684
- context.coreKeyType = SecManKeyType.GREEN_POWER_PROXY_TABLE_KEY;
685
- context.keyIndex = index;
686
- const s = 1 + 4 + 8 + 1 + 1;
687
- plaintextKey.contents = tokenData.data.subarray(s, s + EMBER_ENCRYPTION_KEY_SIZE); // uint8_t+uint32_t+uint8_t[8]+uint8_t+uint8_t
688
-
689
- status = await ezsp.ezspImportKey(context, plaintextKey);
690
- } else if (nvm3Key === NVM3KEY_STACK_GP_SINK_TABLE) {
691
- // typedef struct {
692
- // uint8_t status;
693
- // uint16_t options;
694
- // //EmberGpAddress gpd;
695
- // uint8_t gpAddress[8];
696
- // uint8_t endpoint;
697
- // uint8_t securityOptions;
698
- // uint8_t gpdKey[16]; // ignored if using Secure Key Storage (but moved to PSA and cleared if upgrade code is run)
699
- // uint8_t sinkType[2];
700
- // uint16_t groupList[2][2];
701
- // uint32_t securityFrameCounter; // This is no more used, Incoming FC for gpd in a separate Token to control its update.
702
- // uint16_t assignedAlias;
703
- // uint8_t deviceId;
704
- // uint8_t groupcastRadius;
705
- // } tokTypeStackGpSinkTableEntry;
706
-
707
- context.coreKeyType = SecManKeyType.GREEN_POWER_SINK_TABLE_KEY;
708
- context.keyIndex = index;
709
- const s = 1 + 2 + 8 + 1 + 1;
710
- plaintextKey.contents = tokenData.data.subarray(s, s + EMBER_ENCRYPTION_KEY_SIZE); // uint8_t+uint16_t+uint8_t[8]+uint8_t+uint8_t
711
-
712
- status = await ezsp.ezspImportKey(context, plaintextKey);
713
- } else if (nvm3Key === NVM3KEY_STACK_ZLL_SECURITY) {
714
- // typedef struct {
715
- // uint32_t bitmask;
716
- // uint8_t keyIndex;
717
- // uint8_t encryptionKey[EMBER_ENCRYPTION_KEY_SIZE];
718
- // uint8_t preconfiguredKey[EMBER_ENCRYPTION_KEY_SIZE];
719
- // } EmberTokTypeStackZllSecurity;
720
-
721
- context.coreKeyType = SecManKeyType.ZLL_ENCRYPTION_KEY;
722
- let s = 4 + 1;
723
- plaintextKey.contents = tokenData.data.subarray(s, s + EMBER_ENCRYPTION_KEY_SIZE); // uint32_t+uint8_t
724
-
725
- status = await ezsp.ezspImportKey(context, plaintextKey);
726
-
727
- context.coreKeyType = SecManKeyType.ZLL_PRECONFIGURED_KEY;
728
- s += EMBER_ENCRYPTION_KEY_SIZE; // after `encryptionKey`
729
- plaintextKey.contents = tokenData.data.subarray(s, s + EMBER_ENCRYPTION_KEY_SIZE); // uint32_t+uint8_t+uint8_t[EMBER_ENCRYPTION_KEY_SIZE]
730
-
731
- status = await ezsp.ezspImportKey(context, plaintextKey);
732
- } else {
733
- // unknown key
734
- }
735
-
736
- return status;
737
- }
738
-
739
- /**
740
- * Updates zigbeed tokens from a backup of NCP tokens.
741
- *
742
- * @return SLStatus status code
743
- */
744
- public static async writeNcpTokensToZigbeedTokens(ezsp: Ezsp, inBuffer: Buffer): Promise<SLStatus> {
745
- if (!inBuffer?.length) {
746
- throw new Error("[TOKENS] Restore tokens buffer empty.");
747
- }
748
-
749
- logger.info("[TOKENS] Restoring tokens to Zigbeed...", NS);
750
-
751
- let readOffset = 0;
752
- const inTokenCount = inBuffer.readUInt8(readOffset++);
753
-
754
- for (let i = 0; i < inTokenCount; i++) {
755
- const nvm3Key = inBuffer.readUInt32LE(readOffset); // 4 bytes Token Key/Creator
756
- readOffset += 4;
757
- const size = inBuffer.readUInt8(readOffset++); // 1 byte token size
758
- const arraySize = inBuffer.readUInt8(readOffset++); // 1 byte array size.
759
-
760
- for (let arrayIndex = 0; arrayIndex < arraySize; arrayIndex++) {
761
- const tokenData: EmberTokenData = {
762
- data: inBuffer.subarray(readOffset, readOffset + size),
763
- size,
764
- };
765
-
766
- const creator = EmberTokensManager.getCreatorFromNvm3Key(nvm3Key); // uint16_t
767
- const status = await ezsp.ezspSetTokenData(creator, arrayIndex, tokenData);
768
-
769
- if (status !== SLStatus.OK) {
770
- logger.error(
771
- `[TOKENS] Failed to set Zigbeed token data for key "${nvm3Key}" creator "${creator}" with status=${SLStatus[status]}.`,
772
- NS,
773
- );
774
- }
775
-
776
- readOffset += tokenData.size;
777
- }
778
- }
779
-
780
- return SLStatus.OK;
781
- }
782
- }