Skip to content

Commit 76e8e84

Browse files
authored
Merge pull request #237 from arduino-libraries/enable-bearssl-offload-samd-watchdog
Enable BearSSL Offload for MKR WiFi 1010 and Nano 33 IoT
2 parents b531a03 + 66b2e47 commit 76e8e84

File tree

6 files changed

+97
-16
lines changed

6 files changed

+97
-16
lines changed

.github/workflows/compile-examples.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ jobs:
105105
- name: ArduinoECCX08
106106
- name: RTCZero
107107
- name: MKRWAN
108+
- source-url: https://github.com/adafruit/Adafruit_SleepyDog.git
108109
sketch-paths:
109110
# GSM boards
110111
- board:

src/AIoTC_Config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@
113113
#endif
114114

115115
#if defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKR1000) || \
116-
defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || \
117116
defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_PORTENTA_H7_M7) || \
118117
defined(ARDUINO_PORTENTA_H7_M4) || defined (ARDUINO_NANO_RP2040_CONNECT)
119118
#define BOARD_HAS_ECCX08
120119
#define HAS_TCP
121120
#endif
122121

123-
#if defined(ARDUINO_AVR_UNO_WIFI_REV2)
122+
#if defined(ARDUINO_AVR_UNO_WIFI_REV2) || \
123+
defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT)
124124
#define BOARD_HAS_OFFLOADED_ECCX08
125125
#define HAS_TCP
126126
#endif

src/ArduinoIoTCloudTCP.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,16 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
104104
* PUBLIC MEMBER FUNCTIONS
105105
******************************************************************************/
106106

107-
int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, String brokerAddress, uint16_t brokerPort)
107+
int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
108108
{
109109
_connection = &connection;
110110
_brokerAddress = brokerAddress;
111111
_brokerPort = brokerPort;
112112
_time_service.begin(&connection);
113-
return begin(_brokerAddress, _brokerPort);
113+
return begin(enable_watchdog, _brokerAddress, _brokerPort);
114114
}
115115

116-
int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
116+
int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
117117
{
118118
_brokerAddress = brokerAddress;
119119
_brokerPort = brokerPort;
@@ -189,7 +189,6 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
189189
DEBUG_ERROR("CryptoUtil::readDeviceId(...) failed.");
190190
return 0;
191191
}
192-
ECCX08.end();
193192
#endif
194193

195194
#ifdef BOARD_HAS_ECCX08
@@ -254,23 +253,31 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
254253
#endif
255254

256255
#if OTA_STORAGE_SNU && OTA_ENABLED
257-
String const nina_fw_version = WiFi.firmwareVersion();
258-
if (nina_fw_version < "1.4.1") {
256+
if (String(WiFi.firmwareVersion()) < String("1.4.1")) {
259257
_ota_cap = false;
260-
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());
258+
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());
261259
}
262260
else {
263261
_ota_cap = true;
264262
}
265263
#endif /* OTA_STORAGE_SNU */
266264

265+
#ifdef BOARD_HAS_OFFLOADED_ECCX08
266+
if (String(WiFi.firmwareVersion()) < String("1.4.3")) {
267+
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());
268+
return 0;
269+
}
270+
#endif /* BOARD_HAS_OFFLOADED_ECCX08 */
271+
267272
#ifdef ARDUINO_ARCH_SAMD
268273
/* Since we do not control what code the user inserts
269274
* between ArduinoIoTCloudTCP::begin() and the first
270275
* call to ArduinoIoTCloudTCP::update() it is wise to
271276
* set a rather large timeout at first.
272277
*/
273-
Watchdog.enable(SAMD_WATCHDOG_MAX_TIME_ms);
278+
if (enable_watchdog) {
279+
samd_watchdog_enable();
280+
}
274281
#endif /* ARDUINO_ARCH_SAMD */
275282

