Skip to content

Commit 170931e

Browse files
authored
Merge pull request #12430 from kjbracey-arm/chrono_cellular
Cellular: Convert to Chrono
2 parents f4015c7 + 74a51b6 commit 170931e

32 files changed

+244
-147
lines changed

UNITTESTS/features/cellular/framework/AT/at_cellularcontext/at_cellularcontexttest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
using namespace mbed;
3535
using namespace events;
36+
using namespace std::chrono_literals;
3637

3738
// AStyle ignored as the definition is not clear due to preprocessor usage
3839
// *INDENT-OFF*
@@ -44,7 +45,7 @@ class TestAT_CellularContext : public testing::Test {
4445
ATHandler_stub::nsapi_error_value = 0;
4546
ATHandler_stub::nsapi_error_ok_counter = 0;
4647
ATHandler_stub::int_value = -1;
47-
ATHandler_stub::timeout = 0;
48+
ATHandler_stub::timeout = 0s;
4849
ATHandler_stub::default_timeout = 0;
4950
ATHandler_stub::debug_on = 0;
5051
ATHandler_stub::ssize_value = 0;

UNITTESTS/features/cellular/framework/AT/at_cellulardevice/at_cellulardevicetest.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
using namespace mbed;
2424
using namespace events;
25+
using namespace std::chrono_literals;
2526

2627
class TestAT_CellularDevice : public testing::Test {
2728
protected:
@@ -129,17 +130,17 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_set_timeout)
129130
{
130131
FileHandle_stub fh1;
131132
AT_CellularDevice dev(&fh1);
132-
ATHandler_stub::timeout = 0;
133+
ATHandler_stub::timeout = 0s;
133134
ATHandler_stub::default_timeout = false;
134135

135136
dev.set_timeout(5000);
136-
EXPECT_TRUE(ATHandler_stub::timeout == 5000);
137+
EXPECT_TRUE(ATHandler_stub::timeout == 5s);
137138
EXPECT_TRUE(ATHandler_stub::default_timeout == true);
138139

139140
EXPECT_TRUE(dev.open_sms());
140141

141142
dev.set_timeout(5000);
142-
EXPECT_TRUE(ATHandler_stub::timeout == 5000);
143+
EXPECT_TRUE(ATHandler_stub::timeout == 5s);
143144
EXPECT_TRUE(ATHandler_stub::default_timeout == true);
144145

145146
dev.close_sms();

UNITTESTS/stubs/ATHandler_stub.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const uint8_t MAX_RESP_LENGTH = 7;
3131
nsapi_error_t ATHandler_stub::nsapi_error_value = 0;
3232
uint8_t ATHandler_stub::nsapi_error_ok_counter = 0;
3333
int ATHandler_stub::int_value = -1;
34-
int ATHandler_stub::timeout = 0;
34+
mbed::chrono::milliseconds_u32 ATHandler_stub::timeout{};
3535
bool ATHandler_stub::default_timeout = 0;
3636
bool ATHandler_stub::debug_on = 0;
3737
ssize_t ATHandler_stub::ssize_value = 0;
@@ -81,6 +81,11 @@ void ATHandler_stub::debug_call_count_clear()
8181
}
8282

8383
ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, uint32_t timeout, const char *output_delimiter, uint16_t send_delay) :
84+
ATHandler(fh, queue, mbed::chrono::milliseconds_u32(timeout), output_delimiter, std::chrono::duration<uint16_t, std::milli>(send_delay))
85+
{
86+
}
87+
88+
ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, mbed::chrono::milliseconds_u32 timeout, const char *output_delimiter, std::chrono::duration<uint16_t, std::milli> send_delay) :
8489
#if defined AT_HANDLER_MUTEX && defined MBED_CONF_RTOS_PRESENT
8590
_oobCv(_fileHandleMutex),
8691
#endif
@@ -165,7 +170,12 @@ nsapi_error_t ATHandler::unlock_return_error()
165170

166171
void ATHandler::set_at_timeout(uint32_t timeout_milliseconds, bool default_timeout)
167172
{
168-
ATHandler_stub::timeout = timeout_milliseconds;
173+
set_at_timeout(mbed::chrono::milliseconds_u32(timeout_milliseconds), default_timeout);
174+
}
175+
176+
void ATHandler::set_at_timeout(mbed::chrono::milliseconds_u32 timeout, bool default_timeout)
177+
{
178+
ATHandler_stub::timeout = timeout;
169179
ATHandler_stub::default_timeout = default_timeout;
170180
}
171181

