Skip to content

Commit

Permalink
align z_timestamp_new with zenoh-rust (#537)
Browse files Browse the repository at this point in the history
* align z_timestamp_new with zenoh-rust; fix npt->ntp typo

* unused variable fix

* fix warning, extra checks for zephyr and espidf

* fix

* added platform dependent functions for getting utc time

* format

* typo fixes

* fmt

* added missing new lines at the end of file
  • Loading branch information
DenisBiryukov91 authored Jul 12, 2024
1 parent 7395557 commit 6048008
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 12 deletions.
3 changes: 1 addition & 2 deletions examples/unix/c11/z_pub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ int main(int argc, char **argv) {

// Create timestamp
z_timestamp_t ts;
time_t now = time(NULL);
z_timestamp_new(&ts, z_loan(s), (uint64_t)now);
z_timestamp_new(&ts, z_loan(s));

// Publish data
printf("Press CTRL-C to quit...\n");
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c11/z_sub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) {
// Check timestamp
const z_timestamp_t *ts = z_sample_timestamp(sample);
if (ts != NULL) {
printf(" with timestamp: %" PRIu64 "\n", z_timestamp_npt64_time(ts));
printf(" with timestamp: %" PRIu64 "\n", z_timestamp_ntp64_time(ts));
}
// Check attachment
kv_pairs_t kvp = {.current_idx = 0, .len = KVP_LEN, .data = (kv_pair_t *)malloc(KVP_LEN * sizeof(kv_pair_t))};
Expand Down
9 changes: 4 additions & 5 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -949,17 +949,16 @@ int8_t z_bytes_writer_write(z_loaned_bytes_writer_t *writer, const uint8_t *src,
* Parameters:
* ts: An uninitialized :c:type:`z_timestamp_t`.
* zs: Pointer to a :c:type:`z_loaned_session_t` to get the id from.
* npt64_time: NPT64 time.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
* ``0`` if encode successful, ``negative value`` otherwise (for example if RTC is not available on the system).
*/
int8_t z_timestamp_new(z_timestamp_t *ts, const z_loaned_session_t *zs, uint64_t npt64_time);
int8_t z_timestamp_new(z_timestamp_t *ts, const z_loaned_session_t *zs);

/**
* Returns NPT64 time associated with this timestamp.
* Returns NTP64 time associated with this timestamp.
*/
uint64_t z_timestamp_npt64_time(const z_timestamp_t *ts);
uint64_t z_timestamp_ntp64_time(const z_timestamp_t *ts);

/**
* Returns id associated with this timestamp.
Expand Down
1 change: 1 addition & 0 deletions include/zenoh-pico/protocol/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp);
_z_timestamp_t _z_timestamp_null(void);
void _z_timestamp_clear(_z_timestamp_t *tstamp);
_Bool _z_timestamp_check(const _z_timestamp_t *stamp);
uint64_t _z_timestamp_ntp64_from_time(uint32_t seconds, uint32_t nanos);

/**
* The product of:
Expand Down
6 changes: 6 additions & 0 deletions include/zenoh-pico/system/platform-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ unsigned long z_time_elapsed_us(z_time_t *time);
unsigned long z_time_elapsed_ms(z_time_t *time);
unsigned long z_time_elapsed_s(z_time_t *time);

typedef struct {
uint32_t secs;
uint32_t nanos;
} zp_time_since_epoch;

int8_t zp_get_time_since_epoch(zp_time_since_epoch *t);
#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 7 additions & 3 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "zenoh-pico/session/resource.h"
#include "zenoh-pico/session/subscription.h"
#include "zenoh-pico/session/utils.h"
#include "zenoh-pico/system/platform-common.h"
#include "zenoh-pico/system/platform.h"
#include "zenoh-pico/transport/multicast.h"
#include "zenoh-pico/transport/unicast.h"
Expand Down Expand Up @@ -617,13 +618,16 @@ int8_t z_bytes_writer_write(z_loaned_bytes_writer_t *writer, const uint8_t *src,
return _z_bytes_writer_write(writer, src, len);
}

int8_t z_timestamp_new(z_timestamp_t *ts, const z_loaned_session_t *zs, uint64_t npt64_time) {
int8_t z_timestamp_new(z_timestamp_t *ts, const z_loaned_session_t *zs) {
*ts = _z_timestamp_null();
zp_time_since_epoch t;
_Z_RETURN_IF_ERR(zp_get_time_since_epoch(&t));
ts->time = _z_timestamp_ntp64_from_time(t.secs, t.nanos);
ts->id = zs->in->val._local_zid;
ts->time = npt64_time;
return _Z_RES_OK;
}

uint64_t z_timestamp_npt64_time(const z_timestamp_t *ts) { return ts->time; }
uint64_t z_timestamp_ntp64_time(const z_timestamp_t *ts) { return ts->time; }

z_id_t z_timestamp_id(const z_timestamp_t *ts) { return ts->id; }

Expand Down
10 changes: 10 additions & 0 deletions src/protocol/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "zenoh-pico/collections/slice.h"
#include "zenoh-pico/protocol/codec/core.h"
#include "zenoh-pico/protocol/iobuf.h"
#include "zenoh-pico/utils/endianness.h"
#include "zenoh-pico/utils/logging.h"

#define _Z_ID_LEN (16)
Expand Down Expand Up @@ -69,6 +70,15 @@ _z_source_info_t _z_source_info_null(void) {
return (_z_source_info_t){._source_sn = 0, ._entity_id = 0, ._id = _z_id_empty()};
}
_z_timestamp_t _z_timestamp_null(void) { return (_z_timestamp_t){.id = _z_id_empty(), .time = 0}; }

uint64_t _z_timestamp_ntp64_from_time(uint32_t seconds, uint32_t nanos) {
const uint64_t FRAC_PER_SEC = (uint64_t)1 << 32;
const uint64_t NANOS_PER_SEC = 1000000000;

uint32_t fractions = (uint32_t)((uint64_t)nanos * FRAC_PER_SEC / NANOS_PER_SEC + 1);
return ((uint64_t)seconds << 32) | fractions;
}

_z_value_t _z_value_null(void) { return (_z_value_t){.payload = _z_bytes_null(), .encoding = _z_encoding_null()}; }
_z_value_t _z_value_steal(_z_value_t *value) {
_z_value_t ret = *value;
Expand Down
8 changes: 8 additions & 0 deletions src/system/arduino/esp32/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,11 @@ unsigned long z_time_elapsed_s(z_time_t *time) {
unsigned long elapsed = now.tv_sec - time->tv_sec;
return elapsed;
}

int8_t zp_get_time_since_epoch(zp_time_since_epoch *t) {
z_time_t now;
gettimeofday(&now, NULL);
t->secs = now.tv_sec;
t->nanos = now.tv_usec * 1000;
return 0;
}
8 changes: 8 additions & 0 deletions src/system/arduino/opencr/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,11 @@ unsigned long z_time_elapsed_s(z_time_t *time) {
unsigned long elapsed = now.tv_sec - time->tv_sec;
return elapsed;
}

int8_t zp_get_time_since_epoch(zp_time_since_epoch *t) {
z_time_t now;
gettimeofday(&now, NULL);
t->secs = now.tv_sec;
t->nanos = now.tv_usec * 1000;
return 0;
}
9 changes: 8 additions & 1 deletion src/system/emscripten/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//

#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -132,3 +132,10 @@ unsigned long z_time_elapsed_ms(z_time_t *time) {
}

unsigned long z_time_elapsed_s(z_time_t *time) { return z_time_elapsed_ms(time) * 1000; }

int8_t zp_get_time_since_epoch(zp_time_since_epoch *t) {
double date = emscripten_date_now();
t->secs = (uint32_t)(date / 1000);
t->nanos = (uint32_t)((date - t->secs * 1000) * 1000000);
return 0;
}
8 changes: 8 additions & 0 deletions src/system/espidf/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,11 @@ unsigned long z_time_elapsed_s(z_time_t *time) {
unsigned long elapsed = now.tv_sec - time->tv_sec;
return elapsed;
}

int8_t zp_get_time_since_epoch(zp_time_since_epoch *t) {
z_time_t now;
gettimeofday(&now, NULL);
t->secs = now.tv_sec;
t->nanos = now.tv_usec * 1000;
return 0;
}
2 changes: 2 additions & 0 deletions src/system/flipper/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,5 @@ struct tm* localtime(const time_t* timep) {
static struct tm t;
return &t;
}

int8_t zp_get_time_since_epoch(zp_time_since_epoch* t) { return -1; }
2 changes: 2 additions & 0 deletions src/system/freertos_plus_tcp/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,5 @@ unsigned long z_time_elapsed_ms(z_time_t *time) {
}

unsigned long z_time_elapsed_s(z_time_t *time) { return z_time_elapsed_ms(time) / 1000; }

int8_t zp_get_time_since_epoch(zp_time_since_epoch *t) { return -1; }
8 changes: 8 additions & 0 deletions src/system/mbed/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,12 @@ unsigned long z_time_elapsed_s(z_time_t *time) {
return elapsed;
}

int8_t zp_get_time_since_epoch(zp_time_since_epoch *t) {
z_time_t now;
gettimeofday(&now, NULL);
t->secs = now.tv_sec;
t->nanos = now.tv_usec * 1000;
return 0;
}

} // extern "C"
8 changes: 8 additions & 0 deletions src/system/unix/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,11 @@ unsigned long z_time_elapsed_s(z_time_t *time) {
unsigned long elapsed = (unsigned long)(now.tv_sec - time->tv_sec);
return elapsed;
}

int8_t zp_get_time_since_epoch(zp_time_since_epoch *t) {
z_time_t now;
gettimeofday(&now, NULL);
t->secs = now.tv_sec;
t->nanos = now.tv_usec * 1000;
return 0;
}
8 changes: 8 additions & 0 deletions src/system/windows/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,11 @@ unsigned long z_time_elapsed_s(z_time_t *time) {
unsigned long elapsed = (unsigned long)(now.time - time->time);
return elapsed;
}

int8_t zp_get_time_since_epoch(zp_time_since_epoch *t) {
z_time_t now;
ftime(&now);
t->secs = (uint32_t)now.time;
t->nanos = (uint32_t)(now.millitm * 1000000);
return 0;
}
8 changes: 8 additions & 0 deletions src/system/zephyr/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,11 @@ unsigned long z_time_elapsed_s(z_time_t *time) {
unsigned long elapsed = now.tv_sec - time->tv_sec;
return elapsed;
}

int8_t zp_get_time_since_epoch(zp_time_since_epoch *t) {
z_time_t now;
gettimeofday(&now, NULL);
t->secs = now.tv_sec;
t->nanos = now.tv_usec * 1000;
return 0;
}

0 comments on commit 6048008

Please sign in to comment.