From ffb7925f1167452372958add0e35fa66b25a1732 Mon Sep 17 00:00:00 2001 From: pennam Date: Thu, 8 Aug 2024 08:40:36 +0200 Subject: [PATCH 01/11] ESP32: fix certificate bundle loading using core > 3.0.4 --- src/tls/utility/TLSClientMqtt.cpp | 6 +++++- src/tls/utility/TLSClientOta.cpp | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/tls/utility/TLSClientMqtt.cpp b/src/tls/utility/TLSClientMqtt.cpp index 37461816..dafacd53 100644 --- a/src/tls/utility/TLSClientMqtt.cpp +++ b/src/tls/utility/TLSClientMqtt.cpp @@ -60,7 +60,11 @@ void TLSClientMqtt::begin(ConnectionHandler & connection) { */ (void)connection; #elif defined(ARDUINO_ARCH_ESP32) - setCACertBundle(x509_crt_bundle); + #if (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 4)) + setCACertBundle(x509_crt_bundle, sizeof(x509_crt_bundle)); + #else + setCACertBundle(x509_crt_bundle); + #endif #elif defined(ARDUINO_ARCH_ESP8266) setInsecure(); #endif diff --git a/src/tls/utility/TLSClientOta.cpp b/src/tls/utility/TLSClientOta.cpp index 77096b6f..4e0d06d0 100644 --- a/src/tls/utility/TLSClientOta.cpp +++ b/src/tls/utility/TLSClientOta.cpp @@ -56,7 +56,11 @@ void TLSClientOta::begin(ConnectionHandler &connection) { */ (void)connection; #elif defined(ARDUINO_ARCH_ESP32) - setCACertBundle(x509_crt_bundle); + #if (ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 4)) + setCACertBundle(x509_crt_bundle, sizeof(x509_crt_bundle)); + #else + setCACertBundle(x509_crt_bundle); + #endif #elif defined(ARDUINO_ARCH_ESP8266) setInsecure(); #endif From 3918a9cf085154fda6fa4fc54eec94ab241eee5d Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 25 Jun 2024 13:37:31 +0200 Subject: [PATCH 02/11] Make begin(bool, String, uint16_t) private --- src/ArduinoIoTCloudTCP.cpp | 92 +++++++++++++++++++------------------- src/ArduinoIoTCloudTCP.h | 2 +- 2 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 26052548..11bce04d 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -102,6 +102,52 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_ return begin(enable_watchdog, _brokerAddress, _brokerPort); } +void ArduinoIoTCloudTCP::update() +{ + /* Feed the watchdog. If any of the functions called below + * get stuck than we can at least reset and recover. + */ +#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) + watchdog_reset(); +#endif + + /* Run through the state machine. */ + State next_state = _state; + switch (_state) + { + case State::ConnectPhy: next_state = handle_ConnectPhy(); break; + case State::SyncTime: next_state = handle_SyncTime(); break; + case State::ConnectMqttBroker: next_state = handle_ConnectMqttBroker(); break; + case State::Connected: next_state = handle_Connected(); break; + case State::Disconnect: next_state = handle_Disconnect(); break; + } + _state = next_state; + + /* This watchdog feed is actually needed only by the RP2040 Connect because its + * maximum watchdog window is 8389 ms; despite this we feed it for all + * supported ARCH to keep code aligned. + */ +#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) + watchdog_reset(); +#endif +} + +int ArduinoIoTCloudTCP::connected() +{ + return _mqttClient.connected(); +} + +void ArduinoIoTCloudTCP::printDebugInfo() +{ + DEBUG_INFO("***** Arduino IoT Cloud - configuration info *****"); + DEBUG_INFO("Device ID: %s", getDeviceId().c_str()); + DEBUG_INFO("MQTT Broker: %s:%d", _brokerAddress.c_str(), _brokerPort); +} + +/****************************************************************************** + * PRIVATE MEMBER FUNCTIONS + ******************************************************************************/ + int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, uint16_t brokerPort) { _brokerAddress = brokerAddress; @@ -204,52 +250,6 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, return 1; } -void ArduinoIoTCloudTCP::update() -{ - /* Feed the watchdog. If any of the functions called below - * get stuck than we can at least reset and recover. - */ -#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) - watchdog_reset(); -#endif - - /* Run through the state machine. */ - State next_state = _state; - switch (_state) - { - case State::ConnectPhy: next_state = handle_ConnectPhy(); break; - case State::SyncTime: next_state = handle_SyncTime(); break; - case State::ConnectMqttBroker: next_state = handle_ConnectMqttBroker(); break; - case State::Connected: next_state = handle_Connected(); break; - case State::Disconnect: next_state = handle_Disconnect(); break; - } - _state = next_state; - - /* This watchdog feed is actually needed only by the RP2040 Connect because its - * maximum watchdog window is 8389 ms; despite this we feed it for all - * supported ARCH to keep code aligned. - */ -#if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) - watchdog_reset(); -#endif -} - -int ArduinoIoTCloudTCP::connected() -{ - return _mqttClient.connected(); -} - -void ArduinoIoTCloudTCP::printDebugInfo() -{ - DEBUG_INFO("***** Arduino IoT Cloud - configuration info *****"); - DEBUG_INFO("Device ID: %s", getDeviceId().c_str()); - DEBUG_INFO("MQTT Broker: %s:%d", _brokerAddress.c_str(), _brokerPort); -} - -/****************************************************************************** - * PRIVATE MEMBER FUNCTIONS - ******************************************************************************/ - ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy() { if (_connection->check() == NetworkConnectionState::CONNECTED) diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index 29dfc754..6dfd5f23 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -75,7 +75,6 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass virtual void printDebugInfo() override; int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); - int begin(bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); #ifdef BOARD_HAS_SECRET_KEY inline void setBoardId (String const device_id) { setDeviceId(device_id); } @@ -104,6 +103,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass #endif private: + int begin(bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); static const int MQTT_TRANSMIT_BUFFER_SIZE = 256; enum class State From d896eae7aae187d80cf0be810a8722a254003963 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 25 Jun 2024 13:46:51 +0200 Subject: [PATCH 03/11] rename _brokerClient to _brokerTLSClient --- src/ArduinoIoTCloudTCP.cpp | 6 +++--- src/ArduinoIoTCloudTCP.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 11bce04d..ee989f14 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -87,7 +87,7 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_ #endif /* Setup broker TLS client */ - _brokerClient.begin(connection); + _brokerTLSClient.begin(connection); #if OTA_ENABLED /* Setup OTA TLS client */ @@ -181,7 +181,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__); return 0; } - _brokerClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); + _brokerTLSClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); #if OTA_ENABLED _otaClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); #endif @@ -192,7 +192,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, } #endif - _mqttClient.setClient(_brokerClient); + _mqttClient.setClient(_brokerTLSClient); #ifdef BOARD_HAS_SECRET_KEY if(_password.length()) diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index 6dfd5f23..5eecd708 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -138,7 +138,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass #endif #endif - TLSClientMqtt _brokerClient; + TLSClientMqtt _brokerTLSClient; MqttClient _mqttClient; String _messageTopicOut; From 3637dd5a99f7111d610fa5b88035c722fe72e571 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 25 Jun 2024 13:48:12 +0200 Subject: [PATCH 04/11] rename _otaClient to _otaTLSClient --- src/ArduinoIoTCloudTCP.cpp | 6 +++--- src/ArduinoIoTCloudTCP.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index ee989f14..21912dee 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -91,7 +91,7 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_ #if OTA_ENABLED /* Setup OTA TLS client */ - _otaClient.begin(connection); + _otaTLSClient.begin(connection); #endif /* Setup TimeService */ @@ -183,7 +183,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, } _brokerTLSClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); #if OTA_ENABLED - _otaClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); + _otaTLSClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); #endif #endif #endif @@ -213,7 +213,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, _device.begin(); #if OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) - _ota.setClient(&_otaClient); + _ota.setClient(&_otaTLSClient); #endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) #if OTA_ENABLED && defined(OTA_BASIC_AUTH) diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index 5eecd708..3a53da92 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -148,7 +148,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass #if OTA_ENABLED - TLSClientOta _otaClient; + TLSClientOta _otaTLSClient; ArduinoCloudOTA _ota; onOTARequestCallbackFunc _get_ota_confirmation; #endif /* OTA_ENABLED */ From 0ee2e9c23632bcf5c104d06cd72f10d05199279f Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 25 Jun 2024 15:13:02 +0200 Subject: [PATCH 05/11] Rename TLSClientMqtt to TLSClientBroker --- src/ArduinoIoTCloudTCP.h | 4 ++-- .../{TLSClientMqtt.cpp => TLSClientBroker.cpp} | 4 ++-- .../utility/{TLSClientMqtt.h => TLSClientBroker.h} | 14 +++++++------- 3 files changed, 11 insertions(+), 11 deletions(-) rename src/tls/utility/{TLSClientMqtt.cpp => TLSClientBroker.cpp} (96%) rename src/tls/utility/{TLSClientMqtt.h => TLSClientBroker.h} (78%) diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index 3a53da92..af8f4795 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -36,7 +36,7 @@ #endif #endif -#include +#include #include #if OTA_ENABLED @@ -138,7 +138,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass #endif #endif - TLSClientMqtt _brokerTLSClient; + TLSClientBroker _brokerTLSClient; MqttClient _mqttClient; String _messageTopicOut; diff --git a/src/tls/utility/TLSClientMqtt.cpp b/src/tls/utility/TLSClientBroker.cpp similarity index 96% rename from src/tls/utility/TLSClientMqtt.cpp rename to src/tls/utility/TLSClientBroker.cpp index dafacd53..11231c94 100644 --- a/src/tls/utility/TLSClientMqtt.cpp +++ b/src/tls/utility/TLSClientBroker.cpp @@ -12,7 +12,7 @@ #ifdef HAS_TCP -#include "TLSClientMqtt.h" +#include "TLSClientBroker.h" #if defined(BOARD_HAS_SECRET_KEY) #include "tls/AIoTCUPCert.h" @@ -33,7 +33,7 @@ } #endif -void TLSClientMqtt::begin(ConnectionHandler & connection) { +void TLSClientBroker::begin(ConnectionHandler & connection) { #if defined(BOARD_HAS_OFFLOADED_ECCX08) /* Arduino Root CA is configured in nina-fw diff --git a/src/tls/utility/TLSClientMqtt.h b/src/tls/utility/TLSClientBroker.h similarity index 78% rename from src/tls/utility/TLSClientMqtt.h rename to src/tls/utility/TLSClientBroker.h index 837e76de..a8cadf01 100644 --- a/src/tls/utility/TLSClientMqtt.h +++ b/src/tls/utility/TLSClientBroker.h @@ -19,7 +19,7 @@ * Arduino NANO 33 IoT - WiFi */ #include "WiFiSSLClient.h" - class TLSClientMqtt : public WiFiBearSSLClient { + class TLSClientBroker : public WiFiBearSSLClient { #elif defined(BOARD_HAS_ECCX08) /* * Arduino MKR GSM 1400 @@ -29,38 +29,38 @@ * OPTA */ #include - class TLSClientMqtt : public BearSSLClient { + class TLSClientBroker : public BearSSLClient { #elif defined(ARDUINO_PORTENTA_C33) /* * Arduino Portenta C33 */ #include - class TLSClientMqtt : public SSLClient { + class TLSClientBroker : public SSLClient { #elif defined(ARDUINO_NICLA_VISION) /* * Arduino Nicla Vision */ #include - class TLSClientMqtt : public WiFiSSLSE050Client { + class TLSClientBroker : public WiFiSSLSE050Client { #elif defined(ARDUINO_EDGE_CONTROL) /* * Arduino Edge Control */ #include - class TLSClientMqtt : public GSMSSLClient { + class TLSClientBroker : public GSMSSLClient { #elif defined(ARDUINO_UNOR4_WIFI) /* * Arduino UNO R4 WiFi */ #include - class TLSClientMqtt : public WiFiSSLClient { + class TLSClientBroker : public WiFiSSLClient { #elif defined(BOARD_ESP) /* * ESP32* * ESP82* */ #include - class TLSClientMqtt : public WiFiClientSecure { + class TLSClientBroker : public WiFiClientSecure { #endif public: From 1dcba5813d0187d392412973c3393bb5b35da243 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 25 Jun 2024 15:18:33 +0200 Subject: [PATCH 06/11] Add begin method to configure Clients not using ConnectionHandler --- src/ArduinoIoTCloudTCP.cpp | 104 ++++++++++++++++++++----------------- src/ArduinoIoTCloudTCP.h | 5 +- 2 files changed, 59 insertions(+), 50 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 21912dee..3a280ffe 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -79,27 +79,70 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP() int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort) { _connection = &connection; - _brokerAddress = brokerAddress; -#ifdef BOARD_HAS_SECRET_KEY - _brokerPort = _password.length() ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort; -#else - _brokerPort = brokerPort; -#endif /* Setup broker TLS client */ - _brokerTLSClient.begin(connection); + TLSClientBroker *brokerTLSClient = new TLSClientBroker(); + brokerTLSClient->begin(connection); #if OTA_ENABLED /* Setup OTA TLS client */ - _otaTLSClient.begin(connection); + TLSClientOta *otaTLSClient = new TLSClientOta(); + otaTLSClient->begin(connection); +#endif + +#if defined(BOARD_HAS_SECRET_KEY) + /* If board is not configured for username and password login */ + if(!_password.length()) + { +#endif + +#if defined(BOARD_HAS_SECURE_ELEMENT) + if (!_selement.begin()) + { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not initialize secure element.", __FUNCTION__); + #if defined(ARDUINO_UNOWIFIR4) + if (String(WiFi.firmwareVersion()) < String("0.4.1")) { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to read device certificate, WiFi firmware needs to be >= 0.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion()); + } + #endif + return 0; + } + if (!SElementArduinoCloudDeviceId::read(_selement, getDeviceId(), SElementArduinoCloudSlot::DeviceId)) + { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device id.", __FUNCTION__); + return 0; + } + #if !defined(BOARD_HAS_OFFLOADED_ECCX08) + if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate)) + { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__); + return 0; + } + brokerTLSClient->setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); + #if OTA_ENABLED + otaTLSClient->setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); + #endif + #endif +#endif + +#if defined(BOARD_HAS_SECRET_KEY) + } #endif /* Setup TimeService */ _time_service.begin(_connection); + return begin(brokerTLSClient, otaTLSClient, enable_watchdog, brokerAddress, brokerPort); +} + +int ArduinoIoTCloudTCP::begin(Client * mqttClient, Client * otaClient, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort) +{ + _brokerTLSClient = mqttClient; + _otaTLSClient = otaClient; + /* Setup retry timers */ _connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms); - return begin(enable_watchdog, _brokerAddress, _brokerPort); + return begin(enable_watchdog, brokerAddress, brokerPort); } void ArduinoIoTCloudTCP::update() @@ -151,45 +194,10 @@ void ArduinoIoTCloudTCP::printDebugInfo() int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, uint16_t brokerPort) { _brokerAddress = brokerAddress; +#ifdef BOARD_HAS_SECRET_KEY + _brokerPort = _password.length() ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort; +#else _brokerPort = brokerPort; - -#if defined(BOARD_HAS_SECRET_KEY) - /* If board is not configured for username and password login */ - if(!_password.length()) - { -#endif - -#if defined(BOARD_HAS_SECURE_ELEMENT) - if (!_selement.begin()) - { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not initialize secure element.", __FUNCTION__); - #if defined(ARDUINO_UNOWIFIR4) - if (String(WiFi.firmwareVersion()) < String("0.4.1")) { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to read device certificate, WiFi firmware needs to be >= 0.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion()); - } - #endif - return 0; - } - if (!SElementArduinoCloudDeviceId::read(_selement, getDeviceId(), SElementArduinoCloudSlot::DeviceId)) - { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device id.", __FUNCTION__); - return 0; - } - #if !defined(BOARD_HAS_OFFLOADED_ECCX08) - if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate)) - { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__); - return 0; - } - _brokerTLSClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); - #if OTA_ENABLED - _otaTLSClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); - #endif - #endif -#endif - -#if defined(BOARD_HAS_SECRET_KEY) - } #endif _mqttClient.setClient(_brokerTLSClient); @@ -213,7 +221,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, _device.begin(); #if OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) - _ota.setClient(&_otaTLSClient); + _ota.setClient(_otaTLSClient); #endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) #if OTA_ENABLED && defined(OTA_BASIC_AUTH) diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index af8f4795..3f435d78 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -75,6 +75,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass virtual void printDebugInfo() override; int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); + int begin(Client * mqttClient, Client * otaClient, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); #ifdef BOARD_HAS_SECRET_KEY inline void setBoardId (String const device_id) { setDeviceId(device_id); } @@ -138,7 +139,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass #endif #endif - TLSClientBroker _brokerTLSClient; + Client * _brokerTLSClient; MqttClient _mqttClient; String _messageTopicOut; @@ -148,7 +149,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass #if OTA_ENABLED - TLSClientOta _otaTLSClient; + Client * _otaTLSClient; ArduinoCloudOTA _ota; onOTARequestCallbackFunc _get_ota_confirmation; #endif /* OTA_ENABLED */ From 1c384671e9a76499718de6bdf64fab4ca5418339 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 25 Jun 2024 16:12:28 +0200 Subject: [PATCH 07/11] Fix build if board do not support OTA --- src/ArduinoIoTCloudTCP.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 3a280ffe..477a5cd4 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -84,10 +84,12 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_ TLSClientBroker *brokerTLSClient = new TLSClientBroker(); brokerTLSClient->begin(connection); -#if OTA_ENABLED +#if OTA_ENABLED /* Setup OTA TLS client */ TLSClientOta *otaTLSClient = new TLSClientOta(); otaTLSClient->begin(connection); +#else + Client *otaTLSClient = nullptr; #endif #if defined(BOARD_HAS_SECRET_KEY) From 2f52ca5b872765a79e8a8e1bbe21cf3b89049af1 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 25 Jun 2024 16:13:16 +0200 Subject: [PATCH 08/11] Initialize ota client only if boards supports OTA --- src/ArduinoIoTCloudTCP.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 477a5cd4..7fb25953 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -140,7 +140,9 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_ int ArduinoIoTCloudTCP::begin(Client * mqttClient, Client * otaClient, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort) { _brokerTLSClient = mqttClient; +#if OTA_ENABLED _otaTLSClient = otaClient; +#endif /* Setup retry timers */ _connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms); From dfdb000d9561743d2ef0c299130799dcd2f8b3cc Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 25 Jun 2024 16:13:54 +0200 Subject: [PATCH 09/11] Initialize otaClient to nullptr as default --- src/ArduinoIoTCloudTCP.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index 3f435d78..61dc9942 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -75,7 +75,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass virtual void printDebugInfo() override; int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); - int begin(Client * mqttClient, Client * otaClient, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); + int begin(Client * mqttClient, Client * otaClient = nullptr, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); #ifdef BOARD_HAS_SECRET_KEY inline void setBoardId (String const device_id) { setDeviceId(device_id); } From 446720a7149936eeaf8db2981bbfcf4d7bda1ce6 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 25 Jun 2024 17:09:06 +0200 Subject: [PATCH 10/11] Add ctor defaults --- src/ArduinoIoTCloudTCP.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 7fb25953..788520d8 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -59,12 +59,14 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP() #ifdef BOARD_HAS_SECRET_KEY , _password("") #endif +, _brokerTLSClient(nullptr) , _mqttClient{nullptr} , _messageTopicOut("") , _messageTopicIn("") , _dataTopicOut("") , _dataTopicIn("") #if OTA_ENABLED +, _otaTLSClient(nullptr) , _ota(&_message_stream) , _get_ota_confirmation{nullptr} #endif /* OTA_ENABLED */ From 5fe8be0f1b766a7d94b85778878b16af4cde5944 Mon Sep 17 00:00:00 2001 From: pennam Date: Fri, 9 Aug 2024 18:06:20 +0200 Subject: [PATCH 11/11] Fix connection without ConnectionHandler --- src/ArduinoIoTCloudTCP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 788520d8..cc74a324 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -266,7 +266,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy() { - if (_connection->check() == NetworkConnectionState::CONNECTED) + if ((_connection == nullptr) || (_connection->check() == NetworkConnectionState::CONNECTED)) { if (!_connection_attempt.isRetry() || (_connection_attempt.isRetry() && _connection_attempt.isExpired())) return State::SyncTime;