Skip to main content

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​

  1. Application.run() resolves the operating mode from CLI arguments.
  2. DeviceMonitor initializes a BleakScanner with a detection callback.
  3. 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() calls system.lock_macbook().
  4. The watchdog loop runs concurrently, monitoring for external screen locks and scanner health.
  5. After a lock event, _handle_screen_lock() pauses the scanner, waits for unlock, and reconnects with retry logic.

Scanner Flow​

  1. DeviceScanner.run() calls BleakScanner.discover() with the configured timeout.
  2. Discovered devices are sorted and formatted with RSSI and distance estimates.
  3. 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.