@@ -83,6 +83,7 @@ void CellularStateMachine::reset()
83
83
_plmn_network_found = false ;
84
84
_is_retry = false ;
85
85
_active_context = false ;
86
+ _target_state = STATE_INIT;
86
87
enter_to_state (STATE_INIT);
87
88
}
88
89
@@ -151,7 +152,7 @@ bool CellularStateMachine::open_sim()
151
152
}
152
153
153
154
if (state == CellularDevice::SimStatePinNeeded) {
154
- if (strlen ( _sim_pin) ) {
155
+ if (_sim_pin) {
155
156
tr_info (" Entering PIN to open SIM" );
156
157
_cb_data.error = _cellularDevice.set_pin (_sim_pin);
157
158
if (_cb_data.error ) {
@@ -515,8 +516,13 @@ void CellularStateMachine::continue_from_state(CellularState state)
515
516
nsapi_error_t CellularStateMachine::run_to_state (CellularStateMachine::CellularState state)
516
517
{
517
518
_mutex.lock ();
519
+
520
+ CellularState tmp_state = state;
521
+ if (_plmn && tmp_state == STATE_REGISTERING_NETWORK) {
522
+ tmp_state = STATE_MANUAL_REGISTERING_NETWORK;
523
+ }
518
524
// call pre_event via queue so that it's in same thread and it's safe to decisions
519
- int id = _queue.call_in (0 , this , &CellularStateMachine::pre_event, state );
525
+ int id = _queue.call_in (0 , this , &CellularStateMachine::pre_event, tmp_state );
520
526
if (!id) {
521
527
stop ();
522
528
_mutex.unlock ();
@@ -558,7 +564,11 @@ bool CellularStateMachine::get_current_status(CellularStateMachine::CellularStat
558
564
_mutex.lock ();
559
565
current_state = _state;
560
566
target_state = _target_state;
561
- is_running = _event_id != -1 ;
567
+ if (_event_id == -1 || _event_id == STM_STOPPED) {
568
+ is_running = false ;
569
+ } else {
570
+ is_running = true ;
571
+ }
562
572
_mutex.unlock ();
563
573
return is_running;
564
574
}
@@ -660,8 +670,14 @@ void CellularStateMachine::set_cellular_callback(mbed::Callback<void(nsapi_event
660
670
661
671
bool CellularStateMachine::check_is_target_reached ()
662
672
{
663
- tr_debug (" check_is_target_reached(): target state %s, _state: %s, _cb_data.error: %d, _event_id: %d, _is_retry: %d" , get_state_string (_target_state), get_state_string (_state), _cb_data.error , _event_id, _is_retry);
664
- if ((_target_state == _state && _cb_data.error == NSAPI_ERROR_OK && !_is_retry) || _event_id == STM_STOPPED) {
673
+ tr_debug (" check_is_target_reached(): target state %s, _state: %s, _cb_data.error: %d, _event_id: %d,_is_retry: %d" , get_state_string (_target_state), get_state_string (_state), _cb_data.error , _event_id, _is_retry);
674
+
675
+ if (((_target_state == _state || _target_state < _next_state) && _cb_data.error == NSAPI_ERROR_OK && !_is_retry) ||
676
+ _event_id == STM_STOPPED) {
677
+ if (_target_state != _state && _target_state < _next_state) {
678
+ // we are skipping the state, update _state to current state because we have reached it
679
+ _state = _target_state;
680
+ }
665
681
_event_id = -1 ;
666
682
return true ;
667
683
}
@@ -717,8 +733,12 @@ void CellularStateMachine::device_ready_cb()
717
733
}
718
734
}
719
735
720
- void CellularStateMachine::set_retry_timeout_array (uint16_t timeout[] , int array_len)
736
+ void CellularStateMachine::set_retry_timeout_array (uint16_t * timeout, int array_len)
721
737
{
738
+ if (!timeout || array_len <= 0 ) {
739
+ tr_warn (" set_retry_timeout_array, timeout array null or invalid length" );
740
+ return ;
741
+ }
722
742
_retry_array_length = array_len > RETRY_ARRAY_SIZE ? RETRY_ARRAY_SIZE : array_len;
723
743
724
744
for (int i = 0 ; i < _retry_array_length; i++) {
0 commit comments