Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
101 lines
7.1 KiB
C++
101 lines
7.1 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#pragma once
|
|
#include <functional>
|
|
#include "config_edit.h"
|
|
#include "bms_native.h"
|
|
#include <QElapsedTimer>
|
|
#include <QTimer>
|
|
#include <QtEndian>
|
|
|
|
// All protocol serialization/decoding remains in upstream Commands/ConfigParams.
|
|
// Replies are correlated by configuration index and exact offset; no write retry.
|
|
class NativeCustom {
|
|
public:
|
|
template<class Setup, class Send> static void wait(Setup setup, Send send, int ms=1800) {
|
|
QEventLoop loop; QTimer timeout; QObject owner; bool done=false;
|
|
timeout.setSingleShot(true);
|
|
std::function<void()> finish=[&] {done=true;loop.quit();};
|
|
setup(&owner,finish);
|
|
QObject::connect(&timeout,&QTimer::timeout,&loop,&QEventLoop::quit);
|
|
timeout.start(ms);send();if(!done)loop.exec();
|
|
require(done,"Custom configuration reply timed out");
|
|
}
|
|
static void schema(ConfigParams &config,const QByteArray &compressed) {
|
|
require(compressed.size()>=5 && compressed.size()<=65536,"Custom schema size invalid");
|
|
const auto size=qFromBigEndian<quint32>(reinterpret_cast<const uchar*>(compressed.constData()));
|
|
require(size>0 && size<=512*1024,"Custom schema decompression bound exceeded");
|
|
const auto xml=qUncompress(compressed);
|
|
require(xml.size()==int(size),"Custom schema compression invalid");
|
|
QXmlStreamReader scan(xml);int elements=0;
|
|
while(!scan.atEnd()){auto t=scan.readNext();require(t!=QXmlStreamReader::DTD && t!=QXmlStreamReader::EntityReference,"Custom schema entities forbidden");if(t==QXmlStreamReader::StartElement)require(++elements<=16000,"Custom schema too complex");}
|
|
require(!scan.hasError(),"Malformed custom schema");
|
|
require(config.loadCompressedParamsXml(compressed),"Upstream rejected custom schema");
|
|
require(config.getParamOrder().size()>0 && config.getParamOrder().size()<=512 && config.getSerializeOrder().size()<=512,"Custom parameter count invalid");
|
|
for(const auto &name:config.getSerializeOrder()) {
|
|
auto p=config.getParamCopy(name);
|
|
require(QRegularExpression("^[A-Za-z_][A-Za-z0-9_]{0,95}$").match(name).hasMatch(),"Custom parameter name invalid");
|
|
if(p.type==CFG_T_QSTRING)require(p.maxLen>=0 && p.maxLen<=1024,"Custom string size invalid");
|
|
require(p.enumNames.size()<=256,"Custom enum too large");
|
|
}
|
|
}
|
|
static QByteArray getSchema(Commands *cmd,int index) {
|
|
QByteArray data;int total=-1;QElapsedTimer elapsed;elapsed.start();
|
|
do {
|
|
require(elapsed.elapsed()<20000,"Custom schema read deadline");
|
|
const int offset=data.size(),amount=total<0?10:qMin(400,total-offset);
|
|
int receivedTotal=-1;QByteArray chunk;bool invalid=false;
|
|
wait([&](QObject *owner,std::function<void()> finish){QObject::connect(cmd,&Commands::customConfigChunkRx,owner,[&,finish](int id,int length,int at,QByteArray bytes){
|
|
if(id!=index || at!=offset)return;
|
|
invalid=length<1||length>65536||bytes.isEmpty()||bytes.size()>amount||at+bytes.size()>length||(total>=0&&length!=total);
|
|
receivedTotal=length;chunk=bytes;finish();
|
|
});},[&]{cmd->customConfigGetChunk(index,amount,offset);});
|
|
require(!invalid,"Custom schema fragment mismatch");total=receivedTotal;data.append(chunk);
|
|
}while(data.size()<total);
|
|
return data;
|
|
}
|
|
static QByteArray read(Commands *cmd,ConfigParams *config,int index,bool defaults=false) {
|
|
QByteArray bytes;
|
|
wait([&](QObject *owner,std::function<void()> finish){QObject::connect(cmd,&Commands::customConfigRx,owner,[&,finish](int id,QByteArray data){if(id==index){bytes=data;finish();}});},[&]{cmd->customConfigGet(index,defaults);});
|
|
require(bytes.size()>4 && bytes.size()<=16384,"Custom configuration size invalid");
|
|
// The adapter prefix is a local archive type tag; Commands owns the wire index.
|
|
QByteArray packet(1,char(COMM_GET_CUSTOM_CONFIG));packet.append(bytes);
|
|
loadExactConfig(config,packet,COMM_GET_CUSTOM_CONFIG);return packet;
|
|
}
|
|
static QJsonObject run(VescInterface &vesc,bool &remote,const QJsonObject &req) {
|
|
const auto method=req.value("method").toString();
|
|
const auto id=req.value("can_id"),ix=req.value("index");
|
|
require(id.isDouble()&&id.toDouble()==id.toInt(-1)&&id.toInt()>=0&&id.toInt()<=253,"Exact CAN address required");
|
|
require(ix.isDouble()&&ix.toDouble()==ix.toInt(-1)&&ix.toInt()>=0&&ix.toInt()<8,"Custom configuration index invalid");
|
|
NativeCanTarget route(vesc,remote,id.toInt());const auto target=route.identify();
|
|
require(bmsCommandsSupported(target) && ix.toInt()<target.customConfigNum,"BMS configuration profile unavailable");
|
|
const auto identity=canIdentity(target,id.toInt());
|
|
if(req.contains("target"))require(req.value("target").toObject()==identity,"BMS identity changed");
|
|
auto cmd=vesc.commands();
|
|
require(cmd->getLimitedCompatibilityCommands().contains(COMM_GET_CUSTOM_CONFIG_XML),"Transport does not support custom configuration");
|
|
const auto compressed=getSchema(cmd,ix.toInt());ConfigParams config;schema(config,compressed);
|
|
const auto before=read(cmd,&config,ix.toInt());
|
|
QJsonObject result{{"target",identity},{"index",ix},{"schema_sha256",sha256(compressed)},
|
|
{"schema",QString::fromLatin1(compressed.toBase64())},{"payload",QString::fromLatin1(before.toBase64())},
|
|
{"configuration",exportConfig(&config,before,"CustomConfiguration")}};
|
|
if(method=="custom_preview"||method=="custom_apply") {
|
|
require(req.value("schema_sha256").toString()==sha256(compressed) && req.value("baseline").toString()==sha256(before),"BMS configuration changed since read");
|
|
QJsonObject source=req.value("source").toObject();
|
|
if(req.value("defaults").toBool()) {ConfigParams defaults;defaults=config;source={{"payload",QString::fromLatin1(read(cmd,&defaults,ix.toInt(),true).toBase64())}};}
|
|
auto candidate=prepareConfig(&config,source,COMM_GET_CUSTOM_CONFIG,"CustomConfiguration");
|
|
result.insert("candidate",candidate);
|
|
if(method=="custom_apply") {
|
|
require(req.value("after_sha256")==candidate.value("after_sha256"),"BMS preview changed");
|
|
requireSameCanIdentity(target,route.identify());
|
|
const auto desired=QByteArray::fromBase64(candidate.value("payload").toString().toLatin1());
|
|
if(desired!=before) {
|
|
loadExactConfig(&config,desired,COMM_GET_CUSTOM_CONFIG);
|
|
wait([&](QObject *owner,std::function<void()> finish){QObject::connect(cmd,&Commands::customConfigAckReceived,owner,[=](int index){if(index==ix.toInt())finish();});},[&]{cmd->customConfigSet(ix.toInt(),&config);},3500);
|
|
require(read(cmd,&config,ix.toInt())==desired,"BMS configuration readback differs");
|
|
}
|
|
result.insert("verified",true);
|
|
}
|
|
}
|
|
requireSameCanIdentity(target,route.identify());return result;
|
|
}
|
|
};
|