Skip to content

Commit 917fe08

Browse files
author
Cruz Monrreal
authored
Merge pull request #6928 from jarvte/fix_cellular_connect_disconnect
Cellular: Fixed con-disc sequence can now be called multiple times.
2 parents 5d8570b + 03f50e6 commit 917fe08

File tree

8 files changed

+167
-142
lines changed

8 files changed

+167
-142
lines changed

features/cellular/easy_cellular/CellularConnectionFSM.cpp

Lines changed: 43 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ bool CellularConnectionFSM::power_on()
144144
return true;
145145
}
146146

147-
void CellularConnectionFSM::set_sim_pin(const char * sim_pin)
147+
void CellularConnectionFSM::set_sim_pin(const char *sim_pin)
148148
{
149149
strncpy(_sim_pin, sim_pin, sizeof(_sim_pin));
150150
_sim_pin[sizeof(_sim_pin)-1] = '\0';
151151
}
152152

153-
void CellularConnectionFSM::set_plmn(const char* plmn)
153+
void CellularConnectionFSM::set_plmn(const char *plmn)
154154
{
155155
_plmn = plmn;
156156
}
@@ -184,15 +184,6 @@ bool CellularConnectionFSM::open_sim()
184184
return state == CellularSIM::SimStateReady;
185185
}
186186

