From 483b1ffaa401638b9836c67eac104e38575be0ca Mon Sep 17 00:00:00 2001 From: Audric Date: Wed, 8 Apr 2026 20:48:37 +0200 Subject: [PATCH] Add MQTT documentation to README and docs/MQTT.md --- README.md | 29 ++++++- docs/MQTT.md | 218 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 docs/MQTT.md diff --git a/README.md b/README.md index 4f2bb49..e3f14b7 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,9 @@ automatically. large deployments - **HTTP `/status` endpoint** — JSON status with trunk state, active talkers, satellite connections, and static configuration +- **MQTT publishing** — real-time event-driven updates (talker, client, + trunk state) to an external MQTT broker, plus configurable periodic + full status dumps - SvxLink client nodes are **unmodified** — they connect to their local reflector as normal and are unaware of the trunk @@ -79,7 +82,7 @@ reflectors hear the same audio on TG 25 with no extra configuration. ## Build Requires the same dependencies as SvxReflector: libsigc++, OpenSSL, libjsoncpp, -libpopt. Optional: libopus, libgsm, libspeex. +libpopt, libmosquitto. Optional: libopus, libgsm, libspeex. ```bash cd geureflector @@ -100,7 +103,7 @@ cd tests && bash run_tests.sh ``` Requires Docker and Python 3.7+. The script builds the images, starts the mesh, -runs 11 automated tests, then drops into an interactive prompt where you can +runs 18 automated tests, then drops into an interactive prompt where you can enter any TG number and see which reflector it routes to (verified via container logs). @@ -325,6 +328,26 @@ on the parent is heard on the satellite and vice versa, regardless of Port `5303` is the default satellite port (separate from client port `5300` and trunk port `5302`). +### MQTT publishing (optional) + +Publishes real-time events and periodic status to an external MQTT broker, +eliminating the need to poll the `/status` endpoint. See +[`docs/MQTT.md`](docs/MQTT.md) for the full topic structure, payload format, +and TLS configuration. + +```ini +[MQTT] +HOST=mqtt.example.com +PORT=1883 +USERNAME=reflector +PASSWORD=secret +TOPIC_PREFIX=svxreflector/myreflector +STATUS_INTERVAL=1000 +``` + +Omit the `[MQTT]` section entirely to disable — zero overhead when not +configured. + --- ## HTTP Status @@ -423,6 +446,8 @@ The PTY path is set by `COMMAND_PTY` in `[GLOBAL]` (default deployment example for Italy (20 regions, full mesh) - [`docs/DEPLOYMENT_ITALY_IT.md`](docs/DEPLOYMENT_ITALY_IT.md) — same document in Italian +- [`docs/MQTT.md`](docs/MQTT.md) — MQTT publishing: topic structure, payload + format, configuration reference, and TLS setup - [`docs/DESIGN_SATELLITE_AND_CLUSTER.md`](docs/DESIGN_SATELLITE_AND_CLUSTER.md) — design rationale for satellite reflectors and cluster TGs - [`tests/TESTS.md`](tests/TESTS.md) — integration test suite documentation: diff --git a/docs/MQTT.md b/docs/MQTT.md new file mode 100644 index 0000000..5c27300 --- /dev/null +++ b/docs/MQTT.md @@ -0,0 +1,218 @@ +# MQTT Publishing + +GeuReflector can publish real-time events and periodic status to an external +MQTT broker. This replaces the need to poll the HTTP `/status` endpoint — +dashboards and other consumers subscribe to MQTT topics and receive push +updates only when state changes. + +## Quick start + +Add an `[MQTT]` section to `svxreflector.conf`: + +```ini +[MQTT] +HOST=mqtt.example.com +PORT=1883 +USERNAME=reflector +PASSWORD=secret +TOPIC_PREFIX=svxreflector/myreflector +``` + +Restart the reflector. Events start flowing immediately. + +Omit the `[MQTT]` section entirely to disable — there is zero overhead when +not configured. + +--- + +## Configuration reference + +| Field | Required | Default | Description | +|-------|----------|---------|-------------| +| `HOST` | Yes | — | MQTT broker hostname or IP | +| `PORT` | Yes | — | MQTT broker port (typically 1883, or 8883 for TLS) | +| `USERNAME` | Yes | — | Broker authentication username | +| `PASSWORD` | Yes | — | Broker authentication password | +| `TOPIC_PREFIX` | Yes | — | Base topic path (e.g. `svxreflector/myreflector`) | +| `STATUS_INTERVAL` | No | 1000 | Periodic full status publish interval in milliseconds | +| `TLS_ENABLED` | No | 0 | Enable TLS encryption (`0` or `1`) | +| `TLS_CA_CERT` | If TLS | — | Path to CA certificate file | +| `TLS_CLIENT_CERT` | No | — | Path to client certificate (mutual TLS only) | +| `TLS_CLIENT_KEY` | No | — | Path to client private key (mutual TLS only) | + +`TOPIC_PREFIX` has no default — set it to a unique identifier for this +reflector. It is not derived from `LOCAL_PREFIX` because a reflector may own +multiple prefixes while needing a single stable MQTT identity. + +--- + +## Topic structure + +All topics are published under the configured `TOPIC_PREFIX`. Subscribers can +use `TOPIC_PREFIX/#` to receive everything, or subscribe to specific subtrees. + +### Talker events + +Published when a client or trunk peer starts or stops talking on a talk group. + +``` +{TOPIC_PREFIX}/talker/{tg}/start +{TOPIC_PREFIX}/talker/{tg}/stop +``` + +Payload: +```json +{"callsign": "ON4ABC", "source": "local"} +``` + +`source` is `"local"` when the talker is a directly connected client, or +`"trunk"` when the talker is on a remote reflector reached via trunk. + +### Client events + +Published when a SvxLink node authenticates or disconnects. + +``` +{TOPIC_PREFIX}/client/{callsign}/connected +{TOPIC_PREFIX}/client/{callsign}/disconnected +``` + +Connected payload: +```json +{"tg": 1234, "ip": "192.168.1.10"} +``` + +Disconnected payload: +```json +{} +``` + +### Trunk events + +Published when a trunk link's outbound or inbound connection goes up or down. + +``` +{TOPIC_PREFIX}/trunk/{section}/outbound/up +{TOPIC_PREFIX}/trunk/{section}/outbound/down +{TOPIC_PREFIX}/trunk/{section}/inbound/up +{TOPIC_PREFIX}/trunk/{section}/inbound/down +``` + +Up payload: +```json +{"host": "reflector-b.example.com", "port": 5302} +``` + +Down payload: +```json +{} +``` + +### Periodic full status + +Published at the configured `STATUS_INTERVAL` (default every 1 second). The +payload is identical to the HTTP `/status` JSON response. + +``` +{TOPIC_PREFIX}/status +``` + +This is the only topic published with MQTT **retain** enabled, so new +subscribers immediately receive the last known state. + +--- + +## QoS and retain policy + +| Topic | QoS | Retain | +|-------|-----|--------| +| `talker/...` | 0 | No | +| `client/...` | 0 | No | +| `trunk/...` | 0 | No | +| `status` | 0 | **Yes** | + +QoS 0 (fire-and-forget) is used throughout. Event messages are ephemeral — a +missed talker start is covered by the next periodic status dump. Retain on +`status` ensures late-joining subscribers get a baseline immediately. + +--- + +## TLS + +To encrypt the connection to the broker, enable TLS: + +```ini +[MQTT] +HOST=mqtt.example.com +PORT=8883 +USERNAME=reflector +PASSWORD=secret +TOPIC_PREFIX=svxreflector/myreflector +TLS_ENABLED=1 +TLS_CA_CERT=/etc/svxlink/mqtt-ca.crt +``` + +For mutual TLS (client certificate authentication), also set: + +```ini +TLS_CLIENT_CERT=/etc/svxlink/mqtt-client.crt +TLS_CLIENT_KEY=/etc/svxlink/mqtt-client.key +``` + +The broker must be configured to listen on a TLS port and present a certificate +signed by the CA specified in `TLS_CA_CERT`. + +--- + +## Error handling + +MQTT publishing is best-effort and never impacts reflector operation: + +- **Broker unreachable at startup:** Logged as a warning. The reflector starts + normally and the MQTT client reconnects in the background (1s initial delay, + 30s max). +- **Broker disconnects:** Automatic reconnection. Messages published while + disconnected are silently dropped. +- **Bad credentials:** Logged as an error. The client retries reconnection. +- **Missing required config fields:** Logged as an error. The reflector starts + without MQTT. + +--- + +## Implementation notes + +The MQTT client uses [libmosquitto](https://mosquitto.org/) with +`mosquitto_loop_start()`, which runs a dedicated background thread for all +network I/O, reconnection, and keepalive. Publish calls from the reflector's +main event loop enqueue messages and return immediately — fully non-blocking. + +### Dependencies + +- **libmosquitto** — required at build time (`libmosquitto-dev`) and runtime + (`libmosquitto1`) +- **jsoncpp** — already a project dependency, used for JSON payload + serialization +- **OpenSSL** — already a project dependency, used by libmosquitto for TLS + +### Docker + +The Docker image includes libmosquitto. No extra setup is needed beyond adding +the `[MQTT]` section to your config file. + +--- + +## Example: subscribing with mosquitto_sub + +```bash +# Subscribe to all events from one reflector +mosquitto_sub -h mqtt.example.com -u user -P pass \ + -t 'svxreflector/myreflector/#' -v + +# Subscribe to talker events only +mosquitto_sub -h mqtt.example.com -u user -P pass \ + -t 'svxreflector/myreflector/talker/#' -v + +# Subscribe to trunk state across all reflectors +mosquitto_sub -h mqtt.example.com -u user -P pass \ + -t 'svxreflector/+/trunk/#' -v +```