diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index 559a184ac..7e1595839 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -63,6 +63,7 @@ jobs: - name: ArduinoECCX08 - name: RTCZero - name: WiFi101 + - source-url: https://github.com/adafruit/Adafruit_SleepyDog.git sketch-paths: | - examples/utility/Provisioning # MKR WiFi 1010, Nano 33 IoT @@ -77,6 +78,7 @@ jobs: - name: WiFiNINA - name: Arduino_JSON - name: ArduinoBearSSL + - source-url: https://github.com/adafruit/Adafruit_SleepyDog.git sketch-paths: | - examples/utility/Provisioning - examples/utility/SelfProvisioning @@ -113,6 +115,7 @@ jobs: - name: ArduinoECCX08 - name: RTCZero - name: MKRGSM + - source-url: https://github.com/adafruit/Adafruit_SleepyDog.git sketch-paths: | - examples/utility/Provisioning # NB boards @@ -124,6 +127,7 @@ jobs: - name: ArduinoECCX08 - name: RTCZero - name: MKRNB + - source-url: https://github.com/adafruit/Adafruit_SleepyDog.git sketch-paths: | - examples/utility/Provisioning # Portenta diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 4d6d294e4..d3a5db3fd 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -43,6 +43,8 @@ #include "cbor/CBOREncoder.h" +#include "utility/watchdog/Watchdog.h" + /****************************************************************************** * EXTERN ******************************************************************************/ @@ -262,11 +264,27 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) } #endif /* OTA_STORAGE_SNU */ +#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); +#endif /* ARDUINO_ARCH_SAMD */ + return 1; } void ArduinoIoTCloudTCP::update() { +#ifdef ARDUINO_ARCH_SAMD + /* Feed the watchdog. If any of the functions called below + * get stuck than we can at least reset and recover. + */ + Watchdog.reset(); +#endif /* ARDUINO_ARCH_SAMD */ + /* Run through the state machine. */ State next_state = _state; switch (_state) @@ -522,6 +540,10 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l #if OTA_ENABLED void ArduinoIoTCloudTCP::onOTARequest() { +#ifdef ARDUINO_ARCH_SAMD + Watchdog.reset(); +#endif /* ARDUINO_ARCH_SAMD */ + DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s _ota_url = %s", __FUNCTION__, _ota_url.c_str()); #if OTA_STORAGE_SNU @@ -529,6 +551,10 @@ void ArduinoIoTCloudTCP::onOTARequest() WiFiStorage.remove("/fs/UPDATE.BIN.LZSS"); WiFiStorage.remove("/fs/UPDATE.BIN.LZSS.TMP"); +#ifdef ARDUINO_ARCH_SAMD + Watchdog.reset(); +#endif /* ARDUINO_ARCH_SAMD */ + /* Trigger direct download to nina module. */ uint8_t nina_ota_err_code = 0; if (!WiFiStorage.downloadOTA(_ota_url.c_str(), &nina_ota_err_code)) diff --git a/src/utility/watchdog/Watchdog.h b/src/utility/watchdog/Watchdog.h new file mode 100644 index 000000000..9f81e372a --- /dev/null +++ b/src/utility/watchdog/Watchdog.h @@ -0,0 +1,30 @@ +/* + 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. +*/ + +#ifndef ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ +#define ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ + +/****************************************************************************** + * INCLUDE + ******************************************************************************/ + +#ifdef ARDUINO_ARCH_SAMD +# include +# define SAMD_WATCHDOG_MAX_TIME_ms (16 * 1000) +#endif /* ARDUINO_ARCH_SAMD */ + +#endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */