Skip to content

Commit a3f4cf2

Browse files
committed
Use std::chrono based functions
The chrono based functions improve readability. Using them also removes warnings related to usage of deprecated warnings.
1 parent c7759fe commit a3f4cf2

File tree

23 files changed

+133
-58
lines changed

23 files changed

+133
-58
lines changed

TESTS/netsocket/udp/udpsocket_echotest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
#include "utest.h"
2424
#include "udp_tests.h"
2525

26+
using namespace std::chrono;
2627
using namespace utest::v1;
2728

2829
namespace {
2930
static const int SIGNAL_SIGIO_RX = 0x1;
3031
static const int SIGNAL_SIGIO_TX = 0x2;
31-
static const int SIGIO_TIMEOUT = 5000; //[ms]
32-
static const int SOCKET_TIMEOUT = (10 * 1000); //[ms]
32+
static constexpr seconds SIGIO_TIMEOUT = 5s;
33+
static constexpr seconds SOCKET_TIMEOUT = 10s;
3334
static const int RETRIES = 2;
3435

3536
static const double EXPECTED_LOSS_RATIO = 0.0;

components/802.15.4_RF/atmel-rf-driver/source/NanostackRfPhyAtmel.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include "Timeout.h"
3636
#include "platform/mbed_error.h"
3737

38+
using namespace std::chrono;
39+
3840
#define TRACE_GROUP "AtRF"
3941

4042
/*Worst case sensitivity*/
@@ -349,9 +351,9 @@ static rf_trx_part_e rf_radio_type_read(void)
349351
static void rf_if_ack_wait_timer_start(uint16_t slots)
350352
{
351353
#ifdef MBED_CONF_RTOS_PRESENT
352-
rf->ack_timer.attach_us(rf_if_ack_timer_signal, slots * 50);
354+
rf->ack_timer.attach(rf_if_ack_timer_signal, slots * 50us);
353355
#else
354-
rf->ack_timer.attach_us(rf_ack_wait_timer_interrupt, slots * 50);
356+
rf->ack_timer.attach(rf_ack_wait_timer_interrupt, slots * 50us);
355357
#endif
356358
}
357359

@@ -365,9 +367,9 @@ static void rf_if_ack_wait_timer_start(uint16_t slots)
365367
static void rf_if_calibration_timer_start(uint32_t slots)
366368
{
367369
#ifdef MBED_CONF_RTOS_PRESENT
368-
rf->cal_timer.attach_us(rf_if_cal_timer_signal, slots * 50);
370+
rf->cal_timer.attach(rf_if_cal_timer_signal, slots * 50us);
369371
#else
370-
rf->cal_timer.attach_us(rf_calibration_timer_interrupt, slots * 50);
372+
rf->cal_timer.attach(rf_calibration_timer_interrupt, slots * 50us);
371373
#endif
372374
}
373375

@@ -381,9 +383,9 @@ static void rf_if_calibration_timer_start(uint32_t slots)
381383
static void rf_if_cca_timer_start(uint32_t slots)
382384
{
383385
#ifdef MBED_CONF_RTOS_PRESENT
384-
rf->cca_timer.attach_us(rf_if_cca_timer_signal, slots * 50);
386+
rf->cca_timer.attach(rf_if_cca_timer_signal, slots * 50us);
385387
#else
386-
rf->cca_timer.attach_us(rf_cca_timer_interrupt, slots * 50);
388+
rf->cca_timer.attach(rf_cca_timer_interrupt, slots * 50us);
387389
#endif
388390
}
389391

@@ -512,14 +514,14 @@ static void rf_if_reset_radio(void)
512514
#endif
513515
rf->IRQ.rise(nullptr);
514516
rf->RST = 1;
515-
ThisThread::sleep_for(2);
517+
ThisThread::sleep_for(2ms);
516518
rf->RST = 0;
517-
ThisThread::sleep_for(10);
519+
ThisThread::sleep_for(10ms);
518520
CS_RELEASE();
519521
rf->SLP_TR = 0;
520-
ThisThread::sleep_for(10);
522+
ThisThread::sleep_for(10ms);
521523
rf->RST = 1;
522-
ThisThread::sleep_for(10);
524+
ThisThread::sleep_for(10ms);
523525

524526
rf->IRQ.rise(&rf_if_interrupt_handler);
525527
}

