40
40
#define RETRY_COUNT_DEFAULT 3
41
41
42
42
const int STM_STOPPED = -99 ;
43
+ const int ACTIVE_PDP_CONTEXT = 0x01 ;
44
+ const int ATTACHED_TO_NETWORK = 0x02 ;
43
45
44
46
namespace mbed {
45
47
@@ -48,7 +50,7 @@ CellularStateMachine::CellularStateMachine(CellularDevice &device, events::Event
48
50
_event_status_cb (0 ), _network(0 ), _power(0 ), _sim(0 ), _queue(queue), _queue_thread(0 ), _sim_pin(0 ),
49
51
_retry_count (0 ), _event_timeout(-1 ), _event_id(-1 ), _plmn(0 ), _command_success(false ),
50
52
_plmn_network_found (false ), _is_retry(false ), _cb_data(), _current_event(NSAPI_EVENT_CONNECTION_STATUS_CHANGE),
51
- _active_context ( false )
53
+ _network_status ( 0 )
52
54
{
53
55
#if MBED_CONF_CELLULAR_RANDOM_MAX_START_DELAY == 0
54
56
_start_time = 0 ;
@@ -83,7 +85,7 @@ void CellularStateMachine::reset()
83
85
_event_id = -1 ;
84
86
_plmn_network_found = false ;
85
87
_is_retry = false ;
86
- _active_context = false ;
88
+ _network_status = 0 ;
87
89
enter_to_state (STATE_INIT);
88
90
}
89
91
@@ -193,7 +195,7 @@ bool CellularStateMachine::is_registered()
193
195
}
194
196
195
197
_cb_data.status_data = status;
196
- return is_registered || _active_context ;
198
+ return is_registered || _network_status ;
197
199
}
198
200
199
201
bool CellularStateMachine::get_network_registration (CellularNetwork::RegistrationType type,
@@ -440,8 +442,15 @@ void CellularStateMachine::state_sim_pin()
440
442
return ;
441
443
}
442
444
443
- _active_context = false ;
444
- _active_context = _network->is_active_context (); // check if context was already activated
445
+ if (_network->is_active_context ()) { // check if context was already activated
446
+ tr_debug (" ACTIVE CONTEXT FOUND, skip registering." );
447
+ _network_status |= ACTIVE_PDP_CONTEXT;
448
+ }
449
+ CellularNetwork::AttachStatus status; // check if modem is already attached to a network
450
+ if (_network->get_attach (status) == NSAPI_ERROR_OK && status == CellularNetwork::Attached) {
451
+ _network_status |= ATTACHED_TO_NETWORK;
452
+ tr_debug (" DEVICE IS ALREADY ATTACHED TO NETWORK, skip registering and attach." );
453
+ }
445
454
if (_plmn) {
446
455
enter_to_state (STATE_MANUAL_REGISTERING_NETWORK);
447
456
} else {
@@ -499,7 +508,9 @@ void CellularStateMachine::state_attaching()
499
508
{
500
509
_cellularDevice.set_timeout (TIMEOUT_CONNECT);
501
510
tr_info (" Attaching network (timeout %d s)" , TIMEOUT_CONNECT / 1000 );
502
- _cb_data.error = _network->set_attach ();
511
+ if (_network_status != ATTACHED_TO_NETWORK) {
512
+ _cb_data.error = _network->set_attach ();
513
+ }
503
514
if (_cb_data.error == NSAPI_ERROR_OK) {
504
515
_cellularDevice.close_sim ();
505
516
_sim = NULL ;
0 commit comments