diff --git a/include/coap2/coap_time.h b/include/coap2/coap_time.h index e4a8e7c7..b17cb357 100644 --- a/include/coap2/coap_time.h +++ b/include/coap2/coap_time.h @@ -47,6 +47,10 @@ COAP_STATIC_INLINE coap_time_t coap_ticks_to_rt(coap_tick_t t) { return t / COAP_TICKS_PER_SECOND; } +COAP_STATIC_INLINE uint64_t coap_ticks_to_rt_us(coap_tick_t t) { + return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND; +} + #elif defined(WITH_CONTIKI) #include "clock.h" @@ -75,6 +79,10 @@ COAP_STATIC_INLINE coap_time_t coap_ticks_to_rt(coap_tick_t t) { return t / COAP_TICKS_PER_SECOND; } +COAP_STATIC_INLINE uint64_t coap_ticks_to_rt_us(coap_tick_t t) { + return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND; +} + #else #include diff --git a/src/net.c b/src/net.c index 152bcaa6..7381c6ff 100644 --- a/src/net.c +++ b/src/net.c @@ -2358,6 +2358,8 @@ coap_can_exit(coap_context_t *context) { static int coap_started = 0; void coap_startup(void) { + coap_tick_t now; + uint64_t us; if (coap_started) return; coap_started = 1; @@ -2367,13 +2369,10 @@ void coap_startup(void) { WSAStartup(wVersionRequested, &wsaData); #endif coap_clock_init(); -#if defined(WITH_LWIP) - prng_init(LWIP_RAND()); -#elif defined(WITH_CONTIKI) - prng_init(0); -#elif !defined(_WIN32) - prng_init(0); -#endif + coap_ticks(&now); + us = coap_ticks_to_rt_us(now); + /* Be accurate to the nearest (approx) us */ + prng_init(us); coap_dtls_startup(); }