Validate MQTT config by testing a connection (#6076)

This commit is contained in:
Eric Severance
2025-02-17 13:03:44 -08:00
committed by GitHub
parent 7eb77276cd
commit 3b0232de1b
3 changed files with 173 additions and 78 deletions

View File

@@ -10,12 +10,10 @@
#endif
#if HAS_WIFI
#include <WiFiClient.h>
#if !defined(ARCH_PORTDUINO)
#if defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR < 3
#if __has_include(<WiFiClientSecure.h>)
#include <WiFiClientSecure.h>
#endif
#endif
#endif
#if HAS_ETHERNET
#include <EthernetClient.h>
#endif
@@ -61,7 +59,8 @@ class MQTT : private concurrency::OSThread
bool isUsingDefaultServer() { return isConfiguredForDefaultServer; }
static bool isValidConfig(const meshtastic_ModuleConfig_MQTTConfig &config);
/// Validate the meshtastic_ModuleConfig_MQTTConfig.
static bool isValidConfig(const meshtastic_ModuleConfig_MQTTConfig &config) { return isValidConfig(config, nullptr); }
protected:
struct QueueEntry {
@@ -78,22 +77,23 @@ class MQTT : private concurrency::OSThread
#ifndef PIO_UNIT_TESTING
private:
#endif
// supposedly the current version is busted:
// http://www.iotsharing.com/2017/08/how-to-use-esp32-mqtts-with-mqtts-mosquitto-broker-tls-ssl.html
#if HAS_WIFI
using MQTTClient = WiFiClient;
#if !defined(ARCH_PORTDUINO)
#if (defined(ESP_ARDUINO_VERSION_MAJOR) && ESP_ARDUINO_VERSION_MAJOR < 3) || defined(RPI_PICO)
WiFiClientSecure wifiSecureClient;
#if __has_include(<WiFiClientSecure.h>)
using MQTTClientTLS = WiFiClientSecure;
#define MQTT_SUPPORTS_TLS 1
#endif
#endif
#endif
#if HAS_ETHERNET
#elif HAS_ETHERNET
using MQTTClient = EthernetClient;
#else
using MQTTClient = void;
#endif
#if HAS_NETWORKING
std::unique_ptr<MQTTClient> mqttClient;
#if MQTT_SUPPORTS_TLS
MQTTClientTLS mqttClientTLS;
#endif
PubSubClient pubSub;
explicit MQTT(std::unique_ptr<MQTTClient> mqttClient);
#endif
@@ -109,10 +109,6 @@ class MQTT : private concurrency::OSThread
uint32_t map_position_precision = default_map_position_precision;
uint32_t map_publish_interval_msecs = default_map_publish_interval_secs * 1000;
/** return true if we have a channel that wants uplink/downlink or map reporting is enabled
*/
bool wantsLink() const;
/** Attempt to connect to server if necessary
*/
void reconnect();
@@ -124,6 +120,8 @@ class MQTT : private concurrency::OSThread
/// Callback for direct mqtt subscription messages
static void mqttCallback(char *topic, byte *payload, unsigned int length);
static bool isValidConfig(const meshtastic_ModuleConfig_MQTTConfig &config, MQTTClient *client);
/// Called when a new publish arrives from the MQTT server
void onReceive(char *topic, byte *payload, size_t length);