Skip to content

Enable BearSSL Offload for MKR WiFi 1010 and Nano 33 IoT #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 9, 2021
Merged
1 change: 1 addition & 0 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/AIoTC_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 18 additions & 11 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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. */
Expand Down Expand Up @@ -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());
Expand All @@ -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. */
Expand Down
6 changes: 3 additions & 3 deletions src/ArduinoIoTCloudTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down
58 changes: 58 additions & 0 deletions src/utility/watchdog/Watchdog.cpp
Original file line number Diff line number Diff line change
@@ -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 [email protected].
*/

/******************************************************************************
* 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 */
15 changes: 15 additions & 0 deletions src/utility/watchdog/Watchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,24 @@
* INCLUDE
******************************************************************************/

#include <AIoTC_Config.h>

/******************************************************************************
* PREPROCESSOR SECTION
******************************************************************************/

#ifdef ARDUINO_ARCH_SAMD
# include <Adafruit_SleepyDog.h>
# 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_ */