Skip to content

Commit 6072407

Browse files
Ari ParkkilaTeppo Järvelin
authored andcommitted
Cellular: async FSM updates
1 parent 84b445f commit 6072407

File tree

8 files changed

+112
-36
lines changed

8 files changed

+112
-36
lines changed

features/cellular/easy_cellular/CellularConnectionFSM.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@
3636

3737
#define RETRY_COUNT_DEFAULT 3
3838

39-
namespace mbed {
39+
namespace mbed
40+
{
4041

4142
CellularConnectionFSM::CellularConnectionFSM() :
42-
_serial(0), _state(STATE_INIT), _next_state(_state), _status_callback(0), _event_status_cb(0), _network(0), _power(0), _sim(0),
43-
_queue(8 * EVENTS_EVENT_SIZE), _queue_thread(0), _cellularDevice(0), _retry_count(0), _event_timeout(-1),
44-
_at_queue(8 * EVENTS_EVENT_SIZE), _eventID(0)
43+
_serial(0), _state(STATE_INIT), _next_state(_state), _status_callback(0), _event_status_cb(0), _network(0), _power(0), _sim(0),
44+
_queue(8 * EVENTS_EVENT_SIZE), _queue_thread(0), _cellularDevice(0), _retry_count(0), _event_timeout(-1),
45+
_at_queue(8 * EVENTS_EVENT_SIZE), _eventID(0)
4546
{
4647
memset(_sim_pin, 0, sizeof(_sim_pin));
4748
#if MBED_CONF_CELLULAR_RANDOM_MAX_START_DELAY == 0
@@ -52,14 +53,14 @@ CellularConnectionFSM::CellularConnectionFSM() :
5253
#endif // MBED_CONF_CELLULAR_RANDOM_MAX_START_DELAY
5354

5455
// set initial retry values in seconds
55-
_retry_timeout_array[0] = 1;
56+
_retry_timeout_array[0] = 1; // double time on each retry in order to keep network happy
5657
_retry_timeout_array[1] = 2;
5758
_retry_timeout_array[2] = 4;
58-
_retry_timeout_array[3] = 16;
59-
_retry_timeout_array[4] = 32;
60-
_retry_timeout_array[5] = 60;
61-
_retry_timeout_array[6] = 120;
62-
_retry_timeout_array[7] = 360;
59+
_retry_timeout_array[3] = 8;
60+
_retry_timeout_array[4] = 16;
61+
_retry_timeout_array[5] = 32;
62+
_retry_timeout_array[6] = 64;
63+
_retry_timeout_array[7] = 128; // if around two minutes was not enough then let's wait much longer
6364
_retry_timeout_array[8] = 600;
6465
_retry_timeout_array[9] = TIMEOUT_NETWORK_MAX;
6566
_retry_array_length = MAX_RETRY_ARRAY_SIZE;
@@ -163,7 +164,7 @@ bool CellularConnectionFSM::open_sim()
163164
tr_warn("PIN required but No SIM pin provided.");
164165
}
165166
}
166-
break;
167+
break;
167168
case CellularSIM::SimStatePukNeeded:
168169
tr_info("SIM PUK code needed...");
169170
break;
@@ -223,19 +224,19 @@ bool CellularConnectionFSM::get_network_registration(CellularNetwork::Registrati
223224
switch (status) {
224225
case CellularNetwork::RegisteredRoaming:
225226
is_roaming = true;
226-
// fall-through
227+
// fall-through
227228
case CellularNetwork::RegisteredHomeNetwork:
228229
is_registered = true;
229230
break;
230231
case CellularNetwork::RegisteredSMSOnlyRoaming:
231232
is_roaming = true;
232-
// fall-through
233+
// fall-through
233234
case CellularNetwork::RegisteredSMSOnlyHome:
234235
tr_warn("SMS only network registration!");
235236
break;
236237
case CellularNetwork::RegisteredCSFBNotPreferredRoaming:
237238
is_roaming = true;
238-
// fall-through
239+
// fall-through
239240
case CellularNetwork::RegisteredCSFBNotPreferredHome:
240241
tr_warn("Not preferred network registration!");
241242
break;
@@ -304,7 +305,7 @@ bool CellularConnectionFSM::is_automatic_registering()
304305
nsapi_error_t CellularConnectionFSM::continue_from_state(CellularState state)
305306
{
306307
tr_info("Continue state from %s to %s", get_state_string((CellularConnectionFSM::CellularState)_state),
307-
get_state_string((CellularConnectionFSM::CellularState)state));
308+
get_state_string((CellularConnectionFSM::CellularState)state));
308309
_state = state;
309310
_next_state = state;
310311
_retry_count = 0;
@@ -381,7 +382,7 @@ bool CellularConnectionFSM::device_ready()
381382
bool success = false;
382383
for (int type = 0; type < CellularNetwork::C_MAX; type++) {
383384
if (!_network->set_registration_urc((CellularNetwork::RegistrationType)type, true)) {
384-
success = true;
385+
success = true;
385386
}
386387
}
387388
if (!success) {
@@ -422,13 +423,13 @@ void CellularConnectionFSM::state_sim_pin()
422423
void CellularConnectionFSM::state_registering()
423424
{
424425
_cellularDevice->set_timeout(TIMEOUT_NETWORK);
425-
426426
if (is_registered()) {
427427
// we are already registered, go to attach
428428
enter_to_state(STATE_ATTACHING_NETWORK);
429429
} else {
430430
if (!is_automatic_registering()) { // when we support plmn add this : || plmn
431431
// automatic registering is not on, set registration and retry
432+
_cellularDevice->set_timeout(TIMEOUT_REGISTRATION);
432433
set_network_registration();
433434
}
434435
retry_state_or_fail();
@@ -566,7 +567,7 @@ void CellularConnectionFSM::attach(mbed::Callback<void(nsapi_event_t, intptr_t)>
566567
void CellularConnectionFSM::network_callback(nsapi_event_t ev, intptr_t ptr)
567568
{
568569

569-
tr_debug("FSM: network_callback called with event: %d, intptr: %d", ev, ptr);
570+
tr_info("FSM: network_callback called with event: %d, intptr: %d", ev, ptr);
570571
if ((cellular_connection_status_t)ev == CellularRegistrationStatusChanged && ptr == CellularNetwork::RegisteredHomeNetwork && _state == STATE_REGISTERING_NETWORK) {
571572
_queue.cancel(_eventID);
572573
continue_from_state(STATE_ATTACHING_NETWORK);

features/cellular/framework/API/CellularPower.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
#include "nsapi_types.h"
2121
#include "Callback.h"
2222

23-
namespace mbed {
23+
namespace mbed
24+
{
2425

2526
/**
2627
* Class CellularPower
@@ -121,7 +122,7 @@ class CellularPower
121122
virtual nsapi_error_t opt_receive_period(int mode, EDRXAccessTechnology act_type, uint8_t edrx_value) = 0;
122123

123124
/** Set URC callback function for device specific ready urc. URC is defined in device specific
124-
* header (for example in QUECTEL_BG96.h). Used in startup sequence to listen when device is ready
125+
* power API. Used in startup sequence to listen when device is ready
125126
* for using at commands and possible sim.
126127
*
127128
* @param callback Callback function called when urc received

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ nsapi_error_t ATHandler::set_urc_handler(const char *prefix, mbed::Callback<void
174174
}
175175

176176
oob->prefix = prefix;
177+
oob->prefix_len = prefix_len;
177178
oob->cb = callback;
178179
oob->next = _oobs;
179180
_oobs = oob;
@@ -278,6 +279,7 @@ void ATHandler::process_oob()
278279
timer.start();
279280
do {
280281
if (match_urc()) {
282+
timer.reset();
281283
if (_fileHandle->readable() || (_recv_pos < _recv_len)) {
282284
continue;
283285
}
@@ -286,16 +288,18 @@ void ATHandler::process_oob()
286288
// If no match found, look for CRLF and consume everything up to CRLF
287289
if (mem_str(_recv_buff, _recv_len, CRLF, CRLF_LENGTH)) {
288290
consume_to_tag(CRLF, true);
291+
timer.reset();
289292
} else {
290293
if (_fileHandle->readable()) {
294+
timer.reset();
291295
fill_buffer();
292296
} else {
293297
#ifdef MBED_CONF_RTOS_PRESENT
294298
rtos::Thread::yield();
295299
#endif
296300
}
297301
}
298-
} while (timer.read_ms() < 20); // URC's are very short so 20ms should be enough
302+
} while (timer.read_ms() < 100); // URC's are very short
299303
}
300304
tr_debug("process_oob exit");
301305

@@ -316,7 +320,8 @@ void ATHandler::set_filehandle_sigio()
316320
void ATHandler::reset_buffer()
317321
{
318322
tr_debug("%s", __func__);
319-
_recv_pos = 0; _recv_len = 0;
323+
_recv_pos = 0;
324+
_recv_len = 0;
320325
}
321326

322327
void ATHandler::rewind_buffer()
@@ -607,10 +612,10 @@ bool ATHandler::match_urc()
607612
rewind_buffer();
608613
size_t prefix_len = 0;
609614
for (struct oob_t *oob = _oobs; oob; oob = oob->next) {
610-
prefix_len = strlen(oob->prefix);
615+
prefix_len = oob->prefix_len;
611616
if (_recv_len >= prefix_len) {
612617
if (match(oob->prefix, prefix_len)) {
613-
tr_debug("URC! %s", oob->prefix);
618+
tr_debug("URC! %s\n", oob->prefix);
614619
set_scope(InfoType);
615620
if (oob->cb) {
616621
oob->cb();

features/cellular/framework/AT/ATHandler.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
#include "Callback.h"
3030
#include "EventQueue.h"
3131

32-
namespace mbed {
32+
namespace mbed
33+
{
3334

3435
class FileHandle;
3536

@@ -196,6 +197,7 @@ class ATHandler
196197

197198
struct oob_t {
198199
const char *prefix;
200+
int prefix_len;
199201
mbed::Callback<void()> cb;
200202
oob_t *next;
201203
};
@@ -211,7 +213,7 @@ class ATHandler
211213
bool _processing;
212214
int32_t _ref_count;
213215

214-
//*************************************
216+
//*************************************
215217
public:
216218

217219
/** Starts the command writing by clearing the last error and writing the given command.

features/cellular/framework/AT/AT_CellularPower.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,9 @@ nsapi_error_t AT_CellularPower::opt_receive_period(int mode, EDRXAccessTechnolog
236236

237237
nsapi_error_t AT_CellularPower::set_device_ready_urc_cb(mbed::Callback<void()> callback)
238238
{
239-
#ifdef DEVICE_READY_URC
240-
return _at.set_urc_handler(DEVICE_READY_URC, callback);
241-
#else
242239
return NSAPI_ERROR_UNSUPPORTED;
243-
#endif // DEVICE_READY_URC
244240
}
245241

246242
void AT_CellularPower::remove_device_ready_urc_cb(mbed::Callback<void()> callback)
247243
{
248-
#ifdef DEVICE_READY_URC
249-
_at.remove_urc_handler(DEVICE_READY_URC, callback);
250-
#endif // DEVICE_READY_URC
251244
}

features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020

2121
#include "AT_CellularDevice.h"
2222

23-
namespace mbed {
24-
25-
#define DEVICE_READY_URC "CPIN:"
23+
namespace mbed
24+
{
2625

2726
class QUECTEL_BG96 : public AT_CellularDevice
2827
{
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2018, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "QUECTEL_BG96_CellularPower.h"
19+
20+
#define DEVICE_READY_URC "CPIN:"
21+
22+
using namespace mbed;
23+
24+
QUECTEL_BG96_CellularPower::QUECTEL_BG96_CellularPower(ATHandler &atHandler) : AT_CellularPower(atHandler)
25+
{
26+
}
27+
28+
nsapi_error_t QUECTEL_BG96_CellularPower::set_device_ready_urc_cb(mbed::Callback<void()> callback)
29+
{
30+
return _at.set_urc_handler(DEVICE_READY_URC, callback);
31+
}
32+
33+
void QUECTEL_BG96_CellularPower::remove_device_ready_urc_cb(mbed::Callback<void()> callback)
34+
{
35+
_at.remove_urc_handler(DEVICE_READY_URC, callback);
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2018, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef QUECTEL_BG96_CELLULAR_POWER_H_
19+
#define QUECTEL_BG96_CELLULAR_POWER_H_
20+
21+
#include "AT_CellularPower.h"
22+
23+
namespace mbed
24+
{
25+
26+
class QUECTEL_BG96_CellularPower : public AT_CellularPower
27+
{
28+
public:
29+
QUECTEL_BG96_CellularPower(ATHandler &atHandler);
30+
virtual ~QUECTEL_BG96_CellularPower();
31+
32+
public: //from CellularPower
33+
virtual nsapi_error_t set_device_ready_urc_cb(mbed::Callback<void()> callback);
34+
virtual void remove_device_ready_urc_cb(mbed::Callback<void()> callback);
35+
};
36+
37+
} // namespace mbed
38+
39+
#endif // QUECTEL_BG96_CELLULAR_POWER_H_

0 commit comments

Comments
 (0)