276283
return 1;
@@ -282,7 +289,7 @@ void ArduinoIoTCloudTCP::update()
282289
/* Feed the watchdog. If any of the functions called below
283290
* get stuck than we can at least reset and recover.
284291
*/
285-
Watchdog.reset();
292+
samd_watchdog_reset();
286293
#endif /* ARDUINO_ARCH_SAMD */
287294

288295
/* Run through the state machine. */
@@ -541,7 +548,7 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l
541548
void ArduinoIoTCloudTCP::onOTARequest()
542549
{
543550
#ifdef ARDUINO_ARCH_SAMD
544-
Watchdog.reset();
551+
samd_watchdog_reset();
545552
#endif /* ARDUINO_ARCH_SAMD */
546553

547554
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s _ota_url = %s", __FUNCTION__, _ota_url.c_str());
@@ -552,7 +559,7 @@ void ArduinoIoTCloudTCP::onOTARequest()
552559
WiFiStorage.remove("/fs/UPDATE.BIN.LZSS.TMP");
553560

554561
#ifdef ARDUINO_ARCH_SAMD
555-
Watchdog.reset();
562+
samd_watchdog_reset();
556563
#endif /* ARDUINO_ARCH_SAMD */
557564

558565
/* Trigger direct download to nina module. */

src/ArduinoIoTCloudTCP.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
6666
virtual void printDebugInfo() override;
6767

6868
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08)
69-
int begin(ConnectionHandler & connection, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
69+
int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
7070
#else
71-
int begin(ConnectionHandler & connection, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH);
71+
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);
7272
#endif
73-
int begin(String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
73+
int begin(bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
7474

7575
#ifdef BOARD_ESP
7676
inline void setBoardId (String const device_id) { setDeviceId(device_id); }

src/utility/watchdog/Watchdog.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
/******************************************************************************
19+
* INCLUDE
20+
******************************************************************************/
21+
22+
#include "Watchdog.h"
23+
24+
/******************************************************************************
25+
* GLOBAL VARIABLES
26+
******************************************************************************/
27+
28+
static bool is_watchdog_enabled = false;
29+
30+
/******************************************************************************
31+
* FUNCTION DEFINITION
32+
******************************************************************************/
33+
34+
#ifdef ARDUINO_ARCH_SAMD
35+
void samd_watchdog_enable()
36+
{
37+
is_watchdog_enabled = true;
38+
Watchdog.enable(SAMD_WATCHDOG_MAX_TIME_ms);
39+
}
40+
41+
void samd_watchdog_reset()
42+
{
43+
if (is_watchdog_enabled) {
44+
Watchdog.reset();
45+
}
46+
}
47+
48+
/* This function is called within the WiFiNINA library when invoking
49+
* the method 'connectBearSSL' in order to prevent a premature bite
50+
* of the watchdog (max timeout on SAMD is 16 s). wifi_nina_feed...
51+
* is defined a weak function there and overwritten by this "strong"
52+
* function here.
53+
*/
54+
void wifi_nina_feed_watchdog()
55+
{
56+
samd_watchdog_reset();
57+
}
58+
#endif /* ARDUINO_ARCH_SAMD */

src/utility/watchdog/Watchdog.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,24 @@
2222
* INCLUDE
2323
******************************************************************************/
2424

25+
#include <AIoTC_Config.h>
26+
27+
/******************************************************************************
28+
* PREPROCESSOR SECTION
29+
******************************************************************************/
30+
2531
#ifdef ARDUINO_ARCH_SAMD
2632
# include <Adafruit_SleepyDog.h>
2733
# define SAMD_WATCHDOG_MAX_TIME_ms (16 * 1000)
2834
#endif /* ARDUINO_ARCH_SAMD */
2935

36+
/******************************************************************************
37+
* FUNCTION DECLARATION
38+
******************************************************************************/
39+
40+
#ifdef ARDUINO_ARCH_SAMD
41+
void samd_watchdog_enable();
42+
void samd_watchdog_reset();
43+
#endif /* ARDUINO_ARCH_SAMD */
44+
3045
#endif /* ARDUINO_AIOTC_UTILITY_WATCHDOG_H_ */

0 commit comments

Comments
 (0)