mirror of
https://github.com/efiten/coredrive-rx.git
synced 2026-11-24 13:27:59 +00:00
fix: stop transmitting into a BLE link that is down
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 a write into a characteristic that no longer exists — an "rf sample incomplete — discarded" for every sampler round, and region asks failing the same way. None of it could reach a radio. discoverDecision takes linkUp and reports 'link-down' ahead of every other state; the monitor tick returns right after rendering it, the RF sampler reschedules without sampling, and region asks stop with them. The discover clock is not advanced while down, so the first tick after the link returns sweeps immediately instead of waiting out an interval. The link state is read from the browser's own device object through a new transport.connected(), not mirrored into a flag of ours. A flag would be stale exactly when it matters, which is the same failure shape as the duplicated default fixed in v1.17.1 — one source per fact. Said once in each direction rather than per tick. A log that just goes quiet is indistinguishable from a crash, an area with nothing to hear, or a feature switched off. Capture, queueing and uploading are untouched: receptions already in hand are still queued and published, and the MQTT link keeps its own reconnect. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HSoyBhrLLtSqLgBMEKMiJV
This commit is contained in:
1 parent
9a8614396b
commit
cc710a3a11
9 files changed
+132
-9
No files matched your search
@@ -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).
|
||||
Generated
+2
-2
@@ -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",
|
||||
|
||||
+1
-1
@@ -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",
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+32
-2
@@ -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);
|
||||
|
||||
+11
-3
@@ -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) };
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
@@ -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');
|
||||
});
|
||||
Reference in new issue
Block a user