Skip to content
Open
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
11 changes: 11 additions & 0 deletions internal/firmware/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ const (
// capRxFaults is the bit by which it says its chip reads FailedNoCRC,
// FailedNextBeforeRead and FailedNextDuringRead (virtual-sx1262 ABI 1.8).
capRxFaults = 0x04

// kindFrameStarted is host → node: a frame whose carrier begins here, and
// when in the node's own clock. Its chip lands it in the buffer as it
// arrives, so a firmware that reads late finds the next frame's header
// and bytes behind the last, as on silicon. capFrameStarted says the node
// takes it (virtual-sx1262 ABI 1.9).
kindFrameStarted = 0x0B
capFrameStarted = 0x08
)

// FrameFailure is where a frame the receiver locked onto went wrong, numbered
Expand Down Expand Up @@ -160,6 +168,8 @@ type Bridge struct {
takesNoLock bool
// takesRxFaults is whether its chip reads failures 4 to 6.
takesRxFaults bool
// takesFrameStarted is whether it takes kindFrameStarted.
takesFrameStarted 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
Expand Down Expand Up @@ -328,6 +338,7 @@ func (b *Bridge) read(c net.Conn) {
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.takesFrameStarted = n >= 48 && buf[47]&capFrameStarted != 0
b.mu.Unlock()
}

Expand Down
51 changes: 51 additions & 0 deletions internal/firmware/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,54 @@ func TestConsoleSinkFollowsWhoeverHoldsThePort(t *testing.T) {
t.Errorf("the client saw %q, want %q", got, want)
}
}

// A frame start goes only to a node that has said it takes the kind, with its
// start in front of the bytes: a native bridge built before it ends its
// connection on a kind it does not know.
func TestAFrameStartReachesOnlyANodeThatTakesIt(t *testing.T) {
b, _ := Listen("127.0.0.1:0", "GB7XYZ")
defer func() { _ = b.Close() }()
c := fakeRadio(t, b.Addr())
defer func() { _ = c.Close() }()

for i := 0; i < 50 && !b.Attached(); i++ {
time.Sleep(10 * time.Millisecond)
}
if err := b.FrameStarted(7, []byte{0x11}); err != nil {
t.Fatal(err)
}

record := make([]byte, 48)
record[47] = capFrameStarted
if _, err := c.Write(append([]byte{kindRadioStats, 0, 48}, record...)); err != nil {
t.Fatal(err)
}
for i := 0; i < 100; i++ {
b.mu.Lock()
takes := b.takesFrameStarted
b.mu.Unlock()
if takes {
break
}
time.Sleep(5 * time.Millisecond)
}
if err := b.FrameStarted(0x01020304, []byte{0x22, 0xBB}); err != nil {
t.Fatal(err)
}

var hdr [3]byte
_ = c.SetReadDeadline(time.Now().Add(2 * time.Second))
if _, err := io.ReadFull(c, hdr[:]); err != nil {
t.Fatal(err)
}
if hdr[0] != kindFrameStarted {
t.Fatalf("wrong frame kind 0x%02x, want 0x0b", hdr[0])
}
buf := make([]byte, binary.BigEndian.Uint16(hdr[1:]))
if _, err := io.ReadFull(c, buf); err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf, []byte{1, 2, 3, 4, 0x22, 0xBB}) {
t.Errorf("emulator received % x, want 01 02 03 04 22 bb: the start sent before it said it takes the kind leaked", buf)
}
}
18 changes: 18 additions & 0 deletions internal/firmware/lockstep.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,21 @@ func (b *Bridge) CaughtUp(atMs uint32) bool {
}
}
}

// FrameStarted tells the node a frame's carrier begins here at atMs, its own
// clock, so its chip can land the frame as it arrives. The frame is still
// delivered, or not, when it ends. A node that has not said it takes the kind
// is told nothing, and receives each frame whole as before.
func (b *Bridge) FrameStarted(atMs uint32, frame []byte) error {
b.mu.Lock()
takes := b.takesFrameStarted
b.mu.Unlock()
if !takes {
return nil
}
payload := binary.BigEndian.AppendUint32(make([]byte, 0, 4+len(frame)), atMs)
if err := b.send(kindFrameStarted, append(payload, frame...)); err != nil {
return fmt.Errorf("firmware: frame start to %s: %w", b.node, err)
}
return nil
}
46 changes: 25 additions & 21 deletions internal/sim/engine/channelbusy.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,36 @@ func (e *Engine) channelBusy(now uint32) []bool {
if dst.Firmware == nil {
continue
}
rxPHY := e.phyOf(dst.specRef())
for _, t := range air {
// A node is deaf to the channel while its own transmitter is keyed,
// and it is not listening for itself in any case.
if t.from == i {
continue
}
src := nodes[t.from]
// Activity on another channel is not activity this node can detect
// - the rule delivery already uses. Without it a node would defer
// to a mesh it is not part of.
txPHY := e.phyOf(src.specRef())
if !txPHY.sameChannel(rxPHY) {
continue
}
loss, ok := e.pathLoss(t.from, i)
if !ok {
continue
}
rxDBm := e.rxPowerDBm(nodes, t.from, i, loss)
noiseDBm := dsp.NoiseFloorDBm(txPHY.bandwidthHz, e.noiseFigOf(dst.specRef()))
if rxDBm-noiseDBm >= requiredSNRdB(txPHY.sf) {
if e.detects(nodes, t, i) {
busy[i] = true
break
}
}
}
return busy
}

