diff --git a/examples/ArduinoIoTCloud-WiFiClient/ArduinoIoTCloud-WiFiClient.ino b/examples/ArduinoIoTCloud-WiFiClient/ArduinoIoTCloud-WiFiClient.ino new file mode 100644 index 000000000..4f7f72678 --- /dev/null +++ b/examples/ArduinoIoTCloud-WiFiClient/ArduinoIoTCloud-WiFiClient.ino @@ -0,0 +1,73 @@ +/* + This sketch demonstrates how to use Arduino IoT Cloud without ArduinoConnectionHandler + + * Connect a potentiometer (or other analog sensor) to A0. + * When the potentiometer (or sensor) value changes the data is sent to the Cloud. + * When you flip the switch in the Cloud dashboard the onboard LED lights gets turned ON or OFF. + + IMPORTANT: + This sketch works with only with WiFi. + + The full list of compatible boards can be found here: + - https://github.com/arduino-libraries/ArduinoIoTCloud#what +*/ + +#include +#include "thingProperties.h" + +#if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32) +static int const LED_BUILTIN = 2; +#endif + +WiFiClient brokerClient; +WiFiClient otaClient; +WiFiUDP ntpClient; + +void setup() { + /* Initialize serial and wait up to 5 seconds for port to open */ + Serial.begin(9600); + for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } + + int status = WL_IDLE_STATUS; + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(SECRET_WIFI_SSID); + status = WiFi.begin(SECRET_WIFI_SSID, SECRET_WIFI_PASS); + delay(1000); + } + + /* Set the debug message level: + * - DBG_ERROR: Only show error messages + * - DBG_WARNING: Show warning and error messages + * - DBG_INFO: Show info, warning, and error messages + * - DBG_DEBUG: Show debug, info, warning, and error messages + * - DBG_VERBOSE: Show all messages + */ + setDebugMessageLevel(DBG_INFO); + + /* Configure LED pin as an output */ + pinMode(LED_BUILTIN, OUTPUT); + + /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ + initProperties(); + + /* Initialize Arduino IoT Cloud library */ + ArduinoCloud.begin(brokerClient, otaClient, ntpClient); + + ArduinoCloud.printDebugInfo(); +} + +void loop() { + ArduinoCloud.update(); + potentiometer = analogRead(A0); + seconds = millis() / 1000; +} + +/* + * 'onLedChange' is called when the "led" property of your Thing changes + */ +void onLedChange() { + Serial.print("LED set to "); + Serial.println(led); + digitalWrite(LED_BUILTIN, led); +} diff --git a/examples/ArduinoIoTCloud-WiFiClient/arduino_secrets.h b/examples/ArduinoIoTCloud-WiFiClient/arduino_secrets.h new file mode 100644 index 000000000..cc61fea25 --- /dev/null +++ b/examples/ArduinoIoTCloud-WiFiClient/arduino_secrets.h @@ -0,0 +1,9 @@ +/* A complete list of supported boards with WiFi is available here: + * https://github.com/arduino-libraries/ArduinoIoTCloud/#what + */ + +#define SECRET_WIFI_SSID "YOUR_WIFI_NETWORK_NAME" +#define SECRET_WIFI_PASS "YOUR_WIFI_PASSWORD" + +/* Configure this if you want to use username/password broker authentication */ +#define SECRET_DEVICE_KEY "my-device-password" diff --git a/examples/ArduinoIoTCloud-WiFiClient/thingProperties.h b/examples/ArduinoIoTCloud-WiFiClient/thingProperties.h new file mode 100644 index 000000000..adedc157a --- /dev/null +++ b/examples/ArduinoIoTCloud-WiFiClient/thingProperties.h @@ -0,0 +1,28 @@ +#include +#include "arduino_secrets.h" + +#if !defined(HAS_TCP) + #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" +#endif + +#if !defined(BOARD_HAS_SECURE_ELEMENT) + #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" +#endif + +void onLedChange(); + +bool led; +int potentiometer; +int seconds; + +void initProperties() { + ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); + ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); + ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1); + +#if !defined(BOARD_HAS_SECURE_ELEMENT) + ArduinoCloud.setBoardId(BOARD_ID); + ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); +#endif +} + diff --git a/examples/utility/Provisioning_2.0/CSRHandler.cpp b/examples/utility/Provisioning_2.0/CSRHandler.cpp index 1a6101e4b..339d934e3 100644 --- a/examples/utility/Provisioning_2.0/CSRHandler.cpp +++ b/examples/utility/Provisioning_2.0/CSRHandler.cpp @@ -85,7 +85,7 @@ bool CSRHandlerClass::begin(ConnectionHandler &connectionHandler, SecureElement _fw_version = WiFi.firmwareVersion(); #endif if(!_tlsClient){ - _tlsClient = new TLSClientMqtt(); + _tlsClient = new TLSClientBroker(); } _tlsClient->begin(*_connectionHandler); _tlsClient->setTimeout(RESPONSE_TIMEOUT); diff --git a/examples/utility/Provisioning_2.0/CSRHandler.h b/examples/utility/Provisioning_2.0/CSRHandler.h index ae5956b7e..1f83e64d6 100644 --- a/examples/utility/Provisioning_2.0/CSRHandler.h +++ b/examples/utility/Provisioning_2.0/CSRHandler.h @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include "utility/LEDFeedback.h" #define JITTER_BASE 0 @@ -55,7 +55,7 @@ class CSRHandlerClass { ECP256Certificate *_certForCSR; ConnectionHandler *_connectionHandler; SecureElement *_secureElement; - TLSClientMqtt *_tlsClient; + TLSClientBroker *_tlsClient; HttpClient *_client; LEDFeedbackClass &_ledFeedback; void updateNextRequestAt(); diff --git a/extras/test/include/Arduino.h b/extras/test/include/Arduino.h index 9e743f96f..010f80a91 100644 --- a/extras/test/include/Arduino.h +++ b/extras/test/include/Arduino.h @@ -23,6 +23,9 @@ ******************************************************************************/ typedef std::string String; +typedef struct { + void * udp; +} UDP; /****************************************************************************** FUNCTION PROTOTYPES diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index a25b81080..e0966e588 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -58,6 +58,12 @@ * AUTOMATICALLY CONFIGURED DEFINES ******************************************************************************/ +#if defined(DEBUG_ERROR) || defined(DEBUG_WARNING) || defined(DEBUG_INFO) || defined(DEBUG_DEBUG) || defined(DEBUG_VERBOSE) + #define DEBUG_ENABLED (1) +#else + #define DEBUG_ENABLED (0) +#endif + #if !defined(HAS_NOTECARD) #if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) @@ -148,8 +154,10 @@ #define BOARD_STM32H7 #endif -#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA) \ - || defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_PORTENTA_C33) +#define CONNECTION_HANDLER_ENABLED (1) + +#if ((defined(BOARD_STM32H7) || defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_PORTENTA_C33)) &&\ + (defined(CONNECTION_HANDLER_ENABLED) && (CONNECTION_HANDLER_ENABLED == 1))) #define NETWORK_CONFIGURATOR_ENABLED (1) #else #define NETWORK_CONFIGURATOR_ENABLED (0) diff --git a/src/ArduinoIoTCloud.cpp b/src/ArduinoIoTCloud.cpp index 74443ff5b..e58201fb4 100644 --- a/src/ArduinoIoTCloud.cpp +++ b/src/ArduinoIoTCloud.cpp @@ -26,11 +26,13 @@ ******************************************************************************/ ArduinoIoTCloudClass::ArduinoIoTCloudClass() -: _connection{nullptr} +: _time_service(TimeService) +#if CONNECTION_HANDLER_ENABLED +,_connection{nullptr} +#endif #if NETWORK_CONFIGURATOR_ENABLED , _configurator{nullptr} #endif -, _time_service(TimeService) , _thing_id{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"} , _lib_version{AIOT_CONFIG_LIB_VERSION} , _device_id{"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"} diff --git a/src/ArduinoIoTCloud.h b/src/ArduinoIoTCloud.h index 19e94555d..484038b1f 100644 --- a/src/ArduinoIoTCloud.h +++ b/src/ArduinoIoTCloud.h @@ -24,12 +24,15 @@ #include -#include +#if CONNECTION_HANDLER_ENABLED + #include +#endif + #if NETWORK_CONFIGURATOR_ENABLED #include #endif -#if defined(DEBUG_ERROR) || defined(DEBUG_WARNING) || defined(DEBUG_INFO) || defined(DEBUG_DEBUG) || defined(DEBUG_VERBOSE) +#if DEBUG_ENABLED #include #endif @@ -99,14 +102,16 @@ class ArduinoIoTCloudClass inline void setDeviceId(String const device_id) { _device_id = device_id; }; inline String & getDeviceId() { return _device_id; }; +#if CONNECTION_HANDLER_ENABLED inline ConnectionHandler * getConnection() { return _connection; } +#endif inline unsigned long getInternalTime() { return _time_service.getTime(); } inline unsigned long getLocalTime() { return _time_service.getLocalTime(); } - #if NETWORK_CONFIGURATOR_ENABLED +#if NETWORK_CONFIGURATOR_ENABLED inline void setConfigurator(NetworkConfiguratorClass & configurator) { _configurator = &configurator; } - #endif +#endif void addCallback(ArduinoIoTCloudEvent const event, OnCloudEventCallback callback); #define addProperty( v, ...) addPropertyReal(v, #v, __VA_ARGS__) @@ -151,11 +156,13 @@ class ArduinoIoTCloudClass protected: + TimeServiceClass & _time_service; +#if CONNECTION_HANDLER_ENABLED ConnectionHandler * _connection; - #if NETWORK_CONFIGURATOR_ENABLED +#endif +#if NETWORK_CONFIGURATOR_ENABLED NetworkConfiguratorClass * _configurator; - #endif - TimeServiceClass & _time_service; +#endif String _thing_id; String _lib_version; diff --git a/src/ArduinoIoTCloudDevice.cpp b/src/ArduinoIoTCloudDevice.cpp index 58f00d202..a90026bf3 100644 --- a/src/ArduinoIoTCloudDevice.cpp +++ b/src/ArduinoIoTCloudDevice.cpp @@ -19,6 +19,10 @@ #include "ArduinoIoTCloudDevice.h" #include "interfaces/CloudProcess.h" +#if DEBUG_ENABLED + #include +#endif + /****************************************************************************** CTOR/DTOR ******************************************************************************/ diff --git a/src/ArduinoIoTCloudLPWAN.cpp b/src/ArduinoIoTCloudLPWAN.cpp index 2e2693460..a8da091ca 100644 --- a/src/ArduinoIoTCloudLPWAN.cpp +++ b/src/ArduinoIoTCloudLPWAN.cpp @@ -70,7 +70,7 @@ int ArduinoIoTCloudLPWAN::begin(ConnectionHandler& connection, bool retry) { _connection = &connection; _retryEnable = retry; - _time_service.begin(nullptr); + _time_service.begin(); return 1; } diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 31e972882..238c93e3c 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -80,122 +80,31 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP() * PUBLIC MEMBER FUNCTIONS ******************************************************************************/ -int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort, bool auto_reconnect) +#if CONNECTION_HANDLER_ENABLED +int ArduinoIoTCloudTCP::begin(ConnectionHandler& connection, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect) { _connection = &connection; - _brokerAddress = brokerAddress; - - _authMode = ArduinoIoTAuthenticationMode::CERTIFICATE; -#if defined (BOARD_HAS_SECRET_KEY) - /* If board supports and sketch is configured for username and password login */ - if(_password.length()) { - _authMode = ArduinoIoTAuthenticationMode::PASSWORD; - } -#endif - - /* If board is configured for certificate authentication and mTLS */ - if(_authMode == ArduinoIoTAuthenticationMode::CERTIFICATE) - { -#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 (!_writeCertOnConnect) { - /* No update pending read certificate stored in secure element */ - if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate)) - { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__); - return 0; - } - } - #if !defined(BOARD_HAS_OFFLOADED_ECCX08) - _brokerClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); - #if OTA_ENABLED - _otaClient.setEccSlot(static_cast(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length()); - #endif - #endif - _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_SECURE_AUTH : brokerPort; +#if OTA_ENABLED + return begin(_connection->getClient(), TLSClientOta::getNewClient(_connection->getInterface()), _connection->getUDP(), enableWatchdog, brokerAddress, brokerPort, autoReconnect); +#else + return begin(_connection->getClient(), _connection->getUDP(), enableWatchdog, brokerAddress, brokerPort, autoReconnect); #endif - } - else - { - _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort; - } - - /* 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, auto_reconnect); } - -int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, uint16_t brokerPort, bool auto_reconnect) -{ - _enable_watchdog = enable_watchdog; - _brokerAddress = brokerAddress; - _brokerPort = brokerPort; - _auto_reconnect = auto_reconnect; - - _state = State::ConfigPhy; - - _mqttClient.setClient(_brokerClient); - -#ifdef BOARD_HAS_SECRET_KEY - if(_password.length()) - { - _mqttClient.setUsernamePassword(getDeviceId(), _password); - } #endif - _mqttClient.onMessage(ArduinoIoTCloudTCP::onMessage); - _mqttClient.setKeepAliveInterval(30 * 1000); - _mqttClient.setConnectionTimeout(1500); - _mqttClient.setId(getDeviceId().c_str()); - - _messageTopicOut = getTopic_messageout(); - _messageTopicIn = getTopic_messagein(); - - _thing.begin(); - _device.begin(); - -#if OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) - _ota.setClient(&_otaClient); -#endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) - -#if OTA_ENABLED && defined(OTA_BASIC_AUTH) - _ota.setAuthentication(getDeviceId().c_str(), _password.c_str()); -#endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) && defined(OTA_BASIC_AUTH) - -#ifdef BOARD_HAS_OFFLOADED_ECCX08 - if (String(WiFi.firmwareVersion()) < String("1.6.0")) { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, NINA firmware needs to be >= 1.6.0, current %s", __FUNCTION__, WiFi.firmwareVersion()); - return 0; - } -#endif /* BOARD_HAS_OFFLOADED_ECCX08 */ - -#if defined (ARDUINO_UNOWIFIR4) - if (String(WiFi.firmwareVersion()) < String("0.2.0")) { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, WiFi firmware needs to be >= 0.2.0, current %s", __FUNCTION__, WiFi.firmwareVersion()); - } +int ArduinoIoTCloudTCP::begin(Client& brokerClient, Client& otaClient, UDP& ntpClient, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect) +{ +#if OTA_ENABLED + _otaClient = &otaClient; #endif + return begin(brokerClient, ntpClient, enableWatchdog, brokerAddress, _brokerPort, autoReconnect); +} -#if NETWORK_CONFIGURATOR_ENABLED - if(_configurator != nullptr){ - _configurator->enableAgent(ConfiguratorAgent::AgentTypes::BLE,false); - _configurator->begin(); - } -#endif - return 1; +int ArduinoIoTCloudTCP::begin(Client& brokerClient, UDP& ntpClient, bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect) +{ + _brokerClient = &brokerClient; + _ntpClient = &ntpClient; + return begin(enableWatchdog, brokerAddress, brokerPort, autoReconnect); } void ArduinoIoTCloudTCP::update() @@ -234,18 +143,21 @@ void ArduinoIoTCloudTCP::update() /* Poll the network configurator to check if it is updating the configuration. * The polling must be performed only if the the first configuration is completed. */ - #if NETWORK_CONFIGURATOR_ENABLED +#if NETWORK_CONFIGURATOR_ENABLED if(_configurator != nullptr && _state > State::Init && _configurator->update() == NetworkConfiguratorStates::UPDATING_CONFIG){ _state = State::ConfigPhy; } - #endif +#endif #if OTA_ENABLED /* OTA FSM needs to reach the Idle state before being able to run independently from * the mqttClient. The state can be reached only after the mqttClient is connected to * the broker. + * + * We also have to check that the OTA client is not null. It can happen if we don't + * use the ArduinoConnectionHandler library and the user doesn't provide it. */ - if(_state <= State::ConnectPhy){ + if ((_state <= State::ConnectPhy) || (_otaClient == nullptr)) { return; } @@ -287,7 +199,7 @@ void ArduinoIoTCloudTCP::disconnect() { } _mqttClient.stop(); - _auto_reconnect = false; + _autoReconnect = false; _state = State::Disconnect; } @@ -295,6 +207,117 @@ void ArduinoIoTCloudTCP::disconnect() { * PRIVATE MEMBER FUNCTIONS ******************************************************************************/ +int ArduinoIoTCloudTCP::begin(bool const enableWatchdog, String brokerAddress, uint16_t brokerPort, bool autoReconnect) +{ + _authMode = ArduinoIoTAuthenticationMode::CERTIFICATE; +#if defined (BOARD_HAS_SECRET_KEY) + /* If board supports and sketch is configured for username and password login */ + if(_password.length()) { + _authMode = ArduinoIoTAuthenticationMode::PASSWORD; + } +#endif + + /* If board is configured for certificate authentication and mTLS */ + if(_authMode == ArduinoIoTAuthenticationMode::CERTIFICATE) + { +#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 (!_writeCertOnConnect) { + /* No update pending read certificate stored in secure element */ + if (!SElementArduinoCloudCertificate::read(_selement, _cert, SElementArduinoCloudSlot::CompressedCertificate)) + { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not read device certificate.", __FUNCTION__); + return 0; + } + } + #if !defined(BOARD_HAS_OFFLOADED_ECCX08) + _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 + _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_SECURE_AUTH : brokerPort; +#endif + } + else + { + _brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort; + } + + /* Setup retry timers */ + _connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms); + + _enableWatchdog = enableWatchdog; + _brokerAddress = brokerAddress; + _autoReconnect = autoReconnect; + _state = State::ConfigPhy; + + _mqttClient.setClient(_brokerTLSClient); + +#ifdef BOARD_HAS_SECRET_KEY + if(_password.length()) + { + _mqttClient.setUsernamePassword(getDeviceId(), _password); + } +#endif + + _mqttClient.onMessage(ArduinoIoTCloudTCP::onMessage); + _mqttClient.setKeepAliveInterval(30 * 1000); + _mqttClient.setConnectionTimeout(1500); + _mqttClient.setId(getDeviceId().c_str()); + + _messageTopicOut = getTopic_messageout(); + _messageTopicIn = getTopic_messagein(); + + _thing.begin(); + _device.begin(); + +#if OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) + _ota.setClient(&_otaTLSClient); +#endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) + +#if OTA_ENABLED && defined(OTA_BASIC_AUTH) + _ota.setAuthentication(getDeviceId().c_str(), _password.c_str()); +#endif // OTA_ENABLED && !defined(OFFLOADED_DOWNLOAD) && defined(OTA_BASIC_AUTH) + + +#if defined(BOARD_HAS_OFFLOADED_ECCX08) && defined(CONNECTION_HANDLER_ENABLED) && (CONNECTION_HANDLER_ENABLED == 1) + if (String(WiFi.firmwareVersion()) < String("1.6.0")) { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, NINA firmware needs to be >= 1.6.0, current %s", __FUNCTION__, WiFi.firmwareVersion()); + return 0; + } +#endif /* BOARD_HAS_OFFLOADED_ECCX08 */ + +#if defined(ARDUINO_UNOWIFIR4) && defined(CONNECTION_HANDLER_ENABLED) && (CONNECTION_HANDLER_ENABLED == 1) + if (String(WiFi.firmwareVersion()) < String("0.2.0")) { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, WiFi firmware needs to be >= 0.2.0, current %s", __FUNCTION__, WiFi.firmwareVersion()); + return 0; + } +#endif + +#if NETWORK_CONFIGURATOR_ENABLED + if(_configurator != nullptr){ + _configurator->enableAgent(ConfiguratorAgent::AgentTypes::BLE,false); + _configurator->begin(); + } +#endif + return 1; +} + ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConfigPhy() { #if NETWORK_CONFIGURATOR_ENABLED @@ -302,10 +325,10 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConfigPhy() return State::Init; } - if(_configurator->update() == NetworkConfiguratorStates::CONFIGURED) { - _configurator->disconnectAgent(); - return State::Init; - } + if (_configurator->update() == NetworkConfiguratorStates::CONFIGURED) { + _configurator->disconnectAgent(); + return State::Init; + } return State::ConfigPhy; #else return State::Init; @@ -315,16 +338,20 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConfigPhy() ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init() { /* Setup broker TLS client */ - /* Setup broker TLS client */ - _brokerClient.begin(*_connection, _authMode); + _brokerTLSClient.begin(_brokerClient, _authMode); #if OTA_ENABLED /* Setup OTA TLS client */ - _otaClient.begin(*_connection); + _otaTLSClient.begin(_otaClient); #endif +#if CONNECTION_HANDLER_ENABLED /* Setup TimeService */ - _time_service.begin(_connection); + if (_connection != nullptr) { + _time_service.begin(_connection); + } else +#endif + _time_service.begin(_ntpClient); /* Since we do not control what code the user inserts * between ArduinoIoTCloudTCP::begin() and the first @@ -332,11 +359,15 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init() * set a rather large timeout at first. */ #if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) - if (_enable_watchdog) { + if (_enableWatchdog) { /* Initialize watchdog hardware */ watchdog_enable(); - /* Setup callbacks to feed the watchdog during offloaded network operations (connection/download)*/ - watchdog_enable_network_feed(_connection->getInterface()); +#if CONNECTION_HANDLER_ENABLED + if (_connection != nullptr) { + /* Setup callbacks to feed the watchdog during offloaded network operations (connection/download)*/ + watchdog_enable_network_feed(_connection->getInterface()); + } +#endif } #endif @@ -345,13 +376,20 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Init() ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy() { - if (_connection->check() == NetworkConnectionState::CONNECTED) - { - if (!_connection_attempt.isRetry() || (_connection_attempt.isRetry() && _connection_attempt.isExpired())) - return State::SyncTime; +#if CONNECTION_HANDLER_ENABLED + if (_connection == nullptr) { + return State::SyncTime; } + if (_connection->check() == NetworkConnectionState::CONNECTED) { + if (!_connection_attempt.isRetry() || (_connection_attempt.isRetry() && _connection_attempt.isExpired())) { + return State::SyncTime; + } + } return State::ConnectPhy; +#else + return State::SyncTime; +#endif } ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime() @@ -393,7 +431,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker() _connection_attempt.retry(); #if defined(BOARD_HAS_ECCX08) && !defined(BOARD_HAS_OFFLOADED_ECCX08) - DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not connect to %s:%d Mqtt error: %d TLS error: %d", __FUNCTION__, _brokerAddress.c_str(), _brokerPort, _mqttClient.connectError(), _brokerClient.errorCode()); + DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not connect to %s:%d Mqtt error: %d TLS error: %d", __FUNCTION__, _brokerAddress.c_str(), _brokerPort, _mqttClient.connectError(), _brokerTLSClient.errorCode()); #else DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not connect to %s:%d Error: %d", __FUNCTION__, _brokerAddress.c_str(), _brokerPort, _mqttClient.connectError()); #endif @@ -448,7 +486,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Disconnect() DEBUG_INFO("Disconnected from Arduino IoT Cloud"); execCloudEventCallback(ArduinoIoTCloudEvent::DISCONNECT); - if(_auto_reconnect) { + if(_autoReconnect) { /* Setup timer for broker connection and restart */ _connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms); return State::ConnectPhy; diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index d59c6de97..0de85db04 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -34,7 +34,7 @@ #include #endif -#include +#include #include #if OTA_ENABLED @@ -74,8 +74,11 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass virtual void printDebugInfo() override; virtual void disconnect () override; - int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool auto_reconnect = true); - int begin(bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool auto_reconnect = true); +#if CONNECTION_HANDLER_ENABLED + int begin(ConnectionHandler& connection, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true); +#endif + int begin(Client& brokerClient, Client& otaClient, UDP& ntpClient, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true); + int begin(Client& brokerClient, UDP& ntpClient, bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true); #if defined(BOARD_HAS_SECURE_ELEMENT) int updateCertificate(String authorityKeyIdentifier, String serialNumber, String notBefore, String notAfter, String signature); @@ -117,6 +120,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass #endif private: + + int begin(bool const enableWatchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS, uint16_t brokerPort = DEFAULT_BROKER_PORT_AUTO, bool autoReconnect = true); + static const int MQTT_TRANSMIT_BUFFER_SIZE = 256; enum class State @@ -143,8 +149,8 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass uint8_t _mqtt_data_buf[MQTT_TRANSMIT_BUFFER_SIZE]; int _mqtt_data_len; bool _mqtt_data_request_retransmit; - bool _enable_watchdog; - bool _auto_reconnect; + bool _enableWatchdog; + bool _autoReconnect; #if defined(BOARD_HAS_SECRET_KEY) String _password; @@ -157,8 +163,11 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass bool _writeCertOnConnect; #endif - TLSClientMqtt _brokerClient; + /* Base client from sketch or ConnectionHandler */ + Client * _brokerClient; + TLSClientBroker _brokerTLSClient; MqttClient _mqttClient; + UDP * _ntpClient; String _messageTopicOut; String _messageTopicIn; @@ -166,7 +175,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass String _dataTopicIn; #if OTA_ENABLED - TLSClientOta _otaClient; + /* Base client from sketch or ConnectionHandler */ + Client * _otaClient; + TLSClientOta _otaTLSClient; ArduinoCloudOTA _ota; onOTARequestCallbackFunc _get_ota_confirmation; #endif /* OTA_ENABLED */ diff --git a/src/ArduinoIoTCloudThing.cpp b/src/ArduinoIoTCloudThing.cpp index a6bb74190..164527e97 100644 --- a/src/ArduinoIoTCloudThing.cpp +++ b/src/ArduinoIoTCloudThing.cpp @@ -22,6 +22,10 @@ #include "property/types/CloudWrapperInt.h" #include "property/types/CloudWrapperUnsignedInt.h" +#if DEBUG_ENABLED + #include +#endif + /****************************************************************************** * CTOR/DTOR ******************************************************************************/ diff --git a/src/ota/implementation/OTANanoRP2040.cpp b/src/ota/implementation/OTANanoRP2040.cpp index c24ebf9ed..63eaa3aed 100644 --- a/src/ota/implementation/OTANanoRP2040.cpp +++ b/src/ota/implementation/OTANanoRP2040.cpp @@ -13,7 +13,9 @@ #if defined(ARDUINO_NANO_RP2040_CONNECT) && OTA_ENABLED #include #include "OTANanoRP2040.h" -#include +#if DEBUG_ENABLED + #include +#endif #include "mbed.h" #include "utility/watchdog/Watchdog.h" diff --git a/src/ota/implementation/OTASamd.cpp b/src/ota/implementation/OTASamd.cpp index bc9194491..1369dbecb 100644 --- a/src/ota/implementation/OTASamd.cpp +++ b/src/ota/implementation/OTASamd.cpp @@ -13,7 +13,9 @@ #if defined(ARDUINO_ARCH_SAMD) && OTA_ENABLED #include "OTASamd.h" -#include +#if DEBUG_ENABLED + #include +#endif #if OTA_STORAGE_SNU # include # include /* WiFiStorage */ diff --git a/src/ota/implementation/OTAUnoR4.cpp b/src/ota/implementation/OTAUnoR4.cpp index 5ba12afa9..8fbba3046 100644 --- a/src/ota/implementation/OTAUnoR4.cpp +++ b/src/ota/implementation/OTAUnoR4.cpp @@ -13,7 +13,9 @@ #if defined(ARDUINO_UNOR4_WIFI) && OTA_ENABLED #include "OTAUnoR4.h" -#include +#if DEBUG_ENABLED + #include +#endif #include "fsp_common_api.h" #include "r_flash_lp.h" #include "WiFi.h" diff --git a/src/ota/interface/OTAInterface.h b/src/ota/interface/OTAInterface.h index d3d21f0f1..cf371fec9 100644 --- a/src/ota/interface/OTAInterface.h +++ b/src/ota/interface/OTAInterface.h @@ -21,7 +21,9 @@ #include #include -#include +#if DEBUG_ENABLED + #include +#endif /****************************************************************************** * CLASS DECLARATION diff --git a/src/tls/utility/TLSClientMqtt.cpp b/src/tls/utility/TLSClientBroker.cpp similarity index 85% rename from src/tls/utility/TLSClientMqtt.cpp rename to src/tls/utility/TLSClientBroker.cpp index d775f868d..80638e60d 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" @@ -31,22 +31,28 @@ #endif -void TLSClientMqtt::begin(ConnectionHandler & connection, ArduinoIoTAuthenticationMode authMode) { +void TLSClientBroker::begin(Client* client, ArduinoIoTAuthenticationMode authMode) { + +/* Client* is coming from a reference in ArduinoIoTCloud::begin( .. ) + * The Client must be instantiated in the user sketch, for example: + * WiFiClientSecure client; + */ #if defined(BOARD_HAS_OFFLOADED_ECCX08) /* Arduino Root CA is configured in nina-fw * https://github.com/arduino/nina-fw/blob/master/arduino/libraries/ArduinoBearSSL/src/BearSSLTrustAnchors.h */ (void)authMode; + (void)client; #elif defined(BOARD_HAS_ECCX08) (void)authMode; - setClient(connection.getClient()); + setClient(*client); setProfile(aiotc_client_profile_init); setTrustAnchors(ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM); ArduinoBearSSL.onGetTime(getTime); #elif defined(ARDUINO_PORTENTA_C33) (void)authMode; - setClient(connection.getClient()); + setClient(*client); setCACert(AIoTSSCert); #elif defined(ARDUINO_NICLA_VISION) (void)authMode; @@ -61,7 +67,7 @@ void TLSClientMqtt::begin(ConnectionHandler & connection, ArduinoIoTAuthenticati * also present in older firmware revisions * https://github.com/arduino/uno-r4-wifi-usb-bridge/blob/f09ca94fdcab845b8368d4435fdac9f6999d21d2/certificates/certificates.pem#L852 */ - (void)connection; + (void)client; /* Temporary force CACert to add new CA without rebuilding firmware */ if (authMode == ArduinoIoTAuthenticationMode::CERTIFICATE) { setCACert(AIoTSSCert); diff --git a/src/tls/utility/TLSClientMqtt.h b/src/tls/utility/TLSClientBroker.h similarity index 71% rename from src/tls/utility/TLSClientMqtt.h rename to src/tls/utility/TLSClientBroker.h index 689eff2f8..3bfd1261c 100644 --- a/src/tls/utility/TLSClientMqtt.h +++ b/src/tls/utility/TLSClientBroker.h @@ -10,8 +10,10 @@ #pragma once -#include #include +#if CONNECTION_HANDLER_ENABLED + #include +#endif enum class ArduinoIoTAuthenticationMode { @@ -25,7 +27,7 @@ enum class ArduinoIoTAuthenticationMode * 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 @@ -37,31 +39,31 @@ enum class ArduinoIoTAuthenticationMode */ #include #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) || defined(ARDUINO_RASPBERRY_PI_PICO_W) /* * ESP32* @@ -69,10 +71,10 @@ enum class ArduinoIoTAuthenticationMode * PICOW */ #include - class TLSClientMqtt : public WiFiClientSecure { + class TLSClientBroker : public WiFiClientSecure { #endif public: - void begin(ConnectionHandler & connection, ArduinoIoTAuthenticationMode authMode = ArduinoIoTAuthenticationMode::CERTIFICATE); + void begin(Client* client, ArduinoIoTAuthenticationMode authMode = ArduinoIoTAuthenticationMode::CERTIFICATE); }; diff --git a/src/tls/utility/TLSClientOta.cpp b/src/tls/utility/TLSClientOta.cpp index 8d90e64b6..02e017cb9 100644 --- a/src/tls/utility/TLSClientOta.cpp +++ b/src/tls/utility/TLSClientOta.cpp @@ -30,18 +30,25 @@ } #endif -void TLSClientOta::begin(ConnectionHandler &connection) { +void TLSClientOta::begin(Client* client) { + +/* Client* is coming from a reference in ArduinoIoTCloud::begin( .. ) + * The Client must be instantiated in the user sketch, for example: + * WiFiClientSecure client; + */ + #if defined(BOARD_HAS_OFFLOADED_ECCX08) /* AWS Root CAs are configured in nina-fw * https://github.com/arduino/nina-fw/blob/master/data/roots.pem */ + (void)client; #elif defined(BOARD_HAS_ECCX08) - setClient(*getNewClient(connection.getInterface())); + setClient(*client); setProfile(aiotc_client_profile_init); setTrustAnchors(ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM); ArduinoBearSSL.onGetTime(getTime); #elif defined(ARDUINO_PORTENTA_C33) - setClient(*getNewClient(connection.getInterface())); + setClient(*client); setCACert(AIoTSSCert); #elif defined(ARDUINO_NICLA_VISION) appendCustomCACert(AIoTSSCert); @@ -51,7 +58,7 @@ void TLSClientOta::begin(ConnectionHandler &connection) { /* AWS Root CAs are configured in uno-r4-wifi-usb-bridge/libraries/Arduino_ESP32_OTA * https://github.com/arduino-libraries/Arduino_ESP32_OTA/blob/fc755e7d1d3946232107e2590662ee08d6ccdec4/src/tls/amazon_root_ca.h */ - (void)connection; + (void)client; #elif defined(ARDUINO_RASPBERRY_PI_PICO_W) setCACert(AIoTUPCert); #elif defined(ARDUINO_ARCH_ESP32) diff --git a/src/tls/utility/TLSClientOta.h b/src/tls/utility/TLSClientOta.h index 6859a96e6..5d2c8599a 100644 --- a/src/tls/utility/TLSClientOta.h +++ b/src/tls/utility/TLSClientOta.h @@ -10,8 +10,11 @@ #pragma once -#include #include +#if CONNECTION_HANDLER_ENABLED + #include +#endif + #if defined(BOARD_HAS_OFFLOADED_ECCX08) /* @@ -67,37 +70,40 @@ #endif public: - void begin(ConnectionHandler & connection); + void begin(Client* client); -private: - inline Client* getNewClient(NetworkAdapter net) { +#if CONNECTION_HANDLER_ENABLED +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wswitch" +#pragma GCC diagnostic ignored "-Wreturn-type" + static inline Client& getNewClient(NetworkAdapter net) { switch(net) { #ifdef BOARD_HAS_WIFI case NetworkAdapter::WIFI: - return new WiFiClient(); + return * new WiFiClient(); #endif // BOARD_HAS_WIFI #ifdef BOARD_HAS_ETHERNET case NetworkAdapter::ETHERNET: - return new EthernetClient(); + return * new EthernetClient(); #endif // BOARD_HAS_ETHERNET #ifdef BOARD_HAS_NB case NetworkAdapter::NB: - return new NBClient(); + return * new NBClient(); #endif // BOARD_HAS_NB #ifdef BOARD_HAS_GSM case NetworkAdapter::GSM: - return new GSMClient(); + return * new GSMClient(); #endif // BOARD_HAS_GSM #ifdef BOARD_HAS_CATM1_NBIOT case NetworkAdapter::CATM1: - return new GSMClient(); + return * new GSMClient(); #endif // BOARD_HAS_CATM1_NBIOT #ifdef BOARD_HAS_CELLULAR case NetworkAdapter::CELL: - return new TinyGsmClient(modem, 1); + return * new TinyGsmClient(modem, 1); #endif // BOARD_HAS_CELLULAR - default: - return nullptr; } } +#pragma GCC diagnostic pop +#endif // CONNECTION_HANDLER_ENABLED }; diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index fb7272fc4..1ff604376 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -37,6 +37,10 @@ #include "RTC.h" #endif +#if DEBUG_ENABLED + #include +#endif + /************************************************************************************** * GLOBAL VARIABLES **************************************************************************************/ @@ -116,14 +120,17 @@ static time_t const EPOCH = 0; **************************************************************************************/ TimeServiceClass::TimeServiceClass() -: _con_hdl(nullptr) -, _is_rtc_configured(false) +: _is_rtc_configured(false) , _is_tz_configured(false) , _timezone_offset(24 * 60 * 60) , _timezone_dst_until(0) , _last_sync_tick(0) , _sync_interval_ms(TIMESERVICE_NTP_SYNC_TIMEOUT_ms) , _sync_func(nullptr) +#if CONNECTION_HANDLER_ENABLED +, _con_hdl(nullptr) +#endif +, _ntp_client(nullptr) { } @@ -132,9 +139,26 @@ TimeServiceClass::TimeServiceClass() * PUBLIC MEMBER FUNCTIONS **************************************************************************************/ +#if CONNECTION_HANDLER_ENABLED void TimeServiceClass::begin(ConnectionHandler * con_hdl) { _con_hdl = con_hdl; +#ifdef HAS_TCP + begin(&_con_hdl->getUDP()); +#else + begin(); +#endif +} +#endif + +void TimeServiceClass::begin(UDP * ntp_client) +{ + _ntp_client = ntp_client; + begin(); +} + +void TimeServiceClass::begin() +{ initRTC(); #ifdef HAS_LORA setRTC(EPOCH_AT_COMPILE_TIME); @@ -293,11 +317,12 @@ unsigned long TimeServiceClass::getTimeFromString(const String& input) #if defined(HAS_NOTECARD) || defined(HAS_TCP) bool TimeServiceClass::connected() { - if(_con_hdl == nullptr) { - return false; - } else { +#if CONNECTION_HANDLER_ENABLED + if(_con_hdl != nullptr) { return _con_hdl->check() == NetworkConnectionState::CONNECTED; } +#endif + return true; // If no connection handler is used, assume we are connected } unsigned long TimeServiceClass::getRemoteTime() @@ -308,8 +333,14 @@ unsigned long TimeServiceClass::getRemoteTime() * This is the most reliable time source and it will * ensure a correct behaviour of the library. */ - if(_con_hdl->getInterface() != NetworkAdapter::CELL) { - unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP()); + bool use_ntp = true; +#if CONNECTION_HANDLER_ENABLED + if((_con_hdl != nullptr) && (_con_hdl->getInterface() != NetworkAdapter::CELL)) { + use_ntp = false; + } +#endif + if (use_ntp && (_ntp_client != nullptr)) { + unsigned long const ntp_time = NTPUtils::getTime(*_ntp_client); if(isTimeValid(ntp_time)) { return ntp_time; } @@ -317,16 +348,19 @@ unsigned long TimeServiceClass::getRemoteTime() DEBUG_WARNING("TimeServiceClass::%s cannot get time from NTP, fallback on connection handler", __FUNCTION__); #endif /* HAS_TCP */ +#if CONNECTION_HANDLER_ENABLED /* As fallback if NTP request fails try to obtain the * network time using the connection handler. */ - unsigned long const connection_time = _con_hdl->getTime(); - if(isTimeValid(connection_time)) { - return connection_time; + if (_con_hdl != nullptr) { + unsigned long const connection_time = _con_hdl->getTime(); + if(isTimeValid(connection_time)) { + return connection_time; + } + DEBUG_WARNING("TimeServiceClass::%s cannot get time from connection handler", __FUNCTION__); } - DEBUG_WARNING("TimeServiceClass::%s cannot get time from connection handler", __FUNCTION__); +#endif } - /* Return known invalid value because we are not connected */ return EPOCH; } diff --git a/src/utility/time/TimeService.h b/src/utility/time/TimeService.h index 71656d948..29efe6868 100644 --- a/src/utility/time/TimeService.h +++ b/src/utility/time/TimeService.h @@ -23,7 +23,9 @@ **************************************************************************************/ #include -#include +#if CONNECTION_HANDLER_ENABLED + #include +#endif /****************************************************************************** * TYPEDEF @@ -42,7 +44,11 @@ class TimeServiceClass TimeServiceClass(); - void begin (ConnectionHandler * con_hdl); +#if CONNECTION_HANDLER_ENABLED + void begin(ConnectionHandler * con_hdl); +#endif + void begin(UDP * ntp_client); + void begin(); unsigned long getTime(); void setTime(unsigned long time); unsigned long getLocalTime(); @@ -60,7 +66,6 @@ class TimeServiceClass private: - ConnectionHandler * _con_hdl; bool _is_rtc_configured; bool _is_tz_configured; long _timezone_offset; @@ -68,6 +73,10 @@ class TimeServiceClass unsigned long _last_sync_tick; unsigned long _sync_interval_ms; syncTimeFunctionPtr _sync_func; +#if CONNECTION_HANDLER_ENABLED + ConnectionHandler * _con_hdl; +#endif + UDP * _ntp_client; #if defined(HAS_NOTECARD) || defined(HAS_TCP) unsigned long getRemoteTime(); diff --git a/src/utility/watchdog/Watchdog.cpp b/src/utility/watchdog/Watchdog.cpp index d47234064..0467b1684 100644 --- a/src/utility/watchdog/Watchdog.cpp +++ b/src/utility/watchdog/Watchdog.cpp @@ -23,8 +23,8 @@ #include -#if defined(DEBUG_ERROR) || defined(DEBUG_WARNING) || defined(DEBUG_INFO) || defined(DEBUG_DEBUG) || defined(DEBUG_VERBOSE) -# include +#if DEBUG_ENABLED + #include #endif #ifdef ARDUINO_ARCH_SAMD diff --git a/src/utility/watchdog/Watchdog.h b/src/utility/watchdog/Watchdog.h index 2ba43a875..aa4efd951 100644 --- a/src/utility/watchdog/Watchdog.h +++ b/src/utility/watchdog/Watchdog.h @@ -22,7 +22,10 @@ * INCLUDE ******************************************************************************/ -#include +#include +#if CONNECTION_HANDLER_ENABLED + #include +#endif /****************************************************************************** * FUNCTION DECLARATION @@ -31,7 +34,9 @@ #if defined (ARDUINO_ARCH_SAMD) || defined (ARDUINO_ARCH_MBED) void watchdog_enable(); void watchdog_reset(); +#if CONNECTION_HANDLER_ENABLED void watchdog_enable_network_feed(NetworkAdapter ni); +#endif #endif /* (ARDUINO_ARCH_SAMD) || (ARDUINO_ARCH_MBED) */ #endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */