From 4aff3f6587791e20eb95b901e167b47554ebaa26 Mon Sep 17 00:00:00 2001 From: Mohanad Tarek <241946041+mohanvd@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:06:18 +0200 Subject: [PATCH 1/2] feat: the unit finds the broker by a UDP beacon from the launcher The launcher broadcasts a signed beacon once a second on UDP port 18830. The unit listens for it after Wi-Fi connects and uses the sender's address as the MQTT host, so the laptop's IP address may change. MQTT_HOST in secrets.h is now an optional fallback. When the beacon moves to another address the unit reconnects there, between cycles. Firmware 0.3.0. --- README.md | 2 +- docs/contract.md | 2 + docs/firmware.md | 40 ++++- docs/setup.md | 9 +- firmware/CMakeLists.txt | 4 +- firmware/portable/beacon.c | 140 +++++++++++++++++ firmware/portable/beacon.h | 62 ++++++++ firmware/secrets.h.example | 9 +- firmware/src/broker_find.c | 92 +++++++++++ firmware/src/broker_find.h | 29 ++++ firmware/src/config.h | 12 ++ firmware/src/lwipopts.h | 2 + firmware/src/main.c | 7 + firmware/src/mqtt_link.c | 130 +++++++++++++-- firmware/tests/CMakeLists.txt | 1 + firmware/tests/check_contract.py | 23 ++- firmware/tests/test_portable.c | 192 ++++++++++++++++++++++- invigil/__main__.py | 19 ++- invigil/beacon.py | 119 ++++++++++++++ invigil/tests/test_beacon.py | 46 ++++++ invigil/tests/test_monitor_and_screen.py | 1 + server/fake_publisher.py | 2 +- 22 files changed, 907 insertions(+), 36 deletions(-) create mode 100644 firmware/portable/beacon.c create mode 100644 firmware/portable/beacon.h create mode 100644 firmware/src/broker_find.c create mode 100644 firmware/src/broker_find.h create mode 100644 invigil/beacon.py create mode 100644 invigil/tests/test_beacon.py diff --git a/README.md b/README.md index db87a13..78bd3d5 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ You can run the whole laptop side with fake data and no hardware. These commands ### One command for the real unit -When the unit is on the network, the launcher runs the whole laptop side: Mosquitto with `server/mosquitto.conf`, the logger, and the dashboard on `http://127.0.0.1:8080`. It waits for the unit, opens the dashboard in Live mode and shows a status screen (broker, logger session, unit online, last status, message rates, laptop clock offset). +When the unit is on the network, the launcher runs the whole laptop side: Mosquitto with `server/mosquitto.conf`, the broker beacon the unit uses to [find the laptop](docs/firmware.md#finding-the-broker), the logger, and the dashboard on `http://127.0.0.1:8080`. It waits for the unit, opens the dashboard in Live mode and shows a status screen (broker, logger session, unit online, last status, message rates, laptop clock offset). ```powershell python -m invigil start diff --git a/docs/contract.md b/docs/contract.md index 959ef53..2acbc6d 100644 --- a/docs/contract.md +++ b/docs/contract.md @@ -59,6 +59,8 @@ The topic keeps the `{node_id}` level so more units can be added in Phase 2 with The unit publishes over plain MQTT on port 1883. The dashboard runs in a browser, which cannot open a plain MQTT socket, so it subscribes to `invigil/node/+/+` over WebSockets on port 9001 (see `server/mosquitto.conf`). Topics and payloads are the same on both. +The unit learns the broker's address from a UDP beacon the launcher broadcasts on port 18830. It is not an MQTT message and carries no readings. See [Finding the broker](firmware.md#finding-the-broker). + ## Proposed Not implemented. Nothing publishes this topic yet, `server/contract.py` rejects it, and the dashboard only shows detections from its Demo source. Agree on it here before building the Python side. diff --git a/docs/firmware.md b/docs/firmware.md index 574ed91..8a07e12 100644 --- a/docs/firmware.md +++ b/docs/firmware.md @@ -20,19 +20,43 @@ The unit repeats one cycle, as the [MQTT contract](contract.md) requires: The cycle only runs while Wi-Fi is up, the clock is set by SNTP and MQTT is connected. Nothing is published before the clock is set, because the contract rejects unsynced timestamps. Reconnecting happens between cycles, never inside a scan window. +## Finding the broker + +The unit does not need the laptop's IP address. On a phone hotspot the laptop gets a different address every time, so the unit finds the broker by itself (since version 0.3.0). + +**The beacon.** While the launcher runs (`invigil start`), it broadcasts one small UDP datagram a second to port **18830** on every network the laptop is on: + +``` +invigil broker 1883 52133b7ca99633f6 +``` + +`1883` is the MQTT port. The last field is a signature: the first 16 hex characters of HMAC-SHA256, with the hash salt as the key and `invigil broker ` as the message. The unit computes the same thing from the address the datagram came from. So a device that copies the beacon cannot point the unit at itself, and a laptop with another salt is ignored. `invigil/beacon.py` sends it, `firmware/portable/beacon.c` checks it, and CI compares the two. + +**What the unit does.** + +1. After Wi-Fi is up and the clock is set, it listens for a beacon and connects to the sender. The log says `mqtt: connecting to 10.139.204.170:1883 (beacon) as invigil-N1`. +2. If no beacon arrives within 5 s, it tries `MQTT_HOST` from `secrets.h` if one is set, else the last address a beacon came from. If it has neither, it keeps listening and says so once. +3. If the connection drops, it does the same again, with the usual backoff. +4. While connected, a beacon from another address means the broker has moved (the laptop got a new address, or another laptop took over). The unit waits until the old address has missed 3 beacons, drops the old connection and connects to the new address. With two laptops broadcasting at once, it stays with the first one it heard. + +**Scan windows.** Listening is receive only: a broadcast is never acknowledged or answered, so a beacon that arrives during a scan window makes the unit transmit nothing. Every connect, reconnect and move happens between cycles, as before. + +**If the unit never finds the broker.** `mqtt: no broker beacon on UDP port 18830 yet` means no beacon reached the unit. Check that the launcher is running (its status screen has a `Beacon` line), that the laptop and the unit are on the same network, and that the network does not keep its devices apart (some hotspots and guest networks do, often called client or AP isolation). `ignored N beacons with a wrong signature` means `HASH_SALT` in `secrets.h` and `hash_salt` in `server/config.toml` differ. + ## Modules | File | Job | | --- | --- | | `src/config.h` | Pins, timing constants (each marked as a guess to measure), the debug flag. Includes `secrets.h` | | `src/wifi_sta.c` | Wi-Fi station (WPA2 personal, country Egypt), reconnect with backoff (1 s doubling to 30 s), SNTP clock, prints the Wi-Fi channel and RSSI | -| `src/mqtt_link.c` | lwIP MQTT, QoS 0, client ID `invigil-N1`, reconnect with backoff, waits when the output buffer is full | +| `src/mqtt_link.c` | lwIP MQTT, QoS 0, client ID `invigil-N1`, chooses the broker (beacon, then the `MQTT_HOST` fallback), reconnect with backoff, waits when the output buffer is full | +| `src/broker_find.c` | Listens for the launcher's beacon on UDP port 18830 and keeps the sender's address. Receive only | | `src/ble_scan.c` | BTstack observer, passive scan, duplicate filtering off, boot self-check with the hash test vector, buffer of 1024 readings | | `src/nrf_scan.c` | nRF24L01 on SPI0 at 4 MHz, receive only, RPD hits per channel per window, sweep timing | | `src/main.c` | The cycle, watchdog (8 s), LED, 10 s summary on USB serial | | `src/lwipopts.h`, `src/btstack_config.h` | lwIP and BTstack settings | -| `portable/` | Code that needs no Pico SDK, tested on a PC: SHA-256 and address hashing, manufacturer ID parsing, JSON payloads, spectrum counting, the reading buffer, and the receive-only nRF24L01+ driver | -| `tests/` | Host tests for `portable/`, and `check_contract.py`, which runs sample payloads through `server/contract.py` | +| `portable/` | Code that needs no Pico SDK, tested on a PC: SHA-256 and address hashing, manufacturer ID parsing, JSON payloads, spectrum counting, the reading buffer, the beacon check, and the receive-only nRF24L01+ driver | +| `tests/` | Host tests for `portable/`, and `check_contract.py`, which runs sample payloads through `server/contract.py` and compares the beacons with `invigil/beacon.py` | ### Privacy and receive-only rules in the code @@ -70,10 +94,10 @@ Then fill it in: | Field | Value | | --- | --- | | `WIFI_SSID`, `WIFI_PASSWORD` | The hall Wi-Fi. WPA2 personal only | -| `MQTT_HOST` | The laptop's IPv4 address (run `ipconfig` on the laptop). Not a host name | -| `MQTT_PORT` | `1883` | +| `MQTT_HOST` | Optional fallback, used only when no [beacon](#finding-the-broker) is heard. `""` for none, or the laptop's IPv4 address (not a host name) on a network where it is fixed | +| `MQTT_PORT` | `1883`. The port for `MQTT_HOST`. With the beacon, the port comes from the beacon | | `MQTT_USER`, `MQTT_PASSWORD` | The same login as `server/config.toml` and `server/mosquitto.passwd` | -| `HASH_SALT` | The same value as `hash_salt` in `server/config.toml`. The unit will not publish BLE readings while it is empty | +| `HASH_SALT` | The same value as `hash_salt` in `server/config.toml`. The unit will not publish BLE readings while it is empty. The beacon is signed with it too | | `NODE_ID` | `"N1"` | | `SNTP_SERVER` | `"pool.ntp.org"`, or the laptop's IP address if the hall network has no internet. Then run an NTP server on the laptop | @@ -172,7 +196,7 @@ Every 10 s the unit prints one line of `key=value` pairs. These are the numbers | `send_overruns` | Send windows that needed more than 500 ms to drain. The unit waited longer instead of scanning | | `tx_in_scan` | Frames lwIP handed to the radio during scan windows. Should be 0 | | `sweeps_discarded` | Sweeps thrown away because lwIP sent a frame during them | -| `wifi`, `mqtt` | Connection state | +| `wifi`, `mqtt` | Connection state. `mqtt=finding-broker` means the unit is listening for the [beacon](#finding-the-broker) | `ble_no_rssi`, `ble_invalid` and `SELF-CHECK FAILED` only appear when they apply. @@ -191,6 +215,8 @@ The contract says the unit must not scan the 2.4 GHz band while it transmits ove | SNTP | One request an hour, plus a DNS lookup, whenever the lwIP timer fires | Rarely | | Wi-Fi chip firmware | 802.11 acks for frames it receives, and power-save frames if power saving is on. lwIP does not see these. Power saving is off (`WIFI_PM_MODE` is `CYW43_NONE_PM`) because the default mode sends a power-save frame about 200 ms after the last packet, which would fall inside every scan window | 802.11 acks only, when something arrives | | Reconnecting | Wi-Fi and MQTT reconnects only start between cycles, and the cycle stops while either is down | No | +| Broker beacon | The unit only listens. A broadcast is not acknowledged at the Wi-Fi level and lwIP sends no reply to it | No | +| Broker moved | The old connection is dropped with one TCP reset between cycles, not a normal close, which would keep resending to an address that may be gone | No | | Bluetooth | Passive scan, so the Bluetooth side never transmits | No | | nRF24L01 | Receive only, auto-ack off | No | diff --git a/docs/setup.md b/docs/setup.md index be0c2c3..bad678d 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -80,7 +80,7 @@ The unit connects to the laptop over Wi-Fi, so the broker must listen on the net The config opens two listeners: plain MQTT on port 1883 for the unit and the Python tools, and WebSockets on port 9001 for the [dashboard](../README.md#dashboard), because a browser cannot open a plain MQTT socket. The same login applies to both. -3. Put the same username and password in `server/config.toml` and in the firmware secrets. Point the unit at the laptop's IP address (find it with `ipconfig`, or `ip addr` on Linux). +3. Put the same username and password in `server/config.toml` and in the firmware secrets. The unit finds the laptop by itself through the launcher's beacon (see [Finding the broker](firmware.md#finding-the-broker)), so its IP address is not needed. Without the launcher, set `MQTT_HOST` in the firmware secrets to the laptop's IP address (find it with `ipconfig`, or `ip addr` on Linux). 4. If Windows Firewall asks, allow Mosquitto on private networks. 5. Start the logger as above. @@ -94,7 +94,7 @@ python -m invigil start | Command | What it does | | --- | --- | -| `start` | Starts Mosquitto with `server/mosquitto.conf`, the logger (session `live-