Use std::chrono based functions

The chrono based functions improve readability. Using them also removes
warnings related to usage of deprecated warnings.
pull/13046/head
Hugues Kamba 2020-05-29 23:11:21 +01:00
parent c7759fe505
commit a3f4cf212d
23 changed files with 133 additions and 58 deletions

View File

@ -23,13 +23,14 @@
#include "utest.h"
#include "udp_tests.h"
using namespace std::chrono;
using namespace utest::v1;
namespace {
static const int SIGNAL_SIGIO_RX = 0x1;
static const int SIGNAL_SIGIO_TX = 0x2;
static const int SIGIO_TIMEOUT = 5000; //[ms]
static const int SOCKET_TIMEOUT = (10 * 1000); //[ms]
static constexpr seconds SIGIO_TIMEOUT = 5s;
static constexpr seconds SOCKET_TIMEOUT = 10s;
static const int RETRIES = 2;
static const double EXPECTED_LOSS_RATIO = 0.0;

View File

@ -35,6 +35,8 @@
#include "Timeout.h"
#include "platform/mbed_error.h"
using namespace std::chrono;
#define TRACE_GROUP "AtRF"
/*Worst case sensitivity*/
@ -349,9 +351,9 @@ static rf_trx_part_e rf_radio_type_read(void)
static void rf_if_ack_wait_timer_start(uint16_t slots)
{
#ifdef MBED_CONF_RTOS_PRESENT
rf->ack_timer.attach_us(rf_if_ack_timer_signal, slots * 50);
rf->ack_timer.attach(rf_if_ack_timer_signal, slots * 50us);
#else
rf->ack_timer.attach_us(rf_ack_wait_timer_interrupt, slots * 50);
rf->ack_timer.attach(rf_ack_wait_timer_interrupt, slots * 50us);
#endif
}
@ -365,9 +367,9 @@ static void rf_if_ack_wait_timer_start(uint16_t slots)
static void rf_if_calibration_timer_start(uint32_t slots)
{
#ifdef MBED_CONF_RTOS_PRESENT
rf->cal_timer.attach_us(rf_if_cal_timer_signal, slots * 50);
rf->cal_timer.attach(rf_if_cal_timer_signal, slots * 50us);
#else
rf->cal_timer.attach_us(rf_calibration_timer_interrupt, slots * 50);
rf->cal_timer.attach(rf_calibration_timer_interrupt, slots * 50us);
#endif
}
@ -381,9 +383,9 @@ static void rf_if_calibration_timer_start(uint32_t slots)
static void rf_if_cca_timer_start(uint32_t slots)
{
#ifdef MBED_CONF_RTOS_PRESENT
rf->cca_timer.attach_us(rf_if_cca_timer_signal, slots * 50);
rf->cca_timer.attach(rf_if_cca_timer_signal, slots * 50us);
#else
rf->cca_timer.attach_us(rf_cca_timer_interrupt, slots * 50);
rf->cca_timer.attach(rf_cca_timer_interrupt, slots * 50us);
#endif
}
@ -512,14 +514,14 @@ static void rf_if_reset_radio(void)
#endif
rf->IRQ.rise(nullptr);
rf->RST = 1;
ThisThread::sleep_for(2);
ThisThread::sleep_for(2ms);
rf->RST = 0;
ThisThread::sleep_for(10);
ThisThread::sleep_for(10ms);
CS_RELEASE();
rf->SLP_TR = 0;
ThisThread::sleep_for(10);
ThisThread::sleep_for(10ms);
rf->RST = 1;
ThisThread::sleep_for(10);
ThisThread::sleep_for(10ms);
rf->IRQ.rise(&rf_if_interrupt_handler);
}

View File

