Skip to content

Commit 69bcbd8

Browse files
author
Teppo Järvelin
committed
Cellular: state machine and easycellular now return error fast if sim pin needed but not provided.
1 parent 44925d8 commit 69bcbd8

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

features/cellular/easy_cellular/CellularConnectionFSM.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,17 @@ bool CellularConnectionFSM::open_sim()
161161
// here you could add wait(secs) if you know start delay of your SIM
162162
if (_sim->get_sim_state(state) != NSAPI_ERROR_OK) {
163163
tr_info("Waiting for SIM (err while reading)...");
164+
if (_event_status_cb) {
165+
_event_status_cb((nsapi_event_t)CellularSIMStatusChanged, state);
166+
}
164167
return false;
165168
}
166169

170+
// report current state so callback can set sim pin if needed
171+
if (_event_status_cb) {
172+
_event_status_cb((nsapi_event_t)CellularSIMStatusChanged, state);
173+
}
174+
167175
if (state == CellularSIM::SimStatePinNeeded) {
168176
if (strlen(_sim_pin)) {
169177
tr_info("SIM pin required, entering pin: %s", _sim_pin);
@@ -172,14 +180,13 @@ bool CellularConnectionFSM::open_sim()
172180
tr_error("SIM pin set failed with: %d, bailing out...", err);
173181
}
174182
} else {
175-
tr_warn("PIN required but No SIM pin provided.");
183+
// No sim pin provided even it's needed, stop state machine
184+
tr_error("PIN required but No SIM pin provided.");
185+
_retry_count = MAX_RETRY_ARRAY_SIZE;
186+
return false;
176187
}
177188
}
178189

179-
if (_event_status_cb) {
180-
_event_status_cb((nsapi_event_t)CellularSIMStatusChanged, state);
181-
}
182-
183190
return state == CellularSIM::SimStateReady;
184191
}
185192

features/cellular/easy_cellular/EasyCellularConnection.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ bool EasyCellularConnection::cellular_status(int state, int next_state)
4545
(void)_cellularSemaphore.release();
4646
return false; // return false -> state machine is halted
4747
}
48+
49+
// only in case of an error or when connected is reached state and next_state can be the same.
50+
// Release semaphore to return application instead of waiting for semaphore to complete.
51+
if (state == next_state) {
52+
tr_error("cellular_status: state and next_state are same, release semaphore as this is an error in state machine");
53+
_stm_error = true;
54+
(void)_cellularSemaphore.release();
55+
return false; // return false -> state machine is halted
56+
}
57+
4858
return true;
4959
}
5060

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

6575
EasyCellularConnection::EasyCellularConnection(bool debug) :
66-
_is_connected(false), _is_initialized(false), _target_state(CellularConnectionFSM::STATE_POWER_ON), _cellularSerial(
67-
MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE), _cellularSemaphore(0), _cellularConnectionFSM(0), _credentials_err(
68-
NSAPI_ERROR_OK), _status_cb(0)
76+
_is_connected(false), _is_initialized(false), _stm_error(false),
77+
_target_state(CellularConnectionFSM::STATE_POWER_ON),
78+
_cellularSerial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE), _cellularSemaphore(0),
79+
_cellularConnectionFSM(0), _credentials_err(NSAPI_ERROR_OK), _status_cb(0)
6980
{
7081
tr_info("EasyCellularConnection()");
7182
#if USE_APN_LOOKUP
@@ -86,6 +97,7 @@ EasyCellularConnection::~EasyCellularConnection()
8697
nsapi_error_t EasyCellularConnection::init()
8798
{
8899
nsapi_error_t err = NSAPI_ERROR_OK;
100+
_stm_error = false;
89101
if (!_is_initialized) {
90102
#if defined (MDMRTS) && defined (MDMCTS)
91103
_cellularSerial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS);
@@ -156,7 +168,7 @@ nsapi_error_t EasyCellularConnection::connect(const char *sim_pin, const char *a
156168
}
157169

158170
if (sim_pin) {
159-
this->set_sim_pin(sim_pin);
171+
set_sim_pin(sim_pin);
160172
}
161173

162174
return connect();
@@ -193,7 +205,7 @@ nsapi_error_t EasyCellularConnection::connect()
193205
err = _cellularConnectionFSM->continue_to_state(_target_state);
194206
if (err == NSAPI_ERROR_OK) {
195207
int sim_wait = _cellularSemaphore.wait(60 * 1000); // reserve 60 seconds to access to SIM
196-
if (sim_wait != 1) {
208+
if (sim_wait != 1 || _stm_error) {
197209
tr_error("NO SIM ACCESS");
198210
err = NSAPI_ERROR_NO_CONNECTION;
199211
} else {
@@ -223,7 +235,7 @@ nsapi_error_t EasyCellularConnection::connect()
223235
err = _cellularConnectionFSM->continue_to_state(_target_state);
224236
if (err == NSAPI_ERROR_OK) {
225237
int ret_wait = _cellularSemaphore.wait(10 * 60 * 1000); // cellular network searching may take several minutes
226-
if (ret_wait != 1) {
238+
if (ret_wait != 1 || _stm_error) {
227239
tr_info("No cellular connection");
228240
err = NSAPI_ERROR_NO_CONNECTION;
229241
}
@@ -237,6 +249,7 @@ nsapi_error_t EasyCellularConnection::disconnect()
237249
_credentials_err = NSAPI_ERROR_OK;
238250
_is_connected = false;
239251
_is_initialized = false;
252+
_stm_error = false;
240253
#if USE_APN_LOOKUP
241254
_credentials_set = false;
242255
#endif // #if USE_APN_LOOKUP

features/cellular/easy_cellular/EasyCellularConnection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ class EasyCellularConnection: public CellularBase {
168168

169169
bool _is_connected;
170170
bool _is_initialized;
171+
bool _stm_error;
171172
#if USE_APN_LOOKUP
172173
bool _credentials_set;
173174
#endif // #if USE_APN_LOOKUP

0 commit comments

Comments
 (0)