Skip to content

Defined TimeoutTable for the user to customize timeouts #140

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CatM1ConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
if (ping_result < 0)
{
Debug.print(DBG_ERROR, F("Internet check failed"));
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), 2 * CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
return NetworkConnectionState::CONNECTING;
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/CellularConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ NetworkConnectionState CellularConnectionHandler::update_handleConnecting()

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

Expand Down
36 changes: 25 additions & 11 deletions src/ConnectionHandlerDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,35 @@ enum class NetworkAdapter {
NOTECARD
};

union TimeoutTable {
struct {
// Note: order of the following values must be preserved
// and match against NetworkConnectionState values
uint32_t init;
uint32_t connecting;
uint32_t connected;
uint32_t disconnecting;
uint32_t disconnected;
uint32_t closed;
uint32_t error;
} timeout;
uint32_t intervals[sizeof(timeout) / sizeof(uint32_t)];
};

/******************************************************************************
CONSTANTS
******************************************************************************/

static unsigned int const CHECK_INTERVAL_TABLE[] =
{
constexpr TimeoutTable DefaultTimeoutTable {
#if defined(BOARD_HAS_NOTECARD) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
/* INIT */ 4000,
4000, // init
#else
/* INIT */ 500,
#endif
/* CONNECTING */ 500,
/* CONNECTED */ 10000,
/* DISCONNECTING */ 100,
/* DISCONNECTED */ 1000,
/* CLOSED */ 1000,
/* ERROR */ 1000
500, // init
#endif
500, // connecting
10000, // connected
100, // disconnecting
1000, // disconnected
1000, // closed
1000, // error
};
4 changes: 3 additions & 1 deletion src/ConnectionHandlerInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ConnectionHandler::ConnectionHandler(bool const keep_alive, NetworkAdapter inter
, _lastConnectionTickTime{millis()}
, _check_internet_availability{false}
, _current_net_connection_state{NetworkConnectionState::INIT}
, _timeoutTable(DefaultTimeoutTable)
{

}
Expand All @@ -42,7 +43,8 @@ ConnectionHandler::ConnectionHandler(bool const keep_alive, NetworkAdapter inter
NetworkConnectionState ConnectionHandler::check()
{
unsigned long const now = millis();
unsigned int const connectionTickTimeInterval = CHECK_INTERVAL_TABLE[static_cast<unsigned int>(_current_net_connection_state)];
unsigned int const connectionTickTimeInterval =
_timeoutTable.intervals[static_cast<unsigned int>(_current_net_connection_state)];

if((now - _lastConnectionTickTime) > connectionTickTimeInterval)
{
Expand Down
8 changes: 8 additions & 0 deletions src/ConnectionHandlerInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "ConnectionHandlerDefinitions.h"
#include "connectionHandlerModels/settings.h"

#include <utility>

/******************************************************************************
TYPEDEFS
******************************************************************************/
Expand Down Expand Up @@ -103,6 +105,11 @@ class ConnectionHandler {

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

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

virtual NetworkConnectionState updateConnectionState();
Expand All @@ -120,6 +127,7 @@ class ConnectionHandler {

models::NetworkSetting _settings;

TimeoutTable _timeoutTable;
private:

unsigned long _lastConnectionTickTime;
Expand Down
2 changes: 1 addition & 1 deletion src/EthernetConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
if (ping_result < 0)
{
Debug.print(DBG_ERROR, F("Internet check failed"));
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
return NetworkConnectionState::CONNECTING;
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/GSMConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ NetworkConnectionState GSMConnectionHandler::update_handleConnecting()
if (ping_result < 0)
{
Debug.print(DBG_ERROR, F("PING failed"));
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
return NetworkConnectionState::CONNECTING;
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/LoRaConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ NetworkConnectionState LoRaConnectionHandler::update_handleConnecting()
if (network_status != true)
{
Debug.print(DBG_ERROR, F("Connection to the network failed"));
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
return NetworkConnectionState::INIT;
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/NotecardConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ NetworkConnectionState NotecardConnectionHandler::update_handleConnecting()
if (!conn_status.connected_to_notehub) {
if ((::millis() - _conn_start_ms) > NOTEHUB_CONN_TIMEOUT_MS) {
Debug.print(DBG_ERROR, F("Timeout exceeded, connection to the network failed."));
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
result = NetworkConnectionState::INIT;
} else {
// Continue awaiting the connection to Notehub
Expand Down
4 changes: 2 additions & 2 deletions src/WiFiConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit()
{
#if !defined(__AVR__)
Debug.print(DBG_ERROR, F("Connection to \"%s\" failed"), _settings.wifi.ssid);
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::INIT)]);
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.init);
#endif
return NetworkConnectionState::INIT;
}
Expand Down Expand Up @@ -146,7 +146,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
if (ping_result < 0)
{
Debug.print(DBG_ERROR, F("Internet check failed"));
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), _timeoutTable.timeout.connecting);
return NetworkConnectionState::CONNECTING;
}
#endif
Expand Down