187-
bool CellularConnectionFSM::set_network_registration()
188-
{
189-
if (_network->set_registration(_plmn) != NSAPI_ERROR_OK) {
190-
tr_error("Failed to set network registration.");
191-
return false;
192-
}
193-
return true;
194-
}
195-
196187
bool CellularConnectionFSM::is_registered()
197188
{
198189
CellularNetwork::RegistrationStatus status;
@@ -259,33 +250,15 @@ bool CellularConnectionFSM::get_network_registration(CellularNetwork::Registrati
259250
return true;
260251
}
261252

262-
bool CellularConnectionFSM::get_attach_network(CellularNetwork::AttachStatus &status)
263-
{
264-
nsapi_error_t err = _network->get_attach(status);
265-
if (err != NSAPI_ERROR_OK) {
266-
return false;
267-
}
268-
return true;
269-
}
270-
271-
bool CellularConnectionFSM::set_attach_network()
272-
{
273-
nsapi_error_t attach_err = _network->set_attach();
274-
if (attach_err != NSAPI_ERROR_OK) {
275-
return false;
276-
}
277-
return true;
278-
}
279-
280-
void CellularConnectionFSM::report_failure(const char* msg)
253+
void CellularConnectionFSM::report_failure(const char *msg)
281254
{
282255
tr_error("Cellular network failed: %s", msg);
283256
if (_status_callback) {
284257
_status_callback(_state, _next_state);
285258
}
286259
}
287260

288-
const char* CellularConnectionFSM::get_state_string(CellularState state)
261+
const char *CellularConnectionFSM::get_state_string(CellularState state)
289262
{
290263
#if MBED_CONF_MBED_TRACE_ENABLE
291264
static const char *strings[] = { "Init", "Power", "Device ready", "SIM pin", "Registering network", "Manual registering", "Attaching network", "Activating PDP Context", "Connecting network", "Connected"};
@@ -295,17 +268,6 @@ const char* CellularConnectionFSM::get_state_string(CellularState state)
295268
#endif // #if MBED_CONF_MBED_TRACE_ENABLE
296269
}
297270

298-
nsapi_error_t CellularConnectionFSM::is_automatic_registering(bool& auto_reg)
299-
{
300-
CellularNetwork::NWRegisteringMode mode;
301-
nsapi_error_t err = _network->get_network_registering_mode(mode);
302-
if (err == NSAPI_ERROR_OK) {
303-
tr_debug("automatic registering mode: %d", mode);
304-
auto_reg = (mode == CellularNetwork::NWModeAutomatic);
305-
}
306-
return err;
307-
}
308-
309271
bool CellularConnectionFSM::is_registered_to_plmn()
310272
{
311273
int format;
@@ -325,7 +287,7 @@ bool CellularConnectionFSM::is_registered_to_plmn()
325287
CellularNetwork::operator_names_list names_list;
326288
nsapi_error_t err = _network->get_operator_names(names_list);
327289
if (err == NSAPI_ERROR_OK) {
328-
CellularNetwork::operator_names_t* op_names = names_list.get_head();
290+
CellularNetwork::operator_names_t *op_names = names_list.get_head();
329291
bool found_match = false;
330292
while (op_names) {
331293
if (format == 0) {
@@ -407,9 +369,18 @@ void CellularConnectionFSM::retry_state_or_fail()
407369

408370
void CellularConnectionFSM::state_init()
409371
{
410-
_event_timeout = _start_time;
411-
tr_info("Init state, waiting %d ms before POWER state)", _start_time);
412-
enter_to_state(STATE_POWER_ON);
372+
// we should check that if power is already on then we can jump to device ready state
373+
_cellularDevice->set_timeout(TIMEOUT_POWER_ON);
374+
tr_info("Cellular state init (timeout %d ms)", TIMEOUT_POWER_ON);
375+
nsapi_error_t err = _power->is_device_ready();
376+
if (err != NSAPI_ERROR_OK) {
377+
_event_timeout = _start_time;
378+
tr_info("Init state, waiting %d ms before POWER state)", _start_time);
379+
enter_to_state(STATE_POWER_ON);
380+
} else {
381+
tr_info("Device was ready to accept commands, jump to device ready");
382+
enter_to_state(STATE_DEVICE_READY);
383+
}
413384
}
414385

415386
void CellularConnectionFSM::state_power_on()
@@ -424,37 +395,21 @@ void CellularConnectionFSM::state_power_on()
424395
}
425396
}
426397

427-
bool CellularConnectionFSM::device_ready()
398+
void CellularConnectionFSM::device_ready()
428399
{
429400
tr_info("Cellular device ready");
430401
if (_event_status_cb) {
431402
_event_status_cb((nsapi_event_t)CellularDeviceReady, 0);
432403
}
433-
434404
_power->remove_device_ready_urc_cb(mbed::callback(this, &CellularConnectionFSM::ready_urc_cb));
435-
436-
bool success = false;
437-
for (int type = 0; type < CellularNetwork::C_MAX; type++) {
438-
if (!_network->set_registration_urc((CellularNetwork::RegistrationType)type, true)) {
439-
success = true;
440-
}
441-
}
442-
if (!success) {
443-
tr_error("Failed to set any URC's for registration");
444-
report_failure(get_state_string(_state));
445-
return false;
446-
}
447-
448-
return true;
449405
}
450406

451407
void CellularConnectionFSM::state_device_ready()
452408
{
453409
_cellularDevice->set_timeout(TIMEOUT_POWER_ON);
454410
if (_power->set_at_mode() == NSAPI_ERROR_OK) {
455-
if (device_ready()) {
456-
enter_to_state(STATE_SIM_PIN);
457-
}
411+
device_ready();
412+
enter_to_state(STATE_SIM_PIN);
458413
} else {
459414
if (_retry_count == 0) {
460415
(void)_power->set_device_ready_urc_cb(mbed::callback(this, &CellularConnectionFSM::ready_urc_cb));
@@ -468,6 +423,18 @@ void CellularConnectionFSM::state_sim_pin()
468423
_cellularDevice->set_timeout(TIMEOUT_SIM_PIN);
469424
tr_info("Sim state (timeout %d ms)", TIMEOUT_SIM_PIN);
470425
if (open_sim()) {
426+
bool success = false;
427+
for (int type = 0; type < CellularNetwork::C_MAX; type++) {
428+
if (!_network->set_registration_urc((CellularNetwork::RegistrationType)type, true)) {
429+
success = true;
430+
}
431+
}
432+
if (!success) {
433+
tr_warn("Failed to set any URC's for registration");
434+
retry_state_or_fail();
435+
return;
436+
}
437+
471438
if (_plmn) {
472439
enter_to_state(STATE_MANUAL_REGISTERING_NETWORK);
473440
} else {
@@ -485,12 +452,9 @@ void CellularConnectionFSM::state_registering()
485452
// we are already registered, go to attach
486453
enter_to_state(STATE_ATTACHING_NETWORK);
487454
} else {
488-
bool auto_reg = false;
489-
nsapi_error_t err = is_automatic_registering(auto_reg);
490-
if (err == NSAPI_ERROR_OK && !auto_reg) {
491-
// automatic registering is not on, set registration and retry
492-
_cellularDevice->set_timeout(TIMEOUT_REGISTRATION);
493-
set_network_registration();
455+
_cellularDevice->set_timeout(TIMEOUT_REGISTRATION);
456+
if (!_command_success) {
457+
_command_success = (_network->set_registration() == NSAPI_ERROR_OK);
494458
}
495459
retry_state_or_fail();
496460
}
@@ -507,7 +471,7 @@ void CellularConnectionFSM::state_manual_registering_network()
507471
enter_to_state(STATE_ATTACHING_NETWORK);
508472
} else {
509473
if (!_command_success) {
510-
_command_success = set_network_registration();
474+
_command_success = (_network->set_registration(_plmn) == NSAPI_ERROR_OK);
511475
}
512476
retry_state_or_fail();
513477
}
@@ -517,16 +481,8 @@ void CellularConnectionFSM::state_manual_registering_network()
517481
void CellularConnectionFSM::state_attaching()
518482
{
519483
_cellularDevice->set_timeout(TIMEOUT_CONNECT);
520-
CellularNetwork::AttachStatus attach_status;
521-
if (get_attach_network(attach_status)) {
522-
if (attach_status == CellularNetwork::Attached) {
523-
enter_to_state(STATE_ACTIVATING_PDP_CONTEXT);
524-
} else {
525-
if (!_command_success) {
526-
_command_success = set_attach_network();
527-
}
528-
retry_state_or_fail();
529-
}
484+
if (_network->set_attach() == NSAPI_ERROR_OK) {
485+
enter_to_state(STATE_ACTIVATING_PDP_CONTEXT);
530486
} else {
531487
retry_state_or_fail();
532488
}
@@ -701,9 +657,8 @@ void CellularConnectionFSM::ready_urc_cb()
701657
if (_state == STATE_DEVICE_READY && _power->set_at_mode() == NSAPI_ERROR_OK) {
702658
tr_debug("State was STATE_DEVICE_READY and at mode ready, cancel state and move to next");
703659
_queue.cancel(_event_id);
704-
if (device_ready()) {
705-
continue_from_state(STATE_SIM_PIN);
706-
}
660+
device_ready();
661+
continue_from_state(STATE_SIM_PIN);
707662
}
708663
}
709664

@@ -712,17 +667,17 @@ events::EventQueue *CellularConnectionFSM::get_queue()
712667
return &_queue;
713668
}
714669

715-
CellularNetwork* CellularConnectionFSM::get_network()
670+
CellularNetwork *CellularConnectionFSM::get_network()
716671
{
717672
return _network;
718673
}
719674

720-
CellularDevice* CellularConnectionFSM::get_device()
675+
CellularDevice *CellularConnectionFSM::get_device()
721676
{
722677
return _cellularDevice;
723678
}
724679

725-
CellularSIM* CellularConnectionFSM::get_sim()
680+
CellularSIM *CellularConnectionFSM::get_sim()
726681
{
727682
return _sim;
728683
}

features/cellular/easy_cellular/CellularConnectionFSM.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class CellularConnectionFSM
9595
/** Get event queue that can be chained to main event queue (or use start_dispatch)
9696
* @return event queue
9797
*/
98-
events::EventQueue* get_queue();
98+
events::EventQueue *get_queue();
9999

100100
/** Start event queue dispatching
101101
* @return see nsapi_error_t, 0 on success
@@ -110,17 +110,17 @@ class CellularConnectionFSM
110110
/** Get cellular network interface
111111
* @return network interface, NULL on failure
112112
*/
113-
CellularNetwork* get_network();
113+
CellularNetwork *get_network();
114114

115115
/** Get cellular device interface
116116
* @return device interface, NULL on failure
117117
*/
118-
CellularDevice* get_device();
118+
CellularDevice *get_device();
119119

120120
/** Get cellular sim interface
121121
* @return sim interface, NULL on failure
122122
*/
123-
CellularSIM* get_sim();
123+
CellularSIM *get_sim();
124124

125125
/** Change cellular connection to the target state
126126
* @param state to continue. Default is to connect.
@@ -153,18 +153,14 @@ class CellularConnectionFSM
153153
* @param state state which is returned in string format
154154
* @return string format of the given state
155155
*/
156-
const char* get_state_string(CellularState state);
156+
const char *get_state_string(CellularState state);
157157

158158
private:
159159
bool power_on();
160160
bool open_sim();
161161
bool get_network_registration(CellularNetwork::RegistrationType type, CellularNetwork::RegistrationStatus &status, bool &is_registered);
162-
bool set_network_registration();
163-
bool get_attach_network(CellularNetwork::AttachStatus &status);
164-
bool set_attach_network();
165162
bool is_registered();
166-
bool device_ready();
167-
nsapi_error_t is_automatic_registering(bool& auto_reg);
163+
void device_ready();
168164

169165
// state functions to keep state machine simple
170166
void state_init();
@@ -215,7 +211,7 @@ class CellularConnectionFSM
215211
events::EventQueue _at_queue;
216212
char _st_string[20];
217213
int _event_id;
218-
const char* _plmn;
214+
const char *_plmn;
219215
bool _command_success;
220216
bool _plmn_network_found;
221217
};

0 commit comments

Comments
 (0)