@@ -96,59 +96,14 @@ NetworkConnectionState NBConnectionHandler::check()
9696
9797 switch (_netConnectionState)
9898 {
99- case NetworkConnectionState::INIT:
100- {
101- if (_nb.begin (_pin, _apn, _login, _pass) == NB_READY)
102- {
103- Debug.print (DBG_INFO, " SIM card ok" );
104- _nb.setTimeout (NB_TIMEOUT);
105- changeConnectionState (NetworkConnectionState::CONNECTING);
106- }
107- else
108- {
109- Debug.print (DBG_ERROR, " SIM not present or wrong PIN" );
110- }
111- }
112- break ;
113-
114- case NetworkConnectionState::CONNECTING:
115- {
116- NB_NetworkStatus_t networkStatus;
117- networkStatus = _nb_gprs.attachGPRS (true );
118- Debug.print (DBG_DEBUG, " GPRS.attachGPRS(): %d" , networkStatus);
119- if (networkStatus == NB_NetworkStatus_t::ERROR)
120- {
121- changeConnectionState (NetworkConnectionState::ERROR);
122- return _netConnectionState;
123- }
124- Debug.print (DBG_INFO, " Connected to GPRS Network" );
125- changeConnectionState (NetworkConnectionState::CONNECTED);
126- return _netConnectionState;
127- }
128- break ;
129- case NetworkConnectionState::CONNECTED:
130- {
131- int const nb_is_access_alive = _nb.isAccessAlive ();
132- Debug.print (DBG_VERBOSE, " GPRS.isAccessAlive(): %d" , nb_is_access_alive);
133- if (nb_is_access_alive != 1 )
134- {
135- changeConnectionState (NetworkConnectionState::DISCONNECTED);
136- return _netConnectionState;
137- }
138- Debug.print (DBG_VERBOSE, " Connected to Cellular Network" );
139- }
140- break ;
141- case NetworkConnectionState::DISCONNECTED:
142- {
143- if (_keep_alive)
144- {
145- Debug.print (DBG_VERBOSE, " keep alive > INIT" );
146- changeConnectionState (NetworkConnectionState::INIT);
147- } else {
148- changeConnectionState (NetworkConnectionState::CLOSED);
149- }
150- }
151- break ;
99+ case NetworkConnectionState::INIT: _netConnectionState = update_handleInit (); break ;
100+ case NetworkConnectionState::CONNECTING: _netConnectionState = update_handleConnecting (); break ;
101+ case NetworkConnectionState::CONNECTED: _netConnectionState = update_handleConnected (); break ;
102+ case NetworkConnectionState::GETTIME: /* Unused */ break ;
103+ case NetworkConnectionState::DISCONNECTING: _netConnectionState = update_handleDisconnecting (); break ;
104+ case NetworkConnectionState::DISCONNECTED: _netConnectionState = update_handleDisconnected (); break ;
105+ case NetworkConnectionState::ERROR: break ;
106+ case NetworkConnectionState::CLOSED: break ;
152107 }
153108 }
154109
@@ -160,53 +115,84 @@ void NBConnectionHandler::connect()
160115 if (_netConnectionState != NetworkConnectionState::INIT && _netConnectionState != NetworkConnectionState::CONNECTING)
161116 {
162117 _keep_alive = true ;
163- changeConnectionState ( NetworkConnectionState::INIT) ;
118+ _netConnectionState = NetworkConnectionState::INIT;
164119 }
165120}
166121
167122void NBConnectionHandler::disconnect ()
168123{
169- changeConnectionState (NetworkConnectionState::DISCONNECTING);
170124 _keep_alive = false ;
125+ _netConnectionState = NetworkConnectionState::DISCONNECTING;
171126}
172127
173-
174128/* *****************************************************************************
175129 PRIVATE MEMBER FUNCTIONS
176130 ******************************************************************************/
177131
178- void NBConnectionHandler::changeConnectionState (NetworkConnectionState _newState) {
179- switch (_newState) {
180- case NetworkConnectionState::CONNECTING: {
181- Debug.print (DBG_INFO, " Connecting to Cellular Network" );
182- }
183- break ;
184- case NetworkConnectionState::CONNECTED: {
185- execCallback (NetworkConnectionEvent::CONNECTED);
186- }
187- break ;
188- case NetworkConnectionState::DISCONNECTING: {
189- Debug.print (DBG_VERBOSE, " Disconnecting from Cellular Network" );
190- _nb.shutdown ();
191- }
192- case NetworkConnectionState::DISCONNECTED: {
193- if (_netConnectionState == NetworkConnectionState::CONNECTED) {
194- execCallback (NetworkConnectionEvent::DISCONNECTED);
195- Debug.print (DBG_ERROR, " Disconnected from Cellular Network" );
196- Debug.print (DBG_ERROR, " Attempting reconnection" );
197- if (_keep_alive) {
198- Debug.print (DBG_ERROR, " Attempting reconnection" );
199- }
200- }
201- }
202- break ;
203- case NetworkConnectionState::ERROR: {
204- execCallback (NetworkConnectionEvent::ERROR);
205- Debug.print (DBG_ERROR, " GPRS attach failed\n\r Make sure the antenna is connected and reset your board." );
206- }
207- break ;
132+ NetworkConnectionState NBConnectionHandler::update_handleInit ()
133+ {
134+ if (_nb.begin (_pin, _apn, _login, _pass) == NB_READY)
135+ {
136+ Debug.print (DBG_INFO, " SIM card ok" );
137+ _nb.setTimeout (NB_TIMEOUT);
138+ return NetworkConnectionState::CONNECTING;
139+ }
140+ else
141+ {
142+ Debug.print (DBG_ERROR, " SIM not present or wrong PIN" );
143+ return NetworkConnectionState::ERROR;
144+ }
145+ }
146+
147+ NetworkConnectionState NBConnectionHandler::update_handleConnecting ()
148+ {
149+ NB_NetworkStatus_t const network_status = _nb_gprs.attachGPRS (true );
150+ Debug.print (DBG_DEBUG, " GPRS.attachGPRS(): %d" , network_status);
151+ if (network_status == NB_NetworkStatus_t::ERROR)
152+ {
153+ Debug.print (DBG_ERROR, " GPRS.attachGPRS() failed" );
154+ return NetworkConnectionState::ERROR;
155+ }
156+ else
157+ {
158+ Debug.print (DBG_INFO, " Connected to GPRS Network" );
159+ return NetworkConnectionState::CONNECTED;
160+ }
161+ }
162+
163+ NetworkConnectionState NBConnectionHandler::update_handleConnected ()
164+ {
165+ int const nb_is_access_alive = _nb.isAccessAlive ();
166+ Debug.print (DBG_VERBOSE, " GPRS.isAccessAlive(): %d" , nb_is_access_alive);
167+ if (nb_is_access_alive != 1 )
168+ {
169+ Debug.print (DBG_INFO, " Disconnected from cellular network" );
170+ return NetworkConnectionState::DISCONNECTED;
171+ }
172+ else
173+ {
174+ Debug.print (DBG_VERBOSE, " Connected to Cellular Network" );
175+ return NetworkConnectionState::CONNECTED;
176+ }
177+ }
178+
179+ NetworkConnectionState NBConnectionHandler::update_handleDisconnecting ()
180+ {
181+ Debug.print (DBG_VERBOSE, " Disconnecting from Cellular Network" );
182+ _nb.shutdown ();
183+ return NetworkConnectionState::DISCONNECTED;
184+ }
185+
186+ NetworkConnectionState NBConnectionHandler::update_handleDisconnected ()
187+ {
188+ if (_keep_alive)
189+ {
190+ return NetworkConnectionState::INIT;
191+ }
192+ else
193+ {
194+ return NetworkConnectionState::CLOSED;
208195 }
209- _netConnectionState = _newState;
210196}
211197
212198#endif /* #ifdef BOARD_HAS_NB */
0 commit comments