Skip to content

Commit 17b0d4e

Browse files
authored
Merge pull request #9152 from jarvte/check_attach_status_stm
Cellular: fix state machine to check network attach
2 parents 965f15d + 6bc7ec0 commit 17b0d4e

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

features/cellular/framework/device/CellularStateMachine.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#define RETRY_COUNT_DEFAULT 3
4141

4242
const int STM_STOPPED = -99;
43+
const int ACTIVE_PDP_CONTEXT = 0x01;
44+
const int ATTACHED_TO_NETWORK = 0x02;
4345

4446
namespace mbed {
4547

@@ -48,7 +50,7 @@ CellularStateMachine::CellularStateMachine(CellularDevice &device, events::Event
4850
_event_status_cb(0), _network(0), _power(0), _sim(0), _queue(queue), _queue_thread(0), _sim_pin(0),
4951
_retry_count(0), _event_timeout(-1), _event_id(-1), _plmn(0), _command_success(false),
5052
_plmn_network_found(false), _is_retry(false), _cb_data(), _current_event(NSAPI_EVENT_CONNECTION_STATUS_CHANGE),
51-
_active_context(false)
53+
_network_status(0)
5254
{
5355
#if MBED_CONF_CELLULAR_RANDOM_MAX_START_DELAY == 0
5456
_start_time = 0;
@@ -83,7 +85,7 @@ void CellularStateMachine::reset()
8385
_event_id = -1;
8486
_plmn_network_found = false;
8587
_is_retry = false;
86-
_active_context = false;
88+
_network_status = 0;
8789
enter_to_state(STATE_INIT);
8890
}
8991

@@ -193,7 +195,7 @@ bool CellularStateMachine::is_registered()
193195
}
194196

195197
_cb_data.status_data = status;
196-
return is_registered || _active_context;
198+
return is_registered || _network_status;
197199
}
198200

199201
bool CellularStateMachine::get_network_registration(CellularNetwork::RegistrationType type,
@@ -440,8 +442,15 @@ void CellularStateMachine::state_sim_pin()
440442
return;
441443
}
442444

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+
}
445454
if (_plmn) {
446455
enter_to_state(STATE_MANUAL_REGISTERING_NETWORK);
447456
} else {
@@ -499,7 +508,9 @@ void CellularStateMachine::state_attaching()
499508
{
500509
_cellularDevice.set_timeout(TIMEOUT_CONNECT);
501510
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+
}
503514
if (_cb_data.error == NSAPI_ERROR_OK) {
504515
_cellularDevice.close_sim();
505516
_sim = NULL;

features/cellular/framework/device/CellularStateMachine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class CellularStateMachine {
185185
bool _is_retry;
186186
cell_callback_data_t _cb_data;
187187
nsapi_event_t _current_event;
188-
bool _active_context; // Is there any active context?
188+
int _network_status; // Is there any active context or is modem attached to a network?
189189
PlatformMutex _mutex;
190190
};
191191

0 commit comments

Comments
 (0)