"""Reviewed upstream pin and adapter contract; updates never imply hardware acceptance.""" import hashlib import json from pathlib import Path import re def load_profile(raw=None): profile = json.loads(raw if raw is not None else Path(__file__).with_name("engine-profile.json").read_bytes()) if (profile.get("schema") != "missioncore.vesc.engine-profile/v1" or profile.get("rpc_version") != 1 or type(profile["rpc_version"]) is not int or profile.get("environment") != "ubuntu-24.04-amd64"): raise ValueError("Unsupported native engine contract") source = profile.get("upstream", {}) if (source.get("repository") != "https://github.com/vedderb/vesc_tool" or not re.fullmatch(r"[0-9]+\.[0-9]{2}", source.get("version", "")) or not re.fullmatch(r"[0-9a-f]{40}", source.get("commit", "")) or not re.fullmatch(r"[0-9a-f]{64}", source.get("source_sha256", ""))): raise ValueError("Upstream source must be pinned by commit and archive hash") profiles = profile.get("firmware_profiles") fields = {"major", "minor", "hardware_type", "test_firmware", "custom_configs"} if not isinstance(profiles, list) or not 1 <= len(profiles) <= 64: raise ValueError("Explicit bounded firmware profiles required") for item in profiles: if (not isinstance(item, dict) or set(item) != fields or any(type(v) is not int or not 0 <= v <= 255 for v in item.values())): raise ValueError("Invalid firmware acceptance profile") methods = profile.get("methods") if (not isinstance(methods, list) or not 1 <= len(methods) <= 128 or len(set(methods)) != len(methods) or any(not isinstance(v, str) or not re.fullmatch(r"[a-z_]{1,48}", v) for v in methods) or not {"engine", "query", "configuration"}.issubset(methods)): raise ValueError("Invalid native operation inventory") if profile.get("firmware_formats", ["bin"]) not in (["bin"], ["bin", "hex"]): raise ValueError("Invalid firmware file formats") if profile.get("firmware_targets",["usb"]) not in (["usb"],["usb","can","swd"]):raise ValueError("Invalid firmware transports") if profile.get("control_transports", ["usb"]) not in (["usb"], ["usb", "can"]): raise ValueError("Invalid control transports") bms = profile.get("bms_profiles", []) if not isinstance(bms,list) or len(bms)>16 or any(not isinstance(v,dict) or set(v)!={"major","minor"} or any(type(n) is not int or not 0<=n<=255 for n in v.values()) for v in bms): raise ValueError("Explicit BMS command profiles required") if bms: source=profile.get("bms_protocol_source",{}) if source.get("repository")!="https://github.com/vedderb/vesc_bms_fw" or not re.fullmatch(r"[0-9a-f]{40}",source.get("commit","")): raise ValueError("Pinned BMS protocol source required") code = profile.get("code_profiles", []) if code not in ([], [{"major":6,"minor":0,"hardware_type":0,"test_firmware":0}]): raise ValueError("Unreviewed code protocol profile") return profile def contract(profile): raw = json.dumps(profile, sort_keys=True, separators=(",", ":")).encode() return {"rpc_version": profile["rpc_version"], "profile_sha256": hashlib.sha256(raw).hexdigest(), "control_transports": profile.get("control_transports", ["usb"]), "code_profiles":profile.get("code_profiles",[]), "firmware_profiles": profile["firmware_profiles"], "methods": profile["methods"], "firmware_targets":profile.get("firmware_targets",["usb"]), "firmware_formats": profile.get("firmware_formats", ["bin"]), "bms_profiles": profile.get("bms_profiles", [])} def header(profile): data = {**profile, "contract": contract(profile)} literal = json.dumps(json.dumps(data, sort_keys=True, separators=(",", ":"))) return ("// Generated from the reviewed engine profile; do not edit.\n#pragma once\n" "#include \n#include \n#include \n" "static QJsonObject engineProfile() { return QJsonDocument::fromJson(QByteArray(" + literal + ")).object(); }\n" "static bool nativeFirmwareQualified(int major, int minor, int type, int test, int custom) {\n" " for (const auto &value : engineProfile().value(\"firmware_profiles\").toArray()) {\n" " auto p = value.toObject(); if (p.value(\"major\").toInt() == major && p.value(\"minor\").toInt() == minor &&\n" " p.value(\"hardware_type\").toInt() == type && p.value(\"test_firmware\").toInt() == test &&\n" " p.value(\"custom_configs\").toInt() == custom) return true;\n" " } return false;\n}\n").encode()