From 1e60e1371be586a7e039af7f715e6c9d5fb8987b Mon Sep 17 00:00:00 2001 From: Edward Viaene Date: Fri, 20 Nov 2026 13:47:39 -0600 Subject: [PATCH] Wiring profiles stored on the pod (wiring_list/get/put/delete/default) Firmware 4.0's features_rev tells the server the pod stores its wiring profiles, so the server saves a device's wiring with wiring_put; the simulator answered BP0002. It now keeps up to 16 profiles in flash (a reboot keeps them), uploads in 600-byte pieces with if_revision, and the store's refusals BP0006..BP0014 as API.md describes. Co-Authored-By: Claude Opus 5.5 --- benchsim/firmware.go | 5 + benchsim/pod.go | 1 + benchsim/wiring_store.go | 222 ++++++++++++++++++++++++++++++++++ benchsim/wiring_store_test.go | 86 +++++++++++++ 4 files changed, 314 insertions(+) create mode 100644 benchsim/wiring_store.go create mode 100644 benchsim/wiring_store_test.go diff --git a/benchsim/firmware.go b/benchsim/firmware.go index 4391bf4..9870f5e 100644 --- a/benchsim/firmware.go +++ b/benchsim/firmware.go @@ -57,6 +57,8 @@ func init() { "sensor_status": {0, 0, simple((*Sim).cmdSensorStatus), nil}, "sensor_regs": {0, 0, simple((*Sim).cmdSensorRegs), nil}, "can_status": {0, flagNoHW, simple((*Sim).cmdCanStatus), nil}, + "wiring_list": {0, flagNoHW, simple((*Sim).cmdWiringList), nil}, + "wiring_get": {0, flagNoHW, simple((*Sim).cmdWiringGet), nil}, "ota_status": {0, 0, nil, nil}, "blob_status": {0, 0, nil, nil}, "dac_loop_probe": {0, flagAnalog, (*Sim).cmdLoopProbe, nil}, @@ -126,6 +128,9 @@ func init() { "speedtest": {1, flagNoHW | flagStream, nil, nil}, "fpga_image": {1, 0, (*Sim).cmdFpgaImage, nil}, "psram_recover": {1, 0, simple((*Sim).cmdPsramRecover), nil}, + "wiring_put": {1, flagNoHW, simple((*Sim).cmdWiringPut), nil}, + "wiring_delete": {1, flagNoHW, simple((*Sim).cmdWiringDelete), nil}, + "wiring_default": {1, flagNoHW, simple((*Sim).cmdWiringDefault), nil}, "cloud_set": {2, flagNoHW, nil, nil}, "cloud_clear": {2, flagNoHW, nil, nil}, "wifi_set": {2, flagNoHW, nil, nil}, diff --git a/benchsim/pod.go b/benchsim/pod.go index 1f51f89..d86607e 100644 --- a/benchsim/pod.go +++ b/benchsim/pod.go @@ -97,6 +97,7 @@ type podFlash struct { dacCal [3]*dacLine adcGain [3]*float64 chip spiFlashChip + wiring wiringStore } func (p *podState) init() { diff --git a/benchsim/wiring_store.go b/benchsim/wiring_store.go new file mode 100644 index 0000000..3f38ce0 --- /dev/null +++ b/benchsim/wiring_store.go @@ -0,0 +1,222 @@ +package benchsim + +import ( + "encoding/base64" + "encoding/json" + "regexp" + "sort" +) + +// The pod's wiring profiles (firmware wiring_store.c; API.md "Wiring profiles"): named JSON +// objects the pod keeps in flash, so they survive a reboot. Firmware 3.10.8 and later stores them, +// and firmware 4.0's features_rev tells the server so: the server then saves a device's wiring on +// the pod with wiring_put instead of in its own database. + +const ( + wiringMaxProfiles = 16 + wiringMaxBytes = 4096 // WIRING_PROFILE_MAX + wiringTotalBytes = 16 * 1024 // the store's space for every profile together + wiringPieceMax = 600 // WIRING_PIECE_MAX, raw bytes per piece +) + +// The wiring store's error codes (errors/catalog.toml). +const ( + codeMissingArgument = 6 + codeWiringNotFound = 7 + codeWiringBadName = 8 + codeWiringRevConflict = 9 + codeWiringStoreFull = 10 + codeWiringTooLarge = 11 + codeWiringNotJSONObject = 12 + codeWiringDeleteDefault = 13 + codeWiringOutOfOrder = 14 +) + +var wiringNameRe = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,39}$`) + +type wiringProfile struct { + rev int + data []byte +} + +// wiringStore lives in podFlash: a reboot keeps it. +type wiringStore struct { + profiles map[string]*wiringProfile + def string + // the one upload in progress for the whole pod + upName string + upTotal int + upBuf []byte +} + +func (w *wiringStore) used(except string) int { + n := 0 + for name, p := range w.profiles { + if name != except { + n += len(p.data) + } + } + return n +} + +func wiringName(r request) (string, error) { + if !r.has("name") { + return "", coded(codeMissingArgument, "arg", "name") + } + name := r.str("name") + if !wiringNameRe.MatchString(name) { + return "", coded(codeWiringBadName, "name", name) + } + return name, nil +} + +func (s *Sim) cmdWiringList(request) (any, error) { + s.mu.Lock() + defer s.mu.Unlock() + w := &s.flash.wiring + names := make([]string, 0, len(w.profiles)) + for name := range w.profiles { + names = append(names, name) + } + sort.Strings(names) + list := make([]map[string]any, 0, len(names)) + for _, name := range names { + p := w.profiles[name] + list = append(list, map[string]any{"name": name, "rev": p.rev, "bytes": len(p.data)}) + } + var def any + if w.def != "" { + def = w.def + } + return map[string]any{"default": def, "profiles": list, "max_profiles": wiringMaxProfiles, + "max_bytes": wiringMaxBytes, "free_bytes": wiringTotalBytes - w.used("")}, nil +} + +func (s *Sim) cmdWiringGet(r request) (any, error) { + name, err := wiringName(r) + if err != nil { + return nil, err + } + s.mu.Lock() + defer s.mu.Unlock() + p, ok := s.flash.wiring.profiles[name] + if !ok { + return nil, coded(codeWiringNotFound, "name", name) + } + off := min(max(r.int("offset", 0), 0), len(p.data)) + end := min(off+wiringPieceMax, len(p.data)) + return map[string]any{"name": name, "rev": p.rev, "total": len(p.data), "offset": off, + "data_b64": base64.RawURLEncoding.EncodeToString(p.data[off:end])}, nil +} + +func (s *Sim) cmdWiringPut(r request) (any, error) { + name, err := wiringName(r) + if err != nil { + return nil, err + } + for _, k := range []string{"offset", "total", "data_b64"} { + if !r.has(k) { + return nil, coded(codeMissingArgument, "arg", k) + } + } + off, total := r.int("offset", 0), r.int("total", 0) + piece, err := base64.RawURLEncoding.DecodeString(r.str("data_b64")) + if err != nil { + return nil, coded(codeWiringNotJSONObject) + } + if total > wiringMaxBytes { + return nil, coded(codeWiringTooLarge, "bytes", total, "max", wiringMaxBytes) + } + s.mu.Lock() + defer s.mu.Unlock() + w := &s.flash.wiring + if w.profiles == nil { + w.profiles = map[string]*wiringProfile{} + } + if off == 0 { + cur, exists := w.profiles[name] + if r.has("if_revision") { + want := r.int("if_revision", 0) + have := 0 + if exists { + have = cur.rev + } + if want != have { + return nil, coded(codeWiringRevConflict, "name", name, "rev", have) + } + } + if !exists && len(w.profiles) >= wiringMaxProfiles { + return nil, coded(codeWiringStoreFull, "max", wiringMaxProfiles) + } + w.upName, w.upTotal, w.upBuf = name, total, nil + } else if w.upName != name || w.upTotal != total || off != len(w.upBuf) { + return nil, coded(codeWiringOutOfOrder, "expected", len(w.upBuf)) + } + w.upBuf = append(w.upBuf, piece...) + if len(w.upBuf) > total { + w.upName, w.upBuf = "", nil + return nil, coded(codeWiringOutOfOrder, "expected", total) + } + if len(w.upBuf) < total { + return map[string]any{"name": name, "received": len(w.upBuf), "total": total}, nil + } + data := w.upBuf + w.upName, w.upBuf = "", nil + var obj map[string]any + if json.Unmarshal(data, &obj) != nil || obj == nil { + return nil, coded(codeWiringNotJSONObject) + } + if w.used(name)+len(data) > wiringTotalBytes { + return nil, coded(codeWiringStoreFull, "max", wiringMaxProfiles) + } + p, ok := w.profiles[name] + if !ok { + p = &wiringProfile{} + w.profiles[name] = p + } + p.rev++ + p.data = data + if w.def == "" { + w.def = name + } + return map[string]any{"name": name, "rev": p.rev, "default": w.def == name}, nil +} + +func (s *Sim) cmdWiringDelete(r request) (any, error) { + name, err := wiringName(r) + if err != nil { + return nil, err + } + s.mu.Lock() + defer s.mu.Unlock() + w := &s.flash.wiring + if _, ok := w.profiles[name]; !ok { + return nil, coded(codeWiringNotFound, "name", name) + } + if w.def == name { + return nil, coded(codeWiringDeleteDefault, "name", name) + } + delete(w.profiles, name) + return map[string]any{"name": name, "deleted": true}, nil +} + +func (s *Sim) cmdWiringDefault(r request) (any, error) { + s.mu.Lock() + defer s.mu.Unlock() + w := &s.flash.wiring + if r.has("name") { + name := r.str("name") + if !wiringNameRe.MatchString(name) { + return nil, coded(codeWiringBadName, "name", name) + } + if _, ok := w.profiles[name]; !ok { + return nil, coded(codeWiringNotFound, "name", name) + } + w.def = name + } + var def any + if w.def != "" { + def = w.def + } + return map[string]any{"default": def}, nil +} diff --git a/benchsim/wiring_store_test.go b/benchsim/wiring_store_test.go new file mode 100644 index 0000000..3593a60 --- /dev/null +++ b/benchsim/wiring_store_test.go @@ -0,0 +1,86 @@ +package benchsim + +import ( + "encoding/base64" + "encoding/json" + "strings" + "testing" +) + +// The wiring store: a profile saved in pieces reads back the same, the first one becomes the +// default, a stale if_revision is refused, and a reboot keeps everything. +func TestWiringStoreRoundTrip(t *testing.T) { + s := New(WithFirmware("4.0.0", Hooks{})) + defer s.Close() + exec := func(cmd map[string]any) Reply { + t.Helper() + cmd["client_api"] = 2 + return s.ExecReply(cmd, true) + } + data := func(r Reply) map[string]any { + t.Helper() + if r.Error != "" { + t.Fatalf("refused: %+v", r) + } + var m map[string]any + if err := json.Unmarshal(r.Data, &m); err != nil { + t.Fatal(err) + } + return m + } + if d := data(exec(map[string]any{"cmd": "wiring_list"})); d["default"] != nil || len(d["profiles"].([]any)) != 0 { + t.Fatalf("empty store: %v", d) + } + profile := []byte(`{"version":2,"la_mv":3300,"signals":{"x":"` + strings.Repeat("a", 900) + `"}}`) + put := func(name string, ifRev any) Reply { + var last Reply + for off := 0; off < len(profile); off += wiringPieceMax { + end := min(off+wiringPieceMax, len(profile)) + cmd := map[string]any{"cmd": "wiring_put", "name": name, "offset": float64(off), "total": float64(len(profile)), + "data_b64": base64.RawURLEncoding.EncodeToString(profile[off:end])} + if off == 0 && ifRev != nil { + cmd["if_revision"] = ifRev + } + if last = exec(cmd); last.Error != "" { + return last + } + } + return last + } + if d := data(put("k66", 0.0)); d["rev"] != 1.0 || d["default"] != true { + t.Fatalf("first save: %v", d) + } + if r := put("k66", 0.0); r.Code != "BP0009" { + t.Fatalf("stale if_revision: %+v", r) + } + if d := data(put("k66", 1.0)); d["rev"] != 2.0 { + t.Fatalf("second save: %v", d) + } + data(put("nrf", nil)) + s.Reboot() + var got []byte + for { + d := data(exec(map[string]any{"cmd": "wiring_get", "name": "k66", "offset": float64(len(got))})) + b, _ := base64.RawURLEncoding.DecodeString(d["data_b64"].(string)) + got = append(got, b...) + if len(b) == 0 || len(got) >= int(d["total"].(float64)) { + break + } + } + if string(got) != string(profile) { + t.Fatalf("read back %q", got) + } + if r := exec(map[string]any{"cmd": "wiring_delete", "name": "k66"}); r.Code != "BP0013" { + t.Fatalf("delete the default: %+v", r) + } + if d := data(exec(map[string]any{"cmd": "wiring_default", "name": "nrf"})); d["default"] != "nrf" { + t.Fatalf("set default: %v", d) + } + data(exec(map[string]any{"cmd": "wiring_delete", "name": "k66"})) + if r := exec(map[string]any{"cmd": "wiring_get", "name": "k66", "offset": 0.0}); r.Code != "BP0007" { + t.Fatalf("deleted profile: %+v", r) + } + if r := exec(map[string]any{"cmd": "wiring_get", "name": "-bad", "offset": 0.0}); r.Code != "BP0008" { + t.Fatalf("bad name: %+v", r) + } +}