podsim

package
v0.0.0-...-147aae7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package podsim simulates a BenchPod for tests and local development: the cloud side of its firmware (stm32h563/src/cloud_client.c), with the pod's hardware and command layer in a benchsim.Sim behind it. It has an Ed25519 identity, logs in to the server's pod WebSocket exactly like the firmware (challenge, signed nonce, host-bound v2), announces its capabilities, runs the JSON commands the server and the web app send through the Sim, and carries tunnels as the Sim's LAN protocol.

What it models on purpose, because the real pod does and the server's bugs live there:

  • ONE cloud command runs at a time; a second gets the error "device busy" at once.
  • A reboot is a new boot id, the LA I/O voltage unset again, target power off, the lease gone and an OTA image staged in PSRAM lost, with the connection down for a while.
  • A reconnect without a reboot (a dropped link) keeps all of that, like the firmware.
  • While a cloud consumer holds the pod's lease (lease.state frames), LAN commands are limited to light reads.

It is dev/test only: nothing in the production image imports it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CmdLogEntry

type CmdLogEntry struct {
	Seq    int            `json:"seq"`
	At     time.Time      `json:"at"`
	Source string         `json:"source"` // cloud | lan
	Cmd    string         `json:"cmd"`
	Args   map[string]any `json:"args,omitempty"`
	Status string         `json:"status"` // ok | error
	Error  string         `json:"error,omitempty"`
	Boot   string         `json:"boot_id"`
}

CmdLogEntry is one command the pod saw, for tests to assert on.

type Config

type Config struct {
	// Server is the server's origin, e.g. http://127.0.0.1:8080 (the API is under /api).
	Server string
	// Name is the pod's name on the owner's account.
	Name string
	// Email and Password sign in the owner, for registering the pod.
	Email, Password string
	// Firmware is the version the pod announces.
	Firmware string
	// Board is the announced board (the firmware's BOARD_NAME, "stm32h563", by default).
	Board string
	// OTAVersion is the version the pod runs after a committed OTA (default Firmware + ".1").
	OTAVersion string
	// V1Auth makes the pod log in with the original nonce-only signature instead of host-bound v2.
	V1Auth bool
	// Logf receives the pod's log lines (default log.Printf).
	Logf func(format string, args ...any)
}

Config is how a simulated pod is set up.

type OTASummary

type OTASummary struct {
	State    string `json:"state"`
	Size     int    `json:"size"`
	Received int    `json:"received"`
	Error    string `json:"error,omitempty"`
}

OTASummary is the OTA session as tests see it.

type Pod

type Pod struct {
	// contains filtered or unexported fields
}

Pod is one simulated BenchPod.

func New

func New(cfg Config) (*Pod, error)

New makes a pod with a fresh identity.

func (*Pod) Close

func (p *Pod) Close()

Close ends the simulated hardware and every connection to it.

func (*Pod) Commands

func (p *Pod) Commands() []CmdLogEntry

Commands returns the commands the pod saw, oldest first.

func (*Pod) ControlHandler

func (p *Pod) ControlHandler() http.Handler

ControlHandler is the simulated pod's side door for tests that run it as its own process: read its state and command log, reboot it, drop its link, slow a command down, and talk to it like a LAN client would.

GET  /healthz
GET  /state                      State
GET  /commands                   the commands it saw, oldest first
POST /reboot?down_ms=500         power-cycle
POST /drop?down_ms=500           the link drops, no reboot
POST /unregister                 delete the pod from the account (disconnects it)
POST /register?name=N            register it again as a new pod (new identity)
POST /latency?cmd=X&ms=300       how long command X takes (0 = no delay)
POST /lan/command                {"cmd":...} as a LAN client: {"status":"ok","data":..} or {"status":"error","message":..}

func (*Pod) DeviceID

func (p *Pod) DeviceID() string

DeviceID is the server-assigned id, empty until Register.

func (*Pod) Drop

func (p *Pod) Drop(down time.Duration)

Drop ends the connection without a reboot (the network went away); the pod reconnects after down with its boot id and state intact.

func (*Pod) PublicKey

func (p *Pod) PublicKey() string

PublicKey is the pod's identity as the server stores it (base64url, no padding).

func (*Pod) Reboot

func (p *Pod) Reboot(down time.Duration)

Reboot power-cycles the pod: the connection drops, the pod comes back after down with a new boot id and everything the firmware loses on a reset lost.

func (*Pod) Register

func (p *Pod) Register(ctx context.Context) error

Register signs the owner in and registers (or re-registers) the pod with its public key.

func (*Pod) Reregister

func (p *Pod) Reregister(ctx context.Context, name string) error

Reregister gives the pod a fresh identity, registers it under name (when not empty) and lets the connect loop log in with it.

func (*Pod) Run

func (p *Pod) Run(ctx context.Context) error

Run connects the pod and keeps it connected (reconnecting after drops and reboots) until ctx ends. Register first.

func (*Pod) RunLAN

func (p *Pod) RunLAN(cmd map[string]any) (data any, errMsg string)

RunLAN runs a command as a LAN client would (the pod's own command port): the lease gate applies, and so does the one-command-at-a-time rule.

func (*Pod) ServeLAN

func (p *Pod) ServeLAN(ln net.Listener) error

ServeLAN answers the pod's LAN API on ln until ln closes (the firmware's :8080).

func (*Pod) SetLatency

func (p *Pod) SetLatency(cmd string, d time.Duration)

SetLatency sets how long the pod takes to run cmd (0 removes the delay).

func (*Pod) Snapshot

func (p *Pod) Snapshot() State

Snapshot reads the pod's state.

func (*Pod) Unregister

func (p *Pod) Unregister(ctx context.Context) error

Unregister deletes the pod from the owner's account (what "Delete" in the web app does) and disconnects it. Reregister brings it back as a new pod with a new identity.

type State

type State struct {
	DeviceID  string     `json:"device_id"`
	BootID    string     `json:"boot_id"`
	BootCount int        `json:"boot_count"`
	Firmware  string     `json:"firmware"`
	Connected bool       `json:"connected"`
	Busy      bool       `json:"busy"`
	LaMV      int        `json:"la_mv"`
	Efuse1    bool       `json:"efuse1"`
	Efuse2    bool       `json:"efuse2"`
	LeaseHeld bool       `json:"lease_held"`
	Holder    string     `json:"lease_holder,omitempty"`
	OTA       OTASummary `json:"ota"`
}

State is a snapshot of the pod for tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL