first commit

This commit is contained in:
Bastian Mäuser committed 2026-11-05 14:15:31 +01:00
commit 2dc85ba948
1 file changed
+126
+126
View File
@@ -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.