diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 4e486b629..ea82b4ff6 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -39,6 +39,9 @@ void CommonCLI::loadPrefs(FILESYSTEM* fs) { loadPrefsInt(fs, "/node_prefs"); savePrefs(fs); // save to new filename fs->remove("/node_prefs"); // remove old + } else { + // File doesn't exist - set default bridge settings for fresh installs + _prefs->bridge_pkt_src = 1; // Default to RX (logRx) for new installs } #ifdef WITH_MQTT_BRIDGE // Load MQTT preferences from separate file diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index 8aa8b2c57..6bf452c32 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -38,7 +38,7 @@ struct NodePrefs { // persisted to file // Bridge settings uint8_t bridge_enabled; // boolean uint16_t bridge_delay; // milliseconds (default 500 ms) - uint8_t bridge_pkt_src; // 0 = logTx, 1 = logRx (default logTx) + uint8_t bridge_pkt_src; // 0 = logTx, 1 = logRx (default logRx) uint32_t bridge_baud; // 9600, 19200, 38400, 57600, 115200 (default 115200) uint8_t bridge_channel; // 1-14 (ESP-NOW only) char bridge_secret[16]; // for XOR encryption of bridge packets (ESP-NOW only) diff --git a/src/helpers/MQTTMessageBuilder.cpp b/src/helpers/MQTTMessageBuilder.cpp index b85e4a688..95edcc104 100644 --- a/src/helpers/MQTTMessageBuilder.cpp +++ b/src/helpers/MQTTMessageBuilder.cpp @@ -23,7 +23,8 @@ int MQTTMessageBuilder::buildStatusMessage( int tx_air_secs, int rx_air_secs ) { - DynamicJsonDocument doc(768); // Increased size to accommodate stats + // Use StaticJsonDocument to avoid heap fragmentation (fixed-size stack allocation) + StaticJsonDocument<768> doc; // Increased size to accommodate stats JsonObject root = doc.to(); root["status"] = status; @@ -86,17 +87,25 @@ int MQTTMessageBuilder::buildPacketMessage( char* buffer, size_t buffer_size ) { - // Size-adaptive JSON document: estimate needed size based on raw hex string length + // Use StaticJsonDocument with fixed maximum size to avoid heap fragmentation // Base JSON overhead ~200 bytes, raw hex can be up to 510 chars (255 bytes packet) - // Use minimum needed to reduce memory fragmentation - size_t raw_len = raw ? strlen(raw) : 0; - size_t doc_size = 256 + raw_len + 128; // Base + raw + overhead, but cap at buffer_size - if (doc_size > buffer_size) doc_size = buffer_size; - if (doc_size < 512) doc_size = 512; // Minimum for small packets - if (doc_size > 2048) doc_size = 2048; // Maximum cap - DynamicJsonDocument doc(doc_size); + // Use maximum size (2048) to handle all packet sizes without heap allocation + StaticJsonDocument<2048> doc; JsonObject root = doc.to(); + // Format numeric values as strings to avoid String object allocations + char len_str[16]; + char packet_type_str[16]; + char payload_len_str[16]; + char snr_str[16]; + char rssi_str[16]; + + snprintf(len_str, sizeof(len_str), "%d", len); + snprintf(packet_type_str, sizeof(packet_type_str), "%d", packet_type); + snprintf(payload_len_str, sizeof(payload_len_str), "%d", payload_len); + snprintf(snr_str, sizeof(snr_str), "%.1f", snr); + snprintf(rssi_str, sizeof(rssi_str), "%d", rssi); + root["origin"] = origin; root["origin_id"] = origin_id; root["timestamp"] = timestamp; @@ -104,13 +113,13 @@ int MQTTMessageBuilder::buildPacketMessage( root["direction"] = direction; root["time"] = time; root["date"] = date; - root["len"] = String(len); - root["packet_type"] = String(packet_type); + root["len"] = len_str; + root["packet_type"] = packet_type_str; root["route"] = route; - root["payload_len"] = String(payload_len); + root["payload_len"] = payload_len_str; root["raw"] = raw; - root["SNR"] = String(snr, 1); - root["RSSI"] = String(rssi); + root["SNR"] = snr_str; + root["RSSI"] = rssi_str; root["hash"] = hash; if (path && strlen(path) > 0) { @@ -129,7 +138,8 @@ int MQTTMessageBuilder::buildRawMessage( char* buffer, size_t buffer_size ) { - DynamicJsonDocument doc(512); + // Use StaticJsonDocument to avoid heap fragmentation (fixed-size stack allocation) + StaticJsonDocument<512> doc; JsonObject root = doc.to(); root["origin"] = origin; diff --git a/src/helpers/bridges/MQTTBridge.cpp b/src/helpers/bridges/MQTTBridge.cpp index 6418cd819..a37cfcc8d 100644 --- a/src/helpers/bridges/MQTTBridge.cpp +++ b/src/helpers/bridges/MQTTBridge.cpp @@ -1482,12 +1482,20 @@ void MQTTBridge::maintainAnalyzerConnections() { // Check and renew US server token if needed if (_analyzer_us_enabled && _analyzer_us_client) { // Check if token is expired or will expire soon - // If time wasn't synced when token was created, expiration time will be invalid, so renew - bool token_needs_renewal = (_token_us_expires_at == 0) || - !time_synced || - !(_token_us_expires_at >= 1000000000) || // Expiration time invalid - (time_synced && current_time >= _token_us_expires_at) || - (time_synced && current_time >= (_token_us_expires_at - RENEWAL_BUFFER)); + // Only check expiration if time is synced - if time isn't synced, we can't validate expiration + // If time wasn't synced when token was created, expiration time will be invalid (< 1000000000), so renew when time syncs + bool token_needs_renewal = false; + if (!time_synced) { + // Time not synced yet - only renew if token is missing (expires_at == 0) + // Don't renew if token exists but expiration is invalid - wait for time sync + token_needs_renewal = (_token_us_expires_at == 0); + } else { + // Time is synced - check if token needs renewal + token_needs_renewal = (_token_us_expires_at == 0) || + !(_token_us_expires_at >= 1000000000) || // Expiration time invalid (created before time sync) + (current_time >= _token_us_expires_at) || + (current_time >= (_token_us_expires_at - RENEWAL_BUFFER)); + } // Throttle renewal attempts - don't try more than once per minute to avoid blocking bool can_attempt_renewal = (now_millis - _last_token_renewal_attempt_us) >= RENEWAL_THROTTLE_MS; @@ -1529,8 +1537,14 @@ void MQTTBridge::maintainAnalyzerConnections() { 0, 86400, _auth_token_us, sizeof(_auth_token_us), owner_key, client_version, email)) { unsigned long expires_in = 86400; // 24 hours - _token_us_expires_at = current_time + expires_in; - MQTT_DEBUG_PRINTLN("US token renewed, new expiration: %lu", _token_us_expires_at); + // Only set expiration time if time is synced - otherwise set to 0 to indicate it needs to be set later + if (time_synced) { + _token_us_expires_at = current_time + expires_in; + MQTT_DEBUG_PRINTLN("US token renewed, new expiration: %lu", _token_us_expires_at); + } else { + _token_us_expires_at = 0; // Will be set properly after time sync + MQTT_DEBUG_PRINTLN("US token renewed, expiration will be set after time sync"); + } // Update client credentials with new token _analyzer_us_client->setCredentials(_analyzer_username, _auth_token_us); @@ -1573,12 +1587,20 @@ void MQTTBridge::maintainAnalyzerConnections() { // Check and renew EU server token if needed if (_analyzer_eu_enabled && _analyzer_eu_client) { // Check if token is expired or will expire soon - // If time wasn't synced when token was created, expiration time will be invalid, so renew - bool token_needs_renewal = (_token_eu_expires_at == 0) || - !time_synced || - !(_token_eu_expires_at >= 1000000000) || // Expiration time invalid - (time_synced && current_time >= _token_eu_expires_at) || - (time_synced && current_time >= (_token_eu_expires_at - RENEWAL_BUFFER)); + // Only check expiration if time is synced - if time isn't synced, we can't validate expiration + // If time wasn't synced when token was created, expiration time will be invalid (< 1000000000), so renew when time syncs + bool token_needs_renewal = false; + if (!time_synced) { + // Time not synced yet - only renew if token is missing (expires_at == 0) + // Don't renew if token exists but expiration is invalid - wait for time sync + token_needs_renewal = (_token_eu_expires_at == 0); + } else { + // Time is synced - check if token needs renewal + token_needs_renewal = (_token_eu_expires_at == 0) || + !(_token_eu_expires_at >= 1000000000) || // Expiration time invalid (created before time sync) + (current_time >= _token_eu_expires_at) || + (current_time >= (_token_eu_expires_at - RENEWAL_BUFFER)); + } // Throttle renewal attempts - don't try more than once per minute to avoid blocking bool can_attempt_renewal = (now_millis - _last_token_renewal_attempt_eu) >= RENEWAL_THROTTLE_MS; @@ -1620,8 +1642,14 @@ void MQTTBridge::maintainAnalyzerConnections() { 0, 86400, _auth_token_eu, sizeof(_auth_token_eu), owner_key, client_version, email)) { unsigned long expires_in = 86400; // 24 hours - _token_eu_expires_at = current_time + expires_in; - MQTT_DEBUG_PRINTLN("EU token renewed, new expiration: %lu", _token_eu_expires_at); + // Only set expiration time if time is synced - otherwise set to 0 to indicate it needs to be set later + if (time_synced) { + _token_eu_expires_at = current_time + expires_in; + MQTT_DEBUG_PRINTLN("EU token renewed, new expiration: %lu", _token_eu_expires_at); + } else { + _token_eu_expires_at = 0; // Will be set properly after time sync + MQTT_DEBUG_PRINTLN("EU token renewed, expiration will be set after time sync"); + } // Update client credentials with new token _analyzer_eu_client->setCredentials(_analyzer_username, _auth_token_eu);