@@ -47,7 +47,7 @@ CellularStateMachine::CellularStateMachine(CellularDevice &device, events::Event
4747 _cellularDevice (device), _state(STATE_INIT), _next_state(_state), _target_state(_state),
4848 _event_status_cb (0 ), _network(nw), _queue(queue), _queue_thread(0 ), _sim_pin(0 ),
4949 _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 )
5151{
5252#if MBED_CONF_CELLULAR_RANDOM_MAX_START_DELAY == 0
5353 _start_time = 0 ;
@@ -133,10 +133,8 @@ bool CellularStateMachine::open_sim()
133133 }
134134
135135 // report current state so callback can set sim pin if needed
136- if (_event_status_cb) {
137- _cb_data.status_data = state;
138- _event_status_cb ((nsapi_event_t )CellularSIMStatusChanged, (intptr_t )&_cb_data);
139- }
136+ _cb_data.status_data = state;
137+ send_event_cb (CellularSIMStatusChanged);
140138
141139 if (state == CellularDevice::SimStatePinNeeded) {
142140 if (_sim_pin) {
@@ -156,13 +154,10 @@ bool CellularStateMachine::open_sim()
156154 bool sim_ready = state == CellularDevice::SimStateReady;
157155
158156 if (sim_ready) {
159- // If plmn is set, we should it right after sim is opened so that registration is forced to correct network.
160- if (_plmn && strlen (_plmn)) {
161- _cb_data.error = _network.set_registration (_plmn);
162- tr_debug (" STM: manual set_registration: %d, plmn: %s" , _cb_data.error , _plmn);
163- if (_cb_data.error ) {
164- return false ;
165- }
157+ _cb_data.error = _network.set_registration (_plmn);
158+ tr_debug (" STM: set_registration: %d, plmn: %s" , _cb_data.error , _plmn);
159+ if (_cb_data.error ) {
160+ return false ;
166161 }
167162 }
168163
@@ -250,18 +245,16 @@ void CellularStateMachine::report_failure(const char *msg)
250245 tr_error (" CellularStateMachine failure: %s" , msg);
251246
252247 _event_id = -1 ;
253- if (_event_status_cb) {
254- _cb_data.final_try = true ;
255- _event_status_cb (_current_event, (intptr_t )&_cb_data);
256- }
248+ _cb_data.final_try = true ;
249+ send_event_cb (_current_event);
257250
258251 tr_error (" CellularStateMachine target state %s, current state %s" , get_state_string (_target_state), get_state_string (_state));
259252}
260253
261254const char *CellularStateMachine::get_state_string (CellularState state) const
262255{
263256#if MBED_CONF_MBED_TRACE_ENABLE
264- static const char *strings[STATE_MAX_FSM_STATE] = { " Init" , " Power" , " Device ready" , " SIM pin" , " Registering network" , " Attaching network" };
257+ static const char *strings[STATE_MAX_FSM_STATE] = { " Init" , " Power" , " Device ready" , " SIM pin" , " Signal quality " , " Registering network" , " Attaching network" };
265258 return strings[state];
266259#else
267260 return NULL ;
@@ -282,12 +275,17 @@ void CellularStateMachine::retry_state_or_fail()
282275{
283276 if (++_retry_count < CELLULAR_RETRY_ARRAY_SIZE) {
284277 tr_debug (" %s: retry %d/%d" , get_state_string (_state), _retry_count, CELLULAR_RETRY_ARRAY_SIZE);
278+ // send info to application/driver about error logic so it can implement proper error logic
279+ _cb_data.status_data = _current_event;
280+ _cb_data.data = &_retry_count;
281+ _cb_data.error = NSAPI_ERROR_OK;
282+ send_event_cb (CellularStateRetryEvent);
283+
285284 _event_timeout = _retry_timeout_array[_retry_count];
286285 _is_retry = true ;
287286 _cb_data.error = NSAPI_ERROR_OK;
288287 } else {
289288 report_failure (get_state_string (_state));
290- return ;
291289 }
292290}
293291
@@ -334,9 +332,7 @@ bool CellularStateMachine::device_ready()
334332 }
335333#endif // MBED_CONF_CELLULAR_DEBUG_AT
336334
337- if (_event_status_cb) {
338- _event_status_cb ((nsapi_event_t )CellularDeviceReady, (intptr_t )&_cb_data);
339- }
335+ send_event_cb (CellularDeviceReady);
340336 _cellularDevice.set_ready_cb (0 );
341337
342338 return true ;
@@ -402,12 +398,25 @@ void CellularStateMachine::state_sim_pin()
402398 } else if (_cb_data.error ) {
403399 tr_warning (" Packet domain event reporting set failed!" );
404400 }
405- enter_to_state (STATE_REGISTERING_NETWORK );
401+ enter_to_state (STATE_SIGNAL_QUALITY );
406402 } else {
407403 retry_state_or_fail ();
408404 }
409405}
410406
407+ void CellularStateMachine::state_signal_quality ()
408+ {
409+ _cb_data.error = _network.get_signal_quality (_signal_quality.rssi , &_signal_quality.ber );
410+
411+ if (_cb_data.error != NSAPI_ERROR_OK) {
412+ retry_state_or_fail ();
413+ } else {
414+ _cb_data.data = &_signal_quality;
415+ send_event_cb (_current_event);
416+ enter_to_state (STATE_REGISTERING_NETWORK);
417+ }
418+ }
419+
411420void CellularStateMachine::state_registering ()
412421{
413422 _cellularDevice.set_timeout (TIMEOUT_NETWORK);
@@ -419,7 +428,7 @@ void CellularStateMachine::state_registering()
419428 _cb_data.status_data = CellularNetwork::AlreadyRegistered;
420429 }
421430 _cb_data.error = NSAPI_ERROR_OK;
422- _event_status_cb (_current_event, ( intptr_t )&_cb_data );
431+ send_event_cb (_current_event);
423432 // we are already registered, go to attach
424433 enter_to_state (STATE_ATTACHING_NETWORK);
425434 } else {
@@ -440,10 +449,8 @@ void CellularStateMachine::state_attaching()
440449 _cb_data.error = _network.set_attach ();
441450 }
442451 if (_cb_data.error == NSAPI_ERROR_OK) {
443- if (_event_status_cb) {
444- _cb_data.status_data = CellularNetwork::Attached;
445- _event_status_cb (_current_event, (intptr_t )&_cb_data);
446- }
452+ _cb_data.status_data = CellularNetwork::Attached;
453+ send_event_cb (_current_event);
447454 } else {
448455 retry_state_or_fail ();
449456 }
@@ -524,43 +531,51 @@ bool CellularStateMachine::get_current_status(CellularStateMachine::CellularStat
524531
525532void CellularStateMachine::event ()
526533{
527- #if MBED_CONF_MBED_TRACE_ENABLE
528- int rssi;
529- if (_network.get_signal_quality (rssi) == NSAPI_ERROR_OK) {
530- if (rssi == CellularNetwork::SignalQualityUnknown) {
531- tr_info (" RSSI unknown" );
532- } else {
533- tr_info (" RSSI %d dBm" , rssi);
534+ // Don't send Signal quality when in signal quality state or it can confuse callback functions when running retry logic
535+ if (_state != STATE_SIGNAL_QUALITY) {
536+ _cb_data.error = _network.get_signal_quality (_signal_quality.rssi , &_signal_quality.ber );
537+ _cb_data.data = &_signal_quality;
538+
539+ if (_cb_data.error == NSAPI_ERROR_OK) {
540+ send_event_cb (CellularSignalQuality);
541+ if (_signal_quality.rssi == CellularNetwork::SignalQualityUnknown) {
542+ tr_info (" RSSI unknown" );
543+ } else {
544+ tr_info (" RSSI %d dBm" , _signal_quality.rssi );
545+ }
534546 }
535547 }
536- #endif
537548
538549 _event_timeout = -1 ;
539550 _is_retry = false ;
540551
541552 switch (_state) {
542553 case STATE_INIT:
543- _current_event = ( nsapi_event_t ) CellularDeviceReady;
554+ _current_event = CellularDeviceReady;
544555 state_init ();
545556 break ;
546557 case STATE_POWER_ON:
547- _current_event = ( nsapi_event_t ) CellularDeviceReady;
558+ _current_event = CellularDeviceReady;
548559 state_power_on ();
549560 break ;
550561 case STATE_DEVICE_READY:
551- _current_event = ( nsapi_event_t ) CellularDeviceReady;
562+ _current_event = CellularDeviceReady;
552563 state_device_ready ();
553564 break ;
554565 case STATE_SIM_PIN:
555- _current_event = ( nsapi_event_t ) CellularSIMStatusChanged;
566+ _current_event = CellularSIMStatusChanged;
556567 state_sim_pin ();
557568 break ;
569+ case STATE_SIGNAL_QUALITY:
570+ _current_event = CellularSignalQuality;
571+ state_signal_quality ();
572+ break ;
558573 case STATE_REGISTERING_NETWORK:
559- _current_event = ( nsapi_event_t ) CellularRegistrationStatusChanged;
574+ _current_event = CellularRegistrationStatusChanged;
560575 state_registering ();
561576 break ;
562577 case STATE_ATTACHING_NETWORK:
563- _current_event = ( nsapi_event_t ) CellularAttachNetwork;
578+ _current_event = CellularAttachNetwork;
564579 state_attaching ();
565580 break ;
566581 default :
@@ -612,6 +627,13 @@ void CellularStateMachine::set_cellular_callback(mbed::Callback<void(nsapi_event
612627 _event_status_cb = status_cb;
613628}
614629
630+ void CellularStateMachine::send_event_cb (cellular_connection_status_t status)
631+ {
632+ if (_event_status_cb) {
633+ _event_status_cb ((nsapi_event_t )status, (intptr_t )&_cb_data);
634+ }
635+ }
636+
615637bool CellularStateMachine::check_is_target_reached ()
616638{
617639 if (((_target_state == _state || _target_state < _next_state) && _cb_data.error == NSAPI_ERROR_OK && !_is_retry) ||
@@ -629,7 +651,8 @@ bool CellularStateMachine::check_is_target_reached()
629651void CellularStateMachine::cellular_event_changed (nsapi_event_t ev, intptr_t ptr)
630652{
631653 cell_callback_data_t *data = (cell_callback_data_t *)ptr;
632- if ((cellular_connection_status_t )ev == CellularRegistrationStatusChanged && _state == STATE_REGISTERING_NETWORK) {
654+ if ((cellular_connection_status_t )ev == CellularRegistrationStatusChanged && (
655+ _state == STATE_REGISTERING_NETWORK || _state == STATE_SIGNAL_QUALITY)) {
633656 // expect packet data so only these states are valid
634657 CellularNetwork::registration_params_t reg_params;
635658 nsapi_error_t err = _network.get_registration_params (reg_params);
0 commit comments