Skip to content

Commit ac1b33e

Browse files
committed
Backup config on flash
1 parent 99730ef commit ac1b33e

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

src/js/tabs/firmware_flasher.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import STM32DFU from '../protocols/stm32usbdfu';
1717
import { gui_log } from '../gui_log';
1818
import semver from 'semver';
1919
import { checkChromeRuntimeError } from '../utils/common';
20+
import { generateFilename } from '../utils/generate_filename';
2021

2122
const firmware_flasher = {
2223
targets: null,
@@ -1009,6 +1010,9 @@ firmware_flasher.initialize = function (callback) {
10091010
}).trigger('change');
10101011

10111012
$('a.flash_firmware').click(function () {
1013+
1014+
firmware_flasher.backupConfig();
1015+
10121016
if (!$(this).hasClass('disabled')) {
10131017
if (self.developmentFirmwareLoaded) {
10141018
checkShowAcknowledgementDialog();
@@ -1200,6 +1204,158 @@ firmware_flasher.initialize = function (callback) {
12001204
});
12011205
};
12021206

1207+
firmware_flasher.backupConfig = function () {
1208+
let mspHelper;
1209+
let cliBuffer = '';
1210+
let catchOutputCallback = null;
1211+
1212+
function readOutput(callback) {
1213+
catchOutputCallback = callback;
1214+
}
1215+
1216+
function writeOutput(text) {
1217+
if (catchOutputCallback) {
1218+
catchOutputCallback(text);
1219+
}
1220+
}
1221+
1222+
function readSerial(readInfo) {
1223+
const data = new Uint8Array(readInfo.data);
1224+
1225+
for (const charCode of data) {
1226+
const currentChar = String.fromCharCode(charCode);
1227+
1228+
switch (charCode) {
1229+
case 10:
1230+
if (GUI.operating_system === "Windows") {
1231+
writeOutput(cliBuffer);
1232+
cliBuffer = "";
1233+
}
1234+
break;
1235+
case 13:
1236+
if (GUI.operating_system !== "Windows") {
1237+
writeOutput(cliBuffer);
1238+
cliBuffer = "";
1239+
}
1240+
break;
1241+
default:
1242+
cliBuffer += currentChar;
1243+
}
1244+
}
1245+
}
1246+
1247+
function activateCliMode() {
1248+
return new Promise(resolve => {
1249+
const bufferOut = new ArrayBuffer(1);
1250+
const bufView = new Uint8Array(bufferOut);
1251+
1252+
cliBuffer = "";
1253+
bufView[0] = 0x23;
1254+
1255+
serial.send(bufferOut);
1256+
1257+
GUI.timeout_add('enter_cli_mode_done', () => {
1258+
resolve();
1259+
}, 500);
1260+
});
1261+
}
1262+
1263+
function sendSerial(line, callback) {
1264+
const bufferOut = new ArrayBuffer(line.length);
1265+
const bufView = new Uint8Array(bufferOut);
1266+
1267+
for (let cKey = 0; cKey < line.length; cKey++) {
1268+
bufView[cKey] = line.charCodeAt(cKey);
1269+
}
1270+
1271+
serial.send(bufferOut, callback);
1272+
}
1273+
1274+
function sendCommand(line, callback) {
1275+
sendSerial(`${line}\n`, callback);
1276+
}
1277+
1278+
function readCommand() {
1279+
let timeStamp = performance.now();
1280+
const output = [];
1281+
const commandInterval = "COMMAND_INTERVAL";
1282+
1283+
readOutput(str => {
1284+
timeStamp = performance.now();
1285+
output.push(str);
1286+
});
1287+
1288+
sendCommand("dump all");
1289+
1290+
return new Promise(resolve => {
1291+
GUI.interval_add(commandInterval, () => {
1292+
const currentTime = performance.now();
1293+
if (currentTime - timeStamp > 500) {
1294+
catchOutputCallback = null;
1295+
GUI.interval_remove(commandInterval);
1296+
resolve(output);
1297+
}
1298+
}, 500, false);
1299+
});
1300+
}
1301+
1302+
function onFinishClose() {
1303+
MSP.clearListeners();
1304+
}
1305+
1306+
function onClose(success) {
1307+
serial.disconnect(onFinishClose);
1308+
MSP.disconnect_cleanup();
1309+
}
1310+
1311+
function onSaveConfig() {
1312+
const waitingDialog = GUI.showWaitDialog( { title: i18n.getMessage("presetsLoadingDumpAll"), buttonCancelCallback: null } );
1313+
1314+
activateCliMode()
1315+
.then(readCommand)
1316+
.then(output => {
1317+
const prefix = `cli_backup_${firmware_flasher.selectedBoard}`;
1318+
const suffix = 'txt';
1319+
const text = output.join("\n");
1320+
const filename = generateFilename(prefix, suffix);
1321+
return GUI.saveToTextFileDialog(text, filename, suffix);
1322+
})
1323+
.then(() => {
1324+
waitingDialog.close();
1325+
sendCommand("exit", onClose);
1326+
});
1327+
}
1328+
1329+
function onConnect(openInfo) {
1330+
if (openInfo) {
1331+
mspHelper = new MspHelper();
1332+
serial.onReceive.addListener(readSerial);
1333+
MSP.listen(mspHelper.process_data.bind(mspHelper));
1334+
1335+
onSaveConfig();
1336+
} else {
1337+
gui_log(i18n.getMessage('serialPortOpenFail'));
1338+
}
1339+
}
1340+
1341+
const portPickerElement = $('div#port-picker #port');
1342+
1343+
// Not available in DFU mode
1344+
if (String(portPickerElement.val()) !== '0') {
1345+
const port = String(portPickerElement.val());
1346+
let baud = 115200;
1347+
1348+
if (!(serial.connected || serial.connectionId)) {
1349+
serial.connect(port, {bitrate: baud}, onConnect);
1350+
} else {
1351+
console.warn('Attempting to connect while there still is a connection', serial.connected, serial.connectionId);
1352+
serial.disconnect();
1353+
}
1354+
} else {
1355+
gui_log(i18n.getMessage('firmwareFlasherNoPortSelected'));
1356+
}
1357+
};
1358+
12031359
firmware_flasher.cleanup = function (callback) {
12041360
PortHandler.flush_callbacks();
12051361

0 commit comments

Comments
 (0)