Self-diagnosis: verify the whole pump-control path¶
A single ordered walk that proves TwiistOff can find, connect to, bond with, arm, and read a Twiist pump — with the expected result at every step so a human or an agent can tell pass from fail without guessing. Run it top to bottom; each stage unlocks the next (the UI and the REST bridge share one state machine, so they can never disagree about ordering).
Bench hardware only. Saline or water, never insulin, never a person. The delivery section (§6) actuates the pump; do not run it unless the pump is bench equipment and off-body.
Two surfaces, identical semantics:
- UI: the Connect page — one gated block per stage, top to bottom.
- REST bridge:
adb forward tcp:8090 tcp:8090, then the routes below.GET /setupis the live snapshot and returns the same object after everyPOST /setup/*step. Full route reference: REST API.
Placeholders used below (never commit the real values):
- SERIAL — the unit's serial, advertised as its BLE name, shape 00000000M (8 digits + a letter).
- LTSS — the per-unit long-term session secret (16 or 32 bytes hex), entered by the pump owner.
- Addresses rotate (resolvable private address), so the MAC changes between sessions — match on the
name and the candidate flags, never a fixed address.
0. Bring up the bridge¶
adb forward tcp:8090 tcp:8090
curl -s http://127.0.0.1:8090/status
Pass: JSON with "state" present (e.g. DISCONNECTED before you start). Fail: connection
refused → the debug app is not running/foreground, or the forward did not take. Release builds do not
serve the bridge.
1. Scan → pump detection¶
| Bridge | POST /scan/start, wait ~5 s, GET /devices (or GET /setup → devices[]), POST /scan/stop |
| UI | Connect → block 1 Scan → Start scan |
Each scanned device carries detection flags:
isPumpCandidate: true— the row the UI highlights (coloured card, bold name, badges).- A candidate matches for one or more reasons, each shown as a badge:
- serial-like name — name matches
^\d{8}[A-Za-z]$(isTwiistSerial). - mfg 0x026B — advertises manufacturer-specific data under company ID
0x026B(hasTwiistMfg). - name hint only — name contains
twiist/sequel/dekabut neither strong signal.
Pass: exactly one row with isPumpCandidate:true, name = SERIAL, strong RSSI (≈ −40 to −60 dBm
on the bench). Example healthy row:
{"address":"<rotates>","name":"SERIAL","rssi":-46,"isPumpCandidate":true}
Fail / fixes:
- No candidate but many devices → pump is not advertising. Reset it into pairing mode (cassette off,
battery out/in) to open an advertising window, then rescan.
- The pump appears but is not flagged → the advertisement did not carry a serial-shaped name or
0x026B. Note it (gap): the heuristic may need widening. You can still connect by address.
- UI shows a long list and the pump is hard to spot → toggle Pumps only (the candidates-only
filter) in block 1.
Display note: detection is a display feature — the candidate flag is computed on every scan result and is authoritative at
GET /devices. The list shows candidates as a coloured card with reason badges, a candidate count and a candidates-only toggle.
2. Connect¶
| Bridge | POST /connect?address=<addr from devices> (or POST /setup/connect) |
| UI | tap Connect on the candidate row |
Pass: GET /status → "state":"CONNECTED", "name":"SERIAL", and GATT discovery completes
(the Twiist service e5331fe0-… and its e533* characteristics are present). Fail: CONNECTING
that never settles → the address rotated; rescan and use the fresh address.
3. Bond (label PIN)¶
| Bridge | POST /setup/bond?pin=<6 digits> (or POST /bond/start) |
| UI | block 3 Bond → Start bond, enter the 6-digit PIN printed on the pump label |
Pass: GET /setup → "bonded":true, "bondState":12 (BOND_BONDED). On a pump already bonded to
this phone, /status//setup report bonded:true immediately on connect — this is normal, not a
skipped step.
Fail / fixes:
- bondState:11 (BONDING) that never completes, or an "insufficient authentication" loop → the PIN was
wrong, or the pump still holds an old bond to another phone/the official app. Forget bond here and
in Android Bluetooth settings, unpair on the other device, then re-bond.
4. MTU 247¶
| Bridge | POST /mtu?value=247 |
| UI | block 4 MTU 247 → Request MTU 247 |
The arm write is silently ignored below ATT MTU 247 — at a lower MTU the pump simply never streams.
Pass: GET /status → "mtu":247.
Note: on a bonded reconnect Android usually negotiates MTU 247 automatically during service discovery, so
/statusalready shows247and pressing Request MTU 247 has nothing to do. That is success, not a dead button. The app states "MTU 247 negotiated — nothing to do here" when already satisfied, and echoes the granted value when you press it, so the control is never a silent no-op. Onlymtu < 247is a problem — request it and recheck.
5. Registration → Arm → Ready (read-only)¶
5a. Registration gate¶
For an already-registered pump (the normal bench case) the wizard pauses here for an explicit operator choice; acknowledge and move on:
| Bridge | POST /setup/registration?ack=true®istered=true |
| UI | block 5 Registration → Skip — already registered |
Pass: GET /setup → stage advances past REGISTRATION, actions.ARM.allowed:true.
POST /setup/registration?detect=trueis a read-only probe of the stored blob one5332027. It times out on an ordinary bonded reconnect ("no e5332027 push") because the pump pushes its stored blob only during its own setup/registration connection — expected; use Skip on a known-good pump. A fresh pump probes asFRESHand must be registered by the official app + cloud first.
5b. Arm the channel¶
| Bridge | POST /setup/arm (uses the stored LTSS; or ?ltss=LTSS) |
| UI | block 6 Arm (LTSS) → paste LTSS → Arm channel |
Arm requests MTU 247, reads the session salt (e5332024) + start sequence (e5332023), derives the
keys, writes the arm byte, and waits for a sealed pumpStatus frame.
Pass: GET /setup → "armed":true, "pumpStatusSeen":true; GET /pump/state →
"sessionSalt":"<32 hex>", "startSeq":1, "lastStatus":"0x0200 seq 1", "error":null.
Fail / fixes:
- armed:true but pumpStatusSeen:false and no status arrives → almost always MTU < 247 (recheck §4)
or a stale/invalid LTSS (ltssValid:false in /setup).
- Arm errors immediately → LTSS not stored/invalid; set it via the field or POST /setup/arm?ltss=….
5c. Ready — the healthy armed snapshot¶
GET /setup → pump{} and GET /pump/state decode the sealed status. Reference of a healthy,
idle, armed bench pump (example values; yours vary):
| Field (source) | Example | Meaning / expected on an idle bench |
|---|---|---|
state (/status) |
CONNECTED |
link up |
mtu |
247 |
required for the command channel |
bonded |
true |
LE Secure Connections bond present |
armed |
true |
session established |
pumpStatusSeen |
true |
at least one sealed frame decoded |
pumpState / pumpStateHex |
1 / 0x0001 |
a u16 of per-bit flags, not one state: 0x0001 = no cassette / unconfigured; a configured, primed, self-tested pump with a cassette reads 0x007B (bit 0x0040 = therapy-ready). See Status fields §5 |
selfTestPercent |
0 |
0 with no cassette; 100 once an attached cassette has self-tested |
primingStatus / primingName |
0 / primingIdle |
not priming |
basalRateUe2 |
0 |
hundredths of U/h — 0 before basal is started |
cassetteVolumeUe2 |
28815 |
hundredths of U remaining (= 288.15 U) |
battery |
91 |
percent |
closedLoopEnabled |
false |
must be false on the bench — never command a loop-enabled pump. (Status byte 44 = 0 means the loop is on; the pump then doses by itself, even with no CGM.) |
tempBasalActive |
false |
no temp basal running |
token (/pump/state) |
0x615c |
current therapy number, from pumpStatus[12:13] — the prefix for token commands. After an accepted setup/therapy command the response tail is the valid next number; a bolus response ends with the bolus id (next number = bytes 2–3); after a NACK use NACK bytes 4–5 (every refusal rotates it). TwiistOff tracks all three; a Simple command's reply (e.g. a log read) never moves it. |
commandPath (/pump/state) |
PROVEN |
the command path answered since the last arm/reboot; anything else holds therapy commands |
timeOffsetMinutes / pumpTimeUtc |
-360 / 2026-…Z |
pump clock + local offset, decoded |
error |
null |
no decode/transport error |
If every row matches its "expected" column, the full read path is healthy. This is the furthest a self-diagnosis should go without saline — everything below actuates the pump.
6. Delivery paths (saline only — actuates the pump)¶
Run §6 only with the pump off-body on saline/water. Each command "succeeds" when the pump answers; always confirm the effect in the next status frame (
GET /pump/state/GET /setup), never trust the ack alone. The gate is the pump's configuration state (0x1FFF= configured);verifyTherapySettings(0x800F) answering01is the confirmation the setup flow runs beforestartBasal. A fresh pump answers00until its settings are written.GET /pump/readymust saycanDeliverfirst. Token commands are prefixed with the current therapytokenfrom §5c.
| Path | Bridge | Opcode | Expected result (confirm in next status) |
|---|---|---|---|
| Declare fill (300 U) | POST /setup/fill?units=300 |
— | wire volume 30 75 (300.00 U, little-endian hundredths — a byte swap corrupts it); cassetteVolumeUe2 reflects the declared volume |
| Prime | POST /setup/prime |
0x8900 |
primingName leaves primingIdle; primeVolumeUe2 climbs |
| Stop prime | POST /setup/stop-prime |
0x8901 |
priming halts; then POST /setup/prime-complete once drops form |
| Verify settings | (bundled in basal) | 0x800F |
returns 01 — the delivery gate; 01 required before basal |
| Start basal | POST /setup/basal?rate=2.0 |
0x8100 |
basalRateUe2 = 200 (2.00 U/h) in the next status |
| Temp basal (abs) | POST /pump/temp-basal?... |
0x8102 |
tempBasalActive:true, duration/modifier reflect the request |
| Temp basal stop | POST /pump/temp-basal/stop |
0x8104 |
tempBasalActive:false |
| Cannula fill (0.70 U) | POST /setup/cannula-fill?units=0.7 |
0x820A |
token + 46 00 + reason 00; small volume delivered |
| Stop cannula fill | POST /setup/stop-cannula-fill |
0x8202 |
fill halts (stop path not fully bench-verified) |
| Alarm recovery (only while an alarm is up, status byte 90 = 1) | POST /pump/alarm-reset |
0x0D00 |
the pump reboots: alarm clears, resetCount +1; commandPath goes REBOOTED → … → PROVEN by itself; then verify + POST /setup/basal resumes on the same cassette (no prime) |
Stops (always one tap, never gated): stop bolus 0x8200, stop basal 0x8103, temp-basal cancel
0x8104. Know the stop for any delivery before you start it.
7. When you finish¶
curl -s -X POST http://127.0.0.1:8090/disconnect # expect state → DISCONNECTED
Never leave a session armed and unattended. If a step taught you something new about the pump, record it in What a driver needs and fix any table above that was wrong.
One-shot read-only check (no delivery)¶
A quick "is the whole non-delivering path healthy?" sweep once connected + bonded:
curl -s -X POST "http://127.0.0.1:8090/setup/registration?ack=true®istered=true" >/dev/null
curl -s -X POST "http://127.0.0.1:8090/setup/arm" >/dev/null
curl -s "http://127.0.0.1:8090/pump/state"
Healthy output has "armed":true, a non-null sessionSalt, "lastStatus":"0x0200 seq 1" (or a later
seq), and "error":null. Anything else → walk §1–§5 to find the first stage that is not DONE.