UNITTESTS/stubs/ATHandler_stub.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace ATHandler_stub {
3737
extern nsapi_error_t nsapi_error_value;
3838
extern uint8_t nsapi_error_ok_counter;
3939
extern int int_value;
40-
extern int timeout;
40+
extern mbed::chrono::milliseconds_u32 timeout;
4141
extern bool default_timeout;
4242
extern bool debug_on;
4343
extern ssize_t ssize_value;

UNITTESTS/stubs/AT_CellularDevice_stub.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ AT_CellularInformation *AT_CellularDevice::open_information_impl(ATHandler &at)
137137

138138
void AT_CellularDevice::set_timeout(int timeout)
139139
{
140-
_default_timeout = timeout;
140+
_default_timeout = std::chrono::duration<int, std::milli>(timeout);
141141
}
142142

143143
void AT_CellularDevice::modem_debug_on(bool on)

UNITTESTS/stubs/CellularStateMachine_stub.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,6 @@ void CellularStateMachine::set_retry_timeout_array(const uint16_t timeout[], int
9696
}
9797
}
9898

99-
void CellularStateMachine::set_timeout(int timeout)
99+
void CellularStateMachine::set_timeout(std::chrono::duration<int, std::milli> timeout)
100100
{
101101
}

UNITTESTS/stubs/Semaphore_stub.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,21 @@ bool Semaphore::try_acquire_for(uint32_t millisec)
5353
return Semaphore_stub::acquire_return_value;
5454
}
5555

56+
bool Semaphore::try_acquire_for(Kernel::Clock::duration_u32 rel_time)
57+
{
58+
return Semaphore_stub::acquire_return_value;
59+
}
60+
5661
bool Semaphore::try_acquire_until(uint64_t millisec)
5762
{
5863
return Semaphore_stub::acquire_return_value;
5964
}
6065

66+
bool Semaphore::try_acquire_until(Kernel::Clock::time_point abs_time)
67+
{
68+
return Semaphore_stub::acquire_return_value;
69+
}
70+
6171
osStatus Semaphore::release(void)
6272
{
6373
return 0;

UNITTESTS/stubs/ThisThread_stub.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ void ThisThread::sleep_until(uint64_t millisec)
2323
{
2424
}
2525

26+
void ThisThread::sleep_until(Kernel::Clock::time_point abs_time)
27+
{
28+
}
29+
2630
void ThisThread::sleep_for(uint32_t millisec)
2731
{
2832
}
2933

34+
void ThisThread::sleep_for(Kernel::Clock::duration_u32 rel_time)
35+
{
36+
}
37+
3038
}

features/cellular/framework/API/ATHandler.h

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
#define AT_HANDLER_H_
2020

2121
#include "platform/mbed_retarget.h"
22+
#include "platform/mbed_chrono.h"
2223

2324
#include "events/EventQueue.h"
2425
#include "nsapi_types.h"
2526

2627
#include "Callback.h"
28+
#include "rtos/Kernel.h"
2729

2830
#include <cstdarg>
2931

