Skip to content

Commit

Permalink
Add z_timestamp_new and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Jun 7, 2024
1 parent 5ebdbbc commit 4966644
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -3338,6 +3338,13 @@ const char *z_time_now_as_str(const char *buf,
* Returns id associated with this timestamp.
*/
ZENOHC_API struct z_id_t z_timestamp_id(const struct z_timestamp_t *this_);
/**
* Create timestamp
*/
ZENOHC_API
z_error_t z_timestamp_new(struct z_timestamp_t *this_,
const struct z_id_t *zid,
uint64_t npt64_time);
/**
* Returns NPT64 time associated with this timestamp.
*/
Expand Down
15 changes: 15 additions & 0 deletions src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ impl From<z_sample_kind_t> for SampleKind {
use crate::opaque_types::z_timestamp_t;
decl_transmute_copy!(Timestamp, z_timestamp_t);

/// Create timestamp
#[no_mangle]
pub extern "C" fn z_timestamp_new(
this: &mut z_timestamp_t,
zid: &z_id_t,
npt64_time: u64,
) -> errors::z_error_t {
let timestamp = Timestamp::new(
zenoh::time::NTP64(npt64_time),
(&zid.transmute_copy()).into(),
);
*this = timestamp.transmute_copy();
errors::Z_OK
}

/// Returns NPT64 time associated with this timestamp.
#[no_mangle]
pub extern "C" fn z_timestamp_npt64_time(this: &z_timestamp_t) -> u64 {
Expand Down
25 changes: 25 additions & 0 deletions tests/z_int_pub_sub_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const size_t values_count = sizeof(values) / sizeof(values[0]);

const uint32_t TEST_EID = 42;
const uint64_t TEST_SN = 24;
const uint64_t TEST_TS = 401706000;

int run_publisher() {
SEM_WAIT(sem);
Expand Down Expand Up @@ -62,9 +63,13 @@ int run_publisher() {
z_owned_source_info_t source_info;
z_source_info_new(&source_info, &entity_global_id, TEST_SN);

z_timestamp_t ts;
z_timestamp_new(&ts, &self_id, TEST_TS + i);

z_publisher_put_options_t options;
z_publisher_put_options_default(&options);
options.source_info = &source_info;
options.timestamp = &ts;

z_owned_bytes_t payload;
z_bytes_encode_from_string(&payload, values[i]);
Expand Down Expand Up @@ -116,6 +121,26 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
perror("Unexpected eid value");
exit(-1);
}

const z_timestamp_t *ts = z_sample_timestamp(sample);
if (ts == NULL) {
perror("Unexpected null timestamp");
exit(-1);
}
const uint64_t time = z_timestamp_npt64_time(ts);
if (time != TEST_TS + val_num) {
perror("Unexpected timestamp value");
exit(-1);
}

z_id_t ts_id = z_timestamp_id(ts);
z_id_t gloabl_id = z_entity_global_id_zid(&id);

if (memcmp(ts_id.id, gloabl_id.id, sizeof(ts_id.id)) != 0) {
perror("Timestamp id and global id differ");
exit(-1);
}

if (++val_num == values_count) {
exit(0);
};
Expand Down

0 comments on commit 4966644

Please sign in to comment.