Files
project-vesper/src/Communication/Transport/UARTTransport
bonaminandClaude Sonnet 4.6 7e8d91673f fix: builtin aliases, cmd version default, relay compat, NTP UTC, player guards
BuiltInMelodies (F-018):
- 24 builtin_* UID aliases added for backward compat with old device PIDs
- Two new PROGMEM arrays: builtin_catehetical, builtin_mournfull_toll_alternative
- isBuiltInMelody() helper for lightweight PID existence check
- FileManager download-skip now uses isBuiltInMelody() instead of findMelodyByPID()

Command envelope version default (F-020):
- MQTT, UART, WebSocket transports: missing "v" field defaults to 1 (was 2)
- Fixes legacy adapter rejecting commands from app and Node-RED

RelayHandler v1/v2 compat (F-021):
- set_durations and set_outputs accept both nested {"bells":{...}} (v2)
  and flat {...} (v1) payload shapes

NTP UTC fix (F-019):
- configTime(0,0,ntpServer) fetches raw UTC; stored gmtOffset+dstOffset
  applied manually to epoch before writing RTC
- Eliminates double-offset bug from SDK internal timezone handling

Player guards (F-022):
- speed==0 defaults to 300ms instead of producing silent/broken playback
- calculateProjectedRunTime() guards against singleLoopDuration==0

Telemetry (F-023):
- bellMaxLoad default raised 200→500 to eliminate false overload warnings

Version bump: 2.0.1 → 2.0.3

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 09:52:39 +03:00
..
2026-03-14 22:16:03 +02:00

UARTTransport

Summary

UARTTransport wraps UARTDriver and connects it to the CommandBus for the UART slave interface (LCD panel / button board on Serial2). Unlike other transports, UART requires polling — loop() must be called each iteration of the Arduino main loop. A command whitelist is enforced before dispatch: only five commands are allowed from the UART slave. All other commands are silently dropped. This prevents a compromised or misbehaving slave from issuing sensitive commands (factory reset, OTA update, etc.). Replies are sent back over UART as newline-delimited JSON.


Public API

Method Description
UARTTransport(UARTDriver& uart, CommandBus& bus) Constructor.
void begin() Wires the message callback onto UARTDriver, then calls UARTDriver::begin() to initialise Serial2.
void loop() Calls UARTDriver::loop() to poll for incoming bytes. Must be called from the Arduino main loop.
bool isHealthy() const Returns UARTDriver::isReady().
String name() const Returns "UART".
void broadcast(const String& msg) Sends a JSON string to the UART slave (newline appended by UARTDriver::send()).

Command Whitelist

Only the following commands are accepted from the UART slave:

Command Purpose
ping Connection check
playback Play / stop from physical buttons
clock.pause Button-triggered clock pause
clock.resume Button-triggered clock resume
system.get_time LCD requests current time (pull model, replaces old push)

Any command not in this list is dropped with a LOG_WARNING and the client receives no response.


Initialization

CommunicationRouter::begin() calls _uartTransport.begin(), then adds the transport to the bus. CommunicationRouter::loop() calls _uartTransport.loop(), which is the only mandatory polling call in the system.


Wire Protocol

Messages are newline-delimited JSON on Serial2 (TX: GPIO 12, RX: GPIO 13, 115200 baud):

{ "v": 2, "cmd": "playback", "contents": { ... } }\n

Responses are sent back immediately after dispatch:

{ "status": "SUCCESS", "type": "playback", "message": "Playback command executed" }\n

JSON parse errors on the slave side generate an error response:

{ "status": "ERROR", "type": "parse_error", "payload": "<ArduinoJson error string>" }\n

Dependencies

  • UARTDriver — Serial2 I/O, line buffering, JSON parsing
  • CommandBus — dispatch
  • ArduinoJson, Logging

Gotchas / Notes

  • loop() must be called. Without it, no UART messages are processed. CommunicationRouter::loop() handles this for you.
  • Whitelist is enforced before the bus. Dropped commands produce a LOG_WARNING but no response to the slave. If the slave is polling for a response that never arrives, it will time out. Design slave firmware accordingly.
  • Buffer overflow drops the message. UARTDriver has a 1024-byte receive buffer. Messages longer than this are discarded and _errorCount is incremented.
  • Blocking flush. UARTDriver::send() calls Serial.flush() after each response, which blocks until the UART TX buffer drains. At 115200 baud this is typically under 1 ms for short responses.
  • No v1 translation for UART. In practice the UART slave sends v2 commands, but CommandBus::dispatch() will still translate v1 envelopes if "v": 1 is set.