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
166182void 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
0 commit comments