@willieee802/zigbee-herdsman 0.18.2 → 0.18.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 (327) hide show
  1. package/dist/adapter/adapter.d.ts +61 -61
  2. package/dist/adapter/adapter.d.ts.map +1 -1
  3. package/dist/adapter/adapter.js +146 -146
  4. package/dist/adapter/deconz/adapter/deconzAdapter.d.ts +68 -68
  5. package/dist/adapter/deconz/adapter/deconzAdapter.js +1060 -1060
  6. package/dist/adapter/deconz/adapter/index.d.ts +2 -2
  7. package/dist/adapter/deconz/adapter/index.js +10 -10
  8. package/dist/adapter/deconz/driver/constants.d.ts +104 -104
  9. package/dist/adapter/deconz/driver/constants.js +55 -55
  10. package/dist/adapter/deconz/driver/driver.d.ts +81 -81
  11. package/dist/adapter/deconz/driver/driver.js +732 -732
  12. package/dist/adapter/deconz/driver/frame.d.ts +6 -6
  13. package/dist/adapter/deconz/driver/frame.js +13 -13
  14. package/dist/adapter/deconz/driver/frameParser.d.ts +2 -2
  15. package/dist/adapter/deconz/driver/frameParser.js +443 -443
  16. package/dist/adapter/deconz/driver/parser.d.ts +12 -12
  17. package/dist/adapter/deconz/driver/parser.js +61 -61
  18. package/dist/adapter/deconz/driver/writer.d.ts +8 -8
  19. package/dist/adapter/deconz/driver/writer.js +44 -44
  20. package/dist/adapter/events.d.ts +47 -47
  21. package/dist/adapter/events.js +14 -14
  22. package/dist/adapter/ezsp/adapter/backup.d.ts +9 -9
  23. package/dist/adapter/ezsp/adapter/backup.js +53 -53
  24. package/dist/adapter/ezsp/adapter/ezspAdapter.d.ts +59 -59
  25. package/dist/adapter/ezsp/adapter/ezspAdapter.d.ts.map +1 -1
  26. package/dist/adapter/ezsp/adapter/ezspAdapter.js +603 -585
  27. package/dist/adapter/ezsp/adapter/ezspAdapter.js.map +1 -1
  28. package/dist/adapter/ezsp/adapter/index.d.ts +2 -2
  29. package/dist/adapter/ezsp/adapter/index.js +10 -10
  30. package/dist/adapter/ezsp/driver/commands.d.ts +36 -36
  31. package/dist/adapter/ezsp/driver/commands.js +2359 -2359
  32. package/dist/adapter/ezsp/driver/consts.d.ts +10 -10
  33. package/dist/adapter/ezsp/driver/consts.js +13 -13
  34. package/dist/adapter/ezsp/driver/driver.d.ts +103 -103
  35. package/dist/adapter/ezsp/driver/driver.js +639 -639
  36. package/dist/adapter/ezsp/driver/ezsp.d.ts +96 -96
  37. package/dist/adapter/ezsp/driver/ezsp.js +586 -586
  38. package/dist/adapter/ezsp/driver/index.d.ts +3 -3
  39. package/dist/adapter/ezsp/driver/index.js +8 -8
  40. package/dist/adapter/ezsp/driver/multicast.d.ts +12 -12
  41. package/dist/adapter/ezsp/driver/multicast.js +74 -74
  42. package/dist/adapter/ezsp/driver/parser.d.ts +12 -12
  43. package/dist/adapter/ezsp/driver/parser.js +111 -111
  44. package/dist/adapter/ezsp/driver/types/basic.d.ts +62 -62
  45. package/dist/adapter/ezsp/driver/types/basic.js +208 -208
  46. package/dist/adapter/ezsp/driver/types/basic.js.map +1 -1
  47. package/dist/adapter/ezsp/driver/types/index.d.ts +9 -9
  48. package/dist/adapter/ezsp/driver/types/index.js +133 -133
  49. package/dist/adapter/ezsp/driver/types/named.d.ts +697 -697
  50. package/dist/adapter/ezsp/driver/types/named.js +1726 -1726
  51. package/dist/adapter/ezsp/driver/types/named.js.map +1 -1
  52. package/dist/adapter/ezsp/driver/types/struct.d.ts +251 -251
  53. package/dist/adapter/ezsp/driver/types/struct.js +708 -708
  54. package/dist/adapter/ezsp/driver/types/struct.js.map +1 -1
  55. package/dist/adapter/ezsp/driver/uart.d.ts +44 -44
  56. package/dist/adapter/ezsp/driver/uart.js +368 -368
  57. package/dist/adapter/ezsp/driver/utils/crc16ccitt.d.ts +2 -2
  58. package/dist/adapter/ezsp/driver/utils/crc16ccitt.js +55 -55
  59. package/dist/adapter/ezsp/driver/utils/index.d.ts +18 -18
  60. package/dist/adapter/ezsp/driver/utils/index.js +67 -67
  61. package/dist/adapter/ezsp/driver/writer.d.ts +13 -13
  62. package/dist/adapter/ezsp/driver/writer.js +88 -88
  63. package/dist/adapter/index.d.ts +4 -4
  64. package/dist/adapter/index.js +35 -35
  65. package/dist/adapter/serialPort.d.ts +8 -8
  66. package/dist/adapter/serialPort.js +22 -22
  67. package/dist/adapter/serialPort.js.map +1 -1
  68. package/dist/adapter/serialPortUtils.d.ts +12 -12
  69. package/dist/adapter/serialPortUtils.js +18 -18
  70. package/dist/adapter/socketPortUtils.d.ts +10 -10
  71. package/dist/adapter/socketPortUtils.js +16 -16
  72. package/dist/adapter/tstype.d.ts +85 -85
  73. package/dist/adapter/tstype.js +2 -2
  74. package/dist/adapter/z-stack/adapter/adapter-backup.d.ts +62 -62
  75. package/dist/adapter/z-stack/adapter/adapter-backup.d.ts.map +1 -1
  76. package/dist/adapter/z-stack/adapter/adapter-backup.js +460 -441
  77. package/dist/adapter/z-stack/adapter/adapter-backup.js.map +1 -1
  78. package/dist/adapter/z-stack/adapter/adapter-nv-memory.d.ts +150 -150
  79. package/dist/adapter/z-stack/adapter/adapter-nv-memory.js +258 -258
  80. package/dist/adapter/z-stack/adapter/endpoints.d.ts +11 -11
  81. package/dist/adapter/z-stack/adapter/endpoints.js +73 -73
  82. package/dist/adapter/z-stack/adapter/index.d.ts +2 -2
  83. package/dist/adapter/z-stack/adapter/index.js +8 -8
  84. package/dist/adapter/z-stack/adapter/manager.d.ts +86 -86
  85. package/dist/adapter/z-stack/adapter/manager.js +476 -476
  86. package/dist/adapter/z-stack/adapter/tstype.d.ts +6 -6
  87. package/dist/adapter/z-stack/adapter/tstype.js +10 -10
  88. package/dist/adapter/z-stack/adapter/zStackAdapter.d.ts +81 -81
  89. package/dist/adapter/z-stack/adapter/zStackAdapter.d.ts.map +1 -1
  90. package/dist/adapter/z-stack/adapter/zStackAdapter.js +868 -868
  91. package/dist/adapter/z-stack/adapter/zStackAdapter.js.map +1 -1
  92. package/dist/adapter/z-stack/constants/af.d.ts +23 -23
  93. package/dist/adapter/z-stack/constants/af.js +27 -27
  94. package/dist/adapter/z-stack/constants/common.d.ts +278 -278
  95. package/dist/adapter/z-stack/constants/common.js +289 -289
  96. package/dist/adapter/z-stack/constants/dbg.d.ts +22 -22
  97. package/dist/adapter/z-stack/constants/dbg.js +24 -24
  98. package/dist/adapter/z-stack/constants/index.d.ts +10 -10
  99. package/dist/adapter/z-stack/constants/index.js +47 -47
  100. package/dist/adapter/z-stack/constants/mac.d.ts +127 -127
  101. package/dist/adapter/z-stack/constants/mac.js +129 -129
  102. package/dist/adapter/z-stack/constants/sapi.d.ts +24 -24
  103. package/dist/adapter/z-stack/constants/sapi.js +26 -26
  104. package/dist/adapter/z-stack/constants/sys.d.ts +71 -71
  105. package/dist/adapter/z-stack/constants/sys.js +73 -73
  106. package/dist/adapter/z-stack/constants/util.d.ts +81 -81
  107. package/dist/adapter/z-stack/constants/util.js +83 -83
  108. package/dist/adapter/z-stack/constants/utils.d.ts +4 -4
  109. package/dist/adapter/z-stack/constants/utils.js +14 -14
  110. package/dist/adapter/z-stack/constants/zdo.d.ts +102 -102
  111. package/dist/adapter/z-stack/constants/zdo.js +104 -104
  112. package/dist/adapter/z-stack/models/index.d.ts +1 -1
  113. package/dist/adapter/z-stack/models/index.js +17 -17
  114. package/dist/adapter/z-stack/models/startup-options.d.ts +12 -12
  115. package/dist/adapter/z-stack/models/startup-options.js +2 -2
  116. package/dist/adapter/z-stack/structs/entries/address-manager-entry.d.ts +23 -23
  117. package/dist/adapter/z-stack/structs/entries/address-manager-entry.js +45 -45
  118. package/dist/adapter/z-stack/structs/entries/address-manager-table.d.ts +10 -10
  119. package/dist/adapter/z-stack/structs/entries/address-manager-table.js +22 -22
  120. package/dist/adapter/z-stack/structs/entries/aps-link-key-data-entry.d.ts +10 -10
  121. package/dist/adapter/z-stack/structs/entries/aps-link-key-data-entry.js +21 -21
  122. package/dist/adapter/z-stack/structs/entries/aps-link-key-data-table.d.ts +10 -10
  123. package/dist/adapter/z-stack/structs/entries/aps-link-key-data-table.js +23 -23
  124. package/dist/adapter/z-stack/structs/entries/aps-tc-link-key-entry.d.ts +10 -10
  125. package/dist/adapter/z-stack/structs/entries/aps-tc-link-key-entry.js +24 -24
  126. package/dist/adapter/z-stack/structs/entries/aps-tc-link-key-table.d.ts +10 -10
  127. package/dist/adapter/z-stack/structs/entries/aps-tc-link-key-table.js +23 -23
  128. package/dist/adapter/z-stack/structs/entries/channel-list.d.ts +8 -8
  129. package/dist/adapter/z-stack/structs/entries/channel-list.js +15 -15
  130. package/dist/adapter/z-stack/structs/entries/has-configured.d.ts +8 -8
  131. package/dist/adapter/z-stack/structs/entries/has-configured.js +16 -16
  132. package/dist/adapter/z-stack/structs/entries/index.d.ts +16 -16
  133. package/dist/adapter/z-stack/structs/entries/index.js +32 -32
  134. package/dist/adapter/z-stack/structs/entries/nib.d.ts +10 -10
  135. package/dist/adapter/z-stack/structs/entries/nib.js +68 -68
  136. package/dist/adapter/z-stack/structs/entries/nwk-key-descriptor.d.ts +10 -10
  137. package/dist/adapter/z-stack/structs/entries/nwk-key-descriptor.js +18 -18
  138. package/dist/adapter/z-stack/structs/entries/nwk-key.d.ts +8 -8
  139. package/dist/adapter/z-stack/structs/entries/nwk-key.js +15 -15
  140. package/dist/adapter/z-stack/structs/entries/nwk-pan-id.d.ts +8 -8
  141. package/dist/adapter/z-stack/structs/entries/nwk-pan-id.js +15 -15
  142. package/dist/adapter/z-stack/structs/entries/nwk-sec-material-descriptor-entry.d.ts +13 -13
  143. package/dist/adapter/z-stack/structs/entries/nwk-sec-material-descriptor-entry.js +23 -23
  144. package/dist/adapter/z-stack/structs/entries/nwk-sec-material-descriptor-table.d.ts +10 -10
  145. package/dist/adapter/z-stack/structs/entries/nwk-sec-material-descriptor-table.js +22 -22
  146. package/dist/adapter/z-stack/structs/entries/security-manager-entry.d.ts +20 -20
  147. package/dist/adapter/z-stack/structs/entries/security-manager-entry.js +36 -36
  148. package/dist/adapter/z-stack/structs/entries/security-manager-table.d.ts +10 -10
  149. package/dist/adapter/z-stack/structs/entries/security-manager-table.js +24 -24
  150. package/dist/adapter/z-stack/structs/index.d.ts +4 -4
  151. package/dist/adapter/z-stack/structs/index.js +20 -20
  152. package/dist/adapter/z-stack/structs/serializable-memory-object.d.ts +13 -13
  153. package/dist/adapter/z-stack/structs/serializable-memory-object.js +2 -2
  154. package/dist/adapter/z-stack/structs/struct.d.ts +99 -99
  155. package/dist/adapter/z-stack/structs/struct.js +295 -295
  156. package/dist/adapter/z-stack/structs/table.d.ts +94 -94
  157. package/dist/adapter/z-stack/structs/table.js +161 -161
  158. package/dist/adapter/z-stack/unpi/constants.d.ts +28 -28
  159. package/dist/adapter/z-stack/unpi/constants.js +41 -41
  160. package/dist/adapter/z-stack/unpi/frame.d.ts +16 -16
  161. package/dist/adapter/z-stack/unpi/frame.js +48 -48
  162. package/dist/adapter/z-stack/unpi/index.d.ts +5 -5
  163. package/dist/adapter/z-stack/unpi/index.js +37 -37
  164. package/dist/adapter/z-stack/unpi/parser.d.ts +10 -10
  165. package/dist/adapter/z-stack/unpi/parser.js +74 -74
  166. package/dist/adapter/z-stack/unpi/writer.d.ts +10 -10
  167. package/dist/adapter/z-stack/unpi/writer.js +44 -44
  168. package/dist/adapter/z-stack/utils/channel-list.d.ts +20 -20
  169. package/dist/adapter/z-stack/utils/channel-list.js +40 -40
  170. package/dist/adapter/z-stack/utils/index.d.ts +2 -2
  171. package/dist/adapter/z-stack/utils/index.js +18 -18
  172. package/dist/adapter/z-stack/utils/network-options.d.ts +8 -8
  173. package/dist/adapter/z-stack/utils/network-options.js +22 -22
  174. package/dist/adapter/z-stack/znp/buffaloZnp.d.ts +11 -11
  175. package/dist/adapter/z-stack/znp/buffaloZnp.js +113 -113
  176. package/dist/adapter/z-stack/znp/definition.d.ts +5 -5
  177. package/dist/adapter/z-stack/znp/definition.js +3050 -3050
  178. package/dist/adapter/z-stack/znp/index.d.ts +3 -3
  179. package/dist/adapter/z-stack/znp/index.js +10 -10
  180. package/dist/adapter/z-stack/znp/parameterType.d.ts +22 -22
  181. package/dist/adapter/z-stack/znp/parameterType.js +25 -25
  182. package/dist/adapter/z-stack/znp/tstype.d.ts +21 -21
  183. package/dist/adapter/z-stack/znp/tstype.js +2 -2
  184. package/dist/adapter/z-stack/znp/znp.d.ts +43 -43
  185. package/dist/adapter/z-stack/znp/znp.js +325 -325
  186. package/dist/adapter/z-stack/znp/zpiObject.d.ts +19 -19
  187. package/dist/adapter/z-stack/znp/zpiObject.js +96 -96
  188. package/dist/adapter/zigate/adapter/index.d.ts +2 -2
  189. package/dist/adapter/zigate/adapter/index.js +10 -10
  190. package/dist/adapter/zigate/adapter/zigateAdapter.d.ts +69 -69
  191. package/dist/adapter/zigate/adapter/zigateAdapter.js +678 -678
  192. package/dist/adapter/zigate/debug.d.ts +7 -7
  193. package/dist/adapter/zigate/debug.js +22 -22
  194. package/dist/adapter/zigate/driver/buffaloZiGate.d.ts +18 -18
  195. package/dist/adapter/zigate/driver/buffaloZiGate.js +139 -139
  196. package/dist/adapter/zigate/driver/commandType.d.ts +41 -41
  197. package/dist/adapter/zigate/driver/commandType.js +385 -385
  198. package/dist/adapter/zigate/driver/constants.d.ts +276 -276
  199. package/dist/adapter/zigate/driver/constants.js +371 -371
  200. package/dist/adapter/zigate/driver/frame.d.ts +26 -26
  201. package/dist/adapter/zigate/driver/frame.js +172 -172
  202. package/dist/adapter/zigate/driver/frame.js.map +1 -1
  203. package/dist/adapter/zigate/driver/messageType.d.ts +11 -11
  204. package/dist/adapter/zigate/driver/messageType.js +278 -278
  205. package/dist/adapter/zigate/driver/parameterType.d.ts +20 -20
  206. package/dist/adapter/zigate/driver/parameterType.js +23 -23
  207. package/dist/adapter/zigate/driver/ziGateObject.d.ts +23 -23
  208. package/dist/adapter/zigate/driver/ziGateObject.js +106 -106
  209. package/dist/adapter/zigate/driver/zigate.d.ts +49 -49
  210. package/dist/adapter/zigate/driver/zigate.js +303 -303
  211. package/dist/buffalo/buffalo.d.ts +50 -50
  212. package/dist/buffalo/buffalo.js +322 -322
  213. package/dist/buffalo/index.d.ts +3 -3
  214. package/dist/buffalo/index.js +33 -33
  215. package/dist/buffalo/tstype.d.ts +8 -8
  216. package/dist/buffalo/tstype.js +2 -2
  217. package/dist/controller/controller.d.ts +113 -110
  218. package/dist/controller/controller.d.ts.map +1 -1
  219. package/dist/controller/controller.js +619 -607
  220. package/dist/controller/controller.js.map +1 -1
  221. package/dist/controller/database.d.ts +18 -18
  222. package/dist/controller/database.js +93 -93
  223. package/dist/controller/events.d.ts +58 -55
  224. package/dist/controller/events.d.ts.map +1 -1
  225. package/dist/controller/events.js +101 -101
  226. package/dist/controller/events.js.map +1 -1
  227. package/dist/controller/greenPower.d.ts +12 -12
  228. package/dist/controller/greenPower.js +220 -220
  229. package/dist/controller/helpers/index.d.ts +2 -2
  230. package/dist/controller/helpers/index.js +28 -28
  231. package/dist/controller/helpers/request.d.ts +22 -22
  232. package/dist/controller/helpers/request.js +71 -71
  233. package/dist/controller/helpers/zclFrameConverter.d.ts +7 -7
  234. package/dist/controller/helpers/zclFrameConverter.js +31 -31
  235. package/dist/controller/helpers/zclTransactionSequenceNumber.d.ts +5 -5
  236. package/dist/controller/helpers/zclTransactionSequenceNumber.js +13 -13
  237. package/dist/controller/index.d.ts +5 -5
  238. package/dist/controller/index.js +8 -8
  239. package/dist/controller/logger-stub.d.ts +6 -6
  240. package/dist/controller/logger-stub.js +2 -2
  241. package/dist/controller/model/device.d.ts +133 -132
  242. package/dist/controller/model/device.d.ts.map +1 -1
  243. package/dist/controller/model/device.js +717 -708
  244. package/dist/controller/model/device.js.map +1 -1
  245. package/dist/controller/model/endpoint.d.ts +131 -131
  246. package/dist/controller/model/endpoint.d.ts.map +1 -1
  247. package/dist/controller/model/endpoint.js +816 -817
  248. package/dist/controller/model/endpoint.js.map +1 -1
  249. package/dist/controller/model/entity.d.ts +14 -14
  250. package/dist/controller/model/entity.js +26 -26
  251. package/dist/controller/model/group.d.ts +38 -38
  252. package/dist/controller/model/group.js +221 -221
  253. package/dist/controller/model/index.d.ts +5 -5
  254. package/dist/controller/model/index.js +14 -14
  255. package/dist/controller/touchlink.d.ts +19 -19
  256. package/dist/controller/touchlink.js +157 -157
  257. package/dist/controller/tstype.d.ts +21 -21
  258. package/dist/controller/tstype.js +9 -9
  259. package/dist/index.d.ts +3 -3
  260. package/dist/index.js +33 -33
  261. package/dist/models/backup-storage-legacy.d.ts +26 -26
  262. package/dist/models/backup-storage-legacy.js +2 -2
  263. package/dist/models/backup-storage-unified.d.ts +49 -49
  264. package/dist/models/backup-storage-unified.js +2 -2
  265. package/dist/models/backup.d.ts +37 -37
  266. package/dist/models/backup.js +2 -2
  267. package/dist/models/index.d.ts +4 -4
  268. package/dist/models/index.js +20 -20
  269. package/dist/models/network-options.d.ts +12 -12
  270. package/dist/models/network-options.js +2 -2
  271. package/dist/utils/assertString.d.ts +2 -2
  272. package/dist/utils/assertString.js +8 -8
  273. package/dist/utils/backup.d.ts +20 -20
  274. package/dist/utils/backup.js +187 -187
  275. package/dist/utils/equalsPartial.d.ts +2 -2
  276. package/dist/utils/equalsPartial.js +11 -11
  277. package/dist/utils/index.d.ts +9 -9
  278. package/dist/utils/index.js +45 -45
  279. package/dist/utils/isNumberArray.d.ts +2 -2
  280. package/dist/utils/isNumberArray.js +6 -6
  281. package/dist/utils/queue.d.ts +11 -11
  282. package/dist/utils/queue.js +50 -50
  283. package/dist/utils/realpathSync.d.ts +2 -2
  284. package/dist/utils/realpathSync.js +12 -12
  285. package/dist/utils/wait.d.ts +2 -2
  286. package/dist/utils/wait.js +8 -8
  287. package/dist/utils/waitress.d.ts +21 -21
  288. package/dist/utils/waitress.js +61 -61
  289. package/dist/zcl/buffaloZcl.d.ts +41 -41
  290. package/dist/zcl/buffaloZcl.js +591 -591
  291. package/dist/zcl/definition/buffaloZclDataType.d.ts +17 -17
  292. package/dist/zcl/definition/buffaloZclDataType.js +20 -20
  293. package/dist/zcl/definition/cluster.d.ts +29 -29
  294. package/dist/zcl/definition/cluster.js +5335 -5335
  295. package/dist/zcl/definition/dataType.d.ts +59 -59
  296. package/dist/zcl/definition/dataType.js +64 -64
  297. package/dist/zcl/definition/direction.d.ts +5 -5
  298. package/dist/zcl/definition/direction.js +8 -8
  299. package/dist/zcl/definition/endpointDeviceType.d.ts +4 -4
  300. package/dist/zcl/definition/endpointDeviceType.js +15 -15
  301. package/dist/zcl/definition/foundation.d.ts +11 -11
  302. package/dist/zcl/definition/foundation.js +167 -167
  303. package/dist/zcl/definition/frameControl.d.ts +10 -10
  304. package/dist/zcl/definition/frameControl.js +2 -2
  305. package/dist/zcl/definition/frameType.d.ts +5 -5
  306. package/dist/zcl/definition/frameType.js +8 -8
  307. package/dist/zcl/definition/index.d.ts +13 -13
  308. package/dist/zcl/definition/index.js +51 -51
  309. package/dist/zcl/definition/manufacturerCode.d.ts +1074 -1074
  310. package/dist/zcl/definition/manufacturerCode.js +1079 -1079
  311. package/dist/zcl/definition/powerSource.d.ts +4 -4
  312. package/dist/zcl/definition/powerSource.js +12 -12
  313. package/dist/zcl/definition/status.d.ts +38 -38
  314. package/dist/zcl/definition/status.js +41 -41
  315. package/dist/zcl/definition/tstype.d.ts +16 -16
  316. package/dist/zcl/definition/tstype.js +2 -2
  317. package/dist/zcl/index.d.ts +15 -15
  318. package/dist/zcl/index.js +55 -55
  319. package/dist/zcl/tstype.d.ts +56 -56
  320. package/dist/zcl/tstype.js +10 -10
  321. package/dist/zcl/utils.d.ts +6 -6
  322. package/dist/zcl/utils.js +165 -165
  323. package/dist/zcl/zclFrame.d.ts +45 -45
  324. package/dist/zcl/zclFrame.js +347 -347
  325. package/dist/zcl/zclStatusError.d.ts +5 -5
  326. package/dist/zcl/zclStatusError.js +13 -13
  327. package/package.json +1 -1