// detects is whether node i can detect transmission t at all.
func (e *Engine) detects(nodes []*Node, t transmission, i int) bool {
// A node is deaf to the channel while its own transmitter is keyed, and it
// is not listening for itself in any case.
if t.from == i {
return false
}
dst, src := nodes[i], nodes[t.from]
// Activity on another channel is not activity this node can detect - the
// rule delivery already uses. Without it a node would defer to a mesh it is
// not part of.
txPHY := e.phyOf(src.specRef())
if !txPHY.sameChannel(e.phyOf(dst.specRef())) {
return false
}
loss, ok := e.pathLoss(t.from, i)
if !ok {
return false
}
rxDBm := e.rxPowerDBm(nodes, t.from, i, loss)
noiseDBm := dsp.NoiseFloorDBm(txPHY.bandwidthHz, e.noiseFigOf(dst.specRef()))
return rxDBm-noiseDBm >= requiredSNRdB(txPHY.sf)
}
3 changes: 3 additions & 0 deletions internal/sim/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ type Engine struct {
// every tick. firmwareNewlyDown is the subset a caller has not yet been
// told about; FirmwareFailures drains it.
firmwareDown map[string]bool
// firmwareAt is the instant the firmware was last advanced to, so each
// frame's start is announced once.
firmwareAt uint32
// debug is which nodes are being debugged, and held which of them a
// debugger is holding, at the tick they did not answer. See debug.go.
debug map[string]Debug
Expand Down
20 changes: 20 additions & 0 deletions internal/sim/engine/firmwarerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,17 @@ func (e *Engine) runFirmware(ctx context.Context, now uint32) error {
// instead of the sum — the difference between a 300-node scenario stepping
// and a 300-node scenario saturating the machine.
busy := e.channelBusy(now)
e.mu.Lock()
since := e.firmwareAt
e.firmwareAt = now
e.mu.Unlock()
starts := e.framesStarting(since, now)
sent := make([]ticked, 0, len(targets))
for _, t := range targets {
if err := e.announceStarts(t, now, starts[t.i]); err != nil {
e.markFirmwareDown(t.name, t.fw, "its radio would not take a frame start: "+err.Error())
continue
}
if e.stillHeld(t) {
continue
}
Expand Down Expand Up @@ -188,3 +197,14 @@ func (e *Engine) FirmwareFailures() []FirmwareFailure {
e.firmwareNewlyDown = nil
return out
}

// announceStarts tells one node about the frames that began at it since its
// last tick, each at its start in the node's own clock.
func (e *Engine) announceStarts(t ticked, now uint32, starts []transmission) error {
for _, s := range starts {
if err := t.fw.Bridge.FrameStarted(s.startMs+(t.atMs-now), s.frame); err != nil {
return err
}
}
return nil
}
35 changes: 35 additions & 0 deletions internal/sim/engine/framestarts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Frames as their carriers begin, for a node whose chip lands them as they
// arrive.
//
// A frame is delivered when it ends, and a firmware that reads its radio late
// on hardware finds the next frame's header, and then its bytes, behind the
// last one in the chip's buffer. A chip told only at the end cannot show that,
// so the engine says when each frame starts as well.
package engine

// framesStarting is, for every node, the transmissions that began in
// [since, now] loudly enough to be detected there. Since is included because
// a step collects what the firmware sent after running it, so a frame started
// at the last step's instant is first seen now.
func (e *Engine) framesStarting(since, now uint32) [][]transmission {
e.mu.Lock()
nodes := make([]*Node, len(e.nodes))
copy(nodes, e.nodes)
var starting []transmission
for _, t := range e.inFlight {
if t.startMs >= since && t.startMs <= now {
starting = append(starting, t)
}
}
e.mu.Unlock()

out := make([][]transmission, len(nodes))
for _, t := range starting {
for i, dst := range nodes {
if dst.Firmware != nil && e.detects(nodes, t, i) {
out[i] = append(out[i], t)
}
}
}
return out
}