@@ -78,6 +80,16 @@ class ATHandler {
7880
*/
7981
ATHandler(FileHandle *fh, events::EventQueue &queue, uint32_t timeout, const char *output_delimiter, uint16_t send_delay = 0);
8082

83+
/** Constructor
84+
*
85+
* @param fh file handle used for reading AT responses and writing AT commands
86+
* @param queue Event queue used to transfer sigio events to this thread
87+
* @param timeout Timeout when reading for AT response
88+
* @param output_delimiter delimiter used when parsing at responses, "\r" should be used as output_delimiter
89+
* @param send_delay the minimum delay in ms between the end of last response and the beginning of a new command
90+
*/
91+
ATHandler(FileHandle *fh, events::EventQueue &queue, mbed::chrono::milliseconds_u32 timeout, const char *output_delimiter, std::chrono::duration<uint16_t, std::milli> send_delay = std::chrono::milliseconds(0));
92+
8193
~ATHandler();
8294

8395
/** Return used file handle.
@@ -126,6 +138,13 @@ class ATHandler {
126138
*/
127139
void set_at_timeout(uint32_t timeout_milliseconds, bool default_timeout = false);
128140

141+
/** Set timeout in milliseconds for AT commands
142+
*
143+
* @param timeout Timeout in milliseconds
144+
* @param default_timeout Store as default timeout
145+
*/
146+
void set_at_timeout(mbed::chrono::milliseconds_u32 timeout, bool default_timeout = false);
147+
129148
/** Restore timeout to previous timeout. Handy if there is a need to change timeout temporarily.
130149
*/
131150
void restore_at_timeout();
@@ -157,6 +176,13 @@ class ATHandler {
157176
*/
158177
bool sync(int timeout_ms);
159178

179+
/** Synchronize AT command and response handling to modem.
180+
*
181+
* @param timeout ATHandler timeout when trying to sync. Will be restored when function returns.
182+
* @return true is synchronization was successful, false in case of failure
183+
*/
184+
bool sync(std::chrono::duration<int, std::milli> timeout);
185+
160186
/** Sets the delay to be applied before sending any AT command.
161187
*
162188
* @param send_delay the minimum delay in ms between the end of last response and the beginning of a new command
@@ -565,11 +591,11 @@ class ATHandler {
565591
char *_output_delimiter;
566592

567593
oob_t *_oobs;
568-
uint32_t _at_timeout;
569-
uint32_t _previous_at_timeout;
594+
mbed::chrono::milliseconds_u32 _at_timeout;
595+
mbed::chrono::milliseconds_u32 _previous_at_timeout;
570596

571-
uint16_t _at_send_delay;
572-
uint64_t _last_response_stop;
597+
std::chrono::duration<uint16_t, std::milli> _at_send_delay;
598+
rtos::Kernel::Clock::time_point _last_response_stop;
573599

574600
int32_t _ref_count;
575601
bool _is_fh_usable;
@@ -610,7 +636,7 @@ class ATHandler {
610636
bool _use_delimiter;
611637

612638
// time when a command or an URC processing was started
613-
uint64_t _start_time;
639+
rtos::Kernel::Clock::time_point _start_time;
614640
// eventqueue event id
615641
int _event_id;
616642

features/cellular/framework/AT/AT_CellularContext.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
#endif // #if DEVICE_SERIAL
2828
#include "ThisThread.h"
2929

30-
#define NETWORK_TIMEOUT 30 * 60 * 1000 // 30 minutes
31-
#define DEVICE_TIMEOUT 5 * 60 * 1000 // 5 minutes
30+
#define NETWORK_TIMEOUT 30min
31+
#define DEVICE_TIMEOUT 5min
3232
// Timeout to wait for URC indicating ciot optimization support from network
33-
#define CP_OPT_NW_REPLY_TIMEOUT 3000 // 3 seconds
33+
#define CP_OPT_NW_REPLY_TIMEOUT 3s
3434

3535
#if NSAPI_PPP_AVAILABLE
36-
#define AT_SYNC_TIMEOUT 1000 // 1 second timeout
36+
#define AT_SYNC_TIMEOUT 1s
3737
#include "nsapi_ppp.h"
3838
#endif
3939

@@ -45,6 +45,7 @@
4545
using namespace mbed_cellular_util;
4646
using namespace mbed;
4747
using namespace rtos;
48+
using namespace std::chrono_literals;
4849

4950
AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) :
5051
_current_op(OP_INVALID), _dcd_pin(NC), _active_high(false), _cp_req(cp_req), _is_connected(false), _at(at)
@@ -118,7 +119,7 @@ nsapi_error_t AT_CellularContext::connect()
118119
} else {
119120
if (_cb_data.error == NSAPI_ERROR_ALREADY) {
120121
// device is already attached, to be async we must use queue to connect and give proper callbacks
121-
int id = _device->get_queue()->call_in(0, this, &AT_CellularContext::do_connect_with_retry);
122+
int id = _device->get_queue()->call(this, &AT_CellularContext::do_connect_with_retry);
122123
if (id == 0) {
123124
return NSAPI_ERROR_NO_MEMORY;
124125
}
@@ -162,7 +163,8 @@ nsapi_error_t AT_CellularContext::check_operation(nsapi_error_t err, ContextOper
162163
_current_op = op;
163164
if (err == NSAPI_ERROR_IN_PROGRESS || err == NSAPI_ERROR_OK) {
164165
if (_is_blocking) {
165-
int sema_acq = _semaphore.try_acquire_for(get_timeout_for_operation(op)); // cellular network searching may take several minutes
166+
auto d = std::chrono::duration<uint32_t, std::milli>(get_timeout_for_operation(op));
167+
int sema_acq = _semaphore.try_acquire_for(d); // cellular network searching may take several minutes
166168
if (!sema_acq) {
167169
tr_warning("No cellular connection");
168170
return NSAPI_ERROR_TIMEOUT;
@@ -181,11 +183,11 @@ nsapi_connection_status_t AT_CellularContext::get_connection_status() const
181183

182184
uint32_t AT_CellularContext::get_timeout_for_operation(ContextOperation op) const
183185
{
184-
uint32_t timeout = NETWORK_TIMEOUT; // default timeout is 30 minutes as registration and attach may take time
186+
std::chrono::duration<uint32_t, std::milli> timeout = NETWORK_TIMEOUT; // default timeout is 30 minutes as registration and attach may take time
185187
if (op == OP_SIM_READY || op == OP_DEVICE_READY) {
186188
timeout = DEVICE_TIMEOUT; // use 5 minutes for device ready and sim
187189
}
188-
return timeout;
190+
return timeout.count();
189191
}
190192

191193
bool AT_CellularContext::is_connected()
@@ -716,7 +718,7 @@ nsapi_error_t AT_CellularContext::disconnect()
716718
do_disconnect();
717719
return _cb_data.error;
718720
} else {
719-
int event_id = _device->get_queue()->call_in(0, this, &AT_CellularContext::do_disconnect);
721+
int event_id = _device->get_queue()->call(this, &AT_CellularContext::do_disconnect);
720722
if (event_id == 0) {
721723
return NSAPI_ERROR_NO_MEMORY;
722724
}
@@ -742,7 +744,7 @@ void AT_CellularContext::deactivate_context()
742744
void AT_CellularContext::check_and_deactivate_context()
743745
{
744746
// CGACT and CGATT commands might take up to 3 minutes to respond.
745-
_at.set_at_timeout(180 * 1000);
747+
_at.set_at_timeout(3min);
746748
int active_contexts_count = 0;
747749
_is_context_active = _nw->is_active_context(&active_contexts_count, _cid);
748750

features/cellular/framework/AT/AT_CellularDevice.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
using namespace mbed_cellular_util;
3535
using namespace events;
3636
using namespace mbed;
37+
using namespace std::chrono_literals;
3738

38-
#define DEFAULT_AT_TIMEOUT 1000 // at default timeout in milliseconds
39+
#define DEFAULT_AT_TIMEOUT 1s // at default timeout
3940
const int MAX_SIM_RESPONSE_LENGTH = 16;
4041

4142
AT_CellularDevice::AT_CellularDevice(FileHandle *fh) :
@@ -388,7 +389,7 @@ void AT_CellularDevice::close_information()
388389

389390
void AT_CellularDevice::set_timeout(int timeout)
390391
{
391-
_default_timeout = timeout;
392+
_default_timeout = std::chrono::duration<int, std::milli>(timeout);
392393

393394
_at.set_at_timeout(_default_timeout, true);
394395

@@ -420,7 +421,7 @@ nsapi_error_t AT_CellularDevice::init()
420421
}
421422
}
422423
tr_debug("Wait 100ms to init modem");
423-
rtos::ThisThread::sleep_for(100); // let modem have time to get ready
424+
rtos::ThisThread::sleep_for(100ms); // let modem have time to get ready
424425
}
425426

426427
return _at.unlock_return_error();
@@ -577,7 +578,7 @@ void AT_CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr, Cellul
577578
cellular_connection_status_t cell_ev = (cellular_connection_status_t)ev;
578579
if (cell_ev == CellularDeviceTimeout) {
579580
cell_callback_data_t *data = (cell_callback_data_t *)ptr;
580-
int timeout = *(int *)data->data;
581+
auto timeout = *(std::chrono::duration<int, std::milli> *)data->data;
581582
if (_default_timeout != timeout) {
582583
_default_timeout = timeout;
583584
_at.set_at_timeout(_default_timeout, true);
@@ -611,7 +612,7 @@ nsapi_error_t AT_CellularDevice::set_baud_rate(int baud_rate)
611612
_at.set_baud(baud_rate);
612613

613614
// Give some time before starting using the UART with the new baud rate
614-
rtos::ThisThread::sleep_for(3000);
615+
rtos::ThisThread::sleep_for(3s);
615616
#else
616617
// Currently ATHandler only supports BufferedSerial based communication and
617618
// if serial is disabled, baud rate cannot be set

features/cellular/framework/AT/AT_CellularDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class AT_CellularDevice : public CellularDevice {
197197
AT_CellularInformation *_information;
198198
AT_CellularContext *_context_list;
199199

200-
int _default_timeout;
200+
std::chrono::duration<int, std::milli> _default_timeout;
201201
bool _modem_debug_on;
202202
const intptr_t *_property_array;
203203
};

0 commit comments

Comments
 (0)