From 5af833c2e5288a7756160f33c6c6f607cee82dd2 Mon Sep 17 00:00:00 2001 From: Bert Melis Date: Sat, 4 Mar 2023 11:30:56 +0100 Subject: [PATCH] avoid breaking change * Enable "old" constructors again --- .../largepayload-esp8266.ino | 1 - examples/ota-esp8266/ota-esp8266.ino | 1 - examples/simple-esp32/simple-esp32.ino | 1 - examples/simple-esp8266/simple-esp8266.ino | 1 - .../simpleAsync-esp32/simpleAsync-esp32.ino | 1 - .../simpleAsync-esp8266.ino | 1 - examples/tls-esp32/tls-esp32.ino | 12 ++- src/MqttClient.cpp | 19 ++-- src/MqttClient.h | 8 +- src/MqttClientSetup.h | 9 +- src/TypeDefs.h | 5 + src/espMqttClient.cpp | 97 ++++++++++++------- src/espMqttClient.h | 55 +++++++---- src/espMqttClientAsync.cpp | 11 +-- src/espMqttClientAsync.h | 4 - 15 files changed, 127 insertions(+), 99 deletions(-) diff --git a/examples/largepayload-esp8266/largepayload-esp8266.ino b/examples/largepayload-esp8266/largepayload-esp8266.ino index ce4123f..17e8fb8 100644 --- a/examples/largepayload-esp8266/largepayload-esp8266.ino +++ b/examples/largepayload-esp8266/largepayload-esp8266.ino @@ -1,5 +1,4 @@ #include -#include #include diff --git a/examples/ota-esp8266/ota-esp8266.ino b/examples/ota-esp8266/ota-esp8266.ino index f82b36c..767c601 100644 --- a/examples/ota-esp8266/ota-esp8266.ino +++ b/examples/ota-esp8266/ota-esp8266.ino @@ -1,6 +1,5 @@ #include #include -#include #include diff --git a/examples/simple-esp32/simple-esp32.ino b/examples/simple-esp32/simple-esp32.ino index 4698587..ec57e94 100644 --- a/examples/simple-esp32/simple-esp32.ino +++ b/examples/simple-esp32/simple-esp32.ino @@ -1,5 +1,4 @@ #include -#include #include diff --git a/examples/simple-esp8266/simple-esp8266.ino b/examples/simple-esp8266/simple-esp8266.ino index 6eda510..3e00616 100644 --- a/examples/simple-esp8266/simple-esp8266.ino +++ b/examples/simple-esp8266/simple-esp8266.ino @@ -1,5 +1,4 @@ #include -#include #include diff --git a/examples/simpleAsync-esp32/simpleAsync-esp32.ino b/examples/simpleAsync-esp32/simpleAsync-esp32.ino index afd2445..ec33bf1 100644 --- a/examples/simpleAsync-esp32/simpleAsync-esp32.ino +++ b/examples/simpleAsync-esp32/simpleAsync-esp32.ino @@ -1,5 +1,4 @@ #include -#include #include diff --git a/examples/simpleAsync-esp8266/simpleAsync-esp8266.ino b/examples/simpleAsync-esp8266/simpleAsync-esp8266.ino index 08c88b1..e49a99d 100644 --- a/examples/simpleAsync-esp8266/simpleAsync-esp8266.ino +++ b/examples/simpleAsync-esp8266/simpleAsync-esp8266.ino @@ -1,5 +1,4 @@ #include -#include #include diff --git a/examples/tls-esp32/tls-esp32.ino b/examples/tls-esp32/tls-esp32.ino index b759aff..a26e678 100644 --- a/examples/tls-esp32/tls-esp32.ino +++ b/examples/tls-esp32/tls-esp32.ino @@ -1,5 +1,4 @@ #include -#include #include @@ -16,7 +15,8 @@ const char rootCA[] = \ " add your certificate here \n" \ "-----END CERTIFICATE-----\n"; -espMqttClientSecure mqttClient; +espMqttClientSecure mqttClient(espMqttClientTypes::UseInternalTask::NO); +static TaskHandle_t taskHandle; bool reconnectMqtt = false; uint32_t lastReconnect = 0; @@ -116,6 +116,12 @@ void onMqttPublish(uint16_t packetId) { Serial.println(packetId); } +void networkingTask() { + for (;;) { + mqttClient.loop(); + } +} + void setup() { Serial.begin(115200); Serial.println(); @@ -137,6 +143,8 @@ void setup() { mqttClient.setServer(MQTT_HOST, MQTT_PORT); mqttClient.setCleanSession(true); + xTaskCreatePinnedToCore((TaskFunction_t)networkingTask, "mqttclienttask", 5120, nullptr, 1, &taskHandle, 0); + connectToWiFi(); } diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp index af7f1f9..419f4a6 100644 --- a/src/MqttClient.cpp +++ b/src/MqttClient.cpp @@ -13,12 +13,11 @@ using espMqttClientInternals::PacketType; using espMqttClientTypes::DisconnectReason; using espMqttClientTypes::Error; +MqttClient::MqttClient(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority, uint8_t core) #if defined(ARDUINO_ARCH_ESP32) -MqttClient::MqttClient(bool useTask, uint8_t priority, uint8_t core) -: _useTask(useTask) +: _useInternalTask(useInternalTask) , _transport(nullptr) #else -MqttClient::MqttClient() : _transport(nullptr) #endif , _onConnectCallback(nullptr) @@ -58,19 +57,19 @@ MqttClient::MqttClient() , _lastServerActivity(0) , _pingSent(false) , _disconnectReason(DisconnectReason::TCP_DISCONNECTED) -#if defined(ARDUINO_ARCH_ESP32) -#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO +#if defined(ARDUINO_ARCH_ESP32) && ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO , _highWaterMark(4294967295) -#endif #endif { EMC_GENERATE_CLIENTID(_generatedClientId); #if defined(ARDUINO_ARCH_ESP32) _xSemaphore = xSemaphoreCreateMutex(); EMC_SEMAPHORE_GIVE(); // release before first use - if (useTask) { + if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { xTaskCreatePinnedToCore((TaskFunction_t)_loop, "mqttclient", EMC_TASK_STACK_SIZE, this, priority, &_taskHandle, core); } +#else + (void) useInternalTask; #endif _clientId = _generatedClientId; } @@ -80,7 +79,7 @@ MqttClient::~MqttClient() { _clearQueue(2); #if defined(ARDUINO_ARCH_ESP32) vSemaphoreDelete(_xSemaphore); - if (_useTask) { + if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { #if EMC_USE_WATCHDOG esp_task_wdt_delete(_taskHandle); // not sure if this is really needed #endif @@ -114,7 +113,7 @@ bool MqttClient::connect() { (uint16_t)(_keepAlive / 1000), // 32b to 16b doesn't overflow because it comes from 16b orignally _clientId)) { #if defined(ARDUINO_ARCH_ESP32) - if (_useTask) { + if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { vTaskResume(_taskHandle); } #endif @@ -197,7 +196,7 @@ void MqttClient::loop() { switch (_state) { case State::disconnected: #if defined(ARDUINO_ARCH_ESP32) - if (_useTask) { + if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) { vTaskSuspend(_taskHandle); } #endif diff --git a/src/MqttClient.h b/src/MqttClient.h index 382997c..e04b5aa 100644 --- a/src/MqttClient.h +++ b/src/MqttClient.h @@ -68,12 +68,8 @@ class MqttClient { void loop(); protected: - #if defined(ARDUINO_ARCH_ESP32) - explicit MqttClient(bool useTask, uint8_t priority = 1, uint8_t core = 1); - bool _useTask; - #else - MqttClient(); - #endif + explicit MqttClient(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority = 1, uint8_t core = 1); + espMqttClientTypes::UseInternalTask _useInternalTask; espMqttClientInternals::Transport* _transport; espMqttClientTypes::OnConnectCallback _onConnectCallback; diff --git a/src/MqttClientSetup.h b/src/MqttClientSetup.h index 40c68af..73458d4 100644 --- a/src/MqttClientSetup.h +++ b/src/MqttClientSetup.h @@ -111,11 +111,6 @@ class MqttClientSetup : public MqttClient { */ protected: -#if defined(ESP32) - explicit MqttClientSetup(bool useTask, uint8_t priority = 1, uint8_t core = 1) - : MqttClient(useTask, priority, core) {} -#else - MqttClientSetup() - : MqttClient() {} -#endif + explicit MqttClientSetup(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority = 1, uint8_t core = 1) + : MqttClient(useInternalTask, priority, core) {} }; diff --git a/src/TypeDefs.h b/src/TypeDefs.h index 2e765ad..0f15360 100644 --- a/src/TypeDefs.h +++ b/src/TypeDefs.h @@ -65,4 +65,9 @@ typedef std::function OnPublishCallback; typedef std::function PayloadCallback; typedef std::function OnErrorCallback; +enum class UseInternalTask { + NO = 0, + YES = 1, +}; + } // end namespace espMqttClientTypes diff --git a/src/espMqttClient.cpp b/src/espMqttClient.cpp index bda3038..833ece1 100644 --- a/src/espMqttClient.cpp +++ b/src/espMqttClient.cpp @@ -8,26 +8,16 @@ the LICENSE file. #include "espMqttClient.h" -#if defined(ARDUINO_ARCH_ESP32) -espMqttClient::espMqttClient(bool internalTask, uint8_t priority, uint8_t core) -: MqttClientSetup(internalTask, priority, core) -, _client() { -#else +#if defined(ARDUINO_ARCH_ESP8266) espMqttClient::espMqttClient() -: _client() { -#endif +: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO) +, _client() { _transport = &_client; } -#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) -#if defined(ARDUINO_ARCH_ESP32) -espMqttClientSecure::espMqttClientSecure(bool internalTask, uint8_t priority, uint8_t core) -: MqttClientSetup(internalTask, priority, core) -, _client() { -#else espMqttClientSecure::espMqttClientSecure() -: _client() { -#endif +: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO) +, _client() { _transport = &_client; } @@ -36,51 +26,88 @@ espMqttClientSecure& espMqttClientSecure::setInsecure() { return *this; } -#if defined(ARDUINO_ARCH_ESP32) -espMqttClientSecure& espMqttClientSecure::setCACert(const char* rootCA) { - _client.client.setCACert(rootCA); +espMqttClientSecure& espMqttClientSecure::setFingerprint(const uint8_t fingerprint[20]) { + _client.client.setFingerprint(fingerprint); return *this; } -espMqttClientSecure& espMqttClientSecure::setCertificate(const char* clientCa) { - _client.client.setCertificate(clientCa); +espMqttClientSecure& espMqttClientSecure::setTrustAnchors(const X509List *ta) { + _client.client.setTrustAnchors(ta); return *this; } -espMqttClientSecure& espMqttClientSecure::setPrivateKey(const char* privateKey) { - _client.client.setPrivateKey(privateKey); +espMqttClientSecure& espMqttClientSecure::setClientRSACert(const X509List *cert, const PrivateKey *sk) { + _client.client.setClientRSACert(cert, sk); return *this; } -espMqttClientSecure& espMqttClientSecure::setPreSharedKey(const char* pskIdent, const char* psKey) { - _client.client.setPreSharedKey(pskIdent, psKey); +espMqttClientSecure& espMqttClientSecure::setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type) { + _client.client.setClientECCert(cert, sk, allowed_usages, cert_issuer_key_type); return *this; } -#elif defined(ARDUINO_ARCH_ESP8266) -espMqttClientSecure& espMqttClientSecure::setFingerprint(const uint8_t fingerprint[20]) { - _client.client.setFingerprint(fingerprint); + +espMqttClientSecure& espMqttClientSecure::setCertStore(CertStoreBase *certStore) { + _client.client.setCertStore(certStore); return *this; } +#endif -espMqttClientSecure& espMqttClientSecure::setTrustAnchors(const X509List *ta) { - _client.client.setTrustAnchors(ta); +#if defined(ARDUINO_ARCH_ESP32) +espMqttClient::espMqttClient(espMqttClientTypes::UseInternalTask useInternalTask) +: MqttClientSetup(useInternalTask) +, _client() { + _transport = &_client; +} + +espMqttClient::espMqttClient(uint8_t priority, uint8_t core) +: MqttClientSetup(espMqttClientTypes::UseInternalTask::YES, priority, core) +, _client() { + _transport = &_client; +} + +espMqttClientSecure::espMqttClientSecure(espMqttClientTypes::UseInternalTask useInternalTask) +: MqttClientSetup(useInternalTask) +, _client() { + _transport = &_client; +} + +espMqttClientSecure::espMqttClientSecure(uint8_t priority, uint8_t core) +: MqttClientSetup(espMqttClientTypes::UseInternalTask::YES, priority, core) +, _client() { + _transport = &_client; +} + +espMqttClientSecure& espMqttClientSecure::setInsecure() { + _client.client.setInsecure(); return *this; } -espMqttClientSecure& espMqttClientSecure::setClientRSACert(const X509List *cert, const PrivateKey *sk) { - _client.client.setClientRSACert(cert, sk); +espMqttClientSecure& espMqttClientSecure::setCACert(const char* rootCA) { + _client.client.setCACert(rootCA); return *this; } -espMqttClientSecure& espMqttClientSecure::setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type) { - _client.client.setClientECCert(cert, sk, allowed_usages, cert_issuer_key_type); +espMqttClientSecure& espMqttClientSecure::setCertificate(const char* clientCa) { + _client.client.setCertificate(clientCa); return *this; } -espMqttClientSecure& espMqttClientSecure::setCertStore(CertStoreBase *certStore) { - _client.client.setCertStore(certStore); +espMqttClientSecure& espMqttClientSecure::setPrivateKey(const char* privateKey) { + _client.client.setPrivateKey(privateKey); + return *this; +} + +espMqttClientSecure& espMqttClientSecure::setPreSharedKey(const char* pskIdent, const char* psKey) { + _client.client.setPreSharedKey(pskIdent, psKey); return *this; } + #endif +#if defined(__linux__) +espMqttClient::espMqttClient() +: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO) +, _client() { + _transport = &_client; +} #endif diff --git a/src/espMqttClient.h b/src/espMqttClient.h index a2aba97..4e44801 100644 --- a/src/espMqttClient.h +++ b/src/espMqttClient.h @@ -20,46 +20,61 @@ the LICENSE file. #include "MqttClientSetup.h" +#if defined(ARDUINO_ARCH_ESP8266) class espMqttClient : public MqttClientSetup { public: -#if defined(ARDUINO_ARCH_ESP32) - explicit espMqttClient(bool internalTask = true, uint8_t priority = 1, uint8_t core = 1); -#else espMqttClient(); -#endif protected: -#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) espMqttClientInternals::ClientSync _client; -#elif defined(__linux__) - espMqttClientInternals::ClientPosix _client; -#endif }; -#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) class espMqttClientSecure : public MqttClientSetup { public: - #if defined(ARDUINO_ARCH_ESP32) - explicit espMqttClientSecure(bool internalTask = true, uint8_t priority = 1, uint8_t core = 1); - #else espMqttClientSecure(); - #endif espMqttClientSecure& setInsecure(); - #if defined(ARDUINO_ARCH_ESP32) - espMqttClientSecure& setCACert(const char* rootCA); - espMqttClientSecure& setCertificate(const char* clientCa); - espMqttClientSecure& setPrivateKey(const char* privateKey); - espMqttClientSecure& setPreSharedKey(const char* pskIdent, const char* psKey); - #else espMqttClientSecure& setFingerprint(const uint8_t fingerprint[20]); espMqttClientSecure& setTrustAnchors(const X509List *ta); espMqttClientSecure& setClientRSACert(const X509List *cert, const PrivateKey *sk); espMqttClientSecure& setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type); espMqttClientSecure& setCertStore(CertStoreBase *certStore); - #endif protected: espMqttClientInternals::ClientSecureSync _client; }; +#endif + +#if defined(ARDUINO_ARCH_ESP32) +class espMqttClient : public MqttClientSetup { + public: + explicit espMqttClient(espMqttClientTypes::UseInternalTask useInternalTask); + explicit espMqttClient(uint8_t priority = 1, uint8_t core = 1); + + protected: + espMqttClientInternals::ClientSync _client; +}; + +class espMqttClientSecure : public MqttClientSetup { + public: + explicit espMqttClientSecure(espMqttClientTypes::UseInternalTask useInternalTask); + explicit espMqttClientSecure(uint8_t priority = 1, uint8_t core = 1); + espMqttClientSecure& setInsecure(); + espMqttClientSecure& setCACert(const char* rootCA); + espMqttClientSecure& setCertificate(const char* clientCa); + espMqttClientSecure& setPrivateKey(const char* privateKey); + espMqttClientSecure& setPreSharedKey(const char* pskIdent, const char* psKey); + + protected: + espMqttClientInternals::ClientSecureSync _client; +}; +#endif + +#if defined(__linux__) +class espMqttClient : public MqttClientSetup { + public: + espMqttClient(); + protected: + espMqttClientInternals::ClientPosix _client; +}; #endif diff --git a/src/espMqttClientAsync.cpp b/src/espMqttClientAsync.cpp index bc53878..98b7f15 100644 --- a/src/espMqttClientAsync.cpp +++ b/src/espMqttClientAsync.cpp @@ -10,17 +10,10 @@ the LICENSE file. #include "espMqttClientAsync.h" -#if defined(ARDUINO_ARCH_ESP32) -espMqttClientAsync::espMqttClientAsync(uint8_t priority, uint8_t core) -: MqttClientSetup(false, priority, core) -, _clientAsync() { -#else espMqttClientAsync::espMqttClientAsync() -: _clientAsync() { -#endif +: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO) +, _clientAsync() { _transport = &_clientAsync; - // _onConnectHook = reinterpret_cast(_setupClient); - // _onConnectHookArg = this; _clientAsync.client.onConnect(onConnectCb, this); _clientAsync.client.onDisconnect(onDisconnectCb, this); _clientAsync.client.onData(onDataCb, this); diff --git a/src/espMqttClientAsync.h b/src/espMqttClientAsync.h index f3f321d..1b9ed8b 100644 --- a/src/espMqttClientAsync.h +++ b/src/espMqttClientAsync.h @@ -19,11 +19,7 @@ the LICENSE file. class espMqttClientAsync : public MqttClientSetup { public: -#if defined(ARDUINO_ARCH_ESP32) - explicit espMqttClientAsync(uint8_t priority = 1, uint8_t core = 1); -#else espMqttClientAsync(); -#endif bool connect(); protected: