diff --git a/features/cellular/easy_cellular/CellularConnectionFSM.cpp b/features/cellular/easy_cellular/CellularConnectionFSM.cpp index 657b734ae3..9f41625569 100644 --- a/features/cellular/easy_cellular/CellularConnectionFSM.cpp +++ b/features/cellular/easy_cellular/CellularConnectionFSM.cpp @@ -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); @@ -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; } diff --git a/features/cellular/easy_cellular/EasyCellularConnection.cpp b/features/cellular/easy_cellular/EasyCellularConnection.cpp index 204e4b7bff..f4e73e5b5b 100644 --- a/features/cellular/easy_cellular/EasyCellularConnection.cpp +++ b/features/cellular/easy_cellular/EasyCellularConnection.cpp @@ -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; } @@ -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 @@ -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); @@ -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(); @@ -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 { @@ -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; } @@ -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 diff --git a/features/cellular/easy_cellular/EasyCellularConnection.h b/features/cellular/easy_cellular/EasyCellularConnection.h index 036fa04a78..fd8d8f07e4 100644 --- a/features/cellular/easy_cellular/EasyCellularConnection.h +++ b/features/cellular/easy_cellular/EasyCellularConnection.h @@ -168,6 +168,7 @@ private: bool _is_connected; bool _is_initialized; + bool _stm_error; #if USE_APN_LOOKUP bool _credentials_set; #endif // #if USE_APN_LOOKUP