@@ -144,13 +144,13 @@ bool CellularConnectionFSM::power_on()
144
144
return true ;
145
145
}
146
146
147
- void CellularConnectionFSM::set_sim_pin (const char * sim_pin)
147
+ void CellularConnectionFSM::set_sim_pin (const char *sim_pin)
148
148
{
149
149
strncpy (_sim_pin, sim_pin, sizeof (_sim_pin));
150
150
_sim_pin[sizeof (_sim_pin)-1 ] = ' \0 ' ;
151
151
}
152
152
153
- void CellularConnectionFSM::set_plmn (const char * plmn)
153
+ void CellularConnectionFSM::set_plmn (const char * plmn)
154
154
{
155
155
_plmn = plmn;
156
156
}
@@ -184,15 +184,6 @@ bool CellularConnectionFSM::open_sim()
184
184
return state == CellularSIM::SimStateReady;
185
185
}
186
186
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
-
196
187
bool CellularConnectionFSM::is_registered ()
197
188
{
198
189
CellularNetwork::RegistrationStatus status;
@@ -259,33 +250,15 @@ bool CellularConnectionFSM::get_network_registration(CellularNetwork::Registrati
259
250
return true ;
260
251
}
261
252
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)
281
254
{
282
255
tr_error (" Cellular network failed: %s" , msg);
283
256
if (_status_callback) {
284
257
_status_callback (_state, _next_state);
285
258
}
286
259
}
287
260
288
- const char * CellularConnectionFSM::get_state_string (CellularState state)
261
+ const char * CellularConnectionFSM::get_state_string (CellularState state)
289
262
{
290
263
#if MBED_CONF_MBED_TRACE_ENABLE
291
264
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)
295
268
#endif // #if MBED_CONF_MBED_TRACE_ENABLE
296
269
}
297
270
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
-
309
271
bool CellularConnectionFSM::is_registered_to_plmn ()
310
272
{
311
273
int format;
@@ -325,7 +287,7 @@ bool CellularConnectionFSM::is_registered_to_plmn()
325
287
CellularNetwork::operator_names_list names_list;
326
288
nsapi_error_t err = _network->get_operator_names (names_list);
327
289
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 ();
329
291
bool found_match = false ;
330
292
while (op_names) {
331
293
if (format == 0 ) {
@@ -407,9 +369,18 @@ void CellularConnectionFSM::retry_state_or_fail()
407
369
408
370
void CellularConnectionFSM::state_init ()
409
371
{
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
+ }
413
384
}
414
385
415
386
void CellularConnectionFSM::state_power_on ()
@@ -424,37 +395,21 @@ void CellularConnectionFSM::state_power_on()
424
395
}
425
396
}
426
397
427
- bool CellularConnectionFSM::device_ready ()
398
+ void CellularConnectionFSM::device_ready ()
428
399
{
429
400
tr_info (" Cellular device ready" );
430
401
if (_event_status_cb) {
431
402
_event_status_cb ((nsapi_event_t )CellularDeviceReady, 0 );
432
403
}
433
-
434
404
_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 ;
449
405
}
450
406
451
407
void CellularConnectionFSM::state_device_ready ()
452
408
{
453
409
_cellularDevice->set_timeout (TIMEOUT_POWER_ON);
454
410
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);
458
413
} else {
459
414
if (_retry_count == 0 ) {
460
415
(void )_power->set_device_ready_urc_cb (mbed::callback (this , &CellularConnectionFSM::ready_urc_cb));
@@ -468,6 +423,18 @@ void CellularConnectionFSM::state_sim_pin()
468
423
_cellularDevice->set_timeout (TIMEOUT_SIM_PIN);
469
424
tr_info (" Sim state (timeout %d ms)" , TIMEOUT_SIM_PIN);
470
425
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
+
471
438
if (_plmn) {
472
439
enter_to_state (STATE_MANUAL_REGISTERING_NETWORK);
473
440
} else {
@@ -485,12 +452,9 @@ void CellularConnectionFSM::state_registering()
485
452
// we are already registered, go to attach
486
453
enter_to_state (STATE_ATTACHING_NETWORK);
487
454
} 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);
494
458
}
495
459
retry_state_or_fail ();
496
460
}
@@ -507,7 +471,7 @@ void CellularConnectionFSM::state_manual_registering_network()
507
471
enter_to_state (STATE_ATTACHING_NETWORK);
508
472
} else {
509
473
if (!_command_success) {
510
- _command_success = set_network_registration ( );
474
+ _command_success = (_network-> set_registration (_plmn) == NSAPI_ERROR_OK );
511
475
}
512
476
retry_state_or_fail ();
513
477
}
@@ -517,16 +481,8 @@ void CellularConnectionFSM::state_manual_registering_network()
517
481
void CellularConnectionFSM::state_attaching ()
518
482
{
519
483
_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);
530
486
} else {
531
487
retry_state_or_fail ();
532
488
}
@@ -701,9 +657,8 @@ void CellularConnectionFSM::ready_urc_cb()
701
657
if (_state == STATE_DEVICE_READY && _power->set_at_mode () == NSAPI_ERROR_OK) {
702
658
tr_debug (" State was STATE_DEVICE_READY and at mode ready, cancel state and move to next" );
703
659
_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);
707
662
}
708
663
}
709
664
@@ -712,17 +667,17 @@ events::EventQueue *CellularConnectionFSM::get_queue()
712
667
return &_queue;
713
668
}
714
669
715
- CellularNetwork* CellularConnectionFSM::get_network ()
670
+ CellularNetwork * CellularConnectionFSM::get_network ()
716
671
{
717
672
return _network;
718
673
}
719
674
720
- CellularDevice* CellularConnectionFSM::get_device ()
675
+ CellularDevice * CellularConnectionFSM::get_device ()
721
676
{
722
677
return _cellularDevice;
723
678
}
724
679
725
- CellularSIM* CellularConnectionFSM::get_sim ()
680
+ CellularSIM * CellularConnectionFSM::get_sim ()
726
681
{
727
682
return _sim;
728
683
}
0 commit comments