Refactor MQTT bridge implementation to support up to 3 configurable connection slots with built-in presets for LetsMesh Analyzer (US/EU) and MeshMapper. Update CLI commands for slot management and enhance configuration migration from legacy settings. Adjust related documentation and code structure for improved clarity and functionality.
This commit is contained in:
1 parent
abe6e046fe
commit
b43e9618db
8 files changed
+2005
-3006
No files matched your search
+144
-71
@@ -22,11 +22,10 @@ static size_t getMQTTFieldsSize(const NodePrefs* prefs) {
|
||||
sizeof(prefs->mqtt_raw_enabled) + sizeof(prefs->mqtt_tx_enabled) +
|
||||
sizeof(prefs->mqtt_status_interval) + sizeof(prefs->wifi_ssid) +
|
||||
sizeof(prefs->wifi_password) + sizeof(prefs->timezone_string) +
|
||||
sizeof(prefs->timezone_offset) + sizeof(prefs->mqtt_server) +
|
||||
sizeof(prefs->mqtt_port) + sizeof(prefs->mqtt_username) +
|
||||
sizeof(prefs->mqtt_password) + sizeof(prefs->mqtt_analyzer_us_enabled) +
|
||||
sizeof(prefs->mqtt_analyzer_eu_enabled) + sizeof(prefs->mqtt_owner_public_key) +
|
||||
sizeof(prefs->mqtt_email);
|
||||
sizeof(prefs->timezone_offset) + sizeof(prefs->mqtt_slot_preset) +
|
||||
sizeof(prefs->mqtt_slot_host) + sizeof(prefs->mqtt_slot_port) +
|
||||
sizeof(prefs->mqtt_slot_username) + sizeof(prefs->mqtt_slot_password) +
|
||||
sizeof(prefs->mqtt_owner_public_key) + sizeof(prefs->mqtt_email);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -141,17 +140,16 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
size_t mqtt_fields_size = getMQTTFieldsSize(_prefs);
|
||||
#else
|
||||
// If MQTT bridge not enabled, still skip these fields for file format compatibility
|
||||
size_t mqtt_fields_size =
|
||||
size_t mqtt_fields_size =
|
||||
sizeof(_prefs->mqtt_origin) + sizeof(_prefs->mqtt_iata) +
|
||||
sizeof(_prefs->mqtt_status_enabled) + sizeof(_prefs->mqtt_packets_enabled) +
|
||||
sizeof(_prefs->mqtt_raw_enabled) + sizeof(_prefs->mqtt_tx_enabled) +
|
||||
sizeof(_prefs->mqtt_status_interval) + sizeof(_prefs->wifi_ssid) +
|
||||
sizeof(_prefs->wifi_password) + sizeof(_prefs->timezone_string) +
|
||||
sizeof(_prefs->timezone_offset) + sizeof(_prefs->mqtt_server) +
|
||||
sizeof(_prefs->mqtt_port) + sizeof(_prefs->mqtt_username) +
|
||||
sizeof(_prefs->mqtt_password) + sizeof(_prefs->mqtt_analyzer_us_enabled) +
|
||||
sizeof(_prefs->mqtt_analyzer_eu_enabled) + sizeof(_prefs->mqtt_owner_public_key) +
|
||||
sizeof(_prefs->mqtt_email);
|
||||
sizeof(_prefs->timezone_offset) + sizeof(_prefs->mqtt_slot_preset) +
|
||||
sizeof(_prefs->mqtt_slot_host) + sizeof(_prefs->mqtt_slot_port) +
|
||||
sizeof(_prefs->mqtt_slot_username) + sizeof(_prefs->mqtt_slot_password) +
|
||||
sizeof(_prefs->mqtt_owner_public_key) + sizeof(_prefs->mqtt_email);
|
||||
#endif
|
||||
uint8_t skip_buffer[512]; // Large enough buffer
|
||||
size_t remaining = mqtt_fields_size;
|
||||
@@ -255,17 +253,16 @@ void CommonCLI::savePrefs(FILESYSTEM* fs) {
|
||||
size_t mqtt_fields_size = getMQTTFieldsSize(_prefs);
|
||||
#else
|
||||
// If MQTT bridge not enabled, still write zeros for file format compatibility
|
||||
size_t mqtt_fields_size =
|
||||
size_t mqtt_fields_size =
|
||||
sizeof(_prefs->mqtt_origin) + sizeof(_prefs->mqtt_iata) +
|
||||
sizeof(_prefs->mqtt_status_enabled) + sizeof(_prefs->mqtt_packets_enabled) +
|
||||
sizeof(_prefs->mqtt_raw_enabled) + sizeof(_prefs->mqtt_tx_enabled) +
|
||||
sizeof(_prefs->mqtt_status_interval) + sizeof(_prefs->wifi_ssid) +
|
||||
sizeof(_prefs->wifi_password) + sizeof(_prefs->timezone_string) +
|
||||
sizeof(_prefs->timezone_offset) + sizeof(_prefs->mqtt_server) +
|
||||
sizeof(_prefs->mqtt_port) + sizeof(_prefs->mqtt_username) +
|
||||
sizeof(_prefs->mqtt_password) + sizeof(_prefs->mqtt_analyzer_us_enabled) +
|
||||
sizeof(_prefs->mqtt_analyzer_eu_enabled) + sizeof(_prefs->mqtt_owner_public_key) +
|
||||
sizeof(_prefs->mqtt_email);
|
||||
sizeof(_prefs->timezone_offset) + sizeof(_prefs->mqtt_slot_preset) +
|
||||
sizeof(_prefs->mqtt_slot_host) + sizeof(_prefs->mqtt_slot_port) +
|
||||
sizeof(_prefs->mqtt_slot_username) + sizeof(_prefs->mqtt_slot_password) +
|
||||
sizeof(_prefs->mqtt_owner_public_key) + sizeof(_prefs->mqtt_email);
|
||||
#endif
|
||||
memset(pad, 0, sizeof(pad));
|
||||
size_t remaining = mqtt_fields_size;
|
||||
@@ -294,8 +291,13 @@ static void setMQTTPrefsDefaults(MQTTPrefs* prefs) {
|
||||
prefs->mqtt_raw_enabled = 0; // disabled by default
|
||||
prefs->mqtt_tx_enabled = 0; // disabled by default (RX only)
|
||||
prefs->mqtt_status_interval = 300000; // 5 minutes default
|
||||
prefs->mqtt_analyzer_us_enabled = 1; // enabled by default
|
||||
prefs->mqtt_analyzer_eu_enabled = 1; // enabled by default
|
||||
// Slot presets: analyzer-us and analyzer-eu enabled by default, slot 3 = none
|
||||
strncpy(prefs->mqtt_slot_preset[0], "analyzer-us", sizeof(prefs->mqtt_slot_preset[0]) - 1);
|
||||
prefs->mqtt_slot_preset[0][sizeof(prefs->mqtt_slot_preset[0]) - 1] = '\0';
|
||||
strncpy(prefs->mqtt_slot_preset[1], "analyzer-eu", sizeof(prefs->mqtt_slot_preset[1]) - 1);
|
||||
prefs->mqtt_slot_preset[1][sizeof(prefs->mqtt_slot_preset[1]) - 1] = '\0';
|
||||
strncpy(prefs->mqtt_slot_preset[2], "none", sizeof(prefs->mqtt_slot_preset[2]) - 1);
|
||||
prefs->mqtt_slot_preset[2][sizeof(prefs->mqtt_slot_preset[2]) - 1] = '\0';
|
||||
#ifdef MQTT_WIFI_POWER_SAVE_DEFAULT
|
||||
prefs->wifi_power_save = MQTT_WIFI_POWER_SAVE_DEFAULT; // 0=min, 1=none, 2=max
|
||||
#else
|
||||
@@ -329,6 +331,57 @@ void CommonCLI::loadMQTTPrefs(FILESYSTEM* fs) {
|
||||
setMQTTPrefsDefaults(&_mqtt_prefs);
|
||||
}
|
||||
file.close();
|
||||
|
||||
// Migration: check if legacy fields have data but new slot fields are empty
|
||||
bool slots_empty = (_mqtt_prefs.mqtt_slot_preset[0][0] == '\0' &&
|
||||
_mqtt_prefs.mqtt_slot_preset[1][0] == '\0' &&
|
||||
_mqtt_prefs.mqtt_slot_preset[2][0] == '\0');
|
||||
bool has_legacy = (_mqtt_prefs._legacy_analyzer_us_enabled != 0 ||
|
||||
_mqtt_prefs._legacy_analyzer_eu_enabled != 0 ||
|
||||
_mqtt_prefs._legacy_mqtt_server[0] != '\0');
|
||||
if (slots_empty && has_legacy) {
|
||||
MESH_DEBUG_PRINTLN("MQTT: Migrating legacy prefs to slot-based presets");
|
||||
// Migrate analyzer US
|
||||
if (_mqtt_prefs._legacy_analyzer_us_enabled == 1) {
|
||||
strncpy(_mqtt_prefs.mqtt_slot_preset[0], "analyzer-us", sizeof(_mqtt_prefs.mqtt_slot_preset[0]) - 1);
|
||||
_mqtt_prefs.mqtt_slot_preset[0][sizeof(_mqtt_prefs.mqtt_slot_preset[0]) - 1] = '\0';
|
||||
} else {
|
||||
strncpy(_mqtt_prefs.mqtt_slot_preset[0], "none", sizeof(_mqtt_prefs.mqtt_slot_preset[0]) - 1);
|
||||
_mqtt_prefs.mqtt_slot_preset[0][sizeof(_mqtt_prefs.mqtt_slot_preset[0]) - 1] = '\0';
|
||||
}
|
||||
// Migrate analyzer EU
|
||||
if (_mqtt_prefs._legacy_analyzer_eu_enabled == 1) {
|
||||
strncpy(_mqtt_prefs.mqtt_slot_preset[1], "analyzer-eu", sizeof(_mqtt_prefs.mqtt_slot_preset[1]) - 1);
|
||||
_mqtt_prefs.mqtt_slot_preset[1][sizeof(_mqtt_prefs.mqtt_slot_preset[1]) - 1] = '\0';
|
||||
} else {
|
||||
strncpy(_mqtt_prefs.mqtt_slot_preset[1], "none", sizeof(_mqtt_prefs.mqtt_slot_preset[1]) - 1);
|
||||
_mqtt_prefs.mqtt_slot_preset[1][sizeof(_mqtt_prefs.mqtt_slot_preset[1]) - 1] = '\0';
|
||||
}
|
||||
// Migrate custom server to slot 3
|
||||
if (_mqtt_prefs._legacy_mqtt_server[0] != '\0' && _mqtt_prefs._legacy_mqtt_port > 0) {
|
||||
strncpy(_mqtt_prefs.mqtt_slot_preset[2], "custom", sizeof(_mqtt_prefs.mqtt_slot_preset[2]) - 1);
|
||||
_mqtt_prefs.mqtt_slot_preset[2][sizeof(_mqtt_prefs.mqtt_slot_preset[2]) - 1] = '\0';
|
||||
strncpy(_mqtt_prefs.mqtt_slot_host[2], _mqtt_prefs._legacy_mqtt_server, sizeof(_mqtt_prefs.mqtt_slot_host[2]) - 1);
|
||||
_mqtt_prefs.mqtt_slot_host[2][sizeof(_mqtt_prefs.mqtt_slot_host[2]) - 1] = '\0';
|
||||
_mqtt_prefs.mqtt_slot_port[2] = _mqtt_prefs._legacy_mqtt_port;
|
||||
strncpy(_mqtt_prefs.mqtt_slot_username[2], _mqtt_prefs._legacy_mqtt_username, sizeof(_mqtt_prefs.mqtt_slot_username[2]) - 1);
|
||||
_mqtt_prefs.mqtt_slot_username[2][sizeof(_mqtt_prefs.mqtt_slot_username[2]) - 1] = '\0';
|
||||
strncpy(_mqtt_prefs.mqtt_slot_password[2], _mqtt_prefs._legacy_mqtt_password, sizeof(_mqtt_prefs.mqtt_slot_password[2]) - 1);
|
||||
_mqtt_prefs.mqtt_slot_password[2][sizeof(_mqtt_prefs.mqtt_slot_password[2]) - 1] = '\0';
|
||||
} else {
|
||||
strncpy(_mqtt_prefs.mqtt_slot_preset[2], "none", sizeof(_mqtt_prefs.mqtt_slot_preset[2]) - 1);
|
||||
_mqtt_prefs.mqtt_slot_preset[2][sizeof(_mqtt_prefs.mqtt_slot_preset[2]) - 1] = '\0';
|
||||
}
|
||||
// Clear legacy fields
|
||||
_mqtt_prefs._legacy_analyzer_us_enabled = 0;
|
||||
_mqtt_prefs._legacy_analyzer_eu_enabled = 0;
|
||||
memset(_mqtt_prefs._legacy_mqtt_server, 0, sizeof(_mqtt_prefs._legacy_mqtt_server));
|
||||
_mqtt_prefs._legacy_mqtt_port = 0;
|
||||
memset(_mqtt_prefs._legacy_mqtt_username, 0, sizeof(_mqtt_prefs._legacy_mqtt_username));
|
||||
memset(_mqtt_prefs._legacy_mqtt_password, 0, sizeof(_mqtt_prefs._legacy_mqtt_password));
|
||||
// Save migrated prefs
|
||||
saveMQTTPrefs(fs);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Migration: Try to read from old /com_prefs file if it exists
|
||||
@@ -412,12 +465,14 @@ void CommonCLI::syncMQTTPrefsToNodePrefs() {
|
||||
_prefs->wifi_power_save = _mqtt_prefs.wifi_power_save;
|
||||
StrHelper::strncpy(_prefs->timezone_string, _mqtt_prefs.timezone_string, sizeof(_prefs->timezone_string));
|
||||
_prefs->timezone_offset = _mqtt_prefs.timezone_offset;
|
||||
StrHelper::strncpy(_prefs->mqtt_server, _mqtt_prefs.mqtt_server, sizeof(_prefs->mqtt_server));
|
||||
_prefs->mqtt_port = _mqtt_prefs.mqtt_port;
|
||||
StrHelper::strncpy(_prefs->mqtt_username, _mqtt_prefs.mqtt_username, sizeof(_prefs->mqtt_username));
|
||||
StrHelper::strncpy(_prefs->mqtt_password, _mqtt_prefs.mqtt_password, sizeof(_prefs->mqtt_password));
|
||||
_prefs->mqtt_analyzer_us_enabled = _mqtt_prefs.mqtt_analyzer_us_enabled;
|
||||
_prefs->mqtt_analyzer_eu_enabled = _mqtt_prefs.mqtt_analyzer_eu_enabled;
|
||||
// Slot-based fields
|
||||
for (int i = 0; i < 3; i++) {
|
||||
StrHelper::strncpy(_prefs->mqtt_slot_preset[i], _mqtt_prefs.mqtt_slot_preset[i], sizeof(_prefs->mqtt_slot_preset[i]));
|
||||
StrHelper::strncpy(_prefs->mqtt_slot_host[i], _mqtt_prefs.mqtt_slot_host[i], sizeof(_prefs->mqtt_slot_host[i]));
|
||||
_prefs->mqtt_slot_port[i] = _mqtt_prefs.mqtt_slot_port[i];
|
||||
StrHelper::strncpy(_prefs->mqtt_slot_username[i], _mqtt_prefs.mqtt_slot_username[i], sizeof(_prefs->mqtt_slot_username[i]));
|
||||
StrHelper::strncpy(_prefs->mqtt_slot_password[i], _mqtt_prefs.mqtt_slot_password[i], sizeof(_prefs->mqtt_slot_password[i]));
|
||||
}
|
||||
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));
|
||||
}
|
||||
@@ -437,12 +492,14 @@ void CommonCLI::syncNodePrefsToMQTTPrefs() {
|
||||
_mqtt_prefs.wifi_power_save = _prefs->wifi_power_save;
|
||||
StrHelper::strncpy(_mqtt_prefs.timezone_string, _prefs->timezone_string, sizeof(_mqtt_prefs.timezone_string));
|
||||
_mqtt_prefs.timezone_offset = _prefs->timezone_offset;
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_server, _prefs->mqtt_server, sizeof(_mqtt_prefs.mqtt_server));
|
||||
_mqtt_prefs.mqtt_port = _prefs->mqtt_port;
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_username, _prefs->mqtt_username, sizeof(_mqtt_prefs.mqtt_username));
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_password, _prefs->mqtt_password, sizeof(_mqtt_prefs.mqtt_password));
|
||||
_mqtt_prefs.mqtt_analyzer_us_enabled = _prefs->mqtt_analyzer_us_enabled;
|
||||
_mqtt_prefs.mqtt_analyzer_eu_enabled = _prefs->mqtt_analyzer_eu_enabled;
|
||||
// Slot-based fields
|
||||
for (int i = 0; i < 3; i++) {
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_slot_preset[i], _prefs->mqtt_slot_preset[i], sizeof(_mqtt_prefs.mqtt_slot_preset[i]));
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_slot_host[i], _prefs->mqtt_slot_host[i], sizeof(_mqtt_prefs.mqtt_slot_host[i]));
|
||||
_mqtt_prefs.mqtt_slot_port[i] = _prefs->mqtt_slot_port[i];
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_slot_username[i], _prefs->mqtt_slot_username[i], sizeof(_mqtt_prefs.mqtt_slot_username[i]));
|
||||
StrHelper::strncpy(_mqtt_prefs.mqtt_slot_password[i], _prefs->mqtt_slot_password[i], sizeof(_mqtt_prefs.mqtt_slot_password[i]));
|
||||
}
|
||||
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));
|
||||
}
|
||||
@@ -687,14 +744,24 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
// Display interval in minutes (rounded)
|
||||
uint32_t minutes = (_prefs->mqtt_status_interval + 29999) / 60000; // Round up
|
||||
sprintf(reply, "> %u minutes (%lu ms)", minutes, _prefs->mqtt_status_interval);
|
||||
} else if (memcmp(config, "mqtt.server", 11) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_server);
|
||||
} else if (memcmp(config, "mqtt.port", 9) == 0) {
|
||||
sprintf(reply, "> %d", _prefs->mqtt_port);
|
||||
} else if (memcmp(config, "mqtt.username", 13) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_username);
|
||||
} else if (memcmp(config, "mqtt.password", 13) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_password);
|
||||
} else if (config[0] == 'm' && config[1] == 'q' && config[2] == 't' && config[3] == 't' &&
|
||||
config[4] >= '1' && config[4] <= '3' && config[5] == '.') {
|
||||
// Slot-based commands: get mqtt1.preset, get mqtt1.server, etc.
|
||||
int slot = config[4] - '1'; // 0-2
|
||||
const char* subcmd = &config[6];
|
||||
if (memcmp(subcmd, "preset", 6) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_slot_preset[slot]);
|
||||
} else if (memcmp(subcmd, "server", 6) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_slot_host[slot]);
|
||||
} else if (memcmp(subcmd, "port", 4) == 0) {
|
||||
sprintf(reply, "> %d", _prefs->mqtt_slot_port[slot]);
|
||||
} else if (memcmp(subcmd, "username", 8) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_slot_username[slot]);
|
||||
} else if (memcmp(subcmd, "password", 8) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_slot_password[slot]);
|
||||
} else {
|
||||
sprintf(reply, "??: %s", config);
|
||||
}
|
||||
} else if (memcmp(config, "wifi.ssid", 9) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->wifi_ssid);
|
||||
} else if (memcmp(config, "wifi.pwd", 8) == 0) {
|
||||
@@ -745,10 +812,6 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
sprintf(reply, "> %s", _prefs->timezone_string);
|
||||
} else if (memcmp(config, "timezone.offset", 15) == 0) {
|
||||
sprintf(reply, "> %d", _prefs->timezone_offset);
|
||||
} else if (memcmp(config, "mqtt.analyzer.us", 17) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_analyzer_us_enabled ? "on" : "off");
|
||||
} else if (memcmp(config, "mqtt.analyzer.eu", 17) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_analyzer_eu_enabled ? "on" : "off");
|
||||
} else if (sender_timestamp == 0 && memcmp(config, "mqtt.owner", 10) == 0) { // from serial command line only
|
||||
if (_prefs->mqtt_owner_public_key[0] != '\0') {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_owner_public_key);
|
||||
@@ -761,9 +824,6 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
} else {
|
||||
strcpy(reply, "> (not set)");
|
||||
}
|
||||
} else if (memcmp(config, "mqtt.config.valid", 17) == 0) {
|
||||
bool valid = MQTTBridge::isConfigValid(_prefs);
|
||||
sprintf(reply, "> %s", valid ? "valid" : "invalid");
|
||||
#endif
|
||||
} else if (memcmp(config, "bootloader.ver", 14) == 0) {
|
||||
#ifdef NRF52_PLATFORM
|
||||
@@ -1157,35 +1217,48 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
} else {
|
||||
strcpy(reply, "Error: timezone offset must be between -12 and +14");
|
||||
}
|
||||
} else if (memcmp(config, "mqtt.server ", 12) == 0) {
|
||||
StrHelper::strncpy(_prefs->mqtt_server, &config[12], sizeof(_prefs->mqtt_server));
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "mqtt.port ", 10) == 0) {
|
||||
int port = atoi(&config[10]);
|
||||
if (port > 0 && port <= 65535) {
|
||||
_prefs->mqtt_port = port;
|
||||
} else if (config[0] == 'm' && config[1] == 'q' && config[2] == 't' && config[3] == 't' &&
|
||||
config[4] >= '1' && config[4] <= '3' && config[5] == '.') {
|
||||
// Slot-based commands: set mqtt1.preset <name>, set mqtt1.server <host>, etc.
|
||||
int slot = config[4] - '1'; // 0-2
|
||||
const char* subcmd = &config[6];
|
||||
if (memcmp(subcmd, "preset ", 7) == 0) {
|
||||
const char* preset_name = &subcmd[7];
|
||||
// Validate preset name
|
||||
if (findMQTTPreset(preset_name) != nullptr ||
|
||||
strcmp(preset_name, MQTT_PRESET_CUSTOM) == 0 ||
|
||||
strcmp(preset_name, MQTT_PRESET_NONE) == 0) {
|
||||
StrHelper::strncpy(_prefs->mqtt_slot_preset[slot], preset_name, sizeof(_prefs->mqtt_slot_preset[slot]));
|
||||
savePrefs();
|
||||
_callbacks->restartBridge();
|
||||
sprintf(reply, "OK - slot %d preset: %s", slot + 1, preset_name);
|
||||
} else {
|
||||
strcpy(reply, "Error: valid presets are: analyzer-us, analyzer-eu, meshmapper, custom, none");
|
||||
}
|
||||
} else if (memcmp(subcmd, "server ", 7) == 0) {
|
||||
StrHelper::strncpy(_prefs->mqtt_slot_host[slot], &subcmd[7], sizeof(_prefs->mqtt_slot_host[slot]));
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(subcmd, "port ", 5) == 0) {
|
||||
int port = atoi(&subcmd[5]);
|
||||
if (port > 0 && port <= 65535) {
|
||||
_prefs->mqtt_slot_port[slot] = port;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error: port must be between 1 and 65535");
|
||||
}
|
||||
} else if (memcmp(subcmd, "username ", 9) == 0) {
|
||||
StrHelper::strncpy(_prefs->mqtt_slot_username[slot], &subcmd[9], sizeof(_prefs->mqtt_slot_username[slot]));
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(subcmd, "password ", 9) == 0) {
|
||||
StrHelper::strncpy(_prefs->mqtt_slot_password[slot], &subcmd[9], sizeof(_prefs->mqtt_slot_password[slot]));
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error: port must be between 1 and 65535");
|
||||
sprintf(reply, "unknown config: %s", config);
|
||||
}
|
||||
} else if (memcmp(config, "mqtt.username ", 14) == 0) {
|
||||
StrHelper::strncpy(_prefs->mqtt_username, &config[14], sizeof(_prefs->mqtt_username));
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "mqtt.password ", 14) == 0) {
|
||||
StrHelper::strncpy(_prefs->mqtt_password, &config[14], sizeof(_prefs->mqtt_password));
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "mqtt.analyzer.us ", 17) == 0) {
|
||||
_prefs->mqtt_analyzer_us_enabled = memcmp(&config[17], "on", 2) == 0;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "mqtt.analyzer.eu ", 17) == 0) {
|
||||
_prefs->mqtt_analyzer_eu_enabled = memcmp(&config[17], "on", 2) == 0;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "mqtt.owner ", 11) == 0) {
|
||||
// Validate that it's a valid hex string of the correct length (64 hex chars = 32 bytes)
|
||||
const char* owner_key = &config[11];
|
||||
|
||||
+36
-24
@@ -77,18 +77,19 @@ struct NodePrefs { // persisted to file
|
||||
char timezone_string[32]; // Timezone string (e.g., "America/Los_Angeles")
|
||||
int8_t timezone_offset; // Timezone offset in hours (-12 to +14) - fallback
|
||||
|
||||
// MQTT server settings
|
||||
char mqtt_server[64]; // MQTT server hostname
|
||||
uint16_t mqtt_port; // MQTT server port
|
||||
char mqtt_username[32]; // MQTT username
|
||||
char mqtt_password[64]; // MQTT password
|
||||
|
||||
// Let's Mesh Analyzer settings
|
||||
uint8_t mqtt_analyzer_us_enabled; // Enable US analyzer server
|
||||
uint8_t mqtt_analyzer_eu_enabled; // Enable EU analyzer server
|
||||
// MQTT slot presets (3 slots, each can be a preset name or "custom"/"none")
|
||||
char mqtt_slot_preset[3][24]; // e.g. "analyzer-us", "meshmapper", "custom", "none"
|
||||
|
||||
// Per-slot custom broker settings (only used when slot preset is "custom")
|
||||
char mqtt_slot_host[3][64];
|
||||
uint16_t mqtt_slot_port[3];
|
||||
char mqtt_slot_username[3][32];
|
||||
char mqtt_slot_password[3][64];
|
||||
|
||||
// Shared MQTT authentication
|
||||
char mqtt_owner_public_key[65]; // Owner public key (hex string, same length as repeater public key)
|
||||
char mqtt_email[64]; // Owner email address for matching nodes with owners
|
||||
|
||||
|
||||
uint8_t loop_detect;
|
||||
};
|
||||
|
||||
@@ -103,27 +104,38 @@ struct MQTTPrefs {
|
||||
uint8_t mqtt_raw_enabled; // Enable raw messages
|
||||
uint8_t mqtt_tx_enabled; // Enable TX packet uplinking
|
||||
uint32_t mqtt_status_interval; // Status publish interval (ms)
|
||||
|
||||
|
||||
// WiFi settings
|
||||
char wifi_ssid[32]; // WiFi SSID
|
||||
char wifi_password[64]; // WiFi password
|
||||
uint8_t wifi_power_save; // WiFi power save mode: 0=min, 1=none, 2=max (default: 0=min)
|
||||
|
||||
|
||||
// Timezone settings
|
||||
char timezone_string[32]; // Timezone string (e.g., "America/Los_Angeles")
|
||||
int8_t timezone_offset; // Timezone offset in hours (-12 to +14) - fallback
|
||||
|
||||
// MQTT server settings
|
||||
char mqtt_server[64]; // MQTT server hostname
|
||||
uint16_t mqtt_port; // MQTT server port
|
||||
char mqtt_username[32]; // MQTT username
|
||||
char mqtt_password[64]; // MQTT password
|
||||
|
||||
// Let's Mesh Analyzer settings
|
||||
uint8_t mqtt_analyzer_us_enabled; // Enable US analyzer server
|
||||
uint8_t mqtt_analyzer_eu_enabled; // Enable EU analyzer server
|
||||
char mqtt_owner_public_key[65]; // Owner public key (hex string, same length as repeater public key)
|
||||
char mqtt_email[64]; // Owner email address for matching nodes with owners
|
||||
|
||||
// Slot presets (3 slots)
|
||||
char mqtt_slot_preset[3][24]; // e.g. "analyzer-us", "meshmapper", "custom", "none"
|
||||
|
||||
// Per-slot custom broker settings (only used when preset is "custom")
|
||||
char mqtt_slot_host[3][64];
|
||||
uint16_t mqtt_slot_port[3];
|
||||
char mqtt_slot_username[3][32];
|
||||
char mqtt_slot_password[3][64];
|
||||
|
||||
// Shared authentication
|
||||
char mqtt_owner_public_key[65]; // Owner public key (hex string)
|
||||
char mqtt_email[64]; // Owner email address
|
||||
|
||||
// --- Legacy fields for migration detection ---
|
||||
// These are read during loadMQTTPrefs to detect old-format prefs and auto-migrate.
|
||||
// After migration they are zeroed out and not used again.
|
||||
uint8_t _legacy_analyzer_us_enabled;
|
||||
uint8_t _legacy_analyzer_eu_enabled;
|
||||
char _legacy_mqtt_server[64];
|
||||
uint16_t _legacy_mqtt_port;
|
||||
char _legacy_mqtt_username[32];
|
||||
char _legacy_mqtt_password[64];
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
|
||||
enum MQTTAuthType : uint8_t {
|
||||
MQTT_AUTH_NONE, // No authentication
|
||||
MQTT_AUTH_USERPASS, // Username/password
|
||||
MQTT_AUTH_JWT // Ed25519-signed JWT (device identity)
|
||||
};
|
||||
|
||||
struct MQTTPresetDef {
|
||||
const char* name; // Preset identifier: "analyzer-us", "analyzer-eu", "meshmapper"
|
||||
const char* server_url; // Full URL including scheme: "wss://host:port/path" or "mqtt://host:port"
|
||||
const char* jwt_audience; // JWT audience field (only for MQTT_AUTH_JWT)
|
||||
const char* ca_cert; // PEM CA certificate (nullptr to skip cert pinning)
|
||||
MQTTAuthType auth_type;
|
||||
};
|
||||
|
||||
// Google Trust Services - GTS Root R4 (used by LetsMesh Analyzer)
|
||||
static const char GTS_ROOT_R4[] PROGMEM =
|
||||
"-----BEGIN CERTIFICATE-----\n"
|
||||
"MIIDejCCAmKgAwIBAgIQf+UwvzMTQ77dghYQST2KGzANBgkqhkiG9w0BAQsFADBX\n"
|
||||
"MQswCQYDVQQGEwJCRTEZMBcGA1UEChMQR2xvYmFsU2lnbiBudi1zYTEQMA4GA1UE\n"
|
||||
"CxMHUm9vdCBDQTEbMBkGA1UEAxMSR2xvYmFsU2lnbiBSb290IENBMB4XDTIzMTEx\n"
|
||||
"NTAzNDMyMVoXDTI4MDEyODAwMDA0MlowRzELMAkGA1UEBhMCVVMxIjAgBgNVBAoT\n"
|
||||
"GUdvb2dsZSBUcnVzdCBTZXJ2aWNlcyBMTEMxFDASBgNVBAMTC0dUUyBSb290IFI0\n"
|
||||
"MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE83Rzp2iLYK5DuDXFgTB7S0md+8Fhzube\n"
|
||||
"Rr1r1WEYNa5A3XP3iZEwWus87oV8okB2O6nGuEfYKueSkWpz6bFyOZ8pn6KY019e\n"
|
||||
"WIZlD6GEZQbR3IvJx3PIjGov5cSr0R2Ko4H/MIH8MA4GA1UdDwEB/wQEAwIBhjAd\n"
|
||||
"BgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwDwYDVR0TAQH/BAUwAwEB/zAd\n"
|
||||
"BgNVHQ4EFgQUgEzW63T/STaj1dj8tT7FavCUHYwwHwYDVR0jBBgwFoAUYHtmGkUN\n"
|
||||
"l8qJUC99BM00qP/8/UswNgYIKwYBBQUHAQEEKjAoMCYGCCsGAQUFBzAChhpodHRw\n"
|
||||
"Oi8vaS5wa2kuZ29vZy9nc3IxLmNydDAtBgNVHR8EJjAkMCKgIKAehhxodHRwOi8v\n"
|
||||
"Yy5wa2kuZ29vZy9yL2dzcjEuY3JsMBMGA1UdIAQMMAowCAYGZ4EMAQIBMA0GCSqG\n"
|
||||
"SIb3DQEBCwUAA4IBAQAYQrsPBtYDh5bjP2OBDwmkoWhIDDkic574y04tfzHpn+cJ\n"
|
||||
"odI2D4SseesQ6bDrarZ7C30ddLibZatoKiws3UL9xnELz4ct92vID24FfVbiI1hY\n"
|
||||
"+SW6FoVHkNeWIP0GCbaM4C6uVdF5dTUsMVs/ZbzNnIdCp5Gxmx5ejvEau8otR/Cs\n"
|
||||
"kGN+hr/W5GvT1tMBjgWKZ1i4//emhA1JG1BbPzoLJQvyEotc03lXjTaCzv8mEbep\n"
|
||||
"8RqZ7a2CPsgRbuvTPBwcOMBBmuFeU88+FSBX6+7iP0il8b4Z0QFqIwwMHfs/L6K1\n"
|
||||
"vepuoxtGzi4CZ68zJpiq1UvSqTbFJjtbD4seiMHl\n"
|
||||
"-----END CERTIFICATE-----\n";
|
||||
|
||||
// ISRG Root X1 (used by MeshMapper - Let's Encrypt root CA)
|
||||
static const char ISRG_ROOT_X1[] PROGMEM =
|
||||
"-----BEGIN CERTIFICATE-----\n"
|
||||
"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\n"
|
||||
"-----END CERTIFICATE-----\n";
|
||||
|
||||
// Number of built-in presets
|
||||
static const int MQTT_PRESET_COUNT = 3;
|
||||
|
||||
// Built-in preset definitions (stored in flash)
|
||||
static const MQTTPresetDef MQTT_PRESETS[MQTT_PRESET_COUNT] = {
|
||||
{ "analyzer-us", "wss://mqtt-us-v1.letsmesh.net:443/mqtt", "mqtt-us-v1.letsmesh.net", GTS_ROOT_R4, MQTT_AUTH_JWT },
|
||||
{ "analyzer-eu", "wss://mqtt-eu-v1.letsmesh.net:443/mqtt", "mqtt-eu-v1.letsmesh.net", GTS_ROOT_R4, MQTT_AUTH_JWT },
|
||||
{ "meshmapper", "wss://mqtt.meshmapper.cc:443/mqtt", "mqtt.meshmapper.cc", ISRG_ROOT_X1, MQTT_AUTH_JWT },
|
||||
};
|
||||
|
||||
// Find a preset by name, returns nullptr if not found
|
||||
static const MQTTPresetDef* findMQTTPreset(const char* name) {
|
||||
if (!name || name[0] == '\0') return nullptr;
|
||||
for (int i = 0; i < MQTT_PRESET_COUNT; i++) {
|
||||
if (strcmp(name, MQTT_PRESETS[i].name) == 0) {
|
||||
return &MQTT_PRESETS[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Maximum number of concurrent MQTT connection slots
|
||||
static const int MAX_MQTT_SLOTS = 3;
|
||||
|
||||
// Slot preset name constants
|
||||
static const char MQTT_PRESET_NONE[] = "none";
|
||||
static const char MQTT_PRESET_CUSTOM[] = "custom";
|
||||
|
||||
#endif
|
||||
+1442
-2336
File diff suppressed because it is too large.
Load diff
+103
-291
@@ -8,6 +8,7 @@
|
||||
#include <WiFiUdp.h>
|
||||
#include <Timezone.h>
|
||||
#include "helpers/JWTHelper.h"
|
||||
#include "helpers/MQTTPresets.h"
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include <freertos/FreeRTOS.h>
|
||||
@@ -37,45 +38,52 @@
|
||||
* uplink packet data to multiple MQTT brokers for monitoring and analysis.
|
||||
*
|
||||
* Features:
|
||||
* - Multiple MQTT broker support
|
||||
* - Up to 3 configurable MQTT connection slots
|
||||
* - Built-in presets for LetsMesh Analyzer (US/EU) and MeshMapper
|
||||
* - Custom broker support with username/password auth
|
||||
* - JWT authentication with Ed25519 device signing
|
||||
* - Automatic reconnection with exponential backoff
|
||||
* - JSON message formatting for status, packets, and raw data
|
||||
* - Configurable topics and QoS levels
|
||||
* - Packet queuing during connection issues
|
||||
*
|
||||
* Message Types:
|
||||
* - Status: Device connection status and metadata
|
||||
* - Packets: Full packet data with RF characteristics
|
||||
* - Raw: Minimal raw packet data for map integration
|
||||
*
|
||||
* Configuration:
|
||||
* - Define WITH_MQTT_BRIDGE to enable this bridge
|
||||
* - Configure brokers via CLI commands
|
||||
* - Set origin name and IATA code for topic structure
|
||||
* - Configure slots via: set mqtt1.preset <name>, set mqtt2.preset <name>, etc.
|
||||
* - Available presets: analyzer-us, analyzer-eu, meshmapper, custom, none
|
||||
*/
|
||||
class MQTTBridge : public BridgeBase {
|
||||
private:
|
||||
PsychicMqttClient* _mqtt_client;
|
||||
|
||||
// MQTT broker configuration
|
||||
struct MQTTBroker {
|
||||
// Connection slot - each slot holds one MQTT connection
|
||||
struct MQTTSlot {
|
||||
PsychicMqttClient* client;
|
||||
const MQTTPresetDef* preset; // Points to MQTT_PRESETS[] entry, nullptr for custom/none
|
||||
bool enabled; // true when preset is not "none"
|
||||
bool connected; // Updated in callbacks
|
||||
bool initial_connect_done; // True after first connect() call
|
||||
|
||||
// JWT auth state (only used when preset->auth_type == MQTT_AUTH_JWT)
|
||||
char* auth_token; // PSRAM-allocated, AUTH_TOKEN_SIZE bytes
|
||||
unsigned long token_expires_at;
|
||||
unsigned long last_token_renewal;
|
||||
|
||||
// Custom broker settings (only used when preset_name is "custom")
|
||||
char host[64];
|
||||
uint16_t port;
|
||||
char username[32];
|
||||
char password[64];
|
||||
char client_id[32];
|
||||
uint8_t qos;
|
||||
bool enabled;
|
||||
bool connected;
|
||||
bool initial_connect_done; // True after first connect() call - auto-reconnect handles the rest
|
||||
unsigned long last_attempt;
|
||||
unsigned long reconnect_interval;
|
||||
|
||||
// Reconnect backoff
|
||||
uint8_t reconnect_backoff; // 0..4 index into backoff table
|
||||
unsigned long last_reconnect_attempt;
|
||||
unsigned long last_log_time; // Throttle disconnect log messages
|
||||
};
|
||||
|
||||
static const int MAX_MQTT_BROKERS_COUNT = 3;
|
||||
MQTTBroker _brokers[MAX_MQTT_BROKERS_COUNT];
|
||||
int _active_brokers;
|
||||
|
||||
|
||||
static const size_t AUTH_TOKEN_SIZE = 768;
|
||||
MQTTSlot _slots[MAX_MQTT_SLOTS];
|
||||
|
||||
// JWT username shared across all JWT-auth slots (same device identity)
|
||||
char _jwt_username[70]; // Format: v1_{UPPERCASE_PUBLIC_KEY}
|
||||
|
||||
// Message configuration
|
||||
char _origin[32];
|
||||
char _iata[8];
|
||||
@@ -89,7 +97,7 @@ private:
|
||||
bool _tx_enabled;
|
||||
unsigned long _last_status_publish;
|
||||
unsigned long _status_interval;
|
||||
|
||||
|
||||
// Packet queue for offline scenarios
|
||||
struct QueuedPacket {
|
||||
mesh::Packet* packet;
|
||||
@@ -102,18 +110,18 @@ private:
|
||||
float rssi;
|
||||
bool has_raw_data;
|
||||
};
|
||||
|
||||
|
||||
static const int MAX_QUEUE_SIZE = 10;
|
||||
|
||||
|
||||
// FreeRTOS queue for thread-safe packet queuing
|
||||
#ifdef ESP_PLATFORM
|
||||
QueueHandle_t _packet_queue_handle;
|
||||
TaskHandle_t _mqtt_task_handle;
|
||||
SemaphoreHandle_t _raw_data_mutex; // Mutex for raw radio data
|
||||
// PSRAM-backed task stack (plan §3); TCB kept in internal RAM
|
||||
// PSRAM-backed task stack; TCB kept in internal RAM
|
||||
StackType_t* _mqtt_task_stack; // nullptr if using dynamic task creation
|
||||
StaticTask_t _mqtt_task_tcb;
|
||||
// PSRAM-backed packet queue storage (plan §5)
|
||||
// PSRAM-backed packet queue storage
|
||||
uint8_t* _packet_queue_storage; // nullptr if using dynamic queue
|
||||
StaticQueue_t _packet_queue_struct;
|
||||
#else
|
||||
@@ -123,350 +131,154 @@ private:
|
||||
int _queue_tail;
|
||||
#endif
|
||||
int _queue_count; // Protected by queue operations or mutex
|
||||
|
||||
|
||||
// NTP time sync
|
||||
WiFiUDP _ntp_udp;
|
||||
NTPClient _ntp_client;
|
||||
unsigned long _last_ntp_sync;
|
||||
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
|
||||
|
||||
// Timezone handling
|
||||
Timezone* _timezone;
|
||||
|
||||
// Raw radio data storage (plan §6: PSRAM when BOARD_HAS_PSRAM)
|
||||
|
||||
// Raw radio data storage (PSRAM when BOARD_HAS_PSRAM)
|
||||
static const size_t LAST_RAW_DATA_SIZE = 256;
|
||||
uint8_t* _last_raw_data;
|
||||
int _last_raw_len;
|
||||
float _last_snr;
|
||||
float _last_rssi;
|
||||
unsigned long _last_raw_timestamp;
|
||||
|
||||
// Let's Mesh Analyzer support
|
||||
bool _analyzer_us_enabled;
|
||||
bool _analyzer_eu_enabled;
|
||||
static const size_t AUTH_TOKEN_SIZE = 768;
|
||||
char* _auth_token_us; // JWT token for US server (PSRAM when BOARD_HAS_PSRAM)
|
||||
char* _auth_token_eu; // JWT token for EU server (PSRAM when BOARD_HAS_PSRAM)
|
||||
char _analyzer_username[70]; // Username in format v1_{UPPERCASE_PUBLIC_KEY}
|
||||
|
||||
// Token expiration tracking
|
||||
unsigned long _token_us_expires_at;
|
||||
unsigned long _token_eu_expires_at;
|
||||
|
||||
|
||||
// Memory pressure monitoring
|
||||
unsigned long _last_memory_check;
|
||||
int _skipped_publishes; // Count of skipped publishes due to memory pressure
|
||||
unsigned long _last_fragmentation_recovery; // Throttle: 5 min between recovery runs (task + loop)
|
||||
unsigned long _fragmentation_pressure_since; // 0 = not under pressure; else first time max_alloc < threshold
|
||||
unsigned long _last_fragmentation_recovery; // Throttle: 5 min between recovery runs
|
||||
unsigned long _fragmentation_pressure_since; // 0 = not under pressure
|
||||
unsigned long _last_critical_check_run; // Throttle: run unified check at most every 60s
|
||||
unsigned long _last_token_renewal_attempt_us;
|
||||
unsigned long _last_token_renewal_attempt_eu;
|
||||
unsigned long _last_reconnect_attempt_us;
|
||||
unsigned long _last_reconnect_attempt_eu;
|
||||
|
||||
|
||||
// Status publish retry tracking
|
||||
unsigned long _last_status_retry; // Track last retry attempt (separate from successful publish)
|
||||
static const unsigned long STATUS_RETRY_INTERVAL = 30000; // Retry every 30 seconds if failed
|
||||
|
||||
|
||||
// Device identity for JWT token creation
|
||||
mesh::LocalIdentity *_identity;
|
||||
|
||||
// PsychicMqttClient instances for different brokers
|
||||
PsychicMqttClient* _analyzer_us_client;
|
||||
PsychicMqttClient* _analyzer_eu_client;
|
||||
|
||||
// Configuration validation state
|
||||
bool _config_valid;
|
||||
|
||||
// Cached broker connection status (updated in callbacks to avoid redundant checks)
|
||||
bool _cached_has_brokers;
|
||||
bool _cached_has_analyzer_servers;
|
||||
|
||||
// Throttle logging for disconnected broker messages
|
||||
|
||||
// Cached connection status (updated in callbacks to avoid redundant checks)
|
||||
bool _cached_has_connected_slots;
|
||||
|
||||
// Throttle logging
|
||||
unsigned long _last_no_broker_log;
|
||||
static const unsigned long NO_BROKER_LOG_INTERVAL = 30000; // Log every 30 seconds max
|
||||
|
||||
// Throttle logging for analyzer client disconnected messages
|
||||
unsigned long _last_analyzer_us_log;
|
||||
unsigned long _last_analyzer_eu_log;
|
||||
static const unsigned long ANALYZER_LOG_INTERVAL = 30000; // Log every 30 seconds max
|
||||
static const unsigned long SLOT_LOG_INTERVAL = 30000; // Log every 30 seconds max
|
||||
unsigned long _last_config_warning; // Throttle configuration mismatch warnings
|
||||
static const unsigned long CONFIG_WARNING_INTERVAL = 300000; // Log every 5 minutes max
|
||||
|
||||
// WiFi connection state and exponential backoff (one place for mqttTaskLoop + loop())
|
||||
// WiFi connection state and exponential backoff
|
||||
unsigned long _last_wifi_check;
|
||||
wl_status_t _last_wifi_status;
|
||||
bool _wifi_status_initialized;
|
||||
unsigned long _wifi_disconnected_time; // 0 when connected
|
||||
unsigned long _last_wifi_reconnect_attempt;
|
||||
uint8_t _wifi_reconnect_backoff_attempt; // 0..5 → 15s, 30s, 60s, 120s, 300s; reset on connect
|
||||
// Main broker reconnect backoff (reset in onConnect)
|
||||
uint8_t _main_broker_reconnect_backoff_attempt; // 0..5 → 15s, 30s, 60s, 120s, 300s
|
||||
// Analyzer reconnect backoff (reset when that client is connected)
|
||||
uint8_t _analyzer_us_reconnect_backoff_attempt; // 0..4 → 60s, 120s, 240s, 300000
|
||||
uint8_t _analyzer_eu_reconnect_backoff_attempt;
|
||||
|
||||
|
||||
// Optional pointers for collecting stats internally (set by mesh if available)
|
||||
mesh::Dispatcher* _dispatcher; // For air times and errors
|
||||
mesh::Radio* _radio; // For noise floor
|
||||
mesh::MainBoard* _board; // For battery voltage
|
||||
mesh::MillisecondClock* _ms; // For uptime
|
||||
|
||||
// Internal methods
|
||||
void ensureMainMqttClient(); // Create main MQTT client if _config_valid and _mqtt_client is null (e.g. after reinit)
|
||||
|
||||
// Internal methods - slot management
|
||||
void setupSlot(int index); // Create/destroy client for a slot based on its preset
|
||||
void teardownSlot(int index); // Disconnect and free slot resources
|
||||
void maintainSlotConnections(); // Maintain all slot connections (token renewal, reconnect)
|
||||
void maintainSlotConnection(int index, unsigned long now_millis, unsigned long current_time, bool time_synced);
|
||||
bool createSlotAuthToken(int index); // Create/renew JWT token for a slot
|
||||
bool publishToSlot(int index, const char* topic, const char* payload, bool retained = false);
|
||||
bool publishToAllSlots(const char* topic, const char* payload, bool retained = false);
|
||||
void publishStatusToSlot(int index);
|
||||
void updateCachedConnectionStatus();
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
void runCriticalMemoryCheckAndRecovery(); // Unified heap check, pressure timer, optional recovery
|
||||
void runCriticalMemoryCheckAndRecovery();
|
||||
#endif
|
||||
void recreateMqttClientsForFragmentationRecovery(); // Disconnect, delete, recreate all MQTT clients to recover max_alloc
|
||||
void connectToBrokers();
|
||||
void recreateMqttClientsForFragmentationRecovery();
|
||||
void processPacketQueue();
|
||||
bool publishStatus(); // Returns true if status was successfully published
|
||||
// Single place for WiFi monitoring: disconnect analyzers on drop, force reconnect with exponential backoff.
|
||||
// Returns true if we transitioned to connected this call (e.g. for NTP sync in loop()).
|
||||
bool handleWiFiConnection(unsigned long now);
|
||||
|
||||
|
||||
// FreeRTOS task function (runs on Core 0)
|
||||
#ifdef ESP_PLATFORM
|
||||
static void mqttTask(void* parameter);
|
||||
void mqttTaskLoop(); // Main loop for MQTT task
|
||||
void initializeWiFiInTask(); // WiFi initialization moved to task
|
||||
#endif
|
||||
void publishPacket(mesh::Packet* packet, bool is_tx,
|
||||
const uint8_t* raw_data = nullptr, int raw_len = 0,
|
||||
void publishPacket(mesh::Packet* packet, bool is_tx,
|
||||
const uint8_t* raw_data = nullptr, int raw_len = 0,
|
||||
float snr = 0.0f, float rssi = 0.0f);
|
||||
void publishRaw(mesh::Packet* packet);
|
||||
void queuePacket(mesh::Packet* packet, bool is_tx);
|
||||
void dequeuePacket();
|
||||
bool isAnyBrokerConnected();
|
||||
void setBrokerDefaults();
|
||||
bool isAnySlotConnected();
|
||||
void syncTimeWithNTP();
|
||||
Timezone* createTimezoneFromString(const char* tz_string);
|
||||
bool isMQTTConfigValid();
|
||||
void checkConfigurationMismatch(); // Check for bridge.source/mqtt.tx mismatch
|
||||
bool isIATAValid() const; // Check if IATA code is configured
|
||||
|
||||
void checkConfigurationMismatch();
|
||||
bool isIATAValid() const;
|
||||
|
||||
void optimizeMqttClientConfig(PsychicMqttClient* client, bool needs_large_buffer = false);
|
||||
void getClientVersion(char* buffer, size_t buffer_size) const;
|
||||
void logMemoryStatus();
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructs an MQTTBridge instance
|
||||
*
|
||||
* @param prefs Node preferences for configuration settings
|
||||
* @param mgr PacketManager for allocating and queuing packets
|
||||
* @param rtc RTCClock for timestamping debug messages
|
||||
* @param identity Device identity for JWT token creation
|
||||
*/
|
||||
MQTTBridge(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTCClock *rtc, mesh::LocalIdentity *identity);
|
||||
|
||||
/**
|
||||
* Initializes the MQTT bridge
|
||||
*
|
||||
* - Sets up default broker configuration
|
||||
* - Initializes WiFi client
|
||||
* - Prepares MQTT clients for each broker
|
||||
*/
|
||||
void begin() override;
|
||||
|
||||
/**
|
||||
* Stops the MQTT bridge
|
||||
*
|
||||
* - Disconnects from all brokers
|
||||
* - Clears packet queue
|
||||
* - Releases resources
|
||||
*/
|
||||
void end() override;
|
||||
|
||||
/**
|
||||
* Checks if MQTT configuration is valid
|
||||
*
|
||||
* @return true if all required MQTT settings are properly configured
|
||||
*/
|
||||
bool isConfigValid() const;
|
||||
|
||||
/**
|
||||
* Static method to validate MQTT configuration from preferences
|
||||
*
|
||||
* @param prefs Node preferences containing MQTT settings
|
||||
* @return true if all required MQTT settings are properly configured
|
||||
*/
|
||||
static bool isConfigValid(const NodePrefs* prefs);
|
||||
|
||||
/**
|
||||
* Time (millis()) when WiFi was last seen connected. 0 when disconnected or unknown.
|
||||
* Used by get wifi.status to report uptime when WITH_MQTT_BRIDGE is defined.
|
||||
*/
|
||||
static unsigned long getWifiConnectedAtMillis();
|
||||
|
||||
/**
|
||||
* Format an informative MQTT status line for get mqtt.status (msgs, broker, analyzers, queue).
|
||||
* Writes into buf, at most bufsize bytes. Requires bridge to have been initialized (begin() called).
|
||||
*/
|
||||
static void formatMqttStatusReply(char* buf, size_t bufsize, const NodePrefs* prefs);
|
||||
|
||||
/**
|
||||
* Check if MQTT bridge is ready to operate (has WiFi credentials)
|
||||
*
|
||||
* @return true if WiFi credentials are configured and bridge can connect
|
||||
*/
|
||||
bool isReady() const;
|
||||
|
||||
/**
|
||||
* Main loop handler
|
||||
* - Maintains broker connections
|
||||
* - Processes packet queue
|
||||
* - Publishes status updates
|
||||
*/
|
||||
void loop() override;
|
||||
|
||||
/**
|
||||
* Called when a packet is received via mesh
|
||||
* Queues the packet for MQTT publishing if enabled
|
||||
*
|
||||
* @param packet The received mesh packet
|
||||
*/
|
||||
void onPacketReceived(mesh::Packet *packet) override;
|
||||
|
||||
/**
|
||||
* Called when a packet needs to be transmitted via MQTT
|
||||
* Publishes the packet to all connected brokers
|
||||
*
|
||||
* @param packet The mesh packet to transmit
|
||||
*/
|
||||
void sendPacket(mesh::Packet *packet) override;
|
||||
|
||||
/**
|
||||
* Configure MQTT broker settings
|
||||
* Configure a slot with a preset name. Call this when the user runs
|
||||
* "set mqttN.preset <name>". Handles teardown of old connection and
|
||||
* setup of new one.
|
||||
*
|
||||
* @param broker_index Broker index (0-2)
|
||||
* @param slot_index Slot index (0-2)
|
||||
* @param preset_name Preset name: "analyzer-us", "analyzer-eu", "meshmapper", "custom", "none"
|
||||
*/
|
||||
void setSlotPreset(int slot_index, const char* preset_name);
|
||||
|
||||
/**
|
||||
* Configure custom broker settings for a slot. Only applies when the
|
||||
* slot's preset is "custom".
|
||||
*
|
||||
* @param slot_index Slot index (0-2)
|
||||
* @param host Broker hostname
|
||||
* @param port Broker port
|
||||
* @param username MQTT username
|
||||
* @param password MQTT password
|
||||
* @param enabled Whether broker is enabled
|
||||
* @param username MQTT username (empty for anonymous)
|
||||
* @param password MQTT password (empty for anonymous)
|
||||
*/
|
||||
void setBroker(int broker_index, const char* host, uint16_t port,
|
||||
const char* username, const char* password, bool enabled);
|
||||
void setSlotCustomBroker(int slot_index, const char* host, uint16_t port,
|
||||
const char* username, const char* password);
|
||||
|
||||
/**
|
||||
* Set device origin name for MQTT topics
|
||||
*
|
||||
* @param origin Device name
|
||||
*/
|
||||
void setOrigin(const char* origin);
|
||||
|
||||
/**
|
||||
* Set IATA code for MQTT topics
|
||||
*
|
||||
* @param iata Airport code
|
||||
*/
|
||||
void setIATA(const char* iata);
|
||||
|
||||
/**
|
||||
* Set device public key for MQTT topics
|
||||
*
|
||||
* @param device_id Device public key (hex string)
|
||||
*/
|
||||
void setDeviceID(const char* device_id);
|
||||
|
||||
/**
|
||||
* Set firmware version for status messages
|
||||
*
|
||||
* @param firmware_version Firmware version string
|
||||
*/
|
||||
void setFirmwareVersion(const char* firmware_version);
|
||||
|
||||
/**
|
||||
* Set board model for status messages
|
||||
*
|
||||
* @param board_model Board model string
|
||||
*/
|
||||
void setBoardModel(const char* board_model);
|
||||
|
||||
/**
|
||||
* Set build date for client version
|
||||
*
|
||||
* @param build_date Build date string
|
||||
*/
|
||||
void setBuildDate(const char* build_date);
|
||||
|
||||
/**
|
||||
* Stores raw radio data for MQTT messages
|
||||
*
|
||||
* @param raw_data Raw radio transmission data
|
||||
* @param len Length of raw data
|
||||
* @param snr Signal-to-noise ratio
|
||||
* @param rssi Received signal strength indicator
|
||||
*/
|
||||
void storeRawRadioData(const uint8_t* raw_data, int len, float snr, float rssi);
|
||||
|
||||
// Let's Mesh Analyzer methods
|
||||
void setupAnalyzerServers();
|
||||
bool createAuthToken();
|
||||
bool publishToAnalyzerServers(const char* topic, const char* payload, bool retained = false); // Returns true if at least one publish succeeded
|
||||
|
||||
// PsychicMqttClient WebSocket methods
|
||||
void setupAnalyzerClients();
|
||||
void maintainAnalyzerConnections();
|
||||
bool publishToAnalyzerClient(PsychicMqttClient* client, const char* topic, const char* payload, bool retained = false); // Returns true if publish succeeded
|
||||
void publishStatusToAnalyzerClient(PsychicMqttClient* client, const char* server_name);
|
||||
|
||||
/**
|
||||
* Optimize MQTT client configuration for memory efficiency
|
||||
* Reduces buffer sizes to minimize memory usage while maintaining functionality
|
||||
*
|
||||
* @param client MQTT client to optimize
|
||||
* @param is_analyzer_client If true, uses larger buffer for JWT tokens (768 bytes)
|
||||
*/
|
||||
void optimizeMqttClientConfig(PsychicMqttClient* client, bool is_analyzer_client = false);
|
||||
|
||||
/**
|
||||
* Enable/disable message types
|
||||
*
|
||||
* @param status Enable status messages
|
||||
* @param packets Enable packet messages
|
||||
* @param raw Enable raw messages
|
||||
*/
|
||||
void setMessageTypes(bool status, bool packets, bool raw);
|
||||
|
||||
/**
|
||||
* Get connection status for all brokers
|
||||
*
|
||||
* @return Number of connected brokers
|
||||
*/
|
||||
int getConnectedBrokers() const;
|
||||
|
||||
/**
|
||||
* Get queue status
|
||||
*
|
||||
* @return Number of queued packets
|
||||
*/
|
||||
int getQueueSize() const;
|
||||
bool isReady() const;
|
||||
|
||||
/**
|
||||
* Set optional pointers for stats collection.
|
||||
* If these are set, stats will be collected automatically when publishing status.
|
||||
*
|
||||
* @param dispatcher Dispatcher (or Mesh*) for air times and errors
|
||||
* @param radio Radio for noise floor
|
||||
* @param board MainBoard for battery voltage
|
||||
* @param ms MillisecondClock for uptime
|
||||
*/
|
||||
void setStatsSources(mesh::Dispatcher* dispatcher, mesh::Radio* radio,
|
||||
static unsigned long getWifiConnectedAtMillis();
|
||||
static void formatMqttStatusReply(char* buf, size_t bufsize, const NodePrefs* prefs);
|
||||
|
||||
void setStatsSources(mesh::Dispatcher* dispatcher, mesh::Radio* radio,
|
||||
mesh::MainBoard* board, mesh::MillisecondClock* ms);
|
||||
|
||||
private:
|
||||
/**
|
||||
* Generate client version string in format "meshcore/{firmware_version}"
|
||||
* Memory-efficient: writes to provided buffer, no dynamic allocation
|
||||
*
|
||||
* @param buffer Buffer to write the client version string to
|
||||
* @param buffer_size Size of the buffer (must be at least 64 bytes)
|
||||
*/
|
||||
void getClientVersion(char* buffer, size_t buffer_size) const;
|
||||
|
||||
/**
|
||||
* Log memory status for debugging
|
||||
*/
|
||||
void logMemoryStatus();
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user