Skip to content

Check internet availability #137

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 3 commits 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
33 changes: 27 additions & 6 deletions src/CatM1ConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleInit()
pinMode(ON_MKR2, OUTPUT);
digitalWrite(ON_MKR2, HIGH);
#endif
return NetworkConnectionState::CONNECTING;
}

NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
{
if(!GSM.begin(
_settings.catm1.pin,
_settings.catm1.apn,
Expand All @@ -80,8 +76,33 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
return NetworkConnectionState::ERROR;
}
Debug.print(DBG_INFO, F("Connected to Network"));
return NetworkConnectionState::CONNECTED;
return NetworkConnectionState::CONNECTING;
}

NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
{
if (!GSM.isConnected())
{
return NetworkConnectionState::INIT;
}

if(!_check_internet_availability){
return NetworkConnectionState::CONNECTED;
}

int ping_result = GSM.ping("time.arduino.cc");
Debug.print(DBG_INFO, F("GSM.ping(): %d"), ping_result);
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)]);
return NetworkConnectionState::CONNECTING;
}
else
{
Debug.print(DBG_INFO, F("Connected to Internet"));
return NetworkConnectionState::CONNECTED;
}
}

NetworkConnectionState CatM1ConnectionHandler::update_handleConnected()
Expand Down
23 changes: 19 additions & 4 deletions src/CellularConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,31 @@ NetworkConnectionState CellularConnectionHandler::update_handleInit()
Debug.print(DBG_ERROR, F("SIM not present or wrong PIN"));
return NetworkConnectionState::ERROR;
}
return NetworkConnectionState::CONNECTING;
}

NetworkConnectionState CellularConnectionHandler::update_handleConnecting()
{
if (!_cellular.connect(String(_settings.cell.apn), String(_settings.cell.login), String(_settings.cell.pass))) {
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
return NetworkConnectionState::ERROR;
}
Debug.print(DBG_INFO, F("Connected to Network"));
return NetworkConnectionState::CONNECTING;
}

NetworkConnectionState CellularConnectionHandler::update_handleConnecting()
{
if (!_cellular.isConnectedToInternet()) {
return NetworkConnectionState::INIT;
}

if (!_check_internet_availability) {
return NetworkConnectionState::CONNECTED;
}

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)]);
return NetworkConnectionState::CONNECTING;
}

return NetworkConnectionState::CONNECTED;
}

