diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index 7e1595839..3ed3c6703 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -105,6 +105,7 @@ jobs: - name: ArduinoECCX08 - name: RTCZero - name: MKRWAN + - source-url: https://github.com/adafruit/Adafruit_SleepyDog.git sketch-paths: # GSM boards - board: diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index 76522c7bb..969cbe18a 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -113,14 +113,14 @@ #endif #if defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKR1000) || \ - defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || \ defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_PORTENTA_H7_M7) || \ defined(ARDUINO_PORTENTA_H7_M4) || defined (ARDUINO_NANO_RP2040_CONNECT) #define BOARD_HAS_ECCX08 #define HAS_TCP #endif -#if defined(ARDUINO_AVR_UNO_WIFI_REV2) +#if defined(ARDUINO_AVR_UNO_WIFI_REV2) || \ + defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) #define BOARD_HAS_OFFLOADED_ECCX08 #define HAS_TCP #endif diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index d3a5db3fd..1ec314ec4 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -104,16 +104,16 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP() * PUBLIC MEMBER FUNCTIONS ******************************************************************************/ -int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, String brokerAddress, uint16_t brokerPort) +int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort) { _connection = &connection; _brokerAddress = brokerAddress; _brokerPort = brokerPort; _time_service.begin(&connection); - return begin(_brokerAddress, _brokerPort); + return begin(enable_watchdog, _brokerAddress, _brokerPort); } -int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) +int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, uint16_t brokerPort) { _brokerAddress = brokerAddress; _brokerPort = brokerPort; @@ -189,7 +189,6 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) DEBUG_ERROR("CryptoUtil::readDeviceId(...) failed."); return 0; } - ECCX08.end(); #endif #ifdef BOARD_HAS_ECCX08 @@ -254,23 +253,31 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) #endif #if OTA_STORAGE_SNU && OTA_ENABLED - String const nina_fw_version = WiFi.firmwareVersion(); - if (nina_fw_version < "1.4.1") { + if (String(WiFi.firmwareVersion()) < String("1.4.1")) { _ota_cap = false; - DEBUG_WARNING("ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s", __FUNCTION__, nina_fw_version.c_str()); + DEBUG_WARNING("ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s", __FUNCTION__, WiFi.firmwareVersion()); } else { _ota_cap = true; } #endif /* OTA_STORAGE_SNU */ +#ifdef BOARD_HAS_OFFLOADED_ECCX08 + if (String(WiFi.firmwareVersion()) < String("1.4.3")) { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, NINA firmware needs to be >= 1.4.3, current %s", __FUNCTION__, WiFi.firmwareVersion()); + return 0; + } +#endif /* BOARD_HAS_OFFLOADED_ECCX08 */ + #ifdef ARDUINO_ARCH_SAMD /* Since we do not control what code the user inserts * between ArduinoIoTCloudTCP::begin() and the first * call to ArduinoIoTCloudTCP::update() it is wise to * set a rather large timeout at first. */ - Watchdog.enable(SAMD_WATCHDOG_MAX_TIME_ms); + if (enable_watchdog) { + samd_watchdog_enable(); + } #endif /* ARDUINO_ARCH_SAMD */ return 1; @@ -282,7 +289,7 @@ void ArduinoIoTCloudTCP::update() /* Feed the watchdog. If any of the functions called below * get stuck than we can at least reset and recover. */ - Watchdog.reset(); + samd_watchdog_reset(); #endif /* ARDUINO_ARCH_SAMD */ /* Run through the state machine. */ @@ -541,7 +548,7 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l void ArduinoIoTCloudTCP::onOTARequest() { #ifdef ARDUINO_ARCH_SAMD - Watchdog.reset(); + samd_watchdog_reset(); #endif /* ARDUINO_ARCH_SAMD */ DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s _ota_url = %s", __FUNCTION__, _ota_url.c_str()); @@ -552,7 +559,7 @@ void ArduinoIoTCloudTCP::onOTARequest() WiFiStorage.remove("/fs/UPDATE.BIN.LZSS.TMP"); #ifdef ARDUINO_ARCH_SAMD - Watchdog.reset(); + samd_watchdog_reset(); #endif /* ARDUINO_ARCH_SAMD */ /* Trigger direct download to nina module. */ diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index c26e08ec3..bdf61e105 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -66,11 +66,11 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass virtual void printDebugInfo() override; #if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) - int begin(ConnectionHandler & connection, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); + int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); #else - int begin(ConnectionHandler & connection, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH); + int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH); #endif - int begin(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_ESP inline void setBoardId (String const device_id) { setDeviceId(device_id); } diff --git a/src/utility/watchdog/Watchdog.cpp b/src/utility/watchdog/Watchdog.cpp new file mode 100644 index 000000000..9cabf4d15 --- /dev/null +++ b/src/utility/watchdog/Watchdog.cpp @@ -0,0 +1,58 @@ +/* + This file is part of ArduinoIoTCloud. + + Copyright 2020 ARDUINO SA (http://www.arduino.cc/) + + This software is released under the GNU General Public License version 3, + which covers the main part of arduino-cli. + The terms of this license can be found at: + https://www.gnu.org/licenses/gpl-3.0.en.html + + You can be released from the requirements of the above licenses by purchasing + a commercial license. Buying such a license is mandatory if you want to modify or + otherwise use the software for commercial activities involving the Arduino + software without disclosing the source code of your own applications. To purchase + a commercial license, send an email to license@arduino.cc. +*/ + +/****************************************************************************** + * INCLUDE + ******************************************************************************/ + +#include "Watchdog.h" + +/****************************************************************************** + * GLOBAL VARIABLES + ******************************************************************************/ + +static bool is_watchdog_enabled = false; + +/****************************************************************************** + * FUNCTION DEFINITION + ******************************************************************************/ + +#ifdef ARDUINO_ARCH_SAMD +void samd_watchdog_enable() +{ + is_watchdog_enabled = true; + Watchdog.enable(SAMD_WATCHDOG_MAX_TIME_ms); +} + +void samd_watchdog_reset() +{ + if (is_watchdog_enabled) { + Watchdog.reset(); + } +} + +/* This function is called within the WiFiNINA library when invoking + * the method 'connectBearSSL' in order to prevent a premature bite + * of the watchdog (max timeout on SAMD is 16 s). wifi_nina_feed... + * is defined a weak function there and overwritten by this "strong" + * function here. + */ +void wifi_nina_feed_watchdog() +{ + samd_watchdog_reset(); +} +#endif /* ARDUINO_ARCH_SAMD */ diff --git a/src/utility/watchdog/Watchdog.h b/src/utility/watchdog/Watchdog.h index 9f81e372a..6cdcf307a 100644 --- a/src/utility/watchdog/Watchdog.h +++ b/src/utility/watchdog/Watchdog.h @@ -22,9 +22,24 @@ * INCLUDE ******************************************************************************/ +#include + +/****************************************************************************** + * PREPROCESSOR SECTION + ******************************************************************************/ + #ifdef ARDUINO_ARCH_SAMD # include # define SAMD_WATCHDOG_MAX_TIME_ms (16 * 1000) #endif /* ARDUINO_ARCH_SAMD */ +/****************************************************************************** + * FUNCTION DECLARATION + ******************************************************************************/ + +#ifdef ARDUINO_ARCH_SAMD +void samd_watchdog_enable(); +void samd_watchdog_reset(); +#endif /* ARDUINO_ARCH_SAMD */ + #endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */