added configuration for mqtt.sever, mqtt.username, mqtt.pasword, and mqtt.port
validated primary MQTT server credentials are set before opening connection
This commit is contained in:
1 parent
ed70ceb284
commit
58b73343c1
6 files changed
+418
-23
No files matched your search
@@ -3,6 +3,9 @@
|
||||
#include "TxtDataHelpers.h"
|
||||
#include "AdvertDataHelpers.h"
|
||||
#include <RTClib.h>
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
#include "bridges/MQTTBridge.h"
|
||||
#endif
|
||||
|
||||
// Believe it or not, this std C function is busted on some platforms!
|
||||
static uint32_t _atoi(const char* sp) {
|
||||
@@ -117,9 +120,15 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
file.read((uint8_t *)&_prefs->timezone_string, sizeof(_prefs->timezone_string)); // 305
|
||||
file.read((uint8_t *)&_prefs->timezone_offset, sizeof(_prefs->timezone_offset)); // 337
|
||||
|
||||
// MQTT server settings
|
||||
file.read((uint8_t *)&_prefs->mqtt_server, sizeof(_prefs->mqtt_server)); // 340
|
||||
file.read((uint8_t *)&_prefs->mqtt_port, sizeof(_prefs->mqtt_port)); // 341
|
||||
file.read((uint8_t *)&_prefs->mqtt_username, sizeof(_prefs->mqtt_username)); // 342
|
||||
file.read((uint8_t *)&_prefs->mqtt_password, sizeof(_prefs->mqtt_password)); // 343
|
||||
|
||||
// Let's Mesh Analyzer settings
|
||||
file.read((uint8_t *)&_prefs->mqtt_analyzer_us_enabled, sizeof(_prefs->mqtt_analyzer_us_enabled)); // 338
|
||||
file.read((uint8_t *)&_prefs->mqtt_analyzer_eu_enabled, sizeof(_prefs->mqtt_analyzer_eu_enabled)); // 339
|
||||
file.read((uint8_t *)&_prefs->mqtt_analyzer_us_enabled, sizeof(_prefs->mqtt_analyzer_us_enabled)); // 344
|
||||
file.read((uint8_t *)&_prefs->mqtt_analyzer_eu_enabled, sizeof(_prefs->mqtt_analyzer_eu_enabled)); // 345
|
||||
// 209
|
||||
>>>>>>> 6f42dc3 (Implement Let's Mesh Analyzer integration in MQTT Bridge)
|
||||
|
||||
@@ -246,9 +255,15 @@ void CommonCLI::savePrefs(FILESYSTEM* fs) {
|
||||
file.write((uint8_t *)&_prefs->timezone_string, sizeof(_prefs->timezone_string)); // 305
|
||||
file.write((uint8_t *)&_prefs->timezone_offset, sizeof(_prefs->timezone_offset)); // 337
|
||||
|
||||
// MQTT server settings
|
||||
file.write((uint8_t *)&_prefs->mqtt_server, sizeof(_prefs->mqtt_server)); // 340
|
||||
file.write((uint8_t *)&_prefs->mqtt_port, sizeof(_prefs->mqtt_port)); // 341
|
||||
file.write((uint8_t *)&_prefs->mqtt_username, sizeof(_prefs->mqtt_username)); // 342
|
||||
file.write((uint8_t *)&_prefs->mqtt_password, sizeof(_prefs->mqtt_password)); // 343
|
||||
|
||||
// Let's Mesh Analyzer settings
|
||||
file.write((uint8_t *)&_prefs->mqtt_analyzer_us_enabled, sizeof(_prefs->mqtt_analyzer_us_enabled)); // 338
|
||||
file.write((uint8_t *)&_prefs->mqtt_analyzer_eu_enabled, sizeof(_prefs->mqtt_analyzer_eu_enabled)); // 339
|
||||
file.write((uint8_t *)&_prefs->mqtt_analyzer_us_enabled, sizeof(_prefs->mqtt_analyzer_us_enabled)); // 344
|
||||
file.write((uint8_t *)&_prefs->mqtt_analyzer_eu_enabled, sizeof(_prefs->mqtt_analyzer_eu_enabled)); // 345
|
||||
// 209
|
||||
>>>>>>> 6f42dc3 (Implement Let's Mesh Analyzer integration in MQTT Bridge)
|
||||
|
||||
@@ -453,6 +468,14 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
sprintf(reply, "> %s", _prefs->mqtt_tx_enabled ? "on" : "off");
|
||||
} else if (memcmp(config, "mqtt.interval", 13) == 0) {
|
||||
sprintf(reply, "> %d", (uint32_t)_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 (memcmp(config, "wifi.ssid", 9) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->wifi_ssid);
|
||||
} else if (memcmp(config, "wifi.pwd", 8) == 0) {
|
||||
@@ -461,10 +484,14 @@ 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", 16) == 0) {
|
||||
} 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", 16) == 0) {
|
||||
} else if (memcmp(config, "mqtt.analyzer.eu", 17) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_analyzer_eu_enabled ? "on" : "off");
|
||||
} else if (memcmp(config, "mqtt.config.valid", 17) == 0) {
|
||||
// Check if MQTT configuration is valid using static method
|
||||
bool valid = MQTTBridge::isConfigValid(_prefs);
|
||||
sprintf(reply, "> %s", valid ? "valid" : "invalid");
|
||||
#endif
|
||||
} else {
|
||||
sprintf(reply, "??: %s", config);
|
||||
@@ -713,6 +740,27 @@ 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;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error: port must be between 1 and 65535");
|
||||
}
|
||||
} 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();
|
||||
|
||||
@@ -15,7 +15,7 @@ MQTTBridge::MQTTBridge(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTCCloc
|
||||
_ntp_client(_ntp_udp, "pool.ntp.org", 0, 60000), _last_ntp_sync(0), _ntp_synced(false),
|
||||
_timezone(nullptr), _last_raw_len(0), _last_snr(0), _last_rssi(0), _last_raw_timestamp(0),
|
||||
_analyzer_us_enabled(false), _analyzer_eu_enabled(false), _identity(identity),
|
||||
_analyzer_us_client(nullptr), _analyzer_eu_client(nullptr) {
|
||||
_analyzer_us_client(nullptr), _analyzer_eu_client(nullptr), _config_valid(false) {
|
||||
|
||||
// Initialize default values
|
||||
strncpy(_origin, "MeshCore-Repeater", sizeof(_origin) - 1);
|
||||
@@ -29,6 +29,26 @@ MQTTBridge::MQTTBridge(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTCCloc
|
||||
_raw_enabled = false;
|
||||
_tx_enabled = false; // Disable TX packets by default
|
||||
|
||||
// Initialize MQTT server settings with defaults
|
||||
strncpy(_prefs->mqtt_server, "your-mqtt-broker.com", sizeof(_prefs->mqtt_server) - 1);
|
||||
_prefs->mqtt_port = 1883;
|
||||
strncpy(_prefs->mqtt_username, "your-username", sizeof(_prefs->mqtt_username) - 1);
|
||||
strncpy(_prefs->mqtt_password, "your-password", sizeof(_prefs->mqtt_password) - 1);
|
||||
|
||||
// Override with build flags if defined
|
||||
#ifdef MQTT_SERVER
|
||||
strncpy(_prefs->mqtt_server, MQTT_SERVER, sizeof(_prefs->mqtt_server) - 1);
|
||||
#endif
|
||||
#ifdef MQTT_PORT
|
||||
_prefs->mqtt_port = MQTT_PORT;
|
||||
#endif
|
||||
#ifdef MQTT_USERNAME
|
||||
strncpy(_prefs->mqtt_username, MQTT_USERNAME, sizeof(_prefs->mqtt_username) - 1);
|
||||
#endif
|
||||
#ifdef MQTT_PASSWORD
|
||||
strncpy(_prefs->mqtt_password, MQTT_PASSWORD, sizeof(_prefs->mqtt_password) - 1);
|
||||
#endif
|
||||
|
||||
// Initialize packet queue
|
||||
memset(_packet_queue, 0, sizeof(_packet_queue));
|
||||
|
||||
@@ -39,6 +59,15 @@ MQTTBridge::MQTTBridge(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTCCloc
|
||||
void MQTTBridge::begin() {
|
||||
MQTT_DEBUG_PRINTLN("Initializing MQTT Bridge...");
|
||||
|
||||
// Validate configuration once and store the result
|
||||
_config_valid = isMQTTConfigValid();
|
||||
if (!_config_valid) {
|
||||
MQTT_DEBUG_PRINTLN("MQTT Bridge initialization skipped - configuration not valid");
|
||||
return;
|
||||
}
|
||||
|
||||
MQTT_DEBUG_PRINTLN("MQTT configuration is valid - proceeding with initialization");
|
||||
|
||||
// Update origin and IATA from preferences
|
||||
strncpy(_origin, _prefs->mqtt_origin, sizeof(_origin) - 1);
|
||||
_origin[sizeof(_origin) - 1] = '\0';
|
||||
@@ -137,8 +166,8 @@ void MQTTBridge::begin() {
|
||||
}
|
||||
});
|
||||
|
||||
// Set default broker (meshtastic.pugetmesh.org)
|
||||
setBroker(0, "meshtastic.pugetmesh.org", 1883, "meshdev", "large4cats", true);
|
||||
// Set default broker from preferences or build flags
|
||||
setBroker(0, _prefs->mqtt_server, _prefs->mqtt_port, _prefs->mqtt_username, _prefs->mqtt_password, true);
|
||||
|
||||
// Setup Let's Mesh Analyzer servers
|
||||
setupAnalyzerServers();
|
||||
@@ -191,6 +220,37 @@ void MQTTBridge::end() {
|
||||
MQTT_DEBUG_PRINTLN("MQTT Bridge stopped");
|
||||
}
|
||||
|
||||
bool MQTTBridge::isConfigValid() const {
|
||||
return _config_valid;
|
||||
}
|
||||
|
||||
bool MQTTBridge::isConfigValid(const NodePrefs* prefs) {
|
||||
// Check if MQTT server is configured (not default placeholder)
|
||||
if (strlen(prefs->mqtt_server) == 0 ||
|
||||
strcmp(prefs->mqtt_server, "your-mqtt-broker.com") == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if MQTT port is valid
|
||||
if (prefs->mqtt_port == 0 || prefs->mqtt_port > 65535) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if MQTT username is configured (not default placeholder)
|
||||
if (strlen(prefs->mqtt_username) == 0 ||
|
||||
strcmp(prefs->mqtt_username, "your-username") == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if MQTT password is configured (not default placeholder)
|
||||
if (strlen(prefs->mqtt_password) == 0 ||
|
||||
strcmp(prefs->mqtt_password, "your-password") == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void MQTTBridge::loop() {
|
||||
if (!_initialized) return;
|
||||
|
||||
@@ -216,9 +276,9 @@ void MQTTBridge::loop() {
|
||||
}
|
||||
|
||||
void MQTTBridge::onPacketReceived(mesh::Packet *packet) {
|
||||
if (!_initialized || !_packets_enabled) {
|
||||
MQTT_DEBUG_PRINTLN("Packet received but not processing - initialized: %s, packets_enabled: %s",
|
||||
_initialized ? "true" : "false", _packets_enabled ? "true" : "false");
|
||||
if (!_initialized || !_packets_enabled || !_config_valid) {
|
||||
MQTT_DEBUG_PRINTLN("Packet received but not processing - initialized: %s, packets_enabled: %s, config_valid: %s",
|
||||
_initialized ? "true" : "false", _packets_enabled ? "true" : "false", _config_valid ? "true" : "false");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -228,13 +288,45 @@ void MQTTBridge::onPacketReceived(mesh::Packet *packet) {
|
||||
}
|
||||
|
||||
void MQTTBridge::sendPacket(mesh::Packet *packet) {
|
||||
if (!_initialized || !_packets_enabled || !_tx_enabled) return;
|
||||
if (!_initialized || !_packets_enabled || !_tx_enabled || !_config_valid) return;
|
||||
|
||||
// Queue packet for transmission (only if TX enabled)
|
||||
queuePacket(packet, true);
|
||||
}
|
||||
|
||||
bool MQTTBridge::isMQTTConfigValid() {
|
||||
// Check if MQTT server is configured (not default placeholder)
|
||||
if (strlen(_prefs->mqtt_server) == 0 ||
|
||||
strcmp(_prefs->mqtt_server, "your-mqtt-broker.com") == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if MQTT port is valid
|
||||
if (_prefs->mqtt_port == 0 || _prefs->mqtt_port > 65535) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if MQTT username is configured (not default placeholder)
|
||||
if (strlen(_prefs->mqtt_username) == 0 ||
|
||||
strcmp(_prefs->mqtt_username, "your-username") == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if MQTT password is configured (not default placeholder)
|
||||
if (strlen(_prefs->mqtt_password) == 0 ||
|
||||
strcmp(_prefs->mqtt_password, "your-password") == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void MQTTBridge::connectToBrokers() {
|
||||
// Check if MQTT configuration is valid before attempting connection
|
||||
if (!_config_valid) {
|
||||
return;
|
||||
}
|
||||
|
||||
// For now, connect to the first enabled broker
|
||||
// TODO: Implement multi-broker support with PsychicMqttClient
|
||||
for (int i = 0; i < MAX_MQTT_BROKERS_COUNT; i++) {
|
||||
@@ -282,9 +374,9 @@ void MQTTBridge::connectToBrokers() {
|
||||
}
|
||||
|
||||
void MQTTBridge::processPacketQueue() {
|
||||
if (_queue_count == 0 || !isAnyBrokerConnected()) {
|
||||
if (_queue_count == 0 || !isAnyBrokerConnected() || !_config_valid) {
|
||||
if (_queue_count > 0) {
|
||||
MQTT_DEBUG_PRINTLN("Queue has %d packets but no brokers connected", _queue_count);
|
||||
MQTT_DEBUG_PRINTLN("Queue has %d packets but no brokers connected or config invalid", _queue_count);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -313,7 +405,7 @@ void MQTTBridge::processPacketQueue() {
|
||||
}
|
||||
|
||||
void MQTTBridge::publishStatus() {
|
||||
if (!isAnyBrokerConnected()) return;
|
||||
if (!isAnyBrokerConnected() || !_config_valid) return;
|
||||
|
||||
char json_buffer[512];
|
||||
char origin_id[65];
|
||||
@@ -378,7 +470,7 @@ void MQTTBridge::publishStatus() {
|
||||
}
|
||||
|
||||
void MQTTBridge::publishPacket(mesh::Packet* packet, bool is_tx) {
|
||||
if (!packet) return;
|
||||
if (!packet || !_config_valid) return;
|
||||
|
||||
char json_buffer[1024];
|
||||
char origin_id[65];
|
||||
@@ -426,7 +518,7 @@ void MQTTBridge::publishPacket(mesh::Packet* packet, bool is_tx) {
|
||||
}
|
||||
|
||||
void MQTTBridge::publishRaw(mesh::Packet* packet) {
|
||||
if (!packet) return;
|
||||
if (!packet || !_config_valid) return;
|
||||
|
||||
char json_buffer[512];
|
||||
char origin_id[65];
|
||||
|
||||
@@ -122,6 +122,9 @@ private:
|
||||
PsychicMqttClient* _analyzer_us_client;
|
||||
PsychicMqttClient* _analyzer_eu_client;
|
||||
|
||||
// Configuration validation state
|
||||
bool _config_valid;
|
||||
|
||||
// Internal methods
|
||||
void connectToBrokers();
|
||||
void processPacketQueue();
|
||||
@@ -134,6 +137,7 @@ private:
|
||||
void setBrokerDefaults();
|
||||
void syncTimeWithNTP();
|
||||
Timezone* createTimezoneFromString(const char* tz_string);
|
||||
bool isMQTTConfigValid();
|
||||
|
||||
public:
|
||||
/**
|
||||
@@ -164,6 +168,21 @@ public:
|
||||
*/
|
||||
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);
|
||||
|
||||
/**
|
||||
* Main loop handler
|
||||
* - Maintains broker connections
|
||||
|
||||
Reference in new issue
Block a user