From 9a07ebd3e6ff99b66570c53232eea39dac69622a Mon Sep 17 00:00:00 2001 From: Pete Schwamb Date: Mon, 30 Nov 2026 09:23:43 -0600 Subject: [PATCH] Fault 0x31 on a bolus sent while a bolus is running A real pod faults with 0x31 (049, insulin delivery command error) when it gets a bolus while one is running. It takes neither the new bolus nor its sequence number: in a field report, a third prime sent during a cannula insertion bolus faulted the pod, whose detailed status kept the previous program's sequence number and that bolus's undelivered pulses. The simulator now does the same, so a phone that sends a bolus during a bolus sees the fault it would see with a real pod. --- README.md | 2 +- pkg/pod/pod.go | 8 ++++++++ pkg/pod/state_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5d9b74a..9f0c182 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Fake pod implementation * This version also mimics a behavior we see in some DASH pods where the pod disconnects every 3 minutes; this can be used with iOS hooks to make a heartbeat to run Loop in situations where a BLE CGM is not available. -* It serves a control panel at http://:8080/ (`-api` changes the address): the pod's reservoir, delivery, age, alerts and fault, with controls to set the reservoir and pod age, raise alerts, trigger or clear a fault, and crash on the next command to test unacknowledged commands. The panel drives a JSON API: `GET /status`, and `POST /command` with `{"command": "changeReservoir", "value": 45}` (also `setActiveTime` in minutes, `setAlerts` as a slot mask, `setFault` with a fault code, 0 to clear, `crashNextCommand` with `"beforeProcessing": true|false`, `ignoreNextMessages` with a count, which keeps the link up but leaves that many messages unanswered, as a pod that didn't hear them would, `hangUpOnNextMessages` with a count, which hangs up without answering when each of that many messages arrives, as a pod that drops the link during a command does, or with `"afterAnswering": true` once the phone has acknowledged the answer, and `failNextConnects` with a count and an optional `hangUpAfter` in seconds, 7 by default, which leaves service discovery unanswered on that many connects and then hangs up with CoreBluetooth's "peripheral disconnected" error, as some pods do in the field; this needs `-transport impossible` and an ImpossiBLE that passes `errorDomain` and `errorCode` through on a disconnect). A fault stops a bolus in progress, and the pod reports the part left undelivered however late it is read, as a real pod does. A reset fault (0x0D, 0x0E, 0x0F, 0x10, 0x12, 0x1E, 0x22, 0x34, 0x36, 0x3B, 0x4F, 0xD6 or 0xD7) reports what a reset pod does instead: no fault time (0x0000 on a DASH, 0xFFFF on an Omnipod 5, which has been seen reporting both), nothing delivered or left undelivered, and minutes active counted from the reset. +* It serves a control panel at http://:8080/ (`-api` changes the address): the pod's reservoir, delivery, age, alerts and fault, with controls to set the reservoir and pod age, raise alerts, trigger or clear a fault, and crash on the next command to test unacknowledged commands. The panel drives a JSON API: `GET /status`, and `POST /command` with `{"command": "changeReservoir", "value": 45}` (also `setActiveTime` in minutes, `setAlerts` as a slot mask, `setFault` with a fault code, 0 to clear, `crashNextCommand` with `"beforeProcessing": true|false`, `ignoreNextMessages` with a count, which keeps the link up but leaves that many messages unanswered, as a pod that didn't hear them would, `hangUpOnNextMessages` with a count, which hangs up without answering when each of that many messages arrives, as a pod that drops the link during a command does, or with `"afterAnswering": true` once the phone has acknowledged the answer, and `failNextConnects` with a count and an optional `hangUpAfter` in seconds, 7 by default, which leaves service discovery unanswered on that many connects and then hangs up with CoreBluetooth's "peripheral disconnected" error, as some pods do in the field; this needs `-transport impossible` and an ImpossiBLE that passes `errorDomain` and `errorCode` through on a disconnect). A fault stops a bolus in progress, and the pod reports the part left undelivered however late it is read, as a real pod does. A reset fault (0x0D, 0x0E, 0x0F, 0x10, 0x12, 0x1E, 0x22, 0x34, 0x36, 0x3B, 0x4F, 0xD6 or 0xD7) reports what a reset pod does instead: no fault time (0x0000 on a DASH, 0xFFFF on an Omnipod 5, which has been seen reporting both), nothing delivered or left undelivered, and minutes active counted from the reset. A bolus sent while a bolus is running faults the pod with 0x31, as a real pod does; the pod takes neither the new bolus nor its sequence number. * The websocket API at `/ws`, used by the separate [NodeJS/React frontend](https://github.com/ps2/pod_simulator_frontend), is still there. diff --git a/pkg/pod/pod.go b/pkg/pod/pod.go index 3311d9d..83b1fa8 100644 --- a/pkg/pod/pod.go +++ b/pkg/pod/pod.go @@ -773,6 +773,14 @@ func (p *Pod) handleCommand(cmd command.Command) { case *command.ProgramInsulin: // 0x1A log.Debugf("pkg pod; ProgramInsulin: PodProgress = %d", p.state.PodProgress) + if c.TableNum == 2 && p.state.BolusEnd.After(time.Now()) { + // A bolus while one is running faults the pod with 0x31. The pod + // takes neither the new bolus nor its sequence number. + log.Infof("pkg pod; bolus of %d pulses while a bolus is running: fault 0x31", c.Pulses) + p.state.SetFault(0x31) + return + } + if p.state.PodProgress < response.PodProgressPriming { // this must be the prime command p.state.PodProgress = response.PodProgressPriming diff --git a/pkg/pod/state_test.go b/pkg/pod/state_test.go index a6b4426..87b41d1 100644 --- a/pkg/pod/state_test.go +++ b/pkg/pod/state_test.go @@ -169,6 +169,53 @@ func TestDeactivateReportsTheRealTimeAndReservoir(t *testing.T) { } } +func TestABolusWhileABolusIsRunningFaults0x31(t *testing.T) { + // A second prime sent 15 s into a 2.6 U cannula insertion bolus, as in a + // field report: 37 pulses still to go, the last program had sequence 15 + s := &PODState{ + PodProgress: response.PodProgressInsertingCannula, + ActivationTime: time.Now().Add(-4 * time.Minute), + Delivered: 104, + Reservoir: 2896, + BolusEnd: time.Now().Add(36500 * time.Millisecond), + LastProgSeqNum: 15, + } + p := &Pod{state: s} + + p.handleCommand(&command.ProgramInsulin{Seq: 4, TableNum: 2, Pulses: 52}) + + if s.FaultEvent != 0x31 { + t.Fatalf("fault 0x%02X, want 0x31", s.FaultEvent) + } + if s.LastProgSeqNum != 15 || s.PodProgress != response.PodProgressInsertingCannula { + t.Fatalf("last program %d, progress %d; want 15 and %d", s.LastProgSeqNum, s.PodProgress, response.PodProgressInsertingCannula) + } + if s.Delivered != 67 || s.BolusRemaining() != 37 { + t.Fatalf("delivered %d, bolus remaining %d; want 67 and 37", s.Delivered, s.BolusRemaining()) + } + rsp, ok := p.getResponse(&command.ProgramInsulin{Seq: 4, TableNum: 2, Pulses: 52}).(*response.DetailedStatusResponse) + if !ok || rsp.FaultEvent != 0x31 || rsp.LastProgSeqNum != 15 { + t.Fatalf("answered %+v, want a detailed status with fault 0x31 and sequence 15", rsp) + } +} + +func TestABolusAfterTheLastOneEndedIsTaken(t *testing.T) { + s := &PODState{ + PodProgress: response.PodProgressRunningAbove50U, + ActivationTime: time.Now().Add(-2 * time.Hour), + Delivered: 1080, + Reservoir: 2400, + BolusEnd: time.Now().Add(-time.Second), + } + p := &Pod{state: s} + + p.handleCommand(&command.ProgramInsulin{Seq: 9, TableNum: 2, Pulses: 20}) + + if s.FaultEvent != 0 || s.LastProgSeqNum != 9 || s.Delivered != 1100 { + t.Fatalf("fault 0x%02X, last program %d, delivered %d; want none, 9 and 1100", s.FaultEvent, s.LastProgSeqNum, s.Delivered) + } +} + func TestTheEightyHourFaultHappensAtEightyHours(t *testing.T) { s := &PODState{ PodProgress: response.PodProgressRunningBelow50U,