components/802.15.4_RF/stm-s2lp-rf-driver/source/NanostackRfPhys2lp.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#include <string.h>
1717
#if defined(MBED_CONF_NANOSTACK_CONFIGURATION) && DEVICE_SPI && DEVICE_INTERRUPTIN && defined(MBED_CONF_RTOS_PRESENT)
18+
1819
#include "platform/arm_hal_interrupt.h"
1920
#include "nanostack/platform/arm_hal_phy.h"
2021
#include "ns_types.h"
@@ -31,6 +32,7 @@
3132
#include "mbed_wait_api.h"
3233
#include "platform/mbed_error.h"
3334

35+
using namespace std::chrono;
3436
using namespace mbed;
3537
using namespace rtos;
3638

@@ -273,7 +275,7 @@ static void rf_calculate_symbol_rate(uint32_t baudrate, phy_modulation_e modulat
273275

274276
static uint32_t rf_get_timestamp(void)
275277
{
276-
return (uint32_t)rf->tx_timer.read_us();
278+
return (uint32_t)rf->tx_timer.elapsed_time().count();
277279
}
278280

279281
static void rf_update_tx_active_time(void)
@@ -857,7 +859,7 @@ static void rf_cca_timer_stop(void)
857859

858860
static void rf_cca_timer_start(uint32_t slots)
859861
{
860-
rf->cca_timer.attach_us(rf_cca_timer_signal, slots);
862+
rf->cca_timer.attach(rf_cca_timer_signal, microseconds(slots));
861863
TEST_CSMA_STARTED
862864
}
863865

@@ -894,7 +896,7 @@ static void rf_backup_timer_stop(void)
894896

895897
static void rf_backup_timer_start(uint32_t slots)
896898
{
897-
rf->backup_timer.attach_us(rf_backup_timer_signal, slots);
899+
rf->backup_timer.attach(rf_backup_timer_signal, microseconds(slots));
898900
}
899901

900902
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)
11631165
{
11641166
// Shutdown
11651167
rf->SDN = 1;
1166-
ThisThread::sleep_for(10);
1168+
ThisThread::sleep_for(10ms);
11671169
// Wake up
11681170
rf->SDN = 0;
1169-
ThisThread::sleep_for(10);
1171+
ThisThread::sleep_for(10ms);
11701172
}
11711173

11721174
static void rf_init(void)

components/storage/blockdevice/COMPONENT_QSPIF/QSPIFBlockDevice.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "mbed_trace.h"
2929
#define TRACE_GROUP "QSPIF"
3030

31+
using namespace std::chrono;
3132
using namespace mbed;
3233

3334
/* Default QSPIF Parameters */
@@ -1228,7 +1229,7 @@ bool QSPIFBlockDevice::_is_mem_ready()
12281229
bool mem_ready = true;
12291230

