Skip to content

Commit 8fa8abc

Browse files
author
Teppo Järvelin
committed
Cellular: Fixed connect-disconnect sequence can now be called multiple times.
1 parent 80e1093 commit 8fa8abc

File tree

8 files changed

+143
-125
lines changed

8 files changed

+143
-125
lines changed

features/cellular/easy_cellular/CellularConnectionFSM.cpp

Lines changed: 35 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -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,24 +250,6 @@ 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-
280253
void CellularConnectionFSM::report_failure(const char* msg)
281254
{
282255
tr_error("Cellular network failed: %s", msg);
@@ -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;
@@ -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_warning("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

features/cellular/easy_cellular/CellularConnectionFSM.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,8 @@ class CellularConnectionFSM
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();

0 commit comments

Comments
 (0)