@@ -47,7 +47,7 @@ CellularStateMachine::CellularStateMachine(CellularDevice &device, events::Event
47
47
_cellularDevice (device), _state(STATE_INIT), _next_state(_state), _target_state(_state),
48
48
_event_status_cb (0 ), _network(0 ), _queue(queue), _queue_thread(0 ), _sim_pin(0 ),
49
49
_retry_count (0 ), _event_timeout(-1 ), _event_id(-1 ), _plmn(0 ), _command_success(false ),
50
- _is_retry (false ), _cb_data(), _current_event(NSAPI_EVENT_CONNECTION_STATUS_CHANGE ), _status(0 )
50
+ _is_retry (false ), _cb_data(), _current_event(CellularDeviceReady ), _status(0 )
51
51
{
52
52
#if MBED_CONF_CELLULAR_RANDOM_MAX_START_DELAY == 0
53
53
_start_time = 0 ;
@@ -138,10 +138,8 @@ bool CellularStateMachine::open_sim()
138
138
}
139
139
140
140
// report current state so callback can set sim pin if needed
141
- if (_event_status_cb) {
142
- _cb_data.status_data = state;
143
- _event_status_cb ((nsapi_event_t )CellularSIMStatusChanged, (intptr_t )&_cb_data);
144
- }
141
+ _cb_data.status_data = state;
142
+ send_event_cb (CellularSIMStatusChanged);
145
143
146
144
if (state == CellularDevice::SimStatePinNeeded) {
147
145
if (_sim_pin) {
@@ -161,13 +159,10 @@ bool CellularStateMachine::open_sim()
161
159
bool sim_ready = state == CellularDevice::SimStateReady;
162
160
163
161
if (sim_ready) {
164
- // If plmn is set, we should it right after sim is opened so that registration is forced to correct network.
165
- if (_plmn && strlen (_plmn)) {
166
- _cb_data.error = _network->set_registration (_plmn);
167
- tr_debug (" STM: manual set_registration: %d, plmn: %s" , _cb_data.error , _plmn);
168
- if (_cb_data.error ) {
169
- return false ;
170
- }
162
+ _cb_data.error = _network->set_registration (_plmn);
163
+ tr_debug (" STM: set_registration: %d, plmn: %s" , _cb_data.error , _plmn);
164
+ if (_cb_data.error ) {
165
+ return false ;
171
166
}
172
167
}
173
168
@@ -255,18 +250,16 @@ void CellularStateMachine::report_failure(const char *msg)
255
250
tr_error (" CellularStateMachine failure: %s" , msg);
256
251
257
252
_event_id = -1 ;
258
- if (_event_status_cb) {
259
- _cb_data.final_try = true ;
260
- _event_status_cb (_current_event, (intptr_t )&_cb_data);
261
- }
253
+ _cb_data.final_try = true ;
254
+ send_event_cb (_current_event);
262
255
263
256
tr_error (" CellularStateMachine target state %s, current state %s" , get_state_string (_target_state), get_state_string (_state));
264
257
}
265
258
266
259
const char *CellularStateMachine::get_state_string (CellularState state) const
267
260
{
268
261
#if MBED_CONF_MBED_TRACE_ENABLE
269
- static const char *strings[STATE_MAX_FSM_STATE] = { " Init" , " Power" , " Device ready" , " SIM pin" , " Registering network" , " Attaching network" };
262
+ static const char *strings[STATE_MAX_FSM_STATE] = { " Init" , " Power" , " Device ready" , " SIM pin" , " Signal quality " , " Registering network" , " Attaching network" };
270
263
return strings[state];
271
264
#else
272
265
return NULL ;
@@ -287,12 +280,17 @@ void CellularStateMachine::retry_state_or_fail()
287
280
{
288
281
if (++_retry_count < CELLULAR_RETRY_ARRAY_SIZE) {
289
282
tr_debug (" %s: retry %d/%d" , get_state_string (_state), _retry_count, CELLULAR_RETRY_ARRAY_SIZE);
283
+ // send info to application/driver about error logic so it can implement proper error logic
284
+ _cb_data.status_data = _current_event;
285
+ _cb_data.data = &_retry_count;
286
+ _cb_data.error = NSAPI_ERROR_OK;
287
+ send_event_cb (CellularStateRetryEvent);
288
+
290
289
_event_timeout = _retry_timeout_array[_retry_count];
291
290
_is_retry = true ;
292
291
_cb_data.error = NSAPI_ERROR_OK;
293
292
} else {
294
293
report_failure (get_state_string (_state));
295
- return ;
296
294
}
297
295
}
298
296
@@ -343,9 +341,7 @@ bool CellularStateMachine::device_ready()
343
341
}
344
342
#endif // MBED_CONF_CELLULAR_DEBUG_AT
345
343
346
- if (_event_status_cb) {
347
- _event_status_cb ((nsapi_event_t )CellularDeviceReady, (intptr_t )&_cb_data);
348
- }
344
+ send_event_cb (CellularDeviceReady);
349
345
_cellularDevice.set_ready_cb (0 );
350
346
351
347
return true ;
@@ -411,12 +407,25 @@ void CellularStateMachine::state_sim_pin()
411
407
} else if (_cb_data.error ) {
412
408
tr_warning (" Packet domain event reporting set failed!" );
413
409
}
414
- enter_to_state (STATE_REGISTERING_NETWORK );
410
+ enter_to_state (STATE_SIGNAL_QUALITY );
415
411
} else {
416
412
retry_state_or_fail ();
417
413
}
418
414
}
419
415
416
+ void CellularStateMachine::state_signal_quality ()
417
+ {
418
+ _cb_data.error = _network->get_signal_quality (_signal_quality.rssi , &_signal_quality.ber );
419
+
420
+ if (_cb_data.error != NSAPI_ERROR_OK) {
421
+ retry_state_or_fail ();
422
+ } else {
423
+ _cb_data.data = &_signal_quality;
424
+ send_event_cb (_current_event);
425
+ enter_to_state (STATE_REGISTERING_NETWORK);
426
+ }
427
+ }
428
+
420
429
void CellularStateMachine::state_registering ()
421
430
{
422
431
_cellularDevice.set_timeout (TIMEOUT_NETWORK);
@@ -428,7 +437,7 @@ void CellularStateMachine::state_registering()
428
437
_cb_data.status_data = CellularNetwork::AlreadyRegistered;
429
438
}
430
439
_cb_data.error = NSAPI_ERROR_OK;
431
- _event_status_cb (_current_event, ( intptr_t )&_cb_data );
440
+ send_event_cb (_current_event);
432
441
// we are already registered, go to attach
433
442
enter_to_state (STATE_ATTACHING_NETWORK);
434
443
} else {
@@ -449,10 +458,8 @@ void CellularStateMachine::state_attaching()
449
458
_cb_data.error = _network->set_attach ();
450
459
}
451
460
if (_cb_data.error == NSAPI_ERROR_OK) {
452
- if (_event_status_cb) {
453
- _cb_data.status_data = CellularNetwork::Attached;
454
- _event_status_cb (_current_event, (intptr_t )&_cb_data);
455
- }
461
+ _cb_data.status_data = CellularNetwork::Attached;
462
+ send_event_cb (_current_event);
456
463
} else {
457
464
retry_state_or_fail ();
458
465
}
@@ -533,45 +540,51 @@ bool CellularStateMachine::get_current_status(CellularStateMachine::CellularStat
533
540
534
541
void CellularStateMachine::event ()
535
542
{
536
- #if MBED_CONF_MBED_TRACE_ENABLE
537
- if (_network) {
538
- int rssi;
539
- if (_network->get_signal_quality (rssi) == NSAPI_ERROR_OK) {
540
- if (rssi == CellularNetwork::SignalQualityUnknown) {
543
+ // Don't send Signal quality when in signal quality state or it can confuse callback functions when running retry logic
544
+ if (_network && _state != STATE_SIGNAL_QUALITY) {
545
+ _cb_data.error = _network->get_signal_quality (_signal_quality.rssi , &_signal_quality.ber );
546
+ _cb_data.data = &_signal_quality;
547
+
548
+ if (_cb_data.error == NSAPI_ERROR_OK) {
549
+ send_event_cb (CellularSignalQuality);
550
+ if (_signal_quality.rssi == CellularNetwork::SignalQualityUnknown) {
541
551
tr_info (" RSSI unknown" );
542
552
} else {
543
- tr_info (" RSSI %d dBm" , rssi);
553
+ tr_info (" RSSI %d dBm" , _signal_quality. rssi );
544
554
}
545
555
}
546
556
}
547
- #endif
548
557
549
558
_event_timeout = -1 ;
550
559
_is_retry = false ;
551
560
552
561
switch (_state) {
553
562
case STATE_INIT:
554
- _current_event = ( nsapi_event_t ) CellularDeviceReady;
563
+ _current_event = CellularDeviceReady;
555
564
state_init ();
556
565
break ;
557
566
case STATE_POWER_ON:
558
- _current_event = ( nsapi_event_t ) CellularDeviceReady;
567
+ _current_event = CellularDeviceReady;
559
568
state_power_on ();
560
569
break ;
561
570
case STATE_DEVICE_READY:
562
- _current_event = ( nsapi_event_t ) CellularDeviceReady;
571
+ _current_event = CellularDeviceReady;
563
572
state_device_ready ();
564
573
break ;
565
574
case STATE_SIM_PIN:
566
- _current_event = ( nsapi_event_t ) CellularSIMStatusChanged;
575
+ _current_event = CellularSIMStatusChanged;
567
576
state_sim_pin ();
568
577
break ;
578
+ case STATE_SIGNAL_QUALITY:
579
+ _current_event = CellularSignalQuality;
580
+ state_signal_quality ();
581
+ break ;
569
582
case STATE_REGISTERING_NETWORK:
570
- _current_event = ( nsapi_event_t ) CellularRegistrationStatusChanged;
583
+ _current_event = CellularRegistrationStatusChanged;
571
584
state_registering ();
572
585
break ;
573
586
case STATE_ATTACHING_NETWORK:
574
- _current_event = ( nsapi_event_t ) CellularAttachNetwork;
587
+ _current_event = CellularAttachNetwork;
575
588
state_attaching ();
576
589
break ;
577
590
default :
@@ -623,6 +636,13 @@ void CellularStateMachine::set_cellular_callback(mbed::Callback<void(nsapi_event
623
636
_event_status_cb = status_cb;
624
637
}
625
638
639
+ void CellularStateMachine::send_event_cb (cellular_connection_status_t status)
640
+ {
641
+ if (_event_status_cb) {
642
+ _event_status_cb ((nsapi_event_t )status, (intptr_t )&_cb_data);
643
+ }
644
+ }
645
+
626
646
bool CellularStateMachine::check_is_target_reached ()
627
647
{
628
648
if (((_target_state == _state || _target_state < _next_state) && _cb_data.error == NSAPI_ERROR_OK && !_is_retry) ||
@@ -640,7 +660,8 @@ bool CellularStateMachine::check_is_target_reached()
640
660
void CellularStateMachine::cellular_event_changed (nsapi_event_t ev, intptr_t ptr)
641
661
{
642
662
cell_callback_data_t *data = (cell_callback_data_t *)ptr;
643
- if ((cellular_connection_status_t )ev == CellularRegistrationStatusChanged && _state == STATE_REGISTERING_NETWORK) {
663
+ if ((cellular_connection_status_t )ev == CellularRegistrationStatusChanged && (
664
+ _state == STATE_REGISTERING_NETWORK || _state == STATE_SIGNAL_QUALITY)) {
644
665
// expect packet data so only these states are valid
645
666
CellularNetwork::registration_params_t reg_params;
646
667
nsapi_error_t err = _network->get_registration_params (reg_params);
0 commit comments