12301231
do {
1231-
rtos::ThisThread::sleep_for(1);
1232+
rtos::ThisThread::sleep_for(1ms);
12321233
retries++;
12331234
//Read Status Register 1 from device
12341235
if (QSPI_STATUS_OK != _qspi_send_general_command(QSPIF_INST_RSR1, QSPI_NO_ADDRESS_COMMAND,

features/FEATURE_BLE/ble/common/Duration.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <stdint.h>
2121
#include <stddef.h>
2222
#include "platform/mbed_assert.h"
23+
#include "platform/mbed_chrono.h"
2324

2425
namespace ble {
2526

@@ -231,6 +232,48 @@ struct Duration {
231232
return Duration(Forever::VALUE);
232233
}
233234

235+
#if defined(DOXYGEN_ONLY)
236+
/**
237+
* Test if the forever value is being held
238+
* @return True if the forever value is held False otherwise
239+
*/
240+
bool isForever() const;
241+
#else
242+
// Overload when Forever isn't defined
243+
template<typename DefaultForever = void*>
244+
std::enable_if_t<
245+
std::is_same<DefaultForever, Forever>::value,
246+
bool
247+
>
248+
isForever() const
249+
{
250+
return false;
251+
}
252+
253+
// Overload when Forever is defined
254+
template<typename DefaultForever = void*>
255+
std::enable_if_t<
256+
!std::is_same<DefaultForever, Forever>::value,
257+
bool
258+
>
259+
isForever() const
260+
{
261+
return duration == Forever::VALUE;
262+
}
263+
#endif
264+
265+
/**
266+
* Convert the duration into an std::chrono one.
267+
* @return The duration in the std::chrono format.
268+
*/
269+
std::chrono::duration<Rep, typename std::ratio<TB, 1000000>::type>
270+
valueChrono() const
271+
{
272+
MBED_ASSERT(!isForever());
273+
274+
return std::chrono::duration<Rep, typename std::ratio<TB, 1000000>::type>{duration};
275+
}
276+
234277
private:
235278
static Rep clamp(Rep in)
236279
{

features/FEATURE_BLE/source/generic/GenericGap.tpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#include "drivers/Timeout.h"
2929

30+
using namespace std::chrono;
31+
3032
namespace ble {
3133
namespace generic {
3234

@@ -1390,9 +1392,9 @@ void GenericGap<PalGapImpl, PalSecurityManager, ConnectionEventMonitorEventHandl
13901392
update_random_address();
13911393

13921394
// Schedule rotations every 15 minutes as recomended by the spec
1393-
_address_rotation_ticker.attach_us(
1395+
_address_rotation_ticker.attach(
13941396
mbed::callback(this, &GenericGap::on_address_rotation_timeout),
1395-
15 * 60 * 1000000U
1397+
15min
13961398
);
13971399
} else {
13981400
// Stop ticker
@@ -1917,9 +1919,9 @@ ble_error_t GenericGap<PalGapImpl, PalSecurityManager, ConnectionEventMonitorEve
19171919

19181920
_advertising_timeout.detach();
19191921
if (maxDuration.value()) {
1920-
_advertising_timeout.attach_us(
1922+
_advertising_timeout.attach(
19211923
mbed::callback(this, &GenericGap::on_advertising_timeout),
1922-
durationCast<millisecond_t>(maxDuration).value()
1924+
maxDuration.valueChrono()
19231925
);
19241926
}
19251927
}
@@ -2505,9 +2507,9 @@ ble_error_t GenericGap<PalGapImpl, PalSecurityManager, ConnectionEventMonitorEve
25052507

25062508
_scan_timeout.detach();
25072509
if (duration.value()) {
2508-
_scan_timeout.attach_us(
2510+
_scan_timeout.attach(
25092511
mbed::callback(this, &GenericGap::on_scan_timeout_),
2510-
microsecond_t(duration).value()
2512+
duration.valueChrono()
25112513
);
25122514
}
25132515
}

features/FEATURE_BLE/targets/TARGET_CORDIO/source/CordioBLE.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#include "CordioPalAttClient.h"
3939
#include "CordioPalSecurityManager.h"
4040

41+
using namespace std::chrono;
42+
4143
/*! WSF handler ID */
4244
wsfHandlerId_t stack_handler_id;
4345

@@ -258,9 +260,9 @@ void BLE::waitForEvent()
258260

259261
if (wsfOsReadyToSleep()) {
260262
// setup an mbed timer for the next cordio timeout
261-
nextTimestamp = (timestamp_t)(WsfTimerNextExpiration(&pTimerRunning) * WSF_MS_PER_TICK) * 1000;
263+
nextTimestamp = (timestamp_t)(WsfTimerNextExpiration(&pTimerRunning) * WSF_MS_PER_TICK);
262264
if (pTimerRunning) {
263-
nextTimeout.attach_us(timeoutCallback, nextTimestamp);
265+
nextTimeout.attach(timeoutCallback, milliseconds(nextTimestamp));
264266
}
265267
}
266268
}
@@ -541,7 +543,7 @@ void BLE::callDispatcher()
541543
// process the external event queue
542544
_event_queue.process();
543545

544-
_last_update_us += (uint64_t)_timer.read_high_resolution_us();
546+
_last_update_us += (uint64_t)_timer.elapsed_time().count();
545547
_timer.reset();
546548

547549
uint64_t last_update_ms = (_last_update_us / 1000);
@@ -561,9 +563,9 @@ void BLE::callDispatcher()
561563
if (wsfOsReadyToSleep()) {
562564
// setup an mbed timer for the next Cordio timeout
563565
bool_t pTimerRunning;
564-
timestamp_t nextTimestamp = (timestamp_t) (WsfTimerNextExpiration(&pTimerRunning) * WSF_MS_PER_TICK) * 1000;
566+
timestamp_t nextTimestamp = (timestamp_t) (WsfTimerNextExpiration(&pTimerRunning) * WSF_MS_PER_TICK);
565567
if (pTimerRunning) {
566-
nextTimeout.attach_us(timeoutCallback, nextTimestamp);
568+
nextTimeout.attach(timeoutCallback, milliseconds(nextTimestamp));
567569
} else {
568570
critical_section.disable();
569571
_hci_driver->on_host_stack_inactivity();

features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "CellularLog.h"
2323
#include "rtos/ThisThread.h"
2424

25+
using namespace std::chrono;
2526
using namespace rtos;
2627
using namespace mbed;
2728
using namespace events;
@@ -86,7 +87,7 @@ nsapi_error_t ALT1250_PPP::soft_power_on()
8687
tr_warn("Modem is not responding to AT commands, reset it");
8788
if (_rst.is_connected()) {
8889
_rst = 0;
89-
ThisThread::sleep_for(100);
90+
ThisThread::sleep_for(100ms);
9091
_rst = 1;
9192
}
9293
if (_at.sync(2000)) {

features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularStack.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
#include "CellularLog.h"
2727

2828
#define PACKET_SIZE_MAX 1358
29-
#define TXFULL_EVENT_TIMEOUT (1 * 1000) // ms
29+
#define TXFULL_EVENT_TIMEOUT 1s
3030

3131
#define AT_UPLINK_BUSY 159
3232
#define AT_UART_BUFFER_ERROR 536
3333
#define AT_BACK_OFF_TIMER 537
3434

35+
using namespace std::chrono;
3536
using namespace mbed;
3637
using namespace mbed_cellular_util;
3738

@@ -230,7 +231,7 @@ nsapi_size_or_error_t QUECTEL_BC95_CellularStack::socket_sendto_impl(CellularSoc
230231
if (retry < 3) {
231232
retry++;
232233
tr_warn("Socket %d sendto EAGAIN", socket->id);
233-
rtos::ThisThread::sleep_for(30);
234+
rtos::ThisThread::sleep_for(30ms);
234235
_at.clear_error();
235236
goto retry_send;
236237
}

features/cellular/framework/targets/RiotMicro/AT/RM1000_AT_CellularStack.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
using namespace mbed;
2828
using namespace mbed_cellular_util;
29-
using namespace std::chrono_literals;
29+
using namespace std::chrono;
3030

3131
RM1000_AT_CellularStack::RM1000_AT_CellularStack(ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type, AT_CellularDevice &device) :
3232
AT_CellularStack(atHandler, cid, stack_type, device)
@@ -266,7 +266,7 @@ nsapi_size_or_error_t RM1000_AT_CellularStack::socket_recvfrom_impl(CellularSock
266266
// read() should not fail
267267
success = false;
268268
}
269-
} else if (timer.read_ms() < SOCKET_TIMEOUT) {
269+
} else if (timer.elapsed_time() < SOCKET_TIMEOUT) {
270270
// Wait for URCs
271271
_at.process_oob();
272272
} else {

0 commit comments

Comments
 (0)