State model, readiness and gating¶
How TwiistOff turns the pump's status stream into decisions. A driver does not have to copy this, but it has to answer the same questions: can I send a therapy command now, and will the pump act on it?
What the pump tells you, and what it does not¶
- On the command channel the pump sends only three periodic pushes —
pumpStatus(0x0200),loopStatus(0x8705),cgmStatus(0x8304) — plusrequest | 0x80replies and0xFF80NACKs. There is no pump-initiated alarm or event frame there. Alarms, alerts and set-change state appear as bits inpumpStatus; history appears as the log counter inpumpStatus(bytes 16–19), which you follow withgetLogRecord. - So events come from diffing consecutive
pumpStatuspushes and following the pump log. - Other characteristics do carry unprompted traffic that matters: an
e5332023notification is a session re-key (a reboot), ande5332001re-indicates its RIS block at a reboot and just before a cassette-detach status (BLE and session). pumpStatusarrives about every 5 s on an armed link. TwiistOff treats status older than 8 s as stale (roughly one missed push).
Readiness¶
Never gate on a whole pumpState value; gate on bits (Status fields).
| Question | Rule |
|---|---|
| Pump ready | pumpState bit 0x0040 (therapy-ready) set, configurationState 0x1FFF (when reported), no alarm bit set, not in cassette-change mode (0x0400) |
| Can deliver | pump ready and session armed and the command path is proven (below) |
| Is delivering | basal rate (pumpStatus bytes 42–43) > 0 |
| Zero temp basal | a temp basal is running with basal rate 0: can deliver, delivering nothing until it ends |
| Fresh pump | configurationState == 0 plus alert pumpIdle (pumpState 0x0001 alone is ambiguous) |
"Can deliver" and "is delivering" are different: after a same-cassette reboot the pump is therapy-ready
(0x007B) with basal stopped. A 0 % or 0 U/h temp basal is reported as a zero temp basal, not as
"basal stopped". Alerts and notices (e.g. pumpIdle) are informational, not blockers.
GET /pump/ready reports this as one verdict line: NOT READY: <blockers>, CAN DELIVER; delivering
basal 2.00 U/h, CAN DELIVER; zero temp basal running, 25 min left (…) or CAN DELIVER; NOT
delivering (basal stopped - startBasal to resume).
Conditions and banners¶
Readiness answers one question; the app needs to know, per kind of command, what the pump's state
allows. pump/PumpConditions.kt turns the link state and the latest pumpStatus into a list of
conditions, highest priority first. Each condition has:
- a severity: blocking, warning or info;
- the kinds of command it blocks (stop, read, alarm reset, silence, clear alert / notice, settings write, cassette / battery change, fill, prime, prime complete, and the delivery starts: start basal, temp basal, bolus, cannula fill);
- a text with live values ("Priming (3.52 U so far)", "Pump not configured (configurationState 0x0000)") and the next action for the operator;
- the wizard that resolves it, if any.
The full table is in Setup and recovery §6.4. Stops and reads are never blocked by a pump condition; stops wait only for an armed link (and for the re-arm after a restart, when nothing can be sent).
One table, every consumer, so a command is refused for the same stated reason everywhere:
- The delivery gate (
ui/safety/SafetyGate.kt): a delivery button is disabled with the blocking conditions for its kind of command listed under it. - The banners above every screen: every blocking and warning condition (and notices), with its next action and a one-tap fix — Fix… opens the condition's wizard; Set up, Connect, Resume…, Clear and LOOP OFF act directly. A running bolus, prime and temp basal keep their own banners with a STOP button.
- The wizards: the recovery hub offers first the wizard of the highest-priority blocking condition.
SetupRunnerrefuses fill, prime, configure, settings writes, start basal, cannula fill, the change commands, the volume correction, alarm reset and silence with the first blocking condition's text, so the UI and REST get the same answer.GET /pump/conditions: the active conditions and, per kind of command,allowedor the refusal.
A status older than 8 s is itself a condition (stale), so a gate that is evaluated without fresh status fails closed.
Command path¶
After every arm, and after every pump reboot, TwiistOff does not trust the command path until the pump has answered something, and it stops trusting it as soon as a command goes unanswered:
alarmReset accepted -> AWAITING_REBOOT -> REBOOTED -> REARMING -> UNPROVEN_AFTER_REARM -> PROVEN
PROVEN / UNKNOWN -- a command unanswered --> UNANSWERED -> REARMING -> UNPROVEN_AFTER_REARM -> PROVEN
still unanswered after the one extra re-arm --> REARM_FAILED (Reconnect, or re-arm by hand)
A reboot is detected from an e5332023 re-key notification, or when the status frame seq restarts or
resetCount (bytes 30–31) changes (BLE and session §2.1).
The app then re-arms once on the same link with the stored key (3 s after the signal) and sends one
read-only getLogRecord of the log head; its answer moves the path to PROVEN. A second signal for the
same restart is dropped, never a second arm. A command left unanswered, or an arm that fails, gets one
extra re-arm; after that the path is REARM_FAILED until a reconnect or a manual re-arm. After an
accepted alarmReset the path waits in AWAITING_REBOOT for the restart (re-arming anyway if none is
seen within 30 s), and a second alarmReset is refused until the restart is seen. Therapy commands are
refused in every held state.
Sending therapy commands¶
- Serialised: one command at a time per session.
- Matched: a reply counts only if it is
opcode == request | 0x80with the request'sseq, or a0xFF80NACK naming the request's opcode with the request'sseq. Anything else is dropped and logged. - Sent once. Outcomes:
| Outcome | Meaning | What to do |
|---|---|---|
ACCEPTED |
The pump answered. | Confirm the effect in the next status push. |
NACKED |
Refused; the NACK carries the next valid therapy number. | Fix the cause, then send again deliberately. |
OUTCOME_UNKNOWN |
No answer in time. The app re-reads status and reports whether the therapy number advanced, the log advanced, or the bolus id changed. | Do not resend blindly — a lost reply does not mean the command was not executed, and a resend with a fresh valid number could deliver twice. Read status and the log first. |
NOT_SENT |
Not armed, the command path is held, the opcode is one the pump refuses as invalid (0x0E00–0x0E02), or a gate (e.g. alarmReset without a resettable alarm) refused it. |
See the error. |
Gates that are never applied¶
Stop commands (stop bolus, stop basal, cancel temp basal, stop prime, stop cannula fill) are never gated by readiness, by a pump condition or by the UI's confirmations. Physically removing the pump battery always stops delivery.
Where it lives in the code¶
pump/PumpReadiness.kt (readiness, the alarmReset gate, the re-key rule), pump/RearmGuard.kt (single-flight re-arm, the unanswered-command rule, alarmReset once per alarm), pump/PumpConditions.kt
(the condition table), pump/RecoveryWizards.kt (the wizards' plans), pump/PumpLink.kt (reboot and
re-key detection, re-arm, command path), pump/ReplyRouter.kt (reply matching),
pump/TherapyCommand.kt (send-once outcomes), pump/observe/ (records every frame and channel;
GET /pump/observed), ui/safety/SafetyGate.kt (the UI's delivery gate, reading the condition
table), ui/components/BannerStack.kt (the banners).