zigbee-herdsman 6.0.2 → 6.0.3

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 +7 -0
  2. package/package.json +12 -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,1374 +0,0 @@
1
- import {describe, expect, it, vi} from "vitest";
2
- import * as Zcl from "../../../src/zspec/zcl";
3
- import {BuffaloZcl} from "../../../src/zspec/zcl/buffaloZcl";
4
- import {uint16To8Array, uint32To8Array} from "../../utils/math";
5
-
6
- describe("ZCL Buffalo", () => {
7
- it("Writes invalida data type as buffer if buffer-like", () => {
8
- const value = [1, 2, 3];
9
- const buffer = Buffer.alloc(3);
10
- const buffalo = new BuffaloZcl(buffer);
11
- const writeSpy = vi.spyOn(buffalo, "writeBuffer");
12
- // @ts-expect-error invalid on purpose
13
- buffalo.write(99999, value, {});
14
- expect(writeSpy).toHaveBeenCalledWith(value, value.length);
15
- expect(writeSpy).toHaveBeenCalledTimes(1);
16
-
17
- buffalo.setPosition(0);
18
-
19
- // @ts-expect-error invalid on purpose
20
- buffalo.write(99999, Buffer.from(value), {});
21
- expect(writeSpy).toHaveBeenLastCalledWith(Buffer.from(value), value.length);
22
- expect(writeSpy).toHaveBeenCalledTimes(2);
23
- });
24
-
25
- it("Throws when write/read invalid data type, except for write if buffer-like", () => {
26
- expect(() => {
27
- const buffer = Buffer.alloc(1);
28
- const buffalo = new BuffaloZcl(buffer);
29
- // @ts-expect-error invalid on purpose
30
- buffalo.write(99999, 127, {});
31
- }).toThrow();
32
- expect(() => {
33
- const buffer = Buffer.alloc(1);
34
- const buffalo = new BuffaloZcl(buffer);
35
- // @ts-expect-error invalid on purpose
36
- buffalo.read(99999, {});
37
- }).toThrow();
38
- });
39
-
40
- it("Writes nothing", () => {
41
- for (const type of [Zcl.DataType.NO_DATA, Zcl.DataType.UNKNOWN]) {
42
- const buffer = Buffer.alloc(3);
43
- const buffalo = new BuffaloZcl(buffer);
44
-
45
- buffalo.write(type, 123, {});
46
- expect(buffer).toStrictEqual(Buffer.from([0, 0, 0]));
47
- expect(buffalo.getPosition()).toStrictEqual(0);
48
- }
49
-
50
- {
51
- const buffer = Buffer.alloc(3);
52
- const buffalo = new BuffaloZcl(buffer);
53
-
54
- buffalo.write(Zcl.BuffaloZclDataType.STRUCTURED_SELECTOR, null, {});
55
- expect(buffer).toStrictEqual(Buffer.from([0, 0, 0]));
56
- expect(buffalo.getPosition()).toStrictEqual(0);
57
- }
58
- });
59
-
60
- it("Reads nothing", () => {
61
- for (const type of [Zcl.DataType.NO_DATA, Zcl.DataType.UNKNOWN]) {
62
- const buffer = Buffer.from([1, 2]);
63
- const buffalo = new BuffaloZcl(buffer);
64
- expect(buffalo.read(type, {})).toStrictEqual(undefined);
65
- expect(buffalo.getPosition()).toStrictEqual(0);
66
- }
67
- });
68
-
69
- it.each([
70
- ["boolean", {value: 1, types: [Zcl.DataType.BOOLEAN]}, {position: 1, write: "writeUInt8", read: "readUInt8"}],
71
- ["boolean", {value: 0, types: [Zcl.DataType.BOOLEAN]}, {position: 1, write: "writeUInt8", read: "readUInt8"}],
72
- [
73
- "uint8-like",
74
- {value: 250, types: [Zcl.DataType.DATA8, Zcl.DataType.BITMAP8, Zcl.DataType.UINT8, Zcl.DataType.ENUM8]},
75
- {position: 1, write: "writeUInt8", read: "readUInt8"},
76
- ],
77
- [
78
- "uint16-like",
79
- {
80
- value: 65530,
81
- types: [
82
- Zcl.DataType.DATA16,
83
- Zcl.DataType.BITMAP16,
84
- Zcl.DataType.UINT16,
85
- Zcl.DataType.ENUM16,
86
- Zcl.DataType.CLUSTER_ID,
87
- Zcl.DataType.ATTR_ID,
88
- ],
89
- },
90
- {position: 2, write: "writeUInt16", read: "readUInt16"},
91
- ],
92
- [
93
- "uint24-like",
94
- {value: 16777210, types: [Zcl.DataType.DATA24, Zcl.DataType.BITMAP24, Zcl.DataType.UINT24]},
95
- {position: 3, write: "writeUInt24", read: "readUInt24"},
96
- ],
97
- [
98
- "uint32-like",
99
- {value: 4294967290, types: [Zcl.DataType.DATA32, Zcl.DataType.BITMAP32, Zcl.DataType.UINT32, Zcl.DataType.UTC, Zcl.DataType.BAC_OID]},
100
- {position: 4, write: "writeUInt32", read: "readUInt32"},
101
- ],
102
- ["int8-like", {value: -120, types: [Zcl.DataType.INT8]}, {position: 1, write: "writeInt8", read: "readInt8"}],
103
- ["int16-like", {value: -32760, types: [Zcl.DataType.INT16]}, {position: 2, write: "writeInt16", read: "readInt16"}],
104
- ["int24-like", {value: -8388600, types: [Zcl.DataType.INT24]}, {position: 3, write: "writeInt24", read: "readInt24"}],
105
- ["int32-like", {value: -2147483640, types: [Zcl.DataType.INT32]}, {position: 4, write: "writeInt32", read: "readInt32"}],
106
- ["int48-like", {value: -140737488355320, types: [Zcl.DataType.INT48]}, {position: 6, write: "writeInt48", read: "readInt48"}],
107
- ["float-like", {value: 1.539989614439558e-36, types: [Zcl.DataType.SINGLE_PREC]}, {position: 4, write: "writeFloatLE", read: "readFloatLE"}],
108
- [
109
- "double-like",
110
- {value: 5.447603722011605e-270, types: [Zcl.DataType.DOUBLE_PREC]},
111
- {position: 8, write: "writeDoubleLE", read: "readDoubleLE"},
112
- ],
113
- ["IEEE address", {value: "0xfe1234abcd9876ff", types: [Zcl.DataType.IEEE_ADDR]}, {position: 8, write: "writeIeeeAddr", read: "readIeeeAddr"}],
114
- [
115
- "uint8-like list",
116
- {value: [250, 25, 50], types: [Zcl.BuffaloZclDataType.LIST_UINT8]},
117
- {position: 3, write: "writeListUInt8", read: "readListUInt8"},
118
- ],
119
- [
120
- "uint16-like list",
121
- {value: [65530, 6553, 5530], types: [Zcl.BuffaloZclDataType.LIST_UINT16]},
122
- {position: 6, write: "writeListUInt16", read: "readListUInt16"},
123
- ],
124
- [
125
- "uint24-like list",
126
- {value: [16777210, 1677721, 6777210], types: [Zcl.BuffaloZclDataType.LIST_UINT24]},
127
- {position: 9, write: "writeListUInt24", read: "readListUInt24"},
128
- ],
129
- [
130
- "uint32-like list",
131
- {value: [4294967290, 429496729, 294967290], types: [Zcl.BuffaloZclDataType.LIST_UINT32]},
132
- {position: 12, write: "writeListUInt32", read: "readListUInt32"},
133
- ],
134
- [
135
- "buffer",
136
- {value: Buffer.from([1, 2, 3, 4]), types: [Zcl.BuffaloZclDataType.BUFFER]},
137
- {position: 4, write: "writeBuffer", read: "readBuffer"},
138
- ],
139
- [
140
- "security key",
141
- {value: Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]), types: [Zcl.DataType.SEC_KEY]},
142
- {position: 16, write: "writeBuffer", read: "readBuffer"},
143
- ],
144
- ])("Writes & Reads using base class for %s", (_name, payload, expected) => {
145
- const readOptions: {length?: number} = {};
146
-
147
- if (Array.isArray(payload.value) || payload.value instanceof Buffer) {
148
- readOptions.length = payload.value.length;
149
- }
150
-
151
- for (const type of payload.types) {
152
- const buffer = Buffer.alloc(255);
153
- const buffalo = new BuffaloZcl(buffer);
154
- const writeSpy = vi.spyOn(buffalo, expected.write as keyof BuffaloZcl);
155
- const readSpy = vi.spyOn(buffalo, expected.read as keyof BuffaloZcl);
156
-
157
- buffalo.write(type, payload.value, {});
158
- expect(writeSpy).toHaveBeenCalledTimes(1);
159
- expect(buffalo.getPosition()).toStrictEqual(expected.position);
160
-
161
- buffalo.setPosition(0);
162
-
163
- expect(buffalo.read(type, readOptions)).toStrictEqual(payload.value);
164
- expect(readSpy).toHaveBeenCalledTimes(1);
165
- expect(buffalo.getPosition()).toStrictEqual(expected.position);
166
- }
167
- });
168
-
169
- it.each([
170
- ["boolean", {value: Number.NaN, types: [Zcl.DataType.BOOLEAN]}, {written: 0xff, position: 1, write: "writeUInt8", read: "readUInt8"}],
171
- [
172
- "uint8-like",
173
- {value: Number.NaN, types: [Zcl.DataType.DATA8, Zcl.DataType.BITMAP8, Zcl.DataType.UINT8, Zcl.DataType.ENUM8]},
174
- {written: 0xff, position: 1, write: "writeUInt8", read: "readUInt8"},
175
- ],
176
- [
177
- "uint16-like",
178
- {
179
- value: Number.NaN,
180
- types: [
181
- Zcl.DataType.DATA16,
182
- Zcl.DataType.BITMAP16,
183
- Zcl.DataType.UINT16,
184
- Zcl.DataType.ENUM16,
185
- Zcl.DataType.CLUSTER_ID,
186
- Zcl.DataType.ATTR_ID,
187
- ],
188
- },
189
- {written: 0xffff, position: 2, write: "writeUInt16", read: "readUInt16"},
190
- ],
191
- [
192
- "uint24-like",
193
- {value: Number.NaN, types: [Zcl.DataType.DATA24, Zcl.DataType.BITMAP24, Zcl.DataType.UINT24]},
194
- {written: 0xffffff, position: 3, write: "writeUInt24", read: "readUInt24"},
195
- ],
196
- [
197
- "uint32-like",
198
- {value: Number.NaN, types: [Zcl.DataType.DATA32, Zcl.DataType.BITMAP32, Zcl.DataType.UINT32, Zcl.DataType.UTC, Zcl.DataType.BAC_OID]},
199
- {written: 0xffffffff, position: 4, write: "writeUInt32", read: "readUInt32"},
200
- ],
201
- [
202
- "uint40-like",
203
- {value: Number.NaN, types: [Zcl.DataType.DATA40, Zcl.DataType.BITMAP40, Zcl.DataType.UINT40]},
204
- {written: 0xffffffffff, position: 5, write: "writeUInt40", read: "readUInt40"},
205
- ],
206
- [
207
- "uint48-like",
208
- {value: Number.NaN, types: [Zcl.DataType.DATA48, Zcl.DataType.BITMAP48, Zcl.DataType.UINT48]},
209
- {written: 0xffffffffffff, position: 6, write: "writeUInt48", read: "readUInt48"},
210
- ],
211
- [
212
- "uint56-like",
213
- {value: undefined, types: [Zcl.DataType.DATA56, Zcl.DataType.BITMAP56, Zcl.DataType.UINT56]},
214
- {written: 0xffffffffffffffn, position: 7, write: "writeUInt56", read: "readUInt56"},
215
- ],
216
- [
217
- "uint64-like",
218
- {value: undefined, types: [Zcl.DataType.DATA64, Zcl.DataType.BITMAP64, Zcl.DataType.UINT64]},
219
- {written: 0xffffffffffffffffn, position: 8, write: "writeUInt64", read: "readUInt64"},
220
- ],
221
- [
222
- "octectStr",
223
- {value: undefined, types: [Zcl.DataType.OCTET_STR]},
224
- {written: 0xff, valueRead: Buffer.from([]), position: 1, write: "writeUInt8", read: "readUInt8"},
225
- ],
226
- [
227
- "longOctectStr",
228
- {value: undefined, types: [Zcl.DataType.LONG_OCTET_STR]},
229
- {written: 0xffff, valueRead: Buffer.from([]), position: 2, write: "writeUInt16", read: "readUInt16"},
230
- ],
231
- [
232
- "charStr",
233
- {value: undefined, types: [Zcl.DataType.CHAR_STR]},
234
- {written: 0xff, valueRead: "", position: 1, write: "writeUInt8", read: "readUInt8"},
235
- ],
236
- [
237
- "longCharStr",
238
- {value: undefined, types: [Zcl.DataType.LONG_CHAR_STR]},
239
- {written: 0xffff, valueRead: "", position: 2, write: "writeUInt16", read: "readUInt16"},
240
- ],
241
- [
242
- "array",
243
- {value: undefined, types: [Zcl.DataType.ARRAY]},
244
- {written: 0xffff00, valueRead: [], position: 3, write: "writeUInt16", read: "readUInt16"},
245
- ],
246
- [
247
- "struct",
248
- {value: undefined, types: [Zcl.DataType.STRUCT]},
249
- {written: 0xffff, valueRead: [], position: 2, write: "writeUInt16", read: "readUInt16"},
250
- ],
251
- ])("Writes & Reads non-value for %s", (_name, payload, expected) => {
252
- for (const type of payload.types) {
253
- const buffer = Buffer.alloc(255);
254
- const buffalo = new BuffaloZcl(buffer);
255
- const writeSpy = vi.spyOn(buffalo, expected.write as keyof BuffaloZcl);
256
- const readSpy = vi.spyOn(buffalo, expected.read as keyof BuffaloZcl);
257
-
258
- buffalo.write(type, payload.value, {});
259
- expect(writeSpy).toHaveBeenCalledTimes(1);
260
- expect(buffalo.getPosition()).toStrictEqual(expected.position);
261
- const expectedWrittenBuf = Buffer.alloc(expected.position);
262
-
263
- if (typeof expected.written === "bigint") {
264
- if (expected.position === 7) {
265
- const unsignedValue = expected.written < 0n ? (1n << 56n) + expected.written : expected.written;
266
- expectedWrittenBuf.writeUIntLE(Number(unsignedValue & 0xffffffffffffn), 0, 6);
267
- expectedWrittenBuf.writeUInt8(Number(unsignedValue >> 48n), 0 + 6);
268
- } else {
269
- expectedWrittenBuf.writeBigUInt64LE(expected.written, 0);
270
- }
271
- } else {
272
- expectedWrittenBuf.writeUIntLE(expected.written, 0, expected.position);
273
- }
274
-
275
- expect(buffalo.getWritten()).toStrictEqual(expectedWrittenBuf);
276
-
277
- buffalo.setPosition(0);
278
-
279
- expect(buffalo.read(type, {})).toStrictEqual("valueRead" in expected ? expected.valueRead : payload.value);
280
- expect(readSpy).toHaveBeenCalledTimes(1);
281
- expect(buffalo.getPosition()).toStrictEqual(expected.position);
282
- }
283
- });
284
-
285
- it.each([
286
- ["int8-like", {value: Number.NaN, type: Zcl.DataType.INT8}, {written: -0x80, position: 1, write: "writeInt8", read: "readInt8"}],
287
- ["int16-like", {value: Number.NaN, type: Zcl.DataType.INT16}, {written: -0x8000, position: 2, write: "writeInt16", read: "readInt16"}],
288
- ["int24-like", {value: Number.NaN, type: Zcl.DataType.INT24}, {written: -0x800000, position: 3, write: "writeInt24", read: "readInt24"}],
289
- ["int32-like", {value: Number.NaN, type: Zcl.DataType.INT32}, {written: -0x80000000, position: 4, write: "writeInt32", read: "readInt32"}],
290
- ["int40-like", {value: Number.NaN, type: Zcl.DataType.INT40}, {written: -0x8000000000, position: 5, write: "writeInt40", read: "readInt40"}],
291
- [
292
- "int48-like",
293
- {value: Number.NaN, type: Zcl.DataType.INT48},
294
- {written: -0x800000000000, position: 6, write: "writeInt48", read: "readInt48"},
295
- ],
296
- [
297
- "int56-like",
298
- {value: undefined, type: Zcl.DataType.INT56},
299
- {written: -0x80000000000000n, position: 7, write: "writeInt56", read: "readInt56"},
300
- ],
301
- [
302
- "int64-like",
303
- {value: undefined, type: Zcl.DataType.INT64},
304
- {written: -0x8000000000000000n, position: 8, write: "writeInt64", read: "readInt64"},
305
- ],
306
- ])("Writes & Reads signed non-value for %s", (_name, payload, expected) => {
307
- const buffer = Buffer.alloc(255);
308
- const buffalo = new BuffaloZcl(buffer);
309
- const writeSpy = vi.spyOn(buffalo, expected.write as keyof BuffaloZcl);
310
- const readSpy = vi.spyOn(buffalo, expected.read as keyof BuffaloZcl);
311
-
312
- buffalo.write(payload.type, payload.value, {});
313
- expect(writeSpy).toHaveBeenCalledTimes(1);
314
- expect(buffalo.getPosition()).toStrictEqual(expected.position);
315
- const expectedWrittenBuf = Buffer.alloc(expected.position);
316
-
317
- if (typeof expected.written === "bigint") {
318
- if (expected.position === 7) {
319
- const unsignedValue = expected.written < 0n ? (1n << 56n) + expected.written : expected.written;
320
- expectedWrittenBuf.writeUIntLE(Number(unsignedValue & 0xffffffffffffn), 0, 6);
321
- expectedWrittenBuf.writeUInt8(Number(unsignedValue >> 48n), 0 + 6);
322
- } else {
323
- expectedWrittenBuf.writeBigInt64LE(expected.written, 0);
324
- }
325
- } else {
326
- expectedWrittenBuf.writeIntLE(expected.written, 0, expected.position);
327
- }
328
-
329
- expect(buffalo.getWritten()).toStrictEqual(expectedWrittenBuf);
330
-
331
- buffalo.setPosition(0);
332
-
333
- expect(buffalo.read(payload.type, {})).toStrictEqual("valueRead" in expected ? expected.valueRead : payload.value);
334
- expect(readSpy).toHaveBeenCalledTimes(1);
335
- expect(buffalo.getPosition()).toStrictEqual(expected.position);
336
- });
337
-
338
- it("Reads whole buffer without length option", () => {
339
- const value = [1, 2, 3, 4];
340
- const buffer = Buffer.alloc(4);
341
- const buffalo = new BuffaloZcl(buffer);
342
- const writeSpy = vi.spyOn(buffalo, "writeBuffer");
343
- const readSpy = vi.spyOn(buffalo, "readBuffer");
344
-
345
- buffalo.write(Zcl.BuffaloZclDataType.BUFFER, Buffer.from(value), {});
346
- expect(writeSpy).toHaveBeenCalledTimes(1);
347
- // XXX: inconsistent with read, write is always "whole"
348
- expect(writeSpy).toHaveBeenCalledWith(Buffer.from(value), value.length);
349
- expect(buffalo.getPosition()).toStrictEqual(value.length);
350
-
351
- buffalo.setPosition(0);
352
-
353
- expect(buffalo.read(Zcl.BuffaloZclDataType.BUFFER, {})).toStrictEqual(Buffer.from(value));
354
- expect(readSpy).toHaveBeenCalledTimes(1);
355
- expect(readSpy).toHaveBeenCalledWith(value.length);
356
- expect(buffalo.getPosition()).toStrictEqual(value.length);
357
- });
358
-
359
- it("Reads partial buffer with length option", () => {
360
- const value = [1, 2, 3, 4];
361
- const length = 2;
362
- const buffer = Buffer.alloc(255);
363
- const buffalo = new BuffaloZcl(buffer);
364
- const writeSpy = vi.spyOn(buffalo, "writeBuffer");
365
- const readSpy = vi.spyOn(buffalo, "readBuffer");
366
-
367
- buffalo.write(Zcl.BuffaloZclDataType.BUFFER, Buffer.from(value), {length});
368
- expect(writeSpy).toHaveBeenCalledTimes(1);
369
- // XXX: inconsistent with read, write is always "whole"
370
- expect(writeSpy).toHaveBeenCalledWith(Buffer.from(value), value.length);
371
- expect(buffalo.getPosition()).toStrictEqual(value.length);
372
-
373
- buffalo.setPosition(0);
374
-
375
- expect(buffalo.read(Zcl.BuffaloZclDataType.BUFFER, {length})).toStrictEqual(Buffer.from([value[0], value[1]]));
376
- expect(readSpy).toHaveBeenCalledTimes(1);
377
- expect(readSpy).toHaveBeenCalledWith(length);
378
- expect(buffalo.getPosition()).toStrictEqual(length);
379
- });
380
-
381
- it("Writes & Reads octet str", () => {
382
- const value = [0xfe, 0x01, 0xab, 0x98];
383
- const expectedPosition = 5;
384
- const buffer = Buffer.alloc(10);
385
- const buffalo = new BuffaloZcl(buffer);
386
- buffalo.write(Zcl.DataType.OCTET_STR, value, {});
387
- expect(buffalo.getPosition()).toStrictEqual(expectedPosition);
388
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from([value.length, ...value]));
389
-
390
- buffalo.setPosition(0);
391
-
392
- expect(buffalo.read(Zcl.DataType.OCTET_STR, {})).toStrictEqual(Buffer.from(value));
393
- expect(buffalo.getPosition()).toStrictEqual(expectedPosition);
394
- });
395
-
396
- it("Writes & Reads long octet str", () => {
397
- const value = [0xfe, 0x01, 0xab, 0x98];
398
- const expectedPosition = 6;
399
- const buffer = Buffer.alloc(10);
400
- const buffalo = new BuffaloZcl(buffer);
401
- buffalo.write(Zcl.DataType.LONG_OCTET_STR, value, {});
402
- expect(buffalo.getPosition()).toStrictEqual(expectedPosition);
403
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from([value.length, 0 /*length uint16*/, ...value]));
404
-
405
- buffalo.setPosition(0);
406
-
407
- expect(buffalo.read(Zcl.DataType.LONG_OCTET_STR, {})).toStrictEqual(Buffer.from(value));
408
- expect(buffalo.getPosition()).toStrictEqual(expectedPosition);
409
- });
410
-
411
- it("Writes char str from number array", () => {
412
- const value = [0x61, 0x62, 0x63, 0x64];
413
- const expectedPosition = 4; // value.length not written when number array given
414
- const buffer = Buffer.alloc(10);
415
- const buffalo = new BuffaloZcl(buffer);
416
- buffalo.write(Zcl.DataType.CHAR_STR, value, {});
417
- expect(buffalo.getPosition()).toStrictEqual(expectedPosition);
418
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(value)); // see above comment
419
- });
420
-
421
- it("Writes & Reads char str from string", () => {
422
- const value = "abcd";
423
- const expectedValue = [value.length, 0x61, 0x62, 0x63, 0x64];
424
- const expectedPosition = 5;
425
- const buffer = Buffer.alloc(10);
426
- const buffalo = new BuffaloZcl(buffer);
427
- buffalo.write(Zcl.DataType.CHAR_STR, value, {});
428
- expect(buffalo.getPosition()).toStrictEqual(expectedPosition);
429
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expectedValue));
430
-
431
- buffalo.setPosition(0);
432
-
433
- expect(buffalo.read(Zcl.DataType.CHAR_STR, {})).toStrictEqual(value);
434
- expect(buffalo.getPosition()).toStrictEqual(expectedPosition);
435
- });
436
-
437
- it("[workaround] Reads char str as Mi struct for Xiaomi attridId=65281", () => {
438
- const expectedValue = {"1": 3285, "3": 33, "4": 5032, "5": 43, "6": 327680, "8": 516, "10": 0, "100": 0};
439
- const buffer = Buffer.from([
440
- 34,
441
- 1,
442
- Zcl.DataType.UINT16,
443
- ...uint16To8Array(3285),
444
- 3,
445
- Zcl.DataType.INT8,
446
- 33,
447
- 4,
448
- Zcl.DataType.UINT16,
449
- ...uint16To8Array(5032),
450
- 5,
451
- Zcl.DataType.UINT16,
452
- ...uint16To8Array(43),
453
- 6,
454
- Zcl.DataType.UINT40,
455
- ...uint32To8Array(327680),
456
- 0,
457
- 8,
458
- Zcl.DataType.UINT16,
459
- ...uint16To8Array(516),
460
- 10,
461
- Zcl.DataType.UINT16,
462
- ...uint16To8Array(0),
463
- 100,
464
- Zcl.DataType.BOOLEAN,
465
- 0,
466
- ]);
467
- const buffalo = new BuffaloZcl(buffer);
468
-
469
- expect(buffalo.read(Zcl.BuffaloZclDataType.MI_STRUCT, {})).toStrictEqual(expectedValue);
470
- expect(buffalo.getPosition()).toStrictEqual(buffer.length);
471
- });
472
-
473
- it("Writes & Reads long char str", () => {
474
- const value = "abcd";
475
- const expectedValue = [value.length, 0 /*length uint16*/, 0x61, 0x62, 0x63, 0x64];
476
- const expectedPosition = 6;
477
- const buffer = Buffer.alloc(10);
478
- const buffalo = new BuffaloZcl(buffer);
479
- buffalo.write(Zcl.DataType.LONG_CHAR_STR, value, {});
480
- expect(buffalo.getPosition()).toStrictEqual(expectedPosition);
481
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expectedValue));
482
-
483
- buffalo.setPosition(0);
484
-
485
- expect(buffalo.read(Zcl.DataType.LONG_CHAR_STR, {})).toStrictEqual(value);
486
- expect(buffalo.getPosition()).toStrictEqual(expectedPosition);
487
- });
488
-
489
- it.each([
490
- [
491
- "uint16",
492
- {value: {elementType: Zcl.DataType.UINT16, elements: [256, 1, 65530, 0]}},
493
- {
494
- written: [
495
- Zcl.DataType.UINT16,
496
- 4,
497
- 0 /*length uint16*/,
498
- ...uint16To8Array(256),
499
- ...uint16To8Array(1),
500
- ...uint16To8Array(65530),
501
- ...uint16To8Array(0),
502
- ],
503
- },
504
- ],
505
- [
506
- "char str",
507
- {value: {elementType: Zcl.DataType.CHAR_STR, elements: ["abcd", "cd", "a"]}},
508
- {written: [Zcl.DataType.CHAR_STR, 3, 0 /*length uint16*/, 4, 0x61, 0x62, 0x63, 0x64, 2, 0x63, 0x64, 1, 0x61]},
509
- ],
510
- [
511
- "uint16 with element type passed as string key of Zcl.DataType",
512
- {value: {elementType: "UINT16", elements: [256, 1, 65530, 0]}},
513
- {
514
- written: [
515
- Zcl.DataType.UINT16,
516
- 4,
517
- 0 /*length uint16*/,
518
- ...uint16To8Array(256),
519
- ...uint16To8Array(1),
520
- ...uint16To8Array(65530),
521
- ...uint16To8Array(0),
522
- ],
523
- },
524
- ],
525
- ])("Writes & Reads array of %s", (_name, payload, expected) => {
526
- const buffer = Buffer.alloc(50);
527
- const buffalo = new BuffaloZcl(buffer);
528
- buffalo.write(Zcl.DataType.ARRAY, payload.value, {});
529
- expect(buffalo.getPosition()).toStrictEqual(expected.written.length);
530
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expected.written));
531
-
532
- buffalo.setPosition(0);
533
-
534
- expect(buffalo.read(Zcl.DataType.ARRAY, {})).toStrictEqual(payload.value.elements);
535
- expect(buffalo.getPosition()).toStrictEqual(expected.written.length);
536
- });
537
-
538
- it.each([
539
- [
540
- "uint16",
541
- {
542
- value: [
543
- {elmType: Zcl.DataType.UINT16, elmVal: 256},
544
- {elmType: Zcl.DataType.UINT16, elmVal: 1},
545
- {elmType: Zcl.DataType.UINT16, elmVal: 65530},
546
- {elmType: Zcl.DataType.UINT16, elmVal: 0},
547
- ],
548
- },
549
- {
550
- written: [
551
- 4,
552
- 0 /*length uint16*/,
553
- Zcl.DataType.UINT16,
554
- ...uint16To8Array(256),
555
- Zcl.DataType.UINT16,
556
- ...uint16To8Array(1),
557
- Zcl.DataType.UINT16,
558
- ...uint16To8Array(65530),
559
- Zcl.DataType.UINT16,
560
- ...uint16To8Array(0),
561
- ],
562
- },
563
- ],
564
- [
565
- "char str",
566
- {
567
- value: [
568
- {elmType: Zcl.DataType.CHAR_STR, elmVal: "abcd"},
569
- {elmType: Zcl.DataType.CHAR_STR, elmVal: "cd"},
570
- {elmType: Zcl.DataType.CHAR_STR, elmVal: "a"},
571
- ],
572
- },
573
- {
574
- written: [
575
- 3,
576
- 0 /*length uint16*/,
577
- Zcl.DataType.CHAR_STR,
578
- 4,
579
- 0x61,
580
- 0x62,
581
- 0x63,
582
- 0x64,
583
- Zcl.DataType.CHAR_STR,
584
- 2,
585
- 0x63,
586
- 0x64,
587
- Zcl.DataType.CHAR_STR,
588
- 1,
589
- 0x61,
590
- ],
591
- },
592
- ],
593
- [
594
- "mixed",
595
- {
596
- value: [
597
- {elmType: Zcl.DataType.UINT16, elmVal: 256},
598
- {elmType: Zcl.DataType.CHAR_STR, elmVal: "abcd"},
599
- {elmType: Zcl.DataType.BITMAP8, elmVal: 3},
600
- ],
601
- },
602
- {
603
- written: [
604
- 3,
605
- 0 /*length uint16*/,
606
- Zcl.DataType.UINT16,
607
- ...uint16To8Array(256),
608
- Zcl.DataType.CHAR_STR,
609
- 4,
610
- 0x61,
611
- 0x62,
612
- 0x63,
613
- 0x64,
614
- Zcl.DataType.BITMAP8,
615
- 3,
616
- ],
617
- },
618
- ],
619
- ])("Writes & Reads struct of %s", (_name, payload, expected) => {
620
- const buffer = Buffer.alloc(50);
621
- const buffalo = new BuffaloZcl(buffer);
622
- buffalo.write(Zcl.DataType.STRUCT, payload.value, {});
623
- expect(buffalo.getPosition()).toStrictEqual(expected.written.length);
624
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expected.written));
625
-
626
- buffalo.setPosition(0);
627
-
628
- expect(buffalo.read(Zcl.DataType.STRUCT, {})).toStrictEqual(payload.value);
629
- expect(buffalo.getPosition()).toStrictEqual(expected.written.length);
630
- });
631
-
632
- it("Writes & Reads Time of Day", () => {
633
- const value = {hours: 0, minutes: 59, seconds: 34, hundredths: 88};
634
- const expectedWritten = [0, 59, 34, 88];
635
- const buffer = Buffer.alloc(10);
636
- const buffalo = new BuffaloZcl(buffer);
637
- buffalo.write(Zcl.DataType.TOD, value, {});
638
- expect(buffalo.getPosition()).toStrictEqual(expectedWritten.length);
639
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expectedWritten));
640
-
641
- buffalo.setPosition(0);
642
-
643
- expect(buffalo.read(Zcl.DataType.TOD, {})).toStrictEqual(value);
644
- expect(buffalo.getPosition()).toStrictEqual(expectedWritten.length);
645
- });
646
-
647
- it("Writes & Reads Date", () => {
648
- const value = {year: 2000, month: 8, dayOfMonth: 31, dayOfWeek: 3};
649
- const expectedWritten = [100, 8, 31, 3];
650
- const buffer = Buffer.alloc(10);
651
- const buffalo = new BuffaloZcl(buffer);
652
- buffalo.write(Zcl.DataType.DATE, value, {});
653
- expect(buffalo.getPosition()).toStrictEqual(expectedWritten.length);
654
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expectedWritten));
655
-
656
- buffalo.setPosition(0);
657
-
658
- expect(buffalo.read(Zcl.DataType.DATE, {})).toStrictEqual(value);
659
- expect(buffalo.getPosition()).toStrictEqual(expectedWritten.length);
660
- });
661
-
662
- it.each([
663
- [
664
- "time of day",
665
- {type: Zcl.DataType.TOD, position: 4, returned: {hours: Number.NaN, minutes: Number.NaN, seconds: Number.NaN, hundredths: Number.NaN}},
666
- ],
667
- [
668
- "date",
669
- {type: Zcl.DataType.DATE, position: 4, returned: {year: Number.NaN, month: Number.NaN, dayOfMonth: Number.NaN, dayOfWeek: Number.NaN}},
670
- ],
671
- ["mi struct", {type: Zcl.BuffaloZclDataType.MI_STRUCT, position: 1, returned: {}}],
672
- ])("Reads Non-Value for %s", (_name, payload) => {
673
- const buffalo = new BuffaloZcl(Buffer.alloc(50, 0xff));
674
- expect(buffalo.read(payload.type, {})).toStrictEqual(payload.returned);
675
- expect(buffalo.getPosition()).toStrictEqual(payload.position);
676
- });
677
-
678
- it.each([
679
- [
680
- "time of day",
681
- {
682
- type: Zcl.DataType.TOD,
683
- position: 4,
684
- value: {hours: Number.NaN, minutes: Number.NaN, seconds: Number.NaN, hundredths: Number.NaN},
685
- written: [0xff, 0xff, 0xff, 0xff],
686
- },
687
- ],
688
- [
689
- "date",
690
- {
691
- type: Zcl.DataType.DATE,
692
- position: 4,
693
- value: {year: Number.NaN, month: Number.NaN, dayOfMonth: Number.NaN, dayOfWeek: Number.NaN},
694
- written: [0xff, 0xff, 0xff, 0xff],
695
- },
696
- ],
697
- ])("Writes Non-Value for %s", (_name, payload) => {
698
- const buffer = Buffer.alloc(10);
699
- const buffalo = new BuffaloZcl(buffer);
700
- buffalo.write(payload.type, payload.value, {});
701
- expect(buffalo.getPosition()).toStrictEqual(payload.written.length);
702
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(payload.written));
703
- });
704
-
705
- it.each([
706
- ["time of day", {type: Zcl.DataType.TOD, value: {hours: 1, minutes: 2, seconds: Number.NaN, hundredths: 3}, written: [1, 2, 0xff, 3]}],
707
- ["date", {type: Zcl.DataType.DATE, value: {year: 1901, month: 2, dayOfMonth: Number.NaN, dayOfWeek: 3}, written: [1, 2, 0xff, 3]}],
708
- ])("Writes & Reads partial Non-Value for %s", (_name, payload) => {
709
- const buffer = Buffer.alloc(10);
710
- const buffalo = new BuffaloZcl(buffer);
711
- buffalo.write(payload.type, payload.value, {});
712
- expect(buffalo.getPosition()).toStrictEqual(payload.written.length);
713
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(payload.written));
714
-
715
- buffalo.setPosition(0);
716
-
717
- expect(buffalo.read(payload.type, {})).toStrictEqual(payload.value);
718
- expect(buffalo.getPosition()).toStrictEqual(payload.written.length);
719
- });
720
-
721
- it.each([
722
- ["uint32", {type: Zcl.DataType.UINT32, value: 32902534}, {position: 4, write: "writeUInt32", read: "readUInt32"}],
723
- ["single prec", {type: Zcl.DataType.SINGLE_PREC, value: 1.539989614439558e-36}, {position: 4, write: "writeFloatLE", read: "readFloatLE"}],
724
- ["IEEE address", {type: Zcl.DataType.IEEE_ADDR, value: "0xfe1234abcd9876ff"}, {position: 8, write: "writeIeeeAddr", read: "readIeeeAddr"}],
725
- ])("Writes & Reads Use Data Type for %s", (_name, payload, expected) => {
726
- const buffer = Buffer.alloc(255);
727
- const buffalo = new BuffaloZcl(buffer);
728
- const writeSpy = vi.spyOn(buffalo, expected.write as keyof BuffaloZcl);
729
- const readSpy = vi.spyOn(buffalo, expected.read as keyof BuffaloZcl);
730
-
731
- buffalo.write(Zcl.BuffaloZclDataType.USE_DATA_TYPE, payload.value, {dataType: payload.type});
732
- expect(writeSpy).toHaveBeenCalledTimes(1);
733
- expect(buffalo.getPosition()).toStrictEqual(expected.position);
734
-
735
- buffalo.setPosition(0);
736
-
737
- expect(buffalo.read(Zcl.BuffaloZclDataType.USE_DATA_TYPE, {dataType: payload.type})).toStrictEqual(payload.value);
738
- expect(readSpy).toHaveBeenCalledTimes(1);
739
- expect(buffalo.getPosition()).toStrictEqual(expected.position);
740
- });
741
-
742
- it("Writes & Reads Use Data Type as buffer when missing dataType option", () => {
743
- const value = [12, 34];
744
- const buffer = Buffer.alloc(2);
745
- const buffalo = new BuffaloZcl(buffer);
746
- const writeSpy = vi.spyOn(buffalo, "writeBuffer");
747
- const readSpy = vi.spyOn(buffalo, "readBuffer");
748
- buffalo.write(Zcl.BuffaloZclDataType.USE_DATA_TYPE, value, {});
749
- expect(writeSpy).toHaveBeenCalledTimes(1);
750
- expect(writeSpy).toHaveBeenCalledWith(value, value.length);
751
-
752
- buffalo.setPosition(0);
753
-
754
- expect(buffalo.read(Zcl.BuffaloZclDataType.USE_DATA_TYPE, {})).toStrictEqual(Buffer.from(value));
755
- expect(readSpy).toHaveBeenCalledTimes(1);
756
- expect(readSpy).toHaveBeenCalledWith(value.length);
757
-
758
- buffalo.setPosition(0);
759
-
760
- expect(buffalo.read(Zcl.BuffaloZclDataType.USE_DATA_TYPE, {length: 1})).toStrictEqual(Buffer.from([value[0]]));
761
- expect(readSpy).toHaveBeenCalledTimes(2);
762
- expect(readSpy).toHaveBeenCalledWith(1);
763
- });
764
-
765
- it("Throws when write Use Data Type is missing dataType option and value isnt buffer or number array", () => {
766
- expect(() => {
767
- const payload = 1;
768
- const buffalo = new BuffaloZcl(Buffer.alloc(2));
769
- buffalo.write(Zcl.BuffaloZclDataType.USE_DATA_TYPE, payload, {});
770
- }).toThrow();
771
- });
772
-
773
- it.each([
774
- Zcl.BuffaloZclDataType.LIST_UINT8,
775
- Zcl.BuffaloZclDataType.LIST_UINT16,
776
- Zcl.BuffaloZclDataType.LIST_UINT24,
777
- Zcl.BuffaloZclDataType.LIST_UINT32,
778
- Zcl.BuffaloZclDataType.LIST_ZONEINFO,
779
- ])("Throws when read %s is missing required length option", (type) => {
780
- expect(() => {
781
- const buffalo = new BuffaloZcl(Buffer.alloc(1));
782
- buffalo.read(type, {});
783
- }).toThrow();
784
- });
785
-
786
- it("Writes & Reads zone info list", () => {
787
- const value = [
788
- {zoneID: 1, zoneStatus: 5},
789
- {zoneID: 2, zoneStatus: 6},
790
- ];
791
- const expectedWritten = [1, 5, 0 /*uint16*/, 2, 6, 0 /*uint16*/];
792
- const buffer = Buffer.alloc(10);
793
- const buffalo = new BuffaloZcl(buffer);
794
- buffalo.write(Zcl.BuffaloZclDataType.LIST_ZONEINFO, value, {});
795
- expect(buffalo.getPosition()).toStrictEqual(expectedWritten.length);
796
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expectedWritten));
797
-
798
- buffalo.setPosition(0);
799
-
800
- expect(buffalo.read(Zcl.BuffaloZclDataType.LIST_ZONEINFO, {length: value.length})).toStrictEqual(value);
801
- expect(buffalo.getPosition()).toStrictEqual(expectedWritten.length);
802
- });
803
-
804
- it.each([
805
- ["6" /*uint8 x1*/, {value: [{clstId: 6, len: 1, extField: [1]}]}, {written: [...uint16To8Array(6), 1, 1]}],
806
- ["8" /*uint8 x1*/, {value: [{clstId: 8, len: 1, extField: [2]}]}, {written: [...uint16To8Array(8), 1, 2]}],
807
- ["258" /*uint8 x2*/, {value: [{clstId: 258, len: 2, extField: [1, 2]}]}, {written: [...uint16To8Array(258), 2, 1, 2]}],
808
- [
809
- "768" /*uint16 x3, uint8 x3, uint16 x2*/,
810
- {value: [{clstId: 768, len: 13, extField: [1, 2, 3, 4, 5, 6, 7, 8]}]},
811
- {
812
- written: [
813
- ...uint16To8Array(768),
814
- 13,
815
- ...uint16To8Array(1),
816
- ...uint16To8Array(2),
817
- ...uint16To8Array(3),
818
- 4,
819
- 5,
820
- 6,
821
- ...uint16To8Array(7),
822
- ...uint16To8Array(8),
823
- ],
824
- },
825
- ],
826
- ])("Writes & Reads Extension Field Sets for data type %s", (_name, payload, expected) => {
827
- const buffer = Buffer.alloc(expected.written.length); // XXX: can't be arbitrary atm, see impl for identified issue
828
- const buffalo = new BuffaloZcl(buffer);
829
- buffalo.write(Zcl.BuffaloZclDataType.EXTENSION_FIELD_SETS, payload.value, {});
830
- expect(buffalo.getPosition()).toStrictEqual(expected.written.length);
831
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expected.written));
832
-
833
- buffalo.setPosition(0);
834
-
835
- expect(buffalo.read(Zcl.BuffaloZclDataType.EXTENSION_FIELD_SETS, {})).toStrictEqual(payload.value);
836
- expect(buffalo.getPosition()).toStrictEqual(expected.written.length);
837
- });
838
-
839
- it.each([
840
- [
841
- "single all options",
842
- {value: [{transitionTime: 3, heatSetpoint: 20, coolSetpoint: 15}], readOptions: {payload: {mode: 0b11, numoftrans: 1}}},
843
- {written: [...uint16To8Array(3), ...uint16To8Array(20), ...uint16To8Array(15)]},
844
- ],
845
- [
846
- "single heat-only",
847
- {value: [{transitionTime: 60, heatSetpoint: 25}], readOptions: {payload: {mode: 0b01, numoftrans: 1}}},
848
- {written: [...uint16To8Array(60), ...uint16To8Array(25)]},
849
- ],
850
- [
851
- "single cool-only",
852
- {value: [{transitionTime: 256, coolSetpoint: 15}], readOptions: {payload: {mode: 0b10, numoftrans: 1}}},
853
- {written: [...uint16To8Array(256), ...uint16To8Array(15)]},
854
- ],
855
- [
856
- "multiple all options",
857
- {
858
- value: [
859
- {transitionTime: 3, heatSetpoint: 20, coolSetpoint: 15},
860
- {transitionTime: 7, heatSetpoint: 8, coolSetpoint: 3},
861
- {transitionTime: 257, heatSetpoint: 256, coolSetpoint: 0},
862
- ],
863
- readOptions: {payload: {mode: 0b11, numoftrans: 3}},
864
- },
865
- {
866
- written: [
867
- ...uint16To8Array(3),
868
- ...uint16To8Array(20),
869
- ...uint16To8Array(15),
870
- ...uint16To8Array(7),
871
- ...uint16To8Array(8),
872
- ...uint16To8Array(3),
873
- ...uint16To8Array(257),
874
- ...uint16To8Array(256),
875
- ...uint16To8Array(0),
876
- ],
877
- },
878
- ],
879
- [
880
- "multiple heat-only",
881
- {
882
- value: [
883
- {transitionTime: 3, heatSetpoint: 20},
884
- {transitionTime: 70, heatSetpoint: 8},
885
- ],
886
- readOptions: {payload: {mode: 0b01, numoftrans: 2}},
887
- },
888
- {written: [...uint16To8Array(3), ...uint16To8Array(20), ...uint16To8Array(70), ...uint16To8Array(8)]},
889
- ],
890
- [
891
- "multiple cool-only",
892
- {
893
- value: [
894
- {transitionTime: 3, coolSetpoint: 15},
895
- {transitionTime: 65000, coolSetpoint: 3},
896
- ],
897
- readOptions: {payload: {mode: 0b10, numoftrans: 2}},
898
- },
899
- {written: [...uint16To8Array(3), ...uint16To8Array(15), ...uint16To8Array(65000), ...uint16To8Array(3)]},
900
- ],
901
- ])("Writes & Reads Thermo Transitions List", (_name, payload, expected) => {
902
- const buffer = Buffer.alloc(50);
903
- const buffalo = new BuffaloZcl(buffer);
904
- buffalo.write(Zcl.BuffaloZclDataType.LIST_THERMO_TRANSITIONS, payload.value, {});
905
- expect(buffalo.getPosition()).toStrictEqual(expected.written.length);
906
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expected.written));
907
-
908
- buffalo.setPosition(0);
909
-
910
- expect(buffalo.read(Zcl.BuffaloZclDataType.LIST_THERMO_TRANSITIONS, payload.readOptions)).toStrictEqual(payload.value);
911
- expect(buffalo.getPosition()).toStrictEqual(expected.written.length);
912
- });
913
-
914
- it("Throws when read Thermo Transitions List is missing required payload options", () => {
915
- expect(() => {
916
- const buffalo = new BuffaloZcl(Buffer.alloc(1));
917
- buffalo.read(Zcl.BuffaloZclDataType.LIST_THERMO_TRANSITIONS, {});
918
- }).toThrow();
919
- expect(() => {
920
- const buffalo = new BuffaloZcl(Buffer.alloc(1));
921
- buffalo.read(Zcl.BuffaloZclDataType.LIST_THERMO_TRANSITIONS, {payload: {}});
922
- }).toThrow();
923
- expect(() => {
924
- const buffalo = new BuffaloZcl(Buffer.alloc(1));
925
- buffalo.read(Zcl.BuffaloZclDataType.LIST_THERMO_TRANSITIONS, {payload: {mode: 1}});
926
- }).toThrow();
927
- expect(() => {
928
- const buffalo = new BuffaloZcl(Buffer.alloc(1));
929
- buffalo.read(Zcl.BuffaloZclDataType.LIST_THERMO_TRANSITIONS, {payload: {numoftrans: 1}});
930
- }).toThrow();
931
- });
932
-
933
- describe("GPD Frame", () => {
934
- it("Reads unhandled command as object[raw] if buffer still has bytes to read", () => {
935
- const value = [0xff, 0x00];
936
- const buffalo = new BuffaloZcl(Buffer.from(value));
937
-
938
- expect(buffalo.read(Zcl.BuffaloZclDataType.GPD_FRAME, {payload: {commandID: 0x1ff, payloadSize: 2}})).toStrictEqual({
939
- raw: Buffer.from(value),
940
- });
941
- });
942
-
943
- it("Reads unhandled command as object[raw] and ignores GPP data/mic", () => {
944
- let value = [0xff, 0x00, 0x21, 0x43, 0xfe];
945
- let buffalo = new BuffaloZcl(Buffer.from(value));
946
-
947
- expect(buffalo.read(Zcl.BuffaloZclDataType.GPD_FRAME, {payload: {commandID: 0x1ff, payloadSize: 2}})).toStrictEqual({
948
- raw: Buffer.from(value).subarray(0, -3),
949
- });
950
-
951
- value = [0xff, 0x00, 0x21, 0x43, 0xfe, 0xf1, 0xf2, 0xf3, 0xf4];
952
- buffalo = new BuffaloZcl(Buffer.from(value));
953
-
954
- expect(buffalo.read(Zcl.BuffaloZclDataType.GPD_FRAME, {payload: {commandID: 0x1ff, payloadSize: 2}})).toStrictEqual({
955
- raw: Buffer.from(value).subarray(0, -7),
956
- });
957
- });
958
-
959
- it("Reads unhandled command as empty object if buffer finished reading", () => {
960
- // XXX: this is no longer relevant with proper payload size checking?
961
- const value = [0xff, 0x00];
962
- const buffalo = new BuffaloZcl(Buffer.from(value), value.length /* pos at end*/);
963
-
964
- expect(buffalo.read(Zcl.BuffaloZclDataType.GPD_FRAME, {payload: {commandID: 0x1ff, payloadSize: value.length}})).toStrictEqual({
965
- raw: Buffer.from([]),
966
- });
967
- });
968
-
969
- it("Writes commissioning", () => {
970
- const expected = [1 /*length*/, 0 /*options*/];
971
- const buffalo = new BuffaloZcl(Buffer.alloc(2));
972
-
973
- buffalo.write(
974
- Zcl.BuffaloZclDataType.GPD_FRAME,
975
- {
976
- commandID: 0xf0,
977
- options: 0,
978
- panID: 0,
979
- securityKey: Buffer.alloc(16),
980
- keyMic: 0,
981
- frameCounter: 0,
982
- },
983
- {},
984
- );
985
-
986
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expected));
987
- });
988
-
989
- it("Reads commissioning", () => {
990
- const value = [0xff /*device*/, 0x00 /*options*/];
991
- const buffalo = new BuffaloZcl(Buffer.from(value));
992
-
993
- expect(buffalo.read(Zcl.BuffaloZclDataType.GPD_FRAME, {payload: {payloadSize: value.length, commandID: 0xe0}})).toStrictEqual({
994
- deviceID: 0xff,
995
- options: 0x00,
996
- extendedOptions: 0x00,
997
- securityKey: Buffer.alloc(16),
998
- keyMic: 0,
999
- outgoingCounter: 0,
1000
- manufacturerID: 0,
1001
- modelID: 0,
1002
- numGpdCommands: 0,
1003
- gpdCommandIdList: Buffer.alloc(0),
1004
- numServerClusters: 0,
1005
- numClientClusters: 0,
1006
- gpdServerClusters: Buffer.alloc(0),
1007
- gpdClientClusters: Buffer.alloc(0),
1008
- applicationInfo: 0x00,
1009
- genericSwitchConfig: 0,
1010
- currentContactStatus: 0,
1011
- });
1012
- });
1013
-
1014
- it("Writes commissioning all options", () => {
1015
- const expected = [
1016
- 27, // length
1017
- 0b11111, // options
1018
- 0xff,
1019
- 0xff, // PAN ID
1020
- 0,
1021
- 0,
1022
- 0,
1023
- 0,
1024
- 0,
1025
- 0,
1026
- 0,
1027
- 0,
1028
- 0,
1029
- 0,
1030
- 0,
1031
- 0,
1032
- 0,
1033
- 0,
1034
- 0,
1035
- 0, // security key
1036
- 0,
1037
- 0,
1038
- 0,
1039
- 0, // key mic
1040
- 0,
1041
- 0,
1042
- 0,
1043
- 0, // frame counter
1044
- ];
1045
- const buffalo = new BuffaloZcl(Buffer.alloc(28));
1046
-
1047
- buffalo.write(
1048
- Zcl.BuffaloZclDataType.GPD_FRAME,
1049
- {
1050
- commandID: 0xf0,
1051
- options: 0b11111,
1052
- panID: 0xffff,
1053
- securityKey: Buffer.alloc(16),
1054
- keyMic: 0,
1055
- frameCounter: 0,
1056
- },
1057
- {},
1058
- );
1059
-
1060
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expected));
1061
- });
1062
-
1063
- it("Reads commissioning all options", () => {
1064
- const value = [
1065
- 0xff, // device
1066
- 0x80 | 0x04, // options
1067
- 0x20 | 0x40 | 0x80, // extended options
1068
- 0,
1069
- 0,
1070
- 0,
1071
- 0,
1072
- 0,
1073
- 0,
1074
- 0,
1075
- 0,
1076
- 0,
1077
- 0,
1078
- 0,
1079
- 0,
1080
- 0,
1081
- 0,
1082
- 0,
1083
- 0, // security key
1084
- 0,
1085
- 0,
1086
- 0,
1087
- 0, // key mic
1088
- 0,
1089
- 0,
1090
- 0,
1091
- 0, // outgoing counter
1092
- 0x01 | 0x02 | 0x04 | 0x08 | 0x10, // application info
1093
- 0,
1094
- 0, // manufacturer ID
1095
- 0,
1096
- 0, // model ID
1097
- 0, // num GPD commands + commands
1098
- 0, // clusters
1099
- 2, // switch info length
1100
- 5, // generic switch config
1101
- 2, // current contact status
1102
- ];
1103
- const buffalo = new BuffaloZcl(Buffer.from(value));
1104
-
1105
- expect(buffalo.read(Zcl.BuffaloZclDataType.GPD_FRAME, {payload: {payloadSize: value.length, commandID: 0xe0}})).toStrictEqual({
1106
- deviceID: 0xff,
1107
- options: 0x80 | 0x04,
1108
- extendedOptions: 0x20 | 0x40 | 0x80,
1109
- securityKey: Buffer.alloc(16),
1110
- keyMic: 0,
1111
- outgoingCounter: 0,
1112
- manufacturerID: 0,
1113
- modelID: 0,
1114
- numGpdCommands: 0,
1115
- gpdCommandIdList: Buffer.alloc(0),
1116
- numServerClusters: 0,
1117
- numClientClusters: 0,
1118
- gpdServerClusters: Buffer.alloc(0),
1119
- gpdClientClusters: Buffer.alloc(0),
1120
- applicationInfo: 0x01 | 0x02 | 0x04 | 0x08 | 0x10,
1121
- genericSwitchConfig: 5,
1122
- currentContactStatus: 2,
1123
- });
1124
- });
1125
-
1126
- it("Writes channel configuration", () => {
1127
- const expected = [1 /*length*/, 0xf /*Channel 26*/];
1128
- const buffalo = new BuffaloZcl(Buffer.alloc(2));
1129
- buffalo.write(
1130
- Zcl.BuffaloZclDataType.GPD_FRAME,
1131
- {
1132
- commandID: 0xf3,
1133
- operationalChannel: 0xf,
1134
- basic: false,
1135
- },
1136
- {},
1137
- );
1138
-
1139
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expected));
1140
- });
1141
-
1142
- it("Writes channel configuration basic", () => {
1143
- const expected = [1 /*length*/, 0x1f /*Channel 26 + Basic*/];
1144
- const buffalo = new BuffaloZcl(Buffer.alloc(2));
1145
- buffalo.write(
1146
- Zcl.BuffaloZclDataType.GPD_FRAME,
1147
- {
1148
- commandID: 0xf3,
1149
- operationalChannel: 0xf,
1150
- basic: true,
1151
- },
1152
- {},
1153
- );
1154
-
1155
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expected));
1156
- });
1157
-
1158
- it("Reads channel request", () => {
1159
- const value = [0xfa];
1160
- const buffalo = new BuffaloZcl(Buffer.from(value));
1161
-
1162
- expect(buffalo.read(Zcl.BuffaloZclDataType.GPD_FRAME, {payload: {payloadSize: value.length, commandID: 0xe3}})).toStrictEqual({
1163
- nextChannel: 0xa,
1164
- nextNextChannel: 0xf,
1165
- });
1166
- });
1167
-
1168
- it("Reads attribute report", () => {
1169
- const value = [
1170
- 0x12,
1171
- 0x34, // Manufacturer ID
1172
- 0xff,
1173
- 0xff, // Cluster ID
1174
- 0x00,
1175
- 0x00, // Attribute ID
1176
- Zcl.DataType.UINT32, // Attribute Type
1177
- 0x00,
1178
- 0x01,
1179
- 0x02,
1180
- 0x03,
1181
- 0x01,
1182
- 0x00,
1183
- Zcl.DataType.CHAR_STR,
1184
- 0x06,
1185
- 0x5a,
1186
- 0x49,
1187
- 0x47,
1188
- 0x42,
1189
- 0x45,
1190
- 0x45,
1191
- 0x02,
1192
- 0x00,
1193
- Zcl.DataType.BOOLEAN,
1194
- 0x01,
1195
- ];
1196
- const buffalo = new BuffaloZcl(Buffer.from(value));
1197
-
1198
- expect(buffalo.read(Zcl.BuffaloZclDataType.GPD_FRAME, {payload: {commandID: 0xa1, payloadSize: value.length}})).toStrictEqual({
1199
- manufacturerCode: 13330,
1200
- clusterID: 65535,
1201
- attributes: {"0": 50462976, "1": "ZIGBEE", "2": 1},
1202
- });
1203
- });
1204
-
1205
- it("Writes custom reply", () => {
1206
- const expected = [
1207
- 6, // length
1208
- 90,
1209
- 73,
1210
- 71,
1211
- 66,
1212
- 69,
1213
- 69, // ZIGBEE
1214
- ];
1215
-
1216
- const buffalo = new BuffaloZcl(Buffer.alloc(7));
1217
- buffalo.write(Zcl.BuffaloZclDataType.GPD_FRAME, {commandID: 0xf4, buffer: Buffer.from("ZIGBEE")}, {});
1218
-
1219
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expected));
1220
- });
1221
-
1222
- it("Writes nothing for unhandled command", () => {
1223
- const buffalo = new BuffaloZcl(Buffer.alloc(7));
1224
- buffalo.write(Zcl.BuffaloZclDataType.GPD_FRAME, {commandID: 0x1ff}, {});
1225
-
1226
- expect(buffalo.getWritten()).toStrictEqual(Buffer.alloc(0));
1227
- });
1228
-
1229
- it("Throws when read is missing payload.payloadSize option", () => {
1230
- expect(() => {
1231
- const buffalo = new BuffaloZcl(Buffer.alloc(1));
1232
- buffalo.read(Zcl.BuffaloZclDataType.GPD_FRAME, {payload: {commandID: 0xa1}});
1233
- }).toThrow("Cannot read GPD_FRAME without required payload options specified");
1234
- });
1235
- });
1236
-
1237
- it.each([
1238
- ["whole", {value: {indicatorType: Zcl.StructuredIndicatorType.Whole}}, {written: [Zcl.StructuredIndicatorType.Whole]}],
1239
- [
1240
- "indexes only",
1241
- {value: {indexes: [3, 4, 5, 256]}},
1242
- {written: [4, ...uint16To8Array(3), ...uint16To8Array(4), ...uint16To8Array(5), ...uint16To8Array(256)]},
1243
- ],
1244
- ])("Writes & Reads Structured Selector for %s", (_name, payload, expected) => {
1245
- const buffer = Buffer.alloc(50);
1246
- const buffalo = new BuffaloZcl(buffer);
1247
- buffalo.write(Zcl.BuffaloZclDataType.STRUCTURED_SELECTOR, payload.value, {});
1248
- expect(buffalo.getPosition()).toStrictEqual(expected.written.length);
1249
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expected.written));
1250
-
1251
- buffalo.setPosition(0);
1252
-
1253
- expect(buffalo.read(Zcl.BuffaloZclDataType.STRUCTURED_SELECTOR, {})).toStrictEqual(payload.value);
1254
- expect(buffalo.getPosition()).toStrictEqual(expected.written.length);
1255
- });
1256
-
1257
- it.each([
1258
- ["Add", {value: {indicatorType: Zcl.StructuredIndicatorType.WriteAdd}}, {written: [Zcl.StructuredIndicatorType.WriteAdd]}],
1259
- ["Remove", {value: {indicatorType: Zcl.StructuredIndicatorType.WriteRemove}}, {written: [Zcl.StructuredIndicatorType.WriteRemove]}],
1260
- ])("Writes Structured Selector for %s", (_name, payload, expected) => {
1261
- const buffer = Buffer.alloc(50);
1262
- const buffalo = new BuffaloZcl(buffer);
1263
- buffalo.write(Zcl.BuffaloZclDataType.STRUCTURED_SELECTOR, payload.value, {});
1264
- expect(buffalo.getPosition()).toStrictEqual(expected.written.length);
1265
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expected.written));
1266
- });
1267
-
1268
- it("Throws when read Strctured Select indicator is outside range", () => {
1269
- expect(() => {
1270
- const buffer = Buffer.from([17]);
1271
- const buffalo = new BuffaloZcl(buffer);
1272
- buffalo.read(Zcl.BuffaloZclDataType.STRUCTURED_SELECTOR, {});
1273
- }).toThrow();
1274
- expect(() => {
1275
- const buffer = Buffer.from([17, 1, 2, 3]);
1276
- const buffalo = new BuffaloZcl(buffer);
1277
- buffalo.read(Zcl.BuffaloZclDataType.STRUCTURED_SELECTOR, {});
1278
- }).toThrow();
1279
- });
1280
-
1281
- it.each([
1282
- [
1283
- "single",
1284
- {value: [{dp: 254, datatype: 125, data: Buffer.from([1, 3, 5])}]},
1285
- {written: [254, 125, ...uint16To8Array(3).reverse() /*BE*/, 1, 3, 5]},
1286
- ],
1287
- [
1288
- "multiple",
1289
- {
1290
- value: [
1291
- {dp: 254, datatype: 125, data: Buffer.from([1, 3, 5])},
1292
- {dp: 125, datatype: 254, data: Buffer.from([5, 0, 1, 5])},
1293
- ],
1294
- },
1295
- {written: [254, 125, ...uint16To8Array(3).reverse() /*BE*/, 1, 3, 5, 125, 254, ...uint16To8Array(4).reverse() /*BE*/, 5, 0, 1, 5]},
1296
- ],
1297
- ])("Writes & Reads Tuya Data Point Values List %s", (_name, payload, expected) => {
1298
- const buffer = Buffer.alloc(expected.written.length); // XXX: can't be arbitrary atm, see impl for identified issue
1299
- const buffalo = new BuffaloZcl(buffer);
1300
- buffalo.write(Zcl.BuffaloZclDataType.LIST_TUYA_DATAPOINT_VALUES, payload.value, {});
1301
- expect(buffalo.getPosition()).toStrictEqual(expected.written.length);
1302
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expected.written));
1303
-
1304
- buffalo.setPosition(0);
1305
-
1306
- expect(buffalo.read(Zcl.BuffaloZclDataType.LIST_TUYA_DATAPOINT_VALUES, {})).toStrictEqual(payload.value);
1307
- expect(buffalo.getPosition()).toStrictEqual(expected.written.length);
1308
- });
1309
-
1310
- it("Reads invalid Tuya Data Point Values List as empty array", () => {
1311
- // incomplete
1312
- const buffer = Buffer.from([254, 125]);
1313
- const buffalo = new BuffaloZcl(buffer);
1314
- expect(buffalo.read(Zcl.BuffaloZclDataType.LIST_TUYA_DATAPOINT_VALUES, {})).toStrictEqual([]);
1315
- });
1316
-
1317
- it("Writes & Reads Mi Boxer Zones List", () => {
1318
- const value = [
1319
- {zoneNum: 1, groupId: 0x2b84},
1320
- {zoneNum: 2, groupId: 0x2b98},
1321
- {zoneNum: 3, groupId: 0x2bac},
1322
- {zoneNum: 4, groupId: 0x2bc0},
1323
- {zoneNum: 5, groupId: 0x2bd4},
1324
- {zoneNum: 6, groupId: 0x2be8},
1325
- {zoneNum: 7, groupId: 0x2bfc},
1326
- {zoneNum: 8, groupId: 0x2c10},
1327
- ];
1328
- const expectedWritten = [
1329
- value.length,
1330
- ...uint16To8Array(value[0].groupId),
1331
- value[0].zoneNum,
1332
- ...uint16To8Array(value[1].groupId),
1333
- value[1].zoneNum,
1334
- ...uint16To8Array(value[2].groupId),
1335
- value[2].zoneNum,
1336
- ...uint16To8Array(value[3].groupId),
1337
- value[3].zoneNum,
1338
- ...uint16To8Array(value[4].groupId),
1339
- value[4].zoneNum,
1340
- ...uint16To8Array(value[5].groupId),
1341
- value[5].zoneNum,
1342
- ...uint16To8Array(value[6].groupId),
1343
- value[6].zoneNum,
1344
- ...uint16To8Array(value[7].groupId),
1345
- value[7].zoneNum,
1346
- ];
1347
- const buffer = Buffer.alloc(50);
1348
- const buffalo = new BuffaloZcl(buffer);
1349
- buffalo.write(Zcl.BuffaloZclDataType.LIST_MIBOXER_ZONES, value, {});
1350
- expect(buffalo.getPosition()).toStrictEqual(expectedWritten.length);
1351
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expectedWritten));
1352
-
1353
- buffalo.setPosition(0);
1354
-
1355
- expect(buffalo.read(Zcl.BuffaloZclDataType.LIST_MIBOXER_ZONES, {length: value.length})).toStrictEqual(value);
1356
- expect(buffalo.getPosition()).toStrictEqual(expectedWritten.length);
1357
- });
1358
-
1359
- it("Writes & Reads big endian uint24", () => {
1360
- const value = 16777200;
1361
- const expectedWritten = [0xff, 0xff, 0xf0];
1362
- const expectedPosition = 3;
1363
- const buffer = Buffer.alloc(10);
1364
- const buffalo = new BuffaloZcl(buffer);
1365
- buffalo.write(Zcl.BuffaloZclDataType.BIG_ENDIAN_UINT24, value, {});
1366
- expect(buffalo.getPosition()).toStrictEqual(expectedPosition);
1367
- expect(buffalo.getWritten()).toStrictEqual(Buffer.from(expectedWritten));
1368
-
1369
- buffalo.setPosition(0);
1370
-
1371
- expect(buffalo.read(Zcl.BuffaloZclDataType.BIG_ENDIAN_UINT24, {})).toStrictEqual(value);
1372
- expect(buffalo.getPosition()).toStrictEqual(expectedPosition);
1373
- });
1374
- });