Skip to content

Cellular: Convert to Chrono #12430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

using namespace mbed;
using namespace events;
using namespace std::chrono_literals;

// AStyle ignored as the definition is not clear due to preprocessor usage
// *INDENT-OFF*
Expand All @@ -44,7 +45,7 @@ class TestAT_CellularContext : public testing::Test {
ATHandler_stub::nsapi_error_value = 0;
ATHandler_stub::nsapi_error_ok_counter = 0;
ATHandler_stub::int_value = -1;
ATHandler_stub::timeout = 0;
ATHandler_stub::timeout = 0s;
ATHandler_stub::default_timeout = 0;
ATHandler_stub::debug_on = 0;
ATHandler_stub::ssize_value = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

using namespace mbed;
using namespace events;
using namespace std::chrono_literals;

class TestAT_CellularDevice : public testing::Test {
protected:
Expand Down Expand Up @@ -129,17 +130,17 @@ TEST_F(TestAT_CellularDevice, test_AT_CellularDevice_set_timeout)
{
FileHandle_stub fh1;
AT_CellularDevice dev(&fh1);
ATHandler_stub::timeout = 0;
ATHandler_stub::timeout = 0s;
ATHandler_stub::default_timeout = false;

dev.set_timeout(5000);
EXPECT_TRUE(ATHandler_stub::timeout == 5000);
EXPECT_TRUE(ATHandler_stub::timeout == 5s);
EXPECT_TRUE(ATHandler_stub::default_timeout == true);

EXPECT_TRUE(dev.open_sms());

dev.set_timeout(5000);
EXPECT_TRUE(ATHandler_stub::timeout == 5000);
EXPECT_TRUE(ATHandler_stub::timeout == 5s);
EXPECT_TRUE(ATHandler_stub::default_timeout == true);

dev.close_sms();
Expand Down
14 changes: 12 additions & 2 deletions UNITTESTS/stubs/ATHandler_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const uint8_t MAX_RESP_LENGTH = 7;
nsapi_error_t ATHandler_stub::nsapi_error_value = 0;
uint8_t ATHandler_stub::nsapi_error_ok_counter = 0;
int ATHandler_stub::int_value = -1;
int ATHandler_stub::timeout = 0;
mbed::chrono::milliseconds_u32 ATHandler_stub::timeout{};
bool ATHandler_stub::default_timeout = 0;
bool ATHandler_stub::debug_on = 0;
ssize_t ATHandler_stub::ssize_value = 0;
Expand Down Expand Up @@ -81,6 +81,11 @@ void ATHandler_stub::debug_call_count_clear()
}

ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, uint32_t timeout, const char *output_delimiter, uint16_t send_delay) :
ATHandler(fh, queue, mbed::chrono::milliseconds_u32(timeout), output_delimiter, std::chrono::duration<uint16_t, std::milli>(send_delay))
{
}

ATHandler::ATHandler(FileHandle *fh, EventQueue &queue, mbed::chrono::milliseconds_u32 timeout, const char *output_delimiter, std::chrono::duration<uint16_t, std::milli> send_delay) :
#if defined AT_HANDLER_MUTEX && defined MBED_CONF_RTOS_PRESENT
_oobCv(_fileHandleMutex),
#endif
Expand Down Expand Up @@ -165,7 +170,12 @@ nsapi_error_t ATHandler::unlock_return_error()

void ATHandler::set_at_timeout(uint32_t timeout_milliseconds, bool default_timeout)
{
ATHandler_stub::timeout = timeout_milliseconds;
set_at_timeout(mbed::chrono::milliseconds_u32(timeout_milliseconds), default_timeout);
}

void ATHandler::set_at_timeout(mbed::chrono::milliseconds_u32 timeout, bool default_timeout)
{
ATHandler_stub::timeout = timeout;
ATHandler_stub::default_timeout = default_timeout;
}

Expand Down
2 changes: 1 addition & 1 deletion UNITTESTS/stubs/ATHandler_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace ATHandler_stub {
extern nsapi_error_t nsapi_error_value;
extern uint8_t nsapi_error_ok_counter;
extern int int_value;
extern int timeout;
extern mbed::chrono::milliseconds_u32 timeout;
extern bool default_timeout;
extern bool debug_on;
extern ssize_t ssize_value;
Expand Down
2 changes: 1 addition & 1 deletion UNITTESTS/stubs/AT_CellularDevice_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ AT_CellularInformation *AT_CellularDevice::open_information_impl(ATHandler &at)

void AT_CellularDevice::set_timeout(int timeout)
{
_default_timeout = timeout;
_default_timeout = std::chrono::duration<int, std::milli>(timeout);
}

void AT_CellularDevice::modem_debug_on(bool on)
Expand Down
2 changes: 1 addition & 1 deletion UNITTESTS/stubs/CellularStateMachine_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ void CellularStateMachine::set_retry_timeout_array(const uint16_t timeout[], int
}
}

void CellularStateMachine::set_timeout(int timeout)
void CellularStateMachine::set_timeout(std::chrono::duration<int, std::milli> timeout)
{
}
5 changes: 5 additions & 0 deletions UNITTESTS/stubs/Kernel_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
namespace rtos {

uint64_t Kernel::get_ms_count()
{
return impl::get_tick_count();

}
uint64_t Kernel::impl::get_tick_count()
{
return 20;
}
Expand Down
10 changes: 10 additions & 0 deletions UNITTESTS/stubs/Semaphore_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,21 @@ bool Semaphore::try_acquire_for(uint32_t millisec)
return Semaphore_stub::acquire_return_value;
}

bool Semaphore::try_acquire_for(Kernel::Clock::duration_u32 rel_time)
{
return Semaphore_stub::acquire_return_value;
}

bool Semaphore::try_acquire_until(uint64_t millisec)
{
return Semaphore_stub::acquire_return_value;
}

bool Semaphore::try_acquire_until(Kernel::Clock::time_point abs_time)
{
return Semaphore_stub::acquire_return_value;
}

osStatus Semaphore::release(void)
{
return 0;
Expand Down
8 changes: 8 additions & 0 deletions UNITTESTS/stubs/ThisThread_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ void ThisThread::sleep_until(uint64_t millisec)
{
}

void ThisThread::sleep_until(Kernel::Clock::time_point abs_time)
{
}

void ThisThread::sleep_for(uint32_t millisec)
{
}

void ThisThread::sleep_for(Kernel::Clock::duration_u32 rel_time)
{
}

}
36 changes: 31 additions & 5 deletions features/cellular/framework/API/ATHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
#define AT_HANDLER_H_

#include "platform/mbed_retarget.h"
#include "platform/mbed_chrono.h"

#include "events/EventQueue.h"
#include "nsapi_types.h"

#include "Callback.h"
#include "rtos/Kernel.h"

#include <cstdarg>

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

/** Constructor
*
* @param fh file handle used for reading AT responses and writing AT commands
* @param queue Event queue used to transfer sigio events to this thread
* @param timeout Timeout when reading for AT response
* @param output_delimiter delimiter used when parsing at responses, "\r" should be used as output_delimiter
* @param send_delay the minimum delay in ms between the end of last response and the beginning of a new command
*/
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));

