feat(bms): add read-only diagnostics and guarded operating switch commands

This commit is contained in:
DCCONSTRUCTIONS
2026-11-04 17:14:54 +03:00
parent ab8e977f11
commit 42213001ba
19 changed files with 919 additions and 40 deletions
@@ -21,7 +21,12 @@ func TestBMSProjectionRequiresVersionAndStableIdentity(t *testing.T) {
if bmsProjection(item, model) {
t.Fatal("wrong physical identity accepted")
}
if len(model.Actions) != 0 || model.PrepareUnit != "" || model.Vendor != "" {
t.Fatal("BMS must remain automatic and read-only")
if len(model.Actions) != 3 || model.PrepareUnit != "" || model.Vendor != "" {
t.Fatal("BMS must remain automatic with only three admitted operations")
}
for _, action := range []string{"bms.read", "bms.control.verify", "bms.control.set"} {
if !model.Actions[action] {
t.Fatalf("missing admitted action %s", action)
}
}
}
@@ -39,7 +39,7 @@ var sensorModels = []sensorModel{
Actions: actions("prepare", "details", "rename", "verify", "start", "replay", "stop", "option", "offer", "close-peer")},
{ID: "xgrids.k1", Name: "XGRIDS K1", Prefix: "k1", Kind: "k1",
ActionTimeouts: map[string]time.Duration{"management": 35 * time.Second},
Actions: actions("details", "rename", "verify", "start", "stop", "option", "offer", "close-peer", "management")},
Actions: actions("details", "rename", "verify", "start", "stop", "option", "offer", "close-peer", "management")},
{ID: "insta360.x4", Name: "Insta360 X4", Prefix: "instax4", Kind: "insta360.x4", Plugin: "missioncore.insta360", Version: "0.1.3",
// 2e1a:0002 is shared with other Insta360 models. Require the exact
// OS product descriptor as well; SDK identity is verified after prepare.
@@ -56,8 +56,8 @@ var sensorModels = []sensorModel{
Vendor: "04d8", Product: "0053", USBName: "USB CANFD DEBUG", Socket: "/run/mission-core-usb-can/driver.sock",
PrepareUnit: "mission-core-node-usb-can-prepare.service", Report: "/var/lib/mission-core-node-profiles/usb-can/preparation.json",
ProtocolIdentity: true, Actions: actions("prepare", "details", "rename", "verify", "can.status", "can.configure", "can.start", "can.stop", "can.clear", "can.transmit.preview", "can.transmit.apply", "can.transmit.stop")},
{ID: "jk.bms", Name: "JK BMS", Prefix: "jkbms", Kind: "jk.bms", Plugin: "missioncore.jk-bms", Version: "0.1.0",
Socket: "/run/mission-core-jk-bms/driver.sock", Actions: actions()},
{ID: "jk.bms", Name: "JK BMS", Prefix: "jkbms", Kind: "jk.bms", Plugin: "missioncore.jk-bms", Version: "0.2.0",
Socket: "/run/mission-core-jk-bms/driver.sock", Actions: actions("bms.read", "bms.control.verify", "bms.control.set")},
}
func modelForDevice(id string) *sensorModel {
@@ -2,13 +2,13 @@ package node
import (
"encoding/json"
"strings"
"strings"
"time"
)
// Configuration browsing is a bounded snapshot, not a durable backup or write receipt.
func sensorSnapshot(action string) bool {
return strings.HasPrefix(action, "can.") || action == "vesc.qml.open" || action == "vesc.qml.frame" || action == "vesc.qml.close" || action == "vesc.samples.start" || action == "vesc.samples.status" || action == "vesc.samples.fetch" || action == "vesc.samples.stop" || action == "vesc.samples.read" || action == "vesc.qml.begin" || action == "vesc.qml.chunk" || action == "vesc.qml.render" || action == "vesc.packages.catalog" || action == "vesc.packages.inspect" || action == "vesc.packages.download" || action == "vesc.packages.begin" || action == "vesc.packages.chunk" || action == "vesc.packages.load" || action == "vesc.packages.forget" || action == "vesc.packages.status" || action == "vesc.packages.prepare" || action == "vesc.packages.remove_prepare" || action == "vesc.packages.restore" || action == "vesc.packages.reconcile" || action == "vesc.packages.refresh" || action == "vesc.bms.configuration.read" || action == "vesc.bms.configuration.preview" || action == "vesc.bms.configuration.reconcile" || action == "vesc.bms.configuration.restore" || action == "vesc.code.read" || action == "vesc.code.begin" || action == "vesc.code.chunk" || action == "vesc.code.prepare" || action == "vesc.code.erase_prepare" || action == "vesc.code.reconcile" || action == "vesc.code.control_prepare" || action == "vesc.code.stats" || action == "vesc.code.restore" || action == "vesc.firmware.backup" || action == "vesc.firmware.target" || action == "vesc.bms.read" || action == "vesc.bms.prepare" || action == "vesc.firmware.catalog" || action == "vesc.tools.read" || action == "vesc.terminal.read" || action == "vesc.firmware.status" || action == "vesc.configuration.read" || action == "vesc.configuration.preview" || action == "vesc.diagnostics.read"
return strings.HasPrefix(action, "bms.") || strings.HasPrefix(action, "can.") || action == "vesc.qml.open" || action == "vesc.qml.frame" || action == "vesc.qml.close" || action == "vesc.samples.start" || action == "vesc.samples.status" || action == "vesc.samples.fetch" || action == "vesc.samples.stop" || action == "vesc.samples.read" || action == "vesc.qml.begin" || action == "vesc.qml.chunk" || action == "vesc.qml.render" || action == "vesc.packages.catalog" || action == "vesc.packages.inspect" || action == "vesc.packages.download" || action == "vesc.packages.begin" || action == "vesc.packages.chunk" || action == "vesc.packages.load" || action == "vesc.packages.forget" || action == "vesc.packages.status" || action == "vesc.packages.prepare" || action == "vesc.packages.remove_prepare" || action == "vesc.packages.restore" || action == "vesc.packages.reconcile" || action == "vesc.packages.refresh" || action == "vesc.bms.configuration.read" || action == "vesc.bms.configuration.preview" || action == "vesc.bms.configuration.reconcile" || action == "vesc.bms.configuration.restore" || action == "vesc.code.read" || action == "vesc.code.begin" || action == "vesc.code.chunk" || action == "vesc.code.prepare" || action == "vesc.code.erase_prepare" || action == "vesc.code.reconcile" || action == "vesc.code.control_prepare" || action == "vesc.code.stats" || action == "vesc.code.restore" || action == "vesc.firmware.backup" || action == "vesc.firmware.target" || action == "vesc.bms.read" || action == "vesc.bms.prepare" || action == "vesc.firmware.catalog" || action == "vesc.tools.read" || action == "vesc.terminal.read" || action == "vesc.firmware.status" || action == "vesc.configuration.read" || action == "vesc.configuration.preview" || action == "vesc.diagnostics.read"
}
func expiredSensorSnapshot(value SensorOperation) SensorOperation {
@@ -22,9 +22,11 @@ func expiredSensorSnapshot(value SensorOperation) SensorOperation {
// unacknowledged remote result to admit a new request. Tombstones retain
// operation identity, so retrying an old ID cannot execute hardware work again.
func (s *Sensors) writeOperation(value *SensorOperation) error {
// CAN requests/results are bounded RAM. On restart every USB generation is
// replaced; queued old-session commands cannot execute or replay TX.
if strings.HasPrefix(value.Command.Action, "can.") && value.Result == nil { return nil }
// CAN requests/results are bounded RAM. On restart every USB generation is
// replaced; queued old-session commands cannot execute or replay TX.
if (strings.HasPrefix(value.Command.Action, "can.") || strings.HasPrefix(value.Command.Action, "bms.")) && value.Result == nil {
return nil
}
if !sensorSnapshot(value.Command.Action) || value.Result == nil {
return s.write(value.Command.ID+".json", value)
@@ -61,7 +63,9 @@ func (s *Sensors) writeOperation(value *SensorOperation) error {
*entry = expiredSensorSnapshot(*entry)
}
}
if strings.HasPrefix(value.Command.Action, "can.") { return nil }
persisted := expiredSensorSnapshot(*value)
if strings.HasPrefix(value.Command.Action, "can.") || strings.HasPrefix(value.Command.Action, "bms.") {
return nil
}
persisted := expiredSensorSnapshot(*value)
return s.write(value.Command.ID+".json", &persisted)
}
@@ -59,3 +59,23 @@ func TestConfigurationBackupsRemainDurable(t *testing.T) {
t.Fatal("backup receipt was discarded")
}
}
func TestBMSCommandsAndReadbacksNeverPersistOrReplayFromDisk(t *testing.T) {
s := &Sensors{root: t.TempDir(), operations: map[string]*SensorOperation{}}
for _, action := range []string{"bms.read", "bms.control.verify", "bms.control.set"} {
op := &SensorOperation{Command: SensorCommand{ID: action, Action: action}, State: "running", Updated: time.Now().Unix()}
s.operations[action] = op
if err := s.writeOperation(op); err != nil {
t.Fatal(err)
}
op.State = "complete"
op.Result = map[string]any{"settings": map[string]any{"value": 42}}
if err := s.writeOperation(op); err != nil {
t.Fatal(err)
}
}
files, err := os.ReadDir(s.root)
if err != nil || len(files) != 0 {
t.Fatal("BMS commands or measurements escaped RAM", err)
}
}
+1 -1
View File
@@ -12,7 +12,7 @@ import sys
ROOT = Path(__file__).resolve().parents[1]
BINARY_VERSION = "0.8.53"
VERSION = "0.8.53-112"
VERSION = "0.8.53-113"
sys.path.insert(0, str(ROOT.parents[1] / "scripts/packaging"))
from debian import package