From d8a99651d30bca75f3fb4176608d16dd20d14191 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 3 Mar 2021 15:27:39 +0100 Subject: [PATCH 1/7] This change enabled BearSSL offload for Arduino MKR WiFi 1010 and Nano 33 IoT. --- src/AIoTC_Config.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From fd0bb72153e34db41aa6779753c094593b7f7e11 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 15 Mar 2021 14:02:02 +0100 Subject: [PATCH 2/7] Bugfix: Don't call ECCX08::end() as it disable the I2C module. This has the unpleasant side-effect that any future access to the I2C bus is trying to access an uninitialized I2C module and gets stuck in a loop. --- src/ArduinoIoTCloudTCP.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index d3a5db3fd..4a5fcaf31 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -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 From d766bb3cc1f21d7c52703b2977f5e4fcf9f647c0 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 17 Mar 2021 08:30:17 +0100 Subject: [PATCH 3/7] Provide strong implementation in order to override 'wifi_nina_feed_watchdog' defined weak within WiFiNINA. --- src/utility/watchdog/Watchdog.cpp | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/utility/watchdog/Watchdog.cpp diff --git a/src/utility/watchdog/Watchdog.cpp b/src/utility/watchdog/Watchdog.cpp new file mode 100644 index 000000000..f6606960c --- /dev/null +++ b/src/utility/watchdog/Watchdog.cpp @@ -0,0 +1,39 @@ +/* + 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" + +/****************************************************************************** + * FUNCTION DEFINITION + ******************************************************************************/ + +#ifdef ARDUINO_ARCH_SAMD +/* 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() +{ + Watchdog.reset(); +} +#endif /* ARDUINO_ARCH_SAMD */ From ba61452fa2ce84110bea3648c87919ec9a96c667 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 24 Mar 2021 09:34:16 +0100 Subject: [PATCH 4/7] Allow central enabling/disabling of watchdog for SAMD within AIoTConfig.h --- src/AIoTC_Config.h | 6 ++++++ src/utility/watchdog/Watchdog.cpp | 6 ++++-- src/utility/watchdog/Watchdog.h | 12 ++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index 969cbe18a..e9511bce1 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -87,6 +87,12 @@ * AUTOMATICALLY CONFIGURED DEFINES ******************************************************************************/ +#ifdef ARDUINO_ARCH_SAMD +# define WATCHDOG_ENABLED (1) +#else +# define WATCHDOG_ENABLED (0) +#endif + #if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || \ defined(ARDUINO_AVR_UNO_WIFI_REV2) #define OTA_STORAGE_SNU (1) diff --git a/src/utility/watchdog/Watchdog.cpp b/src/utility/watchdog/Watchdog.cpp index f6606960c..8c06cb7a5 100644 --- a/src/utility/watchdog/Watchdog.cpp +++ b/src/utility/watchdog/Watchdog.cpp @@ -25,7 +25,8 @@ * FUNCTION DEFINITION ******************************************************************************/ -#ifdef ARDUINO_ARCH_SAMD +#if WATCHDOG_ENABLED +# ifdef ARDUINO_ARCH_SAMD /* 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... @@ -36,4 +37,5 @@ void wifi_nina_feed_watchdog() { Watchdog.reset(); } -#endif /* ARDUINO_ARCH_SAMD */ +# endif /* ARDUINO_ARCH_SAMD */ +#endif /* WATCHDOG_ENABLED */ diff --git a/src/utility/watchdog/Watchdog.h b/src/utility/watchdog/Watchdog.h index 9f81e372a..d89003fda 100644 --- a/src/utility/watchdog/Watchdog.h +++ b/src/utility/watchdog/Watchdog.h @@ -22,9 +22,13 @@ * INCLUDE ******************************************************************************/ -#ifdef ARDUINO_ARCH_SAMD -# include -# define SAMD_WATCHDOG_MAX_TIME_ms (16 * 1000) -#endif /* ARDUINO_ARCH_SAMD */ +#include + +#if WATCHDOG_ENABLED +# ifdef ARDUINO_ARCH_SAMD +# include +# define SAMD_WATCHDOG_MAX_TIME_ms (16 * 1000) +# endif /* ARDUINO_ARCH_SAMD */ +#endif /* WATCHDOG_ENABLED */ #endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */ From 11ac4365bb9d02a8d95d333794300fde121bbdf9 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 24 Mar 2021 10:27:47 +0100 Subject: [PATCH 5/7] Provide error message when NINA firmware is < 1.4.3 if the user wants to take advantage of SSL offloading. --- src/ArduinoIoTCloudTCP.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 4a5fcaf31..f3e9fe3ff 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -253,16 +253,22 @@ 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 From 2ce057809b95e8988a00a392a94ac48648b29087 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 7 Apr 2021 14:27:48 +0200 Subject: [PATCH 6/7] Adding missing dependency of Adafruit_SleepyDog for Arduino MKR WAN 1310. --- .github/workflows/compile-examples.yml | 1 + 1 file changed, 1 insertion(+) 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: From 66b2e478f828cff8c2ed08a3952312e04365d1e6 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 8 Apr 2021 13:18:21 +0200 Subject: [PATCH 7/7] Replace compile-time configuration of watchdog (compiled in/out) with parameter in 'begin' function. The watchdog is now always compiled in, but can be disabled when setting the parameter 'enable_watchdog' too false. The parameter is set to true by default. --- src/AIoTC_Config.h | 6 ------ src/ArduinoIoTCloudTCP.cpp | 16 +++++++++------- src/ArduinoIoTCloudTCP.h | 6 +++--- src/utility/watchdog/Watchdog.cpp | 27 ++++++++++++++++++++++----- src/utility/watchdog/Watchdog.h | 23 +++++++++++++++++------ 5 files changed, 51 insertions(+), 27 deletions(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index e9511bce1..969cbe18a 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -87,12 +87,6 @@ * AUTOMATICALLY CONFIGURED DEFINES ******************************************************************************/ -#ifdef ARDUINO_ARCH_SAMD -# define WATCHDOG_ENABLED (1) -#else -# define WATCHDOG_ENABLED (0) -#endif - #if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || \ defined(ARDUINO_AVR_UNO_WIFI_REV2) #define OTA_STORAGE_SNU (1) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index f3e9fe3ff..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; @@ -275,7 +275,9 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) * 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; @@ -287,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. */ @@ -546,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()); @@ -557,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 index 8c06cb7a5..9cabf4d15 100644 --- a/src/utility/watchdog/Watchdog.cpp +++ b/src/utility/watchdog/Watchdog.cpp @@ -21,12 +21,30 @@ #include "Watchdog.h" +/****************************************************************************** + * GLOBAL VARIABLES + ******************************************************************************/ + +static bool is_watchdog_enabled = false; + /****************************************************************************** * FUNCTION DEFINITION ******************************************************************************/ -#if WATCHDOG_ENABLED -# ifdef ARDUINO_ARCH_SAMD +#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... @@ -35,7 +53,6 @@ */ void wifi_nina_feed_watchdog() { - Watchdog.reset(); + samd_watchdog_reset(); } -# endif /* ARDUINO_ARCH_SAMD */ -#endif /* WATCHDOG_ENABLED */ +#endif /* ARDUINO_ARCH_SAMD */ diff --git a/src/utility/watchdog/Watchdog.h b/src/utility/watchdog/Watchdog.h index d89003fda..6cdcf307a 100644 --- a/src/utility/watchdog/Watchdog.h +++ b/src/utility/watchdog/Watchdog.h @@ -24,11 +24,22 @@ #include -#if WATCHDOG_ENABLED -# ifdef ARDUINO_ARCH_SAMD -# include -# define SAMD_WATCHDOG_MAX_TIME_ms (16 * 1000) -# endif /* ARDUINO_ARCH_SAMD */ -#endif /* WATCHDOG_ENABLED */ +/****************************************************************************** + * 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_ */