Merge branch 'mqtt-bridge-implementation-flex' into add-rflab-mqtt-preset

This commit is contained in:
Adam Gessaman authored and GitHub committed 2026-06-24 21:38:49 -07:00
commit b699bf6a3b
31 files changed
+1122 -132

No files matched your search

+4
View File
@@ -64,6 +64,10 @@ public:
virtual uint8_t getStartupReason() const = 0;
virtual bool getBootloaderVersion(char* version, size_t max_len) { return false; }
virtual bool startOTAUpdate(const char* id, char reply[]) { return false; } // not supported
// Pull-based OTA: fetch the firmware build for this variant from a baked-in manifest and flash it.
// current_ver is the running firmware version string (used to skip if already up to date); when
// dry_run is true the build is only reported, not flashed. Observer (ESP32+WiFi) builds only.
virtual bool otaFromManifest(const char* current_ver, bool dry_run, char reply[]) { return false; }
// Power management interface (boards with power management override these)
virtual bool isExternalPowered() { return false; }
+120
View File
@@ -50,6 +50,21 @@ static int getMQTTPresetNameCount() {
return MQTT_PRESET_COUNT + 2; // built-ins + custom + none
}
static bool isValidNtpHostname(const char* host) {
if (!host || host[0] == '\0') return false;
size_t len = strlen(host);
if (len > 63) return false;
if (host[0] == '.' || host[len - 1] == '.') return false;
for (size_t i = 0; i < len; i++) {
char c = host[i];
if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') || c == '.' || c == '-')) {
return false;
}
}
return true;
}
static const char* getMQTTPresetNameByIndex(int index) {
if (index < MQTT_PRESET_COUNT) return MQTT_PRESETS[index].name;
if (index == MQTT_PRESET_COUNT) return MQTT_PRESET_CUSTOM;
@@ -657,6 +672,7 @@ void CommonCLI::syncMQTTPrefsToNodePrefs() {
}
StrHelper::strncpy(_prefs->mqtt_owner_public_key, _mqtt_prefs.mqtt_owner_public_key, sizeof(_prefs->mqtt_owner_public_key));
StrHelper::strncpy(_prefs->mqtt_email, _mqtt_prefs.mqtt_email, sizeof(_prefs->mqtt_email));
StrHelper::strncpy(_prefs->mqtt_ntp_server, _mqtt_prefs.mqtt_ntp_server, sizeof(_prefs->mqtt_ntp_server));
}
void CommonCLI::syncNodePrefsToMQTTPrefs() {
@@ -688,6 +704,7 @@ void CommonCLI::syncNodePrefsToMQTTPrefs() {
}
StrHelper::strncpy(_mqtt_prefs.mqtt_owner_public_key, _prefs->mqtt_owner_public_key, sizeof(_mqtt_prefs.mqtt_owner_public_key));
StrHelper::strncpy(_mqtt_prefs.mqtt_email, _prefs->mqtt_email, sizeof(_mqtt_prefs.mqtt_email));
StrHelper::strncpy(_mqtt_prefs.mqtt_ntp_server, _prefs->mqtt_ntp_server, sizeof(_mqtt_prefs.mqtt_ntp_server));
}
#endif
@@ -791,8 +808,66 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
}
#else
strcpy(reply, "ERR: unsupported on this platform");
#endif
} else if (memcmp(command, "ota check", 9) == 0 || memcmp(command, "ota update", 10) == 0) {
// Observer pull-OTA: fetch this variant's build from the baked-in manifest
// and flash it. Intentionally a separate command from "start ota" (the
// manual ElegantOTA web-upload SoftAP) so a remote/online update is never
// triggered by someone expecting to hand-upload a binary.
// ota check -> report available build, do not flash
// ota update -> download and flash, then reboot
#if defined(WITH_MQTT_BRIDGE) && defined(OTA_MANIFEST_BASE)
if (WiFi.status() != WL_CONNECTED) {
strcpy(reply, "ERR: WiFi not connected");
} else if (memcmp(command, "ota check", 9) == 0) {
// Check is synchronous so its result lands in this reply, and runs with the
// MQTT bridge UP: the slim per-variant manifest is tiny, so the fetch only
// costs a single TLS handshake (no large JSON doc) — which fits alongside
// the live MQTT sessions even on no-PSRAM boards. No bridge bounce needed.
_board->otaFromManifest(_callbacks->getFirmwareVer(), true, reply);
} else {
// `ota update`: cheap pre-check first (plain HTTP, bridge stays up). Only
// schedule the real update — which tears the bridge down, flashes, and
// reboots — when an applicable build actually exists. otaFromManifest(dry)
// returns true iff so; otherwise it leaves the explanation (up to date /
// cable flash / error) in reply, which we send without disturbing the
// bridge or misleading the user with a "Beginning update..." that no-ops.
if (_board->otaFromManifest(_callbacks->getFirmwareVer(), true, reply)) {
// reply now holds "update available: <cur> -> <target> (N behind|new base)",
// where <target> is "vX.Y.Z.B (hash)". Pull <target> out for a friendlier
// start message. The "-> " ... trailing " (" framing is produced by
// ESP32Board::otaFromManifestImpl; <target> ends at the LAST " (" (the
// "(N behind)"/"(new base)" suffix), since the version's own hash-paren
// comes before it.
char target[48] = {0};
const char* arrow = strstr(reply, "-> ");
if (arrow) {
arrow += 3;
const char* suffix = nullptr;
for (const char* p = arrow; (p = strstr(p, " (")) != nullptr; p++) suffix = p;
size_t len = suffix ? (size_t)(suffix - arrow) : strlen(arrow);
if (len >= sizeof(target)) len = sizeof(target) - 1;
memcpy(target, arrow, len);
target[len] = 0;
}
// Update is DEFERRED so this ack goes out over the mesh before the flash
// blocks the loop and reboots (the app loop runs it shortly).
if (_callbacks->beginDeferredOtaUpdate()) {
if (target[0]) {
snprintf(reply, 160, "Updating to %s; reboots when done (~30s offline). Check 'ver' after.", target);
} else {
strcpy(reply, "Beginning update... (node will reboot if successful)");
}
} else {
strcpy(reply, "ERR: online OTA not available");
}
}
}
#else
strcpy(reply, "ERR: online OTA not supported on this build");
#endif
} else if (memcmp(command, "start ota", 9) == 0) {
// Manual OTA: bring up the ElegantOTA SoftAP for a hand-uploaded binary.
if (!_board->startOTAUpdate(_prefs->node_name, reply)) {
strcpy(reply, "Error");
}
@@ -1408,6 +1483,35 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
} else {
strcpy(reply, "Error: interval must be between 1-60 minutes");
}
} else if (memcmp(config, "mqtt.ntp ", 9) == 0) {
const char* host = &config[9];
while (*host == ' ') host++;
bool clearing = strcmp(host, "none") == 0;
if (!clearing && !isValidNtpHostname(host)) {
strcpy(reply, "Error: invalid NTP hostname");
} else {
if (clearing) {
_prefs->mqtt_ntp_server[0] = '\0';
} else {
StrHelper::strncpy(_prefs->mqtt_ntp_server, host, sizeof(_prefs->mqtt_ntp_server));
}
savePrefs();
#ifdef ESP_PLATFORM
// Validate by running an immediate sync. syncMqttNtp() marshals onto the MQTT
// task (Core 0) so no NTP I/O happens on this (Core 1) CLI thread.
if (WiFi.status() != WL_CONNECTED) {
strcpy(reply, "OK - saved (WiFi not connected; NTP sync pending)");
} else if (!_callbacks->isMqttBridgeRunning()) {
strcpy(reply, "OK - saved (MQTT bridge not running)");
} else if (_callbacks->syncMqttNtp()) {
strcpy(reply, "OK - time synced");
} else {
strcpy(reply, "Error: NTP sync failed");
}
#else
strcpy(reply, "OK - saved");
#endif
}
} else if (memcmp(config, "wifi.ssid ", 10) == 0) {
StrHelper::strncpy(_prefs->wifi_ssid, &config[10], sizeof(_prefs->wifi_ssid));
savePrefs();
@@ -1962,6 +2066,22 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep
} else if (memcmp(config, "mqtt.interval", 13) == 0) {
uint32_t minutes = (_prefs->mqtt_status_interval + 29999) / 60000;
sprintf(reply, "> %u minutes (%lu ms)", minutes, (unsigned long)_prefs->mqtt_status_interval);
} else if (memcmp(config, "mqtt.ntp.diag", 13) == 0 && (config[13] == '\0' || config[13] == ' ')) {
#ifdef ESP_PLATFORM
// Connectivity probe across all configured NTP servers; never updates the clock.
// Serial console (sender_timestamp == 0) gets a detailed table; LoRa gets a compact list.
if (WiFi.status() != WL_CONNECTED) {
strcpy(reply, "Error: WiFi not connected");
} else if (!_callbacks->isMqttBridgeRunning()) {
strcpy(reply, "Error: MQTT bridge not running");
} else if (!_callbacks->runMqttNtpDiag(reply, 160, sender_timestamp == 0)) {
strcpy(reply, "Error: NTP diag unavailable");
}
#else
strcpy(reply, "Error: not supported on this platform");
#endif
} else if (memcmp(config, "mqtt.ntp", 8) == 0 && (config[8] == '\0' || config[8] == ' ')) {
sprintf(reply, "> %s", MQTTBridge::effectiveNtpPrimary(_prefs));
} else if (config[0] == 'm' && config[1] == 'q' && config[2] == 't' && config[3] == 't' &&
config[4] >= '1' && config[4] <= ('0' + MAX_MQTT_SLOTS) && config[5] == '.') {
// Slot-based commands: get mqtt1.preset, get mqtt1.server, etc.
+26
View File
@@ -125,6 +125,9 @@ struct NodePrefs { // persisted to file
// time, so the operator can name a region that doesn't exist yet without
// polluting region_map state. Falls back to default_scope on miss.
char alert_region[31];
// Custom NTP server (MQTT observer); empty = built-in default primary (pool.ntp.org)
char mqtt_ntp_server[64];
};
#ifdef WITH_MQTT_BRIDGE
@@ -202,6 +205,7 @@ struct MQTTPrefs {
// --- Appended fields (added after initial 6-slot migration) ---
uint8_t mqtt_rx_enabled; // Enable RX packet uplinking (default: on)
char mqtt_ntp_server[64]; // Custom NTP server; empty = pool.ntp.org
};
// 3-slot MQTTPrefs layout — used for migrating from 3-slot to 6-slot format.
@@ -288,10 +292,32 @@ public:
restartBridge();
};
// Schedule a pull-OTA firmware update to run shortly (from the app loop), after
// the "Beginning update..." CLI reply has been transmitted. Deferred because the
// flash blocks the loop and then reboots, so it can't run inline with the reply.
// Returns true if scheduled. Default: not supported.
virtual bool beginDeferredOtaUpdate() {
return false;
};
virtual int getQueueSize() {
return 0; // no op by default
};
virtual bool syncMqttNtp() {
return false; // WITH_MQTT_BRIDGE builds override
};
virtual bool isMqttBridgeRunning() {
return false;
};
// Probe all configured NTP servers for connectivity (verbose=serial console gets a
// detailed table; otherwise reply gets a compact "<server> ok|fail" list).
virtual bool runMqttNtpDiag(char* reply, size_t reply_size, bool verbose) {
return false; // WITH_MQTT_BRIDGE builds override
};
virtual void setRxBoostedGain(bool enable) {
// no op by default
};
+377 -2
View File
@@ -12,9 +12,19 @@
bool ESP32Board::startOTAUpdate(const char* id, char reply[]) {
inhibit_sleep = true; // prevent sleep during OTA
WiFi.softAP("MeshCore-OTA", NULL);
sprintf(reply, "Started: http://%s/update", WiFi.softAPIP().toString().c_str());
// If the device is already on a WiFi network (e.g. an observer joined in STA
// mode), serve ElegantOTA on the station IP so it's reachable from the LAN
// without joining a separate AP. Otherwise raise the MeshCore-OTA SoftAP.
IPAddress ip;
if (WiFi.status() == WL_CONNECTED) {
ip = WiFi.localIP();
} else {
WiFi.softAP("MeshCore-OTA", NULL);
ip = WiFi.softAPIP();
}
sprintf(reply, "Started: http://%s/update", ip.toString().c_str());
MESH_DEBUG_PRINTLN("startOTAUpdate: %s", reply);
static char id_buf[60];
@@ -44,4 +54,369 @@ bool ESP32Board::startOTAUpdate(const char* id, char reply[]) {
}
#endif
// ---------------------------------------------------------------------------
// Manifest-driven pull OTA (observer / MQTT-bridge builds only)
//
// The observer already holds a live WiFi station connection (for the MQTT
// bridge) and embeds a root-CA bundle, so it can fetch its own firmware. The
// caller (CommonCLI) stops the MQTT bridge first to free heap/TLS, then calls
// this. We read the web-flasher manifest (config.json), find the `flash-update`
// (app-only) build for our own variant, refuse partition-change releases (OTA
// can't rewrite the partition table), skip if already up to date, then stream
// the .bin straight into the inactive OTA slot via HTTPUpdate.
// ---------------------------------------------------------------------------
#if defined(WITH_MQTT_BRIDGE)
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <HTTPUpdate.h>
#include <ArduinoJson.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_partition.h>
// Embedded CA bundle (produced by board_build.embed_files). Weak so non-bundle
// builds still link; we check for presence at runtime.
extern const uint8_t rootca_crt_bundle_start[] asm("_binary_src_certs_x509_crt_bundle_bin_start") __attribute__((weak));
extern const uint8_t rootca_crt_bundle_end[] asm("_binary_src_certs_x509_crt_bundle_bin_end") __attribute__((weak));
// Extract the trailing build hash. For a filename we first drop a ".bin"
// suffix, then take the token after the last '-'. Works for both the manifest
// asset name ("...-v1.16.0-8b084d5.bin" -> "8b084d5") and the embedded
// FIRMWARE_VERSION ("v1.16.0-observer-8b084d5" -> "8b084d5").
static void ota_extractHash(const char* s, char* out, size_t out_sz) {
if (!s) { if (out_sz) out[0] = 0; return; }
size_t len = strlen(s);
if (len > 4 && strcmp(s + len - 4, ".bin") == 0) len -= 4;
size_t i = len;
while (i > 0 && s[i - 1] != '-') i--;
size_t n = len - i;
if (n >= out_sz) n = out_sz - 1;
memcpy(out, s + i, n);
out[n] = 0;
}
// Split a version token "vMAJOR.MINOR.PATCH[.BUILD]" into its base
// ("vMAJOR.MINOR.PATCH") and build number (BUILD, or -1 if there's no 4th
// component). The base has exactly two dots; a third dot introduces the build.
static void ota_parseVersion(const char* ver, char* base_out, size_t base_sz, int* build_out) {
*build_out = -1;
if (base_sz) base_out[0] = 0;
if (!ver) return;
int dots = 0, third_dot = -1;
for (int j = 0; ver[j]; j++) {
if (ver[j] == '.' && ++dots == 3) { third_dot = j; break; }
}
if (third_dot >= 0) {
size_t n = (size_t)third_dot;
if (n >= base_sz) n = base_sz - 1;
memcpy(base_out, ver, n);
base_out[n] = 0;
*build_out = atoi(ver + third_dot + 1);
} else {
strncpy(base_out, ver, base_sz - 1);
base_out[base_sz - 1] = 0;
}
}
// Canonical signature of the FLASHED partition table — MUST match
// scripts/partition_signature.py: each entry "type:subtype:offset:size" in
// lowercase hex, sorted by offset, joined by ','. Lets `ota update` compare the
// target build's partition layout (carried in the manifest as partSig) against
// what's actually on this device, instead of a blanket per-variant flag.
static void ota_partitionSignature(char* out, size_t out_sz) {
struct PE { uint8_t type, subtype; uint32_t off, size; } e[24];
int n = 0;
esp_partition_iterator_t it =
esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, nullptr);
while (it != nullptr && n < (int)(sizeof(e) / sizeof(e[0]))) {
const esp_partition_t* p = esp_partition_get(it);
e[n].type = (uint8_t)p->type;
e[n].subtype = (uint8_t)p->subtype;
e[n].off = p->address;
e[n].size = p->size;
n++;
it = esp_partition_next(it);
}
esp_partition_iterator_release(it); // safe on NULL (loop exhausted)
// insertion sort by offset (matches the script's sort key)
for (int i = 1; i < n; i++) {
PE k = e[i];
int j = i - 1;
while (j >= 0 && e[j].off > k.off) { e[j + 1] = e[j]; j--; }
e[j + 1] = k;
}
size_t pos = 0;
if (out_sz) out[0] = 0;
for (int i = 0; i < n && pos + 1 < out_sz; i++) {
pos += snprintf(out + pos, out_sz - pos, "%s%x:%x:%x:%x",
i ? "," : "", e[i].type, e[i].subtype, (unsigned)e[i].off, (unsigned)e[i].size);
}
}
// Parameters handed to the worker task; lives on otaFromManifest()'s stack,
// which stays valid because that function blocks until the worker signals done.
struct OtaTaskArgs {
ESP32Board* self;
const char* current_ver;
bool dry_run;
char* reply;
volatile bool result;
volatile bool done;
};
static void ota_task_entry(void* param) {
OtaTaskArgs* a = static_cast<OtaTaskArgs*>(param);
a->result = a->self->otaFromManifestImpl(a->current_ver, a->dry_run, a->reply);
a->done = true; // on a successful `ota update` we reboot before reaching here
vTaskDelete(nullptr);
}
bool ESP32Board::otaFromManifest(const char* current_ver, bool dry_run, char reply[]) {
// The TLS handshake (cert-bundle verify) + JSON parse / HTTPUpdate use far more
// stack than the ~8 KB loop task offers — especially when reached via the deep
// mesh-receive call chain (it overflows the loopTask canary). Run the work in a
// dedicated 24 KB-stack task and block here until it finishes. The big stack is
// freed when the task exits; on a successful update the chip reboots inside it.
OtaTaskArgs args = { this, current_ver, dry_run, reply, false, false };
TaskHandle_t handle = nullptr;
BaseType_t ok = xTaskCreatePinnedToCore(ota_task_entry, "ota", 24576, &args, 5, &handle, 1);
if (ok != pdPASS) {
strcpy(reply, "ERR: OTA task spawn failed");
return false;
}
while (!args.done) {
delay(50); // Arduino delay() yields to other tasks
}
return args.result;
}
bool ESP32Board::otaFromManifestImpl(const char* current_ver, bool dry_run, char reply[]) {
#if !defined(OTA_MANIFEST_BASE) || !defined(OTA_VARIANT)
strcpy(reply, "ERR: OTA not configured (build via build.sh)");
return false;
#else
if (WiFi.status() != WL_CONNECTED) {
strcpy(reply, "ERR: WiFi not connected");
return false;
}
size_t bundle_len = 0;
if (rootca_crt_bundle_start != nullptr && rootca_crt_bundle_end != nullptr &&
rootca_crt_bundle_end > rootca_crt_bundle_start) {
bundle_len = (size_t)(rootca_crt_bundle_end - rootca_crt_bundle_start);
}
if (!dry_run && bundle_len == 0) {
strcpy(reply, "ERR: no embedded cert bundle");
return false;
}
// --- Fetch this variant's slim manifest ----------------------------------
// <OTA_MANIFEST_BASE>/<OTA_VARIANT>.json — a ~180 byte per-variant file, not the
// full config.json.
char murl[200];
HTTPClient http;
WiFiClientSecure mclient; // only used for the HTTPS (update) path
if (dry_run) {
// `ota check`: fetch over PLAIN HTTP. With no TLS handshake the fetch costs
// negligible heap, so the check runs with the MQTT bridge UP even on no-PSRAM
// — where the cert-bundle TLS verify would otherwise exhaust internal heap
// alongside the two live MQTT TLS sessions (free heap collapses to a few KB
// and the handshake + the bridge both fail). This only reads version info; the
// firmware download below (ota update) is always TLS-verified. Requires the
// manifest host to serve /v over HTTP (no forced HTTPS redirect).
if (strncmp(OTA_MANIFEST_BASE, "https://", 8) == 0) {
snprintf(murl, sizeof(murl), "http://%s/%s.json", OTA_MANIFEST_BASE + 8, OTA_VARIANT);
} else {
snprintf(murl, sizeof(murl), "%s/%s.json", OTA_MANIFEST_BASE, OTA_VARIANT);
}
if (!http.begin(murl)) {
strcpy(reply, "ERR: manifest connect failed");
return false;
}
} else {
// `ota update`: HTTPS. The bridge is torn down for an update so heap is free,
// and integrity matters because we're about to flash.
#if ESP_ARDUINO_VERSION_MAJOR >= 3
mclient.setCACertBundle(rootca_crt_bundle_start, bundle_len);
#else
mclient.setCACertBundle(rootca_crt_bundle_start);
#endif
mclient.setTimeout(15000);
snprintf(murl, sizeof(murl), "%s/%s.json", OTA_MANIFEST_BASE, OTA_VARIANT);
if (!http.begin(mclient, murl)) {
strcpy(reply, "ERR: manifest connect failed");
return false;
}
}
if (!dry_run) { Serial.print("OTA: checking manifest "); Serial.println(murl); }
// Force HTTP/1.0: a CDN (e.g. Cloudflare) answers HTTP/1.1 with chunked encoding
// and no Content-Length; the raw chunked stream corrupts the parse. HTTP/1.0
// yields a Connection: close, unframed body.
http.useHTTP10(true);
http.setTimeout(20000);
int code = http.GET();
if (code != HTTP_CODE_OK) {
snprintf(reply, 160, "ERR: manifest HTTP %d", code);
http.end();
return false;
}
WiFiClient* stream = http.getStreamPtr();
stream->setTimeout(20000); // readBytes honours this, so a slow TLS link != EOF
JsonDocument doc;
DeserializationError err = deserializeJson(doc, *stream);
http.end();
if (err) {
snprintf(reply, 160, "ERR: manifest parse (%s)", err.c_str());
return false;
}
// Copy fields out before the document is reused/cleared.
char file_url[200] = {0}, avail_version[40] = {0}, avail_base[40] = {0}, avail_hash[24] = {0};
strncpy(file_url, doc["file"] | "", sizeof(file_url) - 1);
strncpy(avail_version, doc["version"] | "", sizeof(avail_version) - 1);
strncpy(avail_base, doc["baseVersion"] | "", sizeof(avail_base) - 1);
strncpy(avail_hash, doc["hash"] | "", sizeof(avail_hash) - 1);
int avail_build = doc["build"] | -1;
bool legacy_partition_change = doc["partitionChange"] | false;
char manifest_partsig[256] = {0};
strncpy(manifest_partsig, doc["partSig"] | "", sizeof(manifest_partsig) - 1);
doc.clear();
if (!file_url[0]) {
strcpy(reply, "ERR: manifest missing file");
return false;
}
// Partition compatibility: prefer the precise per-build signature — compare the
// target build's partition layout (manifest partSig) to what's actually flashed
// on THIS device. Refuse only on a real mismatch (OTA can't rewrite the table).
// Fall back to the legacy bool for manifests that predate partSig.
bool partition_change;
if (manifest_partsig[0]) {
char dev_partsig[256];
ota_partitionSignature(dev_partsig, sizeof(dev_partsig));
partition_change = (strcmp(dev_partsig, manifest_partsig) != 0);
} else {
partition_change = legacy_partition_change;
}
// --- Determine current-vs-available --------------------------------------
// Our running version token (e.g. "v1.16.0.5"), i.e. current_ver up to the
// first '-' (which precedes "-observer-<hash>").
char own_version[40] = {0};
for (size_t i = 0; current_ver && current_ver[i] && current_ver[i] != '-' && i < sizeof(own_version) - 1; i++) {
own_version[i] = current_ver[i];
}
char own_base[40];
int own_build;
ota_parseVersion(own_version, own_base, sizeof(own_base), &own_build);
// Fallback identity by commit hash (handles pre-build-number / local images that
// carry no 4th version component). Shared-prefix compare absorbs the 7- vs 8-char
// git abbreviation difference.
char cur_hash[24];
ota_extractHash(current_ver, cur_hash, sizeof(cur_hash));
size_t lh = strlen(avail_hash), lc = strlen(cur_hash);
size_t m = (lh < lc) ? lh : lc;
bool hash_equal = (m >= 7 && strncmp(avail_hash, cur_hash, m) == 0);
bool same_base = (own_base[0] && avail_base[0] && strcmp(own_base, avail_base) == 0);
bool have_builds = (own_build >= 0 && avail_build >= 0);
bool diff_base = (own_base[0] && avail_base[0] && !same_base);
int behind = 0;
bool up_to_date;
if (same_base && have_builds) {
behind = avail_build - own_build;
up_to_date = (behind <= 0);
} else if (diff_base) {
up_to_date = false; // different base version is always an update
} else {
up_to_date = hash_equal; // unknown build numbers -> fall back to hash
}
// Display strings carry the short commit hash in the same form as the asset
// filename, e.g. "v1.16.0.2 (5acfdd7)" (or just the hash if there's no version).
char avail_disp[72], own_disp[72];
if (avail_version[0]) snprintf(avail_disp, sizeof(avail_disp), "%s (%s)", avail_version, avail_hash);
else snprintf(avail_disp, sizeof(avail_disp), "%s", avail_hash);
if (own_version[0]) snprintf(own_disp, sizeof(own_disp), "%s (%s)", own_version, cur_hash);
else snprintf(own_disp, sizeof(own_disp), "%s", cur_hash);
const char* pc_note = partition_change ? " [partition change: cable flash]" : "";
// --- Report (dry run / `ota check`) --------------------------------------
// Returns true iff an OTA-applicable update is available (not up-to-date and not
// a partition-change build). `ota check` ignores the return and just shows the
// reply; `ota update` uses it to decide whether to actually schedule the flash.
if (dry_run) {
if (up_to_date) {
snprintf(reply, 160, "up to date: %s", avail_disp);
} else if (same_base && have_builds) {
snprintf(reply, 160, "update available: %s -> %s (%d behind)%s", own_disp, avail_disp, behind, pc_note);
} else if (diff_base) {
snprintf(reply, 160, "update available: %s -> %s (new base)%s", own_disp, avail_disp, pc_note);
} else {
snprintf(reply, 160, "update available: %s -> %s%s", own_disp, avail_disp, pc_note);
}
return (!up_to_date && !partition_change);
}
// --- Gates (real `ota update`) -------------------------------------------
if (partition_change) {
snprintf(reply, 160, "ERR: %s needs cable flash (partition change)", avail_disp);
return false;
}
if (up_to_date) {
snprintf(reply, 160, "OK: already up to date: %s", avail_disp);
return false;
}
// --- Stream the .bin (the manifest's full URL) into the inactive OTA slot -
Serial.printf("OTA: update %s -> %s\n", own_disp, avail_disp);
Serial.print("OTA: downloading "); Serial.println(file_url);
inhibit_sleep = true; // keep awake through the flash
WiFiClientSecure uclient;
#if ESP_ARDUINO_VERSION_MAJOR >= 3
uclient.setCACertBundle(rootca_crt_bundle_start, bundle_len);
#else
uclient.setCACertBundle(rootca_crt_bundle_start);
#endif
uclient.setTimeout(20000);
// Console progress to the USB serial (always on; MESH_DEBUG is off on the default
// observer profile). Non-capturing lambdas + a file-static decile, so the global
// httpUpdate object never holds a dangling reference after this function returns.
static int ota_progress_decile;
ota_progress_decile = -1;
httpUpdate.onProgress([](int cur, int total) {
if (total <= 0) return;
int d = (int)((int64_t)cur * 10 / total);
if (d != ota_progress_decile) { ota_progress_decile = d; Serial.printf("OTA: %d%%\n", d * 10); }
});
httpUpdate.onEnd([]() { Serial.println("OTA: write complete, rebooting..."); });
httpUpdate.rebootOnUpdate(true); // reboots into the new image on success
t_httpUpdate_return ret = httpUpdate.update(uclient, file_url);
// Only reached on failure (success reboots inside update()).
inhibit_sleep = false;
snprintf(reply, 160, "ERR: OTA failed (%d): %s", (int)ret,
httpUpdate.getLastErrorString().c_str());
Serial.print("OTA: FAILED - "); Serial.println(reply);
return false;
#endif // OTA_MANIFEST_BASE && OTA_VARIANT
}
#else
bool ESP32Board::otaFromManifest(const char* current_ver, bool dry_run, char reply[]) {
strcpy(reply, "ERR: not supported");
return false;
}
#endif // WITH_MQTT_BRIDGE
#endif
+5
View File
@@ -151,6 +151,11 @@ public:
}
bool startOTAUpdate(const char* id, char reply[]) override;
bool otaFromManifest(const char* current_ver, bool dry_run, char reply[]) override;
// Heavy body (TLS + JSON / HTTPUpdate). Runs in a dedicated large-stack task
// spawned by otaFromManifest() — public only so that task entry point can call
// it; not meant to be invoked directly.
bool otaFromManifestImpl(const char* current_ver, bool dry_run, char reply[]);
void setInhibitSleep(bool inhibit) {
inhibit_sleep = inhibit;
+6 -39
View File
@@ -401,28 +401,6 @@ int MQTTMessageBuilder::buildRawJSON(
return buildRawMessage(origin, origin_id, timestamp, raw_hex, buffer, buffer_size);
}
const char* MQTTMessageBuilder::getPacketTypeString(int packet_type) {
switch (packet_type) {
case 0: return "0"; // REQ
case 1: return "1"; // RESPONSE
case 2: return "2"; // TXT_MSG
case 3: return "3"; // ACK
case 4: return "4"; // ADVERT
case 5: return "5"; // GRP_TXT
case 6: return "6"; // GRP_DATA
case 7: return "7"; // ANON_REQ
case 8: return "8"; // PATH
case 9: return "9"; // TRACE
case 10: return "10"; // MULTIPART
case 11: return "11"; // Type11
case 12: return "12"; // Type12
case 13: return "13"; // Type13
case 14: return "14"; // Type14
case 15: return "15"; // RAW_CUSTOM
default: return "0";
}
}
const char* MQTTMessageBuilder::getRouteTypeString(int route_type) {
switch (route_type) {
case 0: return "F"; // FLOOD
@@ -432,26 +410,15 @@ const char* MQTTMessageBuilder::getRouteTypeString(int route_type) {
}
}
void MQTTMessageBuilder::formatTimestamp(unsigned long timestamp, char* buffer, size_t buffer_size) {
// Simplified timestamp formatting - in real implementation would use proper time
snprintf(buffer, buffer_size, "2024-01-01T12:00:00.000000");
}
void MQTTMessageBuilder::formatTime(unsigned long timestamp, char* buffer, size_t buffer_size) {
// Simplified time formatting
snprintf(buffer, buffer_size, "12:00:00");
}
void MQTTMessageBuilder::formatDate(unsigned long timestamp, char* buffer, size_t buffer_size) {
// Simplified date formatting
snprintf(buffer, buffer_size, "01/01/2024");
}
void MQTTMessageBuilder::bytesToHex(const uint8_t* data, size_t len, char* hex, size_t hex_size) {
if (hex_size < len * 2 + 1) return;
// Nibble lookup instead of a per-byte snprintf("%02X"): same uppercase hex
// output, but avoids re-parsing the format string up to ~512 times per publish.
static const char HEX_DIGITS[] = "0123456789ABCDEF";
for (size_t i = 0; i < len; i++) {
snprintf(hex + i * 2, 3, "%02X", data[i]);
hex[i * 2] = HEX_DIGITS[data[i] >> 4];
hex[i * 2 + 1] = HEX_DIGITS[data[i] & 0x0F];
}
hex[len * 2] = '\0';
}
-23
View File
@@ -19,9 +19,6 @@
* regex fields.
*/
class MQTTMessageBuilder {
private:
static const int JSON_BUFFER_SIZE = 1024;
public:
/**
* Format the MQTT JSON `timestamp` field (same rule for status, packet, raw).
@@ -214,31 +211,11 @@ public:
);
private:
/**
* Convert packet type to string
*/
static const char* getPacketTypeString(int packet_type);
/**
* Convert route type to string
*/
static const char* getRouteTypeString(int route_type);
/**
* Format timestamp to ISO 8601 format
*/
static void formatTimestamp(unsigned long timestamp, char* buffer, size_t buffer_size);
/**
* Format time to HH:MM:SS format
*/
static void formatTime(unsigned long timestamp, char* buffer, size_t buffer_size);
/**
* Format date to DD/MM/YYYY format
*/
static void formatDate(unsigned long timestamp, char* buffer, size_t buffer_size);
/**
* Convert bytes to hex string (uppercase)
*/
+4 -1
View File
@@ -105,7 +105,7 @@ static const char ISRG_ROOT_X1[] PROGMEM =
"-----END CERTIFICATE-----\n";
// Number of built-in presets
static const int MQTT_PRESET_COUNT = 22;
static const int MQTT_PRESET_COUNT = 25;
// Built-in preset definitions (stored in flash)
static const MQTTPresetDef MQTT_PRESETS[MQTT_PRESET_COUNT] = {
@@ -129,9 +129,12 @@ static const MQTTPresetDef MQTT_PRESETS[MQTT_PRESET_COUNT] = {
{ "dutchmeshcore-2", "wss://collector2.dutchmeshcore.nl:443/mqtt", "collector2.dutchmeshcore.nl", GTS_ROOT_R4, MQTT_AUTH_JWT, MQTT_TOPIC_MESHCORE, 0, true, 55, nullptr, nullptr },
{ "meshcore-ca-1", "wss://mqtt1.meshcore.ca:443/mqtt", "mqtt1.meshcore.ca", ISRG_ROOT_X1, MQTT_AUTH_JWT, MQTT_TOPIC_MESHCORE, 0, true, 55, nullptr, nullptr },
{ "meshcore-ca-2", "wss://mqtt2.meshcore.ca:443/mqtt", "mqtt2.meshcore.ca", ISRG_ROOT_X1, MQTT_AUTH_JWT, MQTT_TOPIC_MESHCORE, 0, true, 55, nullptr, nullptr },
{ "meshcore-fi", "wss://mc-mqtt.meshcore.fi:443/", "mc-mqtt.meshcore.fi", ISRG_ROOT_X1, MQTT_AUTH_JWT, MQTT_TOPIC_MESHCORE, 0, true, 55, nullptr, nullptr },
{ "inwmesh", "mqtts://scope.inwmesh.org:8883", nullptr, ISRG_ROOT_X1, MQTT_AUTH_USERPASS, MQTT_TOPIC_MESHCORE, 0, true, 55, nullptr, nullptr },
{ "bostonmesh", "wss://mqttmc01.bostonme.sh:443/mqtt", "mqttmc01.bostonme.sh", GTS_ROOT_R4, MQTT_AUTH_JWT, MQTT_TOPIC_MESHCORE, 0, true, 55, nullptr, nullptr },
{ "rflab", "wss://mqtt.rflab.io:443", "mqtt.rflab.io", ISRG_ROOT_X1, MQTT_AUTH_JWT, MQTT_TOPIC_MESHCORE, 0, true, 55, nullptr, nullptr },
{ "ipnt.uk", "wss://mqtt.ipnt.uk:443", "mqtt.ipnt.uk", ISRG_ROOT_X1, MQTT_AUTH_JWT, MQTT_TOPIC_MESHCORE, 0, true, 55, nullptr, nullptr },
{ "flmesh", "wss://mcmqtt.jntconnections.com:443", "mcmqtt.jntconnections.com", GTS_ROOT_R4, MQTT_AUTH_JWT, MQTT_TOPIC_MESHCORE, 0, true, 55, nullptr, nullptr },
};
// Find a preset by name, returns nullptr if not found
+270 -55
View File
@@ -7,6 +7,7 @@
#include <time.h>
#include <sys/time.h>
#include <math.h>
#include <strings.h>
#ifdef WITH_SNMP
#include "../SNMPAgent.h"
@@ -34,6 +35,50 @@ static void applyEffectiveOrigin(const NodePrefs* prefs, char* dest, size_t dest
StrHelper::stripSurroundingQuotes(dest, dest_size);
}
static const char* const kNtpBuiltinFallbacks[] = {
"pool.ntp.org",
"time.google.com",
"time.cloudflare.com",
"time.aws.com",
"time.nist.gov",
};
static constexpr size_t kNtpBuiltinFallbackCount =
sizeof(kNtpBuiltinFallbacks) / sizeof(kNtpBuiltinFallbacks[0]);
static_assert(MQTTBridge::kMaxNtpServers >= 1 + (int)kNtpBuiltinFallbackCount,
"kMaxNtpServers must hold the custom primary plus all built-in fallbacks");
static bool ntpHostnameEquals(const char* a, const char* b) {
if (!a || !b) return false;
return strcasecmp(a, b) == 0;
}
static void fillNtpServerList(const NodePrefs* prefs, const char* servers[], int& count) {
count = 0;
if (prefs && prefs->mqtt_ntp_server[0] != '\0') {
servers[count++] = prefs->mqtt_ntp_server;
}
for (size_t i = 0; i < kNtpBuiltinFallbackCount && count < MQTTBridge::kMaxNtpServers; i++) {
const char* fb = kNtpBuiltinFallbacks[i];
bool dup = false;
for (int j = 0; j < count; j++) {
if (ntpHostnameEquals(servers[j], fb)) {
dup = true;
break;
}
}
if (!dup) {
servers[count++] = fb;
}
}
}
const char* MQTTBridge::effectiveNtpPrimary(const NodePrefs* prefs) {
if (prefs && prefs->mqtt_ntp_server[0] != '\0') {
return prefs->mqtt_ntp_server;
}
return kNtpBuiltinFallbacks[0];
}
void MQTTBridge::refreshOriginFromPrefs() {
if (!_prefs) return;
applyEffectiveOrigin(_prefs, _origin, sizeof(_origin));
@@ -343,7 +388,9 @@ MQTTBridge::MQTTBridge(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTCCloc
: BridgeBase(prefs, mgr, rtc),
_queue_count(0),
_last_status_publish(0), _last_status_retry(0), _status_interval(300000),
_ntp_client(_ntp_udp, "pool.ntp.org", 0, 60000), _last_ntp_sync(0), _ntp_synced(false), _ntp_sync_pending(false), _slots_setup_done(false), _max_active_slots(RUNTIME_MQTT_SLOTS),
_ntp_client(_ntp_udp, effectiveNtpPrimary(prefs), 0, 60000), _last_ntp_sync(0), _ntp_synced(false), _ntp_sync_pending(false), _slots_setup_done(false), _max_active_slots(RUNTIME_MQTT_SLOTS),
_ntp_force_requested(false), _ntp_force_done(false), _ntp_force_result(false),
_ntp_diag_requested(false), _ntp_diag_done(false), _ntp_diag_count(0),
// Default to UTC; setRules() will be called from syncTimeWithNTP when a
// non-UTC timezone string is configured. Timezone has no default ctor,
// so we must pass rules here.
@@ -402,6 +449,16 @@ MQTTBridge::MQTTBridge(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTCCloc
_slot_reconfigure_pending[i] = false;
}
// Reset CLI-requested forced NTP sync handshake (bridge object is reused across restarts)
_ntp_force_requested = false;
_ntp_force_done = false;
_ntp_force_result = false;
// Reset CLI-requested NTP diagnostic handshake
_ntp_diag_requested = false;
_ntp_diag_done = false;
_ntp_diag_count = 0;
// Initialize JWT username
_jwt_username[0] = '\0';
@@ -754,27 +811,44 @@ void MQTTBridge::initializeWiFiInTask() {
WiFi.setAutoReconnect(true);
WiFi.setAutoConnect(true);
// Set up WiFi event handlers for better diagnostics and immediate disconnection detection
WiFi.onEvent([this](WiFiEvent_t event, WiFiEventInfo_t info) {
switch(event) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
MQTT_DEBUG_PRINTLN("WiFi connected: %s", IPAddress(info.got_ip.ip_info.ip.addr).toString().c_str());
// Set flag to trigger NTP sync from loop() instead of doing it here
if (!_ntp_synced && !_ntp_sync_pending) {
_ntp_sync_pending = true;
}
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
s_wifi_disconnect_reason = info.wifi_sta_disconnected.reason;
s_wifi_disconnect_time = millis();
MQTT_DEBUG_PRINTLN("WiFi disconnected: reason %d", s_wifi_disconnect_reason);
break;
default:
break;
}
});
// Set up WiFi event handlers for better diagnostics and immediate disconnection
// detection. Register ONCE — the bridge is reused across restarts (e.g. stopped
// for `ota check`/`ota update`, or `set mqtt…` reconfigure) and WiFi.onEvent()
// never removes prior callbacks, so re-registering leaks handlers and duplicates
// every log line.
if (!_wifi_event_registered) {
WiFi.onEvent([this](WiFiEvent_t event, WiFiEventInfo_t info) {
switch(event) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
MQTT_DEBUG_PRINTLN("WiFi connected: %s", IPAddress(info.got_ip.ip_info.ip.addr).toString().c_str());
// Set flag to trigger NTP sync from loop() instead of doing it here
if (!_ntp_synced && !_ntp_sync_pending) {
_ntp_sync_pending = true;
}
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
s_wifi_disconnect_reason = info.wifi_sta_disconnected.reason;
s_wifi_disconnect_time = millis();
MQTT_DEBUG_PRINTLN("WiFi disconnected: reason %d", s_wifi_disconnect_reason);
break;
default:
break;
}
});
_wifi_event_registered = true;
}
WiFi.begin(_prefs->wifi_ssid, _prefs->wifi_password);
// Only (re)start the WiFi association if it isn't already up. end() leaves the
// STA link connected, so on a restart (e.g. after `ota check`) calling
// WiFi.begin() again forces a needless disconnect/reconnect — which also races
// the MQTT task's first DNS lookup (getaddrinfo fails until WiFi/DNS recovers).
// When already connected, the deferred slot setup still fires in mqttTaskLoop()
// because _ntp_synced persists across end() (only _slots_setup_done is reset).
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(_prefs->wifi_ssid, _prefs->wifi_password);
} else if (!_ntp_synced && !_ntp_sync_pending) {
_ntp_sync_pending = true; // already connected but never synced — kick NTP now
}
// NOTE: Slot setup is deferred until after NTP sync in mqttTaskLoop().
// JWT-auth slots need valid timestamps for token creation, and connecting
@@ -842,6 +916,25 @@ void MQTTBridge::mqttTaskLoop() {
}
}
// Process a CLI-requested forced NTP sync (queued from Core 1). Running it here
// keeps all NTP I/O on Core 0; requestForcedNtpSync() blocks the CLI thread until
// we publish the outcome below.
if (_ntp_force_requested) {
_ntp_force_requested = false;
// primary_only: validate just the server that was set, so a typo fails fast.
bool ok = syncTimeWithNTP(true, /*primary_only=*/true);
_ntp_force_result = ok;
_ntp_force_done = true; // set last so the waiter sees a consistent result
}
// Process a CLI-requested NTP connectivity diagnostic (queued from Core 1).
// Probe-only — never touches the system clock.
if (_ntp_diag_requested) {
_ntp_diag_requested = false;
runNtpDiagProbe();
_ntp_diag_done = true; // set last so the waiter sees populated results
}
// Deferred slot setup: wait until NTP is synced so JWT tokens get valid timestamps.
// This avoids wasted TLS handshakes that get rejected due to bad token times.
if (_ntp_synced && !_slots_setup_done) {
@@ -2810,52 +2903,70 @@ void MQTTBridge::refreshNTP() {
// Lightweight periodic refresh: just restart SNTP which runs async in the background.
// No blocking DNS, no UDP sockets, no retry loops on the MQTT task loop.
// The heavy syncTimeWithNTP() is only used for initial sync and WiFi reconnect recovery.
configTime(0, 0, "pool.ntp.org");
configTime(0, 0, effectiveNtpPrimary(_prefs));
_last_ntp_sync = millis();
MQTT_DEBUG_PRINTLN("NTP refresh triggered (async SNTP)");
}
void MQTTBridge::syncTimeWithNTP() {
bool MQTTBridge::syncTimeWithNTP(bool force, bool primary_only) {
if (!WiFi.isConnected()) {
MQTT_DEBUG_PRINTLN("Cannot sync time - WiFi not connected");
return;
return false;
}
unsigned long now = millis();
if (_ntp_synced && (now - _last_ntp_sync) < 5000) {
return;
if (!force && _ntp_synced && (now - _last_ntp_sync) < 5000) {
return false;
}
static bool sync_in_progress = false;
if (sync_in_progress) {
return;
return false;
}
sync_in_progress = true;
MQTT_DEBUG_PRINTLN("Syncing time with NTP...");
#ifdef ESP_PLATFORM
IPAddress resolved_ip;
if (!WiFi.hostByName("pool.ntp.org", resolved_ip)) {
MQTT_DEBUG_PRINTLN("WARNING: DNS resolution failed for pool.ntp.org - NTP sync may fail");
const char* servers[kMaxNtpServers];
int server_count = 0;
if (primary_only) {
// Validation path (e.g. set mqtt.ntp): test only the configured primary so a
// typo fails fast instead of walking the entire fallback list.
servers[0] = effectiveNtpPrimary(_prefs);
server_count = 1;
} else {
fillNtpServerList(_prefs, servers, server_count);
}
#endif
bool ntp_ok = false;
unsigned long epochTime = 0;
const unsigned long kMinValidEpoch = 1767225600; // 2026-01-01 00:00:00 UTC
const char* ntp_server_used = nullptr;
_ntp_client.begin();
const int kMaxNtpRetries = 3;
for (int attempt = 1; attempt <= kMaxNtpRetries && !ntp_ok; attempt++) {
if (attempt > 1) {
MQTT_DEBUG_PRINTLN("NTP retry %d/%d...", attempt, kMaxNtpRetries);
delay(1000);
const int kMaxNtpRetriesPerServer = 2;
for (int s = 0; s < server_count && !ntp_ok; s++) {
const char* server = servers[s];
_ntp_client.setPoolServerName(server);
#ifdef ESP_PLATFORM
IPAddress resolved_ip;
if (!WiFi.hostByName(server, resolved_ip)) {
MQTT_DEBUG_PRINTLN("WARNING: DNS resolution failed for %s - NTP sync may fail", server);
}
if (_ntp_client.forceUpdate()) {
epochTime = _ntp_client.getEpochTime();
if (epochTime >= kMinValidEpoch) {
ntp_ok = true;
#endif
for (int attempt = 1; attempt <= kMaxNtpRetriesPerServer && !ntp_ok; attempt++) {
if (attempt > 1) {
MQTT_DEBUG_PRINTLN("NTP retry %d/%d on %s...", attempt, kMaxNtpRetriesPerServer, server);
delay(1000);
}
if (_ntp_client.forceUpdate()) {
epochTime = _ntp_client.getEpochTime();
if (epochTime >= kMinValidEpoch) {
ntp_ok = true;
ntp_server_used = server;
}
}
}
}
@@ -2865,21 +2976,26 @@ void MQTTBridge::syncTimeWithNTP() {
#ifdef ESP_PLATFORM
if (!ntp_ok) {
MQTT_DEBUG_PRINTLN("NTP client failed, trying SNTP fallback...");
configTime(0, 0, "pool.ntp.org");
for (int i = 0; i < 20; i++) {
delay(500);
epochTime = (unsigned long)time(nullptr);
if (epochTime >= kMinValidEpoch) {
ntp_ok = true;
MQTT_DEBUG_PRINTLN("SNTP fallback succeeded: %lu", epochTime);
break;
for (int s = 0; s < server_count && !ntp_ok; s++) {
const char* server = servers[s];
MQTT_DEBUG_PRINTLN("SNTP fallback trying %s...", server);
configTime(0, 0, server);
for (int i = 0; i < 20; i++) {
delay(500);
epochTime = (unsigned long)time(nullptr);
if (epochTime >= kMinValidEpoch) {
ntp_ok = true;
ntp_server_used = server;
MQTT_DEBUG_PRINTLN("SNTP fallback succeeded on %s: %lu", server, epochTime);
break;
}
}
}
}
#endif
if (ntp_ok) {
configTime(0, 0, "pool.ntp.org");
if (ntp_ok && ntp_server_used) {
configTime(0, 0, ntp_server_used);
if (_rtc) {
_rtc->setCurrentTime(epochTime);
@@ -2890,7 +3006,7 @@ void MQTTBridge::syncTimeWithNTP() {
_last_ntp_sync = millis();
sync_in_progress = false;
MQTT_DEBUG_PRINTLN("Time synced: %lu", epochTime);
MQTT_DEBUG_PRINTLN("Time synced: %lu (via %s)", epochTime, ntp_server_used);
// If slots are already set up and the time jumped significantly (e.g., SNTP
// initially returned stale RTC time, then a later sync corrected it), tear down
@@ -2932,10 +3048,109 @@ void MQTTBridge::syncTimeWithNTP() {
(void)gmtime((time_t*)&epochTime);
(void)localtime((time_t*)&epochTime);
} else {
MQTT_DEBUG_PRINTLN("NTP sync failed");
sync_in_progress = false;
return true;
}
MQTT_DEBUG_PRINTLN("NTP sync failed");
sync_in_progress = false;
return false;
}
bool MQTTBridge::requestForcedNtpSync(uint32_t timeout_ms) {
if (!isRunning()) return false;
// Publish the request to the MQTT task. Clear the completion flags before
// raising _ntp_force_requested so the task can't observe a stale result.
_ntp_force_done = false;
_ntp_force_result = false;
_ntp_force_requested = true;
unsigned long start = millis();
while (!_ntp_force_done) {
if (millis() - start >= timeout_ms) {
MQTT_DEBUG_PRINTLN("Forced NTP sync timed out waiting for MQTT task");
return false; // task still running; result is ignored when it eventually completes
}
vTaskDelay(pdMS_TO_TICKS(50));
}
return _ntp_force_result;
}
// Runs on the MQTT task (Core 0). Probes every configured NTP server for connectivity
// and records the time each reports. Deliberately does NOT call configTime() or update
// the RTC — this is a read-only diagnostic and must leave the system clock untouched.
void MQTTBridge::runNtpDiagProbe() {
const char* servers[kMaxNtpServers];
int count = 0;
fillNtpServerList(_prefs, servers, count);
_ntp_client.begin();
for (int i = 0; i < count; i++) {
_ntp_client.setPoolServerName(servers[i]);
bool ok = _ntp_client.forceUpdate();
NtpDiagResult& r = _ntp_diag_results[i];
strncpy(r.server, servers[i], sizeof(r.server) - 1);
r.server[sizeof(r.server) - 1] = '\0';
r.ok = ok;
r.epoch = ok ? (uint32_t)_ntp_client.getEpochTime() : 0;
}
_ntp_diag_count = count;
}
bool MQTTBridge::ntpDiag(char* reply, size_t reply_size, bool verbose) {
if (!isRunning() || reply == nullptr || reply_size == 0) return false;
// Marshal the probe onto the MQTT task (Core 0); clear the completion flag first.
_ntp_diag_done = false;
_ntp_diag_requested = true;
unsigned long start = millis();
while (!_ntp_diag_done) {
if (millis() - start >= 30000) {
snprintf(reply, reply_size, "Error: NTP diag timed out");
return true;
}
vTaskDelay(pdMS_TO_TICKS(50));
}
int ok_count = 0;
for (int i = 0; i < _ntp_diag_count; i++) {
if (_ntp_diag_results[i].ok) ok_count++;
}
if (verbose) {
// Detailed table to the serial console; reply carries a short summary (the operator
// sees the table above the "-> <reply>" line). Mirrors the dumpLogFile() convention.
Serial.printf("NTP diag - %d server(s):\r\n", _ntp_diag_count);
for (int i = 0; i < _ntp_diag_count; i++) {
const NtpDiagResult& r = _ntp_diag_results[i];
if (r.ok) {
time_t t = (time_t)r.epoch;
struct tm* tmv = gmtime(&t);
Serial.printf(" %-20s OK %04d-%02d-%02d %02d:%02d:%02d UTC\r\n",
r.server, tmv->tm_year + 1900, tmv->tm_mon + 1, tmv->tm_mday,
tmv->tm_hour, tmv->tm_min, tmv->tm_sec);
} else {
Serial.printf(" %-20s FAIL\r\n", r.server);
}
}
snprintf(reply, reply_size, "> NTP diag: %d/%d OK (see console)", ok_count, _ntp_diag_count);
} else {
// Compact "<server> ok|fail" list for LoRa, bounded to reply_size.
size_t used = 0;
reply[0] = '\0';
for (int i = 0; i < _ntp_diag_count; i++) {
const NtpDiagResult& r = _ntp_diag_results[i];
int n = snprintf(reply + used, reply_size - used, "%s%s %s",
used ? "\n" : "", r.server, r.ok ? "ok" : "fail");
if (n < 0 || (size_t)n >= reply_size - used) {
reply[used] = '\0'; // out of room — truncate cleanly
break;
}
used += (size_t)n;
}
}
return true;
}
// ---------------------------------------------------------------------------
+54 -1
View File
@@ -56,6 +56,10 @@ class MeshSNMPAgent; // Forward declaration
* - Available presets: analyzer-us, analyzer-eu, meshmapper, custom, none
*/
class MQTTBridge : public BridgeBase {
public:
// Max NTP servers in a try-list: 1 custom primary + the built-in fallbacks.
static const int kMaxNtpServers = 6;
private:
static const size_t AUTH_TOKEN_SIZE = 768;
@@ -180,11 +184,41 @@ private:
bool _ntp_synced;
bool _ntp_sync_pending; // Flag to trigger NTP sync from loop() instead of event handler
bool _slots_setup_done; // Deferred: slots set up after NTP sync
// WiFi.onEvent() handler registered once and never removed by end(); the bridge
// object is reused across restarts, so re-registering would leak handlers and
// duplicate every connect/disconnect log line. Inline-initialised so it survives
// construction and is NOT reset by end().
bool _wifi_event_registered = false;
int _max_active_slots; // Runtime limit: 5 with PSRAM, 2 without
// Pending slot reconfigure: set from CLI (Core 1), processed by MQTT task (Core 0)
volatile bool _slot_reconfigure_pending[RUNTIME_MQTT_SLOTS];
// CLI-requested forced NTP sync, marshalled onto the MQTT task (Core 0).
// All NTP I/O (_ntp_client, configTime) must run on Core 0; the CLI thread
// (Core 1) sets _ntp_force_requested and blocks in requestForcedNtpSync()
// until the task publishes the outcome via _ntp_force_result/_ntp_force_done.
// Single-requester assumption: CLI commands are serialized, so at most one
// forced sync is outstanding at a time.
volatile bool _ntp_force_requested;
volatile bool _ntp_force_done;
volatile bool _ntp_force_result;
// CLI-requested NTP connectivity diagnostic, marshalled onto the MQTT task (Core 0)
// with the same handshake as the forced sync. Probe-only: it queries each server and
// records the reported time but never calls configTime()/setCurrentTime(), so the
// system clock is left untouched. Results are written by the task and read by the CLI
// thread once _ntp_diag_done is set.
volatile bool _ntp_diag_requested;
volatile bool _ntp_diag_done;
struct NtpDiagResult {
char server[64];
bool ok;
uint32_t epoch; // server-reported UTC epoch when ok
};
NtpDiagResult _ntp_diag_results[kMaxNtpServers];
int _ntp_diag_count;
// Timezone handling.
// _timezone_storage is inline class storage (zero heap) that is reconfigured
// via setRules() whenever the preferred timezone string changes. _timezone
@@ -326,8 +360,8 @@ private:
void queuePacket(mesh::Packet* packet, bool is_tx);
void dequeuePacket();
bool isAnySlotConnected();
void syncTimeWithNTP();
void refreshNTP(); // Lightweight periodic NTP refresh (non-blocking)
void runNtpDiagProbe(); // Probe every server for connectivity; never sets the clock. Core 0 only.
// Populates dst_out/std_out with TimeChangeRules for the given IANA or
// abbreviation string. Returns false if the string is not recognized
// (callers should fall back to UTC). Zero-allocation.
@@ -406,6 +440,25 @@ public:
static int getRuntimeSlotCount() { return RUNTIME_MQTT_SLOTS; }
/** Resolved origin for MQTT JSON: node_name when mqtt_origin is empty, else mqtt_origin (with quote stripping). */
static void getEffectiveMqttOrigin(const NodePrefs* prefs, char* buf, size_t buf_size);
static const char* effectiveNtpPrimary(const NodePrefs* prefs);
/** Sync system clock via NTP. force=true bypasses the 5s post-sync rate limit.
* primary_only=true tests just the effective primary server (no fallback walk) so a
* mistyped hostname fails fast instead of blocking through the whole fallback list.
* Performs blocking NTP I/O and must only be called from the MQTT task (Core 0).
* Other tasks (e.g. the CLI on Core 1) must use requestForcedNtpSync() instead. */
bool syncTimeWithNTP(bool force = false, bool primary_only = false);
/** Request a forced NTP sync from another task (e.g. CLI on Core 1). Marshals the
* work onto the MQTT task so all NTP I/O stays on Core 0, then blocks up to
* timeout_ms for the result. Returns true if the sync succeeded, false on failure,
* timeout, or if the bridge is not running. */
bool requestForcedNtpSync(uint32_t timeout_ms = 30000);
/** Probe every configured NTP server (custom primary + built-in fallbacks) for
* connectivity and report each server's reported time WITHOUT touching the system
* clock. Marshals the probe onto the MQTT task (Core 0), then formats on the caller's
* thread. verbose=true prints a detailed table to the serial console and leaves a short
* summary in reply; verbose=false fills reply with a compact "<server> ok|fail" list
* (for LoRa). Returns false if the bridge is not running. */
bool ntpDiag(char* reply, size_t reply_size, bool verbose);
static void formatMqttStatusReply(char* buf, size_t bufsize, const NodePrefs* prefs);
/** True when WiFi is set and at least one MQTT slot can run (preset + custom host if needed). */
static bool isConfigValid(const NodePrefs* prefs);