update mqttClient, works with tasmota-arduino 2.14 / 3.0 without WiFiSecure

This commit is contained in:
MichaelDvP
2023-12-19 13:47:10 +01:00
parent 9f3e2dbcd3
commit 446601c6e0
6 changed files with 172 additions and 8 deletions

View File

@@ -8,6 +8,10 @@ the LICENSE file.
#pragma once #pragma once
#ifndef TASMOTA_SDK
#define EMC_CLIENT_SECURE
#endif
#ifndef EMC_TX_TIMEOUT #ifndef EMC_TX_TIMEOUT
#define EMC_TX_TIMEOUT 2000 #define EMC_TX_TIMEOUT 2000
#endif #endif
@@ -53,6 +57,10 @@ the LICENSE file.
#define EMC_TASK_STACK_SIZE 5120 #define EMC_TASK_STACK_SIZE 5120
#endif #endif
#ifndef EMC_MULTIPLE_CALLBACKS
#define EMC_MULTIPLE_CALLBACKS 0
#endif
#ifndef EMC_USE_WATCHDOG #ifndef EMC_USE_WATCHDOG
#define EMC_USE_WATCHDOG 0 #define EMC_USE_WATCHDOG 0
#endif #endif

View File

@@ -11,6 +11,11 @@ the LICENSE file.
#pragma once #pragma once
#if EMC_MULTIPLE_CALLBACKS
#include <list>
#include <utility>
#endif
#include "MqttClient.h" #include "MqttClient.h"
template <typename T> template <typename T>
@@ -73,36 +78,128 @@ class MqttClientSetup : public MqttClient {
return static_cast<T&>(*this); return static_cast<T&>(*this);
} }
T& onConnect(espMqttClientTypes::OnConnectCallback callback) { T& onConnect(espMqttClientTypes::OnConnectCallback callback, uint32_t id = 0) {
#if EMC_MULTIPLE_CALLBACKS
_onConnectCallbacks.emplace_back(callback, id);
#else
(void) id;
_onConnectCallback = callback; _onConnectCallback = callback;
#endif
return static_cast<T&>(*this); return static_cast<T&>(*this);
} }
T& onDisconnect(espMqttClientTypes::OnDisconnectCallback callback) { T& onDisconnect(espMqttClientTypes::OnDisconnectCallback callback, uint32_t id = 0) {
#if EMC_MULTIPLE_CALLBACKS
_onDisconnectCallbacks.emplace_back(callback, id);
#else
(void) id;
_onDisconnectCallback = callback; _onDisconnectCallback = callback;
#endif
return static_cast<T&>(*this); return static_cast<T&>(*this);
} }
T& onSubscribe(espMqttClientTypes::OnSubscribeCallback callback) { T& onSubscribe(espMqttClientTypes::OnSubscribeCallback callback, uint32_t id = 0) {
#if EMC_MULTIPLE_CALLBACKS
_onSubscribeCallbacks.emplace_back(callback, id);
#else
(void) id;
_onSubscribeCallback = callback; _onSubscribeCallback = callback;
#endif
return static_cast<T&>(*this); return static_cast<T&>(*this);
} }
T& onUnsubscribe(espMqttClientTypes::OnUnsubscribeCallback callback) { T& onUnsubscribe(espMqttClientTypes::OnUnsubscribeCallback callback, uint32_t id = 0) {
#if EMC_MULTIPLE_CALLBACKS
_onUnsubscribeCallbacks.emplace_back(callback, id);
#else
(void) id;
_onUnsubscribeCallback = callback; _onUnsubscribeCallback = callback;
#endif
return static_cast<T&>(*this); return static_cast<T&>(*this);
} }
T& onMessage(espMqttClientTypes::OnMessageCallback callback) { T& onMessage(espMqttClientTypes::OnMessageCallback callback, uint32_t id = 0) {
#if EMC_MULTIPLE_CALLBACKS
_onMessageCallbacks.emplace_back(callback, id);
#else
(void) id;
_onMessageCallback = callback; _onMessageCallback = callback;
#endif
return static_cast<T&>(*this); return static_cast<T&>(*this);
} }
T& onPublish(espMqttClientTypes::OnPublishCallback callback) { T& onPublish(espMqttClientTypes::OnPublishCallback callback, uint32_t id = 0) {
#if EMC_MULTIPLE_CALLBACKS
_onPublishCallbacks.emplace_back(callback, id);
#else
(void) id;
_onPublishCallback = callback; _onPublishCallback = callback;
#endif
return static_cast<T&>(*this); return static_cast<T&>(*this);
} }
#if EMC_MULTIPLE_CALLBACKS
T& removeOnConnect(uint32_t id) {
for (auto it = _onConnectCallbacks.begin(); it != _onConnectCallbacks.end(); ++it) {
if (it->second == id) {
_onConnectCallbacks.erase(it);
break;
}
}
return static_cast<T&>(*this);
}
T& removeOnDisconnect(uint32_t id) {
for (auto it = _onDisconnectCallbacks.begin(); it != _onDisconnectCallbacks.end(); ++it) {
if (it->second == id) {
_onDisconnectCallbacks.erase(it);
break;
}
}
return static_cast<T&>(*this);
}
T& removeOnSubscribe(uint32_t id) {
for (auto it = _onSubscribeCallbacks.begin(); it != _onSubscribeCallbacks.end(); ++it) {
if (it->second == id) {
_onSubscribeCallbacks.erase(it);
break;
}
}
return static_cast<T&>(*this);
}
T& removeOnUnsubscribe(uint32_t id) {
for (auto it = _onUnsubscribeCallbacks.begin(); it != _onUnsubscribeCallbacks.end(); ++it) {
if (it->second == id) {
_onUnsubscribeCallbacks.erase(it);
break;
}
}
return static_cast<T&>(*this);
}
T& removeOnMessage(uint32_t id) {
for (auto it = _onMessageCallbacks.begin(); it != _onMessageCallbacks.end(); ++it) {
if (it->second == id) {
_onMessageCallbacks.erase(it);
break;
}
}
return static_cast<T&>(*this);
}
T& removeOnPublish(uint32_t id) {
for (auto it = _onPublishCallbacks.begin(); it != _onPublishCallbacks.end(); ++it) {
if (it->second == id) {
_onPublishCallbacks.erase(it);
break;
}
}
return static_cast<T&>(*this);
}
#endif
/* /*
T& onError(espMqttClientTypes::OnErrorCallback callback) { T& onError(espMqttClientTypes::OnErrorCallback callback) {
_onErrorCallback = callback; _onErrorCallback = callback;
@@ -112,5 +209,37 @@ class MqttClientSetup : public MqttClient {
protected: protected:
explicit MqttClientSetup(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority = 1, uint8_t core = 1) explicit MqttClientSetup(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority = 1, uint8_t core = 1)
: MqttClient(useInternalTask, priority, core) {} : MqttClient(useInternalTask, priority, core) {
#if EMC_MULTIPLE_CALLBACKS
_onConnectCallback = [this](bool sessionPresent) {
for (auto callback : _onConnectCallbacks) if (callback.first) callback.first(sessionPresent);
};
_onDisconnectCallback = [this](espMqttClientTypes::DisconnectReason reason) {
for (auto callback : _onDisconnectCallbacks) if (callback.first) callback.first(reason);
};
_onSubscribeCallback = [this](uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* returncodes, size_t len) {
for (auto callback : _onSubscribeCallbacks) if (callback.first) callback.first(packetId, returncodes, len);
};
_onUnsubscribeCallback = [this](int16_t packetId) {
for (auto callback : _onUnsubscribeCallbacks) if (callback.first) callback.first(packetId);
};
_onMessageCallback = [this](const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) {
for (auto callback : _onMessageCallbacks) if (callback.first) callback.first(properties, topic, payload, len, index, total);
};
_onPublishCallback = [this](uint16_t packetId) {
for (auto callback : _onPublishCallbacks) if (callback.first) callback.first(packetId);
};
#else
// empty
#endif
}
#if EMC_MULTIPLE_CALLBACKS
std::list<std::pair<espMqttClientTypes::OnConnectCallback, uint32_t>> _onConnectCallbacks;
std::list<std::pair<espMqttClientTypes::OnDisconnectCallback, uint32_t>> _onDisconnectCallbacks;
std::list<std::pair<espMqttClientTypes::OnSubscribeCallback, uint32_t>> _onSubscribeCallbacks;
std::list<std::pair<espMqttClientTypes::OnUnsubscribeCallback, uint32_t>> _onUnsubscribeCallbacks;
std::list<std::pair<espMqttClientTypes::OnMessageCallback, uint32_t>> _onMessageCallbacks;
std::list<std::pair<espMqttClientTypes::OnPublishCallback, uint32_t>> _onPublishCallbacks;
#endif
}; };

View File

@@ -10,7 +10,12 @@ the LICENSE file.
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#include "../Config.h"
#if defined(EMC_CLIENT_SECURE)
#include <WiFiClientSecure.h> // includes IPAddress #include <WiFiClientSecure.h> // includes IPAddress
#else
#include <WiFiClient.h>
#endif
#include "Transport.h" #include "Transport.h"
@@ -26,7 +31,11 @@ class ClientSecureSync : public Transport {
void stop() override; void stop() override;
bool connected() override; bool connected() override;
bool disconnected() override; bool disconnected() override;
#if defined(EMC_CLIENT_SECURE)
WiFiClientSecure client; WiFiClientSecure client;
#else
WiFiClient client;
#endif
}; };
} // namespace espMqttClientInternals } // namespace espMqttClientInternals

View File

@@ -78,27 +78,37 @@ espMqttClientSecure::espMqttClientSecure(uint8_t priority, uint8_t core)
} }
espMqttClientSecure& espMqttClientSecure::setInsecure() { espMqttClientSecure& espMqttClientSecure::setInsecure() {
#if defined(EMC_CLIENT_SECURE)
_client.client.setInsecure(); _client.client.setInsecure();
#endif
return *this; return *this;
} }
espMqttClientSecure& espMqttClientSecure::setCACert(const char* rootCA) { espMqttClientSecure& espMqttClientSecure::setCACert(const char* rootCA) {
#if defined(EMC_CLIENT_SECURE)
_client.client.setCACert(rootCA); _client.client.setCACert(rootCA);
#endif
return *this; return *this;
} }
espMqttClientSecure& espMqttClientSecure::setCertificate(const char* clientCa) { espMqttClientSecure& espMqttClientSecure::setCertificate(const char* clientCa) {
#if defined(EMC_CLIENT_SECURE)
_client.client.setCertificate(clientCa); _client.client.setCertificate(clientCa);
#endif
return *this; return *this;
} }
espMqttClientSecure& espMqttClientSecure::setPrivateKey(const char* privateKey) { espMqttClientSecure& espMqttClientSecure::setPrivateKey(const char* privateKey) {
#if defined(EMC_CLIENT_SECURE)
_client.client.setPrivateKey(privateKey); _client.client.setPrivateKey(privateKey);
#endif
return *this; return *this;
} }
espMqttClientSecure& espMqttClientSecure::setPreSharedKey(const char* pskIdent, const char* psKey) { espMqttClientSecure& espMqttClientSecure::setPreSharedKey(const char* pskIdent, const char* psKey) {
#if defined(EMC_CLIENT_SECURE)
_client.client.setPreSharedKey(pskIdent, psKey); _client.client.setPreSharedKey(pskIdent, psKey);
#endif
return *this; return *this;
} }

View File

@@ -218,8 +218,10 @@ bool MqttSettingsService::configureMqtt() {
void MqttSettings::read(MqttSettings & settings, JsonObject & root) { void MqttSettings::read(MqttSettings & settings, JsonObject & root) {
#if CONFIG_IDF_TARGET_ESP32S3 #if CONFIG_IDF_TARGET_ESP32S3
#ifndef TASMOTA_SDK
root["enableTLS"] = settings.enableTLS; root["enableTLS"] = settings.enableTLS;
root["rootCA"] = settings.rootCA; root["rootCA"] = settings.rootCA;
#endif
#endif #endif
root["enabled"] = settings.enabled; root["enabled"] = settings.enabled;
root["host"] = settings.host; root["host"] = settings.host;
@@ -255,8 +257,10 @@ StateUpdateResult MqttSettings::update(JsonObject & root, MqttSettings & setting
bool changed = false; bool changed = false;
#if CONFIG_IDF_TARGET_ESP32S3 #if CONFIG_IDF_TARGET_ESP32S3
#ifndef TASMOTA_SDK
newSettings.enableTLS = root["enableTLS"] | false; newSettings.enableTLS = root["enableTLS"] | false;
newSettings.rootCA = root["rootCA"] | ""; newSettings.rootCA = root["rootCA"] | "";
#endif
#endif #endif
newSettings.enabled = root["enabled"] | FACTORY_MQTT_ENABLED; newSettings.enabled = root["enabled"] | FACTORY_MQTT_ENABLED;
newSettings.host = root["host"] | FACTORY_MQTT_HOST; newSettings.host = root["host"] | FACTORY_MQTT_HOST;

View File

@@ -51,8 +51,12 @@ extra_scripts =
[espressi32_base_tasmota] [espressi32_base_tasmota]
; use Tasmota's libary which removes some libs (like mbedtsl) and increases available heap ; use Tasmota's libary which removes some libs (like mbedtsl) and increases available heap
; platform = https://github.com/tasmota/platform-espressif32.git ; latest development ; platform = https://github.com/tasmota/platform-espressif32.git ; latest development
; latest release with WiFi_secure.h
platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.10.03/platform-espressif32-2023.10.03.zip ; latest stable platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.10.03/platform-espressif32-2023.10.03.zip ; latest stable
; platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.10.02/platform-espressif32.zip ; latest arduino 2.xx release:
; platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.11.01/platform-espressif32.zip
; latest arduino 3.0/IDF 5.1.(alpha 3):
; platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.12.10/platform-espressif32.zip
framework = arduino framework = arduino
board_build.filesystem = littlefs board_build.filesystem = littlefs
build_flags = build_flags =