Skip to content

Commit

Permalink
Use gettimeofday for z_time functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsowa committed Dec 8, 2024
1 parent 1b867ec commit f742e92
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
4 changes: 3 additions & 1 deletion include/zenoh-pico/system/platform/freertos_plus_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#ifndef ZENOH_PICO_SYSTEM_FREERTOS_PLUS_TCP_TYPES_H
#define ZENOH_PICO_SYSTEM_FREERTOS_PLUS_TCP_TYPES_H

#include <time.h>

#include "FreeRTOS.h"
#include "FreeRTOS_IP.h"
#include "semphr.h"
Expand Down Expand Up @@ -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 {
Expand Down
50 changes: 41 additions & 9 deletions src/system/freertos_plus_tcp/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

#include "FreeRTOS_IP.h"
#include "zenoh-pico/config.h"
Expand Down Expand Up @@ -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;
}

0 comments on commit f742e92

Please sign in to comment.