Skip to content

Commit 3e42991

Browse files
authored
Merge pull request #13046 from hugueskamba/hk_use_chrono
Use std::chrono based functions
2 parents 51446de + 2b934a2 commit 3e42991

File tree

25 files changed

+156
-72
lines changed

25 files changed

+156
-72
lines changed

TESTS/netsocket/udp/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ void fill_tx_buffer_ascii(char *buff, size_t len)
118118
}
119119
}
120120

121-
int split2half_rmng_udp_test_time()
121+
microseconds split2half_rmng_udp_test_time()
122122
{
123-
return (udp_global::TESTS_TIMEOUT - tc_bucket.read()) / 2;
123+
return (udp_global::TESTS_TIMEOUT - tc_bucket.elapsed_time()) / 2;
124124
}
125125

126126
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
@@ -133,7 +133,7 @@ int fetch_stats()
133133
// Test setup
134134
utest::v1::status_t greentea_setup(const size_t number_of_cases)
135135
{
136-
GREENTEA_SETUP(udp_global::TESTS_TIMEOUT, "default_auto");
136+
GREENTEA_SETUP(seconds(udp_global::TESTS_TIMEOUT).count(), "default_auto");
137137
_ifup();
138138
tc_bucket.start();
139139
return greentea_test_setup_handler(number_of_cases);

TESTS/netsocket/udp/udp_tests.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "../test_params.h"
2222
#include "mbed_trace.h"
2323

24+
using namespace std::chrono;
25+
2426
#define TRACE_GROUP "GRNT"
2527

2628
NetworkInterface *get_interface();
@@ -37,18 +39,18 @@ int fetch_stats(void);
3739
/**
3840
* Single testcase might take only half of the remaining execution time
3941
*/
40-
int split2half_rmng_udp_test_time(); // [s]
42+
microseconds split2half_rmng_udp_test_time();
4143

4244
namespace udp_global {
4345
#ifdef MBED_GREENTEA_TEST_UDPSOCKET_TIMEOUT_S
44-
static const int TESTS_TIMEOUT = MBED_GREENTEA_TEST_UDPSOCKET_TIMEOUT_S;
46+
static constexpr seconds TESTS_TIMEOUT(MBED_GREENTEA_TEST_UDPSOCKET_TIMEOUT_S);
4547
#else
4648
#define MESH 3
4749
#define WISUN 0x2345
4850
#if MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == MESH && MBED_CONF_NSAPI_DEFAULT_MESH_TYPE == WISUN
49-
static const int TESTS_TIMEOUT = (25 * 60);
51+
static constexpr seconds TESTS_TIMEOUT = 25min;
5052
#else
51-
static const int TESTS_TIMEOUT = (20 * 60);
53+
static constexpr seconds TESTS_TIMEOUT = 20min;
5254
#endif
5355
#endif
5456

TESTS/netsocket/udp/udpsocket_echotest.cpp

Lines changed: 17 additions & 9 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;
@@ -53,7 +54,7 @@ static bool pkt_received[PKTS] = {false, false, false, false, false, false, fals
5354
};
5455

5556
Timer tc_exec_time;
56-
int time_allotted;
57+
microseconds time_allotted;
5758
}
5859

5960
static void _sigio_handler()
@@ -75,7 +76,7 @@ void UDPSOCKET_ECHOTEST_impl(bool use_sendto)
7576
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.connect(udp_addr));
7677
}
7778

