-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathflasher.js
More file actions
664 lines (564 loc) · 19.4 KB
/
flasher.js
File metadata and controls
664 lines (564 loc) · 19.4 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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import "/lib/beer.min.js";
import { createApp, reactive, ref, nextTick, watch, computed } from "/lib/vue.min.js";
import { Dfu } from "/lib/dfu.js";
import { ESPLoader, Transport, HardReset } from "/lib/esp32.js";
import { SerialConsole } from '/lib/console.js';
const searchParams = new URLSearchParams(location.search);
const configName = searchParams.get('config')?.replaceAll(/[^a-z_-]/g, '') ?? 'config';
const configRes = await fetch(`/${configName}.json`);
const config = await configRes.json();
const githubRes = await fetch('/releases');
const github = await githubRes.json();
const commandReference = {
'time ': 'Set time {epoch-secs}',
'erase': 'Erase filesystem',
'advert': 'Send Advertisment packet',
'reboot': 'Reboot device',
'clock': 'Display current time',
'password ': 'Set new password',
'log': 'Ouput log',
'log start': 'Start packet logging to file system',
'log stop': 'Stop packet logging to file system',
'log erase': 'Erase the packet logs from file system',
'ver': 'Show device version',
'set freq ': 'Set frequency {Mhz}',
'set af ': 'Set Air-time factor',
'set tx ': 'Set Tx power {dBm}',
'set repeat ': 'Set repeater mode {on|off}',
'set advert.interval ': 'Set advert rebroadcast interval {minutes}',
'set guest.password ': 'Set guest password',
'set name ': 'Set advertisement name',
'set lat': 'Set the advertisement map latitude',
'set lon': 'Set the advertisement map longitude',
'get freq ': 'Get frequency (Mhz)',
'get af': 'Get Air-time factor',
'get tx': 'Get Tx power (dBm)',
'get repeat': 'Get repeater mode',
'get advert.interval': 'Get advert rebroadcast interval (minutes)',
'get name': 'Get advertisement name',
'get lat': 'Get the advertisement map latitude',
'get lon': 'Get the advertisement map longitude',
};
async function delay(milis) {
return await new Promise((resolve) => setTimeout(resolve, milis));
}
function toSlug(text) {
return String(text).toLowerCase()
.replace(/[^a-z0-9.]+/g, '-')
.replace(/^-|-$/g, '');
}
function getGithubReleases(roleType, files) {
const versions = {};
for(const [fileType, matchRE] of Object.entries(files)) {
for(const versionType of github) {
if(versionType.type !== roleType) { continue }
const version = versions[versionType.version] ??= {
notes: versionType.notes,
files: []
};
for(const file of versionType.files) {
if(!new RegExp(matchRE).test(file.name)) { continue }
version.files.push({
type: fileType,
name: file.url,
title: file.name,
})
}
}
}
return versions;
}
function addGithubFiles() {
for(const device of config.device) {
for(const firmware of device.firmware) {
const gDef = firmware.github;
if(!gDef?.files) { continue }
firmware.version = getGithubReleases(gDef.type, gDef.files);
// clean versions without files
for(const [verName, verValue] of Object.entries(firmware.version)) {
if(verValue.files.length === 0) delete firmware.version[verName]
}
}
}
config.device = config.device.filter(device => device.firmware.some(firmware => Object.keys(firmware.version).length > 0 ));
return config;
}
async function digestMessage(message) {
const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array
const hashBuffer = await window.crypto.subtle.digest("SHA-256", msgUint8); // hash the message
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, "0"))
.join(""); // convert bytes to hex string
return hashHex;
}
async function blobToBinaryString(blob) {
const bytes = new Uint8Array(await blob.arrayBuffer())
let binString = '';
for (let i = 0; i < bytes.length; i++) {
binString += String.fromCharCode(bytes[i]);
}
return binString;
}
console.log(addGithubFiles());
function setup() {
const consoleEditBox = ref();
const consoleWindow = ref();
const deviceFilterText = ref('');
const snackbar = reactive({
text: '',
class: '',
icon: '',
});
const selected = reactive({
device: null,
firmware: null,
version: null,
wipe: false,
espFlashAddress: 0x10000,
nrfEraserFlashingPercent: 0,
nrfEraserFlashing: false,
port: null,
});
const getRoleFwValue = (firmware, key) => {
const role = config.role[firmware.role] ?? {};
return firmware[key] ?? role[key] ?? '';
}
const getSelFwValue = (key) => {
const fwVersion = selected.firmware.version[selected.version];
return fwVersion ? fwVersion[key] || '' : '';
}
const getNotice = (selected) => {
let notice = config.notice[selected.firmware.notice] || selected.firmware.notice || '';
if(notice) {
notice = notice.replaceAll(/\$\{(\w+)\}/g, (_, varName) => selected.device[varName] || '');
}
return notice;
}
const formatChangeLog = (changelog) => {
return changelog
.replace(/change log:\r?\n/i, '')
.replace(/^[-*] /mg, '')
.replace(/#(\d+)$/gm, `<a target="_blank" href="https://github.com/meshcore-dev/MeshCore/pull/$1">#$1</a>`)
// .split(/\r?\n/)
// .map(l => `* ${l}`)
// .join('\n')
}
const flashing = reactive({
supported: 'Serial' in window || 'serial' in window.navigator,
instance: null,
locked: false,
percent: 0,
log: '',
error: '',
dfuComplete: false,
});
const serialCon = reactive({
instance: null,
opened: false,
content: '',
edit: '',
});
window.app = { selected, flashing, serialCon };
const log = {
clean() { flashing.log = '' },
write(data) { flashing.log += data },
writeLine(data) { flashing.log += data + '\n' }
};
const retry = async() => {
flashing.active = false;
flashing.log = '';
flashing.error = '';
flashing.dfuComplete = false;
flashing.percent = 0;
if(flashing.instance instanceof ESPLoader) {
await flashing.instance?.hr.reset();
await flashing.instance?.transport?.disconnect();
}
}
const close = () => {
location.reload()
}
const getFirmwarePath = (file) => {
return file.name.startsWith('/') ? file.name : `${config.staticPath}/${file.name}`;
}
const firmwareHasData = (firmware) => {
const firstVersion = Object.keys(firmware.version)[0];
if(!firstVersion) return false;
return firmware.version[firstVersion].files.length > 0;
}
// --- URL Routing ---
// NOTE: the server must serve index.html for all paths (catch-all / try_files).
const deviceToSlug = (device) => toSlug([device.class, device.name].join('-'));
const firmwareToSlug = (firmware) => {
const title = getRoleFwValue(firmware, 'title');
const subTitle = getRoleFwValue(firmware, 'subTitle');
return toSlug(subTitle ? `${title}-${subTitle}` : title);
};
let initializingFromUrl = false;
const buildUrl = () => {
if (serialCon.opened) return '/console';
if (!selected.device) return '/';
let path = '/' + deviceToSlug(selected.device) + '/';
if (!selected.firmware) return path;
path += firmwareToSlug(selected.firmware) + '/';
if (selected.version) path += toSlug(selected.version);
return path;
};
const updateUrl = (replace = false) => {
if (initializingFromUrl) return;
const path = buildUrl();
if (window.location.pathname !== path) {
replace ? history.replaceState(null, '', path) : history.pushState(null, '', path);
}
};
const applyUrlPath = (path) => {
initializingFromUrl = true;
const segments = path.replace(/^\/|\/$/g, '').split('/').filter(Boolean);
if (segments.length === 0 || segments[0] === 'console') {
nextTick(() => { initializingFromUrl = false; });
return;
}
const [deviceSlug, roleSlug, versionSlug] = segments;
const matchingDevices = config.device.filter(d => deviceToSlug(d) === deviceSlug);
if (matchingDevices.length === 0) {
nextTick(() => { initializingFromUrl = false; });
return;
}
// When multiple devices share the same slug, use the firmware slug to pick the right one
let device, firmware;
if (roleSlug && matchingDevices.length > 1) {
for (const d of matchingDevices) {
const f = d.firmware.find(f => firmwareToSlug(f) === roleSlug && firmwareHasData(f));
if (f) { device = d; firmware = f; break; }
}
}
if (!device) device = matchingDevices[0];
selected.device = device;
if (!roleSlug) {
nextTick(() => { initializingFromUrl = false; });
return;
}
if (!firmware) firmware = device.firmware.find(f => firmwareToSlug(f) === roleSlug && firmwareHasData(f));
if (!firmware) {
nextTick(() => { initializingFromUrl = false; });
return;
}
selected.firmware = firmware;
// Use nextTick so the firmware watcher sets the default version first,
// then we override it with the version from the URL.
nextTick(() => {
if (versionSlug) {
const versionName = Object.keys(firmware.version).find(v => toSlug(v) === versionSlug);
if (versionName) selected.version = versionName;
}
initializingFromUrl = false;
});
};
const stepBack = () => {
if(selected.device && selected.firmware) {
if(selected.firmware.version[selected.version].customFile) {
selected.firmware = null;
selected.device = null;
return
}
selected.firmware = null;
return;
}
if(selected.device) {
selected.device = null;
}
}
const flasherCleanup = async () => {
flashing.active = false;
flashing.log = '';
flashing.error = '';
flashing.dfuComplete = false;
flashing.percent = 0;
selected.firmware = null;
selected.version = null;
selected.wipe = false;
selected.device = null;
selected.nrfEraserFlashingPercent = 0;
selected.nrfEraserFlashing = false;
if(flashing.instance instanceof ESPLoader) {
await flashing.instance?.hr.reset();
await flashing.instance?.transport?.disconnect();
}
else if(flashing.instance instanceof Dfu) {
try {
flashing.instance.port.close()
}
catch(e) {
console.error(e);
}
}
flashing.instance = null;
}
const openSerialGUI = () => {
window.open('https://config.meshcore.dev','meshcore_config','directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=1000,height=800');
}
const openSerialCon = async() => {
const port = selected.port = await navigator.serial.requestPort();
const serialConsole = serialCon.instance = new SerialConsole(port);
serialCon.content = '-------------------------------------------------------------------------\n';
serialCon.content += 'Welcome to MeshCore serial console.\n'
serialCon.content += 'Click on the cursor to get all supported commands.\n';
serialCon.content += '-------------------------------------------------------------------------\n\n';
serialConsole.onOutput = (text) => {
serialCon.content += text;
};
serialConsole.connect();
serialCon.opened = true;
await nextTick();
consoleEditBox.value.focus();
}
const closeSerialCon = async() => {
serialCon.opened = false;
await serialCon.instance.disconnect();
}
const sendCommand = async(text) => {
const consoleEl = consoleWindow.value;
serialCon.edit = '';
await serialCon.instance.sendCommand(text);
setTimeout(() => consoleEl.scrollTop = consoleEl.scrollHeight, 100);
}
const dfuMode = async() => {
await Dfu.forceDfuMode(await navigator.serial.requestPort({}))
flashing.dfuComplete = true;
}
const customFirmwareLoad = async(ev) => {
const firmwareFile = ev.target.files[0];
const type = firmwareFile.name.endsWith('.bin') ? 'esp32' : 'nrf52';
selected.device = {
name: 'Custom device',
type,
};
if(firmwareFile.name.endsWith('-merged.bin')) {
alert(
'You selected custom file that ends with "merged.bin".'+
'This will erase your flash! Proceed with caution.'+
'If you want just to update your firmware, please use non-merged bin.'
);
selected.wipe = true;
selected.espFlashAddress = 0;
}
selected.firmware = {
icon: 'unknown_document',
title: firmwareFile.name,
version: {},
}
selected.version = firmwareFile.name;
selected.firmware.version[selected.version] = {
customFile: true,
files: [{ type: 'flash', file: firmwareFile }]
}
}
const espReset = async(t) => {
await t.setRTS(true);
await delay(100)
await t.setRTS(false);
}
const nrfErase = async() => {
if(!(selected.device.type === 'nrf52' && selected.device.erase)) {
console.error('nRF erase called for non-nrf device or device.erase is not defined')
return;
}
const url = `${config.staticPath}/${selected.device.erase}`;
console.log('downloading: ' + url);
const resp = await fetch(url);
if(resp.status !== 200) {
alert(`Could not download the firmware file from the server, reported: HTTP ${resp.status}.\nPlease try again.`)
return;
}
const flashData = await resp.blob();
const port = selected.port = await navigator.serial.requestPort({});
const dfu = new Dfu(port);
try {
selected.nrfEraserFlashing = true;
await dfu.dfuUpdate(flashData, async (progress) => {
selected.nrfEraserFlashingPercent = progress;
if(progress === 100 && selected.nrfEraserFlashing) {
selected.nrfEraserFlashing = false;
selected.dfuComplete = false;
setTimeout(() => {
alert('Device erase firmware has been flashed and flash has been erased.\nYou can flash MeshCore now.');
}, 200);
}
}, 60000);
}
catch(e) {
alert(`nRF flashing erase firmware failed: ${e}.\nDid you put the device into DFU mode before attempting erasing?`);
selected.nrfEraserFlashing = false;
selected.nrfEraserFlashingPercent = 0;
return;
}
}
const canFlash = (device) => {
return device.type !== 'noflash'
}
const flashDevice = async() => {
const device = selected.device;
const firmware = selected.firmware.version[selected.version];
const flashFiles = firmware.files.filter(f => f.type.startsWith('flash'));
if(!flashFiles[0]) {
alert('Cannot find configuration for flash file! please report this to Discord.')
flasherCleanup();
return;
}
let flashData;
if(flashFiles[0].file) {
flashData = flashFiles[0].file;
} else {
let flashFile;
if(device.type === 'esp32') {
flashFile = flashFiles.find(f => f.type === (selected.wipe ? 'flash-wipe' : 'flash-update'));
if(selected.wipe) selected.espFlashAddress = 0x00000;
}
else {
flashFile = flashFiles[0];
}
console.log({flashFiles, flashFile});
const url = getFirmwarePath(flashFile);
console.log('downloading: ' + url);
const resp = await fetch(url);
if(resp.status !== 200) {
alert(`Could not download the firmware file from the server, reported: HTTP ${resp.status}.\nPlease try again.`)
return;
}
flashData = await resp.blob();
}
const port = selected.port = await navigator.serial.requestPort({});
if(device.type === 'esp32') {
let esploader;
let transport;
const flashOptions = {
terminal: log,
compress: true,
eraseAll: selected.wipe,
flashSize: 'keep',
flashMode: 'keep',
flashFreq: 'keep',
baudrate: 115200,
romBaudrate: 115200,
enableTracing: false,
fileArray: [{
data: await blobToBinaryString(flashData),
address: selected.espFlashAddress
}],
reportProgress: async (_, written, total) => {
flashing.percent = (written / total) * 100;
},
};
try {
flashing.active = true;
transport = new Transport(port, true);
flashOptions.transport = transport;
flashing.instance = esploader = new ESPLoader(flashOptions);
esploader.hr = new HardReset(transport);
await esploader.main();
await esploader.flashId();
}
catch(e) {
console.error(e);
flashing.error = `Failed to initialize. Did you place the device into firmware download mode? Detail: ${e}`;
esploader = null;
return;
}
try {
await esploader.writeFlash(flashOptions);
await delay(100);
await esploader.after('hard_reset');
await delay(100);
await espReset(transport);
await transport.disconnect();
}
catch(e) {
console.error(e);
flashing.error = `ESP32 flashing failed: ${e}`;
await espReset(transport);
await transport.disconnect();
return;
}
}
else if(device.type === 'nrf52') {
const dfu = flashing.instance = new Dfu(port);
flashing.active = true;
try {
await dfu.dfuUpdate(flashData, async (progress) => {
flashing.percent = progress;
}, 60000);
}
catch(e) {
console.error(e);
flashing.error = `nRF flashing failed: ${e}. Please reset the device and try again.`;
return;
}
}
};
const devices = computed(() => {
const classes = ['ripple', 'meshos', 'community'];
const deviceGroups = {};
let index = 0;
for(const cls of classes) {
const devices = config.device.toSorted(
(a, b) => (index + a.maker + a.name).localeCompare(index + b.maker + b.name)
).filter(
d => d.class === cls && (deviceFilterText.value == '' || d.name.toLowerCase().includes(deviceFilterText.value?.toLowerCase()))
)
if(devices.length > 0) deviceGroups[cls] = devices;
}
return deviceGroups;
});
const showMessage = (text, icon, displayMs) => {
snackbar.class = 'active';
snackbar.text = text;
snackbar.icon = icon || '';
setTimeout(() => {
snackbar.icon = '';
snackbar.text = '';
snackbar.class = '';
}, displayMs || 2000);
}
const consoleMouseUp = (ev) => {
if(window.getSelection().toString().length) {
navigator.clipboard.writeText(window.getSelection().toString())
showMessage('text copied to clipboard');
}
consoleEditBox.value.focus();
}
watch(() => selected.firmware, (firmware) => {
if(firmware == null) return;
selected.version = Object.keys(firmware.version)[0];
});
watch(() => selected.device, updateUrl);
watch(() => selected.firmware, updateUrl);
watch(() => selected.version, () => updateUrl(true)); // replace: version is a refinement, not a new nav step
watch(() => serialCon.opened, updateUrl);
window.addEventListener('popstate', () => {
if (serialCon.opened) closeSerialCon();
flashing.active = false;
flashing.log = '';
flashing.error = '';
selected.firmware = null;
selected.version = null;
selected.device = null;
applyUrlPath(window.location.pathname);
});
applyUrlPath(window.location.pathname);
return {
snackbar,
consoleEditBox, consoleWindow, consoleMouseUp,
config, devices, selected, flashing, deviceFilterText,
flashDevice, flasherCleanup, dfuMode,
serialCon, closeSerialCon, openSerialCon,
sendCommand, openSerialGUI,
retry, close, commandReference,
stepBack,
customFirmwareLoad, getFirmwarePath,
getSelFwValue, getRoleFwValue, getNotice, formatChangeLog,
firmwareHasData,
canFlash, nrfErase
}
}
createApp({ setup }).mount('#app');