feat: Reduce risk of corrupt radio.yml (#1481)
* reduce risk of corrupt radio.yml #1412 Adds CRC16 checksum to radio.yml when saved. Adds save to temporary file, folowed by rename to radio.yml Adds check on load, with fall-back to temporary file * Made sure that only the first read block of radio.yml is checked for the 'checksum' value. Added checksum generation on radio.yml produced by conversion. * Added namespace. Github actions triggered build failed on this - compiler difference. * Fixed typo in code not run locally. * Made checksum result code a enum class. Fixed null-poionter dereference bug. * Added check and handling for radio.yml file not read (will be handled as if checksum failed) Added evaluation of checksum when radio_new.yml is read. Added f_unlink() calls before f_rename() to ensure the target filename does not exist. * Added radio.yml cheksum handling to Simulator/Companion. Cleaned radio firmware handling of checksum field up to be newline agnostic. * Added handling of files without checksum. - Letting files pass the check if no checksum field is present (old files) and the size is realistic (25 bytes) * Fixed bug causing new conversion from binary if no radio.yml BUT a radio_new.yaml is present in the filesystem. Now the conversion is only performed if neither file exsits. * repair rebase problems * rerun yaml generator * fix another rebase problem * update text in companion to be more meaningful when the radio.yml file has a checksum error fix wrong error message in trace * add trigger to check radio config when leavin menu_radio * fix: add handling for temporary and error yaml radio settings files to simu/simulator/libsimulator * fix corner cases when comverting non crc-ed files * chore: Tweak radio data warnings for screen sizes - Changed the define to be a little shorter - Added missing define for TW - Three string lengths to suit 128, 212 and basically colorlcd Co-authored-by: gagarinlg <malte@langermann.net> Co-authored-by: Peter Feerick <peter.feerick@gmail.com>
This commit is contained in:
co-authored by
gagarinlg
Peter Feerick
parent
e6697bca89
commit
e576925bee
@@ -20,12 +20,47 @@
|
||||
|
||||
#include "edgetxinterface.h"
|
||||
|
||||
#include "crc.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "yaml_ops.h"
|
||||
#include "yaml_generalsettings.h"
|
||||
#include "yaml_modeldata.h"
|
||||
|
||||
#include <QMessageBox>
|
||||
|
||||
|
||||
static uint16_t calculateChecksum(const QByteArray& data, uint16_t checksum)
|
||||
{
|
||||
std::string fileContent = data.toStdString();
|
||||
|
||||
size_t startPos = 0;
|
||||
size_t pos = 0;
|
||||
uint8_t isCRLF = 0;
|
||||
std::string newLine[2] = {"\n","\r\n"};
|
||||
|
||||
pos = fileContent.find("\n", 0);
|
||||
if(pos == 0) {
|
||||
return checksum;
|
||||
}
|
||||
|
||||
if(fileContent[pos-1] == '\r') {
|
||||
isCRLF = 1;
|
||||
qDebug() << "** File has CRLF line endings";
|
||||
} else {
|
||||
isCRLF = 0;
|
||||
qDebug() << "** File has LF line endings";
|
||||
}
|
||||
|
||||
// The first line contains the "checksum" value - if present. Skip it
|
||||
if ( fileContent.find("checksum:") == 0) {
|
||||
startPos = fileContent.find(newLine[isCRLF]) + newLine[isCRLF].length();
|
||||
}
|
||||
|
||||
checksum = crc16(0, (const uint8_t *) &fileContent[startPos], fileContent.length() - startPos, checksum);
|
||||
return checksum;
|
||||
}
|
||||
|
||||
static YAML::Node loadYamlFromByteArray(const QByteArray& data)
|
||||
{
|
||||
// TODO: use real streaming to avoid memory copies
|
||||
@@ -33,15 +68,25 @@ static YAML::Node loadYamlFromByteArray(const QByteArray& data)
|
||||
return YAML::Load(data_istream);
|
||||
}
|
||||
|
||||
static void writeYamlToByteArray(const YAML::Node& node, QByteArray& data)
|
||||
static void writeYamlToByteArray(const YAML::Node& node, QByteArray& data, bool addChecksum = false)
|
||||
{
|
||||
// TODO: use real streaming to avoid memory copies
|
||||
std::stringstream data_ostream;
|
||||
|
||||
data_ostream << node;
|
||||
data = QByteArray::fromStdString(data_ostream.str());
|
||||
|
||||
qDebug() << "Saving YAML:";
|
||||
qDebug() << data_ostream.str().c_str();
|
||||
|
||||
if(addChecksum) {
|
||||
uint16_t checksum = 0xFFFF;
|
||||
checksum = calculateChecksum(data, checksum);
|
||||
std::stringstream checksum_ostream;
|
||||
checksum_ostream << "checksum: " << checksum << std::endl;
|
||||
data.prepend(checksum_ostream.str().c_str());
|
||||
}
|
||||
|
||||
qDebug() << data.toStdString().c_str();
|
||||
}
|
||||
|
||||
bool loadModelsListFromYaml(std::vector<CategoryData>& categories,
|
||||
@@ -87,6 +132,28 @@ bool loadModelFromYaml(ModelData& model, const QByteArray& data)
|
||||
|
||||
bool loadRadioSettingsFromYaml(GeneralSettings& settings, const QByteArray& data)
|
||||
{
|
||||
if(data.indexOf("checksum:") == 0) {
|
||||
int startPos = strlen("checksum:");
|
||||
int endPos = data.indexOf("\n");
|
||||
if (endPos > 0) {
|
||||
while( ((const char)data[startPos] == ' ') && (startPos < data.size())) {
|
||||
startPos++;
|
||||
}
|
||||
if (startPos < data.size()) {
|
||||
QByteArray checksumStr = data.mid(startPos, endPos - startPos);
|
||||
uint16_t fileChecksum = std::stoi(checksumStr.toStdString());
|
||||
uint16_t calculatedChecksum = calculateChecksum(data, 0xFFFF);
|
||||
qDebug() << "File checksum:" << fileChecksum;
|
||||
qDebug() << "Calculated checksum:" << calculatedChecksum;
|
||||
if (fileChecksum != calculatedChecksum) {
|
||||
qDebug() << "File checksum mismatch! Got: " << fileChecksum << ", expected: " << calculatedChecksum;
|
||||
QMessageBox::critical(NULL, CPN_STR_APP_NAME, QCoreApplication::translate("EdgeTXInterface", "Radio settings file checksum error. You are advised to review the settings"));
|
||||
//return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
YAML::Node node = loadYamlFromByteArray(data);
|
||||
node >> settings;
|
||||
if (settings.version < CPN_CURRENT_SETTINGS_VERSION) {
|
||||
@@ -159,7 +226,7 @@ bool writeRadioSettingsToYaml(const GeneralSettings& settings, QByteArray& data)
|
||||
YAML::Node node;
|
||||
node = settings;
|
||||
|
||||
writeYamlToByteArray(node, data);
|
||||
writeYamlToByteArray(node, data, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user