-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathactions.ts
More file actions
500 lines (432 loc) · 14.9 KB
/
actions.ts
File metadata and controls
500 lines (432 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// SPDX-License-Identifier: MIT
// Copyright (c) 2020-2026 The Pybricks Authors
import { FirmwareReaderError, HubType } from '@pybricks/firmware';
import { createAction } from '../actions';
import { Hub } from '../components/hubPicker';
export enum MetadataProblem {
Missing = 'metadata.missing',
NotSupported = 'metadata.notSupported',
}
export enum HubError {
UnknownCommand = 'hubError.unknownCommand',
EraseFailed = 'hubError.eraseFailed',
InitFailed = 'hubError.initFailed',
CountMismatch = 'hubError.countMismatch',
ChecksumMismatch = 'hubError.checksumMismatch',
}
function isHubError(arg: unknown): arg is HubError {
return Object.values(HubError).includes(arg as HubError);
}
export enum FailToFinishReasonType {
/** Connecting to the hub failed. */
FailedToConnect = 'flashFirmware.failToFinish.reason.failedToConnect',
/** The hub connection timed out. */
TimedOut = 'flashFirmware.failToFinish.reason.timedOut',
/** Something went wrong with the BLE connection. */
BleError = 'flashFirmware.failToFinish.reason.bleError',
/** The hub was disconnected. */
Disconnected = 'flashFirmware.failToFinish.reason.disconnected',
/** The hub sent a response indicating a problem. */
HubError = 'flashFirmware.failToFinish.reason.hubError',
/** The is no firmware available that matches the connected hub. */
NoFirmware = 'flashFirmware.failToFinish.reason.noFirmware',
/** The provided firmware.zip does not match the connected hub. */
DeviceMismatch = 'flashFirmware.failToFinish.reason.deviceMismatch',
/** Failed to fetch firmware from the server. */
FailedToFetch = 'flashFirmware.failToFinish.reason.failedToFetch',
/** There was a problem with the zip file. */
ZipError = 'flashFirmware.failToFinish.reason.zipError',
/** Metadata property is missing or invalid. */
BadMetadata = 'flashFirmware.failToFinish.reason.badMetadata',
/** The main.py file failed to compile. */
FailedToCompile = 'flashFirmware.failToFinish.reason.failedToCompile',
/** The combined firmware-base.bin and main.mpy are too big. */
FirmwareSize = 'flashFirmware.failToFinish.reason.firmwareSize',
/** An unexpected error occurred. */
Unknown = 'flashFirmware.failToFinish.reason.unknown',
}
type Reason<T extends FailToFinishReasonType> = {
reason: T;
};
export type FailToFinishReasonFailedToConnect =
Reason<FailToFinishReasonType.FailedToConnect>;
export type FailToFinishReasonTimedOut = Reason<FailToFinishReasonType.TimedOut>;
export type FailToFinishReasonBleError = Reason<FailToFinishReasonType.BleError> & {
error: Error;
};
export type FailToFinishReasonDisconnected =
Reason<FailToFinishReasonType.Disconnected>;
export type FailToFinishReasonHubError = Reason<FailToFinishReasonType.HubError> & {
hubError: HubError;
};
export type FailToFinishReasonNoFirmware = Reason<FailToFinishReasonType.NoFirmware>;
export type FailToFinishReasonDeviceMismatch =
Reason<FailToFinishReasonType.DeviceMismatch>;
export type FailToFinishReasonFailedToFetch =
Reason<FailToFinishReasonType.FailedToFetch> & {
response: Response;
};
export type FailToFinishReasonZipError = Reason<FailToFinishReasonType.ZipError> & {
err: FirmwareReaderError;
};
export type FailToFinishReasonBadMetadata =
Reason<FailToFinishReasonType.BadMetadata> & {
property: string;
problem: MetadataProblem;
};
export type FailToFinishReasonFirmwareSize =
Reason<FailToFinishReasonType.FirmwareSize>;
export type FailToFinishReasonFailedToCompile =
Reason<FailToFinishReasonType.FailedToCompile>;
export type FailToFinishReasonUnknown = Reason<FailToFinishReasonType.Unknown> & {
error: Error;
};
export type FailToFinishReason =
| FailToFinishReasonFailedToConnect
| FailToFinishReasonTimedOut
| FailToFinishReasonBleError
| FailToFinishReasonDisconnected
| FailToFinishReasonHubError
| FailToFinishReasonNoFirmware
| FailToFinishReasonDeviceMismatch
| FailToFinishReasonFailedToFetch
| FailToFinishReasonZipError
| FailToFinishReasonBadMetadata
| FailToFinishReasonFirmwareSize
| FailToFinishReasonFailedToCompile
| FailToFinishReasonUnknown;
// High-level bootloader actions.
/**
* Creates a new action to flash firmware to a hub.
* @param data The firmware zip file data or `null` to get firmware later.
* @param hubName A custom hub name or an empty string to use the default name.
*/
export const flashFirmware = createAction(
(data: ArrayBuffer | null, hubName: string) => ({
type: 'flashFirmware.action.flashFirmware',
data,
hubName,
}),
);
/**
* Action that indicates flashing firmware started.
* @param total The total number of bytes to be flashed.
*/
export const didStart = createAction(() => ({
type: 'flashFirmware.action.didStart',
}));
/** Action that indicates that flashing firmware completed successfully. */
export const didFinish = createAction(() => ({
type: 'flashFirmware.action.didFinish',
}));
function isError(err: unknown): err is Error {
const maybeError = err as Error;
return (
maybeError !== undefined &&
typeof maybeError.name === 'string' &&
typeof maybeError.message === 'string'
);
}
// FIXME: get rid of this monstrosity
const didFailToFinishType = 'flashFirmware.action.didFailToFinish';
function didFailToFinishCreator(reason: FailToFinishReasonType.FailedToConnect): {
type: typeof didFailToFinishType;
reason: FailToFinishReasonFailedToConnect;
};
function didFailToFinishCreator(reason: FailToFinishReasonType.TimedOut): {
type: typeof didFailToFinishType;
reason: FailToFinishReasonTimedOut;
};
function didFailToFinishCreator(
reason: FailToFinishReasonType.BleError,
error: Error,
): {
type: typeof didFailToFinishType;
reason: FailToFinishReasonBleError;
};
function didFailToFinishCreator(reason: FailToFinishReasonType.Disconnected): {
type: typeof didFailToFinishType;
reason: FailToFinishReasonDisconnected;
};
function didFailToFinishCreator(
reason: FailToFinishReasonType.HubError,
hubError: HubError,
): {
type: typeof didFailToFinishType;
reason: FailToFinishReasonHubError;
};
function didFailToFinishCreator(reason: FailToFinishReasonType.NoFirmware): {
type: typeof didFailToFinishType;
reason: FailToFinishReasonNoFirmware;
};
function didFailToFinishCreator(reason: FailToFinishReasonType.DeviceMismatch): {
type: typeof didFailToFinishType;
reason: FailToFinishReasonDeviceMismatch;
};
function didFailToFinishCreator(
reason: FailToFinishReasonType.FailedToFetch,
response: Response,
): {
type: typeof didFailToFinishType;
reason: FailToFinishReasonFailedToFetch;
};
function didFailToFinishCreator(
reason: FailToFinishReasonType.ZipError,
err: FirmwareReaderError,
): {
type: typeof didFailToFinishType;
reason: FailToFinishReasonZipError;
};
function didFailToFinishCreator(
reason: FailToFinishReasonType.BadMetadata,
property: string,
problem: MetadataProblem,
): {
type: typeof didFailToFinishType;
reason: FailToFinishReasonBadMetadata;
};
function didFailToFinishCreator(reason: FailToFinishReasonType.FirmwareSize): {
type: typeof didFailToFinishType;
reason: FailToFinishReasonFirmwareSize;
};
function didFailToFinishCreator(reason: FailToFinishReasonType.FailedToCompile): {
type: typeof didFailToFinishType;
reason: FailToFinishReasonFailedToCompile;
};
function didFailToFinishCreator(
reason: FailToFinishReasonType.Unknown,
error: Error,
): {
type: typeof didFailToFinishType;
reason: FailToFinishReasonUnknown;
};
function didFailToFinishCreator<T extends FailToFinishReasonType>(
reason: T,
arg1?: string | HubError | Error | Response,
arg2?: MetadataProblem,
): {
type: typeof didFailToFinishType;
reason: FailToFinishReason;
};
function didFailToFinishCreator(
reason: FailToFinishReasonType,
arg1?: string | HubError | Error | Response,
arg2?: MetadataProblem,
): {
type: typeof didFailToFinishType;
reason: FailToFinishReason;
} {
if (reason === FailToFinishReasonType.BleError) {
// istanbul ignore if: programmer error give wrong arg
if (!isError(arg1)) {
throw new Error('missing or invalid err');
}
return {
type: didFailToFinishType,
reason: { reason, error: arg1 },
};
}
if (reason === FailToFinishReasonType.HubError) {
// istanbul ignore if: programmer error give wrong arg
if (!isHubError(arg1)) {
throw new Error('missing or invalid hubError');
}
return {
type: didFailToFinishType,
reason: { reason, hubError: arg1 },
};
}
if (reason === FailToFinishReasonType.FailedToFetch) {
// istanbul ignore if: programmer error give wrong arg
if (!(arg1 instanceof Response)) {
throw new Error('missing or invalid response');
}
return {
type: didFailToFinishType,
reason: { reason, response: arg1 },
};
}
if (reason === FailToFinishReasonType.ZipError) {
// istanbul ignore if: programmer error give wrong arg
if (!(arg1 instanceof FirmwareReaderError)) {
throw new Error('missing or invalid err');
}
return {
type: didFailToFinishType,
reason: { reason, err: arg1 },
};
}
if (reason === FailToFinishReasonType.BadMetadata) {
// istanbul ignore if: programmer error give wrong arg
if (
arg1 !== 'metadata-version' &&
arg1 !== 'firmware-version' &&
arg1 !== 'device-id' &&
arg1 !== 'checksum-type' &&
arg1 !== 'mpy-abi-version' &&
arg1 !== 'mpy-cross-options' &&
arg1 !== 'user-mpy-offset' &&
arg1 !== 'max-firmware-size' &&
arg1 !== 'checksum-size' &&
arg1 !== 'hub-name-size'
) {
throw new Error('missing or invalid property');
}
// istanbul ignore if: programmer error give wrong arg
if (arg2 === undefined) {
throw new Error('missing or invalid problem');
}
return {
type: didFailToFinishType,
reason: { reason, property: arg1, problem: arg2 },
};
}
if (reason === FailToFinishReasonType.Unknown) {
// istanbul ignore if: programmer error give wrong arg
if (!isError(arg1)) {
throw new Error('missing or invalid err');
}
return {
type: didFailToFinishType,
reason: { reason, error: arg1 },
};
}
return { type: didFailToFinishType, reason: { reason } };
}
/**
* Action that indicates flashing did not start because of an error.
* @param total The total number of bytes to be flashed.
*/
export const didFailToFinish = createAction(didFailToFinishCreator);
/**
* Low-level action to flash firmware using LEGO's DFU over USB.
* @param firmware The firmware binary blob.
* @param hubType The hub type the firmware blob is for.
*/
export const firmwareFlashUsbDfu = createAction(
(firmware: ArrayBuffer, hubType: HubType) => ({
type: 'firmware.action.flashUsbDfu',
firmware,
hubType,
}),
);
/**
* Low-level action that indicates {@link firmwareFlashUsbDfu} succeeded.
*/
export const firmwareDidFlashUsbDfu = createAction(() => ({
type: 'firmware.action.didFlashUsbDfu',
}));
/**
* Low-level action that indicates {@link firmwareFlashUsbDfu} failed.
*/
export const firmwareDidFailToFlashUsbDfu = createAction(() => ({
type: 'firmware.action.didFailToFlashUsbDfu',
}));
// High-level actions
/**
* Action that triggers the install Pybricks firmware saga.
*/
export const firmwareInstallPybricks = createAction(() => ({
type: 'firmware.action.installPybricks',
}));
/**
* Action that indicates {@link firmwareInstallPybricks} succeeded.
*/
export const firmwareDidInstallPybricks = createAction(() => ({
type: 'firmware.action.didInstallPybricks',
}));
/**
* Action that indicates {@link firmwareInstallPybricks} failed.
*/
export const firmwareDidFailToInstallPybricks = createAction(() => ({
type: 'firmware.action.didFailToInstallPybricks',
}));
/**
* Action that triggers the restore official DFU firmware saga.
*/
export const firmwareRestoreOfficialDfu = createAction((hub: Hub) => ({
type: 'firmware.action.restoreOfficialDfu',
hub,
}));
/**
* Action that indicates {@link firmwareRestoreOfficialDfu} succeeded.
*/
export const firmwareDidRestoreOfficialDfu = createAction(() => ({
type: 'firmware.action.didRestoreOfficialDfu',
}));
/**
* Action that indicates {@link firmwareRestoreOfficialDfu} failed.
*/
export const firmwareDidFailToRestoreOfficialDfu = createAction(() => ({
type: 'firmware.action.didFailToRestoreOfficialDfu',
}));
/**
* Versions of official LEGO EV3 firmware available for restore.
*/
export enum EV3OfficialFirmwareVersion {
/** Official LEGO EV3 firmware version 1.09H (Home edition). */
home = '1.09H',
/** Official LEGO EV3 firmware version 1.09E (Education edition). */
education = '1.09E',
/** Official LEGO EV3 firmware version 1.10E (only useful for Microsoft MakeCode). */
makecode = '1.10E',
}
/**
* Action that triggers the restore official EV3 firmware saga.
*/
export const firmwareRestoreOfficialEV3 = createAction(
(version: EV3OfficialFirmwareVersion) => ({
type: 'firmware.action.restoreOfficialEV3',
version,
}),
);
/**
* Action that indicates {@link firmwareRestoreOfficialEV3} succeeded.
*/
export const firmwareDidRestoreOfficialEV3 = createAction(() => ({
type: 'firmware.action.didRestoreOfficialEV3',
}));
/**
* Action that indicates {@link firmwareRestoreOfficialEV3} failed.
*/
export const firmwareDidFailToRestoreOfficialEV3 = createAction(() => ({
type: 'firmware.action.didFailToRestoreOfficialEV3',
}));
/**
* Low-level action to flash firmware to an EV3 hub.
* @param firmware The firmware binary blob.
*/
export const firmwareFlashEV3 = createAction((firmware: ArrayBuffer) => ({
type: 'firmware.action.flashEV3',
firmware,
}));
/**
* Low-level action that indicates {@link firmwareFlashEV3} succeeded.
*/
export const firmwareDidFlashEV3 = createAction(() => ({
type: 'firmware.action.didFlashEV3',
}));
/**
* Low-level action that indicates {@link firmwareFlashEV3} failed.
*/
export const firmwareDidFailToFlashEV3 = createAction(() => ({
type: 'firmware.action.didFailToFlashEV3',
}));
export const firmwareDidReceiveEV3Reply = createAction(
(
length: number,
replyNumber: number,
messageType: number,
replyCommand: number,
status: number,
payload: ArrayBufferLike,
) => ({
type: 'firmware.action.didReceiveEV3Reply',
length,
replyNumber,
messageType,
replyCommand,
status,
payload,
}),
);