Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
923d0fc485 | ||
|
|
e4623f33c3 | ||
|
|
dcf403200a | ||
|
|
38087583cf |
No files matched your search
@@ -66,9 +66,16 @@ rearms monitoring. See README.md for the full design.
|
|||||||
during a pause the phone is both undetectable and unconnectable.
|
during a pause the phone is both undetectable and unconnectable.
|
||||||
- `DuplicateData = true` in the discovery filter does not bypass the
|
- `DuplicateData = true` in the discovery filter does not bypass the
|
||||||
RSSI dedupe at the `Device1` property level.
|
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,
|
- `Device1.Connect` is the only reliable "is it actually there" oracle,
|
||||||
and it works whenever the phone advertises. Scan-based checks cannot
|
and it works whenever the phone advertises — but connection attempts
|
||||||
distinguish "silent but present" from "gone".
|
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
|
- The long-term fix for all of the above is the AdvertisementMonitor
|
||||||
roadmap (controller-offloaded patterns, BlueZ ≥ 5.65 + Experimental).
|
roadmap (controller-offloaded patterns, BlueZ ≥ 5.65 + Experimental).
|
||||||
|
|
||||||
|
|||||||
Generated
+1
-1
@@ -115,7 +115,7 @@ checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "btle-proximity"
|
name = "btle-proximity"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "btle-proximity"
|
name = "btle-proximity"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.85"
|
rust-version = "1.85"
|
||||||
resolver = "3"
|
resolver = "3"
|
||||||
|
|||||||
@@ -59,7 +59,57 @@ proof of absence.
|
|||||||
factor; OS-generated advertisements (Fast Pair, Continuity) are too
|
factor; OS-generated advertisements (Fast Pair, Continuity) are too
|
||||||
intermittent to rely on.
|
intermittent to rely on.
|
||||||
|
|
||||||
## Build & install
|
## Install (Debian packages)
|
||||||
|
|
||||||
|
Prebuilt `.deb` packages for amd64 and arm64 are attached to the
|
||||||
|
[releases](https://git.netz.org/bmaeuser/btle-proximity/releases) and
|
||||||
|
available through the Gitea Debian package registry.
|
||||||
|
|
||||||
|
### Option A — apt repository (recommended, delivers updates)
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sudo install -d /etc/apt/keyrings
|
||||||
|
curl -fsSL https://git.netz.org/api/packages/bmaeuser/debian/repository.key \
|
||||||
|
| sudo gpg --dearmor -o /etc/apt/keyrings/btle-proximity.gpg
|
||||||
|
echo "deb [signed-by=/etc/apt/keyrings/btle-proximity.gpg] https://git.netz.org/api/packages/bmaeuser/debian stable main" \
|
||||||
|
| sudo tee /etc/apt/sources.list.d/btle-proximity.list
|
||||||
|
sudo apt update
|
||||||
|
sudo apt install btle-proximity
|
||||||
|
```
|
||||||
|
|
||||||
|
Future releases land with the usual `apt upgrade`.
|
||||||
|
|
||||||
|
### Option B — manual .deb from a release
|
||||||
|
|
||||||
|
```sh
|
||||||
|
curl -fLO https://git.netz.org/bmaeuser/btle-proximity/releases/download/v0.1.0/btle-proximity_0.1.0-1_amd64.deb
|
||||||
|
sudo apt install ./btle-proximity_0.1.0-1_amd64.deb
|
||||||
|
# arm64: btle-proximity_0.1.0-1_arm64.deb (fully static musl binary)
|
||||||
|
```
|
||||||
|
|
||||||
|
Both options install the binary to `/usr/bin`, the user unit to
|
||||||
|
`/usr/lib/systemd/user/`, a man page and the config example to
|
||||||
|
`/usr/share/doc/btle-proximity/`.
|
||||||
|
|
||||||
|
### Per-user setup
|
||||||
|
|
||||||
|
```sh
|
||||||
|
mkdir -p ~/.config/btle-proximity
|
||||||
|
cp /usr/share/doc/btle-proximity/examples/config.toml ~/.config/btle-proximity/config.toml
|
||||||
|
btle-proximity list-paired # pick your phone's MAC, edit config.toml
|
||||||
|
systemctl --user enable --now btle-proximity
|
||||||
|
journalctl --user -u btle-proximity -f
|
||||||
|
```
|
||||||
|
|
||||||
|
Coming from the `cargo install` flow? Remove that copy first
|
||||||
|
(`cargo uninstall btle-proximity`) and the unit copied to
|
||||||
|
`~/.config/systemd/user/`, so neither shadows the package.
|
||||||
|
|
||||||
|
The Android app (`btle-proximity-beacon-release.apk`) is attached to the
|
||||||
|
same releases; Obtainium can track the repo for automatic updates — see
|
||||||
|
`android/README.md`.
|
||||||
|
|
||||||
|
## Build & install from source
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cargo install --path . # installs to ~/.cargo/bin
|
cargo install --path . # installs to ~/.cargo/bin
|
||||||
@@ -83,24 +133,15 @@ RUST_LOG=debug btle-proximity --dry-run
|
|||||||
```
|
```
|
||||||
|
|
||||||
Tune `rssi_lock` / `presence_timeout_secs` / `missing_after_secs` to your
|
Tune `rssi_lock` / `presence_timeout_secs` / `missing_after_secs` to your
|
||||||
room. Then install the user service (runs inside your session, Type=notify):
|
room.
|
||||||
|
|
||||||
### Debian package
|
### Building the Debian packages
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cargo install cargo-deb --version 2.12.1 # 3.x needs rustc > 1.85
|
cargo install cargo-deb --version 2.12.1 # 3.x needs rustc > 1.85
|
||||||
cargo deb # -> target/debian/btle-proximity_*.deb
|
cargo deb # -> target/debian/btle-proximity_*.deb
|
||||||
sudo apt install ./target/debian/btle-proximity_0.1.0-1_amd64.deb
|
|
||||||
systemctl --user enable --now btle-proximity
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Installs the binary to `/usr/bin`, the user unit to
|
|
||||||
`/usr/lib/systemd/user/`, a man page and the config example to
|
|
||||||
`/usr/share/doc/btle-proximity/`. Per-user config still lives at
|
|
||||||
`~/.config/btle-proximity/config.toml`. If you previously installed via
|
|
||||||
`cargo install`, remove that copy (`cargo uninstall btle-proximity`) to
|
|
||||||
avoid PATH shadowing.
|
|
||||||
|
|
||||||
#### arm64 (aarch64)
|
#### arm64 (aarch64)
|
||||||
|
|
||||||
Cross-built as a **static musl binary**, so no cross-gcc/sysroot is needed
|
Cross-built as a **static musl binary**, so no cross-gcc/sysroot is needed
|
||||||
@@ -111,7 +152,7 @@ an isolated rustup under `/tmp` so the distro cargo is not touched:
|
|||||||
export RUSTUP_HOME=/tmp/rustup CARGO_HOME=/tmp/rustup-cargo PATH=/tmp/rustup-cargo/bin:$PATH
|
export RUSTUP_HOME=/tmp/rustup CARGO_HOME=/tmp/rustup-cargo PATH=/tmp/rustup-cargo/bin:$PATH
|
||||||
curl -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain 1.85.0 --profile minimal --no-modify-path
|
curl -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain 1.85.0 --profile minimal --no-modify-path
|
||||||
rustup target add aarch64-unknown-linux-musl
|
rustup target add aarch64-unknown-linux-musl
|
||||||
mkdir -p /tmp/lld-bin && ln -sf "$(rustc --print rustlib-dir)"/x86_64-unknown-linux-gnu/bin/rust-lld /tmp/lld-bin/ld.lld
|
mkdir -p /tmp/lld-bin && ln -sf "$(rustc --print sysroot)"/lib/rustlib/x86_64-unknown-linux-gnu/bin/rust-lld /tmp/lld-bin/ld.lld
|
||||||
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=/tmp/lld-bin/ld.lld
|
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=/tmp/lld-bin/ld.lld
|
||||||
~/.cargo/bin/cargo-deb --target aarch64-unknown-linux-musl --no-strip
|
~/.cargo/bin/cargo-deb --target aarch64-unknown-linux-musl --no-strip
|
||||||
# -> target/aarch64-unknown-linux-musl/debian/btle-proximity_*_arm64.deb
|
# -> target/aarch64-unknown-linux-musl/debian/btle-proximity_*_arm64.deb
|
||||||
@@ -122,6 +163,10 @@ linker, and the host `strip` cannot read aarch64 objects.
|
|||||||
|
|
||||||
### Manual install (dev flow)
|
### Manual install (dev flow)
|
||||||
|
|
||||||
|
The unit in `systemd/` runs the cargo-installed binary
|
||||||
|
(`%h/.cargo/bin/btle-proximity`); the packaged variant under
|
||||||
|
`packaging/` points at `/usr/bin` instead.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cp systemd/btle-proximity.service ~/.config/systemd/user/
|
cp systemd/btle-proximity.service ~/.config/systemd/user/
|
||||||
systemctl --user daemon-reload
|
systemctl --user daemon-reload
|
||||||
|
|||||||
+12
-8
@@ -6,14 +6,15 @@ device_mac = "AA:BB:CC:DD:EE:FF"
|
|||||||
#adapter = "hci0"
|
#adapter = "hci0"
|
||||||
|
|
||||||
# Lock the session after this long without strong evidence of presence.
|
# Lock the session after this long without strong evidence of presence.
|
||||||
# Before locking, the daemon additionally probes the phone with a 10s
|
# Before locking, the daemon additionally probes the phone with a short
|
||||||
# connection attempt, which cancels the lock if the phone answers.
|
# connection attempt (2 tries, discovery paused), which cancels the lock
|
||||||
# 30s is safe with the companion beacon app (android/): it advertises
|
# if the phone answers. Keep in mind the sampling math: the beacon
|
||||||
# reliably from a foreground service and rotates its payload so the
|
# rotates its counter every 10 s, but the daemon only hears it a fraction
|
||||||
# daemon always sees fresh evidence. Third-party beacon apps pause
|
# of the time (2-20% duty), so event gaps of 25-60 s are routine when
|
||||||
# advertising while the phone sits still — with those, raise this to
|
# the RSSI is frozen — 30 s false-locked in practice. 60 s is a sane
|
||||||
# 120s or expect occasional false locks.
|
# default with the companion app; third-party beacon apps that pause
|
||||||
missing_after_secs = 30
|
# advertising warrant 120 s.
|
||||||
|
missing_after_secs = 60
|
||||||
|
|
||||||
# Advertisement updates newer than this count as "recently seen".
|
# Advertisement updates newer than this count as "recently seen".
|
||||||
# Once updates stall past half of this, the radio steps up to the
|
# Once updates stall past half of this, the radio steps up to the
|
||||||
@@ -24,6 +25,9 @@ presence_timeout_secs = 15
|
|||||||
rssi_floor = -90
|
rssi_floor = -90
|
||||||
|
|
||||||
# At or below this RSSI the device counts as out of range (dBm).
|
# 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
|
rssi_lock = -80
|
||||||
|
|
||||||
# Scan duty cycle per state: radio on for window_ms, then rest until
|
# Scan duty cycle per state: radio on for window_ms, then rest until
|
||||||
|
|||||||
@@ -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
|
learned, and which threads are still open. AGENTS.md carries the distilled
|
||||||
rules; this file carries the history behind them.
|
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
|
## 2026-11-05 — false-lock debugging session
|
||||||
|
|
||||||
**Symptoms.** The daemon flipped Present⇄Suspect every minute or so and
|
**Symptoms.** The daemon flipped Present⇄Suspect every minute or so and
|
||||||
|
|||||||
+44
-34
@@ -152,7 +152,13 @@ pub async fn run(cfg: Config) -> Result<()> {
|
|||||||
// LE connection attempt. A phone in range accepts it even
|
// LE connection attempt. A phone in range accepts it even
|
||||||
// when advertisement updates have gone quiet (BlueZ
|
// when advertisement updates have gone quiet (BlueZ
|
||||||
// dedupes identical RSSI values), so silence alone is not
|
// 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 {
|
if probe_reachable(&conn, target.as_ref()).await {
|
||||||
info!("connect probe: phone in range, not locking");
|
info!("connect probe: phone in range, not locking");
|
||||||
policy.note_event(Instant::now(), None, Some(true));
|
policy.note_event(Instant::now(), None, Some(true));
|
||||||
@@ -207,11 +213,13 @@ struct Target {
|
|||||||
path: OwnedObjectPath,
|
path: OwnedObjectPath,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// How long to wait for the connect probe before concluding the phone is
|
/// The probe runs while our own discovery duty-cycling is active, and on
|
||||||
/// gone. The controller scans for the phone's advertisements for this whole
|
/// this controller a connection attempt can stall or be aborted under that
|
||||||
/// window, so slow or intermittent advertising still gets caught; aborting
|
/// contention ("le-connection-abort-by-local"). So: keep each attempt short,
|
||||||
/// cancels BlueZ's connection attempt.
|
/// retry before concluding absence, and let the caller stop discovery first.
|
||||||
const PROBE_TIMEOUT: Duration = Duration::from_secs(10);
|
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
|
/// 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
|
/// 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;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
match tokio::time::timeout(PROBE_TIMEOUT, proxy.call::<_, _, ()>("Connect", &())).await {
|
for attempt in 0..PROBE_ATTEMPTS {
|
||||||
Ok(Ok(())) => {
|
if attempt > 0 {
|
||||||
if let Err(e) = tokio::time::timeout(
|
tokio::time::sleep(PROBE_RETRY_PAUSE).await;
|
||||||
Duration::from_secs(1),
|
}
|
||||||
proxy.call::<_, _, ()>("Disconnect", &()),
|
match tokio::time::timeout(PROBE_TIMEOUT, proxy.call::<_, _, ()>("Connect", &())).await {
|
||||||
)
|
Ok(Ok(())) => {
|
||||||
.await
|
if let Err(e) = tokio::time::timeout(
|
||||||
{
|
Duration::from_secs(1),
|
||||||
debug!("probe: disconnect failed: {e:#}");
|
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
|
/// Extract the MAC from a device object path such as
|
||||||
|
|||||||
Reference in new issue
Block a user