Cellular: state machine and easycellular now return error fast if sim pin needed but not provided.

pull/7978/head
Teppo Järvelin 2018-09-04 13:13:35 +03:00
parent 44925d8527
commit 69bcbd84db
3 changed files with 32 additions and 11 deletions

View File

@ -161,9 +161,17 @@ bool CellularConnectionFSM::open_sim()
// here you could add wait(secs) if you know start delay of your SIM // here you could add wait(secs) if you know start delay of your SIM
if (_sim->get_sim_state(state) != NSAPI_ERROR_OK) { if (_sim->get_sim_state(state) != NSAPI_ERROR_OK) {
tr_info("Waiting for SIM (err while reading)..."); tr_info("Waiting for SIM (err while reading)...");
if (_event_status_cb) {
_event_status_cb((nsapi_event_t)CellularSIMStatusChanged, state);
}
return false; 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 (state == CellularSIM::SimStatePinNeeded) {
if (strlen(_sim_pin)) { if (strlen(_sim_pin)) {
tr_info("SIM pin required, entering pin: %s", _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); tr_error("SIM pin set failed with: %d, bailing out...", err);
} }
} else { } 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; return state == CellularSIM::SimStateReady;
} }

View File

@ -45,6 +45,16 @@ bool EasyCellularConnection::cellular_status(int state, int next_state)
(void)_cellularSemaphore.release(); (void)_cellularSemaphore.release();
return false; // return false -> state machine is halted 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; return true;
} }
@ -63,9 +73,10 @@ void EasyCellularConnection::network_callback(nsapi_event_t ev, intptr_t ptr)
} }
EasyCellularConnection::EasyCellularConnection(bool debug) : EasyCellularConnection::EasyCellularConnection(bool debug) :
_is_connected(false), _is_initialized(false), _target_state(CellularConnectionFSM::STATE_POWER_ON), _cellularSerial( _is_connected(false), _is_initialized(false), _stm_error(false),
MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE), _cellularSemaphore(0), _cellularConnectionFSM(0), _credentials_err( _target_state(CellularConnectionFSM::STATE_POWER_ON),
NSAPI_ERROR_OK), _status_cb(0) _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()"); tr_info("EasyCellularConnection()");
#if USE_APN_LOOKUP #if USE_APN_LOOKUP
@ -86,6 +97,7 @@ EasyCellularConnection::~EasyCellularConnection()
nsapi_error_t EasyCellularConnection::init() nsapi_error_t EasyCellularConnection::init()
{ {
nsapi_error_t err = NSAPI_ERROR_OK; nsapi_error_t err = NSAPI_ERROR_OK;
_stm_error = false;
if (!_is_initialized) { if (!_is_initialized) {
#if defined (MDMRTS) && defined (MDMCTS) #if defined (MDMRTS) && defined (MDMCTS)
_cellularSerial.set_flow_control(SerialBase::RTSCTS, MDMRTS, 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) { if (sim_pin) {
this->set_sim_pin(sim_pin); set_sim_pin(sim_pin);
} }
return connect(); return connect();
@ -193,7 +205,7 @@ nsapi_error_t EasyCellularConnection::connect()
err = _cellularConnectionFSM->continue_to_state(_target_state); err = _cellularConnectionFSM->continue_to_state(_target_state);
if (err == NSAPI_ERROR_OK) { if (err == NSAPI_ERROR_OK) {
int sim_wait = _cellularSemaphore.wait(60 * 1000); // reserve 60 seconds to access to SIM 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"); tr_error("NO SIM ACCESS");
err = NSAPI_ERROR_NO_CONNECTION; err = NSAPI_ERROR_NO_CONNECTION;
} else { } else {
@ -223,7 +235,7 @@ nsapi_error_t EasyCellularConnection::connect()
err = _cellularConnectionFSM->continue_to_state(_target_state); err = _cellularConnectionFSM->continue_to_state(_target_state);
if (err == NSAPI_ERROR_OK) { if (err == NSAPI_ERROR_OK) {
int ret_wait = _cellularSemaphore.wait(10 * 60 * 1000); // cellular network searching may take several minutes 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"); tr_info("No cellular connection");
err = NSAPI_ERROR_NO_CONNECTION; err = NSAPI_ERROR_NO_CONNECTION;
} }
@ -237,6 +249,7 @@ nsapi_error_t EasyCellularConnection::disconnect()
_credentials_err = NSAPI_ERROR_OK; _credentials_err = NSAPI_ERROR_OK;
_is_connected = false; _is_connected = false;
_is_initialized = false; _is_initialized = false;
_stm_error = false;
#if USE_APN_LOOKUP #if USE_APN_LOOKUP
_credentials_set = false; _credentials_set = false;
#endif // #if USE_APN_LOOKUP #endif // #if USE_APN_LOOKUP

View File

@ -168,6 +168,7 @@ private:
bool _is_connected; bool _is_connected;
bool _is_initialized; bool _is_initialized;
bool _stm_error;
#if USE_APN_LOOKUP #if USE_APN_LOOKUP
bool _credentials_set; bool _credentials_set;
#endif // #if USE_APN_LOOKUP #endif // #if USE_APN_LOOKUP