brilliantsole 0.0.1

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 (158) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +21 -0
  3. package/build/brilliantsole.cjs +5957 -0
  4. package/build/brilliantsole.cjs.map +1 -0
  5. package/build/brilliantsole.js +5448 -0
  6. package/build/brilliantsole.js.map +1 -0
  7. package/build/brilliantsole.ls.js +4872 -0
  8. package/build/brilliantsole.ls.js.map +1 -0
  9. package/build/brilliantsole.min.js +6 -0
  10. package/build/brilliantsole.min.js.map +1 -0
  11. package/build/brilliantsole.module.d.ts +908 -0
  12. package/build/brilliantsole.module.js +5412 -0
  13. package/build/brilliantsole.module.js.map +1 -0
  14. package/build/brilliantsole.module.min.d.ts +908 -0
  15. package/build/brilliantsole.module.min.js +6 -0
  16. package/build/brilliantsole.module.min.js.map +1 -0
  17. package/build/brilliantsole.node.module.d.ts +908 -0
  18. package/build/brilliantsole.node.module.js +5906 -0
  19. package/build/brilliantsole.node.module.js.map +1 -0
  20. package/build/dts/BS.d.ts +25 -0
  21. package/build/dts/Device.d.ts +136 -0
  22. package/build/dts/DeviceInformationManager.d.ts +56 -0
  23. package/build/dts/DeviceManager.d.ts +67 -0
  24. package/build/dts/FileTransferManager.d.ts +84 -0
  25. package/build/dts/FirmwareManager.d.ts +71 -0
  26. package/build/dts/InformationManager.d.ts +66 -0
  27. package/build/dts/TfliteManager.d.ts +92 -0
  28. package/build/dts/connection/BaseConnectionManager.d.ts +59 -0
  29. package/build/dts/connection/ClientConnectionManager.d.ts +23 -0
  30. package/build/dts/connection/WebSocketClientConnectionManager.d.ts +23 -0
  31. package/build/dts/connection/bluetooth/BluetoothConnectionManager.d.ts +10 -0
  32. package/build/dts/connection/bluetooth/NobleConnectionManager.d.ts +42 -0
  33. package/build/dts/connection/bluetooth/WebBluetoothConnectionManager.d.ts +20 -0
  34. package/build/dts/connection/bluetooth/bluetoothUUIDs.d.ts +14 -0
  35. package/build/dts/connection/webSocket/ClientConnectionManager.d.ts +23 -0
  36. package/build/dts/connection/webSocket/WebSocketClientConnectionManager.d.ts +23 -0
  37. package/build/dts/devicePair/DevicePair.d.ts +60 -0
  38. package/build/dts/devicePair/DevicePairPressureSensorDataManager.d.ts +25 -0
  39. package/build/dts/devicePair/DevicePairSensorDataManager.d.ts +33 -0
  40. package/build/dts/scanner/BaseScanner.d.ts +66 -0
  41. package/build/dts/scanner/NobleScanner.d.ts +17 -0
  42. package/build/dts/scanner/Scanner.d.ts +3 -0
  43. package/build/dts/sensor/BarometerSensorDataManager.d.ts +16 -0
  44. package/build/dts/sensor/MotionSensorDataManager.d.ts +69 -0
  45. package/build/dts/sensor/PressureSensorDataManager.d.ts +36 -0
  46. package/build/dts/sensor/SensorConfigurationManager.d.ts +44 -0
  47. package/build/dts/sensor/SensorDataManager.d.ts +40 -0
  48. package/build/dts/server/BaseClient.d.ts +85 -0
  49. package/build/dts/server/BaseServer.d.ts +48 -0
  50. package/build/dts/server/ServerUtils.d.ts +23 -0
  51. package/build/dts/server/udp/UDPServer.d.ts +11 -0
  52. package/build/dts/server/udp/UDPUtils.d.ts +9 -0
  53. package/build/dts/server/websocket/WebSocketClient.d.ts +17 -0
  54. package/build/dts/server/websocket/WebSocketServer.d.ts +13 -0
  55. package/build/dts/server/websocket/WebSocketUtils.d.ts +9 -0
  56. package/build/dts/utils/ArrayBufferUtils.d.ts +7 -0
  57. package/build/dts/utils/ArrayUtils.d.ts +2 -0
  58. package/build/dts/utils/CenterOfPressureHelper.d.ts +15 -0
  59. package/build/dts/utils/Console.d.ts +34 -0
  60. package/build/dts/utils/EventDispatcher.d.ts +50 -0
  61. package/build/dts/utils/EventUtils.d.ts +6 -0
  62. package/build/dts/utils/MathUtils.d.ts +21 -0
  63. package/build/dts/utils/ParseUtils.d.ts +5 -0
  64. package/build/dts/utils/RangeHelper.d.ts +8 -0
  65. package/build/dts/utils/Text.d.ts +6 -0
  66. package/build/dts/utils/Timer.d.ts +14 -0
  67. package/build/dts/utils/TypeScriptUtils.d.ts +19 -0
  68. package/build/dts/utils/cbor.d.ts +6 -0
  69. package/build/dts/utils/checksum.d.ts +3 -0
  70. package/build/dts/utils/environment.d.ts +13 -0
  71. package/build/dts/utils/mcumgr.d.ts +88 -0
  72. package/build/dts/utils/stringUtils.d.ts +2 -0
  73. package/build/dts/vibration/VibrationManager.d.ts +45 -0
  74. package/build/dts/vibration/VibrationWaveformEffects.d.ts +2 -0
  75. package/build/index.d.ts +908 -0
  76. package/build/index.node.d.ts +908 -0
  77. package/examples/3d/index.html +109 -0
  78. package/examples/3d/scene.html +57 -0
  79. package/examples/3d/script.js +419 -0
  80. package/examples/balance/index.html +138 -0
  81. package/examples/balance/script.js +243 -0
  82. package/examples/basic/index.html +327 -0
  83. package/examples/basic/script.js +1093 -0
  84. package/examples/center-of-pressure/index.html +132 -0
  85. package/examples/center-of-pressure/script.js +207 -0
  86. package/examples/device-pair/index.html +72 -0
  87. package/examples/device-pair/script.js +187 -0
  88. package/examples/edge-impulse/index.html +94 -0
  89. package/examples/edge-impulse/script.js +1033 -0
  90. package/examples/graph/index.html +83 -0
  91. package/examples/graph/script.js +469 -0
  92. package/examples/machine-learning/index.html +366 -0
  93. package/examples/machine-learning/script.js +1774 -0
  94. package/examples/pressure/index.html +145 -0
  95. package/examples/pressure/script.js +201 -0
  96. package/examples/recording/index.html +187 -0
  97. package/examples/recording/script.js +736 -0
  98. package/examples/server/index.html +266 -0
  99. package/examples/server/script.js +925 -0
  100. package/examples/utils/aframe/fingertip-button-component.js +201 -0
  101. package/examples/utils/aframe/fingertip-collider-target-component.js +102 -0
  102. package/examples/utils/aframe/fingertip-colliders-component.js +147 -0
  103. package/examples/utils/three/three.module.min.js +24846 -0
  104. package/examples/webxr/index.html +221 -0
  105. package/examples/webxr/script.js +1127 -0
  106. package/package.json +83 -0
  107. package/src/BS.ts +68 -0
  108. package/src/Device.ts +734 -0
  109. package/src/DeviceInformationManager.ts +146 -0
  110. package/src/DeviceManager.ts +354 -0
  111. package/src/FileTransferManager.ts +452 -0
  112. package/src/FirmwareManager.ts +357 -0
  113. package/src/InformationManager.ts +283 -0
  114. package/src/TfliteManager.ts +450 -0
  115. package/src/connection/BaseConnectionManager.ts +255 -0
  116. package/src/connection/ClientConnectionManager.ts +120 -0
  117. package/src/connection/bluetooth/BluetoothConnectionManager.ts +34 -0
  118. package/src/connection/bluetooth/NobleConnectionManager.ts +302 -0
  119. package/src/connection/bluetooth/WebBluetoothConnectionManager.ts +269 -0
  120. package/src/connection/bluetooth/bluetoothUUIDs.ts +218 -0
  121. package/src/devicePair/DevicePair.ts +253 -0
  122. package/src/devicePair/DevicePairPressureSensorDataManager.ts +82 -0
  123. package/src/devicePair/DevicePairSensorDataManager.ts +90 -0
  124. package/src/scanner/BaseScanner.ts +189 -0
  125. package/src/scanner/NobleScanner.ts +195 -0
  126. package/src/scanner/Scanner.ts +16 -0
  127. package/src/sensor/BarometerSensorDataManager.ts +41 -0
  128. package/src/sensor/MotionSensorDataManager.ts +151 -0
  129. package/src/sensor/PressureSensorDataManager.ts +112 -0
  130. package/src/sensor/SensorConfigurationManager.ts +177 -0
  131. package/src/sensor/SensorDataManager.ts +166 -0
  132. package/src/server/BaseClient.ts +368 -0
  133. package/src/server/BaseServer.ts +344 -0
  134. package/src/server/ServerUtils.ts +93 -0
  135. package/src/server/udp/UDPServer.ts +229 -0
  136. package/src/server/udp/UDPUtils.ts +20 -0
  137. package/src/server/websocket/WebSocketClient.ts +179 -0
  138. package/src/server/websocket/WebSocketServer.ts +184 -0
  139. package/src/server/websocket/WebSocketUtils.ts +20 -0
  140. package/src/utils/ArrayBufferUtils.ts +88 -0
  141. package/src/utils/ArrayUtils.ts +15 -0
  142. package/src/utils/CenterOfPressureHelper.ts +39 -0
  143. package/src/utils/Console.ts +156 -0
  144. package/src/utils/EventDispatcher.ts +153 -0
  145. package/src/utils/EventUtils.ts +41 -0
  146. package/src/utils/MathUtils.ts +53 -0
  147. package/src/utils/ParseUtils.ts +46 -0
  148. package/src/utils/RangeHelper.ts +38 -0
  149. package/src/utils/Text.ts +30 -0
  150. package/src/utils/Timer.ts +72 -0
  151. package/src/utils/TypeScriptUtils.ts +22 -0
  152. package/src/utils/cbor.js +429 -0
  153. package/src/utils/checksum.ts +41 -0
  154. package/src/utils/environment.ts +46 -0
  155. package/src/utils/mcumgr.js +444 -0
  156. package/src/utils/stringUtils.ts +11 -0
  157. package/src/vibration/VibrationManager.ts +308 -0
  158. package/src/vibration/VibrationWaveformEffects.ts +128 -0
