Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
142 lines
7.9 KiB
C++
142 lines
7.9 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#pragma once
|
|
#include "config_export.h"
|
|
#include <cmath>
|
|
#include <QSet>
|
|
|
|
static QJsonValue configValue(const ConfigParam &p) {
|
|
switch (p.type) {
|
|
case CFG_T_DOUBLE: return p.valDouble;
|
|
case CFG_T_INT: case CFG_T_ENUM: case CFG_T_BITFIELD: return p.valInt;
|
|
case CFG_T_BOOL: return bool(p.valInt);
|
|
case CFG_T_QSTRING: return p.valString;
|
|
default: return QJsonValue();
|
|
}
|
|
}
|
|
|
|
static void validateEdit(const ConfigParam &p, const QJsonValue &v) {
|
|
if (p.type == CFG_T_BOOL) { require(v.isBool(), "Expected a boolean parameter"); return; }
|
|
if (p.type == CFG_T_QSTRING) {
|
|
require(v.isString() && v.toString().size() <= p.maxLen, "String parameter exceeds its native bound"); return;
|
|
}
|
|
require(v.isDouble() && std::isfinite(v.toDouble()), "Expected a finite number");
|
|
const double n = v.toDouble();
|
|
if (p.type == CFG_T_DOUBLE) require(n >= p.minDouble && n <= p.maxDouble, "Number outside native parameter bounds");
|
|
else {
|
|
require(std::trunc(n) == n && n >= -2147483648.0 && n <= 2147483647.0, "Expected a bounded integer");
|
|
if (p.type == CFG_T_ENUM) require(n >= 0 && n < p.enumNames.size(), "Unknown native enum value");
|
|
else if (p.type == CFG_T_BITFIELD) require(n >= 0 && n <= 255, "Invalid bitfield value");
|
|
else { require(p.type == CFG_T_INT, "Unsupported parameter type"); require(n >= p.minInt && n <= p.maxInt, "Integer outside native parameter bounds"); }
|
|
}
|
|
}
|
|
|
|
static void updateEdit(ConfigParams *config, const QString &name, const QJsonValue &v) {
|
|
require(config->hasParam(name), "Unknown parameter name");
|
|
const auto p = config->getParamCopy(name);
|
|
require(p.transmittable && config->getSerializeOrder().contains(name), "Parameter is not transmitted by this firmware");
|
|
validateEdit(p, v);
|
|
switch (p.type) {
|
|
case CFG_T_DOUBLE: config->updateParamDouble(name, v.toDouble()); break;
|
|
case CFG_T_INT: case CFG_T_BITFIELD: config->updateParamInt(name, v.toInt()); break;
|
|
case CFG_T_ENUM: config->updateParamEnum(name, v.toInt()); break;
|
|
case CFG_T_BOOL: config->updateParamBool(name, v.toBool()); break;
|
|
case CFG_T_QSTRING: config->updateParamString(name, v.toString()); break;
|
|
default: throw std::runtime_error("Unsupported parameter editor");
|
|
}
|
|
}
|
|
|
|
static QByteArray configPacket(ConfigParams *config, int code) {
|
|
VByteArray raw; raw.vbAppendInt8(code); config->serialize(raw); return raw;
|
|
}
|
|
|
|
static void loadExactConfig(ConfigParams *config, const QByteArray &packet, int code) {
|
|
const auto shape = configPacket(config, code);
|
|
require(packet.size() == shape.size() && packet.size() > 5 && quint8(packet[0]) == code,
|
|
"Configuration packet shape mismatch");
|
|
VByteArray raw(packet.mid(1));
|
|
require(config->deSerialize(raw) && raw.isEmpty(), "Native configuration decoder rejected payload");
|
|
require(configPacket(config, code) == packet, "Configuration packet is not byte-exact");
|
|
}
|
|
|
|
// Validate the flat value document before handing it to upstream setXML. Never
|
|
// import a parameter-definition/schema document or silently ignore unknown tags.
|
|
static void validateConfigXml(ConfigParams *config, const QString &xml, const QString &root) {
|
|
require(!xml.isEmpty() && xml.toUtf8().size() <= 32768, "XML exceeds import bound");
|
|
QXmlStreamReader reader(xml);
|
|
QSet<QString> seen;
|
|
int depth = 0; bool rooted = false;
|
|
while (!reader.atEnd()) {
|
|
const auto token = reader.readNext();
|
|
require(token != QXmlStreamReader::DTD && token != QXmlStreamReader::EntityReference,
|
|
"XML entities and DTD are not accepted");
|
|
if (token == QXmlStreamReader::StartElement) {
|
|
++depth;
|
|
if (depth == 1) { require(!rooted && reader.name() == root, "Wrong configuration XML root"); rooted = true; continue; }
|
|
require(depth == 2, "Nested XML values are not accepted");
|
|
const auto name = reader.name().toString();
|
|
if(name=="ConfigVersion" && config->getStoreConfigVersion()) {
|
|
require(!seen.contains(name),"Duplicate XML version");seen.insert(name);
|
|
require(reader.readElementText(QXmlStreamReader::ErrorOnUnexpectedElement)==QString::number(VT_CONFIG_VERSION),"XML configuration version differs");--depth;continue;
|
|
}
|
|
require(config->hasParam(name) && !seen.contains(name), "Unknown or duplicate XML parameter");
|
|
seen.insert(name);
|
|
const auto p = config->getParamCopy(name);
|
|
const auto text = reader.readElementText(QXmlStreamReader::ErrorOnUnexpectedElement);
|
|
require(!reader.hasError(), "Malformed XML parameter");
|
|
--depth;
|
|
bool ok = true;
|
|
if (p.type == CFG_T_QSTRING) {
|
|
if (p.transmittable) require(text.size() <= p.maxLen, "XML string exceeds parameter bound");
|
|
else require(text == p.valString, "XML changes non-transmitted metadata");
|
|
}
|
|
else if (p.type == CFG_T_BOOL) require(text == "0" || text == "1", "Invalid XML boolean");
|
|
else {
|
|
const auto n = text.toDouble(&ok);
|
|
require(ok && std::isfinite(n), "Invalid XML number");
|
|
if (p.type != CFG_T_DOUBLE) require(std::trunc(n) == n, "Invalid XML integer");
|
|
}
|
|
} else if (token == QXmlStreamReader::EndElement) --depth;
|
|
}
|
|
require(!reader.hasError() && rooted && depth == 0 && !seen.isEmpty(), "Incomplete configuration XML");
|
|
}
|
|
|
|
static QJsonObject prepareConfig(ConfigParams *before, const QJsonObject &source, int code, const QString &root) {
|
|
ConfigParams candidate; candidate = *before;
|
|
require(source.size() == 1, "One configuration source is required");
|
|
if (source.contains("changes")) {
|
|
require(source.value("changes").isObject(), "Changes must be an object");
|
|
const auto changes = source.value("changes").toObject();
|
|
require(changes.size() <= 512, "Too many changed parameters");
|
|
for (auto i = changes.begin(); i != changes.end(); ++i) updateEdit(&candidate, i.key(), i.value());
|
|
} else if (source.contains("xml")) {
|
|
require(source.value("xml").isString(), "XML must be a string");
|
|
const auto xml = source.value("xml").toString();
|
|
validateConfigXml(before, xml, root);
|
|
QXmlStreamReader reader(xml);
|
|
require(candidate.setXML(reader, root), "Upstream rejected XML configuration");
|
|
} else if (source.contains("payload")) {
|
|
const auto encoded = source.value("payload").toString().toLatin1();
|
|
const auto bytes = QByteArray::fromBase64(encoded, QByteArray::AbortOnBase64DecodingErrors);
|
|
require(bytes.toBase64() == encoded, "Invalid backup encoding");
|
|
loadExactConfig(&candidate, bytes, code);
|
|
} else throw std::runtime_error("Unsupported configuration source");
|
|
for (const auto &name : candidate.getParamOrder()) {
|
|
const auto p = candidate.getParamCopy(name);
|
|
if (configValue(p) == configValue(before->getParamCopy(name))) continue;
|
|
require(p.transmittable && candidate.getSerializeOrder().contains(name), "XML changes a non-transmitted value");
|
|
validateEdit(p, configValue(p));
|
|
}
|
|
const auto packet = configPacket(&candidate, code);
|
|
// Show actual representable values, including native wire quantization.
|
|
loadExactConfig(&candidate, packet, code);
|
|
QJsonArray changes;
|
|
for (const auto &name : candidate.getSerializeOrder()) {
|
|
const auto p = candidate.getParamCopy(name), old = before->getParamCopy(name);
|
|
if (configValue(p) != configValue(old)) changes.append(QJsonObject{
|
|
{"name", name}, {"title", p.longName}, {"before", configValue(old)}, {"after", configValue(p)},
|
|
{"suffix", p.suffix}, {"editor_scale", p.editorScale}});
|
|
}
|
|
return {{"before_sha256", sha256(configPacket(before, code))}, {"after_sha256", sha256(packet)},
|
|
{"payload", QString::fromLatin1(packet.toBase64())}, {"changes", changes}};
|
|
}
|