Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
102 lines
5.9 KiB
C++
102 lines
5.9 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#pragma once
|
|
#include "firmware_source.h"
|
|
#include "bms_native.h"
|
|
#include "swd_native.h"
|
|
#include <memory>
|
|
#include <QElapsedTimer>
|
|
#include <QTimer>
|
|
|
|
class FirmwareUpload {
|
|
QByteArray image;
|
|
QJsonObject manifest;
|
|
QElapsedTimer age;
|
|
bool sealed = false;
|
|
public:
|
|
bool running = false;
|
|
bool consumed = false;
|
|
bool cancelRequested = false;
|
|
void cancel(VescInterface &v){cancelRequested=true;v.fwUploadCancel();v.swdCancel();}
|
|
QJsonObject state{{"state", "empty"}, {"progress", 0}};
|
|
QJsonObject stage(const QString &method, const QJsonObject &p, const FW_RX_PARAMS &identity) {
|
|
require(!running, "Firmware upload owns the controller");
|
|
if (method == "firmware_begin") {
|
|
require(!p.contains("swd") || (!p.contains("can_id") && p.value("kind")=="firmware"), "SWD cannot share a CAN route or bootloader mode");
|
|
require(p.value("hardware").toString() == identity.hw && identity.hwType == HW_TYPE_VESC,
|
|
"Declared hardware does not match this controller");
|
|
require(p.value("uuid").toString().toLower() == QString::fromLatin1(identity.uuid.toHex()),
|
|
"Firmware target UUID changed");
|
|
const int size = p.value("bytes").toInt(-1);
|
|
const auto format = p.value("format").toString("bin");
|
|
require(format == "bin" || format == "hex", "Unsupported image format");
|
|
require(size >= (format == "hex" ? 32 : 256) && size <= (format == "hex" ? 8 : 2) * 1024 * 1024, "Image size outside bound");
|
|
require(QRegularExpression("^[0-9a-f]{64}$").match(p.value("sha256").toString()).hasMatch(), "Invalid image digest");
|
|
require(p.value("kind") == "firmware" || p.value("kind") == "bootloader", "Invalid image kind");
|
|
image.clear(); manifest = p; sealed = consumed = false; age.start();
|
|
state = {{"state", "staging"}, {"progress", 0}};
|
|
return {{"offset", 0}};
|
|
}
|
|
require(age.isValid() && age.elapsed() < 600000 && !consumed, "Staged image expired or consumed");
|
|
if (method == "firmware_chunk") {
|
|
require(!sealed && p.value("offset").toInt(-1) == image.size(), "Image offset changed");
|
|
const auto text = p.value("data").toString().toLatin1();
|
|
const auto data = QByteArray::fromBase64(text);
|
|
require(data.toBase64() == text && !data.isEmpty() && data.size() <= 32768,
|
|
"Invalid image chunk");
|
|
require(image.size() + data.size() <= manifest.value("bytes").toInt(), "Image exceeds declared size");
|
|
image.append(data); return {{"offset", image.size()}};
|
|
}
|
|
require(method == "firmware_seal", "Unknown staging operation");
|
|
require(image.size() == manifest.value("bytes").toInt() && sha256(image) == manifest.value("sha256").toString(),
|
|
"Image size or digest changed");
|
|
const auto range=manifest.contains("swd")?swdRange(manifest.value("swd").toObject()):QPair<quint32,int>{0,0};
|
|
auto source = inspectFirmwareSource(image, manifest.value("kind").toString(), manifest.value("format").toString("bin"),range.first,range.second);
|
|
image = source.image;
|
|
auto info = source.info;
|
|
manifest.insert("sha256", info.value("sha256"));
|
|
manifest.insert("bytes", image.size());
|
|
sealed = true; state = {{"state", "ready"}, {"progress", 0}};
|
|
return info;
|
|
}
|
|
QJsonObject start(VescInterface &vesc, const QJsonObject &p, const FW_RX_PARAMS &identity, bool &remoteQuery) {
|
|
require(!running && !consumed && sealed && age.elapsed() < 600000, "No fresh sealed image");
|
|
std::shared_ptr<NativeCanTarget> route;
|
|
FW_RX_PARAMS target=identity;
|
|
if(manifest.contains("can_id")) {
|
|
route=std::make_shared<NativeCanTarget>(vesc,remoteQuery,manifest.value("can_id").toInt(-1));
|
|
target=route->identify();
|
|
require(nativeFirmwareQualified(target.major,target.minor,target.hwType,target.isTestFw,target.customConfigNum),"CAN firmware schema not qualified");
|
|
}
|
|
require(p.value("sha256") == manifest.value("sha256") && manifest.value("hardware") == target.hw &&
|
|
manifest.value("uuid") == QString::fromLatin1(target.uuid.toHex()), "Firmware target changed");
|
|
cancelRequested=false;consumed = running = true; state = {{"state", "uploading"}, {"progress", 0}};
|
|
// The original upload runs nested Qt event loops; the private input
|
|
// notifier remains available for status/cancel only. No second owner.
|
|
QTimer::singleShot(0, [&vesc, this, route] {
|
|
QTimer bound; bound.setSingleShot(true);
|
|
QObject::connect(&bound, &QTimer::timeout, [&] { cancel(vesc); });
|
|
QObject observer;
|
|
QObject::connect(&vesc, &VescInterface::fwUploadStatus, &observer,
|
|
[&](const QString &message, double progress, bool) {
|
|
state.insert("message", message.left(512));
|
|
state.insert("progress", qBound(0.0, progress, 1.0));
|
|
});
|
|
bound.start(240000);
|
|
bool ok = false;
|
|
try {
|
|
require(!cancelRequested,"Upload cancelled before start");
|
|
if(manifest.contains("swd")) {
|
|
const auto result=uploadSwd(vesc,manifest.value("swd").toObject(),image,cancelRequested);
|
|
for(auto i=result.begin();i!=result.end();++i)state.insert(i.key(),i.value());ok=true;
|
|
} else ok = vesc.fwUpload(image, manifest.value("kind") == "bootloader", false, true, false);
|
|
}
|
|
catch (...) { state.insert("message", "Upload exception; outcome unknown"); }
|
|
image.clear(); sealed = false; running = false;
|
|
state.insert("state", ok ? "uploaded" : "unknown");
|
|
state.insert("upload_confirmed", ok);
|
|
if (ok) state.insert("progress", 1);
|
|
});
|
|
return state;
|
|
}
|
|
};
|