Skip to content

Commit 4893200

Browse files
authored
Merge pull request #131 from pennam/catm1-retry
CatM1: allow retry if connection fails
2 parents 34a52e4 + c38bc16 commit 4893200

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/CatM1ConnectionHandler.cpp

+20-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ CatM1ConnectionHandler::CatM1ConnectionHandler(
3737
: ConnectionHandler{keep_alive, NetworkAdapter::CATM1}
3838
{
3939
_settings.type = NetworkAdapter::CATM1;
40+
// To keep the backward compatibility, the user can call enableCheckInternetAvailability(false) for disabling the check
41+
_check_internet_availability = true;
4042
strncpy(_settings.catm1.pin, pin, sizeof(_settings.catm1.pin)-1);
4143
strncpy(_settings.catm1.apn, apn, sizeof(_settings.catm1.apn)-1);
4244
strncpy(_settings.catm1.login, login, sizeof(_settings.catm1.login)-1);
4345
strncpy(_settings.catm1.pass, pass, sizeof(_settings.catm1.pass)-1);
4446
_settings.catm1.rat = static_cast<uint8_t>(rat);
4547
_settings.catm1.band = band;
48+
_reset = false;
4649
}
4750

4851
/******************************************************************************
@@ -51,7 +54,10 @@ CatM1ConnectionHandler::CatM1ConnectionHandler(
5154

5255
unsigned long CatM1ConnectionHandler::getTime()
5356
{
54-
return GSM.getTime();
57+
/* It is not safe to call GSM.getTime() since we don't know if modem internal
58+
* RTC is in sync with current time.
59+
*/
60+
return 0;
5561
}
5662

5763
/******************************************************************************
@@ -61,6 +67,7 @@ unsigned long CatM1ConnectionHandler::getTime()
6167
NetworkConnectionState CatM1ConnectionHandler::update_handleInit()
6268
{
6369
#if defined (ARDUINO_EDGE_CONTROL)
70+
/* Power on module */
6471
pinMode(ON_MKR2, OUTPUT);
6572
digitalWrite(ON_MKR2, HIGH);
6673
#endif
@@ -71,31 +78,36 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleInit()
7178
_settings.catm1.login,
7279
_settings.catm1.pass,
7380
static_cast<RadioAccessTechnologyType>(_settings.catm1.rat) ,
74-
_settings.catm1.band))
81+
_settings.catm1.band,
82+
_reset))
7583
{
7684
Debug.print(DBG_ERROR, F("The board was not able to register to the network..."));
77-
return NetworkConnectionState::ERROR;
85+
_reset = true;
86+
return NetworkConnectionState::DISCONNECTED;
7887
}
88+
_reset = false;
7989
return NetworkConnectionState::CONNECTING;
8090
}
8191

8292
NetworkConnectionState CatM1ConnectionHandler::update_handleConnecting()
8393
{
8494
if (!GSM.isConnected())
8595
{
86-
return NetworkConnectionState::INIT;
96+
Debug.print(DBG_ERROR, F("GSM connection not alive... disconnecting"));
97+
return NetworkConnectionState::DISCONNECTED;
8798
}
8899

89100
if(!_check_internet_availability){
90101
return NetworkConnectionState::CONNECTED;
91102
}
92103

93-
int ping_result = GSM.ping("time.arduino.cc");
104+
Debug.print(DBG_INFO, F("Sending PING to outer space..."));
105+
int const ping_result = GSM.ping("time.arduino.cc");
94106
Debug.print(DBG_INFO, F("GSM.ping(): %d"), ping_result);
95107
if (ping_result < 0)
96108
{
97109
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)]);
110+
Debug.print(DBG_INFO, F("Retrying in \"%d\" milliseconds"), 2 * CHECK_INTERVAL_TABLE[static_cast<unsigned int>(NetworkConnectionState::CONNECTING)]);
99111
return NetworkConnectionState::CONNECTING;
100112
}
101113
else
@@ -110,6 +122,7 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleConnected()
110122
int const is_gsm_access_alive = GSM.isConnected();
111123
if (is_gsm_access_alive != 1)
112124
{
125+
Debug.print(DBG_ERROR, F("GSM connection not alive... disconnecting"));
113126
return NetworkConnectionState::DISCONNECTED;
114127
}
115128
return NetworkConnectionState::CONNECTED;
@@ -123,6 +136,7 @@ NetworkConnectionState CatM1ConnectionHandler::update_handleDisconnecting()
123136

124137
NetworkConnectionState CatM1ConnectionHandler::update_handleDisconnected()
125138
{
139+
GSM.end();
126140
if (_keep_alive)
127141
{
128142
return NetworkConnectionState::INIT;

src/CatM1ConnectionHandler.h

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class CatM1ConnectionHandler : public ConnectionHandler
6060

6161
private:
6262

63+
bool _reset;
64+
6365
GSMUDP _gsm_udp;
6466
GSMClient _gsm_client;
6567
};

0 commit comments

Comments
 (0)