Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions benchsim/firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down
1 change: 1 addition & 0 deletions benchsim/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type podFlash struct {
dacCal [3]*dacLine
adcGain [3]*float64
chip spiFlashChip
wiring wiringStore
}

func (p *podState) init() {
Expand Down
222 changes: 222 additions & 0 deletions benchsim/wiring_store.go
Original file line number Diff line number Diff line change
@@ -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
}
86 changes: 86 additions & 0 deletions benchsim/wiring_store_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}