Skip to content

CatM1: allow retry if connection fails #131

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

Merged
merged 6 commits into from
May 14, 2025
Merged
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
26 changes: 20 additions & 6 deletions src/CatM1ConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ CatM1ConnectionHandler::CatM1ConnectionHandler(
: ConnectionHandler{keep_alive, NetworkAdapter::CATM1}
{
_settings.type = NetworkAdapter::CATM1;
// To keep the backward compatibility, the user can call enableCheckInternetAvailability(false) for disabling the check
_check_internet_availability = true;
strncpy(_settings.catm1.pin, pin, sizeof(_settings.catm1.pin)-1);
strncpy(_settings.catm1.apn, apn, sizeof(_settings.catm1.apn)-1);
strncpy(_settings.catm1.login, login, sizeof(_settings.catm1.login)-1);
strncpy(_settings.catm1.pass, pass, sizeof(_settings.catm1.pass)-1);
_settings.catm1.rat = static_cast<uint8_t>(rat);
_settings.catm1.band = band;
_reset = false;
}

/******************************************************************************
Expand All @@ -51,7 +54,10 @@ CatM1ConnectionHandler::CatM1ConnectionHandler(

unsigned long CatM1ConnectionHandler::getTime()
{
return GSM.getTime();
/* It is not safe to call GSM.getTime() since we don't know if modem internal
* RTC is in sync with current time.
*/
return 0;
}

/******************************************************************************
Expand All @@ -61,6 +67,7 @@ unsigned long CatM1ConnectionHandler::getTime()
NetworkConnectionState CatM1ConnectionHandler::update_handleInit()
{
#if defined (ARDUINO_EDGE_CONTROL)
/* Power on module */
pinMode(ON_MKR2, OUTPUT);
digitalWrite(ON_MKR2, HIGH);
#endif
Expand All @@ -71,31 +78,36 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleInit()
_settings.catm1.login,
_settings.catm1.pass,
static_cast<RadioAccessTechnologyType>(_settings.catm1.rat) ,
_settings.catm1.band))
_settings.catm1.band,
_reset))
{
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
return NetworkConnectionState::ERROR;
_reset = true;
return NetworkConnectionState::DISCONNECTED;
}
_reset = false;
return NetworkConnectionState::CONNECTING;
}

NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
{
if (!GSM.isConnected())
{
return NetworkConnectionState::INIT;
Debug.print(DBG_ERROR, F("GSM connection not alive... disconnecting"));
return NetworkConnectionState::DISCONNECTED;
}

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

int ping_result = GSM.ping("time.arduino.cc");
Debug.print(DBG_INFO, F("Sending PING to outer space..."));
int const 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)]);
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), 2 * CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
return NetworkConnectionState::CONNECTING;
}
else
Expand All @@ -110,6 +122,7 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleConnected()
int const is_gsm_access_alive = GSM.isConnected();
if (is_gsm_access_alive != 1)
{
Debug.print(DBG_ERROR, F("GSM connection not alive... disconnecting"));
return NetworkConnectionState::DISCONNECTED;
}
return NetworkConnectionState::CONNECTED;
Expand All @@ -123,6 +136,7 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleDisconnecting()

NetworkConnectionState CatM1ConnectionHandler::update_handleDisconnected()
{
GSM.end();
if (_keep_alive)
{
return NetworkConnectionState::INIT;
Expand Down
2 changes: 2 additions & 0 deletions src/CatM1ConnectionHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class CatM1ConnectionHandler : public ConnectionHandler

private:

bool _reset;

GSMUDP _gsm_udp;
GSMClient _gsm_client;
};
Expand Down