Skip to content

Cellular: state machine and easycellular now return error fast if sim… #7978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions features/cellular/easy_cellular/CellularConnectionFSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,17 @@ bool CellularConnectionFSM::open_sim()
// here you could add wait(secs) if you know start delay of your SIM
if (_sim->get_sim_state(state) != NSAPI_ERROR_OK) {
tr_info("Waiting for SIM (err while reading)...");
if (_event_status_cb) {
_event_status_cb((nsapi_event_t)CellularSIMStatusChanged, state);
}
return false;
}

// report current state so callback can set sim pin if needed
if (_event_status_cb) {
_event_status_cb((nsapi_event_t)CellularSIMStatusChanged, state);
}

if (state == CellularSIM::SimStatePinNeeded) {
if (strlen(_sim_pin)) {
tr_info("SIM pin required, entering pin: %s", _sim_pin);
Expand All @@ -172,14 +180,13 @@ bool CellularConnectionFSM::open_sim()
tr_error("SIM pin set failed with: %d, bailing out...", err);
}
} else {
tr_warn("PIN required but No SIM pin provided.");
// No sim pin provided even it's needed, stop state machine
tr_error("PIN required but No SIM pin provided.");
_retry_count = MAX_RETRY_ARRAY_SIZE;
return false;
}
}

if (_event_status_cb) {
_event_status_cb((nsapi_event_t)CellularSIMStatusChanged, state);
}

return state == CellularSIM::SimStateReady;
}

Expand Down
25 changes: 19 additions & 6 deletions features/cellular/easy_cellular/EasyCellularConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ bool EasyCellularConnection::cellular_status(int state, int next_state)
(void)_cellularSemaphore.release();
return false; // return false -> state machine is halted
}

// only in case of an error or when connected is reached state and next_state can be the same.
// Release semaphore to return application instead of waiting for semaphore to complete.
if (state == next_state) {
tr_error("cellular_status: state and next_state are same, release semaphore as this is an error in state machine");
_stm_error = true;
(void)_cellularSemaphore.release();
return false; // return false -> state machine is halted
}

return true;
}

Expand All @@ -63,9 +73,10 @@ void EasyCellularConnection::network_callback(nsapi_event_t ev, intptr_t ptr)
}

EasyCellularConnection::EasyCellularConnection(bool debug) :
_is_connected(false), _is_initialized(false), _target_state(CellularConnectionFSM::STATE_POWER_ON), _cellularSerial(
MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE), _cellularSemaphore(0), _cellularConnectionFSM(0), _credentials_err(
NSAPI_ERROR_OK), _status_cb(0)
_is_connected(false), _is_initialized(false), _stm_error(false),
_target_state(CellularConnectionFSM::STATE_POWER_ON),
_cellularSerial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE), _cellularSemaphore(0),
_cellularConnectionFSM(0), _credentials_err(NSAPI_ERROR_OK), _status_cb(0)
{
tr_info("EasyCellularConnection()");
#if USE_APN_LOOKUP
Expand All @@ -86,6 +97,7 @@ EasyCellularConnection::~EasyCellularConnection()
nsapi_error_t EasyCellularConnection::init()
{
nsapi_error_t err = NSAPI_ERROR_OK;
_stm_error = false;
if (!_is_initialized) {
#if defined (MDMRTS) && defined (MDMCTS)
_cellularSerial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS);
Expand Down Expand Up @@ -156,7 +168,7 @@ nsapi_error_t EasyCellularConnection::connect(const char *sim_pin, const char *a
}

if (sim_pin) {
this->set_sim_pin(sim_pin);
set_sim_pin(sim_pin);
}

return connect();
Expand Down Expand Up @@ -193,7 +205,7 @@ nsapi_error_t EasyCellularConnection::connect()
err = _cellularConnectionFSM->continue_to_state(_target_state);
if (err == NSAPI_ERROR_OK) {
int sim_wait = _cellularSemaphore.wait(60 * 1000); // reserve 60 seconds to access to SIM
if (sim_wait != 1) {
if (sim_wait != 1 || _stm_error) {
tr_error("NO SIM ACCESS");
err = NSAPI_ERROR_NO_CONNECTION;
} else {
Expand Down Expand Up @@ -223,7 +235,7 @@ nsapi_error_t EasyCellularConnection::connect()
err = _cellularConnectionFSM->continue_to_state(_target_state);
if (err == NSAPI_ERROR_OK) {
int ret_wait = _cellularSemaphore.wait(10 * 60 * 1000); // cellular network searching may take several minutes
if (ret_wait != 1) {
if (ret_wait != 1 || _stm_error) {
tr_info("No cellular connection");
err = NSAPI_ERROR_NO_CONNECTION;
}
Expand All @@ -237,6 +249,7 @@ nsapi_error_t EasyCellularConnection::disconnect()
_credentials_err = NSAPI_ERROR_OK;
_is_connected = false;
_is_initialized = false;
_stm_error = false;
#if USE_APN_LOOKUP
_credentials_set = false;
#endif // #if USE_APN_LOOKUP
Expand Down
1 change: 1 addition & 0 deletions features/cellular/easy_cellular/EasyCellularConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class EasyCellularConnection: public CellularBase {

bool _is_connected;
bool _is_initialized;
bool _stm_error;
#if USE_APN_LOOKUP
bool _credentials_set;
#endif // #if USE_APN_LOOKUP
Expand Down