Skip to content

Commit 7b4b50a

Browse files
committed
Using table for check interval times instead of member variable
1 parent 7e7121d commit 7b4b50a

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

src/Arduino_NBConnectionHandler.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,19 @@
3636
CONSTANTS
3737
******************************************************************************/
3838

39-
static const unsigned long NETWORK_CONNECTION_INTERVAL = 30000;
39+
static int const NB_TIMEOUT = 30000;
40+
41+
static unsigned int const CHECK_INTERVAL_TABLE[] =
42+
{
43+
/* INIT */ 100,
44+
/* CONNECTING */ 500,
45+
/* CONNECTED */ 10000,
46+
/* GETTIME */ 100,
47+
/* DISCONNECTING */ 100,
48+
/* DISCONNECTED */ 1000,
49+
/* CLOSED */ 1000,
50+
/* ERROR */ 1000
51+
};
4052

4153
/******************************************************************************
4254
CTOR/DTOR
@@ -58,8 +70,7 @@ NBConnectionHandler::NBConnectionHandler(char const * pin, char const * apn, cha
5870
, _apn(apn)
5971
, _login(login)
6072
, _pass(pass)
61-
, lastConnectionTickTime(millis())
62-
, connectionTickTimeInterval(CHECK_INTERVAL_IDLE)
73+
, _lastConnectionTickTime(millis())
6374
, _keep_alive(keep_alive)
6475
{
6576

@@ -73,15 +84,21 @@ unsigned long NBConnectionHandler::getTime() {
7384
return _nb.getTime();
7485
}
7586

76-
NetworkConnectionState NBConnectionHandler::check() {
87+
NetworkConnectionState NBConnectionHandler::check()
88+
{
89+
7790
unsigned long const now = millis();
78-
int _nbAlive;
79-
if (now - lastConnectionTickTime > connectionTickTimeInterval) {
91+
unsigned int const connectionTickTimeInterval = CHECK_INTERVAL_TABLE[static_cast<unsigned int>(_netConnectionState)];
92+
93+
if((now - _lastConnectionTickTime) > connectionTickTimeInterval)
94+
{
95+
_lastConnectionTickTime = now;
96+
8097
switch (_netConnectionState) {
8198
case NetworkConnectionState::INIT: {
8299
if (_nb.begin(_pin, _apn, _login, _pass) == NB_READY) {
83100
Debug.print(DBG_INFO, "SIM card ok");
84-
_nb.setTimeout(CHECK_INTERVAL_RETRYING);
101+
_nb.setTimeout(NB_TIMEOUT);
85102
changeConnectionState(NetworkConnectionState::CONNECTING);
86103
} else {
87104
Debug.print(DBG_ERROR, "SIM not present or wrong PIN");
@@ -116,9 +133,9 @@ NetworkConnectionState NBConnectionHandler::check() {
116133
}
117134
break;
118135
case NetworkConnectionState::CONNECTED: {
119-
_nbAlive = _nb.isAccessAlive();
120-
Debug.print(DBG_VERBOSE, "GPRS.isAccessAlive(): %d", _nbAlive);
121-
if (_nbAlive != 1) {
136+
int const nb_is_access_alive = _nb.isAccessAlive();
137+
Debug.print(DBG_VERBOSE, "GPRS.isAccessAlive(): %d", nb_is_access_alive);
138+
if (nb_is_access_alive != 1) {
122139
changeConnectionState(NetworkConnectionState::DISCONNECTED);
123140
return _netConnectionState;
124141
}
@@ -137,7 +154,6 @@ NetworkConnectionState NBConnectionHandler::check() {
137154
}
138155
break;
139156
}
140-
lastConnectionTickTime = now;
141157
}
142158

143159
return _netConnectionState;
@@ -164,23 +180,13 @@ void NBConnectionHandler::disconnect()
164180
******************************************************************************/
165181

166182
void NBConnectionHandler::changeConnectionState(NetworkConnectionState _newState) {
167-
int newInterval = CHECK_INTERVAL_IDLE;
168183
switch (_newState) {
169-
case NetworkConnectionState::INIT: {
170-
newInterval = CHECK_INTERVAL_INIT;
171-
}
172-
break;
173184
case NetworkConnectionState::CONNECTING: {
174185
Debug.print(DBG_INFO, "Connecting to Cellular Network");
175-
newInterval = CHECK_INTERVAL_CONNECTING;
176186
}
177187
break;
178188
case NetworkConnectionState::CONNECTED: {
179189
execCallback(NetworkConnectionEvent::CONNECTED);
180-
newInterval = CHECK_INTERVAL_CONNECTED;
181-
}
182-
break;
183-
case NetworkConnectionState::GETTIME: {
184190
}
185191
break;
186192
case NetworkConnectionState::DISCONNECTING: {
@@ -196,7 +202,6 @@ void NBConnectionHandler::changeConnectionState(NetworkConnectionState _newState
196202
Debug.print(DBG_ERROR, "Attempting reconnection");
197203
}
198204
}
199-
newInterval = CHECK_INTERVAL_DISCONNECTED;
200205
}
201206
break;
202207
case NetworkConnectionState::ERROR: {
@@ -205,8 +210,6 @@ void NBConnectionHandler::changeConnectionState(NetworkConnectionState _newState
205210
}
206211
break;
207212
}
208-
connectionTickTimeInterval = newInterval;
209-
lastConnectionTickTime = millis();
210213
_netConnectionState = _newState;
211214
}
212215

src/Arduino_NBConnectionHandler.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,11 @@ class NBConnectionHandler : public ConnectionHandler {
4747

4848
void changeConnectionState(NetworkConnectionState _newState);
4949

50-
const int CHECK_INTERVAL_IDLE = 100;
51-
const int CHECK_INTERVAL_INIT = 100;
52-
const int CHECK_INTERVAL_CONNECTING = 500;
53-
const int CHECK_INTERVAL_CONNECTED = 10000;
54-
const int CHECK_INTERVAL_RETRYING = 30000;
55-
const int CHECK_INTERVAL_DISCONNECTED = 1000;
56-
const int CHECK_INTERVAL_ERROR = 500;
57-
5850
char const * _pin;
5951
char const * _apn;
6052
char const * _login;
6153
char const * _pass;
62-
unsigned long lastConnectionTickTime;
63-
int connectionTickTimeInterval;
54+
unsigned long _lastConnectionTickTime;
6455
bool _keep_alive;
6556

6657
NB _nb;

0 commit comments

Comments
 (0)