~ATHandler();

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

/** Set timeout in milliseconds for AT commands
*
* @param timeout Timeout in milliseconds
* @param default_timeout Store as default timeout
*/
void set_at_timeout(mbed::chrono::milliseconds_u32 timeout, bool default_timeout = false);

/** Restore timeout to previous timeout. Handy if there is a need to change timeout temporarily.
*/
void restore_at_timeout();
Expand Down Expand Up @@ -157,6 +176,13 @@ class ATHandler {
*/
bool sync(int timeout_ms);

/** Synchronize AT command and response handling to modem.
*
* @param timeout ATHandler timeout when trying to sync. Will be restored when function returns.
* @return true is synchronization was successful, false in case of failure
*/
bool sync(std::chrono::duration<int, std::milli> timeout);

/** Sets the delay to be applied before sending any AT command.
*
* @param send_delay the minimum delay in ms between the end of last response and the beginning of a new command
Expand Down Expand Up @@ -565,11 +591,11 @@ class ATHandler {
char *_output_delimiter;

oob_t *_oobs;
uint32_t _at_timeout;
uint32_t _previous_at_timeout;
mbed::chrono::milliseconds_u32 _at_timeout;
mbed::chrono::milliseconds_u32 _previous_at_timeout;

uint16_t _at_send_delay;
uint64_t _last_response_stop;
std::chrono::duration<uint16_t, std::milli> _at_send_delay;
rtos::Kernel::Clock::time_point _last_response_stop;

int32_t _ref_count;
bool _is_fh_usable;
Expand Down Expand Up @@ -610,7 +636,7 @@ class ATHandler {
bool _use_delimiter;

// time when a command or an URC processing was started
uint64_t _start_time;
rtos::Kernel::Clock::time_point _start_time;
// eventqueue event id
int _event_id;

Expand Down
22 changes: 12 additions & 10 deletions features/cellular/framework/AT/AT_CellularContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
#endif // #if DEVICE_SERIAL
#include "ThisThread.h"

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

#if NSAPI_PPP_AVAILABLE
#define AT_SYNC_TIMEOUT 1000 // 1 second timeout
#define AT_SYNC_TIMEOUT 1s
#include "nsapi_ppp.h"
#endif

Expand All @@ -45,6 +45,7 @@
using namespace mbed_cellular_util;
using namespace mbed;
using namespace rtos;
using namespace std::chrono_literals;

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

uint32_t AT_CellularContext::get_timeout_for_operation(ContextOperation op) const
{
uint32_t timeout = NETWORK_TIMEOUT; // default timeout is 30 minutes as registration and attach may take time
std::chrono::duration<uint32_t, std::milli> timeout = NETWORK_TIMEOUT; // default timeout is 30 minutes as registration and attach may take time
if (op == OP_SIM_READY || op == OP_DEVICE_READY) {
timeout = DEVICE_TIMEOUT; // use 5 minutes for device ready and sim
}
return timeout;
return timeout.count();
}

bool AT_CellularContext::is_connected()
Expand Down Expand Up @@ -716,7 +718,7 @@ nsapi_error_t AT_CellularContext::disconnect()
do_disconnect();
return _cb_data.error;
} else {
int event_id = _device->get_queue()->call_in(0, this, &AT_CellularContext::do_disconnect);
int event_id = _device->get_queue()->call(this, &AT_CellularContext::do_disconnect);
if (event_id == 0) {
return NSAPI_ERROR_NO_MEMORY;
}
Expand All @@ -742,7 +744,7 @@ void AT_CellularContext::deactivate_context()
void AT_CellularContext::check_and_deactivate_context()
{
// CGACT and CGATT commands might take up to 3 minutes to respond.
_at.set_at_timeout(180 * 1000);
_at.set_at_timeout(3min);
int active_contexts_count = 0;
_is_context_active = _nw->is_active_context(&active_contexts_count, _cid);

Expand Down
11 changes: 6 additions & 5 deletions features/cellular/framework/AT/AT_CellularDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
using namespace mbed_cellular_util;
using namespace events;
using namespace mbed;
using namespace std::chrono_literals;

#define DEFAULT_AT_TIMEOUT 1000 // at default timeout in milliseconds
#define DEFAULT_AT_TIMEOUT 1s // at default timeout
const int MAX_SIM_RESPONSE_LENGTH = 16;

AT_CellularDevice::AT_CellularDevice(FileHandle *fh) :
Expand Down Expand Up @@ -388,7 +389,7 @@ void AT_CellularDevice::close_information()

void AT_CellularDevice::set_timeout(int timeout)
{
_default_timeout = timeout;
_default_timeout = std::chrono::duration<int, std::milli>(timeout);

_at.set_at_timeout(_default_timeout, true);

Expand Down Expand Up @@ -420,7 +421,7 @@ nsapi_error_t AT_CellularDevice::init()
}
}
tr_debug("Wait 100ms to init modem");
rtos::ThisThread::sleep_for(100); // let modem have time to get ready
rtos::ThisThread::sleep_for(100ms); // let modem have time to get ready
}

return _at.unlock_return_error();
Expand Down Expand Up @@ -577,7 +578,7 @@ void AT_CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr, Cellul
cellular_connection_status_t cell_ev = (cellular_connection_status_t)ev;
if (cell_ev == CellularDeviceTimeout) {
cell_callback_data_t *data = (cell_callback_data_t *)ptr;
int timeout = *(int *)data->data;
auto timeout = *(std::chrono::duration<int, std::milli> *)data->data;
if (_default_timeout != timeout) {
_default_timeout = timeout;
_at.set_at_timeout(_default_timeout, true);
Expand Down Expand Up @@ -611,7 +612,7 @@ nsapi_error_t AT_CellularDevice::set_baud_rate(int baud_rate)
_at.set_baud(baud_rate);

// Give some time before starting using the UART with the new baud rate
rtos::ThisThread::sleep_for(3000);
rtos::ThisThread::sleep_for(3s);
#else
// Currently ATHandler only supports BufferedSerial based communication and
// if serial is disabled, baud rate cannot be set
Expand Down
2 changes: 1 addition & 1 deletion features/cellular/framework/AT/AT_CellularDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class AT_CellularDevice : public CellularDevice {
AT_CellularInformation *_information;
AT_CellularContext *_context_list;

int _default_timeout;
std::chrono::duration<int, std::milli> _default_timeout;
bool _modem_debug_on;
const intptr_t *_property_array;
};
Expand Down
Loading