Skip to content

Commit

Permalink
feat: use _z_bytes_t in value and sample
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Jun 11, 2024
1 parent 7ac59d2 commit ed613b7
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 42 deletions.
32 changes: 27 additions & 5 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,9 @@ int8_t z_encoding_null(z_owned_encoding_t *encoding);
* value: Pointer to a :c:type:`z_loaned_value_t` to get data from.
*
* Return:
* Pointer to the data as a :c:type:`z_loaned_slice_t`.
* Pointer to the data as a :c:type:`z_loaned_bytes_t`.
*/
const z_loaned_slice_t *z_value_payload(const z_loaned_value_t *value);
const z_loaned_bytes_t *z_value_payload(const z_loaned_value_t *value);

/**
* Gets date pointer of a bytes array.
Expand All @@ -440,6 +440,28 @@ const uint8_t *z_slice_data(const z_loaned_slice_t *slice);
*/
size_t z_slice_len(const z_loaned_slice_t *slice);

/**
* Gets date pointer of a bytes array.
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_slice_t` to get data from.
*
* Return:
* The data pointer.
*/
const uint8_t *z_bytes_data(const z_loaned_bytes_t *bytes);

/**
* Gets total number of bytes in a bytes array.
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_slice_t` to get length from.
*
* Return:
* The number of bytes.
*/
size_t z_bytes_len(const z_loaned_bytes_t *bytes);

/**
* Decodes data into a :c:type:`z_owned_string_t`
*
Expand Down Expand Up @@ -872,9 +894,9 @@ const z_loaned_keyexpr_t *z_sample_keyexpr(const z_loaned_sample_t *sample);
* sample: Pointer to a :c:type:`z_loaned_sample_t` to get the payload from.
*
* Return:
* The payload wrapped as a :c:type:`z_loaned_slice_t`.
* The payload wrapped as a :c:type:`z_loaned_bytes_t`.
*/
const z_loaned_slice_t *z_sample_payload(const z_loaned_sample_t *sample);
const z_loaned_bytes_t *z_sample_payload(const z_loaned_sample_t *sample);

/**
* Gets the timestamp of a sample by aliasing it.
Expand Down Expand Up @@ -1184,7 +1206,7 @@ void z_query_reply_options_default(z_query_reply_options_t *options);
* Return:
* ``0`` if reply operation successful, ``negative value`` otherwise.
*/
int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, z_owned_slice_t *payload,
int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, z_owned_bytes_t *payload,
const z_query_reply_options_t *options);
#endif

