From 2dc85ba9485a49e4b9b309f1a1c728ef294f794d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=A4user?= Date: Thu, 5 Nov 2026 14:15:31 +0100 Subject: [PATCH] first commit --- README.md | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..56b970c --- /dev/null +++ b/README.md @@ -0,0 +1,126 @@ +# btle-proximity + +A Linux daemon that locks your desktop session when your phone walks away from +Bluetooth range — and barely spends any power doing it. + +Written in Rust, talks to BlueZ over D-Bus, locks via logind. No unlock: the +phone returning only rearms monitoring. + +## How it works + +BLE scanning runs in the Bluetooth controller firmware, so the CPU sleeps; +the real power lever is the radio duty cycle (`scan_window / scan_interval`). +The daemon adapts it to a small state machine: + +``` + fresh + strong sighting + Bootstrapping ───────────────► Present low duty (~2%: 100ms/5s) + │ never locks │ + │ │ stale or weak signal + ▼ ▼ + (idle) ◄── sighting ──── Suspect burst (200ms/1s) + │ + │ still no good evidence + │ for missing_after_secs + ▼ + Locked ───────► lock via logind, + (waits for the phone at 100ms/30s) +``` + +- **Present**: fresh advertisement updates with RSSI above `rssi_lock`, or an + active connection to the device. +- **Suspect**: high-duty re-check so a few dropped beacons don't lock you out. +- **Bootstrapping**: the daemon never locks before it has seen the phone at + least once, so you can boot with the phone in another room. +- **Locked**: no unlock; the next sighting just returns to Present. + +Locking goes through `loginctl lock-session` (works with GNOME, KDE, Sway, +...), configurable via `lock_command`. + +## Requirements + +- Linux with BlueZ ≥ 5.50 (`bluetoothd` running, adapter powered) +- The phone must be **paired** with the desktop. Pairing gives the daemon a + stable identity address (BlueZ resolves the phone's rotating random address + against the bonding keys) and is the foundation for authenticated GATT in + the future. +- The phone must **advertise regularly**. A smartphone doesn't advertise on + its own — run a BLE beacon app (e.g. iBeacon/Eddystone beacons from + F-Droid/Play Store, ~1 Hz). This is the single most important reliability + factor; OS-generated advertisements (Fast Pair, Continuity) are too + intermittent to rely on. + +## Build & install + +```sh +cargo install --path . # installs to ~/.cargo/bin +mkdir -p ~/.config/btle-proximity +cp config.toml.example ~/.config/btle-proximity/config.toml +btle-proximity list-paired # pick your phone's MAC, edit config.toml +``` + +Test without locking anything, walk around with the phone and watch: + +```sh +RUST_LOG=debug btle-proximity --dry-run +``` + +Tune `rssi_lock` / `presence_timeout_secs` / `missing_after_secs` to your +room. Then install the user service (runs inside your session, Type=notify): + +```sh +cp systemd/btle-proximity.service ~/.config/systemd/user/ +systemctl --user daemon-reload +systemctl --user enable --now btle-proximity +``` + +## Configuration + +See `config.toml.example`; every knob is documented there. Highlights: + +| key | default | meaning | +| ---------------------- | ------- | ------------------------------------------ | +| `device_mac` | — | identity MAC of the paired phone | +| `missing_after_secs` | 30 | absence confirmation before locking | +| `presence_timeout_secs`| 15 | how fresh an update must be to count | +| `rssi_floor` / `rssi_lock` | -90/-80 | discovery floor / out-of-range threshold | +| `duty.*` | see file| per-state scan window/interval | + +## Power notes + +- Discovery is duty-cycled by toggling BlueZ discovery: radio on ~2% of the + time while the phone is present, burst when absent is suspected, near-idle + while locked. +- Roadmap, in order of power payoff: + 1. **AdvertisementMonitor** (BlueZ `org.bluez.AdvertisementMonitor1`, + kernel-offloaded on supported controllers): pattern-match the phone's + beacon payload in the controller, wake the host only on Found/Lost + transitions. Needs BlueZ ≥ 5.65 and `Experimental = true` in + `/etc/bluetooth/main.conf` on many distros. + 2. mgmt-socket based scanning with exact controller scan parameters. + +## Known trade-offs (v1) + +- Presence is inferred from advertisement property updates. If BlueZ + suppresses updates for identical RSSI values (its property cache dedupes), + a *perfectly* stationary phone in a perfectly stationary room can look + absent; the default `presence_timeout_secs = 15` plus real-world RSSI + jitter makes this unlikely, but `--dry-run` on your hardware is the + arbiter. The AdvertisementMonitor roadmap removes this class of problem. +- While the screen is locked the daemon keeps low-duty scanning to notice + the phone's return (rearming only; it never unlocks). + +## Roadmap: unlock with the phone + +Planned v2 (companion app on the phone, GATT service on the desktop): + +1. Daemon exposes an encrypted GATT service (LE Secure Connections bonding). +2. On approach, the desktop sends the phone a fresh nonce. +3. The phone asks for fingerprint (Android `BiometricPrompt`), signs the + nonce with a Keystore-bound key whose use is gated on biometric + authentication, and returns the signature. +4. The daemon verifies the enrolled public key and calls + `loginctl unlock-session`. + +Replay-safe (nonce), no secrets over the air, and the unlock capability is +worth exactly as much as your phone's lock screen.