Fix probe reliability and restore sane missing_after

Two false locks with the phone present: missing_after=30 sat below the
realistic worst-case event gap (beacon counter rotation every 10s times
2-20% duty sampling, with frozen-RSSI dedupe collapsing catches inside
one window), and the connect probe ran while our own discovery
duty-cycling was active, where attempts stall or abort with
le-connection-abort-by-local.

Probe now stops discovery first and retries (2x6s); sample config and
handoff document the sampling math. Local config additionally raises
rssi_lock to -85, where nearby-phone readings were pinning.
This commit is contained in:
Bastian Mäuser committed 2026-11-06 12:17:47 +01:00
1 parent 38087583cf
commit dcf403200a
4 files changed
+105 -44

No files matched your search

+9 -2
View File
@@ -66,9 +66,16 @@ rearms monitoring. See README.md for the full design.
during a pause the phone is both undetectable and unconnectable.
- `DuplicateData = true` in the discovery filter does not bypass the
RSSI dedupe at the `Device1` property level.
- **Sampling math**: the beacon rotates its counter every 10 s, but the
daemon only hears it a fraction of the time (2-20% duty), and frozen
RSSI dedupes catches within one rotation window — event gaps of
25-60 s are routine. `missing_after_secs` must be well above that
(30 s false-locked twice; 60 s is the sane floor).
- `Device1.Connect` is the only reliable "is it actually there" oracle,
and it works whenever the phone advertises. Scan-based checks cannot
distinguish "silent but present" from "gone".
and it works whenever the phone advertises — but connection attempts
stall or abort (`le-connection-abort-by-local`) while our own
discovery duty-cycling is active. Stop discovery before probing and
retry; do not run a single long attempt under scan contention.
- The long-term fix for all of the above is the AdvertisementMonitor
roadmap (controller-offloaded patterns, BlueZ ≥ 5.65 + Experimental).
+12 -8
View File
@@ -6,14 +6,15 @@ device_mac = "AA:BB:CC:DD:EE:FF"
#adapter = "hci0"
# Lock the session after this long without strong evidence of presence.
# Before locking, the daemon additionally probes the phone with a 10s
# connection attempt, which cancels the lock if the phone answers.
# 30s is safe with the companion beacon app (android/): it advertises
# reliably from a foreground service and rotates its payload so the
# daemon always sees fresh evidence. Third-party beacon apps pause
# advertising while the phone sits still — with those, raise this to
# 120s or expect occasional false locks.
missing_after_secs = 30
# Before locking, the daemon additionally probes the phone with a short
# connection attempt (2 tries, discovery paused), which cancels the lock
# if the phone answers. Keep in mind the sampling math: the beacon
# rotates its counter every 10 s, but the daemon only hears it a fraction
# of the time (2-20% duty), so event gaps of 25-60 s are routine when
# the RSSI is frozen — 30 s false-locked in practice. 60 s is a sane
# default with the companion app; third-party beacon apps that pause
# advertising warrant 120 s.
missing_after_secs = 60
# Advertisement updates newer than this count as "recently seen".
# Once updates stall past half of this, the radio steps up to the
@@ -24,6 +25,9 @@ presence_timeout_secs = 15
rssi_floor = -90
# At or below this RSSI the device counts as out of range (dBm).
# Watch your desk readings (sighting reports in the journal): if a
# nearby phone pins at the threshold, raise this — e.g. -85 when
# readings hover around -80.
rssi_lock = -80
# Scan duty cycle per state: radio on for window_ms, then rest until
+40
View File
@@ -4,6 +4,46 @@ Dated notes for picking this project back up: what happened, what was
learned, and which threads are still open. AGENTS.md carries the distilled
rules; this file carries the history behind them.
## 2026-11-06 — two more false locks: sampling math and probe contention
**Symptoms.** Two false locks while the user was present: 11:24:24 (RSSI
-64, strong, phone advertising) and 11:57:30 (RSSI -82 after 10 minutes
pinned at -78..-82).
**Root causes.**
1. *`missing_after = 30` sat below the realistic worst-case event gap.*
The beacon rotates its ServiceData counter every 10 s, but the daemon
only *samples* the radio (2-20% duty). When the RSSI is frozen (BlueZ
dedupe), catches within one rotation window dedupe to nothing, so
event gaps of 25-60 s are routine — e.g. 11:56:32 → 11:57:30 (58 s
gap) reached `missing_after` and triggered a probe.
2. *The connect probe ran while our own discovery duty-cycling was
active.* With the phone in range and advertising, the probe still
stalled for its whole window and BlueZ returned
`le-connection-abort-by-local` — controller contention between our
scan windows and the connection attempt. The probe is the last line
of defense and was not reliable under load.
**Fixes shipped.**
- Probe: 2 attempts × 6 s with a 1 s pause, and discovery is stopped
before probing so the connection attempt gets a clean radio.
- `missing_after = 60` with the companion app (30 s is not enough given
the sampling math; third-party beacons still warrant 120 s).
- Local config: `rssi_lock = -85` — nearby-phone readings pinned at
-78..-82 dBm sat exactly on the -80 threshold and made the weak-signal
path flap constantly. Watch desk readings via the sighting reports.
**Lingering observations.**
- Desk RSSI varies hugely day to day (-44..-65 one day, -52..-86 the
next): interference, phone position, TX power. The `rssi_lock`
threshold needs headroom against this variance.
- `le-connection-abort-by-local` at probe time is now expected only when
our 6 s timeout cancels the attempt (zbus drops the call → bluetoothd
aborts); the retry covers the transient case.
## 2026-11-05 — false-lock debugging session
**Symptoms.** The daemon flipped Present⇄Suspect every minute or so and
+44 -34
View File
@@ -152,7 +152,13 @@ pub async fn run(cfg: Config) -> Result<()> {
// LE connection attempt. A phone in range accepts it even
// when advertisement updates have gone quiet (BlueZ
// dedupes identical RSSI values), so silence alone is not
// proof of absence.
// proof of absence. Stop scanning first: a connection
// attempt does not complete reliably while our own
// discovery duty-cycling contends with it.
if radio_on {
stop_discovery(&adapter).await;
radio_on = false;
}
if probe_reachable(&conn, target.as_ref()).await {
info!("connect probe: phone in range, not locking");
policy.note_event(Instant::now(), None, Some(true));
@@ -207,11 +213,13 @@ struct Target {
path: OwnedObjectPath,
}
/// How long to wait for the connect probe before concluding the phone is
/// gone. The controller scans for the phone's advertisements for this whole
/// window, so slow or intermittent advertising still gets caught; aborting
/// cancels BlueZ's connection attempt.
const PROBE_TIMEOUT: Duration = Duration::from_secs(10);
/// The probe runs while our own discovery duty-cycling is active, and on
/// this controller a connection attempt can stall or be aborted under that
/// contention ("le-connection-abort-by-local"). So: keep each attempt short,
/// retry before concluding absence, and let the caller stop discovery first.
const PROBE_ATTEMPTS: usize = 2;
const PROBE_TIMEOUT: Duration = Duration::from_secs(6);
const PROBE_RETRY_PAUSE: Duration = Duration::from_secs(1);
/// Attempt a short LE connection to the paired phone. Returns true if the
/// device accepted the connection (it is in range); the connection is closed
@@ -227,37 +235,39 @@ async fn probe_reachable(conn: &Connection, target: Option<&Target>) -> bool {
return false;
}
};
match tokio::time::timeout(PROBE_TIMEOUT, proxy.call::<_, _, ()>("Connect", &())).await {
Ok(Ok(())) => {
if let Err(e) = tokio::time::timeout(
Duration::from_secs(1),
proxy.call::<_, _, ()>("Disconnect", &()),
)
.await
{
debug!("probe: disconnect failed: {e:#}");
for attempt in 0..PROBE_ATTEMPTS {
if attempt > 0 {
tokio::time::sleep(PROBE_RETRY_PAUSE).await;
}
match tokio::time::timeout(PROBE_TIMEOUT, proxy.call::<_, _, ()>("Connect", &())).await {
Ok(Ok(())) => {
if let Err(e) = tokio::time::timeout(
Duration::from_secs(1),
proxy.call::<_, _, ()>("Disconnect", &()),
)
.await
{
debug!("probe: disconnect failed: {e:#}");
}
return true;
}
Ok(Err(e)) => {
info!("probe: connect rejected (attempt {attempt}): {e:#}");
}
Err(_) => {
info!("probe: attempt {attempt} timed out");
// Cancel the pending connection attempt so BlueZ does not
// keep paging for the device in the background.
let _ = tokio::time::timeout(
Duration::from_secs(1),
proxy.call::<_, _, ()>("Disconnect", &()),
)
.await;
}
true
}
Ok(Err(e)) => {
info!("probe: connect rejected: {e:#}");
false
}
Err(_) => {
warn!(
"probe: no connection within {}s, treating phone as out of range",
PROBE_TIMEOUT.as_secs()
);
// Cancel the pending connection attempt so BlueZ does not keep
// paging for the device in the background.
let _ = tokio::time::timeout(
Duration::from_secs(1),
proxy.call::<_, _, ()>("Disconnect", &()),
)
.await;
false
}
}
warn!("probe: no connection after {PROBE_ATTEMPTS} attempts, treating phone as out of range");
false
}
/// Extract the MAC from a device object path such as