Expand Down
2 changes: 1 addition & 1 deletion include/zenoh-pico/api/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ typedef struct {
* z_owned_encoding_t *encoding: Payload encoding.
*/
typedef struct {
z_owned_slice_t *payload;
z_owned_bytes_t *payload;
z_owned_encoding_t *encoding;
z_query_consolidation_t consolidation;
z_query_target_t target;
Expand Down
7 changes: 6 additions & 1 deletion include/zenoh-pico/collections/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ void _z_slice_free(_z_slice_t **bs);
* _z_slice_t slice: content of the container.
*/
typedef struct {
_z_slice_t slice;
_z_slice_t _slice;
} _z_bytes_t;

_Bool _z_bytes_check(_z_bytes_t bytes);
_z_bytes_t _z_bytes_null(void);
void _z_bytes_copy(_z_bytes_t *dst, const _z_bytes_t *src);
_z_bytes_t _z_bytes_duplicate(const _z_bytes_t *src);
void _z_bytes_move(_z_bytes_t *dst, _z_bytes_t *src);
void _z_bytes_clear(_z_bytes_t *bytes);
void _z_bytes_free(_z_bytes_t **bs);

#endif /* ZENOH_PICO_COLLECTIONS_SLICE_H */
2 changes: 1 addition & 1 deletion include/zenoh-pico/net/sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
typedef struct _z_sample_t {
_z_keyexpr_t keyexpr;
_z_slice_t payload;
_z_bytes_t payload;
_z_timestamp_t timestamp;
_z_encoding_t encoding;
z_sample_kind_t kind;
Expand Down
2 changes: 2 additions & 0 deletions include/zenoh-pico/protocol/codec/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ int8_t _z_slice_val_decode_na(_z_slice_t *bs, _z_zbuf_t *zbf);
int8_t _z_slice_encode(_z_wbuf_t *buf, const _z_slice_t *bs);
size_t _z_slice_encode_len(const _z_slice_t *bs);
int8_t _z_slice_decode(_z_slice_t *bs, _z_zbuf_t *buf);
int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *zbf);
int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs);
int8_t _z_zbuf_read_exact(_z_zbuf_t *zbf, uint8_t *dest, size_t length);

int8_t _z_str_encode(_z_wbuf_t *buf, const char *s);
Expand Down
4 changes: 2 additions & 2 deletions include/zenoh-pico/protocol/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ typedef struct {
* Represents a Zenoh value.
*
* Members:
* _z_bytes_t payload: The payload of this zenoh value.
* _z_encoding_t encoding: The encoding of the `payload`.
* _z_slice_t payload: The payload of this zenoh value.
*/
typedef struct {
_z_slice_t payload;
_z_bytes_t payload;
_z_encoding_t encoding;
} _z_value_t;
_z_value_t _z_value_null(void);
Expand Down
30 changes: 16 additions & 14 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,15 @@ static _z_encoding_t _z_encoding_from_owned(const z_owned_encoding_t *encoding)
return *encoding->_val;
}

const z_loaned_slice_t *z_value_payload(const z_loaned_value_t *value) { return &value->payload; }
const z_loaned_bytes_t *z_value_payload(const z_loaned_value_t *value) { return &value->payload; }

const uint8_t *z_slice_data(const z_loaned_slice_t *slice) { return slice->start; }

size_t z_slice_len(const z_loaned_slice_t *slice) { return slice->len; }

int8_t z_slice_decode_into_string(const z_loaned_slice_t *slice, z_owned_string_t *s) {
const uint8_t *z_bytes_data(const z_loaned_bytes_t *bytes) { return bytes->_slice.start; }

size_t z_bytes_len(const z_loaned_bytes_t *bytes) { return bytes->_slice.len; }

int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *s) {
// Init owned string
Expand All @@ -274,7 +276,7 @@ int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_
return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
}
// Convert slice to string
*s->_val = _z_string_from_bytes(&bytes->slice);
*s->_val = _z_string_from_bytes(&bytes->_slice);
return _Z_RES_OK;
}

Expand All @@ -286,7 +288,7 @@ int8_t z_bytes_encode_from_string(z_owned_bytes_t *buffer, const z_loaned_string
return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
}
// Encode data
buffer->_val->slice = _z_slice_wrap((uint8_t *)s->val, s->len);
buffer->_val->_slice = _z_slice_wrap((uint8_t *)s->val, s->len);
return _Z_RES_OK;
}

Expand Down Expand Up @@ -443,10 +445,10 @@ VIEW_FUNCTIONS_PTR(_z_string_vec_t, string_array)
OWNED_FUNCTIONS_PTR(_z_slice_t, slice, _z_slice_copy, _z_slice_free)
OWNED_FUNCTIONS_PTR(_z_bytes_t, bytes, _z_bytes_copy, _z_bytes_free)

