Skip to content

Commit 2ab2a4b

Browse files
committed
Instead of relying on the 'hidden mechanism' of passing a function to feed the watchdog via a weak function definition pass a function pointer via a public API instead. This increases visibility and reduces unintentional introductions of bugs.
1 parent 5d63d7d commit 2ab2a4b

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

src/WiFi.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extern "C" {
2727
#include "utility/debug.h"
2828
}
2929

30-
WiFiClass::WiFiClass() : _timeout(50000)
30+
WiFiClass::WiFiClass() : _timeout(50000), _feed_watchdog_func(0)
3131
{
3232
}
3333

@@ -49,6 +49,7 @@ int WiFiClass::begin(const char* ssid)
4949
{
5050
for (unsigned long start = millis(); (millis() - start) < _timeout;)
5151
{
52+
feedWatchdog();
5253
delay(WL_DELAY_START_CONNECTION);
5354
status = WiFiDrv::getConnectionStatus();
5455
if ((status != WL_IDLE_STATUS) && (status != WL_NO_SSID_AVAIL) && (status != WL_SCAN_COMPLETED)) {
@@ -71,6 +72,7 @@ int WiFiClass::begin(const char* ssid, uint8_t key_idx, const char *key)
7172
{
7273
for (unsigned long start = millis(); (millis() - start) < _timeout;)
7374
{
75+
feedWatchdog();
7476
delay(WL_DELAY_START_CONNECTION);
7577
status = WiFiDrv::getConnectionStatus();
7678
if ((status != WL_IDLE_STATUS) && (status != WL_NO_SSID_AVAIL) && (status != WL_SCAN_COMPLETED)) {
@@ -92,6 +94,7 @@ int WiFiClass::begin(const char* ssid, const char *passphrase)
9294
{
9395
for (unsigned long start = millis(); (millis() - start) < _timeout;)
9496
{
97+
feedWatchdog();
9598
delay(WL_DELAY_START_CONNECTION);
9699
status = WiFiDrv::getConnectionStatus();
97100
if ((status != WL_IDLE_STATUS) && (status != WL_NO_SSID_AVAIL) && (status != WL_SCAN_COMPLETED)) {
@@ -382,4 +385,16 @@ void WiFiClass::setTimeout(unsigned long timeout)
382385
{
383386
_timeout = timeout;
384387
}
388+
389+
void WiFiClass::setFeedWatchdogFunc(FeedHostProcessorWatchdogFuncPointer func)
390+
{
391+
_feed_watchdog_func = func;
392+
}
393+
394+
void WiFiClass::feedWatchdog()
395+
{
396+
if (_feed_watchdog_func)
397+
_feed_watchdog_func();
398+
}
399+
385400
WiFiClass WiFi;

src/WiFi.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ extern "C" {
3636
#include "WiFiServer.h"
3737
#include "WiFiStorage.h"
3838

39+
typedef void(*FeedHostProcessorWatchdogFuncPointer)();
40+
3941
class WiFiClass
4042
{
4143
private:
4244

4345
static void init();
4446
unsigned long _timeout;
47+
FeedHostProcessorWatchdogFuncPointer _feed_watchdog_func;
4548
public:
4649
WiFiClass();
4750

@@ -274,6 +277,9 @@ class WiFiClass
274277
int ping(IPAddress host, uint8_t ttl = 128);
275278

276279
void setTimeout(unsigned long timeout);
280+
281+
void setFeedWatchdogFunc(FeedHostProcessorWatchdogFuncPointer func);
282+
void feedWatchdog();
277283
};
278284

279285
extern WiFiClass WiFi;

src/utility/spi_drv.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <SPI.h>
2323
#include "utility/spi_drv.h"
2424
#include "pins_arduino.h"
25+
#include "WiFi.h"
2526

2627
#ifdef ARDUINO_SAMD_MKRVIDOR4000
2728

@@ -67,13 +68,7 @@ static bool inverted_reset = false;
6768

6869
bool SpiDrv::initialized = false;
6970

70-
__attribute__((weak)) void wifi_nina_feed_watchdog()
71-
{
72-
/* This function can be overwritten by a "strong" implementation
73-
* in a higher level application, such as the ArduinoIoTCloud
74-
* firmware stack.
75-
*/
76-
}
71+
extern WiFiClass WiFi;
7772

7873
void SpiDrv::begin()
7974
{
@@ -227,7 +222,7 @@ void SpiDrv::waitForSlaveReady(bool const feed_watchdog)
227222
{
228223
if (feed_watchdog) {
229224
if ((millis() - start) < 10000) {
230-
wifi_nina_feed_watchdog();
225+
WiFi.feedWatchdog();
231226
}
232227
}
233228
}

0 commit comments

Comments
 (0)