@@ -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(nw), _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 ;
@@ -133,10 +133,8 @@ bool CellularStateMachine::open_sim()
133
133
}
134
134
135
135
// 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);
140
138
141
139
if (state == CellularDevice::SimStatePinNeeded) {
142
140
if (_sim_pin) {
@@ -156,13 +154,10 @@ bool CellularStateMachine::open_sim()
156
154
bool sim_ready = state == CellularDevice::SimStateReady;
157
155
158
156
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 ;
166
161
}
167
162
}
168
163
@@ -250,18 +245,16 @@ void CellularStateMachine::report_failure(const char *msg)
250
245
tr_error (" CellularStateMachine failure: %s" , msg);
251
246
252
247
_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);
257
250
258
251
tr_error (" CellularStateMachine target state %s, current state %s" , get_state_string (_target_state), get_state_string (_state));
259
252
}
260
253
261
254
const char *CellularStateMachine::get_state_string (CellularState state) const
262
255
{
263
256
#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" };
265
258
return strings[state];
266
259
#else
267
260
return NULL ;
@@ -282,12 +275,17 @@ void CellularStateMachine::retry_state_or_fail()
282
275
{
283
276
if (++_retry_count < CELLULAR_RETRY_ARRAY_SIZE) {
284
277
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
+
285
284
_event_timeout = _retry_timeout_array[_retry_count];
286
285
_is_retry = true ;
287
286
_cb_data.error = NSAPI_ERROR_OK;
288
287
} else {
289
288
report_failure (get_state_string (_state));
290
- return ;
291
289
}
292
290
}
293
291
@@ -334,9 +332,7 @@ bool CellularStateMachine::device_ready()
334
332
}
335
333
#endif // MBED_CONF_CELLULAR_DEBUG_AT
336
334
337
- if (_event_status_cb) {
338
- _event_status_cb ((nsapi_event_t )CellularDeviceReady, (intptr_t )&_cb_data);
339
- }
335
+ send_event_cb (CellularDeviceReady);
340
336
_cellularDevice.set_ready_cb (0 );
341
337
342
338
return true ;
@@ -402,12 +398,25 @@ void CellularStateMachine::state_sim_pin()
402
398
} else if (_cb_data.error ) {
403
399
tr_warning (" Packet domain event reporting set failed!" );
404
400
}
405
- enter_to_state (STATE_REGISTERING_NETWORK );
401
+ enter_to_state (STATE_SIGNAL_QUALITY );
406
402
} else {
407
403
retry_state_or_fail ();
408
404
}
409
405
}
410
406
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
+
411
420
void CellularStateMachine::state_registering ()
412
421
{
413
422
_cellularDevice.set_timeout (TIMEOUT_NETWORK);
@@ -419,7 +428,7 @@ void CellularStateMachine::state_registering()
419
428
_cb_data.status_data = CellularNetwork::AlreadyRegistered;
420
429
}
421
430
_cb_data.error = NSAPI_ERROR_OK;
422
- _event_status_cb (_current_event, ( intptr_t )&_cb_data );
431
+ send_event_cb (_current_event);
423
432
// we are already registered, go to attach
424
433
enter_to_state (STATE_ATTACHING_NETWORK);
425
434
} else {
@@ -440,10 +449,8 @@ void CellularStateMachine::state_attaching()
440
449
_cb_data.error = _network.set_attach ();
441
450
}
442
451
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);
447
454
} else {
448
455
retry_state_or_fail ();
449
456
}
@@ -524,43 +531,51 @@ bool CellularStateMachine::get_current_status(CellularStateMachine::CellularStat
524
531
525
532
void CellularStateMachine::event ()
526
533
{
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
+ }
534
546
}
535
547
}
536
- #endif
537
548
538
549
_event_timeout = -1 ;
539
550
_is_retry = false ;
540
551
541
552
switch (_state) {
542
553
case STATE_INIT:
543
- _current_event = ( nsapi_event_t ) CellularDeviceReady;
554
+ _current_event = CellularDeviceReady;
544
555
state_init ();
545
556
break ;
546
557
case STATE_POWER_ON:
547
- _current_event = ( nsapi_event_t ) CellularDeviceReady;
558
+ _current_event = CellularDeviceReady;
548
559
state_power_on ();
549
560
break ;
550
561
case STATE_DEVICE_READY:
551
- _current_event = ( nsapi_event_t ) CellularDeviceReady;
562
+ _current_event = CellularDeviceReady;
552
563
state_device_ready ();
553
564
break ;
554
565
case STATE_SIM_PIN:
555
- _current_event = ( nsapi_event_t ) CellularSIMStatusChanged;
566
+ _current_event = CellularSIMStatusChanged;
556
567
state_sim_pin ();
557
568
break ;
569
+ case STATE_SIGNAL_QUALITY:
570
+ _current_event = CellularSignalQuality;
571
+ state_signal_quality ();
572
+ break ;
558
573
case STATE_REGISTERING_NETWORK:
559
- _current_event = ( nsapi_event_t ) CellularRegistrationStatusChanged;
574
+ _current_event = CellularRegistrationStatusChanged;
560
575
state_registering ();
561
576
break ;
562
577
case STATE_ATTACHING_NETWORK:
563
- _current_event = ( nsapi_event_t ) CellularAttachNetwork;
578
+ _current_event = CellularAttachNetwork;
564
579
state_attaching ();
565
580
break ;
566
581
default :
@@ -612,6 +627,13 @@ void CellularStateMachine::set_cellular_callback(mbed::Callback<void(nsapi_event
612
627
_event_status_cb = status_cb;
613
628
}
614
629
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
+
615
637
bool CellularStateMachine::check_is_target_reached ()
616
638
{
617
639
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()
629
651
void CellularStateMachine::cellular_event_changed (nsapi_event_t ev, intptr_t ptr)
630
652
{
631
653
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)) {
633
656
// expect packet data so only these states are valid
634
657
CellularNetwork::registration_params_t reg_params;
635
658
nsapi_error_t err = _network.get_registration_params (reg_params);
0 commit comments