Skip to content

Commit d314734

Browse files
Merge pull request #140 from andreagilardoni/timeout-table
Defined TimeoutTable for the user to customize timeouts
2 parents e4c1616 + 98ca332 commit d314734

12 files changed

+146
-20
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* SECRET_ fields are in `arduino_secrets.h` (included below)
2+
*
3+
* This example is a lightly modified version of ConnectionHandlerDemo to showcase
4+
* the possibility of changing the timeout values for the different states a connectionhandler have
5+
*/
6+
7+
#include <Arduino_ConnectionHandler.h>
8+
9+
#include "arduino_secrets.h"
10+
11+
#if !(defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \
12+
defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT))
13+
#error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md"
14+
#endif
15+
16+
#if defined(BOARD_HAS_ETHERNET)
17+
EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
18+
#elif defined(BOARD_HAS_WIFI)
19+
WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
20+
#elif defined(BOARD_HAS_GSM)
21+
GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
22+
#elif defined(BOARD_HAS_NB)
23+
NBConnectionHandler conMan(SECRET_PIN);
24+
#elif defined(BOARD_HAS_LORA)
25+
LoRaConnectionHandler conMan(SECRET_APP_EUI, SECRET_APP_KEY);
26+
#elif defined(BOARD_HAS_CATM1_NBIOT)
27+
CatM1ConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
28+
#elif defined(BOARD_HAS_CELLULAR)
29+
CellularConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
30+
#endif
31+
32+
bool attemptConnect = false;
33+
uint32_t lastConnToggleMs = 0;
34+
35+
void setup() {
36+
/* Initialize serial debug port and wait up to 5 seconds for port to open */
37+
Serial.begin(9600);
38+
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
39+
40+
#ifndef __AVR__
41+
/* Set the debug message level:
42+
* - DBG_ERROR: Only show error messages
43+
* - DBG_WARNING: Show warning and error messages
44+
* - DBG_INFO: Show info, warning, and error messages
45+
* - DBG_DEBUG: Show debug, info, warning, and error messages
46+
* - DBG_VERBOSE: Show all messages
47+
*/
48+
setDebugMessageLevel(DBG_INFO);
49+
#endif
50+
51+
/* Add callbacks to the ConnectionHandler object to get notified of network
52+
* connection events. */
53+
conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect);
54+
conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect);
55+
conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError);
56+
57+
/* By using updateTimeoutInterval I can change the timeout value for a specific
58+
* state of the connection handler
59+
*/
60+
conMan.updateTimeoutInterval(NetworkConnectionState::INIT, 8000);
61+
conMan.updateTimeoutInterval(NetworkConnectionState::CONNECTING, 1000);
62+
conMan.updateTimeoutInterval(NetworkConnectionState::CONNECTED, 20000);
63+
conMan.updateTimeoutInterval(NetworkConnectionState::DISCONNECTING, 200);
64+
conMan.updateTimeoutInterval(NetworkConnectionState::DISCONNECTED, 2000);
65+
conMan.updateTimeoutInterval(NetworkConnectionState::CLOSED, 2000);
66+
conMan.updateTimeoutInterval(NetworkConnectionState::ERROR, 2000);
67+
}
68+
69+
void loop() {
70+
conMan.check();
71+
}
72+
73+
void onNetworkConnect() {
74+
Serial.println(">>>> CONNECTED to network");
75+
}
76+
77+
void onNetworkDisconnect() {
78+
Serial.println(">>>> DISCONNECTED from network");
79+
}
80+
81+
void onNetworkError() {
82+
Serial.println(">>>> ERROR");
83+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Required for WiFiConnectionHandler
2+
const char SECRET_WIFI_SSID[] = "SSID";
3+
const char SECRET_WIFI_PASS[] = "PASSWORD";
4+
5+
// Required for GSMConnectionHandler
6+
const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS";
7+
const char SECRET_PIN[] = "0000"; // Required for NBConnectionHandler
8+
const char SECRET_GSM_USER[] = "GSM USERNAME";
9+
const char SECRET_GSM_PASS[] = "GSM PASSWORD";
10+
11+
// Required for LoRaConnectionHandler
12+
const char SECRET_APP_EUI[] = "APP_EUI";
13+
const char SECRET_APP_KEY[] = "APP_KEY";
14+
15+
// Required for EthernetConnectionHandler (without DHCP mode)
16+
const char SECRET_IP[] = "IP ADDRESS";
17+
const char SECRET_DNS[] = "DNS ADDRESS";
18+
const char SECRET_GATEWAY[] = "GATEWAY ADDRESS";
19+
const char SECRET_NETMASK[] = "NETWORK MASK";

src/CatM1ConnectionHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
107107
if (ping_result < 0)
108108
{
109109
Debug.print(DBG_ERROR, F("Internet check failed"));
110-
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), 2 * CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
110+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
111111
return NetworkConnectionState::CONNECTING;
112112
}
113113
else

src/CellularConnectionHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ NetworkConnectionState CellularConnectionHandler::update_handleConnecting()
8383

8484
if(getTime() == 0){
8585
Debug.print(DBG_ERROR, F("Internet check failed"));
86-
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
86+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
8787
return NetworkConnectionState::CONNECTING;
8888
}
8989

src/ConnectionHandlerDefinitions.h

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,35 @@ enum class NetworkAdapter {
185185
NOTECARD
186186
};
187187

