diff --git a/docs/scripting-api.md b/docs/scripting-api.md index df0e4f90..67a5dd77 100644 --- a/docs/scripting-api.md +++ b/docs/scripting-api.md @@ -39,7 +39,7 @@ and a verb added tomorrow is usable today. ### Why two layers rather than one -**260** addressable methods hand-written across two +**261** addressable methods hand-written across two languages is twice that many things to keep in step. Keeping them in step is what the verb manifest (`docs/verbs.json`) is for: a hand-written surface that names a verb the tree no longer has is exactly the drift it catches, where once a tool could call a deleted verb and nothing diff --git a/docs/scripting-verbs.md b/docs/scripting-verbs.md index 29e6554d..c0d5d151 100644 --- a/docs/scripting-verbs.md +++ b/docs/scripting-verbs.md @@ -1,8 +1,8 @@ # Every verb, and the call that covers it The wire underneath [scripting-api.md](scripting-api.md). The store registers -**222** public verbs a script can call and -**38** internal callbacks it cannot, **260** +**223** public verbs a script can call and +**38** internal callbacks it cannot, **261** in total, plus the two the socket answers itself, with what each reads, what each returns, and which façade call reaches it. @@ -77,8 +77,8 @@ Two verbs are **not** in this table: ## What this table shows about the surface -- **222** public verbs, plus **38** - the workbench keeps to itself, **260** registered in +- **223** public verbs, plus **38** + the workbench keeps to itself, **261** registered in total. The façade covers the public verbs, over roughly 60 calls once objects and properties absorb them. - **The naming is not regular.** `node.*` and `nodes.*` are both node verbs and @@ -258,6 +258,8 @@ Two verbs are **not** in this table: | `bench.refresh` | — | — | `wb.endpoints` | | `bench.serve` | `node` string, `kind` string | `node`, `addr` | `node.serve(kind='tcp'\|'serial')` | | `bench.stray` | — | `at` | `wb.endpoints.stray()` - *planned*, no client defines `wb.endpoints` yet; call the verb | +| `radio.deliver` | `node` string, `hex` string, `as` string | `node`, `as`, `bytes`, `delivered` | `node.deliver(frame_hex, as_='ok')` | +| `radio.preset` | *a bare string*, `preset` string, `node` string | `preset`, `nodes` | `wb.radio.presets / wb.radio.apply(preset, node=)` - *planned*, no client defines `wb.radio` yet; call the verb | | `sdr.serve` | *a bare string*, `node` string | `node`, `addr`, `rate_hz` | `node.serve_sdr()` | | `sdr.stop` | *a bare string*, `node` string | `stopped` | `node.unserve_sdr()` | @@ -367,7 +369,6 @@ Two verbs are **not** in this table: | `environ.fetch` | *a bare string*, `source` string | `source`, `started` | `wb.rf.fetch_environment(source)` - *planned*, no client defines `wb.rf` yet; call the verb | | `environ.fetched` | *a bare string*, `note` string | — | *none* — the building fetch reporting success | | `environ.list` | — | `dirs`, `current` | `wb.rf.environments` - *planned*, no client defines `wb.rf` yet; call the verb | -| `radio.preset` | *a bare string*, `preset` string, `node` string | `preset`, `nodes` | `wb.radio.presets / wb.radio.apply(preset, node=)` - *planned*, no client defines `wb.radio` yet; call the verb | | `rf.environment` | *a bare string*, `dir` string, `on` bool | `environment` | `wb.rf.environment = dir` - *planned*, no client defines `wb.rf` yet; call the verb | | `rf.excess_loss` | *a bare string*, `db` number | `db`, `links` | `wb.rf.excess_loss_db = n` - *planned*, no client defines `wb.rf` yet; call the verb | | `rf.mode` | *a bare string*, `mode` string | `mode` | `wb.rf.mode = 'calculated'\|'waveform'` - *planned*, no client defines `wb.rf` yet; call the verb | diff --git a/docs/verb-reference.md b/docs/verb-reference.md index 36e84e35..1f064299 100644 --- a/docs/verb-reference.md +++ b/docs/verb-reference.md @@ -3,9 +3,9 @@ Generated. Run `tools/verbdoc/verbdoc.py` to rewrite it and `tools/verbdoc/verbdoc.py --check` to fail when it is stale. -The store registers 260 verbs: 222 a script may call and +The store registers 261 verbs: 223 a script may call and 38 the workbench calls on itself, which the socket refuses. Of those, -260 say what they are for and 0 do not yet; the ones that +261 say what they are for and 0 do not yet; the ones that do not are marked, and what is printed for them is read out of the handler rather than said by it. @@ -2522,6 +2522,55 @@ Not made by the test suite: this call needs more than the two-node headless sess Planned, not written: no client defines `wb.endpoints` yet - the TCP and serial endpoints a node is served on. Call the verb itself in the meantime. +### `radio.deliver` + +Hand a node's radio one frame directly, as received or as a chosen kind of failed reception, to test the firmware's receive path without the channel deciding anything. + +**Takes** + +| parameter | type | | what | +|---|---|---|---| +| `node` | string | optional | the node whose radio receives it; it needs firmware running | +| `hex` | string | optional | the frame, header first, as hex digits | +| `as` | string | optional | ok (the default), crc_error, header_error, no_crc (the header said the payload has no CRC), next_before_read or next_during_read (the next frame's header arriving in continuous receive before or during the firmware's read of this one) | + +**Answers** `node`, `as`, `bytes`, `delivered`. `delivered` is false when the node has no firmware or its chip does not read that kind, which is an answer rather than a refusal. The next_* kinds need a frame already received and not yet read, so they follow an ok delivery. + +**Example** - a frame whose header said it carries no CRC + +```json +{"id":1,"method":"radio.deliver","params":{"as":"no_crc","hex":"1500348b2db7","node":"West Lomond"}} +``` + +Not made by the test suite: this call needs more than the two-node headless session the runnable examples go to. + +**Client** `node.deliver(frame_hex, as_='ok')` + +### `radio.preset` + +Put the nodes on one of the community's agreed modem settings, which decides sensitivity and airtime and therefore every number downstream of them, or list the settings there are to choose from. + +**Takes** + +| parameter | type | | what | +|---|---|---|---| +| `preset` | string | optional, primary | the preset's label, exactly as it is listed; a label no preset has is refused, and absent lists the labels instead of changing anything | +| `node` | string | optional | one node to move, named rather than passed bare; absent moves every node in the network | + +**Answers** `preset`, `nodes`. `nodes` is how many were moved, and zero is the answer where `node` named nobody. Called with no preset at all it answers instead with `presets`, the labels available. The frequency is part of a preset, so a change rebuilds the engine and remeasures every link on a worker. + +**Example** - put the whole network on the settings its neighbours use + +```json +{"id":1,"method":"radio.preset","params":"EU/UK (Narrow)"} +``` + +Not made by the test suite: this call needs more than the two-node headless session the runnable examples go to. + +**Client** `wb.radio.presets / wb.radio.apply(preset, node=)` + +Planned, not written: no client defines `wb.radio` yet - the preset list and applying one. Call the verb itself in the meantime. + ### `sdr.serve` Offer what one node's antenna hears as an rtl_tcp source, so real SDR software can be pointed at the simulated spectrum rather than at a drawing of it. @@ -3955,31 +4004,6 @@ Name every building pull already on this disk, so moving between environments is Planned, not written: no client defines `wb.rf` yet - the radio model: which physics, what realism, how much excess loss. Call the verb itself in the meantime. -### `radio.preset` - -Put the nodes on one of the community's agreed modem settings, which decides sensitivity and airtime and therefore every number downstream of them, or list the settings there are to choose from. - -**Takes** - -| parameter | type | | what | -|---|---|---|---| -| `preset` | string | optional, primary | the preset's label, exactly as it is listed; a label no preset has is refused, and absent lists the labels instead of changing anything | -| `node` | string | optional | one node to move, named rather than passed bare; absent moves every node in the network | - -**Answers** `preset`, `nodes`. `nodes` is how many were moved, and zero is the answer where `node` named nobody. Called with no preset at all it answers instead with `presets`, the labels available. The frequency is part of a preset, so a change rebuilds the engine and remeasures every link on a worker. - -**Example** - put the whole network on the settings its neighbours use - -```json -{"id":1,"method":"radio.preset","params":"EU/UK (Narrow)"} -``` - -Not made by the test suite: this call needs more than the two-node headless session the runnable examples go to. - -**Client** `wb.radio.presets / wb.radio.apply(preset, node=)` - -Planned, not written: no client defines `wb.radio` yet - the preset list and applying one. Call the verb itself in the meantime. - ### `rf.environment` Point the session at a directory of environment tiles so both RF modes price buildings into the path budget, or take it away again and go back to bare earth; asked with nothing it reports the tiles in force instead of setting any. diff --git a/docs/verbs.json b/docs/verbs.json index bc16e7ab..3177a9ea 100644 --- a/docs/verbs.json +++ b/docs/verbs.json @@ -4242,6 +4242,41 @@ "runnable": true } }, + "radio.deliver": { + "what": "hand a node's radio one frame directly, as received or as a chosen kind of failed reception, to test the firmware's receive path without the channel deciding anything", + "params": [ + { + "name": "node", + "type": "string", + "what": "the node whose radio receives it; it needs firmware running" + }, + { + "name": "hex", + "type": "string", + "what": "the frame, header first, as hex digits" + }, + { + "name": "as", + "type": "string", + "what": "ok (the default), crc_error, header_error, no_crc (the header said the payload has no CRC), next_before_read or next_during_read (the next frame's header arriving in continuous receive before or during the firmware's read of this one)" + } + ], + "returns": [ + "node", + "as", + "bytes", + "delivered" + ], + "answers": "`delivered` is false when the node has no firmware or its chip does not read that kind, which is an answer rather than a refusal. The next_* kinds need a frame already received and not yet read, so they follow an ok delivery.", + "example": { + "params": { + "as": "no_crc", + "hex": "1500348b2db7", + "node": "West Lomond" + }, + "what": "a frame whose header said it carries no CRC" + } + }, "radio.preset": { "what": "put the nodes on one of the community's agreed modem settings, which decides sensitivity and airtime and therefore every number downstream of them, or list the settings there are to choose from", "params": [ diff --git a/internal/app/session/verbs.go b/internal/app/session/verbs.go index 34c262b5..8cb49394 100644 --- a/internal/app/session/verbs.go +++ b/internal/app/session/verbs.go @@ -324,6 +324,7 @@ func Register(st *state.Store, s *Sim) { registerBudgetVerbs(st, s) registerBenchVerbs(st, s) + registerRadioVerbs(st, s) registerRunAndPlanVerbs(st, s) registerImportFeedVerbs(st, s) registerNodeFirmwareVerbs(st, s) diff --git a/internal/app/session/verbsradio.go b/internal/app/session/verbsradio.go new file mode 100644 index 00000000..b3aad35b --- /dev/null +++ b/internal/app/session/verbsradio.go @@ -0,0 +1,61 @@ +// Handing a node's radio a frame directly, for testing a firmware's receive +// path against receptions the channel would rarely produce on demand. +package session + +import ( + "encoding/hex" + "strings" + + "github.com/MeshBench/meshbench/internal/app/state" + "github.com/MeshBench/meshbench/internal/firmware" +) + +// deliverKinds names what radio.deliver can say about a reception, as +// virtual-sx1262 numbers the failures. "ok" is a frame that arrived intact. +var deliverKinds = map[string]firmware.FrameFailure{ + "ok": 0, + "crc_error": firmware.FailedCRC, + "header_error": firmware.FailedHeader, + "no_crc": firmware.FailedNoCRC, + "next_before_read": firmware.FailedNextBeforeRead, + "next_during_read": firmware.FailedNextDuringRead, +} + +func registerRadioVerbs(st *state.Store, s *Sim) { + st.Handle("radio.deliver", func(w *state.World, p any) (any, error) { + m, _ := p.(map[string]any) + name, _ := m["node"].(string) + raw, _ := m["hex"].(string) + as, _ := m["as"].(string) + if as == "" { + as = "ok" + } + why, known := deliverKinds[as] + if !known { + return nil, badParams("radio.deliver: as is one of ok, crc_error, header_error, no_crc, next_before_read, next_during_read, not %q", as) + } + frame, err := hex.DecodeString(strings.ReplaceAll(raw, " ", "")) + if err != nil || len(frame) == 0 { + return nil, badParams("radio.deliver: hex is the frame as hex digits") + } + at := -1 + for i := range w.Nodes { + if w.Nodes[i].Name == name { + at = i + } + } + if at < 0 { + return nil, UnknownNames("radio.deliver", w.Nodes, []string{name}) + } + if s.Engine() == nil { + return nil, ErrNoSimulation + } + // Not delivered is an answer, not an error: a node without firmware, + // or whose chip predates the kind, was handed nothing. + taken, err := s.Engine().DeliverFrame(at, frame, why) + if err != nil { + return nil, err + } + return map[string]any{"node": name, "as": as, "bytes": len(frame), "delivered": taken}, nil + }) +} diff --git a/internal/app/session/verbsradio.verbs.json b/internal/app/session/verbsradio.verbs.json new file mode 100644 index 00000000..67e90c97 --- /dev/null +++ b/internal/app/session/verbsradio.verbs.json @@ -0,0 +1,17 @@ +{ + "radio.deliver": { + "what": "hand a node's radio one frame directly, as received or as a chosen kind of failed reception, to test the firmware's receive path without the channel deciding anything", + "params": [ + {"name": "node", "type": "string", "what": "the node whose radio receives it; it needs firmware running"}, + {"name": "hex", "type": "string", "what": "the frame, header first, as hex digits"}, + {"name": "as", "type": "string", "what": "ok (the default), crc_error, header_error, no_crc (the header said the payload has no CRC), next_before_read or next_during_read (the next frame's header arriving in continuous receive before or during the firmware's read of this one)"} + ], + "returns": ["node", "as", "bytes", "delivered"], + "answers": "`delivered` is false when the node has no firmware or its chip does not read that kind, which is an answer rather than a refusal. The next_* kinds need a frame already received and not yet read, so they follow an ok delivery.", + "example": { + "params": {"node": "West Lomond", "hex": "1500348b2db7", "as": "no_crc"}, + "what": "a frame whose header said it carries no CRC", + "runnable": false + } + } +} diff --git a/internal/firmware/bridge.go b/internal/firmware/bridge.go index 8f5ff8f2..c5493ced 100644 --- a/internal/firmware/bridge.go +++ b/internal/firmware/bridge.go @@ -76,6 +76,9 @@ const ( // older than that reads the code as a CRC error and would raise RxDone for // a frame it never locked onto, so the code goes only where this is set. capNoLock = 0x02 + // capRxFaults is the bit by which it says its chip reads FailedNoCRC, + // FailedNextBeforeRead and FailedNextDuringRead (virtual-sx1262 ABI 1.8). + capRxFaults = 0x04 ) // FrameFailure is where a frame the receiver locked onto went wrong, numbered @@ -94,6 +97,18 @@ const ( // chip model withdraws the HeaderValid it raised from the carrier's length, // because that was its stand-in for a header it cannot decode. FailedNoLock FrameFailure = 3 + // FailedNoCRC is a frame whose header said its payload carries no CRC. The + // part checks nothing and raises RxDone alone, so the firmware sees a good + // frame unless it reads the header's CRC flag. + FailedNoCRC FrameFailure = 4 + // FailedNextBeforeRead is the next frame's header arriving in continuous + // receive after the last frame's RxDone and before the firmware read it: + // the chip reports the new length with the old start pointer. A model to + // test a driver against, not documented behaviour of the part. + FailedNextBeforeRead FrameFailure = 5 + // FailedNextDuringRead is the same, arriving after the firmware's first + // buffer status read. + FailedNextDuringRead FrameFailure = 6 ) // ErrClosed is returned once a bridge has been shut down. @@ -143,6 +158,8 @@ type Bridge struct { // it does not know, so it is sent only to a node that has asked for it. takesFailed bool takesNoLock bool + // takesRxFaults is whether its chip reads failures 4 to 6. + takesRxFaults bool // done is closed when the bridge is. Anything waiting on the node must // select on it: a node whose process has gone will never ack, and a wait @@ -310,6 +327,7 @@ func (b *Bridge) read(c net.Conn) { b.stats = st b.takesFailed = n >= 48 && buf[47]&capFrameFailed != 0 b.takesNoLock = n >= 48 && buf[47]&capNoLock != 0 + b.takesRxFaults = n >= 48 && buf[47]&capRxFaults != 0 b.mu.Unlock() } @@ -339,6 +357,20 @@ func (b *Bridge) Deliver(frame []byte) error { return nil } +// Takes reports whether the node has said its chip reads this failure. Before +// the first radio stats record nothing is known, and the answer is no. +func (b *Bridge) Takes(why FrameFailure) bool { + b.mu.Lock() + defer b.mu.Unlock() + switch why { + case FailedNoLock: + return b.takesFailed && b.takesNoLock + case FailedNoCRC, FailedNextBeforeRead, FailedNextDuringRead: + return b.takesFailed && b.takesRxFaults + } + return b.takesFailed +} + // DeliverFailed tells the firmware's chip that a frame it locked onto failed, // and where. Nothing is decoded from it; the frame rides along because a CRC // failure leaves the corrupt bytes in the chip's buffer, as the part does. @@ -346,10 +378,7 @@ func (b *Bridge) Deliver(frame []byte) error { // A node that has not said it takes the kind is told nothing, which is what it // was always told. func (b *Bridge) DeliverFailed(frame []byte, why FrameFailure) error { - b.mu.Lock() - takes := b.takesFailed && (why != FailedNoLock || b.takesNoLock) - b.mu.Unlock() - if !takes { + if !b.Takes(why) { return nil } if err := b.send(kindFrameFailed, append([]byte{byte(why)}, frame...)); err != nil { diff --git a/internal/sim/engine/inject.go b/internal/sim/engine/inject.go index b8782b23..a12d8e86 100644 --- a/internal/sim/engine/inject.go +++ b/internal/sim/engine/inject.go @@ -60,3 +60,29 @@ func (e *Engine) InjectFrame(nodeIndex int, frame []byte) { } e.startTransmission(nodeIndex, frame, now) } + +// DeliverFrame hands a frame straight to a node's radio, as received, without +// the channel deciding anything: the bench for a firmware's receive path. +// why is zero for a good frame, otherwise how the reception went wrong. +// It reports false when the node has no firmware or its chip does not read +// that kind, so a script cannot mistake a frame nobody was given for one that +// was ignored. +func (e *Engine) DeliverFrame(nodeIndex int, frame []byte, why firmware.FrameFailure) (bool, error) { + e.mu.Lock() + ok := nodeIndex >= 0 && nodeIndex < len(e.nodes) + var fw *firmware.Node + if ok { + fw = e.nodes[nodeIndex].Firmware + } + e.mu.Unlock() + if fw == nil || fw.Bridge == nil || len(frame) == 0 { + return false, nil + } + if why == 0 { + return true, fw.Bridge.Deliver(frame) + } + if !fw.Bridge.Takes(why) { + return false, nil + } + return true, fw.Bridge.DeliverFailed(frame, why) +} diff --git a/pkg/client-go/meshbench/nodes.go b/pkg/client-go/meshbench/nodes.go index 63b77299..c398125e 100644 --- a/pkg/client-go/meshbench/nodes.go +++ b/pkg/client-go/meshbench/nodes.go @@ -401,6 +401,20 @@ func (n Node) Inject(ctx context.Context) error { return n.w.Do(ctx, "sim.inject", n.name) } +// Deliver hands this node's radio one frame directly, as received or as a +// kind of failed reception: "ok", "crc_error", "header_error", "no_crc", +// "next_before_read" or "next_during_read". It reports false when the node has +// no firmware or its chip does not read that kind, which is an answer, not a +// refusal. +func (n Node) Deliver(ctx context.Context, frameHex, as string) (bool, error) { + var got struct { + Delivered bool `json:"delivered"` + } + err := n.w.CallInto(ctx, "radio.deliver", + map[string]any{"node": n.name, "hex": frameHex, "as": as}, &got) + return got.Delivered, err +} + // Provisioning is what this node is told at boot, in the console's own words. func (n Node) Provisioning(ctx context.Context) ([]string, error) { var out struct { diff --git a/pkg/client-python/meshbench/nodes.py b/pkg/client-python/meshbench/nodes.py index af91ac9c..37dcf9dd 100644 --- a/pkg/client-python/meshbench/nodes.py +++ b/pkg/client-python/meshbench/nodes.py @@ -436,6 +436,18 @@ def inject(self) -> None: """ self._wb.call("sim.inject", self.name) + def deliver(self, frame_hex: str, as_: str = "ok") -> bool: + """Hand this node's radio one frame directly, as received or as a kind + of failed reception: ok, crc_error, header_error, no_crc, + next_before_read or next_during_read. + + False when the node has no firmware or its chip does not read that + kind, which is an answer, not a refusal. + """ + params = {"node": self.name, "hex": frame_hex, "as": as_} + got = self._wb.call("radio.deliver", params) or {} + return bool(got.get("delivered")) + def provisioning(self) -> list[str]: """What this node is told at boot, in the console's own words.""" return (self._wb.call("node.provisioning", self.name) or {}).get("commands", []) diff --git a/tools/verbdoc/facade.json b/tools/verbdoc/facade.json index 9cfcc057..280c8e20 100644 --- a/tools/verbdoc/facade.json +++ b/tools/verbdoc/facade.json @@ -19,6 +19,7 @@ "had an rf at all." ], "facade": { + "radio.deliver": "node.deliver(frame_hex, as_='ok')", "session.describe": "wb.describe()", "session.list": "meshbench.sessions()", "session.status": "wb.status()", @@ -404,6 +405,7 @@ "title": "Serving to real clients", "prefixes": [ "bench", + "radio", "sdr" ] },