Skip to content

Commit ee4e4b9

Browse files
Defined TimeoutTable for the user to customize timeouts
changing the approach from defining a static array CHECK_INTERVAL_TABLE we can allow the user to change timeout values at runtime depending on his needs.
1 parent 34a52e4 commit ee4e4b9

10 files changed

+40
-20
lines changed

src/CatM1ConnectionHandler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
9595
if (ping_result < 0)
9696
{
9797
Debug.print(DBG_ERROR, F("Internet check failed"));
98-
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
98+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
9999
return NetworkConnectionState::CONNECTING;
100100
}
101101
else

src/CellularConnectionHandler.cpp

+1-1
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

+25-11
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

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ConnectionHandler::ConnectionHandler(bool const keep_alive, NetworkAdapter inter
3131
, _lastConnectionTickTime{millis()}
3232
, _check_internet_availability{false}
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

+4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ class ConnectionHandler {
103103

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

106+
void updateTimeoutTable(const TimeoutTable& t) { _timeoutTable = t; }
107+
void updateTimeoutTable(TimeoutTable&& t) { _timeoutTable = std::move(t); }
108+
106109
protected:
107110

108111
virtual NetworkConnectionState updateConnectionState();
@@ -120,6 +123,7 @@ class ConnectionHandler {
120123

121124
models::NetworkSetting _settings;
122125

126+
TimeoutTable _timeoutTable;
123127
private:
124128

125129
unsigned long _lastConnectionTickTime;

src/EthernetConnectionHandler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
117117
if (ping_result < 0)
118118
{
119119
Debug.print(DBG_ERROR, F("Internet check 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/GSMConnectionHandler.cpp

+1-1
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

+1-1
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

+1-1
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

+2-2
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)