@ -15,6 +15,7 @@
*/
#include <string.h>
#if defined(MBED_CONF_NANOSTACK_CONFIGURATION) && DEVICE_SPI && DEVICE_INTERRUPTIN && defined(MBED_CONF_RTOS_PRESENT)
#include "platform/arm_hal_interrupt.h"
#include "nanostack/platform/arm_hal_phy.h"
#include "ns_types.h"
@ -31,6 +32,7 @@
#include "mbed_wait_api.h"
#include "platform/mbed_error.h"
using namespace std::chrono;
using namespace mbed;
using namespace rtos;
@ -273,7 +275,7 @@ static void rf_calculate_symbol_rate(uint32_t baudrate, phy_modulation_e modulat
static uint32_t rf_get_timestamp(void)
{
return (uint32_t)rf->tx_timer.read_us();
return (uint32_t)rf->tx_timer.elapsed_time().count();
}
static void rf_update_tx_active_time(void)
@ -857,7 +859,7 @@ static void rf_cca_timer_stop(void)
static void rf_cca_timer_start(uint32_t slots)
{
rf->cca_timer.attach_us(rf_cca_timer_signal, slots);
rf->cca_timer.attach(rf_cca_timer_signal, microseconds(slots));
TEST_CSMA_STARTED
}
@ -894,7 +896,7 @@ static void rf_backup_timer_stop(void)
static void rf_backup_timer_start(uint32_t slots)
{
rf->backup_timer.attach_us(rf_backup_timer_signal, slots);
rf->backup_timer.attach(rf_backup_timer_signal, microseconds(slots));
}
static int8_t rf_start_cca(uint8_t *data_ptr, uint16_t data_length, uint8_t tx_handle, data_protocol_e data_protocol)
@ -1163,10 +1165,10 @@ static void rf_reset(void)
{
// Shutdown
rf->SDN = 1;
ThisThread::sleep_for(10);
ThisThread::sleep_for(10ms);
// Wake up
rf->SDN = 0;
ThisThread::sleep_for(10);
ThisThread::sleep_for(10ms);
}
static void rf_init(void)

View File

@ -28,6 +28,7 @@
#include "mbed_trace.h"
#define TRACE_GROUP "QSPIF"
using namespace std::chrono;
using namespace mbed;
/* Default QSPIF Parameters */
@ -1228,7 +1229,7 @@ bool QSPIFBlockDevice::_is_mem_ready()
bool mem_ready = true;
do {
rtos::ThisThread::sleep_for(1);
rtos::ThisThread::sleep_for(1ms);
retries++;
//Read Status Register 1 from device
if (QSPI_STATUS_OK != _qspi_send_general_command(QSPIF_INST_RSR1, QSPI_NO_ADDRESS_COMMAND,

View File

@ -20,6 +20,7 @@
#include <stdint.h>
#include <stddef.h>
#include "platform/mbed_assert.h"
#include "platform/mbed_chrono.h"
namespace ble {
@ -231,6 +232,48 @@ struct Duration {
return Duration(Forever::VALUE);
}
#if defined(DOXYGEN_ONLY)
/**
* Test if the forever value is being held
* @return True if the forever value is held False otherwise
*/
bool isForever() const;
#else
// Overload when Forever isn't defined
template<typename DefaultForever = void*>
std::enable_if_t<
std::is_same<DefaultForever, Forever>::value,
bool
>
isForever() const
{
return false;
}
// Overload when Forever is defined
template<typename DefaultForever = void*>
std::enable_if_t<
!std::is_same<DefaultForever, Forever>::value,
bool
>
isForever() const
{
return duration == Forever::VALUE;
}
#endif
/**
* Convert the duration into an std::chrono one.
* @return The duration in the std::chrono format.
*/
std::chrono::duration<Rep, typename std::ratio<TB, 1000000>::type>
valueChrono() const
{
MBED_ASSERT(!isForever());
return std::chrono::duration<Rep, typename std::ratio<TB, 1000000>::type>{duration};
}
private:
static Rep clamp(Rep in)
{

View File

@ -27,6 +27,8 @@
#include "drivers/Timeout.h"
using namespace std::chrono;
namespace ble {
namespace generic {
@ -1390,9 +1392,9 @@ void GenericGap<PalGapImpl, PalSecurityManager, ConnectionEventMonitorEventHandl
update_random_address();
// Schedule rotations every 15 minutes as recomended by the spec
_address_rotation_ticker.attach_us(
_address_rotation_ticker.attach(
mbed::callback(this, &GenericGap::on_address_rotation_timeout),
15 * 60 * 1000000U
15min
);
} else {
// Stop ticker
@ -1917,9 +1919,9 @@ ble_error_t GenericGap<PalGapImpl, PalSecurityManager, ConnectionEventMonitorEve
_advertising_timeout.detach();
if (maxDuration.value()) {
_advertising_timeout.attach_us(
_advertising_timeout.attach(
mbed::callback(this, &GenericGap::on_advertising_timeout),
durationCast<millisecond_t>(maxDuration).value()
maxDuration.valueChrono()
);
}
}
@ -2505,9 +2507,9 @@ ble_error_t GenericGap<PalGapImpl, PalSecurityManager, ConnectionEventMonitorEve
_scan_timeout.detach();
if (duration.value()) {
_scan_timeout.attach_us(
_scan_timeout.attach(
mbed::callback(this, &GenericGap::on_scan_timeout_),
microsecond_t(duration).value()
duration.valueChrono()
);
}
}

View File

@ -38,6 +38,8 @@
#include "CordioPalAttClient.h"
#include "CordioPalSecurityManager.h"
using namespace std::chrono;
/*! WSF handler ID */
wsfHandlerId_t stack_handler_id;
@ -258,9 +260,9 @@ void BLE::waitForEvent()
if (wsfOsReadyToSleep()) {
// setup an mbed timer for the next cordio timeout
nextTimestamp = (timestamp_t)(WsfTimerNextExpiration(&pTimerRunning) * WSF_MS_PER_TICK) * 1000;
nextTimestamp = (timestamp_t)(WsfTimerNextExpiration(&pTimerRunning) * WSF_MS_PER_TICK);
if (pTimerRunning) {
nextTimeout.attach_us(timeoutCallback, nextTimestamp);
nextTimeout.attach(timeoutCallback, milliseconds(nextTimestamp));
}
}
}
@ -541,7 +543,7 @@ void BLE::callDispatcher()
// process the external event queue
_event_queue.process();
_last_update_us += (uint64_t)_timer.read_high_resolution_us();
_last_update_us += (uint64_t)_timer.elapsed_time().count();
_timer.reset();
uint64_t last_update_ms = (_last_update_us / 1000);
@ -561,9 +563,9 @@ void BLE::callDispatcher()
if (wsfOsReadyToSleep()) {
// setup an mbed timer for the next Cordio timeout
bool_t pTimerRunning;
timestamp_t nextTimestamp = (timestamp_t) (WsfTimerNextExpiration(&pTimerRunning) * WSF_MS_PER_TICK) * 1000;
timestamp_t nextTimestamp = (timestamp_t) (WsfTimerNextExpiration(&pTimerRunning) * WSF_MS_PER_TICK);
if (pTimerRunning) {
nextTimeout.attach_us(timeoutCallback, nextTimestamp);
nextTimeout.attach(timeoutCallback, milliseconds(nextTimestamp));
} else {
critical_section.disable();
_hci_driver->on_host_stack_inactivity();

View File

@ -22,6 +22,7 @@
#include "CellularLog.h"
#include "rtos/ThisThread.h"
using namespace std::chrono;
using namespace rtos;
using namespace mbed;
using namespace events;
@ -86,7 +87,7 @@ nsapi_error_t ALT1250_PPP::soft_power_on()
tr_warn("Modem is not responding to AT commands, reset it");
if (_rst.is_connected()) {
_rst = 0;
ThisThread::sleep_for(100);
ThisThread::sleep_for(100ms);
_rst = 1;
}
if (_at.sync(2000)) {

View File

@ -26,12 +26,13 @@
#include "CellularLog.h"
#define PACKET_SIZE_MAX 1358
#define TXFULL_EVENT_TIMEOUT (1 * 1000) // ms
#define TXFULL_EVENT_TIMEOUT 1s
#define AT_UPLINK_BUSY 159
#define AT_UART_BUFFER_ERROR 536
#define AT_BACK_OFF_TIMER 537
using namespace std::chrono;
using namespace mbed;
using namespace mbed_cellular_util;
@ -230,7 +231,7 @@ retry_send:
if (retry < 3) {
retry++;
tr_warn("Socket %d sendto EAGAIN", socket->id);
rtos::ThisThread::sleep_for(30);
rtos::ThisThread::sleep_for(30ms);
_at.clear_error();
goto retry_send;
}

View File

@ -26,7 +26,7 @@
using namespace mbed;
using namespace mbed_cellular_util;
using namespace std::chrono_literals;
using namespace std::chrono;
RM1000_AT_CellularStack::RM1000_AT_CellularStack(ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type, AT_CellularDevice &device) :
AT_CellularStack(atHandler, cid, stack_type, device)
@ -266,7 +266,7 @@ nsapi_size_or_error_t RM1000_AT_CellularStack::socket_recvfrom_impl(CellularSock
// read() should not fail
success = false;
}
} else if (timer.read_ms() < SOCKET_TIMEOUT) {
} else if (timer.elapsed_time() < SOCKET_TIMEOUT) {
// Wait for URCs
_at.process_oob();
} else {

View File

@ -17,11 +17,14 @@
#ifndef RM1000_AT_CELLULARSTACK_H_
#define RM1000_AT_CELLULARSTACK_H_
#include <chrono>
#include "AT_CellularStack.h"
#include "CellularUtil.h"
#include "mbed_wait_api.h"
#include "drivers/Timer.h"
using namespace std::chrono;
namespace mbed {
@ -54,7 +57,7 @@ protected: // AT_CellularStack
* call to the functions here when they return NSAPI_ERROR_WOULD_BLOCK
* and the user has set a larger timeout or full blocking.
*/
static const int SOCKET_TIMEOUT = 1000;
static constexpr seconds SOCKET_TIMEOUT = 1s;
/** The maximum number of bytes in a packet that can be write/read from
* the AT interface in one go.

View File

@ -88,7 +88,7 @@ nsapi_error_t UBLOX_AT_CellularNetwork::ubx_reboot()
nsapi_error_t err = NSAPI_ERROR_OK;
Timer t1;
t1.start();
while (!(t1.read() >= 30)) {
while (!(t1.elapsed_time() >= 30s)) {
err = _at.at_cmd_discard("E0", "");
if (err == NSAPI_ERROR_OK) {
break;

View File

@ -177,7 +177,7 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_sendto_impl(CellularSocket
}
_at.cmd_start_stop("+USOST", "=", "%d%s%d%d", socket->id, address.get_ip_address(), address.get_port(), size);
_at.resp_start("@", true);
rtos::ThisThread::sleep_for(50); //wait for 50ms before sending data
rtos::ThisThread::sleep_for(50ms);
_at.write_bytes((uint8_t *)data, size);
@ -201,7 +201,7 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_sendto_impl(CellularSocket
}
_at.cmd_start_stop("+USOWR", "=", "%d%d", socket->id, blk);
_at.resp_start("@", true);
rtos::ThisThread::sleep_for(50); //wait for 50ms before sending data
rtos::ThisThread::sleep_for(50ms);
_at.write_bytes((uint8_t *)buf, blk);
@ -289,7 +289,7 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_recvfrom_impl(CellularSocke
// read() should not fail
success = false;
}
} else if (timer.read_ms() < SOCKET_TIMEOUT) {
} else if (timer.elapsed_time() < SOCKET_TIMEOUT) {
// Wait for URCs
_at.process_oob();
} else {
@ -332,7 +332,7 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_recvfrom_impl(CellularSocke
} else {
success = false;
}
} else if (timer.read_ms() < SOCKET_TIMEOUT) {
} else if (timer.elapsed_time() < SOCKET_TIMEOUT) {
// Wait for URCs
_at.process_oob();
} else {

View File

@ -17,11 +17,14 @@
#ifndef UBLOX_AT_CELLULARSTACK_H_
#define UBLOX_AT_CELLULARSTACK_H_
#include <chrono>
#include "AT_CellularStack.h"
#include "CellularUtil.h"
#include "rtos/ThisThread.h"
#include "drivers/Timer.h"
using namespace std::chrono;
namespace mbed {
@ -56,7 +59,7 @@ protected:
* call to the functions here when they return NSAPI_ERROR_WOULD_BLOCK
* and the user has set a larger timeout or full blocking.
*/
static const int SOCKET_TIMEOUT = 1000;
static constexpr seconds SOCKET_TIMEOUT = 1s;
/** The maximum number of bytes in a packet that can be write/read from
* the AT interface in one go.

View File

@ -185,7 +185,7 @@ nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_recvfrom_impl(CellularSoc
// read() should not fail
success = false;
}
} else if (timer.read_ms() < SOCKET_TIMEOUT) {
} else if (timer.elapsed_time() < SOCKET_TIMEOUT) {
// Wait for URCs
_at.process_oob();
} else {

View File

@ -17,11 +17,15 @@
#ifndef UBLOX_N2XX_CELLULARSTACK_H_
#define UBLOX_N2XX_CELLULARSTACK_H_
#include <chrono>
#include "AT_CellularStack.h"
#include "CellularUtil.h"
#include "mbed_wait_api.h"
#include "drivers/Timer.h"
using namespace std::chrono;
namespace mbed {
class UBLOX_N2XX_CellularStack : public AT_CellularStack {
@ -45,7 +49,7 @@ protected:
* call to the functions here when they return NSAPI_ERROR_WOULD_BLOCK
* and the user has set a larger timeout or full blocking.
*/
static const int SOCKET_TIMEOUT = 1000;
static constexpr seconds SOCKET_TIMEOUT = 1s;
/** The maximum number of bytes in a packet that can be write/read from
* the AT interface in one go.

View File

@ -21,6 +21,7 @@
#include "platform/SingletonPtr.h"
#include "Timeout.h"
using mbed::Timeout;
using namespace std::chrono;
// only one callback is active at any given time
static volatile utest_v1_harness_callback_t minimal_callback;
@ -46,13 +47,11 @@ static int32_t utest_us_ticker_init()
static void *utest_us_ticker_post(const utest_v1_harness_callback_t callback, timestamp_t delay_ms)
{
UTEST_LOG_FUNCTION();
timestamp_t delay_us = delay_ms *1000;
if (delay_ms) {
ticker_callback = callback;
// fire the interrupt in 1000us * delay_ms
utest_timeout_object->attach_us(ticker_handler, delay_us);
utest_timeout_object->attach(ticker_handler, milliseconds(delay_ms));
}
else {
minimal_callback = callback;

View File

@ -20,6 +20,8 @@ SPDX-License-Identifier: BSD-3-Clause
#include "LoRaWANTimer.h"
using namespace std::chrono;
LoRaWANTimeHandler::LoRaWANTimeHandler()
: _queue(NULL)
{
@ -53,7 +55,7 @@ void LoRaWANTimeHandler::init(timer_event_t &obj, mbed::Callback<void()> callbac
void LoRaWANTimeHandler::start(timer_event_t &obj, const uint32_t timeout)
{
obj.timer_id = _queue->call_in(timeout, obj.callback);
obj.timer_id = _queue->call_in(milliseconds(timeout), obj.callback);
MBED_ASSERT(obj.timer_id != 0);
}

View File

@ -13,11 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "NanostackEthernetInterface.h"
#include "mesh_system.h"
#include "callback_handler.h"
#include "enet_tasklet.h"
using namespace std::chrono_literals;
nsapi_error_t Nanostack::EthernetInterface::initialize()
{
nanostack_lock();
@ -78,7 +81,7 @@ nsapi_error_t Nanostack::EthernetInterface::bringup(bool dhcp, const char *ip,
}
if (blocking) {
bool acquired = connect_semaphore.try_acquire_for(30000);
bool acquired = connect_semaphore.try_acquire_for(30s);
if (!acquired) {
return NSAPI_ERROR_DHCP_FAILURE; // sort of...
@ -112,9 +115,9 @@ nsapi_error_t Nanostack::EthernetInterface::bringdown()
}
if (_blocking) {
int32_t count = disconnect_semaphore.try_acquire_for(30000);
bool acquired = disconnect_semaphore.try_acquire_for(30s);
if (count <= 0) {
if (!acquired) {
return NSAPI_ERROR_TIMEOUT;
}
}

View File

@ -26,6 +26,8 @@
#include "enet_tasklet.h"
#include "ip6string.h"
using namespace std::chrono_literals;
class PPPPhy : public NanostackPPPPhy {
public:
PPPPhy(NanostackMemoryManager &mem, PPP &m);
@ -86,8 +88,8 @@ nsapi_error_t Nanostack::PPPInterface::bringup(bool dhcp, const char *ip,
if (blocking) {
uint8_t retries = 10;
while (_connect_status != NSAPI_STATUS_GLOBAL_UP) {
int32_t count = connect_semaphore.try_acquire_for(3000);
if (count <= 0 && retries-- == 0) {
bool acquired = connect_semaphore.try_acquire_for(3s);
if (!acquired && retries-- == 0) {
return NSAPI_ERROR_DHCP_FAILURE; // sort of...
}
// Not up until global up
@ -153,9 +155,9 @@ nsapi_error_t Nanostack::PPPInterface::bringdown()
}
if (_blocking) {
int32_t count = disconnect_semaphore.try_acquire_for(30000);
bool acquired = disconnect_semaphore.try_acquire_for(30s);
if (count <= 0) {
if (!acquired) {
return NSAPI_ERROR_TIMEOUT;
}
}

View File

@ -24,6 +24,8 @@
// Default NetworkStack operations
using namespace std::chrono;
nsapi_error_t NetworkStack::get_ip_address(SocketAddress *address)
{
return NSAPI_ERROR_UNSUPPORTED;
@ -197,7 +199,7 @@ nsapi_error_t NetworkStack::call_in(int delay, mbed::Callback<void()> func)
}
if (delay > 0) {
if (event_queue->call_in(delay, func) == 0) {
if (event_queue->call_in(milliseconds(delay), func) == 0) {
goto NO_MEM;
}
} else {

View File

@ -222,7 +222,9 @@ void nfc_scheduler_timer_start(nfc_scheduler_timer_t *timer)
uint32_t nfc_scheduler_timer_get(nfc_scheduler_timer_t *timer)
{
Timer *mbed_timer = (Timer *)timer;
return (uint32_t)mbed_timer->read_ms();
return (uint32_t)std::chrono::duration_cast<std::chrono::milliseconds>(
mbed_timer->elapsed_time()
).count();
}
void nfc_scheduler_timer_stop(nfc_scheduler_timer_t *timer)

View File

@ -33,7 +33,7 @@ static void (*_rtc_write)(time_t t) = rtc_write;
#include "drivers/LowPowerTimer.h"
#define US_PER_SEC 1000000
using namespace std::chrono;
static SingletonPtr<mbed::LowPowerTimer> _rtc_lp_timer;
static uint64_t _rtc_lp_base;
@ -52,7 +52,7 @@ static int _rtc_lpticker_isenabled(void)
static time_t _rtc_lpticker_read(void)
{
return _rtc_lp_timer->read_high_resolution_us() / US_PER_SEC + _rtc_lp_base;
return duration_cast<seconds>(_rtc_lp_timer->elapsed_time()).count() + _rtc_lp_base;
}
static void _rtc_lpticker_write(time_t t)