diff --git a/docs/releases/v1.18.0.md b/docs/releases/v1.18.0.md new file mode 100644 index 0000000..3f0517d --- /dev/null +++ b/docs/releases/v1.18.0.md @@ -0,0 +1,33 @@ +# CoreDrive RX v1.18.0 + +When the companion link drops, the app now waits for it instead of transmitting into it. + +## What's new + +- **Nothing is written to a dead BLE link.** The 2026-09-13 log ends with two and a half + minutes of `discover send failed: GATT Server is disconnected` — one sweep every 30 + seconds, each one a write into a characteristic that no longer exists — plus an + `rf sample incomplete — discarded` for every sampler round, and region asks failing the + same way. All three now stand down while the link is down and resume on the first tick + after it returns. + + The discover clock is deliberately not advanced while down, so the sweep fires + immediately when the companion is back rather than waiting out an interval first. + +- **The link state is read, not remembered.** `transport.connected()` reports what the + browser's own device object says. A flag of ours would be stale exactly when it + matters, since the link drops without telling anyone first. + +- **The log says it once, both ways.** `companion link down — discover, RF sampling and + region asks held until it is back`, and the matching line when it returns. A log that + simply goes quiet is indistinguishable from a crash, an empty area, or a feature + switched off; this names it. The Home screen says `Discover paused — companion link + down` for as long as it lasts. + +## Upgrade notes + +- **Service-worker cache bumped to `coredrive-rx-v49`.** +- No config or contract change. Capture, queueing and uploading are untouched: receptions + that already arrived are still queued and published, and the MQTT link has its own + reconnect. +- Tests: 270 pass (+5). diff --git a/package-lock.json b/package-lock.json index e367213..b616535 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "coredrive-rx", - "version": "1.17.1", + "version": "1.18.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coredrive-rx", - "version": "1.17.1", + "version": "1.18.0", "license": "GPL-3.0-or-later", "dependencies": { "@noble/ed25519": "^3.2.0", diff --git a/package.json b/package.json index 7dbe538..b66a12d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coredrive-rx", - "version": "1.17.1", + "version": "1.18.0", "private": false, "license": "GPL-3.0-or-later", "type": "module", diff --git a/public/sw.js b/public/sw.js index 8e6920e..00ac3af 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,7 +1,7 @@ // Minimal service worker: makes the app installable and lets the shell load // offline. Network-first (always fresh online), cache fallback (offline). // Only the static app assets go through fetch; MQTT runs over WSS (not cached). -const CACHE = 'coredrive-rx-v48'; +const CACHE = 'coredrive-rx-v49'; // config.json is deliberately NEVER cached, in either direction. It is // per-deployment runtime config, and a stale copy is worse than none: flags added diff --git a/src/app.js b/src/app.js index a7cf6e2..4d1c6df 100644 --- a/src/app.js +++ b/src/app.js @@ -68,6 +68,9 @@ const state = { lastHeardAt: null, lastFireAt: 0, tick: null, // RF environment sampler rfTimer: null, lastRfSample: null, rfGen: 0, + // linkQuiet: true while the BLE link is down, so the pause and the resume are each + // said once instead of once per tick. + linkQuiet: false, // Region discovery (ANON_REQ_TYPE_REGIONS) — purely reception-driven: a repeater is // asked while we are hearing it, never on a clock (src/regionsched.js holds the // rules). supported reflects the FIRMWARE_VER_CODE gate. @@ -465,6 +468,7 @@ function onRegionsFrame(dv) { function renderDiscoverStatus(dec) { const el = els('discStatus'); + if (dec.state === 'link-down') { el.textContent = '🎯 Discover paused — companion link down'; return; } if (!state.connected || dec.state === 'paused') { el.textContent = ''; return; } if (dec.state === 'backoff') { el.textContent = '🎯 Backoff (verkeer actief)'; return; } el.textContent = dec.secs > 0 ? '🎯 Discover actief — volgende in ' + dec.secs + 's' : '🎯 Discover actief'; @@ -585,14 +589,33 @@ async function retryConfig() { return true; } +// bleLinkUp asks the transport, which asks the browser. Not a flag of ours: the link +// drops without warning and a mirrored copy is stale exactly when it matters. +function bleLinkUp() { + return !!state.transport && state.transport.connected(); +} + +// noteLinkState says once that the radio work has stopped, and once that it resumed. +// Without it a dropped link reads as a log that simply goes quiet — which is the same +// thing as an app that has crashed, an area with no traffic, or a feature turned off. +function noteLinkState(linkUp) { + if (linkUp === state.linkQuiet) return; // linkQuiet holds the INVERSE, so this is a change + state.linkQuiet = !linkUp; + if (!linkUp) dbg('companion link down — discover, RF sampling and region asks held until it is back', 'no'); + else dbg('companion link back — discover, RF sampling and region asks resumed', 'ok'); +} + // --- Per-second monitor tick: drives auto-discover, the SNR-meter decay, and the // time-relative labels (last-heard / last-upload / rate / discover countdown). Runs only // while connected. function monitorTick() { const now = Date.now(); - const dec = discoverDecision(now, state.lastHeardAt, state.lastFireAt, state.paused); - if (dec.fire) { fireDiscover(now); renderDiscoverStatus(discoverDecision(now, state.lastHeardAt, state.lastFireAt, state.paused)); } + const linkUp = bleLinkUp(); + noteLinkState(linkUp); + const dec = discoverDecision(now, state.lastHeardAt, state.lastFireAt, state.paused, linkUp); + if (dec.fire) { fireDiscover(now); renderDiscoverStatus(discoverDecision(now, state.lastHeardAt, state.lastFireAt, state.paused, linkUp)); } else renderDiscoverStatus(dec); + if (!linkUp) return; // nothing below this can reach the radio // The only region-discovery work left on the clock: free a timed-out ask, so the // slot is available to the very next repeater heard instead of one tick later. The // asking itself happens on receptions (noteRepeaterHeard), never on a timer. @@ -1142,6 +1165,13 @@ function startRfSampler() { const tick = async () => { if (!state.transport || !state.companionPubkey) return; + // A sample taken over a dead link is three writes that throw and one + // "rf sample incomplete — discarded" line. Reschedule and wait for the link. + if (!bleLinkUp()) { + if (state.rfGen !== myGen) return; + state.rfTimer = setTimeout(tick, nextSampleDelay(state.motion ? state.motion.paused : false)); + return; + } const fix = currentFix(); if (fix) { const core = await ask(STATS_CORE); diff --git a/src/monitor.js b/src/monitor.js index a3cd0bf..d64ae3d 100644 --- a/src/monitor.js +++ b/src/monitor.js @@ -11,13 +11,21 @@ export const DISCOVER_BACKOFF_MS = 15000; // silence required after organic tra // discoverDecision decides, on each tick, whether to fire a discover sweep now and // what status to surface. `lastHeardAt` = ms of the last ORGANIC reception (an // overheard forwarder/advert — NOT our own discover response); null if none yet. -// `lastFireAt` = ms of the last sweep (0 = never). Returns: -// { fire, state: 'paused'|'backoff'|'active', secs } +// `lastFireAt` = ms of the last sweep (0 = never). `linkUp` is whether the BLE link is +// actually up. Returns: +// { fire, state: 'link-down'|'paused'|'backoff'|'active', secs } // where `secs` is the backoff seconds remaining (backoff) or seconds until the next // sweep (active). On fire the caller sets lastFireAt = now. -export function discoverDecision(now, lastHeardAt, lastFireAt, paused, opts = {}) { +// +// link-down comes FIRST because a sweep while the companion is gone is not a sweep: the +// write throws on a dead characteristic. A field log (2026-09-13, 01:57 onward) shows +// 2.5 minutes of "discover send failed: GATT Server is disconnected" every 30 seconds, +// one per tick, with nothing else able to happen anyway. lastFireAt is deliberately not +// advanced while down, so the first tick after the link returns sweeps immediately. +export function discoverDecision(now, lastHeardAt, lastFireAt, paused, linkUp = true, opts = {}) { const interval = opts.intervalMs ?? DISCOVER_INTERVAL_MS; const backoff = opts.backoffMs ?? DISCOVER_BACKOFF_MS; + if (!linkUp) return { fire: false, state: 'link-down', secs: 0 }; if (paused) return { fire: false, state: 'paused', secs: 0 }; if (lastHeardAt != null && now - lastHeardAt < backoff) { return { fire: false, state: 'backoff', secs: Math.ceil((backoff - (now - lastHeardAt)) / 1000) }; diff --git a/src/transport.js b/src/transport.js index 074e707..5503c2e 100644 --- a/src/transport.js +++ b/src/transport.js @@ -38,6 +38,14 @@ export class WebBluetoothTransport { return true; } + // connected() reports whether the GATT link is actually up right now. Read from the + // browser's own device object rather than mirrored into a flag of ours: this is + // exactly the state that changes without us being told first, and a stale copy sends + // writes into a dead characteristic. + connected() { + return !!(this.device && this.device.gatt && this.device.gatt.connected); + } + // _setup (re)establishes GATT, characteristics and notifications. Re-runnable // on reconnect (the characteristic objects are recreated each time). async _setup() { diff --git a/test/monitor.test.mjs b/test/monitor.test.mjs index dd5b96c..40ac801 100644 --- a/test/monitor.test.mjs +++ b/test/monitor.test.mjs @@ -76,3 +76,33 @@ test('capture-rate window drops entries older than 60 s', () => { // --- Region discovery cadence (deliberately independent of the stationary pause) --- + +// --- Link-down ---------------------------------------------------------------- +// Field log 2026-09-13, 01:57:54 onward: the BLE link dropped and the sweep kept firing +// every 30s for two and a half minutes, each one logging +// "discover send failed: GATT Server is disconnected". A sweep over a dead link is not +// a sweep, so the decision has to know. + +test('discoverDecision does not sweep while the link is down', () => { + const d = discoverDecision(1_000_000, null, 0, false, false); + assert.equal(d.fire, false); + assert.equal(d.state, 'link-down'); +}); + +test('link-down outranks both the pause and a sweep that is due', () => { + // Due (lastFireAt 0) and stationary at the same time: neither may turn into a write. + assert.equal(discoverDecision(1_000_000, null, 0, true, false).state, 'link-down'); + assert.equal(discoverDecision(1_000_000, null, 0, false, false).fire, false); +}); + +test('the first tick after the link returns sweeps immediately', () => { + // lastFireAt is not advanced while down, so nothing has to be waited out afterwards. + const now = 1_000_000; + const lastFire = now - DISCOVER_INTERVAL_MS * 5; + assert.equal(discoverDecision(now, null, lastFire, false, false).fire, false); + assert.equal(discoverDecision(now, null, lastFire, false, true).fire, true); +}); + +test('linkUp defaults to true, so an existing caller keeps sweeping', () => { + assert.equal(discoverDecision(1_000_000, null, 0, false).fire, true); +}); diff --git a/test/transport.test.mjs b/test/transport.test.mjs index c873986..6fcd216 100644 --- a/test/transport.test.mjs +++ b/test/transport.test.mjs @@ -53,3 +53,17 @@ test('send rejects when not connected', async () => { const t = new WebBluetoothTransport(); await assert.rejects(t.send(new Uint8Array([1])), /not connected/); }); + +// connected() is read straight off the browser's device object rather than mirrored +// into a flag: the link drops without warning, and a stale copy is what sends writes +// into a dead characteristic. +test('connected() reports the live GATT state, and false before there is a device', () => { + const t = new WebBluetoothTransport(); + assert.equal(t.connected(), false, 'no device yet'); + t.device = { gatt: { connected: true } }; + assert.equal(t.connected(), true); + t.device.gatt.connected = false; + assert.equal(t.connected(), false); + t.device = { }; + assert.equal(t.connected(), false, 'a device with no gatt is not connected'); +});