@@ -1,369 +1,369 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.SerialDriver = void 0;
30
- /* istanbul ignore file */
31
- const events_1 = require("events");
32
- const net_1 = __importDefault(require("net"));
33
- const serialPort_1 = require("../../serialPort");
34
- const socketPortUtils_1 = __importDefault(require("../../socketPortUtils"));
35
- const utils_1 = require("./utils");
36
- const utils_2 = require("../../../utils");
37
- const consts = __importStar(require("./consts"));
38
- const writer_1 = require("./writer");
39
- const parser_1 = require("./parser");
40
- const debug_1 = __importDefault(require("debug"));
41
- const debug = (0, debug_1.default)('zigbee-herdsman:adapter:ezsp:uart');
42
- var NcpResetCode;
43
- (function (NcpResetCode) {
44
- NcpResetCode[NcpResetCode["RESET_UNKNOWN_REASON"] = 0] = "RESET_UNKNOWN_REASON";
45
- NcpResetCode[NcpResetCode["RESET_EXTERNAL"] = 1] = "RESET_EXTERNAL";
46
- NcpResetCode[NcpResetCode["RESET_POWER_ON"] = 2] = "RESET_POWER_ON";
47
- NcpResetCode[NcpResetCode["RESET_WATCHDOG"] = 3] = "RESET_WATCHDOG";
48
- NcpResetCode[NcpResetCode["RESET_ASSERT"] = 6] = "RESET_ASSERT";
49
- NcpResetCode[NcpResetCode["RESET_BOOTLOADER"] = 9] = "RESET_BOOTLOADER";
50
- NcpResetCode[NcpResetCode["RESET_SOFTWARE"] = 11] = "RESET_SOFTWARE";
51
- NcpResetCode[NcpResetCode["ERROR_EXCEEDED_MAXIMUM_ACK_TIMEOUT_COUNT"] = 81] = "ERROR_EXCEEDED_MAXIMUM_ACK_TIMEOUT_COUNT";
52
- NcpResetCode[NcpResetCode["ERROR_UNKNOWN_EM3XX_ERROR"] = 128] = "ERROR_UNKNOWN_EM3XX_ERROR";
53
- })(NcpResetCode || (NcpResetCode = {}));
54
- class SerialDriver extends events_1.EventEmitter {
55
- constructor() {
56
- super();
57
- this.sendSeq = 0; // next frame number to send
58
- this.recvSeq = 0; // next frame number to receive
59
- this.ackSeq = 0; // next number after the last accepted frame
60
- this.initialized = false;
61
- this.queue = new utils_2.Queue(1);
62
- this.waitress = new utils_2.Waitress(this.waitressValidator, this.waitressTimeoutFormatter);
63
- }
64
- async connect(path, options) {
65
- this.portType = socketPortUtils_1.default.isTcpPath(path) ? 'socket' : 'serial';
66
- if (this.portType === 'serial') {
67
- await this.openSerialPort(path, options);
68
- }
69
- else {
70
- await this.openSocketPort(path);
71
- }
72
- }
73
- async openSerialPort(path, opt) {
74
- const options = {
75
- path,
76
- baudRate: typeof opt.baudRate === 'number' ? opt.baudRate : 115200,
77
- rtscts: typeof opt.rtscts === 'boolean' ? opt.rtscts : false,
78
- autoOpen: false
79
- };
80
- debug(`Opening SerialPort with ${JSON.stringify(options)}`);
81
- this.serialPort = new serialPort_1.SerialPort(options);
82
- this.writer = new writer_1.Writer();
83
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
84
- // @ts-ignore
85
- this.writer.pipe(this.serialPort);
86
- this.parser = new parser_1.Parser();
87
- this.serialPort.pipe(this.parser);
88
- this.parser.on('parsed', this.onParsed.bind(this));
89
- return new Promise((resolve, reject) => {
90
- this.serialPort.open(async (error) => {
91
- if (error) {
92
- this.initialized = false;
93
- if (this.serialPort.isOpen) {
94
- this.serialPort.close();
95
- }
96
- reject(new Error(`Error while opening serialport '${error}'`));
97
- }
98
- else {
99
- debug('Serialport opened');
100
- this.serialPort.once('close', this.onPortClose.bind(this));
101
- this.serialPort.once('error', (error) => {
102
- debug(`Serialport error: ${error}`);
103
- });
104
- // reset
105
- await this.reset();
106
- this.initialized = true;
107
- this.emit('connected');
108
- resolve();
109
- }
110
- });
111
- });
112
- }
113
- async openSocketPort(path) {
114
- const info = socketPortUtils_1.default.parseTcpPath(path);
115
- debug(`Opening TCP socket with ${info.host}:${info.port}`);
116
- this.socketPort = new net_1.default.Socket();
117
- this.socketPort.setNoDelay(true);
118
- this.socketPort.setKeepAlive(true, 15000);
119
- this.writer = new writer_1.Writer();
120
- this.writer.pipe(this.socketPort);
121
- this.parser = new parser_1.Parser();
122
- this.socketPort.pipe(this.parser);
123
- this.parser.on('parsed', this.onParsed.bind(this));
124
- return new Promise((resolve, reject) => {
125
- this.socketPort.on('connect', function () {
126
- debug('Socket connected');
127
- });
128
- // eslint-disable-next-line
129
- const self = this;
130
- this.socketPort.on('ready', async () => {
131
- debug('Socket ready');
132
- // reset
133
- await this.reset();
134
- self.initialized = true;
135
- this.emit('connected');
136
- resolve();
137
- });
138
- this.socketPort.once('close', this.onPortClose.bind(this));
139
- this.socketPort.on('error', function () {
140
- debug('Socket error');
141
- self.initialized = false;
142
- reject(new Error(`Error while opening socket`));
143
- });
144
- this.socketPort.connect(info.port, info.host);
145
- });
146
- }
147
- onParsed(data) {
148
- // check CRC
149
- const crc = (0, utils_1.crc16ccitt)(data.subarray(0, -3), 65535);
150
- const crcArr = Buffer.from([(crc >> 8), (crc % 256)]);
151
- if (!data.subarray(-3, -1).equals(crcArr)) {
152
- // CRC error
153
- debug(`<-- CRC error: ${data.toString('hex')}|` +
154
- `${data.subarray(-3, -1).toString('hex')}|` +
155
- `${crcArr.toString('hex')}`);
156
- // send NAK
157
- this.writer.sendNAK(this.recvSeq);
158
- // skip handler
159
- return;
160
- }
161
- try {
162
- /* Frame receive handler */
163
- switch (true) {
164
- case ((data[0] & 0x80) === 0):
165
- debug(`<-- DATA (${(data[0] & 0x70) >> 4},` +
166
- `${data[0] & 0x07},${(data[0] & 0x08) >> 3}): ${data.toString('hex')}`);
167
- this.handleDATA(data);
168
- break;
169
- case ((data[0] & 0xE0) === 0x80):
170
- debug(`<-- ACK (${data[0] & 0x07}): ${data.toString('hex')}`);
171
- this.handleACK(data[0]);
172
- break;
173
- case ((data[0] & 0xE0) === 0xA0):
174
- debug(`<-- NAK (${data[0] & 0x07}): ${data.toString('hex')}`);
175
- this.handleNAK(data[0]);
176
- break;
177
- case (data[0] === 0xC0):
178
- debug(`<-- RST: ${data.toString('hex')}`);
179
- break;
180
- case (data[0] === 0xC1):
181
- debug(`<-- RSTACK: ${data.toString('hex')}`);
182
- this.rstack_frame_received(data);
183
- break;
184
- case (data[0] === 0xC2):
185
- debug(`<-- Error: ${data.toString('hex')}`);
186
- // send reset
187
- this.reset();
188
- break;
189
- default:
190
- debug("UNKNOWN FRAME RECEIVED: %r", data);
191
- }
192
- }
193
- catch (error) {
194
- debug(`Error while parsing to ZpiObject '${error.stack}'`);
195
- }
196
- }
197
- handleDATA(data) {
198
- /* Data frame receive handler */
199
- const frmNum = (data[0] & 0x70) >> 4;
200
- const reTx = (data[0] & 0x08) >> 3;
201
- this.recvSeq = (frmNum + 1) & 7; // next
202
- debug(`--> ACK (${this.recvSeq})`);
203
- this.writer.sendACK(this.recvSeq);
204
- const handled = this.handleACK(data[0]);
205
- if (reTx && !handled) {
206
- // if the package is resent and did not expect it,
207
- // then will skip it - already processed it earlier
208
- debug(`Skipping the packet as repeated (${this.recvSeq})`);
209
- return;
210
- }
211
- data = data.subarray(1, -3);
212
- const frame = this.randomize(data);
213
- this.emit('received', frame);
214
- }
215
- handleACK(control) {
216
- /* Handle an acknowledgement frame */
217
- // next number after the last accepted frame
218
- this.ackSeq = control & 0x07;
219
- const handled = this.waitress.resolve({ sequence: this.ackSeq });
220
- if (!handled && this.sendSeq !== this.ackSeq) {
221
- debug(`Unexpected packet sequence ${this.ackSeq} | ${this.sendSeq}`);
222
- }
223
- return handled;
224
- }
225
- handleNAK(control) {
226
- /* Handle negative acknowledgment frame */
227
- const nakNum = control & 0x07;
228
- const handled = this.waitress.reject({ sequence: nakNum }, 'Recv NAK frame');
229
- if (!handled) {
230
- // send NAK
231
- debug(`NAK Unexpected packet sequence ${nakNum}`);
232
- }
233
- else {
234
- debug(`NAK Expected packet sequence ${nakNum}`);
235
- }
236
- }
237
- rstack_frame_received(data) {
238
- /* Reset acknowledgement frame receive handler */
239
- let code;
240
- this.sendSeq = 0;
241
- this.recvSeq = 0;
242
- try {
243
- code = NcpResetCode[data[2]];
244
- }
245
- catch (e) {
246
- code = NcpResetCode.ERROR_UNKNOWN_EM3XX_ERROR;
247
- }
248
- debug("RSTACK Version: %d Reason: %s frame: %s", data[1], code.toString(), data.toString('hex'));
249
- if (NcpResetCode[code].toString() !== NcpResetCode.RESET_SOFTWARE.toString()) {
250
- return;
251
- }
252
- this.waitress.resolve({ sequence: -1 });
253
- }
254
- randomize(s) {
255
- /*XOR s with a pseudo-random sequence for transmission
256
- Used only in data frames
257
- */
258
- let rand = consts.RANDOMIZE_START;
259
- const out = Buffer.alloc(s.length);
260
- let outIdx = 0;
261
- for (const c of s) {
262
- out.writeUInt8(c ^ rand, outIdx++);
263
- if ((rand % 2)) {
264
- rand = ((rand >> 1) ^ consts.RANDOMIZE_SEQ);
265
- }
266
- else {
267
- rand = (rand >> 1);
268
- }
269
- }
270
- return out;
271
- }
272
- async reset() {
273
- debug('Uart reseting');
274
- this.parser.reset();
275
- this.queue.clear();
276
- return this.queue.execute(async () => {
277
- debug(`--> Write reset`);
278
- const waiter = this.waitFor(-1, 10000).start();
279
- this.writer.sendReset();
280
- debug(`-?- waiting reset`);
281
- return waiter.promise.catch(async (e) => {
282
- debug(`--> Error: ${e}`);
283
- this.emit('reset');
284
- throw new Error(`Reset error: ${e}`);
285
- }).then(() => {
286
- debug(`-+- waiting reset success`);
287
- });
288
- });
289
- }
290
- close() {
291
- return new Promise((resolve, reject) => {
292
- this.queue.clear();
293
- if (this.initialized) {
294
- if (this.portType === 'serial') {
295
- this.serialPort.flush(() => {
296
- this.serialPort.close((error) => {
297
- this.initialized = false;
298
- this.emit('close');
299
- error == null ?
300
- resolve() :
301
- reject(new Error(`Error while closing serialport '${error}'`));
302
- });
303
- });
304
- }
305
- else {
306
- this.socketPort.destroy();
307
- resolve();
308
- }
309
- }
310
- else {
311
- this.emit('close');
312
- resolve();
313
- }
314
- });
315
- }
316
- onPortClose() {
317
- debug('Port closed');
318
- this.initialized = false;
319
- this.emit('close');
320
- }
321
- isInitialized() {
322
- return this.initialized;
323
- }
324
- async sendDATA(data) {
325
- const seq = this.sendSeq;
326
- this.sendSeq = ((seq + 1) % 8); // next
327
- const nextSeq = this.sendSeq;
328
- const ackSeq = this.recvSeq;
329
- return this.queue.execute(async () => {
330
- debug(`--> DATA (${seq},${ackSeq},0): ${data.toString('hex')}`);
331
- const randData = this.randomize(data);
332
- const waiter = this.waitFor(nextSeq).start();
333
- this.writer.sendData(randData, seq, 0, ackSeq);
334
- debug(`-?- waiting (${nextSeq})`);
335
- return waiter.promise.catch(async (e) => {
336
- debug(`--> Error: ${e}`);
337
- debug(`-!- break waiting (${nextSeq})`);
338
- debug(`Can't send DATA frame (${seq},${ackSeq},0): ${data.toString('hex')}`);
339
- await (0, utils_2.Wait)(500);
340
- debug(`->> DATA (${seq},${ackSeq},1): ${data.toString('hex')}`);
341
- const waiter = this.waitFor(nextSeq).start();
342
- this.writer.sendData(randData, seq, 1, ackSeq);
343
- debug(`-?- rewaiting (${nextSeq})`);
344
- return waiter.promise.catch(async (e) => {
345
- debug(`--> Error: ${e}`);
346
- debug(`-!- break rewaiting (${nextSeq})`);
347
- debug(`Can't resend DATA frame (${seq},${ackSeq},1): ${data.toString('hex')}`);
348
- this.emit('reset');
349
- throw new Error(`sendDATA error: ${e}`);
350
- }).then(() => {
351
- debug(`-+- rewaiting (${nextSeq}) success`);
352
- });
353
- }).then(() => {
354
- debug(`-+- waiting (${nextSeq}) success`);
355
- });
356
- });
357
- }
358
- waitFor(sequence, timeout = 2000) {
359
- return this.waitress.waitFor({ sequence }, timeout);
360
- }
361
- waitressTimeoutFormatter(matcher, timeout) {
362
- return `${JSON.stringify(matcher)} after ${timeout}ms`;
363
- }
364
- waitressValidator(payload, matcher) {
365
- return (payload.sequence === matcher.sequence);
366
- }
367
- }
368
- exports.SerialDriver = SerialDriver;
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.SerialDriver = void 0;
30
+ /* istanbul ignore file */
31
+ const events_1 = require("events");
32
+ const net_1 = __importDefault(require("net"));
33
+ const serialPort_1 = require("../../serialPort");
34
+ const socketPortUtils_1 = __importDefault(require("../../socketPortUtils"));
35
+ const utils_1 = require("./utils");
36
+ const utils_2 = require("../../../utils");
37
+ const consts = __importStar(require("./consts"));
38
+ const writer_1 = require("./writer");
39
+ const parser_1 = require("./parser");
40
+ const debug_1 = __importDefault(require("debug"));
41
+ const debug = (0, debug_1.default)('zigbee-herdsman:adapter:ezsp:uart');
42
+ var NcpResetCode;
43
+ (function (NcpResetCode) {
44
+ NcpResetCode[NcpResetCode["RESET_UNKNOWN_REASON"] = 0] = "RESET_UNKNOWN_REASON";
45
+ NcpResetCode[NcpResetCode["RESET_EXTERNAL"] = 1] = "RESET_EXTERNAL";
46
+ NcpResetCode[NcpResetCode["RESET_POWER_ON"] = 2] = "RESET_POWER_ON";
47
+ NcpResetCode[NcpResetCode["RESET_WATCHDOG"] = 3] = "RESET_WATCHDOG";
48
+ NcpResetCode[NcpResetCode["RESET_ASSERT"] = 6] = "RESET_ASSERT";
49
+ NcpResetCode[NcpResetCode["RESET_BOOTLOADER"] = 9] = "RESET_BOOTLOADER";
50
+ NcpResetCode[NcpResetCode["RESET_SOFTWARE"] = 11] = "RESET_SOFTWARE";
51
+ NcpResetCode[NcpResetCode["ERROR_EXCEEDED_MAXIMUM_ACK_TIMEOUT_COUNT"] = 81] = "ERROR_EXCEEDED_MAXIMUM_ACK_TIMEOUT_COUNT";
52
+ NcpResetCode[NcpResetCode["ERROR_UNKNOWN_EM3XX_ERROR"] = 128] = "ERROR_UNKNOWN_EM3XX_ERROR";
53
+ })(NcpResetCode || (NcpResetCode = {}));
54
+ class SerialDriver extends events_1.EventEmitter {
55
+ constructor() {
56
+ super();
57
+ this.sendSeq = 0; // next frame number to send
58
+ this.recvSeq = 0; // next frame number to receive
59
+ this.ackSeq = 0; // next number after the last accepted frame
60
+ this.initialized = false;
61
+ this.queue = new utils_2.Queue(1);
62
+ this.waitress = new utils_2.Waitress(this.waitressValidator, this.waitressTimeoutFormatter);
63
+ }
64
+ async connect(path, options) {
65
+ this.portType = socketPortUtils_1.default.isTcpPath(path) ? 'socket' : 'serial';
66
+ if (this.portType === 'serial') {
67
+ await this.openSerialPort(path, options);
68
+ }
69
+ else {
70
+ await this.openSocketPort(path);
71
+ }
72
+ }
73
+ async openSerialPort(path, opt) {
74
+ const options = {
75
+ path,
76
+ baudRate: typeof opt.baudRate === 'number' ? opt.baudRate : 115200,
77
+ rtscts: typeof opt.rtscts === 'boolean' ? opt.rtscts : false,
78
+ autoOpen: false
79
+ };
80
+ debug(`Opening SerialPort with ${JSON.stringify(options)}`);
81
+ this.serialPort = new serialPort_1.SerialPort(options);
82
+ this.writer = new writer_1.Writer();
83
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
84
+ // @ts-ignore
85
+ this.writer.pipe(this.serialPort);
86
+ this.parser = new parser_1.Parser();
87
+ this.serialPort.pipe(this.parser);
88
+ this.parser.on('parsed', this.onParsed.bind(this));
89
+ return new Promise((resolve, reject) => {
90
+ this.serialPort.open(async (error) => {
91
+ if (error) {
92
+ this.initialized = false;
93
+ if (this.serialPort.isOpen) {
94
+ this.serialPort.close();
95
+ }
96
+ reject(new Error(`Error while opening serialport '${error}'`));
97
+ }
98
+ else {
99
+ debug('Serialport opened');
100
+ this.serialPort.once('close', this.onPortClose.bind(this));
101
+ this.serialPort.once('error', (error) => {
102
+ debug(`Serialport error: ${error}`);
103
+ });
104
+ // reset
105
+ await this.reset();
106
+ this.initialized = true;
107
+ this.emit('connected');
108
+ resolve();
109
+ }
110
+ });
111
+ });
112
+ }
113
+ async openSocketPort(path) {
114
+ const info = socketPortUtils_1.default.parseTcpPath(path);
115
+ debug(`Opening TCP socket with ${info.host}:${info.port}`);
116
+ this.socketPort = new net_1.default.Socket();
117
+ this.socketPort.setNoDelay(true);
118
+ this.socketPort.setKeepAlive(true, 15000);
119
+ this.writer = new writer_1.Writer();
120
+ this.writer.pipe(this.socketPort);
121
+ this.parser = new parser_1.Parser();
122
+ this.socketPort.pipe(this.parser);
123
+ this.parser.on('parsed', this.onParsed.bind(this));
124
+ return new Promise((resolve, reject) => {
125
+ this.socketPort.on('connect', function () {
126
+ debug('Socket connected');
127
+ });
128
+ // eslint-disable-next-line
129
+ const self = this;
130
+ this.socketPort.on('ready', async () => {
131
+ debug('Socket ready');
132
+ // reset
133
+ await this.reset();
134
+ self.initialized = true;
135
+ this.emit('connected');
136
+ resolve();
137
+ });
138
+ this.socketPort.once('close', this.onPortClose.bind(this));
139
+ this.socketPort.on('error', function () {
140
+ debug('Socket error');
141
+ self.initialized = false;
142
+ reject(new Error(`Error while opening socket`));
143
+ });
144
+ this.socketPort.connect(info.port, info.host);
145
+ });
146
+ }
147
+ onParsed(data) {
148
+ // check CRC
149
+ const crc = (0, utils_1.crc16ccitt)(data.subarray(0, -3), 65535);
150
+ const crcArr = Buffer.from([(crc >> 8), (crc % 256)]);
151
+ if (!data.subarray(-3, -1).equals(crcArr)) {
152
+ // CRC error
153
+ debug(`<-- CRC error: ${data.toString('hex')}|` +
154
+ `${data.subarray(-3, -1).toString('hex')}|` +
155
+ `${crcArr.toString('hex')}`);
156
+ // send NAK
157
+ this.writer.sendNAK(this.recvSeq);
158
+ // skip handler
159
+ return;
160
+ }
161
+ try {
162
+ /* Frame receive handler */
163
+ switch (true) {
164
+ case ((data[0] & 0x80) === 0):
165
+ debug(`<-- DATA (${(data[0] & 0x70) >> 4},` +
166
+ `${data[0] & 0x07},${(data[0] & 0x08) >> 3}): ${data.toString('hex')}`);
167
+ this.handleDATA(data);
168
+ break;
169
+ case ((data[0] & 0xE0) === 0x80):
170
+ debug(`<-- ACK (${data[0] & 0x07}): ${data.toString('hex')}`);
171
+ this.handleACK(data[0]);
172
+ break;
173
+ case ((data[0] & 0xE0) === 0xA0):
174
+ debug(`<-- NAK (${data[0] & 0x07}): ${data.toString('hex')}`);
175
+ this.handleNAK(data[0]);
176
+ break;
177
+ case (data[0] === 0xC0):
178
+ debug(`<-- RST: ${data.toString('hex')}`);
179
+ break;
180
+ case (data[0] === 0xC1):
181
+ debug(`<-- RSTACK: ${data.toString('hex')}`);
182
+ this.rstack_frame_received(data);
183
+ break;
184
+ case (data[0] === 0xC2):
185
+ debug(`<-- Error: ${data.toString('hex')}`);
186
+ // send reset
187
+ this.reset();
188
+ break;
189
+ default:
190
+ debug("UNKNOWN FRAME RECEIVED: %r", data);
191
+ }
192
+ }
193
+ catch (error) {
194
+ debug(`Error while parsing to ZpiObject '${error.stack}'`);
195
+ }
196
+ }
197
+ handleDATA(data) {
198
+ /* Data frame receive handler */
199
+ const frmNum = (data[0] & 0x70) >> 4;
200
+ const reTx = (data[0] & 0x08) >> 3;
201
+ this.recvSeq = (frmNum + 1) & 7; // next
202
+ debug(`--> ACK (${this.recvSeq})`);
203
+ this.writer.sendACK(this.recvSeq);
204
+ const handled = this.handleACK(data[0]);
205
+ if (reTx && !handled) {
206
+ // if the package is resent and did not expect it,
207
+ // then will skip it - already processed it earlier
208
+ debug(`Skipping the packet as repeated (${this.recvSeq})`);
209
+ return;
210
+ }
211
+ data = data.subarray(1, -3);
212
+ const frame = this.randomize(data);
213
+ this.emit('received', frame);
214
+ }
215
+ handleACK(control) {
216
+ /* Handle an acknowledgement frame */
217
+ // next number after the last accepted frame
218
+ this.ackSeq = control & 0x07;
219
+ const handled = this.waitress.resolve({ sequence: this.ackSeq });
220
+ if (!handled && this.sendSeq !== this.ackSeq) {
221
+ debug(`Unexpected packet sequence ${this.ackSeq} | ${this.sendSeq}`);
222
+ }
223
+ return handled;
224
+ }
225
+ handleNAK(control) {
226
+ /* Handle negative acknowledgment frame */
227
+ const nakNum = control & 0x07;
228
+ const handled = this.waitress.reject({ sequence: nakNum }, 'Recv NAK frame');
229
+ if (!handled) {
230
+ // send NAK
231
+ debug(`NAK Unexpected packet sequence ${nakNum}`);
232
+ }
233
+ else {
234
+ debug(`NAK Expected packet sequence ${nakNum}`);
235
+ }
236
+ }
237
+ rstack_frame_received(data) {
238
+ /* Reset acknowledgement frame receive handler */
239
+ let code;
240
+ this.sendSeq = 0;
241
+ this.recvSeq = 0;
242
+ try {
243
+ code = NcpResetCode[data[2]];
244
+ }
245
+ catch (e) {
246
+ code = NcpResetCode.ERROR_UNKNOWN_EM3XX_ERROR;
247
+ }
248
+ debug("RSTACK Version: %d Reason: %s frame: %s", data[1], code.toString(), data.toString('hex'));
249
+ if (NcpResetCode[code].toString() !== NcpResetCode.RESET_SOFTWARE.toString()) {
250
+ return;
251
+ }
252
+ this.waitress.resolve({ sequence: -1 });
253
+ }
254
+ randomize(s) {
255
+ /*XOR s with a pseudo-random sequence for transmission
256
+ Used only in data frames
257
+ */
258
+ let rand = consts.RANDOMIZE_START;
259
+ const out = Buffer.alloc(s.length);
260
+ let outIdx = 0;
261
+ for (const c of s) {
262
+ out.writeUInt8(c ^ rand, outIdx++);
263
+ if ((rand % 2)) {
264
+ rand = ((rand >> 1) ^ consts.RANDOMIZE_SEQ);
265
+ }
266
+ else {
267
+ rand = (rand >> 1);
268
+ }
269
+ }
270
+ return out;
271
+ }
272
+ async reset() {
273
+ debug('Uart reseting');
274
+ this.parser.reset();
275
+ this.queue.clear();
276
+ return this.queue.execute(async () => {
277
+ debug(`--> Write reset`);
278
+ const waiter = this.waitFor(-1, 10000).start();
279
+ this.writer.sendReset();
280
+ debug(`-?- waiting reset`);
281
+ return waiter.promise.catch(async (e) => {
282
+ debug(`--> Error: ${e}`);
283
+ this.emit('reset');
284
+ throw new Error(`Reset error: ${e}`);
285
+ }).then(() => {
286
+ debug(`-+- waiting reset success`);
287
+ });
288
+ });
289
+ }
290
+ close() {
291
+ return new Promise((resolve, reject) => {
292
+ this.queue.clear();
293
+ if (this.initialized) {
294
+ if (this.portType === 'serial') {
295
+ this.serialPort.flush(() => {
296
+ this.serialPort.close((error) => {
297
+ this.initialized = false;
298
+ this.emit('close');
299
+ error == null ?
300
+ resolve() :
301
+ reject(new Error(`Error while closing serialport '${error}'`));
302
+ });
303
+ });
304
+ }
305
+ else {
306
+ this.socketPort.destroy();
307
+ resolve();
308
+ }
309
+ }
310
+ else {
311
+ this.emit('close');
312
+ resolve();
313
+ }
314
+ });
315
+ }
316
+ onPortClose() {
317
+ debug('Port closed');
318
+ this.initialized = false;
319
+ this.emit('close');
320
+ }
321
+ isInitialized() {
322
+ return this.initialized;
323
+ }
324
+ async sendDATA(data) {
325
+ const seq = this.sendSeq;
326
+ this.sendSeq = ((seq + 1) % 8); // next
327
+ const nextSeq = this.sendSeq;
328
+ const ackSeq = this.recvSeq;
329
+ return this.queue.execute(async () => {
330
+ debug(`--> DATA (${seq},${ackSeq},0): ${data.toString('hex')}`);
331
+ const randData = this.randomize(data);
332
+ const waiter = this.waitFor(nextSeq).start();
333
+ this.writer.sendData(randData, seq, 0, ackSeq);
334
+ debug(`-?- waiting (${nextSeq})`);
335
+ return waiter.promise.catch(async (e) => {
336
+ debug(`--> Error: ${e}`);
337
+ debug(`-!- break waiting (${nextSeq})`);
338
+ debug(`Can't send DATA frame (${seq},${ackSeq},0): ${data.toString('hex')}`);
339
+ await (0, utils_2.Wait)(500);
340
+ debug(`->> DATA (${seq},${ackSeq},1): ${data.toString('hex')}`);
341
+ const waiter = this.waitFor(nextSeq).start();
342
+ this.writer.sendData(randData, seq, 1, ackSeq);
343
+ debug(`-?- rewaiting (${nextSeq})`);
344
+ return waiter.promise.catch(async (e) => {
345
+ debug(`--> Error: ${e}`);
346
+ debug(`-!- break rewaiting (${nextSeq})`);
347
+ debug(`Can't resend DATA frame (${seq},${ackSeq},1): ${data.toString('hex')}`);
348
+ this.emit('reset');
349
+ throw new Error(`sendDATA error: ${e}`);
350
+ }).then(() => {
351
+ debug(`-+- rewaiting (${nextSeq}) success`);
352
+ });
353
+ }).then(() => {
354
+ debug(`-+- waiting (${nextSeq}) success`);
355
+ });
356
+ });
357
+ }
358
+ waitFor(sequence, timeout = 2000) {
359
+ return this.waitress.waitFor({ sequence }, timeout);
360
+ }
361
+ waitressTimeoutFormatter(matcher, timeout) {
362
+ return `${JSON.stringify(matcher)} after ${timeout}ms`;
363
+ }
364
+ waitressValidator(payload, matcher) {
365
+ return (payload.sequence === matcher.sequence);
366
+ }
367
+ }
368
+ exports.SerialDriver = SerialDriver;
369
369
  //# sourceMappingURL=uart.js.map