Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
103 lines
8.0 KiB
C++
103 lines
8.0 KiB
C++
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#pragma once
|
|
#include "code_native.h"
|
|
#include <QJSEngine>
|
|
#include <condition_variable>
|
|
#include <mutex>
|
|
#include <thread>
|
|
#include <zlib.h>
|
|
|
|
class NativePackage {
|
|
public:
|
|
static QByteArray unpackBounded(const QByteArray &data) {
|
|
require(data.size()>=5&&data.size()<=1024*1024,"Package size exceeds bound");
|
|
auto size=qFromBigEndian<quint32>(reinterpret_cast<const uchar*>(data.constData()));
|
|
require(size>0&&size<=4*1024*1024,"Expanded package exceeds bound");
|
|
QByteArray raw(int(size),'\0');z_stream z{};require(inflateInit(&z)==Z_OK,"Cannot inspect package compression");
|
|
z.next_in=reinterpret_cast<Bytef*>(const_cast<char*>(data.constData()+4));z.avail_in=data.size()-4;
|
|
z.next_out=reinterpret_cast<Bytef*>(raw.data());z.avail_out=size;const auto code=inflate(&z,Z_FINISH);
|
|
const bool valid=code==Z_STREAM_END&&z.total_out==size&&z.avail_in==0;inflateEnd(&z);require(valid,"Invalid package compression");
|
|
require(raw.startsWith(QByteArray("VESC Packet\0",12)),"Invalid VESC package magic");
|
|
int at=12;QSet<QByteArray> fields;
|
|
while(at<raw.size()) {
|
|
int end=raw.indexOf('\0',at);require(end>at&&end-at<=64&&end+5<=raw.size(),"Invalid package field");
|
|
const auto name=raw.mid(at,end-at);const auto length=qFromBigEndian<qint32>(reinterpret_cast<const uchar*>(raw.constData()+end+1));at=end+5;
|
|
require(!fields.contains(name)&&fields.size()<32&&length>=0&&length<=raw.size()-at,"Duplicate/truncated package field");fields.insert(name);
|
|
if(name=="qmlIsFullscreen")require(length==1&&(raw[at]==0||raw[at]==1),"Invalid QML flag");
|
|
at+=length;
|
|
}
|
|
return raw;
|
|
}
|
|
// Evaluate only the descriptor's pure compatibility function. A bare JS
|
|
// engine has no Qt/QObject, USB, filesystem, network or application bridge.
|
|
// QML object creation and Component.onCompleted are never evaluated.
|
|
static QJsonValue compatible(const QString &descriptor,const QJsonObject &identity) {
|
|
if(descriptor.isEmpty())return QJsonValue(QJsonValue::Null);
|
|
if(descriptor.size()>16384)return QJsonValue(QJsonValue::Null);
|
|
auto match=QRegularExpression("function\\s+isCompatible\\s*\\(\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*\\)\\s*\\{").match(descriptor);
|
|
if(!match.hasMatch())return QJsonValue(QJsonValue::Null);
|
|
int end=match.capturedEnd(),depth=1;QChar quote;bool line=false,block=false,escape=false;
|
|
for(;end<descriptor.size()&&depth;end++) {
|
|
auto c=descriptor[end],next=end+1<descriptor.size()?descriptor[end+1]:QChar();
|
|
if(line){if(c=='\n')line=false;continue;}if(block){if(c=='*'&&next=='/'){block=false;end++;}continue;}
|
|
if(!quote.isNull()){if(escape){escape=false;continue;}if(c=='\\'){escape=true;continue;}if(c==quote)quote=QChar();continue;}
|
|
if(c=='`')return QJsonValue(QJsonValue::Null);
|
|
if(c=='/'&&next=='/'){line=true;end++;continue;}if(c=='/'&&next=='*'){block=true;end++;continue;}
|
|
if(c=='\''||c=='"'){quote=c;continue;}if(c=='{')depth++;if(c=='}')depth--;
|
|
}
|
|
if(depth)return QJsonValue(QJsonValue::Null);
|
|
QJSEngine engine;auto fw=engine.newObject();
|
|
for(auto it=identity.begin();it!=identity.end();++it)fw.setProperty(it.key(),engine.toScriptValue(it.value().toVariant()));
|
|
fw.setProperty("hw",identity.value("hardware").toString());fw.setProperty("fwName",identity.value("firmware_name").toString());
|
|
const int type=identity.value("hardware_type").toInt(-1);
|
|
const auto typeName=type==0?"VESC":type==1?"VESC BMS":type==2?"Custom Module":"Unknown";
|
|
auto name=engine.newObject();name.setProperty("name",typeName);engine.globalObject().setProperty("_hwType",name);
|
|
fw.setProperty("hwTypeStr",engine.evaluate("(function(){return _hwType.name;})"));
|
|
std::mutex mutex;std::condition_variable condition;bool done=false;
|
|
std::thread timer([&]{std::unique_lock<std::mutex> lock(mutex);if(!condition.wait_for(lock,std::chrono::milliseconds(250),[&]{return done;}))engine.setInterrupted(true);});
|
|
auto fn=engine.evaluate("("+descriptor.mid(match.capturedStart(),end-match.capturedStart())+")");
|
|
auto result=fn.isCallable()?fn.call({fw}):QJSValue();
|
|
{std::lock_guard<std::mutex> lock(mutex);done=true;}condition.notify_one();timer.join();
|
|
if(engine.isInterrupted()||!result.isBool())return QJsonValue(QJsonValue::Null);
|
|
return result.toBool();
|
|
}
|
|
static QByteArray image(const QByteArray &content) {
|
|
if(content.isEmpty())return {};
|
|
require(content.size()>=2,"Code flags missing");VByteArray out;out.vbAppendUint32(content.size()-2);
|
|
out.vbAppendUint16(Packet::crc16(reinterpret_cast<const unsigned char*>(content.constData()),content.size()));out.append(content);return out;
|
|
}
|
|
static QJsonObject library(const QByteArray &content) {
|
|
require(content.size()>=5&&content.size()<=512*1024&&content.startsWith(QByteArray(2,'\0')), "Invalid library Lisp content");
|
|
const auto data=content.mid(2);const int end=data.indexOf('\0');require(end>0&&end+3<=data.size(),"Invalid library source");
|
|
VByteArray table(data.mid(end+1));const auto count=table.vbPopFrontInt16();require(count>=0&&count<500,"Invalid library import count");
|
|
int tableEnd=end+3;QList<QPair<int,int>> ranges;QSet<QString> names;
|
|
for(int i=0;i<count;i++) {
|
|
const int n=table.indexOf('\0');require(n>0&&n<=64&&n+9<=table.size(),"Invalid library tag");
|
|
const auto tag=table.vbPopFrontString();int offset=table.vbPopFrontInt32(),length=table.vbPopFrontInt32();
|
|
require(!names.contains(tag)&&offset>=0&&length>0&&offset<=data.size()-length&&data[offset+length-1]=='\0',"Invalid library range");
|
|
names.insert(tag);ranges.append(qMakePair(offset,length));tableEnd+=n+9;
|
|
}
|
|
int previous=tableEnd;for(auto range:ranges){require(range.first>=previous,"Overlapping library range");previous=range.first+range.second;}
|
|
CodeLoader loader;const auto parts=loader.lispUnpackImports(content);QJsonObject imports;
|
|
for(auto part:parts.second)imports.insert(part.first,QString::fromLatin1(part.second.toBase64()));
|
|
return {{"source",parts.first},{"imports",imports}};
|
|
}
|
|
static QJsonObject inspect(const QJsonObject &request) {
|
|
const auto encoded=request.value("data").toString().toLatin1();const auto data=QByteArray::fromBase64(encoded,QByteArray::AbortOnBase64DecodingErrors);
|
|
require(data.toBase64()==encoded,"Invalid package encoding");unpackBounded(data);
|
|
CodeLoader loader;const auto pkg=loader.unpackVescPackage(data);require(pkg.loadOk&&!pkg.name.isEmpty()&&pkg.name.size()<=256,"Package metadata missing");
|
|
auto lisp=image(pkg.lispData);VByteArray qml;
|
|
if(!pkg.qmlFile.isEmpty()){qml.vbAppendUint16(pkg.qmlIsFullscreen?2:1);qml.append(loader.qmlCompress(pkg.qmlFile));}
|
|
const auto qmlImage=image(qml);const bool fits=lisp.size()<=120*1024&&qmlImage.size()<=120*1024&&pkg.qmlFile.toUtf8().size()<=500*1024;
|
|
if(!lisp.isEmpty())require(pkg.lispData.startsWith(QByteArray(2,'\0')),"Unknown Lisp flags");
|
|
// Dependencies are already embedded by the upstream package builder.
|
|
// Original bytes are retained, including native libraries and alignment.
|
|
QJsonObject result{{"name",pkg.name},{"sha256",sha256(data)},{"description",pkg.description_md.left(65536)},
|
|
{"compatible",compatible(pkg.pkgDescQml,request.value("identity").toObject())},{"fits",fits},
|
|
{"lisp_bytes",lisp.size()},{"qml_bytes",qmlImage.size()},{"qml_source",pkg.qmlFile.toUtf8().size()<=120*1024?QJsonValue(pkg.qmlFile):QJsonValue()},
|
|
{"images",QJsonObject{{"lisp",fits?QString::fromLatin1(lisp.toBase64()):QString()},{"qml",fits?QString::fromLatin1(qmlImage.toBase64()):QString()}}}};
|
|
if(request.value("export_library").toBool())result.insert("library",library(pkg.lispData));
|
|
return result;
|
|
}
|
|
};
|