From f742e927b5a02ad59e3271fcab7ca39263b3fcde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Sowa?= Date: Sun, 8 Dec 2024 16:56:20 +0100 Subject: [PATCH] Use gettimeofday for z_time functions --- .../system/platform/freertos_plus_tcp.h | 4 +- src/system/freertos_plus_tcp/system.c | 50 +++++++++++++++---- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/include/zenoh-pico/system/platform/freertos_plus_tcp.h b/include/zenoh-pico/system/platform/freertos_plus_tcp.h index 795a69c98..a2cfd5781 100644 --- a/include/zenoh-pico/system/platform/freertos_plus_tcp.h +++ b/include/zenoh-pico/system/platform/freertos_plus_tcp.h @@ -14,6 +14,8 @@ #ifndef ZENOH_PICO_SYSTEM_FREERTOS_PLUS_TCP_TYPES_H #define ZENOH_PICO_SYSTEM_FREERTOS_PLUS_TCP_TYPES_H +#include + #include "FreeRTOS.h" #include "FreeRTOS_IP.h" #include "semphr.h" @@ -54,7 +56,7 @@ typedef void *_z_condvar_t; #endif // Z_MULTI_THREAD == 1 typedef TickType_t z_clock_t; -typedef TickType_t z_time_t; +typedef struct timeval z_time_t; typedef struct { union { diff --git a/src/system/freertos_plus_tcp/system.c b/src/system/freertos_plus_tcp/system.c index bf9585270..d079c34a5 100644 --- a/src/system/freertos_plus_tcp/system.c +++ b/src/system/freertos_plus_tcp/system.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "FreeRTOS_IP.h" #include "zenoh-pico/config.h" @@ -193,31 +194,62 @@ z_result_t z_sleep_s(size_t time) { } /*------------------ Clock ------------------*/ -z_clock_t z_clock_now(void) { return z_time_now(); } +z_clock_t z_clock_now(void) { return xTaskGetTickCount(); } unsigned long z_clock_elapsed_us(z_clock_t *instant) { return z_clock_elapsed_ms(instant) * 1000; } -unsigned long z_clock_elapsed_ms(z_clock_t *instant) { return z_time_elapsed_ms(instant); } +unsigned long z_clock_elapsed_ms(z_clock_t *instant) { + z_clock_t now = z_clock_now(); + + unsigned long elapsed = (now - *instant) * portTICK_PERIOD_MS; + return elapsed; +} unsigned long z_clock_elapsed_s(z_clock_t *instant) { return z_clock_elapsed_ms(instant) / 1000; } /*------------------ Time ------------------*/ -z_time_t z_time_now(void) { return xTaskGetTickCount(); } +z_time_t z_time_now(void) { + z_time_t now; + gettimeofday(&now, NULL); + return now; +} const char *z_time_now_as_str(char *const buf, unsigned long buflen) { - snprintf(buf, buflen, "%u", z_time_now()); + z_time_t tv = z_time_now(); + struct tm ts; + ts = *localtime(&tv.tv_sec); + strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); return buf; } -unsigned long z_time_elapsed_us(z_time_t *time) { return z_time_elapsed_ms(time) * 1000; } +unsigned long z_time_elapsed_us(z_time_t *time) { + z_time_t now; + gettimeofday(&now, NULL); + + unsigned long elapsed = (unsigned long)(1000000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec)); + return elapsed; +} unsigned long z_time_elapsed_ms(z_time_t *time) { - z_time_t now = z_time_now(); + z_time_t now; + gettimeofday(&now, NULL); - unsigned long elapsed = (now - *time) * portTICK_PERIOD_MS; + unsigned long elapsed = (unsigned long)(1000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec) / 1000); return elapsed; } -unsigned long z_time_elapsed_s(z_time_t *time) { return z_time_elapsed_ms(time) / 1000; } +unsigned long z_time_elapsed_s(z_time_t *time) { + z_time_t now; + gettimeofday(&now, NULL); -z_result_t _z_get_time_since_epoch(_z_time_since_epoch *t) { return -1; } + unsigned long elapsed = (unsigned long)(now.tv_sec - time->tv_sec); + return elapsed; +} + +z_result_t _z_get_time_since_epoch(_z_time_since_epoch *t) { + z_time_t now; + gettimeofday(&now, NULL); + t->secs = (uint32_t)now.tv_sec; + t->nanos = (uint32_t)now.tv_usec * 1000; + return 0; +}