Files
project-vesper/src/Communication/Transport/UARTTransport/UARTTransport.cpp
T
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

106 lines
2.8 KiB
C++

#include "UARTTransport.hpp"
#include "../../CommandBus/CommandBus.hpp"
#include "../../CommandBus/CommandEnvelope.hpp"
#include "../../CommandBus/CommandResult.hpp"
#define TAG "UARTTransport"
#include "../../../Logging/Logging.hpp"
// ---------------------------------------------------------------------------
// Whitelist
// ---------------------------------------------------------------------------
const char* const UARTTransport::WHITELIST[] = {
"ping",
"playback",
"clock.pause",
"clock.resume",
"system.get_time",
};
const size_t UARTTransport::WHITELIST_SIZE =
sizeof(UARTTransport::WHITELIST) / sizeof(UARTTransport::WHITELIST[0]);
// ---------------------------------------------------------------------------
UARTTransport::UARTTransport(UARTDriver& uart, CommandBus& bus)
: _uart(uart)
, _bus(bus) {
}
void UARTTransport::begin() {
_uart.setCallback([this](JsonDocument& doc) {
onMessage(doc);
});
_uart.begin();
LOG_INFO(TAG, "UARTTransport initialised (whitelist: %u commands)", WHITELIST_SIZE);
}
void UARTTransport::loop() {
_uart.loop();
}
bool UARTTransport::isHealthy() const {
return _uart.isReady();
}
void UARTTransport::broadcast(const String& message) {
_uart.send(message);
}
// ---------------------------------------------------------------------------
bool UARTTransport::isWhitelisted(const String& command) const {
for (size_t i = 0; i < WHITELIST_SIZE; ++i) {
if (command == WHITELIST[i]) {
return true;
}
}
return false;
}
void UARTTransport::onMessage(JsonDocument& doc) {
const char* cmd = doc["cmd"] | "";
if (cmd[0] == '\0') {
LOG_WARNING(TAG, "UART message missing 'cmd' field — dropping");
return;
}
if (!isWhitelisted(cmd)) {
LOG_WARNING(TAG, "UART command '%s' not whitelisted — silently dropped", cmd);
return;
}
LOG_DEBUG(TAG, "UART CMD (whitelisted): %s", cmd);
CommandEnvelope envelope;
envelope.transport = CommandEnvelope::Transport::UART;
envelope.clientId = 0;
envelope.command = cmd;
envelope.version = doc["v"] | 1;
if (doc.containsKey("contents")) {
envelope.contents.set(doc["contents"]);
}
// Reply routes back over UART to the slave
envelope.reply = [this](const CommandResult& result) {
JsonDocument resp;
resp["status"] = result.isOk() ? "SUCCESS"
: (result.status == CommandResult::Status::INFO ? "INFO" : "ERROR");
resp["type"] = result.type;
if (!result.payload.isEmpty()) {
resp["message"] = result.payload;
}
if (result.hasData) {
resp["data"] = result.data;
}
String out;
serializeJson(resp, out);
_uart.send(out);
};
_bus.dispatch(envelope);
}