78-
sock.set_timeout(SOCKET_TIMEOUT);
79+
sock.set_timeout(milliseconds(SOCKET_TIMEOUT).count());
7980
int recvd;
8081
int sent;
8182
int packets_sent = 0;
@@ -169,7 +170,7 @@ void UDPSOCKET_ECHOTEST_CONNECT_SEND_RECV()
169170
void UDPSOCKET_ECHOTEST_NONBLOCK_impl(bool use_sendto)
170171
{
171172
tc_exec_time.start();
172-
time_allotted = split2half_rmng_udp_test_time(); // [s]
173+
time_allotted = split2half_rmng_udp_test_time();
173174

174175
SocketAddress udp_addr;
175176
NetworkInterface::get_default_instance()->gethostbyname(ECHO_SERVER_ADDR, &udp_addr);
@@ -211,8 +212,12 @@ void UDPSOCKET_ECHOTEST_NONBLOCK_impl(bool use_sendto)
211212
} else if (sent == pkt_s) {
212213
packets_sent++;
213214
} else if (sent == NSAPI_ERROR_WOULD_BLOCK) {
214-
if (tc_exec_time.read() >= time_allotted ||
215-
signals.wait_all(SIGNAL_SIGIO_TX, SIGIO_TIMEOUT) == osFlagsErrorTimeout) {
215+
if (
216+
(tc_exec_time.elapsed_time() >= time_allotted)
217+
|| signals.wait_all(
218+
SIGNAL_SIGIO_TX, milliseconds(SIGIO_TIMEOUT).count()
219+
) == osFlagsErrorTimeout
220+
) {
216221
continue;
217222
}
218223
--retry_cnt;
@@ -230,10 +235,13 @@ void UDPSOCKET_ECHOTEST_NONBLOCK_impl(bool use_sendto)
230235
}
231236

232237
if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
233-
if (tc_exec_time.read() >= time_allotted) {
238+
if (tc_exec_time.elapsed_time() >= time_allotted) {
234239
break;
235240
}
236-
signals.wait_all(SIGNAL_SIGIO_RX, SIGIO_TIMEOUT);
241+
signals.wait_all(
242+
SIGNAL_SIGIO_RX,
243+
milliseconds(SIGIO_TIMEOUT).count()
244+
);
237245
--retry_recv;
238246
continue;
239247
} else if (recvd < 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*/
@@ -356,9 +358,9 @@ static rf_trx_part_e rf_radio_type_read(void)
356358
static void rf_if_ack_wait_timer_start(uint16_t slots)
357359
{
358360
#ifdef MBED_CONF_RTOS_PRESENT
359-
rf->ack_timer.attach_us(rf_if_ack_timer_signal, slots * 50);
361+
rf->ack_timer.attach(rf_if_ack_timer_signal, slots * 50us);
360362
#else
361-
rf->ack_timer.attach_us(rf_ack_wait_timer_interrupt, slots * 50);
363+
rf->ack_timer.attach(rf_ack_wait_timer_interrupt, slots * 50us);
362364
#endif
363365
}
364366

@@ -372,9 +374,9 @@ static void rf_if_ack_wait_timer_start(uint16_t slots)
372374
static void rf_if_calibration_timer_start(uint32_t slots)
373375
{
374376
#ifdef MBED_CONF_RTOS_PRESENT
375-
rf->cal_timer.attach_us(rf_if_cal_timer_signal, slots * 50);
377+
rf->cal_timer.attach(rf_if_cal_timer_signal, slots * 50us);
376378
#else
377-
rf->cal_timer.attach_us(rf_calibration_timer_interrupt, slots * 50);
379+
rf->cal_timer.attach(rf_calibration_timer_interrupt, slots * 50us);
378380
#endif
379381
}
380382

@@ -388,9 +390,9 @@ static void rf_if_calibration_timer_start(uint32_t slots)
388390
static void rf_if_cca_timer_start(uint32_t slots)
389391
{
390392
#ifdef MBED_CONF_RTOS_PRESENT
391-
rf->cca_timer.attach_us(rf_if_cca_timer_signal, slots * 50);
393+
rf->cca_timer.attach(rf_if_cca_timer_signal, slots * 50us);
392394
#else
393-
rf->cca_timer.attach_us(rf_cca_timer_interrupt, slots * 50);
395+
rf->cca_timer.attach(rf_cca_timer_interrupt, slots * 50us);
394396
#endif
395397
}
396398

@@ -519,14 +521,14 @@ static void rf_if_reset_radio(void)
519521
#endif
520522
rf->IRQ.rise(nullptr);
521523
rf->RST = 1;
522-
ThisThread::sleep_for(2);
524+
ThisThread::sleep_for(2ms);
523525
rf->RST = 0;
524-
ThisThread::sleep_for(10);
526+
ThisThread::sleep_for(10ms);
525527
CS_RELEASE();
526528
rf->SLP_TR = 0;
527-
ThisThread::sleep_for(10);
529+
ThisThread::sleep_for(10ms);
528530
rf->RST = 1;
529-
ThisThread::sleep_for(10);
531+
ThisThread::sleep_for(10ms);
530532

531533
rf->IRQ.rise(&rf_if_interrupt_handler);
532534
}

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

@@ -276,7 +278,7 @@ static void rf_calculate_symbol_rate(uint32_t baudrate, phy_modulation_e modulat
276278

277279
static uint32_t rf_get_timestamp(void)
278280
{
279-
return (uint32_t)rf->tx_timer.read_us();
281+
return (uint32_t)rf->tx_timer.elapsed_time().count();
280282
}
281283

282284
static void rf_update_tx_active_time(void)
@@ -866,7 +868,7 @@ static void rf_cca_timer_stop(void)
866868

867869
static void rf_cca_timer_start(uint32_t slots)
868870
{
869-
rf->cca_timer.attach_us(rf_cca_timer_signal, slots);
871+
rf->cca_timer.attach(rf_cca_timer_signal, microseconds(slots));
870872
TEST_CSMA_STARTED
871873
}
872874

@@ -903,7 +905,7 @@ static void rf_backup_timer_stop(void)
903905

904906
static void rf_backup_timer_start(uint32_t slots)
905907
{
906-
rf->backup_timer.attach_us(rf_backup_timer_signal, slots);
908+
rf->backup_timer.attach(rf_backup_timer_signal, microseconds(slots));
907909
}
908910

909911
static int8_t rf_start_cca(uint8_t *data_ptr, uint16_t data_length, uint8_t tx_handle, data_protocol_e data_protocol)
@@ -1177,10 +1179,10 @@ static void rf_reset(void)
11771179
{
11781180
// Shutdown
11791181
rf->SDN = 1;
1180-
ThisThread::sleep_for(10);
1182+
ThisThread::sleep_for(10ms);
11811183
// Wake up
11821184
rf->SDN = 0;
1183-
ThisThread::sleep_for(10);
1185+
ThisThread::sleep_for(10ms);
11841186
}
11851187

11861188
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
}
@@ -2500,9 +2502,9 @@ ble_error_t GenericGap<PalGapImpl, PalSecurityManager, ConnectionEventMonitorEve
25002502

25012503
_scan_timeout.detach();
25022504
if (duration.value()) {
2503-
_scan_timeout.attach_us(
2505+
_scan_timeout.attach(
25042506
mbed::callback(this, &GenericGap::on_scan_timeout_),
2505-
microsecond_t(duration).value()
2507+
duration.valueChrono()
25062508
);
25072509
}
25082510
}

0 commit comments

Comments
 (0)