188+
union TimeoutTable {
189+
struct {
190+
// Note: order of the following values must be preserved
191+
// and match against NetworkConnectionState values
192+
uint32_t init;
193+
uint32_t connecting;
194+
uint32_t connected;
195+
uint32_t disconnecting;
196+
uint32_t disconnected;
197+
uint32_t closed;
198+
uint32_t error;
199+
} timeout;
200+
uint32_t intervals[sizeof(timeout) / sizeof(uint32_t)];
201+
};
202+
188203
/******************************************************************************
189204
CONSTANTS
190205
******************************************************************************/
191206

192-
static unsigned int const CHECK_INTERVAL_TABLE[] =
193-
{
207+
constexpr TimeoutTable DefaultTimeoutTable {
194208
#if defined(BOARD_HAS_NOTECARD) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
195-
/* INIT */ 4000,
209+
4000, // init
196210
#else
197-
/* INIT */ 500,
198-
#endif
199-
/* CONNECTING */ 500,
200-
/* CONNECTED */ 10000,
201-
/* DISCONNECTING */ 100,
202-
/* DISCONNECTED */ 1000,
203-
/* CLOSED */ 1000,
204-
/* ERROR */ 1000
211+
500, // init
212+
#endif
213+
500, // connecting
214+
10000, // connected
215+
100, // disconnecting
216+
1000, // disconnected
217+
1000, // closed
218+
1000, // error
205219
};

src/ConnectionHandlerInterface.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ConnectionHandler::ConnectionHandler(bool const keep_alive, NetworkAdapter inter
3131
, _interface{interface}
3232
, _lastConnectionTickTime{millis()}
3333
, _current_net_connection_state{NetworkConnectionState::INIT}
34+
, _timeoutTable(DefaultTimeoutTable)
3435
{
3536

3637
}
@@ -42,7 +43,8 @@ ConnectionHandler::ConnectionHandler(bool const keep_alive, NetworkAdapter inter
4243
NetworkConnectionState ConnectionHandler::check()
4344
{
4445
unsigned long const now = millis();
45-
unsigned int const connectionTickTimeInterval = CHECK_INTERVAL_TABLE[static_cast<unsigned int>(_current_net_connection_state)];
46+
unsigned int const connectionTickTimeInterval =
47+
_timeoutTable.intervals[static_cast<unsigned int>(_current_net_connection_state)];
4648

4749
if((now - _lastConnectionTickTime) > connectionTickTimeInterval)
4850
{

src/ConnectionHandlerInterface.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "ConnectionHandlerDefinitions.h"
3232
#include "connectionHandlerModels/settings.h"
3333

34+
#include <utility>
35+
3436
/******************************************************************************
3537
TYPEDEFS
3638
******************************************************************************/
@@ -108,6 +110,11 @@ class ConnectionHandler {
108110

109111
virtual void setKeepAlive(bool keep_alive=true) { this->_keep_alive = keep_alive; }
110112

113+
inline void updateTimeoutTable(const TimeoutTable& t) { _timeoutTable = t; }
114+
inline void updateTimeoutTable(TimeoutTable&& t) { _timeoutTable = std::move(t); }
115+
inline void updateTimeoutInterval(NetworkConnectionState state, uint32_t interval) {
116+
_timeoutTable.intervals[static_cast<unsigned int>(state)] = interval;
117+
}
111118
protected:
112119

113120
virtual NetworkConnectionState updateConnectionState();
@@ -125,6 +132,7 @@ class ConnectionHandler {
125132

126133
models::NetworkSetting _settings;
127134

135+
TimeoutTable _timeoutTable;
128136
private:
129137

130138
unsigned long _lastConnectionTickTime;

src/EthernetConnectionHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
119119
if (ping_result < 0)
120120
{
121121
Debug.print(DBG_ERROR, F("Internet check failed"));
122-
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
122+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
123123
return NetworkConnectionState::CONNECTING;
124124
}
125125
else

src/GSMConnectionHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ NetworkConnectionState GSMConnectionHandler::update_handleConnecting()
117117
if (ping_result < 0)
118118
{
119119
Debug.print(DBG_ERROR, F("PING failed"));
120-
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
120+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
121121
return NetworkConnectionState::CONNECTING;
122122
}
123123
else

src/LoRaConnectionHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ NetworkConnectionState LoRaConnectionHandler::update_handleConnecting()
126126
if (network_status != true)
127127
{
128128
Debug.print(DBG_ERROR, F("Connection to the network failed"));
129-
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
129+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
130130
return NetworkConnectionState::INIT;
131131
}
132132
else

src/NotecardConnectionHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ NetworkConnectionState NotecardConnectionHandler::update_handleConnecting()
518518
if (!conn_status.connected_to_notehub) {
519519
if ((::millis() - _conn_start_ms) > NOTEHUB_CONN_TIMEOUT_MS) {
520520
Debug.print(DBG_ERROR, F("Timeout exceeded, connection to the network failed."));
521-
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
521+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
522522
result = NetworkConnectionState::INIT;
523523
} else {
524524
// Continue awaiting the connection to Notehub

src/WiFiConnectionHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit()
114114
{
115115
#if !defined(__AVR__)
116116
Debug.print(DBG_ERROR, F("Connection to \"%s\" failed"), _settings.wifi.ssid);
117-
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::INIT)]);
117+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.init);
118118
#endif
119119
return NetworkConnectionState::INIT;
120120
}
@@ -146,7 +146,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
146146
if (ping_result < 0)
147147
{
148148
Debug.print(DBG_ERROR, F("Internet check failed"));
149-
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
149+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
150150
return NetworkConnectionState::CONNECTING;
151151
}
152152
#endif

0 commit comments

Comments
 (0)