@@ -0,0 +1,444 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2023 Laird Connectivity
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ /**
26
+ * @file mcumgr
27
+ * @brief Provides MCU manager operation functions for the Xbit USB Shell.
28
+ * This file is inspired by the MIT licensed mcumgr file originally
29
+ * authored by Andras Barthazi (https://github.com/boogie/mcumgr-web),
30
+ * updated to also support file upload/download over SMP.
31
+ */
32
+
33
+ import { CBOR } from "./cbor.js";
34
+ import { createConsole } from "./Console.ts";
35
+
36
+ const _console = createConsole("mcumgr", { log: true });
37
+
38
+ export const constants = {
39
+ // Opcodes
40
+ MGMT_OP_READ: 0,
41
+ MGMT_OP_READ_RSP: 1,
42
+ MGMT_OP_WRITE: 2,
43
+ MGMT_OP_WRITE_RSP: 3,
44
+
45
+ // Groups
46
+ MGMT_GROUP_ID_OS: 0,
47
+ MGMT_GROUP_ID_IMAGE: 1,
48
+ MGMT_GROUP_ID_STAT: 2,
49
+ MGMT_GROUP_ID_CONFIG: 3,
50
+ MGMT_GROUP_ID_LOG: 4,
51
+ MGMT_GROUP_ID_CRASH: 5,
52
+ MGMT_GROUP_ID_SPLIT: 6,
53
+ MGMT_GROUP_ID_RUN: 7,
54
+ MGMT_GROUP_ID_FS: 8,
55
+ MGMT_GROUP_ID_SHELL: 9,
56
+
57
+ // OS group
58
+ OS_MGMT_ID_ECHO: 0,
59
+ OS_MGMT_ID_CONS_ECHO_CTRL: 1,
60
+ OS_MGMT_ID_TASKSTAT: 2,
61
+ OS_MGMT_ID_MPSTAT: 3,
62
+ OS_MGMT_ID_DATETIME_STR: 4,
63
+ OS_MGMT_ID_RESET: 5,
64
+
65
+ // Image group
66
+ IMG_MGMT_ID_STATE: 0,
67
+ IMG_MGMT_ID_UPLOAD: 1,
68
+ IMG_MGMT_ID_FILE: 2,
69
+ IMG_MGMT_ID_CORELIST: 3,
70
+ IMG_MGMT_ID_CORELOAD: 4,
71
+ IMG_MGMT_ID_ERASE: 5,
72
+
73
+ // Filesystem group
74
+ FS_MGMT_ID_FILE: 0,
75
+ };
76
+
77
+ export class MCUManager {
78
+ constructor() {
79
+ this._mtu = 256;
80
+ this._messageCallback = null;
81
+ this._imageUploadProgressCallback = null;
82
+ this._imageUploadNextCallback = null;
83
+ this._fileUploadProgressCallback = null;
84
+ this._fileUploadNextCallback = null;
85
+ this._uploadIsInProgress = false;
86
+ this._downloadIsInProgress = false;
87
+ this._buffer = new Uint8Array();
88
+ this._seq = 0;
89
+ }
90
+
91
+ onMessage(callback) {
92
+ this._messageCallback = callback;
93
+ return this;
94
+ }
95
+
96
+ onImageUploadNext(callback) {
97
+ this._imageUploadNextCallback = callback;
98
+ return this;
99
+ }
100
+
101
+ onImageUploadProgress(callback) {
102
+ this._imageUploadProgressCallback = callback;
103
+ return this;
104
+ }
105
+
106
+ onImageUploadFinished(callback) {
107
+ this._imageUploadFinishedCallback = callback;
108
+ return this;
109
+ }
110
+
111
+ onFileUploadNext(callback) {
112
+ this._fileUploadNextCallback = callback;
113
+ return this;
114
+ }
115
+
116
+ onFileUploadProgress(callback) {
117
+ this._fileUploadProgressCallback = callback;
118
+ return this;
119
+ }
120
+
121
+ onFileUploadFinished(callback) {
122
+ this._fileUploadFinishedCallback = callback;
123
+ return this;
124
+ }
125
+
126
+ onFileDownloadNext(callback) {
127
+ this._fileDownloadNextCallback = callback;
128
+ return this;
129
+ }
130
+
131
+ onFileDownloadProgress(callback) {
132
+ this._fileDownloadProgressCallback = callback;
133
+ return this;
134
+ }
135
+
136
+ onFileDownloadFinished(callback) {
137
+ this._fileDownloadFinishedCallback = callback;
138
+ return this;
139
+ }
140
+
141
+ _getMessage(op, group, id, data) {
142
+ const _flags = 0;
143
+ let encodedData = [];
144
+ if (typeof data !== "undefined") {
145
+ encodedData = [...new Uint8Array(CBOR.encode(data))];
146
+ }
147
+ const lengthLo = encodedData.length & 255;
148
+ const lengthHi = encodedData.length >> 8;
149
+ const groupLo = group & 255;
150
+ const groupHi = group >> 8;
151
+ const message = [op, _flags, lengthHi, lengthLo, groupHi, groupLo, this._seq, id, ...encodedData];
152
+ this._seq = (this._seq + 1) % 256;
153
+
154
+ return message;
155
+ }
156
+
157
+ _notification(buffer) {
158
+ _console.log("mcumgr - message received");
159
+ const message = new Uint8Array(buffer);
160
+ this._buffer = new Uint8Array([...this._buffer, ...message]);
161
+ const messageLength = this._buffer[2] * 256 + this._buffer[3];
162
+ if (this._buffer.length < messageLength + 8) return;
163
+ this._processMessage(this._buffer.slice(0, messageLength + 8));
164
+ this._buffer = this._buffer.slice(messageLength + 8);
165
+ }
166
+
167
+ _processMessage(message) {
168
+ const [op, , lengthHi, lengthLo, groupHi, groupLo, , id] = message;
169
+ const data = CBOR.decode(message.slice(8).buffer);
170
+ const length = lengthHi * 256 + lengthLo;
171
+ const group = groupHi * 256 + groupLo;
172
+
173
+ _console.log("mcumgr - Process Message - Group: " + group + ", Id: " + id + ", Off: " + data.off);
174
+ if (group === constants.MGMT_GROUP_ID_IMAGE && id === constants.IMG_MGMT_ID_UPLOAD && data.off) {
175
+ this._uploadOffset = data.off;
176
+ this._uploadNext();
177
+ return;
178
+ }
179
+ if (
180
+ op === constants.MGMT_OP_WRITE_RSP &&
181
+ group === constants.MGMT_GROUP_ID_FS &&
182
+ id === constants.FS_MGMT_ID_FILE &&
183
+ data.off
184
+ ) {
185
+ this._uploadFileOffset = data.off;
186
+ this._uploadFileNext();
187
+ return;
188
+ }
189
+ if (op === constants.MGMT_OP_READ_RSP && group === constants.MGMT_GROUP_ID_FS && id === constants.FS_MGMT_ID_FILE) {
190
+ this._downloadFileOffset += data.data.length;
191
+ if (data.len != undefined) {
192
+ this._downloadFileLength = data.len;
193
+ }
194
+ _console.log("downloaded " + this._downloadFileOffset + " bytes of " + this._downloadFileLength);
195
+ if (this._downloadFileLength > 0) {
196
+ this._fileDownloadProgressCallback({
197
+ percentage: Math.floor((this._downloadFileOffset / this._downloadFileLength) * 100),
198
+ });
199
+ }
200
+ if (this._messageCallback) this._messageCallback({ op, group, id, data, length });
201
+ this._downloadFileNext();
202
+ return;
203
+ }
204
+
205
+ if (this._messageCallback) this._messageCallback({ op, group, id, data, length });
206
+ }
207
+
208
+ cmdReset() {
209
+ return this._getMessage(constants.MGMT_OP_WRITE, constants.MGMT_GROUP_ID_OS, constants.OS_MGMT_ID_RESET);
210
+ }
211
+
212
+ smpEcho(message) {
213
+ return this._getMessage(constants.MGMT_OP_WRITE, constants.MGMT_GROUP_ID_OS, constants.OS_MGMT_ID_ECHO, {
214
+ d: message,
215
+ });
216
+ }
217
+
218
+ cmdImageState() {
219
+ return this._getMessage(constants.MGMT_OP_READ, constants.MGMT_GROUP_ID_IMAGE, constants.IMG_MGMT_ID_STATE);
220
+ }
221
+
222
+ cmdImageErase() {
223
+ return this._getMessage(constants.MGMT_OP_WRITE, constants.MGMT_GROUP_ID_IMAGE, constants.IMG_MGMT_ID_ERASE, {});
224
+ }
225
+
226
+ cmdImageTest(hash) {
227
+ return this._getMessage(constants.MGMT_OP_WRITE, constants.MGMT_GROUP_ID_IMAGE, constants.IMG_MGMT_ID_STATE, {
228
+ hash,
229
+ confirm: false,
230
+ });
231
+ }
232
+
233
+ cmdImageConfirm(hash) {
234
+ return this._getMessage(constants.MGMT_OP_WRITE, constants.MGMT_GROUP_ID_IMAGE, constants.IMG_MGMT_ID_STATE, {
235
+ hash,
236
+ confirm: true,
237
+ });
238
+ }
239
+
240
+ _hash(image) {
241
+ return crypto.subtle.digest("SHA-256", image);
242
+ }
243
+
244
+ async _uploadNext() {
245
+ if (!this._uploadImage) {
246
+ return;
247
+ }
248
+
249
+ if (this._uploadOffset >= this._uploadImage.byteLength) {
250
+ this._uploadIsInProgress = false;
251
+ this._imageUploadFinishedCallback();
252
+ return;
253
+ }
254
+
255
+ const nmpOverhead = 8;
256
+ const message = { data: new Uint8Array(), off: this._uploadOffset };
257
+ if (this._uploadOffset === 0) {
258
+ message.len = this._uploadImage.byteLength;
259
+ message.sha = new Uint8Array(await this._hash(this._uploadImage));
260
+ }
261
+ this._imageUploadProgressCallback({
262
+ percentage: Math.floor((this._uploadOffset / this._uploadImage.byteLength) * 100),
263
+ });
264
+
265
+ const length = this._mtu - CBOR.encode(message).byteLength - nmpOverhead - 3 - 5;
266
+
267
+ message.data = new Uint8Array(this._uploadImage.slice(this._uploadOffset, this._uploadOffset + length));
268
+
269
+ this._uploadOffset += length;
270
+
271
+ const packet = this._getMessage(
272
+ constants.MGMT_OP_WRITE,
273
+ constants.MGMT_GROUP_ID_IMAGE,
274
+ constants.IMG_MGMT_ID_UPLOAD,
275
+ message
276
+ );
277
+
278
+ _console.log("mcumgr - _uploadNext: Message Length: " + packet.length);
279
+
280
+ this._imageUploadNextCallback({ packet });
281
+ }
282
+ async reset() {
283
+ this._messageCallback = null;
284
+ this._imageUploadProgressCallback = null;
285
+ this._imageUploadNextCallback = null;
286
+ this._fileUploadProgressCallback = null;
287
+ this._fileUploadNextCallback = null;
288
+ this._uploadIsInProgress = false;
289
+ this._downloadIsInProgress = false;
290
+ this._buffer = new Uint8Array();
291
+ this._seq = 0;
292
+ }
293
+
294
+ async cmdUpload(image, slot = 0) {
295
+ if (this._uploadIsInProgress) {
296
+ _console.error("Upload is already in progress.");
297
+ return;
298
+ }
299
+ this._uploadIsInProgress = true;
300
+
301
+ this._uploadOffset = 0;
302
+ this._uploadImage = image;
303
+ this._uploadSlot = slot;
304
+
305
+ this._uploadNext();
306
+ }
307
+
308
+ async cmdUploadFile(filebuf, destFilename) {
309
+ if (this._uploadIsInProgress) {
310
+ _console.error("Upload is already in progress.");
311
+ return;
312
+ }
313
+ this._uploadIsInProgress = true;
314
+ this._uploadFileOffset = 0;
315
+ this._uploadFile = filebuf;
316
+ this._uploadFilename = destFilename;
317
+
318
+ this._uploadFileNext();
319
+ }
320
+
321
+ async _uploadFileNext() {
322
+ _console.log("uploadFileNext - offset: " + this._uploadFileOffset + ", length: " + this._uploadFile.byteLength);
323
+
324
+ if (this._uploadFileOffset >= this._uploadFile.byteLength) {
325
+ this._uploadIsInProgress = false;
326
+ this._fileUploadFinishedCallback();
327
+ return;
328
+ }
329
+
330
+ const nmpOverhead = 8;
331
+ const message = { data: new Uint8Array(), off: this._uploadFileOffset };
332
+ if (this._uploadFileOffset === 0) {
333
+ message.len = this._uploadFile.byteLength;
334
+ }
335
+ message.name = this._uploadFilename;
336
+ this._fileUploadProgressCallback({
337
+ percentage: Math.floor((this._uploadFileOffset / this._uploadFile.byteLength) * 100),
338
+ });
339
+
340
+ const length = this._mtu - CBOR.encode(message).byteLength - nmpOverhead;
341
+
342
+ message.data = new Uint8Array(this._uploadFile.slice(this._uploadFileOffset, this._uploadFileOffset + length));
343
+
344
+ this._uploadFileOffset += length;
345
+
346
+ const packet = this._getMessage(
347
+ constants.MGMT_OP_WRITE,
348
+ constants.MGMT_GROUP_ID_FS,
349
+ constants.FS_MGMT_ID_FILE,
350
+ message
351
+ );
352
+
353
+ _console.log("mcumgr - _uploadNext: Message Length: " + packet.length);
354
+
355
+ this._fileUploadNextCallback({ packet });
356
+ }
357
+
358
+ async cmdDownloadFile(filename, destFilename) {
359
+ if (this._downloadIsInProgress) {
360
+ _console.error("Download is already in progress.");
361
+ return;
362
+ }
363
+ this._downloadIsInProgress = true;
364
+ this._downloadFileOffset = 0;
365
+ this._downloadFileLength = 0;
366
+ this._downloadRemoteFilename = filename;
367
+ this._downloadLocalFilename = destFilename;
368
+
369
+ this._downloadFileNext();
370
+ }
371
+
372
+ async _downloadFileNext() {
373
+ if (this._downloadFileLength > 0) {
374
+ if (this._downloadFileOffset >= this._downloadFileLength) {
375
+ this._downloadIsInProgress = false;
376
+ this._fileDownloadFinishedCallback();
377
+ return;
378
+ }
379
+ }
380
+
381
+ const message = { off: this._downloadFileOffset };
382
+ if (this._downloadFileOffset === 0) {
383
+ message.name = this._downloadRemoteFilename;
384
+ }
385
+
386
+ const packet = this._getMessage(
387
+ constants.MGMT_OP_READ,
388
+ constants.MGMT_GROUP_ID_FS,
389
+ constants.FS_MGMT_ID_FILE,
390
+ message
391
+ );
392
+ _console.log("mcumgr - _downloadNext: Message Length: " + packet.length);
393
+ this._fileDownloadNextCallback({ packet });
394
+ }
395
+
396
+ async imageInfo(image) {
397
+ const info = {};
398
+ const view = new Uint8Array(image);
399
+
400
+ // check header length
401
+ if (view.length < 32) {
402
+ throw new Error("Invalid image (too short file)");
403
+ }
404
+
405
+ // check MAGIC bytes 0x96f3b83d
406
+ if (view[0] !== 0x3d || view[1] !== 0xb8 || view[2] !== 0xf3 || view[3] !== 0x96) {
407
+ throw new Error("Invalid image (wrong magic bytes)");
408
+ }
409
+
410
+ // check load address is 0x00000000
411
+ if (view[4] !== 0x00 || view[5] !== 0x00 || view[6] !== 0x00 || view[7] !== 0x00) {
412
+ throw new Error("Invalid image (wrong load address)");
413
+ }
414
+
415
+ const headerSize = view[8] + view[9] * 2 ** 8;
416
+
417
+ // check protected TLV area size is 0
418
+ if (view[10] !== 0x00 || view[11] !== 0x00) {
419
+ throw new Error("Invalid image (wrong protected TLV area size)");
420
+ }
421
+
422
+ const imageSize = view[12] + view[13] * 2 ** 8 + view[14] * 2 ** 16 + view[15] * 2 ** 24;
423
+ info.imageSize = imageSize;
424
+
425
+ // check image size is correct
426
+ if (view.length < imageSize + headerSize) {
427
+ throw new Error("Invalid image (wrong image size)");
428
+ }
429
+
430
+ // check flags is 0x00000000
431
+ if (view[16] !== 0x00 || view[17] !== 0x00 || view[18] !== 0x00 || view[19] !== 0x00) {
432
+ throw new Error("Invalid image (wrong flags)");
433
+ }
434
+
435
+ const version = `${view[20]}.${view[21]}.${view[22] + view[23] * 2 ** 8}`;
436
+ info.version = version;
437
+
438
+ info.hash = [...new Uint8Array(await this._hash(image.slice(0, imageSize + 32)))]
439
+ .map((b) => b.toString(16).padStart(2, "0"))
440
+ .join("");
441
+
442
+ return info;
443
+ }
444
+ }
@@ -0,0 +1,11 @@
1
+ export function spacesToPascalCase(string: string) {
2
+ return string
3
+ .replace(/(?:^\w|\b\w)/g, function (match) {
4
+ return match.toUpperCase();
5
+ })
6
+ .replace(/\s+/g, "");
7
+ }
8
+
9
+ export function capitalizeFirstCharacter(string: string) {
10
+ return string[0].toUpperCase() + string.slice(1);
11
+ }