static _z_slice_t _z_slice_from_owned_bytes(z_owned_slice_t *bytes) {
_z_slice_t b = _z_slice_empty();
static _z_bytes_t _z_bytes_from_owned_bytes(z_owned_bytes_t *bytes) {
_z_bytes_t b = _z_bytes_null();
if ((bytes != NULL) && (bytes->_val != NULL)) {
b = _z_slice_wrap(bytes->_val->start, bytes->_val->len);
b._slice = _z_slice_wrap(bytes->_val->_slice.start, bytes->_val->_slice.len);
}
return b;
}
Expand Down Expand Up @@ -624,7 +626,7 @@ z_id_t z_info_zid(const z_loaned_session_t *zs) { return _Z_RC_IN_VAL(zs)._local

const z_loaned_keyexpr_t *z_sample_keyexpr(const z_loaned_sample_t *sample) { return &_Z_RC_IN_VAL(sample).keyexpr; }
z_sample_kind_t z_sample_kind(const z_loaned_sample_t *sample) { return _Z_RC_IN_VAL(sample).kind; }
const z_loaned_slice_t *z_sample_payload(const z_loaned_sample_t *sample) { return &_Z_RC_IN_VAL(sample).payload; }
const z_loaned_bytes_t *z_sample_payload(const z_loaned_sample_t *sample) { return &_Z_RC_IN_VAL(sample).payload; }
z_timestamp_t z_sample_timestamp(const z_loaned_sample_t *sample) { return _Z_RC_IN_VAL(sample).timestamp; }
const z_loaned_encoding_t *z_sample_encoding(const z_loaned_sample_t *sample) { return &_Z_RC_IN_VAL(sample).encoding; }
z_qos_t z_sample_qos(const z_loaned_sample_t *sample) { return _Z_RC_IN_VAL(sample).qos; }
Expand Down Expand Up @@ -860,7 +862,7 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co
opt.consolidation = options->consolidation;
opt.target = options->target;
opt.encoding = options->encoding;
opt.payload = z_slice_move(options->payload);
opt.payload = z_bytes_move(options->payload);
#if Z_FEATURE_ATTACHMENT == 1
opt.attachment = options->attachment;
#endif
Expand All @@ -875,7 +877,7 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co
}
}
// Set value
_z_value_t value = {.payload = _z_slice_from_owned_bytes(opt.payload),
_z_value_t value = {.payload = _z_bytes_from_owned_bytes(opt.payload),
.encoding = _z_encoding_from_owned(opt.encoding)};

ret = _z_query(&_Z_RC_IN_VAL(zs), *keyexpr, parameters, opt.target, opt.consolidation.mode, value, callback->call,
Expand All @@ -886,7 +888,7 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co
#endif
);
if (opt.payload != NULL) {
z_slice_drop(opt.payload);
z_bytes_drop(opt.payload);
}
// Clean-up
z_encoding_drop(opt.encoding);
Expand Down Expand Up @@ -956,7 +958,7 @@ int8_t z_undeclare_queryable(z_owned_queryable_t *queryable) { return _z_queryab

void z_query_reply_options_default(z_query_reply_options_t *options) { options->encoding = NULL; }

int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, z_owned_slice_t *payload,
int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, z_owned_bytes_t *payload,
const z_query_reply_options_t *options) {
z_query_reply_options_t opts;
if (options == NULL) {
Expand All @@ -965,12 +967,12 @@ int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *ke
opts = *options;
}
// Set value
_z_value_t value = {.payload = _z_slice_from_owned_bytes(payload),
_z_value_t value = {.payload = _z_bytes_from_owned_bytes(payload),
.encoding = _z_encoding_from_owned(opts.encoding)};

int8_t ret = _z_send_reply(&query->in->val, *keyexpr, value, Z_SAMPLE_KIND_PUT, opts.attachment);
if (payload != NULL) {
z_slice_drop(payload);
z_bytes_drop(payload);
}
// Clean-up
z_encoding_drop(opts.encoding);
Expand Down
26 changes: 22 additions & 4 deletions src/collections/slice.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,39 @@ _Bool _z_slice_eq(const _z_slice_t *left, const _z_slice_t *right) {
}

/*-------- Bytes --------*/
_Bool _z_bytes_check(_z_bytes_t bytes) { return _z_slice_check(bytes._slice); }

_z_bytes_t _z_bytes_null(void) {
return (_z_bytes_t){
._slice = _z_slice_empty(),
};
}

void _z_bytes_copy(_z_bytes_t *dst, const _z_bytes_t *src) {
// Init only if needed
if (!_z_slice_check(dst->slice)) {
if (_z_slice_init(&dst->slice, src->slice.len) != _Z_RES_OK) {
if (!_z_slice_check(dst->_slice)) {
if (_z_slice_init(&dst->_slice, src->_slice.len) != _Z_RES_OK) {
return;
}
}
(void)memcpy((uint8_t *)dst->slice.start, src->slice.start, src->slice.len);
(void)memcpy((uint8_t *)dst->_slice.start, src->_slice.start, src->_slice.len);
}

_z_bytes_t _z_bytes_duplicate(const _z_bytes_t *src) {
_z_bytes_t dst = _z_bytes_null();
_z_bytes_copy(&dst, src);
return dst;
}

void _z_bytes_move(_z_bytes_t *dst, _z_bytes_t *src) { _z_slice_move(&dst->_slice, &src->_slice); }

void _z_bytes_clear(_z_bytes_t *bytes) { _z_slice_clear(&bytes->_slice); }

void _z_bytes_free(_z_bytes_t **bs) {
_z_bytes_t *ptr = *bs;

if (ptr != NULL) {
_z_slice_clear(&ptr->slice);
_z_bytes_clear(ptr);

z_free(ptr);
*bs = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/net/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void _z_hello_free(_z_hello_t **hello) {

void _z_value_clear(_z_value_t *value) {
_z_encoding_clear(&value->encoding);
_z_slice_clear(&value->payload);
_z_bytes_clear(&value->payload);
}

void _z_value_free(_z_value_t **value) {
Expand Down
2 changes: 1 addition & 1 deletion src/net/primitives.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ int8_t _z_send_reply(const _z_query_t *query, _z_keyexpr_t keyexpr, const _z_val
z_msg._body._response._tag = _Z_RESPONSE_BODY_REPLY;
z_msg._body._response._body._reply._consolidation = Z_CONSOLIDATION_MODE_DEFAULT;
z_msg._body._response._body._reply._body._is_put = true;
z_msg._body._response._body._reply._body._body._put._payload = payload.payload;
z_msg._body._response._body._reply._body._body._put._payload = payload.payload._slice;
z_msg._body._response._body._reply._body._body._put._encoding = payload.encoding;
z_msg._body._response._body._reply._body._body._put._commons._timestamp = _z_timestamp_null();
z_msg._body._response._body._reply._body._body._put._commons._source_info = _z_source_info_null();
Expand Down
2 changes: 1 addition & 1 deletion src/net/reply.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ _z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id,
_z_sample_t sample = _z_sample_null();
sample.keyexpr = keyexpr; // FIXME: call z_keyexpr_move or copy
sample.encoding = encoding; // FIXME: call z_encoding_move or copy
_z_slice_copy(&sample.payload, payload);
_z_slice_copy(&sample.payload._slice, payload);
sample.kind = kind;
sample.timestamp = _z_timestamp_duplicate(timestamp);
#if Z_FEATURE_ATTACHMENT == 1
Expand Down
12 changes: 6 additions & 6 deletions src/net/sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
_z_sample_t _z_sample_null(void) {
_z_sample_t s = {
.keyexpr = _z_keyexpr_null(),
.payload = _z_slice_empty(),
.payload = _z_bytes_null(),
.encoding = _z_encoding_null(),
.timestamp = _z_timestamp_null(),
.kind = 0,
Expand All @@ -32,15 +32,15 @@ _z_sample_t _z_sample_null(void) {
}

_Bool _z_sample_check(const _z_sample_t *sample) {
return _z_keyexpr_check(sample->keyexpr) && _z_slice_check(sample->payload);
return _z_keyexpr_check(sample->keyexpr) && _z_bytes_check(sample->payload);
}

void _z_sample_move(_z_sample_t *dst, _z_sample_t *src) {
dst->keyexpr._id = src->keyexpr._id; // FIXME: call the z_keyexpr_move
dst->keyexpr._suffix = src->keyexpr._suffix; // FIXME: call the z_keyexpr_move
src->keyexpr._suffix = NULL; // FIXME: call the z_keyexpr_move

_z_slice_move(&dst->payload, &src->payload);
_z_bytes_move(&dst->payload, &src->payload);
_z_encoding_move(&dst->encoding, &src->encoding);

dst->timestamp.time = src->timestamp.time; // FIXME: call the z_timestamp_move
Expand All @@ -49,7 +49,7 @@ void _z_sample_move(_z_sample_t *dst, _z_sample_t *src) {

void _z_sample_clear(_z_sample_t *sample) {
_z_keyexpr_clear(&sample->keyexpr);
_z_slice_clear(&sample->payload);
_z_bytes_clear(&sample->payload);
_z_encoding_clear(&sample->encoding);
_z_timestamp_clear(&sample->timestamp);
#if Z_FEATURE_ATTACHMENT == 1
Expand All @@ -68,7 +68,7 @@ void _z_sample_free(_z_sample_t **sample) {

void _z_sample_copy(_z_sample_t *dst, const _z_sample_t *src) {
dst->keyexpr = _z_keyexpr_duplicate(src->keyexpr);
dst->payload = _z_slice_duplicate(&src->payload);
dst->payload = _z_bytes_duplicate(&src->payload);
dst->timestamp = _z_timestamp_duplicate(&src->timestamp);
_z_encoding_copy(&dst->encoding, &src->encoding);

Expand All @@ -90,7 +90,7 @@ _z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_slice_t *payload,
const z_attachment_t att) {
_z_sample_t s = _z_sample_null();
_z_keyexpr_copy(&s.keyexpr, key);
_z_slice_copy(&s.payload, payload);
_z_slice_copy(&s.payload._slice, payload);
_z_encoding_copy(&s.encoding, &encoding);
s.kind = kind;
s.timestamp = timestamp;
Expand Down
4 changes: 4 additions & 0 deletions src/protocol/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ int8_t _z_slice_val_decode(_z_slice_t *bs, _z_zbuf_t *zbf) { return _z_slice_val

int8_t _z_slice_decode(_z_slice_t *bs, _z_zbuf_t *zbf) { return _z_slice_decode_na(bs, zbf); }

int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *zbf) { return _z_slice_decode_na(&bs->_slice, zbf); }

int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { return _z_slice_encode(wbf, &bs->_slice); }

/*------------------ string with null terminator ------------------*/
int8_t _z_str_encode(_z_wbuf_t *wbf, const char *s) {
size_t len = strlen(s);
Expand Down
6 changes: 3 additions & 3 deletions src/protocol/codec/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,9 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *msg) {
}
_Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader));
_Z_RETURN_IF_ERR(
_z_zsize_encode(wbf, _z_encoding_len(&msg->_ext_value.encoding) + msg->_ext_value.payload.len));
_z_zsize_encode(wbf, _z_encoding_len(&msg->_ext_value.encoding) + msg->_ext_value.payload._slice.len));
_Z_RETURN_IF_ERR(_z_encoding_encode(wbf, &msg->_ext_value.encoding));
_Z_RETURN_IF_ERR(_z_slice_val_encode(wbf, &msg->_ext_value.payload));
_Z_RETURN_IF_ERR(_z_slice_val_encode(wbf, &msg->_ext_value.payload._slice));
}
if (required_exts.info) {
uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01;
Expand Down Expand Up @@ -471,7 +471,7 @@ int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) {
_z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val);
ret = _z_encoding_decode(&msg->_ext_value.encoding, &zbf);
_z_slice_t bytes = _z_slice_wrap((uint8_t *)_z_zbuf_start(&zbf), _z_zbuf_len(&zbf));
_z_slice_copy(&msg->_ext_value.payload, &bytes);
_z_slice_copy(&msg->_ext_value.payload._slice, &bytes);
break;
}
#if Z_FEATURE_ATTACHMENT == 1
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ _z_value_t _z_value_steal(_z_value_t *value) {
}
void _z_value_copy(_z_value_t *dst, const _z_value_t *src) {
_z_encoding_copy(&dst->encoding, &src->encoding);
_z_slice_copy(&dst->payload, &src->payload);
_z_bytes_copy(&dst->payload, &src->payload);
}

z_attachment_t z_attachment_null(void) { return (z_attachment_t){.data = NULL, .iteration_driver = NULL}; }
Expand Down
Loading

0 comments on commit ed613b7

Please sign in to comment.