From 7416d632b907c7432ccace1a7ca392c9f868a627 Mon Sep 17 00:00:00 2001 From: agessaman Date: Wed, 24 Jun 2026 17:14:11 -0700 Subject: [PATCH] feat(mqtt): add NTP server configuration and diagnostics commands Implemented new commands for configuring and diagnosing NTP server settings in the MQTT bridge. Users can now set a custom NTP server and probe connectivity to configured servers. This enhancement improves time synchronization reliability for JWT authentication and provides better diagnostics for NTP connectivity issues. --- MQTT_IMPLEMENTATION.md | 12 +- docs/cli_commands.md | 27 +++ examples/simple_repeater/MyMesh.h | 15 ++ examples/simple_room_server/MyMesh.h | 15 ++ src/helpers/CommonCLI.cpp | 62 +++++++ src/helpers/CommonCLI.h | 18 ++ src/helpers/bridges/MQTTBridge.cpp | 268 +++++++++++++++++++++++---- src/helpers/bridges/MQTTBridge.h | 50 ++++- 8 files changed, 429 insertions(+), 38 deletions(-) diff --git a/MQTT_IMPLEMENTATION.md b/MQTT_IMPLEMENTATION.md index d218eef9b..6695ed193 100644 --- a/MQTT_IMPLEMENTATION.md +++ b/MQTT_IMPLEMENTATION.md @@ -378,6 +378,8 @@ These settings apply across all MQTT slots: - `get mqtt.rx` - Get RX packet uplinking setting (on/off) - `get mqtt.tx` - Get TX packet uplinking setting (on/off/advert) - `get mqtt.interval` - Get status publish interval +- `get mqtt.ntp` - Get effective NTP server hostname +- `get mqtt.ntp.diag` - Probe every configured NTP server for connectivity (does not change the clock; serial console shows each server's reported time, LoRa shows a compact ` ok|fail` list) - `get mqtt.owner` - Get owner public key (serial console only) - `get mqtt.email` - Get owner email address (serial console only) @@ -393,6 +395,7 @@ These settings apply across all MQTT slots: - `advert` - Uplink only this node's own advert packets (self-originated) - `off` - Disable TX packet uplinking - `set mqtt.interval ` - Set status publish interval (1-60 minutes) +- `set mqtt.ntp ` - Set custom NTP server (validated with immediate sync); `none` reverts to default - `set mqtt.owner <64-hex-char-public-key>` - Set owner public key - `set mqtt.email ` - Set owner email address @@ -607,8 +610,13 @@ Minimal raw packet data for map integration. - Automatic reconnection with exponential backoff ### NTP Time Synchronization -- Automatic time synchronization with NTP servers -- Periodic time updates (every hour) +- Automatic time synchronization with NTP servers (required for JWT authentication) +- Default primary: `pool.ntp.org`; built-in fallbacks (tried sequentially on failure): `time.google.com`, `time.cloudflare.com`, `time.aws.com`, `time.nist.gov` +- Custom primary via `set mqtt.ntp `; `set mqtt.ntp none` reverts to default +- `set mqtt.ntp` runs an immediate sync (primary only, so a typo fails fast) when WiFi is connected and the bridge is running +- `get mqtt.ntp` returns the effective primary hostname +- `get mqtt.ntp.diag` probes every configured server (primary + fallbacks) for connectivity and reports each server's time without changing the system clock — a pure diagnostic +- Periodic time updates (every hour) on the effective primary only - Proper UTC system time handling ### Authentication diff --git a/docs/cli_commands.md b/docs/cli_commands.md index 32aeb95c2..b0badbcd3 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -1076,6 +1076,33 @@ region save --- +#### View or change the NTP server (MQTT observer only) +**Usage:** +- `get mqtt.ntp` +- `set mqtt.ntp ` +- `set mqtt.ntp none` + +**Description:** Sets the primary NTP server used for clock sync (required for JWT MQTT auth). On `set`, the device attempts an immediate sync of the just-configured server (primary only, so a typo fails fast) when WiFi is connected and the MQTT bridge is running. + +**Fallbacks:** If the primary fails, the firmware tries `pool.ntp.org`, `time.google.com`, `time.cloudflare.com`, `time.aws.com`, and `time.nist.gov` in order (skipping duplicates). + +**Default:** `pool.ntp.org` (when unset or `none`) + +--- + +#### Diagnose NTP server connectivity (MQTT observer only) +**Usage:** +- `get mqtt.ntp.diag` + +**Description:** Probes every configured NTP server (the custom primary, if set, plus the built-in fallbacks) and reports whether each responds. This is a pure connectivity diagnostic — it does **not** change the system clock. + +- **Serial console:** prints a detailed table with each server's reported UTC time (or `FAIL`). +- **Over LoRa:** returns a compact ` ok|fail` list, one per line. + +Requires WiFi connected and the MQTT bridge running. + +--- + #### View or change the speed of the bridge (RS-232 only) **Usage:** - `get bridge.baud` diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 09ee140e7..9ef04ac8b 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -319,6 +319,21 @@ public: int getQueueSize() override { return bridge ? bridge->getQueueSize() : 0; } + + bool isMqttBridgeRunning() override { + return bridge && bridge->isRunning(); + } + + bool syncMqttNtp() override { + if (!bridge || !bridge->isRunning()) return false; + // Marshal onto the MQTT task (Core 0); this runs on the CLI thread (Core 1). + return bridge->requestForcedNtpSync(); + } + + bool runMqttNtpDiag(char* reply, size_t reply_size, bool verbose) override { + if (!bridge || !bridge->isRunning()) return false; + return bridge->ntpDiag(reply, reply_size, verbose); + } #endif // To check if there is pending work diff --git a/examples/simple_room_server/MyMesh.h b/examples/simple_room_server/MyMesh.h index afc4828cf..72e5ff969 100644 --- a/examples/simple_room_server/MyMesh.h +++ b/examples/simple_room_server/MyMesh.h @@ -296,5 +296,20 @@ public: int getQueueSize() override { return bridge ? bridge->getQueueSize() : 0; } + + bool isMqttBridgeRunning() override { + return bridge && bridge->isRunning(); + } + + bool syncMqttNtp() override { + if (!bridge || !bridge->isRunning()) return false; + // Marshal onto the MQTT task (Core 0); this runs on the CLI thread (Core 1). + return bridge->requestForcedNtpSync(); + } + + bool runMqttNtpDiag(char* reply, size_t reply_size, bool verbose) override { + if (!bridge || !bridge->isRunning()) return false; + return bridge->ntpDiag(reply, reply_size, verbose); + } #endif }; diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 99849b910..507736381 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -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 @@ -1466,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(); @@ -2020,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. diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index a158324c7..49f7fe04f 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -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. @@ -300,6 +304,20 @@ public: 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 " 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 }; diff --git a/src/helpers/bridges/MQTTBridge.cpp b/src/helpers/bridges/MQTTBridge.cpp index 0c1ff86b9..3c3ac91f3 100644 --- a/src/helpers/bridges/MQTTBridge.cpp +++ b/src/helpers/bridges/MQTTBridge.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #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'; @@ -859,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) { @@ -2827,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; + } } } } @@ -2882,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); @@ -2907,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 @@ -2949,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 "-> " 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 " 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; } // --------------------------------------------------------------------------- diff --git a/src/helpers/bridges/MQTTBridge.h b/src/helpers/bridges/MQTTBridge.h index e19e0544a..b9df91ab8 100644 --- a/src/helpers/bridges/MQTTBridge.h +++ b/src/helpers/bridges/MQTTBridge.h @@ -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; @@ -190,6 +194,31 @@ private: // 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 @@ -331,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. @@ -411,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 " 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);