fix(mqtt): harden TLora MQTT observer and trim MQTT task stack use

- LilyGo T-LoRa V2.1 observer envs: set ESP32_CPU_FREQ=240 and
  MQTT_TASK_STACK_SIZE=16384 only for those targets so classic ESP32
  TLS + JWT connect fits the FreeRTOS task; other boards keep the
  default 8192 B unless overridden.
- MQTTBridge: use large JSON stack fallbacks only for PSRAM builds when
  heap-backed buffers may be null; non-PSRAM paths use the inline
  member buffers only, cutting peak stack in status/packet/raw publish.
- MQTTBridge.h: update the StaticJsonDocument comment (no longer refers
  to an 8 KiB MQTT task stack).
This commit is contained in:
agessaman committed 2026-05-13 09:13:17 -07:00
1 parent fc99d6b388
commit daf9ee4798
3 files changed
+24 -3

No files matched your search

+19 -2
View File
@@ -1665,8 +1665,12 @@ void MQTTBridge::publishStatusToSlot(int index) {
// Reuse pre-allocated buffer to avoid heap alloc/free churn under memory pressure.
// _status_json_buffer and _last_raw_data are both Core 0-owned; no mutex needed.
#if defined(BOARD_HAS_PSRAM)
char fallback_status_buffer[STATUS_JSON_BUFFER_SIZE];
char* json_buffer = (_status_json_buffer != nullptr) ? _status_json_buffer : fallback_status_buffer;
#else
char* json_buffer = _status_json_buffer;
#endif
char origin_id[65];
char timestamp[32];
@@ -2356,8 +2360,12 @@ bool MQTTBridge::publishStatus() {
// Reuse pre-allocated buffer to avoid heap alloc/free churn under memory pressure.
// _status_json_buffer and _last_raw_data are both Core 0-owned; no mutex needed.
#if defined(BOARD_HAS_PSRAM)
char fallback_status_buffer[STATUS_JSON_BUFFER_SIZE];
char* json_buffer = (_status_json_buffer != nullptr) ? _status_json_buffer : fallback_status_buffer;
#else
char* json_buffer = _status_json_buffer;
#endif
char origin_id[65];
char timestamp[32];
char radio_info[64];
@@ -2475,7 +2483,8 @@ bool MQTTBridge::publishPacket(mesh::Packet* packet, bool is_tx,
}
#endif
// Use pre-allocated buffer; fallback to single stack buffer if not available
// Use pre-allocated buffer; stack fallback only when PSRAM heap alloc may be null.
#if defined(BOARD_HAS_PSRAM)
char json_buffer_stack[PUBLISH_JSON_BUFFER_SIZE];
char* active_buffer;
size_t active_buffer_size;
@@ -2486,6 +2495,10 @@ bool MQTTBridge::publishPacket(mesh::Packet* packet, bool is_tx,
active_buffer = json_buffer_stack;
active_buffer_size = PUBLISH_JSON_BUFFER_SIZE;
}
#else
char* active_buffer = _publish_json_buffer;
const size_t active_buffer_size = PUBLISH_JSON_BUFFER_SIZE;
#endif
char origin_id[65];
strncpy(origin_id, _device_id, sizeof(origin_id) - 1);
@@ -2552,7 +2565,7 @@ bool MQTTBridge::publishRaw(mesh::Packet* packet) {
refreshOriginFromPrefs();
// Use pre-allocated buffer; fallback to single stack buffer if not available
#if defined(BOARD_HAS_PSRAM)
char json_buffer_stack[PUBLISH_JSON_BUFFER_SIZE];
char* active_buffer;
size_t active_buffer_size;
@@ -2563,6 +2576,10 @@ bool MQTTBridge::publishRaw(mesh::Packet* packet) {
active_buffer = json_buffer_stack;
active_buffer_size = PUBLISH_JSON_BUFFER_SIZE;
}
#else
char* active_buffer = _publish_json_buffer;
const size_t active_buffer_size = PUBLISH_JSON_BUFFER_SIZE;
#endif
char origin_id[65];
strncpy(origin_id, _device_id, sizeof(origin_id) - 1);
+1 -1
View File
@@ -223,7 +223,7 @@ private:
char _status_json_buffer[STATUS_JSON_BUFFER_SIZE];
#endif
// JSON document scratch space — inline StaticJsonDocument keeps the pool off the 8 KB MQTT
// JSON document scratch space — inline StaticJsonDocument keeps the pool off the MQTT
// task stack and eliminates two separate heap allocations (fragmentation reduction).
StaticJsonDocument<PUBLISH_JSON_BUFFER_SIZE> _packet_json_doc;
StaticJsonDocument<STATUS_JSON_BUFFER_SIZE> _status_json_doc;
@@ -155,6 +155,8 @@ build_flags =
-D WITH_MQTT_BRIDGE=1
-D MAX_MQTT_BROKERS=3
-D MQTT_MAX_PACKET_SIZE=1024
-D MQTT_TASK_STACK_SIZE=16384
-D ESP32_CPU_FREQ=240
; -D MQTT_DEBUG=1
; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1
@@ -197,6 +199,8 @@ build_flags =
-D WITH_MQTT_BRIDGE=1
-D MAX_MQTT_BROKERS=3
-D MQTT_MAX_PACKET_SIZE=1024
-D MQTT_TASK_STACK_SIZE=16384
-D ESP32_CPU_FREQ=240
-D MQTT_DEBUG=1
; -D MESH_PACKET_LOGGING=1
; -D MESH_DEBUG=1