Architecture Overview
B.R.I.O.S. is organized as a modular Python package with clearly separated concerns. This page describes the high-level architecture and how the components interact.
Package Structureβ
brios/
βββ __init__.py # Version resolution
βββ __main__.py # Entry point for `python -m brios`
βββ cli.py # CLI argument parsing and Application orchestrator
βββ core/
βββ config.py # Environment variable loading and constants
βββ monitor.py # Real-time BLE device monitoring
βββ scanner.py # One-time BLE device discovery
βββ service.py # Background daemon lifecycle management
βββ system.py # macOS system integration (lock, screen state)
βββ utils.py # Shared utilities, colors, signal processing
Component Diagramβ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CLI (cli.py) β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Application β β
β β β’ Parses command-line arguments β β
β β β’ Delegates to ServiceManager, DeviceScanner, or Monitor β β
β ββββββββββββ¬βββββββββββββββββββ¬βββββββββββββββββββ¬βββββββββββββββ β
β β β β β
β ββββββββββΌβββββββ βββββββββΌβββββββββ ββββββββΌββββββββββββ β
β β ServiceManagerβ β DeviceScanner β β DeviceMonitor β β
β β (service.py) β β (scanner.py) β β (monitor.py) β β
β β β β β β β β
β β β’ start/stop β β β’ BLE scan β β β’ RSSI tracking β β
β β β’ restart β β β’ Device list β β β’ Distance calc β β
β β β’ status β β β’ Distance est β β β’ Alert logic β β
β β β’ PID mgmt β β β β β’ Lock handling β β
β βββββββββββββββββ ββββββββββββββββββ β β’ Watchdog β β
β ββββββββ¬ββββββββββββ β
β β β
β βββββββββββββββββββββΌβββββββββββββ β
β β β β β
β ββββββββββΌβββββββ ββββββββββΌβββββββ β β
β β system.py β β utils.py β β β
β β β β β β β
β β β’ lock_macbookβ β β’ estimate_ β β β
β β β’ is_screen_ β β distance β β β
β β locked β β β’ smooth_rssi β β β
β βββββββββββββββββ β β’ Colors β β β
β β β’ Flags β β β
β βββββββββββββββββ β β
β ββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββΌββββββ
β config.py β
β β
β β’ .env β
β loading β
β β’ Constantsβ
ββββββββββββββ
Data Flowβ
Monitoring Flowβ
Application.run()resolves the operating mode from CLI arguments.DeviceMonitorinitializes aBleakScannerwith a detection callback.- For each BLE advertisement received:
_detection_callback()filters for the target device._process_signal()appends the RSSI to a rolling buffer and computes a smoothed value.estimate_distance()converts the smoothed RSSI to a distance estimate using the Log-Distance Path Loss model.- If the distance exceeds the threshold,
_trigger_out_of_range_alert()callssystem.lock_macbook().
- The watchdog loop runs concurrently, monitoring for external screen locks and scanner health.
- After a lock event,
_handle_screen_lock()pauses the scanner, waits for unlock, and reconnects with retry logic.
Scanner Flowβ
DeviceScanner.run()callsBleakScanner.discover()with the configured timeout.- Discovered devices are sorted and formatted with RSSI and distance estimates.
- Results are printed to the terminal.
Key Design Decisionsβ
Signal Smoothingβ
Raw RSSI values fluctuate significantly due to multipath propagation, reflections, and environmental interference. B.R.I.O.S. uses a rolling mean over a configurable window (SAMPLE_WINDOW) to stabilize readings before distance calculations.
Grace Periodβ
After the screen is unlocked (either by the user or because the device returned), a grace period (GRACE_PERIOD_SECONDS) suppresses re-triggering. This prevents rapid lock/unlock cycles when the device is near the threshold boundary.
Lock Loop Protectionβ
If the system detects LOCK_LOOP_THRESHOLD lock events within LOCK_LOOP_WINDOW seconds, it pauses monitoring for LOCK_LOOP_PENALTY seconds. This prevents pathological behavior at the threshold boundary.
Bleak Monkeypatchβ
On macOS, the retrieveAddressForPeripheral_ CoreBluetooth API can return None for some peripherals, causing Bleak to crash with an AttributeError. B.R.I.O.S. applies a runtime patch to handle this gracefully.