-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathfirmware.action.ts
More file actions
391 lines (380 loc) · 12.9 KB
/
firmware.action.ts
File metadata and controls
391 lines (380 loc) · 12.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
import {
AppActions,
LayoutOptionsActions,
NotificationActions,
} from './actions';
import { ThunkAction, ThunkDispatch } from 'redux-thunk';
import {
IFlashFirmwareDialogFlashMode,
IFlashFirmwareDialogMode,
RootState,
} from '../store/state';
import { sendEventToGoogleAnalytics } from '../utils/GoogleAnalytics';
import intelHex from 'intel-hex';
import { IFirmware, IFirmwareBuildingTask } from '../services/storage/Storage';
import { IBootloaderType } from '../services/firmware/Types';
import { isError } from '../types';
export const UPLOAD_FIRMWARE_DIALOG_ACTIONS = '@UploadFirmwareDialog';
export const UPLOAD_FIRMWARE_DIALOG_UPDATE_OPEN = `${UPLOAD_FIRMWARE_DIALOG_ACTIONS}/UploadOpen`;
export const UPLOAD_FIRMWARE_DIALOG_UPDATE_FIRMWARE_FILE_BUFFER = `${UPLOAD_FIRMWARE_DIALOG_ACTIONS}/UploadFirmwareFileBuffer`;
export const UploadFirmwareDialogActions = {
updateOpen: (open: boolean) => {
return {
type: UPLOAD_FIRMWARE_DIALOG_UPDATE_OPEN,
value: open,
};
},
uploadFirmwareFileBuffer: (buffer: ArrayBuffer) => {
return {
type: UPLOAD_FIRMWARE_DIALOG_UPDATE_FIRMWARE_FILE_BUFFER,
value: buffer,
};
},
};
export const FLASH_FIRMWARE_DIALOG_ACTIONS = '@FlashFirmwareDialog';
export const FLASH_FIRMWARE_DIALOG_UPDATE_FIRMWARE = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateFirmware`;
export const FLASH_FIRMWARE_DIALOG_UPDATE_BOOTLOADER_TYPE = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateBootloaderType`;
export const FLASH_FIRMWARE_DIALOG_UPDATE_FLASHING = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateFlashing`;
export const FLASH_FIRMWARE_DIALOG_UPDATE_PROGRESS_RATE = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateProgressRate`;
export const FLASH_FIRMWARE_DIALOG_UPDATE_MODE = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateMode`;
export const FLASH_FIRMWARE_DIALOG_UPDATE_LOGS = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateLogs`;
export const FLASH_FIRMWARE_DIALOG_APPEND_LOG = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/AppendLog`;
export const FLASH_FIRMWARE_DIALOG_CLEAR = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/Clear`;
export const FLASH_FIRMWARE_DIALOG_UPDATE_KEYBOARD_NAME = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateKeyboardName`;
export const FLASH_FIRMWARE_DIALOG_UPDATE_FLASH_MODE = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateFlashMode`;
export const FLASH_FIRMWARE_DIALOG_UPDATE_BUILDING_FIRMWARE_TASK = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateBuildingFirmwareTask`;
export const FLASH_FIRMWARE_DIALOG_UPDATE_FIRMWARE_BLOB = `${FLASH_FIRMWARE_DIALOG_ACTIONS}/UpdateFirmwareBlob`;
export const FlashFirmwareDialogActions = {
updateFirmware: (firmware: IFirmware | null) => {
return {
type: FLASH_FIRMWARE_DIALOG_UPDATE_FIRMWARE,
value: firmware,
};
},
updateBootloaderType: (bootloaderType: IBootloaderType) => {
return {
type: FLASH_FIRMWARE_DIALOG_UPDATE_BOOTLOADER_TYPE,
value: bootloaderType,
};
},
updateFlashing: (flashing: boolean) => {
return {
type: FLASH_FIRMWARE_DIALOG_UPDATE_FLASHING,
value: flashing,
};
},
updateProgressRate: (progressRate: number) => {
return {
type: FLASH_FIRMWARE_DIALOG_UPDATE_PROGRESS_RATE,
value: progressRate,
};
},
updateLogs: (logs: string[]) => {
return {
type: FLASH_FIRMWARE_DIALOG_UPDATE_LOGS,
value: logs,
};
},
appendLog: (message: string, lineBreak: boolean = true) => {
return {
type: FLASH_FIRMWARE_DIALOG_APPEND_LOG,
value: {
message,
lineBreak,
},
};
},
updateMode: (mode: IFlashFirmwareDialogMode) => {
return {
type: FLASH_FIRMWARE_DIALOG_UPDATE_MODE,
value: mode,
};
},
clear: () => {
return {
type: FLASH_FIRMWARE_DIALOG_CLEAR,
};
},
updateKeyboardName: (keyboardName: string) => {
return {
type: FLASH_FIRMWARE_DIALOG_UPDATE_KEYBOARD_NAME,
value: keyboardName,
};
},
updateFlashMode: (flashMode: IFlashFirmwareDialogFlashMode) => {
return {
type: FLASH_FIRMWARE_DIALOG_UPDATE_FLASH_MODE,
value: flashMode,
};
},
updateBuildingFirmwareTask: (task: IFirmwareBuildingTask | null) => {
return {
type: FLASH_FIRMWARE_DIALOG_UPDATE_BUILDING_FIRMWARE_TASK,
value: task,
};
},
updateFirmwareBlob: (blob: Buffer | undefined) => {
return {
type: FLASH_FIRMWARE_DIALOG_UPDATE_FIRMWARE_BLOB,
value: blob,
};
},
};
type ActionTypes = ReturnType<
| (typeof LayoutOptionsActions)[keyof typeof LayoutOptionsActions]
| (typeof NotificationActions)[keyof typeof NotificationActions]
| (typeof AppActions)[keyof typeof AppActions]
>;
type ThunkPromiseAction<T> = ThunkAction<
Promise<T>,
RootState,
undefined,
ActionTypes
>;
export const firmwareActionsThunk = {
uploadFirmwareFile:
(file: File): ThunkPromiseAction<void> =>
async (
dispatch: ThunkDispatch<RootState, undefined, ActionTypes>,
_getState: () => RootState
) => {
// eslint-disable-next-line no-undef
const loadBinaryFile = (file: File): Promise<ArrayBuffer> => {
return new Promise<ArrayBuffer>((resolve) => {
// eslint-disable-next-line no-undef
const fileReader = new FileReader();
fileReader.addEventListener('load', () => {
resolve(fileReader.result as ArrayBuffer);
});
fileReader.readAsArrayBuffer(file);
});
};
const firmwareArrayBuffer = await loadBinaryFile(file);
dispatch(UploadFirmwareDialogActions.updateOpen(false));
dispatch(
UploadFirmwareDialogActions.uploadFirmwareFileBuffer(
firmwareArrayBuffer
)
);
dispatch(FlashFirmwareDialogActions.clear());
const firmwareName = file.name;
dispatch(FlashFirmwareDialogActions.updateKeyboardName('Your keyboard'));
dispatch(FlashFirmwareDialogActions.updateFlashMode('upload_and_flash'));
dispatch(
FlashFirmwareDialogActions.updateFirmware({
name: firmwareName,
default_bootloader_type: 'caterina',
flash_support: true,
filename: firmwareName,
description: '',
hash: '',
sourceCodeUrl: '',
created_at: new Date(),
})
);
await dispatch(firmwareActionsThunk.loadFirmwareBlob());
},
loadFirmwareBlob:
(): ThunkPromiseAction<void> =>
async (
dispatch: ThunkDispatch<RootState, undefined, ActionTypes>,
getState: () => RootState
) => {
const handleError = (error: string, cause?: any) => {
console.error(error);
dispatch(NotificationActions.addError(error, cause));
dispatch(FlashFirmwareDialogActions.clear());
};
dispatch(FlashFirmwareDialogActions.updateMode('loading'));
dispatch(FlashFirmwareDialogActions.updateFlashing(false));
const { common, entities, storage } = getState();
const firmware = common.firmware.flashFirmwareDialog.firmware!;
const bootloaderType =
common.firmware.flashFirmwareDialog.bootloaderType!;
const flashMode = common.firmware.flashFirmwareDialog.flashMode;
let flashBytes: Buffer | undefined;
if (flashMode === 'fetch_and_flash') {
const definitionDocument = entities.keyboardDefinitionDocument!;
sendEventToGoogleAnalytics('catalog/flash_firmware', {
vendor_id: definitionDocument.vendorId,
product_id: definitionDocument.productId,
product_name: definitionDocument.productName,
});
dispatch(
FlashFirmwareDialogActions.appendLog(
'Reading the firmware binary from the server.',
false
)
);
const fetchBlobResult = await storage.instance!.fetchFirmwareFileBlob(
definitionDocument.id,
firmware.filename,
'flash'
);
if (isError(fetchBlobResult)) {
handleError(fetchBlobResult.error, fetchBlobResult.cause);
return;
}
const blob: Blob = fetchBlobResult.value.blob;
flashBytes = createFlashBytes(
Buffer.from(new Uint8Array(await blob.arrayBuffer())),
bootloaderType,
dispatch
);
if (flashBytes === undefined) {
return;
}
} else if (flashMode === 'build_and_flash') {
dispatch(
FlashFirmwareDialogActions.appendLog(
'Reading the firmware binary from the server.',
false
)
);
const task = common.firmware.flashFirmwareDialog.buildingFirmwareTask!;
const fetchBlobResult =
await storage.instance!.fetchBuiltFirmwareFileBlob(
task.firmwareFilePath
);
if (isError(fetchBlobResult)) {
handleError(fetchBlobResult.error, fetchBlobResult.cause);
return;
}
const blob: Blob = fetchBlobResult.value.blob;
flashBytes = createFlashBytes(
Buffer.from(new Uint8Array(await blob.arrayBuffer())),
bootloaderType,
dispatch
);
if (flashBytes === undefined) {
return;
}
} else {
flashBytes = createFlashBytes(
Buffer.from(
new Uint8Array(
common.firmware.uploadFirmwareDialog.firmwareFileBuffer!
)
),
bootloaderType,
dispatch
);
if (flashBytes === undefined) {
return;
}
}
dispatch(FlashFirmwareDialogActions.updateFirmwareBlob(flashBytes));
dispatch(FlashFirmwareDialogActions.updateMode('instruction'));
},
// eslint-disable-next-line no-undef
flashFirmware:
(): ThunkPromiseAction<void> =>
async (
dispatch: ThunkDispatch<RootState, undefined, ActionTypes>,
getState: () => RootState
) => {
const handleError = (error: string, cause?: any) => {
console.error(error);
dispatch(NotificationActions.addError(error, cause));
dispatch(FlashFirmwareDialogActions.appendLog(`Error: ${error}`));
dispatch(FlashFirmwareDialogActions.updateFlashing(false));
};
dispatch(FlashFirmwareDialogActions.updateLogs([]));
dispatch(FlashFirmwareDialogActions.updateProgressRate(0));
dispatch(FlashFirmwareDialogActions.updateMode('flashing'));
dispatch(FlashFirmwareDialogActions.updateFlashing(true));
const { serial, common } = getState();
const firmwareWriter = serial.writer;
const bootloaderType =
common.firmware.flashFirmwareDialog.bootloaderType!;
let flashBytes: Buffer | undefined =
common.firmware.flashFirmwareDialog.firmwareBlob;
if (flashBytes === undefined) {
dispatch(
NotificationActions.addError('Firmware binary is not loaded.')
);
dispatch(FlashFirmwareDialogActions.clear());
return;
}
dispatch(
FlashFirmwareDialogActions.appendLog(
'Firmware binary has already been loaded. Start writing the firmware.'
)
);
dispatch(FlashFirmwareDialogActions.updateProgressRate(15));
const writeResult = await firmwareWriter.write(
bootloaderType,
new Uint8Array(flashBytes),
null,
(message, lineBreak) => {
dispatch(FlashFirmwareDialogActions.appendLog(message, lineBreak));
},
(phase) => {
let rate;
switch (phase) {
case 'opened':
rate = 30;
break;
case 'initialized':
rate = 45;
break;
case 'cleared':
rate = 60;
break;
case 'wrote':
rate = 75;
break;
case 'verified':
rate = 90;
break;
case 'closed':
rate = 100;
break;
default:
throw new Error(`Unknown phase: ${phase}`);
}
dispatch(FlashFirmwareDialogActions.updateProgressRate(rate));
if (phase === 'closed') {
dispatch(
FlashFirmwareDialogActions.appendLog(
'Writing the firmware finished successfully.'
)
);
dispatch(FlashFirmwareDialogActions.updateFlashing(false));
}
},
(error, cause) => {
handleError(error, cause);
}
);
if (isError(writeResult)) {
handleError(writeResult.error!, writeResult.cause);
return;
}
},
};
const createFlashBytes = (
buffer: Buffer,
bootloaderType: IBootloaderType,
dispatch: ThunkDispatch<RootState, undefined, ActionTypes>
): Buffer | undefined => {
try {
switch (bootloaderType) {
case 'caterina':
case 'dfu':
return intelHex.parse(buffer).data;
case 'copy':
return buffer;
}
} catch (error) {
console.error('Creating a flashed bytes failed.', error);
dispatch(
NotificationActions.addError(
'Creating the firmware binary failed.',
error
)
);
dispatch(FlashFirmwareDialogActions.clear());
return undefined;
}
};