Expand Down
6 changes: 3 additions & 3 deletions src/ConnectionHandlerDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ enum class NetworkAdapter {

static unsigned int const CHECK_INTERVAL_TABLE[] =
{
/* INIT */ 100,
#if defined(BOARD_HAS_NOTECARD) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
/* CONNECTING */ 4000,
/* INIT */ 4000,
#else
/* CONNECTING */ 500,
/* INIT */ 500,
#endif
/* CONNECTING */ 500,
/* CONNECTED */ 10000,
/* DISCONNECTING */ 100,
/* DISCONNECTED */ 1000,
Expand Down
1 change: 1 addition & 0 deletions src/ConnectionHandlerInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ConnectionHandler::ConnectionHandler(bool const keep_alive, NetworkAdapter inter
: _keep_alive{keep_alive}
, _interface{interface}
, _lastConnectionTickTime{millis()}
, _check_internet_availability{false}
, _current_net_connection_state{NetworkConnectionState::INIT}
{

Expand Down
4 changes: 4 additions & 0 deletions src/ConnectionHandlerInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class ConnectionHandler {

virtual void connect();
virtual void disconnect();
void enableCheckInternetAvailability(bool enable) {
_check_internet_availability = enable;
}

virtual void addCallback(NetworkConnectionEvent const event, OnNetworkEventCallback callback);
void addConnectCallback(OnNetworkEventCallback callback) __attribute__((deprecated));
Expand Down Expand Up @@ -106,6 +109,7 @@ class ConnectionHandler {
virtual void updateCallback(NetworkConnectionState next_net_connection_state);

bool _keep_alive;
bool _check_internet_availability;
NetworkAdapter _interface;

virtual NetworkConnectionState update_handleInit () = 0;
Expand Down
38 changes: 30 additions & 8 deletions src/EthernetConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */
#include "EthernetConnectionHandler.h"
#include <Udp.h>

/******************************************************************************
CTOR/DTOR
Expand Down Expand Up @@ -72,11 +73,6 @@ NetworkConnectionState EthernetConnectionHandler::update_handleInit()
Debug.print(DBG_ERROR, F("Error, ethernet shield was not found."));
return NetworkConnectionState::ERROR;
}
return NetworkConnectionState::CONNECTING;
}

NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
{
IPAddress ip(_settings.eth.ip.type, _settings.eth.ip.bytes);

// An ip address is provided -> static ip configuration
Expand All @@ -91,7 +87,7 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection"));
Debug.print(DBG_VERBOSE, "timeout: %d, response timeout: %d",
_settings.eth.timeout, _settings.eth.response_timeout);
return NetworkConnectionState::CONNECTING;
return NetworkConnectionState::INIT;
}
// An ip address is not provided -> dhcp configuration
} else {
Expand All @@ -100,11 +96,37 @@ NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
Debug.print(DBG_VERBOSE, "timeout: %d, response timeout: %d",
_settings.eth.timeout, _settings.eth.response_timeout);

return NetworkConnectionState::CONNECTING;
return NetworkConnectionState::INIT;
}
}

return NetworkConnectionState::CONNECTED;
return NetworkConnectionState::CONNECTING;
}

NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
{
if (Ethernet.linkStatus() == LinkOFF) {
return NetworkConnectionState::INIT;
}

if (!_check_internet_availability) {
return NetworkConnectionState::CONNECTED;
}

int ping_result = Ethernet.ping("time.arduino.cc");
Debug.print(DBG_INFO, F("Ethernet.ping(): %d"), ping_result);
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)]);
return NetworkConnectionState::CONNECTING;
}
else
{
Debug.print(DBG_INFO, F("Connected to Internet"));
return NetworkConnectionState::CONNECTED;
}

}

NetworkConnectionState EthernetConnectionHandler::update_handleConnected()
Expand Down
6 changes: 6 additions & 0 deletions src/GSMConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ GSMConnectionHandler::GSMConnectionHandler(const char * pin, const char * apn, c
: ConnectionHandler{keep_alive, NetworkAdapter::GSM}
{
_settings.type = NetworkAdapter::GSM;
// To keep the backward compatibility, the user can call enableCheckInternetAvailability(false) for disabling the check
_check_internet_availability = true;
strncpy(_settings.gsm.pin, pin, sizeof(_settings.gsm.pin)-1);
strncpy(_settings.gsm.apn, apn, sizeof(_settings.gsm.apn)-1);
strncpy(_settings.gsm.login, login, sizeof(_settings.gsm.login)-1);
Expand Down Expand Up @@ -105,6 +107,10 @@ NetworkConnectionState GSMConnectionHandler::update_handleInit()

NetworkConnectionState GSMConnectionHandler::update_handleConnecting()
{
if(!_check_internet_availability){
return NetworkConnectionState::CONNECTED;
}

Debug.print(DBG_INFO, F("Sending PING to outer space..."));
int const ping_result = _gprs.ping("time.arduino.cc");
Debug.print(DBG_INFO, F("GPRS.ping(): %d"), ping_result);
Expand Down
1 change: 1 addition & 0 deletions src/GenericConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ bool GenericConnectionHandler::updateSetting(const models::NetworkSetting& s) {
if(_ch != nullptr) {
_interface = s.type;
_ch->setKeepAlive(_keep_alive);
_ch->enableCheckInternetAvailability(_check_internet_availability);
return _ch->updateSetting(s);
} else {
_interface = NetworkAdapter::NONE;
Expand Down
31 changes: 26 additions & 5 deletions src/WiFiConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleInit()
#else
WiFi.mode(WIFI_STA);
#endif /* #if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32) */
return NetworkConnectionState::CONNECTING;
}

NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
{
if (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(_settings.wifi.ssid, _settings.wifi.pwd);
Expand All @@ -120,7 +116,7 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
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::CONNECTING)]);
#endif
return NetworkConnectionState::CONNECTING;
return NetworkConnectionState::INIT;
}
else
{
Expand All @@ -130,8 +126,33 @@ NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov");
#endif
return NetworkConnectionState::CONNECTING;
}
}

NetworkConnectionState WiFiConnectionHandler::update_handleConnecting()
{
if (WiFi.status() != WL_CONNECTED){
return NetworkConnectionState::INIT;
}

if(!_check_internet_availability){
return NetworkConnectionState::CONNECTED;
}

#if !defined(ARDUINO_ARCH_ESP8266) && !defined(ARDUINO_ARCH_ESP32)
int ping_result = WiFi.ping("time.arduino.cc");
Debug.print(DBG_INFO, F("WiFi.ping(): %d"), ping_result);
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)]);
return NetworkConnectionState::CONNECTING;
}
#endif
Debug.print(DBG_INFO, F("Connected to Internet"));
return NetworkConnectionState::CONNECTED;

}

NetworkConnectionState WiFiConnectionHandler::update_handleConnected()
Expand Down