From 58a73de82d2dcfa11c02e6600b11353654ccc2eb Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 20 Jun 2024 18:32:11 +0200 Subject: [PATCH 01/25] wip --- CMakeLists.txt | 3 + include/zenoh-pico/collections/arc_slice.h | 56 +++ include/zenoh-pico/collections/bytes.h | 99 +++++ include/zenoh-pico/collections/element.h | 5 + include/zenoh-pico/collections/slice.h | 1 + include/zenoh-pico/collections/string.h | 1 + include/zenoh-pico/collections/vec.h | 58 ++- include/zenoh-pico/protocol/codec/core.h | 8 + include/zenoh-pico/utils/result.h | 2 + src/collections/arc_slice.c | 89 +++++ src/collections/bytes.c | 427 +++++++++++++++++++++ src/collections/slice.c | 5 + src/collections/string.c | 18 +- src/collections/vec.c | 139 ++++++- src/protocol/codec.c | 55 ++- tests/z_bytes_test.c | 87 +++++ zenohpico.pc | 2 +- 17 files changed, 1031 insertions(+), 24 deletions(-) create mode 100644 include/zenoh-pico/collections/arc_slice.h create mode 100644 include/zenoh-pico/collections/bytes.h create mode 100644 src/collections/arc_slice.c create mode 100644 src/collections/bytes.c create mode 100644 tests/z_bytes_test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index cf15dadea..e873b396d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -342,6 +342,7 @@ if(UNIX OR MSVC) add_executable(z_test_fragment_rx ${PROJECT_SOURCE_DIR}/tests/z_test_fragment_rx.c) add_executable(z_perf_tx ${PROJECT_SOURCE_DIR}/tests/z_perf_tx.c) add_executable(z_perf_rx ${PROJECT_SOURCE_DIR}/tests/z_perf_rx.c) + add_executable(z_bytes_test ${PROJECT_SOURCE_DIR}/tests/z_bytes_test.c) target_link_libraries(z_data_struct_test ${Libname}) target_link_libraries(z_channels_test ${Libname}) @@ -356,6 +357,7 @@ if(UNIX OR MSVC) target_link_libraries(z_test_fragment_rx ${Libname}) target_link_libraries(z_perf_tx ${Libname}) target_link_libraries(z_perf_rx ${Libname}) + target_link_libraries(z_bytes_test ${Libname}) configure_file(${PROJECT_SOURCE_DIR}/tests/modularity.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/modularity.py COPYONLY) configure_file(${PROJECT_SOURCE_DIR}/tests/raweth.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/raweth.py COPYONLY) @@ -373,6 +375,7 @@ if(UNIX OR MSVC) add_test(z_keyexpr_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_keyexpr_test) add_test(z_api_null_drop_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_null_drop_test) add_test(z_api_double_drop_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_double_drop_test) + add_test(z_bytes_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_bytes_test) endif() if(BUILD_MULTICAST) diff --git a/include/zenoh-pico/collections/arc_slice.h b/include/zenoh-pico/collections/arc_slice.h new file mode 100644 index 000000000..8c9738918 --- /dev/null +++ b/include/zenoh-pico/collections/arc_slice.h @@ -0,0 +1,56 @@ +// +// Copyright (c) 2024 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#ifndef ZENOH_PICO_COLLECTIONS_ARC_SLICE_H +#define ZENOH_PICO_COLLECTIONS_ARC_SLICE_H + +#include +#include +#include +#include + +#include "slice.h" +#include "refcount.h" +#include "zenoh-pico/system/platform-common.h" + + +_Z_REFCOUNT_DEFINE(_z_slice, _z_slice); + +/*-------- ArcSlice --------*/ +/** + * An atomically reference counted subslice. + * + * Members: + * _z_slice_rc_t len: Rc counted slice. + * size_t start: Offset to the subslice start. + * size_t len: Length of the subslice. + */ + +typedef struct { + _z_slice_rc_t slice; + size_t start; + size_t len; +} _z_arc_slice_t; + +_z_arc_slice_t _z_arc_slice_empty(void); +_z_arc_slice_t _z_arc_slice_wrap(_z_slice_t s, size_t offset, size_t len); +_z_arc_slice_t _z_arc_slice_get_subslice(const _z_arc_slice_t* s, size_t offset, size_t len); +size_t _z_arc_slice_len(const _z_arc_slice_t* s); +_Bool _z_arc_slice_is_empty(const _z_arc_slice_t* s); +const uint8_t* _z_arc_slice_data(const _z_arc_slice_t* s); +int8_t _z_arc_slice_copy(_z_arc_slice_t *dst, const _z_arc_slice_t *src); +int8_t _z_arc_slice_move(_z_arc_slice_t *dst, _z_arc_slice_t *src); +int8_t _z_arc_slice_drop(_z_arc_slice_t *s); + +#endif /* ZENOH_PICO_COLLECTIONS_ARC_SLICE_H */ \ No newline at end of file diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h new file mode 100644 index 000000000..286fe22fb --- /dev/null +++ b/include/zenoh-pico/collections/bytes.h @@ -0,0 +1,99 @@ +// +// Copyright (c) 2024 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#ifndef ZENOH_PICO_COLLECTIONS_BYTES_H +#define ZENOH_PICO_COLLECTIONS_BYTES_H + +#include +#include +#include +#include "vec.h" +#include "arc_slice.h" +#include "zenoh-pico/protocol/iobuf.h" + + + +inline size_t _z_arc_slice_size(const _z_arc_slice_t *s) { + (void)s; + return sizeof(_z_arc_slice_size); +} +static inline void _z_arc_slice_elem_move(void *dst, void *src) { + _z_arc_slice_move((_z_arc_slice_t *)dst, (_z_arc_slice_t*)src); +} +_Z_ELEM_DEFINE(_z_arc_slice, _z_arc_slice_t, _z_arc_slice_size, _z_arc_slice_drop, _z_arc_slice_copy) +_Z_SVEC_DEFINE(_z_arc_slice, _z_arc_slice_t) + + +/*-------- Bytes --------*/ +/** + * A container for slices. + * + * Members: + * _z_slice_vec_t _slices: contents of the container. + */ + +typedef struct { + _z_arc_slice_svec_t _slices; +} _zz_bytes_t; + + +_Bool _zz_bytes_check(const _zz_bytes_t *bytes); +_zz_bytes_t _zz_bytes_null(void); +_zz_bytes_t _zz_bytes_make(size_t capacity); +int8_t _zz_bytes_append(_zz_bytes_t* dst, _zz_bytes_t* src); +int8_t _zz_bytes_append_slice(_zz_bytes_t* dst, _z_arc_slice_t* s); +int8_t _zz_bytes_copy(_zz_bytes_t *dst, const _zz_bytes_t *src); +_zz_bytes_t _zz_bytes_duplicate(const _zz_bytes_t *src); +void _zz_bytes_move(_zz_bytes_t *dst, _zz_bytes_t *src); +void _zz_bytes_clear(_zz_bytes_t *bytes); +void _zz_bytes_free(_zz_bytes_t **bs); +size_t _zz_bytes_num_slices(const _zz_bytes_t *bs); +_z_arc_slice_t* _zz_bytes_get_slice(const _zz_bytes_t *bs, size_t i); +size_t _zz_bytes_len(const _zz_bytes_t *bs); +_Bool _zz_bytes_is_empty(const _zz_bytes_t *bs); +uint8_t _zz_bytes_to_uint8(const _zz_bytes_t *bs); +uint16_t _zz_bytes_to_uint16(const _zz_bytes_t *bs); +uint32_t _zz_bytes_to_uint32(const _zz_bytes_t *bs); +uint64_t _zz_bytes_to_uint64(const _zz_bytes_t *bs); +float _zz_bytes_to_float(const _zz_bytes_t *bs); +double _zz_bytes_to_double(const _zz_bytes_t *bs); +_z_slice_t _zz_bytes_to_slice(const _zz_bytes_t *bytes); +_zz_bytes_t _zz_bytes_from_slice(_z_slice_t s); +_zz_bytes_t _zz_bytes_from_uint8(uint8_t val); +_zz_bytes_t _zz_bytes_from_uint16(uint16_t val); +_zz_bytes_t _zz_bytes_from_uint32(uint32_t val); +_zz_bytes_t _zz_bytes_from_uint64(uint64_t val); +_zz_bytes_t _zz_bytes_from_float(float val); +_zz_bytes_t _zz_bytes_from_double(double val); +size_t _zz_bytes_to_buf(const _zz_bytes_t *bytes, uint8_t* dst, size_t len); +_zz_bytes_t _zz_bytes_from_buf(uint8_t* src, size_t len); + + +typedef struct { + size_t slice_idx; + size_t in_slice_idx; + size_t byte_idx; + const _zz_bytes_t* bytes; +} _zz_bytes_reader_t; + + + +_zz_bytes_reader_t _zz_bytes_get_reader(const _zz_bytes_t *bytes); +int8_t _zz_bytes_reader_seek(_zz_bytes_reader_t *reader, int64_t offset, int origin); +int64_t _zz_bytes_reader_tell(const _zz_bytes_reader_t *reader); +int8_t _zz_bytes_reader_read_slices(_zz_bytes_reader_t* reader, size_t len, _zz_bytes_t* out); +int8_t _zz_bytes_reader_read(_zz_bytes_reader_t *reader, uint8_t* buf, size_t len); +int8_t _zz_bytes_reader_read_next(_zz_bytes_reader_t* reader, _zz_bytes_t* out); + +#endif /* ZENOH_PICO_COLLECTIONS_BYTES_H */ \ No newline at end of file diff --git a/include/zenoh-pico/collections/element.h b/include/zenoh-pico/collections/element.h index 5f1bfd7b0..8f7dc507d 100644 --- a/include/zenoh-pico/collections/element.h +++ b/include/zenoh-pico/collections/element.h @@ -66,6 +66,11 @@ static inline void _z_noop_copy(void *dst, const void *src) { (void)(src); } +static inline void _z_noop_move(void *dst, void *src) { + (void)(dst); + (void)(src); +} + _Z_ELEM_DEFINE(_z_noop, _z_noop_t, _z_noop_size, _z_noop_clear, _z_noop_copy) #endif /* ZENOH_PICO_COLLECTIONS_ELEMENT_H */ diff --git a/include/zenoh-pico/collections/slice.h b/include/zenoh-pico/collections/slice.h index e491a2041..875771dbe 100644 --- a/include/zenoh-pico/collections/slice.h +++ b/include/zenoh-pico/collections/slice.h @@ -39,6 +39,7 @@ inline static _Bool _z_slice_check(_z_slice_t value) { return value.start != NUL int8_t _z_slice_init(_z_slice_t *bs, size_t capacity); _z_slice_t _z_slice_make(size_t capacity); _z_slice_t _z_slice_wrap(const uint8_t *bs, size_t len); +_z_slice_t _z_slice_wrap_copy(const uint8_t *bs, size_t len); _z_slice_t _z_slice_steal(_z_slice_t *b); void _z_slice_copy(_z_slice_t *dst, const _z_slice_t *src); _z_slice_t _z_slice_duplicate(const _z_slice_t *src); diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index e03139cfe..24546d00f 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -83,6 +83,7 @@ void _z_string_free(_z_string_t **s); void _z_string_reset(_z_string_t *s); _z_string_t _z_string_convert_bytes(const _z_slice_t *bs); _z_string_t _z_string_from_bytes(const _z_slice_t *bs); +_z_string_t _z_string_preallocate(const size_t len); _Z_ELEM_DEFINE(_z_string, _z_string_t, _z_string_size, _z_string_clear, _z_string_copy) diff --git a/include/zenoh-pico/collections/vec.h b/include/zenoh-pico/collections/vec.h index a6bcdb71d..58874550b 100644 --- a/include/zenoh-pico/collections/vec.h +++ b/include/zenoh-pico/collections/vec.h @@ -43,6 +43,7 @@ void _z_vec_remove(_z_vec_t *sv, size_t pos, z_element_free_f f); void _z_vec_reset(_z_vec_t *v, z_element_free_f f); void _z_vec_clear(_z_vec_t *v, z_element_free_f f); void _z_vec_free(_z_vec_t **v, z_element_free_f f); +void _z_vec_release(_z_vec_t *v); #define _Z_VEC_DEFINE(name, type) \ typedef _z_vec_t name##_vec_t; \ @@ -60,6 +61,61 @@ void _z_vec_free(_z_vec_t **v, z_element_free_f f); } \ static inline void name##_vec_reset(name##_vec_t *v) { _z_vec_reset(v, name##_elem_free); } \ static inline void name##_vec_clear(name##_vec_t *v) { _z_vec_clear(v, name##_elem_free); } \ - static inline void name##_vec_free(name##_vec_t **v) { _z_vec_free(v, name##_elem_free); } + static inline void name##_vec_free(name##_vec_t **v) { _z_vec_free(v, name##_elem_free); } \ + static inline void name##_vec_release(name##_vec_t *v) { _z_vec_release(v); } + + +/*-------- Dynamically allocated sized vector --------*/ +/** + * A dynamically allocate vector. + */ +typedef struct { + size_t _capacity; + size_t _len; + void *_val; +} _z_svec_t; + +_z_svec_t _z_svec_make(size_t capacity, size_t element_size); +void _z_svec_copy(_z_svec_t *dst, const _z_svec_t *src, z_element_copy_f copy, size_t element_size); + +size_t _z_svec_len(const _z_svec_t *v); +_Bool _z_svec_is_empty(const _z_svec_t *v); + +bool _z_svec_append(_z_svec_t *v, const void *e, z_element_move_f m, size_t element_size); +void *_z_svec_get(const _z_svec_t *v, size_t pos, size_t element_size); +void _z_svec_set(_z_svec_t *sv, size_t pos, void *e, z_element_clear_f f, size_t element_size); +void _z_svec_remove(_z_svec_t *sv, size_t pos, z_element_clear_f f, z_element_move_f m, size_t element_size); + +void _z_svec_reset(_z_svec_t *v, z_element_clear_f f, size_t element_size); +void _z_svec_clear(_z_svec_t *v, z_element_clear_f f, size_t element_size); +void _z_svec_free(_z_svec_t **v, z_element_clear_f f, size_t element_size); +void _z_svec_release(_z_svec_t *v); + +#define _Z_SVEC_DEFINE(name, type) \ + typedef _z_svec_t name##_svec_t; \ + static inline name##_svec_t name##_svec_make(size_t capacity) { \ + return _z_svec_make(capacity, sizeof(type)); \ + } \ + static inline size_t name##_svec_len(const name##_svec_t *v) { return _z_svec_len(v); } \ + static inline _Bool name##_svec_is_empty(const name##_svec_t *v) { return _z_svec_is_empty(v); } \ + static inline _Bool name##_svec_append(name##_svec_t *v, type *e) { \ + return _z_svec_append(v, e, name##_elem_move, sizeof(type)); \ + } \ + static inline type *name##_svec_get(const name##_svec_t *v, size_t pos) { \ + return (type *)_z_svec_get(v, pos, sizeof(type)); \ + } \ + static inline void name##_svec_set(name##_svec_t *v, size_t pos, type *e) { \ + _z_svec_set(v, pos, e, name##_elem_clear, sizeof(type)); \ + } \ + static inline void name##_svec_remove(name##_svec_t *v, size_t pos) { \ + _z_svec_remove(v, pos, name##_elem_clear, name##_elem_move, sizeof(type)); \ + } \ + static inline void name##_svec_copy(name##_svec_t *dst, const name##_svec_t *src) { \ + _z_svec_copy(dst, src, name##_elem_copy, sizeof(type)); \ + } \ + static inline void name##_svec_reset(name##_svec_t *v) { _z_svec_reset(v, name##_elem_clear, sizeof(type)); } \ + static inline void name##_svec_clear(name##_svec_t *v) { _z_svec_clear(v, name##_elem_clear, sizeof(type)); } \ + static inline void name##_svec_free(name##_svec_t **v) { _z_svec_free(v, name##_elem_clear, sizeof(type)); } \ + #endif /* ZENOH_PICO_COLLECTIONS_VECTOR_H */ diff --git a/include/zenoh-pico/protocol/codec/core.h b/include/zenoh-pico/protocol/codec/core.h index cfa32cbad..0431699de 100644 --- a/include/zenoh-pico/protocol/codec/core.h +++ b/include/zenoh-pico/protocol/codec/core.h @@ -41,6 +41,7 @@ } \ } +typedef int8_t (*__z_single_byte_reader_t)(uint8_t*, void* context); /*------------------ Internal Zenoh-net Macros ------------------*/ int8_t _z_consolidation_mode_encode(_z_wbuf_t *wbf, z_consolidation_mode_t en); int8_t _z_consolidation_mode_decode(z_consolidation_mode_t *en, _z_zbuf_t *zbf); @@ -56,11 +57,18 @@ int8_t _z_uint16_encode(_z_wbuf_t *buf, uint16_t v); int8_t _z_uint16_decode(uint16_t *u16, _z_zbuf_t *buf); uint8_t _z_zint_len(uint64_t v); +uint8_t _z_zint64_encode_buf(uint8_t* buf, uint64_t v); +static inline uint8_t _z_zsize_encode_buf(uint8_t* buf, _z_zint_t v) { + return _z_zint64_encode_buf(buf, (uint64_t)v); +} + int8_t _z_zsize_encode(_z_wbuf_t *buf, _z_zint_t v); int8_t _z_zint64_encode(_z_wbuf_t *buf, uint64_t v); int8_t _z_zint16_decode(uint16_t *zint, _z_zbuf_t *buf); int8_t _z_zint32_decode(uint32_t *zint, _z_zbuf_t *buf); int8_t _z_zint64_decode(uint64_t *zint, _z_zbuf_t *buf); +int8_t _z_zint64_decode_with_reader(uint64_t *zint, __z_single_byte_reader_t reader, void *context); +int8_t _z_zsize_decode_with_reader(_z_zint_t *zint, __z_single_byte_reader_t reader, void* context); int8_t _z_zsize_decode(_z_zint_t *zint, _z_zbuf_t *buf); int8_t _z_slice_val_encode(_z_wbuf_t *buf, const _z_slice_t *bs); diff --git a/include/zenoh-pico/utils/result.h b/include/zenoh-pico/utils/result.h index a3ff7c1d6..effa199ce 100644 --- a/include/zenoh-pico/utils/result.h +++ b/include/zenoh-pico/utils/result.h @@ -67,6 +67,8 @@ typedef enum { _Z_ERR_CONNECTION_CLOSED = -77, + _Z_ERR_DID_NOT_READ = -76, + _Z_ERR_GENERIC = -128 } _z_res_t; diff --git a/src/collections/arc_slice.c b/src/collections/arc_slice.c new file mode 100644 index 000000000..0a5bb0435 --- /dev/null +++ b/src/collections/arc_slice.c @@ -0,0 +1,89 @@ +// +// Copyright (c) 2024 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#include "zenoh-pico/collections/arc_slice.h" + +#include +#include +#include +#include + + + +_z_arc_slice_t _z_arc_slice_empty(void) { + _z_arc_slice_t s; + s.len = 0; + s.start = 0; + s.slice.in = NULL; + return s; +} + +_z_arc_slice_t _z_arc_slice_wrap(_z_slice_t s, size_t offset, size_t len) { + assert(offset + len <= s.len); + + _z_arc_slice_t arc_s; + arc_s.slice = _z_slice_rc_new_from_val(s); + arc_s.len = len; + arc_s.start = offset; + return arc_s; +} + +_z_arc_slice_t _z_arc_slice_get_subslice(const _z_arc_slice_t* s, size_t offset, size_t len) { + assert(offset + len <= s->len); + assert(s->slice.in != NULL || (len == 0 && offset == 0)); + + _z_arc_slice_t out; + out.len = len; + out.start = offset; + if (s->slice.in == NULL) { + out.slice.in = NULL; + } else { + out.slice = _z_slice_rc_clone(&s->slice); + } + return out; +} + +size_t _z_arc_slice_len(const _z_arc_slice_t* s) { + return s->len; +} + +_Bool _z_arc_slice_is_empty(const _z_arc_slice_t* s) { + return _z_arc_slice_len(s) == 0; +} + +const uint8_t* _z_arc_slice_data(const _z_arc_slice_t* s) { + return s->slice.in->val.start; +} + +int8_t _z_arc_slice_copy(_z_arc_slice_t *dst, const _z_arc_slice_t *src) { + _z_slice_rc_copy(&dst->slice, &src->slice); + dst->len = src->len; + dst->start = src->start; + return _Z_RES_OK; +} + +int8_t _z_arc_slice_move(_z_arc_slice_t *dst, _z_arc_slice_t *src) { + dst->slice = src->slice; + src->len = 0; + src->start = 0; + src->slice.in = NULL; + return _Z_RES_OK; +} + +int8_t _z_arc_slice_drop(_z_arc_slice_t *s) { + _z_slice_rc_drop(&s->slice); + s->len = 0; + s->start = 0; + return _Z_RES_OK; +} \ No newline at end of file diff --git a/src/collections/bytes.c b/src/collections/bytes.c new file mode 100644 index 000000000..6845255e2 --- /dev/null +++ b/src/collections/bytes.c @@ -0,0 +1,427 @@ +// +// Copyright (c) 2024 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#include "zenoh-pico/collections/bytes.h" + +#include +#include +#include + +#include "zenoh-pico/system/platform.h" +#include "zenoh-pico/utils/result.h" +#include "zenoh-pico/protocol/codec/core.h" + +/*-------- Bytes --------*/ +_Bool _zz_bytes_check(const _zz_bytes_t *bytes) { + return !_zz_bytes_is_empty(bytes); +} + +_zz_bytes_t _zz_bytes_null(void) { + _zz_bytes_t b; + b._slices = _z_arc_slice_svec_make(0); + return b; +} + +int8_t _zz_bytes_copy(_zz_bytes_t *dst, const _zz_bytes_t *src) { + _z_arc_slice_svec_copy(&dst->_slices, &src->_slices); + if (_z_arc_slice_svec_len(&dst->_slices) == _z_arc_slice_svec_len(&dst->_slices)) { + return _Z_RES_OK; + } else { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } +} + +_zz_bytes_t _zz_bytes_duplicate(const _zz_bytes_t *src) { + _zz_bytes_t dst = _zz_bytes_null(); + _zz_bytes_copy(&dst, src); + return dst; +} + +size_t _zz_bytes_len(const _zz_bytes_t *bs) { + size_t len = 0; + for (size_t i = 0; i < _z_arc_slice_svec_len(&bs->_slices); i++) { + const _z_arc_slice_t *s = _z_arc_slice_svec_get(&bs->_slices, i); + len += _z_arc_slice_len(s); + } + return len; +} + +_Bool _zz_bytes_is_empty(const _zz_bytes_t *bs) { + for (size_t i = 0; i < _z_arc_slice_svec_len(&bs->_slices); i++) { + const _z_arc_slice_t *s = _z_arc_slice_svec_get(&bs->_slices, i); + if ( _z_arc_slice_len(s) > 0) return false; + } + return true; +} + +size_t _zz_bytes_num_slices(const _zz_bytes_t *bs) { + return _z_arc_slice_svec_len(&bs->_slices); +} + +_z_arc_slice_t* _zz_bytes_get_slice(const _zz_bytes_t *bs, size_t i) { + if (i >= _zz_bytes_num_slices(bs)) return NULL; + return _z_arc_slice_svec_get(&bs->_slices, i); +} + +void _zz_bytes_clear(_zz_bytes_t *bytes) { _z_arc_slice_svec_clear(&bytes->_slices); } + +void _zz_bytes_free(_zz_bytes_t **bs) { + _zz_bytes_t *ptr = *bs; + + if (ptr != NULL) { + _zz_bytes_clear(ptr); + + z_free(ptr); + *bs = NULL; + } +} + +size_t _zz_bytes_to_buf(const _zz_bytes_t *bytes, uint8_t* dst, size_t len) { + uint8_t* start = dst; + + for (size_t i = 0; i < _zz_bytes_num_slices(bytes) && len > 0; ++i) { + // Recopy data + _z_arc_slice_t *s = _zz_bytes_get_slice(bytes, i); + size_t s_len = _z_arc_slice_len(s); + size_t len_to_copy = len >= s_len ? s_len : len; + memcpy(start, _z_arc_slice_data(s), len_to_copy); + start += s_len; + len -= len_to_copy; + } + + return len; +} +_zz_bytes_t _zz_bytes_from_slice(_z_slice_t s) { + _zz_bytes_t b = _zz_bytes_null(); + _z_arc_slice_t arc_s = _z_arc_slice_wrap(s, 0, s.len); + _z_arc_slice_svec_append(&b._slices, &arc_s); + return b; +} + +_zz_bytes_t _zz_bytes_from_buf(uint8_t* src, size_t len) { + _z_slice_t s = _z_slice_wrap(src, len); + return _zz_bytes_from_slice(s); +} + +_z_slice_t _zz_bytes_to_slice(const _zz_bytes_t *bytes) { + // Allocate slice + _z_slice_t ret = _z_slice_make(_zz_bytes_len(bytes)); + if (!_z_slice_check(ret)) { + return ret; + } + uint8_t* start = (uint8_t*)ret.start; + for (size_t i = 0; i < _zz_bytes_num_slices(bytes); ++i) { + // Recopy data + _z_arc_slice_t* s = _zz_bytes_get_slice(bytes, i); + size_t s_len = _z_arc_slice_len(s); + memcpy(start, _z_arc_slice_data(s), s_len); + start += s_len; + } + + return ret; +} + +int8_t _zz_bytes_append_slice(_zz_bytes_t* dst, _z_arc_slice_t* s) { + return _z_arc_slice_svec_append(&dst->_slices, s); +} + +int8_t _zz_bytes_append_inner(_zz_bytes_t* dst, _zz_bytes_t* src) { + _Bool success = true; + for (size_t i = 0; i < _zz_bytes_num_slices(src); ++i) { + _z_arc_slice_t *s = _zz_bytes_get_slice(src, i); + success = success && _z_arc_slice_svec_append(&dst->_slices, s); + } + if (!success) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + + _z_svec_release(&src->_slices); + return _Z_RES_OK; +} + +int8_t _zz_bytes_append(_zz_bytes_t* dst, _zz_bytes_t* src) { + uint8_t l_buf[16]; + size_t l_len = _z_zsize_encode_buf(l_buf, _zz_bytes_len(src)); + _z_slice_t s = _z_slice_wrap_copy(l_buf, l_len); + if (!_z_slice_check(s)) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + _z_arc_slice_t arc_s = _z_arc_slice_wrap(s, 0, l_len); + _z_arc_slice_svec_append(&dst->_slices, &arc_s); + + if (dst->_slices._val == NULL) { + _z_arc_slice_drop(&arc_s); + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + int8_t res = _zz_bytes_append_inner(dst, src); + if (res != _Z_RES_OK) { + return res; + } + return _Z_RES_OK; +} + + + + +_zz_bytes_t _zz_bytes_serialize_from_pair(_zz_bytes_t* first, _zz_bytes_t* second) { + _zz_bytes_t out = _zz_bytes_null(); + + if (_zz_bytes_append(&out, first) != _Z_RES_OK) { + _zz_bytes_clear(&out); + _zz_bytes_clear(first); + } else if (_zz_bytes_append(&out, second) != _Z_RES_OK) { + _zz_bytes_clear(&out); + _zz_bytes_clear(second); + } + + return out; +} + + +int8_t _zz_bytes_deserialize_into_pair(const _zz_bytes_t* bs, _zz_bytes_t* first_out, _zz_bytes_t* second_out) { + _zz_bytes_reader_t reader = _zz_bytes_get_reader(bs); + int8_t res = _zz_bytes_reader_read_next(&reader, first_out); + if (res != _Z_RES_OK) return res; + res = _zz_bytes_reader_read_next(&reader, second_out); + if (res != _Z_RES_OK) { + _zz_bytes_clear(first_out); + }; + return res; +} + + + +uint8_t _zz_bytes_to_uint8(const _zz_bytes_t *bs) { + uint8_t val = 0; + _zz_bytes_to_buf(bs, &val, sizeof(val)); + return val; +} + +// FIXME: int16+ endianness, Issue #420 +uint16_t _zz_bytes_to_uint16(const _zz_bytes_t *bs) { + uint16_t val = 0; + _zz_bytes_to_buf(bs, (uint8_t*)&val, sizeof(val)); + return val; +} + +uint32_t _zz_bytes_to_uint32(const _zz_bytes_t *bs) { + uint32_t val = 0; + _zz_bytes_to_buf(bs, (uint8_t*)&val, sizeof(val)); + return val; +} + +uint64_t _zz_bytes_to_uint64(const _zz_bytes_t *bs) { + uint64_t val = 0; + _zz_bytes_to_buf(bs, (uint8_t*)&val, sizeof(val)); + return val; +} + +float _zz_bytes_to_float(const _zz_bytes_t *bs) { + float val = 0; + _zz_bytes_to_buf(bs, (uint8_t*)&val, sizeof(val)); + return val; +} + +double _zz_bytes_to_double(const _zz_bytes_t *bs) { + double val = 0; + _zz_bytes_to_buf(bs, (uint8_t*)&val, sizeof(val)); + return val; +} + +_zz_bytes_t _zz_bytes_from_uint8(uint8_t val) { + return _zz_bytes_from_buf(&val, sizeof(val)); +} + +_zz_bytes_t _zz_bytes_from_uint16(uint16_t val) { + return _zz_bytes_from_buf((uint8_t*)&val, sizeof(val)); +} + +_zz_bytes_t _zz_bytes_from_uint32(uint32_t val) { + return _zz_bytes_from_buf((uint8_t*)&val, sizeof(val)); +} + +_zz_bytes_t _zz_bytes_from_uint64(uint64_t val) { + return _zz_bytes_from_buf((uint8_t*)&val, sizeof(val)); +} + +_zz_bytes_t _zz_bytes_from_float(float val) { + return _zz_bytes_from_buf((uint8_t*)&val, sizeof(val)); +} + +_zz_bytes_t _zz_bytes_from_double(double val) { + return _zz_bytes_from_buf((uint8_t*)&val, sizeof(val)); +} + +_zz_bytes_reader_t _zz_bytes_get_reader(const _zz_bytes_t *bytes) { + _zz_bytes_reader_t r; + r.bytes = bytes; + r.byte_idx = 0; + r.in_slice_idx = 0; + return r; +} + +int8_t _zz_bytes_reader_seek_forward(_zz_bytes_reader_t *reader, size_t offset) { + size_t start_slice = reader->slice_idx; + for (size_t i = start_slice; i < _zz_bytes_num_slices(reader->bytes); ++i) { + _z_arc_slice_t* s = _zz_bytes_get_slice(reader->bytes, i); + size_t remaining = _z_arc_slice_len(s) - reader->in_slice_idx; + if (offset >= remaining) { + reader->slice_idx += 1; + reader->in_slice_idx = 0; + reader->byte_idx += remaining; + offset -= remaining; + } else { + reader->in_slice_idx += offset; + reader->byte_idx += offset; + offset = 0; + } + if (offset == 0) break; + } + + if (offset > 0) return _Z_ERR_DID_NOT_READ; + return _Z_RES_OK; +} + +int8_t _zz_bytes_reader_seek_backward(_zz_bytes_reader_t *reader, size_t offset) { + while (offset != 0) { + if (reader->in_slice_idx == 0) { + if (reader->slice_idx == 0) return _Z_ERR_DID_NOT_READ; + reader->slice_idx--; + _z_arc_slice_t* s = _zz_bytes_get_slice(reader->bytes, reader->slice_idx); + s = _zz_bytes_get_slice(reader->bytes, reader->slice_idx); + reader->in_slice_idx = _z_arc_slice_len(s); + } + + if (offset > reader->in_slice_idx) { + offset -= reader->in_slice_idx; + reader->byte_idx -= reader->in_slice_idx; + reader->in_slice_idx = 0; + } else { + offset = 0; + reader->byte_idx -= offset; + reader->in_slice_idx -= offset; + } + } + return _Z_RES_OK; +} + + +int8_t _zz_bytes_reader_seek(_zz_bytes_reader_t *reader, int64_t offset, int origin) { + switch (origin) { + case SEEK_SET: { + reader->byte_idx = 0; + reader->in_slice_idx = 0; + reader->slice_idx = 0; + if (offset < 0) return _Z_ERR_DID_NOT_READ; + return _zz_bytes_reader_seek_forward(reader, offset); + } + case SEEK_CUR: { + if (offset >= 0) return _zz_bytes_reader_seek_forward(reader, offset); + else return _zz_bytes_reader_seek_backward(reader, -offset); + } + case SEEK_END: { + if (offset > 0) return _Z_ERR_DID_NOT_READ; + else return _zz_bytes_reader_seek_backward(reader, -offset); + } + default: return _Z_ERR_GENERIC; + } +} + +int64_t _zz_bytes_reader_tell(const _zz_bytes_reader_t *reader) { + return reader->byte_idx; +} + +int8_t _zz_bytes_reader_read(_zz_bytes_reader_t *reader, uint8_t* buf, size_t len) { + uint8_t* buf_start = buf; + + for (size_t i = reader->slice_idx; i < _zz_bytes_num_slices(reader->bytes); ++i) { + _z_arc_slice_t* s = _zz_bytes_get_slice(reader->bytes, i); + size_t remaining = _z_arc_slice_len(s) - reader->in_slice_idx; + if (len >= remaining) { + memcpy(buf_start, _z_arc_slice_data(s) + reader->in_slice_idx, remaining); + reader->slice_idx += 1; + reader->in_slice_idx = 0; + reader->byte_idx += remaining; + len -= remaining; + buf_start += remaining; + } else { + memcpy(buf_start, _z_arc_slice_data(s) + reader->in_slice_idx, len); + reader->in_slice_idx += len; + reader->byte_idx += len; + len = 0; + } + if (len == 0) break; + } + + if (len > 0) return _Z_ERR_DID_NOT_READ; + return _Z_RES_OK; +} + + +int8_t __read_single_byte(uint8_t* b, void* context) { + _zz_bytes_reader_t* reader = (_zz_bytes_reader_t*)context; + return _zz_bytes_reader_read(reader, b, 1); +} + +int8_t _zz_bytes_reader_read_zint(_zz_bytes_reader_t *reader, _z_zint_t *zint) { + return _z_zsize_decode_with_reader(zint, __read_single_byte, reader); +} + +int8_t _zz_bytes_reader_read_slices(_zz_bytes_reader_t* reader, size_t len, _zz_bytes_t* out) { + *out = _zz_bytes_null(); + int8_t res = _Z_RES_OK; + + for (size_t i = reader->slice_idx; i < _zz_bytes_num_slices(reader->bytes) && len > 0; ++i) { + _z_arc_slice_t* s = _zz_bytes_get_slice(reader->bytes, i); + size_t s_len = _z_arc_slice_len(s); + + size_t remaining = s_len - reader->in_slice_idx; + size_t len_to_copy = remaining > len ? len : remaining; + _z_arc_slice_t ss = _z_arc_slice_get_subslice(s, reader->in_slice_idx, len_to_copy); + reader->in_slice_idx += len_to_copy; + reader->byte_idx += len_to_copy; + if (reader->in_slice_idx == s_len) { + reader->slice_idx++; + reader->in_slice_idx = 0; + } + + if (ss.slice.in == NULL) { + res = _Z_ERR_SYSTEM_OUT_OF_MEMORY; + break; + } + res = _z_arc_slice_svec_append(&out->_slices, &ss); + if (res != _Z_RES_OK) { + _z_arc_slice_drop(&ss); + break; + } + len -= len_to_copy; + } + + if (len > 0 && res == _Z_RES_OK) res = _Z_ERR_DID_NOT_READ; + if (res != _Z_RES_OK) _zz_bytes_clear(out); + + return res; + +} + +int8_t _zz_bytes_reader_read_next(_zz_bytes_reader_t* reader, _zz_bytes_t* out) { + *out = _zz_bytes_null(); + _z_zint_t len; + if (_zz_bytes_reader_read_zint(reader, &len) != _Z_RES_OK) { + return _Z_ERR_DID_NOT_READ; + } + size_t offset = _zz_bytes_reader_tell(reader); + + return _zz_bytes_reader_read_slices(reader, len, out); +} \ No newline at end of file diff --git a/src/collections/slice.c b/src/collections/slice.c index 59668747e..6de05ddad 100644 --- a/src/collections/slice.c +++ b/src/collections/slice.c @@ -53,6 +53,11 @@ _z_slice_t _z_slice_wrap(const uint8_t *p, size_t len) { return bs; } +_z_slice_t _z_slice_wrap_copy(const uint8_t *p, size_t len) { + _z_slice_t bs = _z_slice_wrap(p, len); + return _z_slice_duplicate(&bs); +} + void _z_slice_reset(_z_slice_t *bs) { bs->start = NULL; bs->len = 0; diff --git a/src/collections/string.c b/src/collections/string.c index 47c3609ad..253feb55d 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -111,18 +111,26 @@ _z_string_t _z_string_convert_bytes(const _z_slice_t *bs) { } _z_string_t _z_string_from_bytes(const _z_slice_t *bs) { + _z_string_t s = _z_string_preallocate(bs->len); + if (s.val == NULL) { + return s; + } + // Recopy data + memcpy(s.val, bs->start, bs->len); + return s; +} + +_z_string_t _z_string_preallocate(size_t len) { _z_string_t s = _z_string_null(); // Allocate string - s.len = bs->len + (size_t)1; // bytes data + null terminator + s.len = len + (size_t)1; // bytes data + null terminator char *str_val = (char *)z_malloc(s.len * sizeof(char)); if (str_val == NULL) { + s.len = 0; return s; } - // Recopy data s.val = str_val; - memcpy(s.val, bs->start, bs->len); - // Set null terminator - s.val[bs->len] = '\0'; + s.val[len] = '\0'; return s; } diff --git a/src/collections/vec.c b/src/collections/vec.c index 8da49dd72..92ab772cb 100644 --- a/src/collections/vec.c +++ b/src/collections/vec.c @@ -53,12 +53,15 @@ void _z_vec_clear(_z_vec_t *v, z_element_free_f free_f) { for (size_t i = 0; i < v->_len; i++) { free_f(&v->_val[i]); } + _z_vec_release(v); +} +void _z_vec_release(_z_vec_t *v) { z_free(v->_val); v->_val = NULL; v->_capacity = 0; - v->_len = 0; + v->_len = 0; } void _z_vec_free(_z_vec_t **v, z_element_free_f free_f) { @@ -124,3 +127,137 @@ void _z_vec_remove(_z_vec_t *v, size_t pos, z_element_free_f free_f) { v->_val[v->_len] = NULL; v->_len = v->_len - 1; } + + + +/*-------- svec --------*/ +_z_svec_t _z_svec_make(size_t capacity, size_t element_size) { + _z_svec_t v = {._capacity = 0, ._len = 0, ._val = NULL}; + if (capacity != 0) { + v._val = z_malloc(element_size * capacity); + } + if (v._val != NULL) { + v._capacity = capacity; + } + return v; +} + +void __z_svec_copy_inner(void *dst, const void *src, z_element_copy_f copy, size_t num_elements, size_t element_size) { + if (copy == NULL) { + memcpy(dst, src, num_elements * element_size); + } else { + size_t offset = 0; + for (size_t i = 0; i < num_elements; i++) { + copy(dst + offset, src + offset); + offset += element_size; + } + } +} + +void __z_svec_move_inner(void *dst, void *src, z_element_move_f move, size_t num_elements, size_t element_size) { + if (move == NULL) { + memcpy(dst, src, num_elements * element_size); + } else { + size_t offset = 0; + for (size_t i = 0; i < num_elements; i++) { + move(dst + offset, src + offset); + offset += element_size; + } + } +} + +void _z_svec_copy(_z_svec_t *dst, const _z_svec_t *src, z_element_copy_f copy, size_t element_size) { + dst->_capacity = 0; + dst->_len = 0; + dst->_val = z_malloc(sizeof(void *) * src->_capacity); + if (dst->_val != NULL) { + dst->_capacity = src->_capacity; + dst->_len = src->_len; + __z_svec_copy_inner(dst->_val, src->_val, copy, src->_len, element_size); + } +} + +void _z_svec_reset(_z_svec_t *v, z_element_clear_f clear, size_t element_size) { + if (clear != NULL) { + size_t offset = 0; + for (size_t i = 0; i < v->_len; i++) { + clear(v->_val + offset); + } + } + v->_len = 0; +} + +void _z_svec_clear(_z_svec_t *v, z_element_clear_f clear_f, size_t element_size) { + _z_svec_reset(v, clear_f, element_size); + _z_svec_release(v); +} + +void _z_svec_release(_z_svec_t *v) { + z_free(v->_val); + v->_val = NULL; + + v->_capacity = 0; + v->_len = 0; +} + +void _z_svec_free(_z_svec_t **v, z_element_clear_f clear, size_t element_size) { + _z_svec_t *ptr = (_z_svec_t *)*v; + + if (ptr != NULL) { + _z_svec_clear(ptr, clear, element_size); + + z_free(ptr); + *v = NULL; + } +} + +size_t _z_svec_len(const _z_svec_t *v) { return v->_len; } + +_Bool _z_svec_is_empty(const _z_svec_t *v) { return v->_len == 0; } + +bool _z_svec_append(_z_svec_t *v, const void *e, z_element_move_f move, size_t element_size) { + if (v->_len == v->_capacity) { + // Allocate a new vector + size_t _capacity = (v->_capacity << 1) | 0x01; + void *_val = (void *)z_malloc(_capacity * element_size); + if (_val != NULL) { + __z_svec_move_inner(_val, v->_val, move, v->_len, element_size); + // Free the old data + z_free(v->_val); + + // Update the current vector + v->_val = _val; + v->_capacity = _capacity; + memcpy(v->_val + v->_len * element_size, e, element_size); + v->_len = v->_len + 1; + } else { + return false; + } + } else { + memcpy(v->_val + v->_len * element_size, e, element_size); + v->_len = v->_len + 1; + } + return true; +} + +void *_z_svec_get(const _z_svec_t *v, size_t i, size_t element_size) { + assert(i < v->_len); + return v->_val + i * element_size; +} + +void _z_svec_set(_z_svec_t *v, size_t i, void *e, z_element_clear_f clear, size_t element_size) { + assert(i < v->_len); + clear(v->_val + i * element_size); + memcpy(v->_val + i * element_size, e, element_size); +} + +void _z_svec_remove(_z_svec_t *v, size_t pos, z_element_clear_f clear, z_element_move_f move, size_t element_size) { + assert(pos < v->_len); + clear(v->_val + pos * element_size); + __z_svec_move_inner( + v->_val + pos * element_size, v->_val + (pos + 1) * element_size, + move, (v->_len - pos - 1) * element_size, element_size + ); + + v->_len--; +} diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 9ad0b9f2c..e92ea138e 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -120,18 +120,30 @@ uint8_t _z_zint_len(uint64_t v) { } } -int8_t _z_zint64_encode(_z_wbuf_t *wbf, uint64_t v) { +uint8_t _z_zint64_encode_buf(uint8_t* buf, uint64_t v) { uint64_t lv = v; uint8_t len = 0; + size_t start = 0; while ((lv & (uint64_t)~0x7f) != 0) { uint8_t c = (lv & 0x7f) | 0x80; - _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, c)) + buf[start++] = c; len++; lv = lv >> (uint64_t)7; } if (len != VLE_LEN) { uint8_t c = (lv & 0xff); - _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, c)) + buf[start++] = c; + } + + return start; +} + +int8_t _z_zint64_encode(_z_wbuf_t *wbf, uint64_t v) { + uint8_t buf[VLE_LEN]; + size_t len = _z_zint64_encode_buf(buf, v); + + for (size_t i = 0; i < len; i++) { + _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, buf[i])); } return _Z_RES_OK; @@ -141,21 +153,40 @@ int8_t _z_zint16_encode(_z_wbuf_t *wbf, uint16_t v) { return _z_zint64_encode(wb int8_t _z_zint32_encode(_z_wbuf_t *wbf, uint32_t v) { return _z_zint64_encode(wbf, (uint64_t)v); } int8_t _z_zsize_encode(_z_wbuf_t *wbf, _z_zint_t v) { return _z_zint64_encode(wbf, (uint64_t)v); } -int8_t _z_zint64_decode(uint64_t *zint, _z_zbuf_t *zbf) { +int8_t _z_zint64_decode_with_reader(uint64_t *zint, __z_single_byte_reader_t reader, void* context) { *zint = 0; uint8_t b = 0; - _Z_RETURN_IF_ERR(_z_uint8_decode(&b, zbf)); + _Z_RETURN_IF_ERR(reader(&b, context)); uint8_t i = 0; while (((b & 0x80) != 0) && (i != 7 * (VLE_LEN - 1))) { *zint = *zint | ((uint64_t)(b & 0x7f)) << i; - _Z_RETURN_IF_ERR(_z_uint8_decode(&b, zbf)); + _Z_RETURN_IF_ERR(reader(&b, context)); i = i + (uint8_t)7; } *zint = *zint | ((uint64_t)b << i); - return _Z_RES_OK; + return _Z_RES_OK; +} + +int8_t _z_zsize_decode_with_reader(_z_zint_t *zint, __z_single_byte_reader_t reader, void* context) { + uint64_t i = 0; + int8_t res = _z_zint64_decode_with_reader(&i, reader, context); + if (res == _Z_RES_OK && i > SIZE_MAX) { + res = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } else { + *zint = (_z_zint_t)i; + } + return res; +} + +int8_t _z_uint8_decode_reader(uint8_t* zint, void* context) { + return _z_uint8_decode(zint, (_z_zbuf_t*)context); +} + +int8_t _z_zint64_decode(uint64_t *zint, _z_zbuf_t *zbf) { + return _z_zint64_decode_with_reader(zint, _z_uint8_decode_reader, (void*)zbf); } int8_t _z_zint16_decode(uint16_t *zint, _z_zbuf_t *zbf) { @@ -183,15 +214,7 @@ int8_t _z_zint32_decode(uint32_t *zint, _z_zbuf_t *zbf) { } int8_t _z_zsize_decode(_z_zint_t *zint, _z_zbuf_t *zbf) { - int8_t ret = _Z_RES_OK; - uint64_t buf; - _Z_RETURN_IF_ERR(_z_zint64_decode(&buf, zbf)); - if (buf <= SIZE_MAX) { - *zint = (_z_zint_t)buf; - } else { - ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; - } - return ret; + return _z_zsize_decode_with_reader(zint, _z_uint8_decode_reader, (void*)zbf); } /*------------------ uint8_array ------------------*/ diff --git a/tests/z_bytes_test.c b/tests/z_bytes_test.c new file mode 100644 index 000000000..becc057fa --- /dev/null +++ b/tests/z_bytes_test.c @@ -0,0 +1,87 @@ +// +// Copyright (c) 2024 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#include +#include +#include + +#include "zenoh-pico/collections/bytes.h" + +#undef NDEBUG +#include + +void test_null_bytes() { + _zz_bytes_t b = _zz_bytes_null(); + assert(_zz_bytes_len(&b) == 0); + assert(_zz_bytes_is_empty(&b)); + assert(!_zz_bytes_check(&b)); + assert(_zz_bytes_num_slices(&b) == 0); + _zz_bytes_clear(&b); // no crush +} + +void test_slice() { + uint8_t data[5] = {1, 2, 3, 4, 5}; + uint8_t data_out[5] = {}; + _z_slice_t s = _z_slice_wrap_copy(data, 5); + _zz_bytes_t b = _zz_bytes_from_slice(s); + + assert(_zz_bytes_len(&b) == 5); + assert(!_zz_bytes_is_empty(&b)); + assert(_zz_bytes_check(&b)); + assert(_zz_bytes_num_slices(&b) == 1); + assert(_z_slice_eq(&_zz_bytes_get_slice(&b, 0)->slice.in->val, &s)); + + _zz_bytes_to_buf(&b, data_out, 5); + assert(memcmp(data, data_out, 5) == 0); + + _zz_bytes_clear(&b); +} + +void test_append() { + uint8_t data1[5] = {1, 2, 3, 4, 5}; + uint8_t data2[5] = {1, 2, 6, 7, 8}; + uint8_t data3[3] = {3, 9, 10}; + uint8_t data_in[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + uint8_t data_out[10] = {}; + _z_arc_slice_t s1 = _z_arc_slice_wrap(_z_slice_wrap_copy(data1, 5), 0, 5); + _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_wrap_copy(data2, 5), 2, 3); + _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); + + _zz_bytes_t b = _zz_bytes_null(); + + _zz_bytes_append_slice(&b, &s1); + _zz_bytes_append_slice(&b, &s2); + _zz_bytes_append_slice(&b, &s3); + + printf("elements: %zu, ", _zz_bytes_len(&b)); + assert(_zz_bytes_len(&b) == 10); + assert(!_zz_bytes_is_empty(&b)); + assert(_zz_bytes_check(&b)); + assert(_zz_bytes_num_slices(&b) == 3); + assert(_z_slice_eq(&_zz_bytes_get_slice(&b, 0)->slice.in->val, &s1.slice.in->val)); + assert(_z_slice_eq(&_zz_bytes_get_slice(&b, 0)->slice.in->val, &s2.slice.in->val)); + assert(_z_slice_eq(&_zz_bytes_get_slice(&b, 0)->slice.in->val, &s3.slice.in->val)); + + _zz_bytes_to_buf(&b, data_out, 10); + assert(memcmp(data_in, data_out, 10) == 0); + + _zz_bytes_clear(&b); +} + +int main(void) { + test_null_bytes(); + test_slice(); + test_append(); + return 0; +} diff --git a/zenohpico.pc b/zenohpico.pc index 642e15247..129afce02 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.11.20240517dev +Version: 0.11.20240620dev Cflags: -I${prefix}/include Libs: -L${prefix}/lib -lzenohpico From eaff4a2b311d6ebe006dd28e89a8d8cfc0664a9e Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Fri, 21 Jun 2024 22:33:35 +0200 Subject: [PATCH 02/25] bytes tests and fixes --- include/zenoh-pico/collections/bytes.h | 3 +- src/collections/arc_slice.c | 4 +- src/collections/bytes.c | 25 ++++--- src/collections/slice.c | 5 +- src/collections/vec.c | 7 +- tests/z_bytes_test.c | 97 ++++++++++++++++++++++++-- 6 files changed, 117 insertions(+), 24 deletions(-) diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index 286fe22fb..471cbee8a 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -50,13 +50,12 @@ typedef struct { _Bool _zz_bytes_check(const _zz_bytes_t *bytes); _zz_bytes_t _zz_bytes_null(void); -_zz_bytes_t _zz_bytes_make(size_t capacity); int8_t _zz_bytes_append(_zz_bytes_t* dst, _zz_bytes_t* src); int8_t _zz_bytes_append_slice(_zz_bytes_t* dst, _z_arc_slice_t* s); int8_t _zz_bytes_copy(_zz_bytes_t *dst, const _zz_bytes_t *src); _zz_bytes_t _zz_bytes_duplicate(const _zz_bytes_t *src); void _zz_bytes_move(_zz_bytes_t *dst, _zz_bytes_t *src); -void _zz_bytes_clear(_zz_bytes_t *bytes); +void _zz_bytes_drop(_zz_bytes_t *bytes); void _zz_bytes_free(_zz_bytes_t **bs); size_t _zz_bytes_num_slices(const _zz_bytes_t *bs); _z_arc_slice_t* _zz_bytes_get_slice(const _zz_bytes_t *bs, size_t i); diff --git a/src/collections/arc_slice.c b/src/collections/arc_slice.c index 0a5bb0435..ce698bab6 100644 --- a/src/collections/arc_slice.c +++ b/src/collections/arc_slice.c @@ -63,7 +63,7 @@ _Bool _z_arc_slice_is_empty(const _z_arc_slice_t* s) { } const uint8_t* _z_arc_slice_data(const _z_arc_slice_t* s) { - return s->slice.in->val.start; + return s->slice.in->val.start + s->start; } int8_t _z_arc_slice_copy(_z_arc_slice_t *dst, const _z_arc_slice_t *src) { @@ -75,6 +75,8 @@ int8_t _z_arc_slice_copy(_z_arc_slice_t *dst, const _z_arc_slice_t *src) { int8_t _z_arc_slice_move(_z_arc_slice_t *dst, _z_arc_slice_t *src) { dst->slice = src->slice; + dst->len = src->len; + dst->start = src->start; src->len = 0; src->start = 0; src->slice.in = NULL; diff --git a/src/collections/bytes.c b/src/collections/bytes.c index 6845255e2..e6a26fc28 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -50,7 +50,7 @@ _zz_bytes_t _zz_bytes_duplicate(const _zz_bytes_t *src) { size_t _zz_bytes_len(const _zz_bytes_t *bs) { size_t len = 0; - for (size_t i = 0; i < _z_arc_slice_svec_len(&bs->_slices); i++) { + for (size_t i = 0; i < _z_arc_slice_svec_len(&bs->_slices); ++i) { const _z_arc_slice_t *s = _z_arc_slice_svec_get(&bs->_slices, i); len += _z_arc_slice_len(s); } @@ -74,13 +74,13 @@ _z_arc_slice_t* _zz_bytes_get_slice(const _zz_bytes_t *bs, size_t i) { return _z_arc_slice_svec_get(&bs->_slices, i); } -void _zz_bytes_clear(_zz_bytes_t *bytes) { _z_arc_slice_svec_clear(&bytes->_slices); } +void _zz_bytes_drop(_zz_bytes_t *bytes) { _z_arc_slice_svec_clear(&bytes->_slices); } void _zz_bytes_free(_zz_bytes_t **bs) { _zz_bytes_t *ptr = *bs; if (ptr != NULL) { - _zz_bytes_clear(ptr); + _zz_bytes_drop(ptr); z_free(ptr); *bs = NULL; @@ -178,11 +178,11 @@ _zz_bytes_t _zz_bytes_serialize_from_pair(_zz_bytes_t* first, _zz_bytes_t* secon _zz_bytes_t out = _zz_bytes_null(); if (_zz_bytes_append(&out, first) != _Z_RES_OK) { - _zz_bytes_clear(&out); - _zz_bytes_clear(first); + _zz_bytes_drop(&out); + _zz_bytes_drop(first); } else if (_zz_bytes_append(&out, second) != _Z_RES_OK) { - _zz_bytes_clear(&out); - _zz_bytes_clear(second); + _zz_bytes_drop(&out); + _zz_bytes_drop(second); } return out; @@ -195,7 +195,7 @@ int8_t _zz_bytes_deserialize_into_pair(const _zz_bytes_t* bs, _zz_bytes_t* first if (res != _Z_RES_OK) return res; res = _zz_bytes_reader_read_next(&reader, second_out); if (res != _Z_RES_OK) { - _zz_bytes_clear(first_out); + _zz_bytes_drop(first_out); }; return res; } @@ -266,6 +266,7 @@ _zz_bytes_t _zz_bytes_from_double(double val) { _zz_bytes_reader_t _zz_bytes_get_reader(const _zz_bytes_t *bytes) { _zz_bytes_reader_t r; r.bytes = bytes; + r.slice_idx = 0; r.byte_idx = 0; r.in_slice_idx = 0; return r; @@ -299,7 +300,6 @@ int8_t _zz_bytes_reader_seek_backward(_zz_bytes_reader_t *reader, size_t offset) if (reader->slice_idx == 0) return _Z_ERR_DID_NOT_READ; reader->slice_idx--; _z_arc_slice_t* s = _zz_bytes_get_slice(reader->bytes, reader->slice_idx); - s = _zz_bytes_get_slice(reader->bytes, reader->slice_idx); reader->in_slice_idx = _z_arc_slice_len(s); } @@ -308,9 +308,9 @@ int8_t _zz_bytes_reader_seek_backward(_zz_bytes_reader_t *reader, size_t offset) reader->byte_idx -= reader->in_slice_idx; reader->in_slice_idx = 0; } else { - offset = 0; reader->byte_idx -= offset; reader->in_slice_idx -= offset; + offset = 0; } } return _Z_RES_OK; @@ -331,6 +331,9 @@ int8_t _zz_bytes_reader_seek(_zz_bytes_reader_t *reader, int64_t offset, int ori else return _zz_bytes_reader_seek_backward(reader, -offset); } case SEEK_END: { + reader->byte_idx = _zz_bytes_len(reader->bytes); + reader->in_slice_idx = 0; + reader->slice_idx = _zz_bytes_num_slices(reader->bytes); if (offset > 0) return _Z_ERR_DID_NOT_READ; else return _zz_bytes_reader_seek_backward(reader, -offset); } @@ -409,7 +412,7 @@ int8_t _zz_bytes_reader_read_slices(_zz_bytes_reader_t* reader, size_t len, _zz_ } if (len > 0 && res == _Z_RES_OK) res = _Z_ERR_DID_NOT_READ; - if (res != _Z_RES_OK) _zz_bytes_clear(out); + if (res != _Z_RES_OK) _zz_bytes_drop(out); return res; diff --git a/src/collections/slice.c b/src/collections/slice.c index 6de05ddad..1fa0c2472 100644 --- a/src/collections/slice.c +++ b/src/collections/slice.c @@ -26,13 +26,16 @@ _z_slice_t _z_slice_empty(void) { return (_z_slice_t){.start = NULL, .len = 0, . int8_t _z_slice_init(_z_slice_t *bs, size_t capacity) { int8_t ret = _Z_RES_OK; - bs->start = (uint8_t *)z_malloc(capacity); + bs->start = capacity == 0 ? NULL : (uint8_t *)z_malloc(capacity); if (bs->start != NULL) { bs->len = capacity; bs->_is_alloc = true; } else { bs->len = 0; bs->_is_alloc = false; + } + + if (bs->len != capacity) { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; } diff --git a/src/collections/vec.c b/src/collections/vec.c index 92ab772cb..51374a500 100644 --- a/src/collections/vec.c +++ b/src/collections/vec.c @@ -182,6 +182,7 @@ void _z_svec_reset(_z_svec_t *v, z_element_clear_f clear, size_t element_size) { size_t offset = 0; for (size_t i = 0; i < v->_len; i++) { clear(v->_val + offset); + offset += element_size; } } v->_len = 0; @@ -218,7 +219,7 @@ _Bool _z_svec_is_empty(const _z_svec_t *v) { return v->_len == 0; } bool _z_svec_append(_z_svec_t *v, const void *e, z_element_move_f move, size_t element_size) { if (v->_len == v->_capacity) { // Allocate a new vector - size_t _capacity = (v->_capacity << 1) | 0x01; + size_t _capacity = v->_capacity == 0 ? 1 : (v->_capacity << 1); void *_val = (void *)z_malloc(_capacity * element_size); if (_val != NULL) { __z_svec_move_inner(_val, v->_val, move, v->_len, element_size); @@ -229,13 +230,13 @@ bool _z_svec_append(_z_svec_t *v, const void *e, z_element_move_f move, size_t e v->_val = _val; v->_capacity = _capacity; memcpy(v->_val + v->_len * element_size, e, element_size); - v->_len = v->_len + 1; + v->_len++; } else { return false; } } else { memcpy(v->_val + v->_len * element_size, e, element_size); - v->_len = v->_len + 1; + v->_len++; } return true; } diff --git a/tests/z_bytes_test.c b/tests/z_bytes_test.c index becc057fa..f14ba43af 100644 --- a/tests/z_bytes_test.c +++ b/tests/z_bytes_test.c @@ -27,7 +27,7 @@ void test_null_bytes() { assert(_zz_bytes_is_empty(&b)); assert(!_zz_bytes_check(&b)); assert(_zz_bytes_num_slices(&b) == 0); - _zz_bytes_clear(&b); // no crush + _zz_bytes_drop(&b); // no crush } void test_slice() { @@ -45,7 +45,7 @@ void test_slice() { _zz_bytes_to_buf(&b, data_out, 5); assert(memcmp(data, data_out, 5) == 0); - _zz_bytes_clear(&b); + _zz_bytes_drop(&b); } void test_append() { @@ -64,24 +64,109 @@ void test_append() { _zz_bytes_append_slice(&b, &s2); _zz_bytes_append_slice(&b, &s3); - printf("elements: %zu, ", _zz_bytes_len(&b)); assert(_zz_bytes_len(&b) == 10); assert(!_zz_bytes_is_empty(&b)); assert(_zz_bytes_check(&b)); assert(_zz_bytes_num_slices(&b) == 3); assert(_z_slice_eq(&_zz_bytes_get_slice(&b, 0)->slice.in->val, &s1.slice.in->val)); - assert(_z_slice_eq(&_zz_bytes_get_slice(&b, 0)->slice.in->val, &s2.slice.in->val)); - assert(_z_slice_eq(&_zz_bytes_get_slice(&b, 0)->slice.in->val, &s3.slice.in->val)); + assert(_z_slice_eq(&_zz_bytes_get_slice(&b, 1)->slice.in->val, &s2.slice.in->val)); + assert(_z_slice_eq(&_zz_bytes_get_slice(&b, 2)->slice.in->val, &s3.slice.in->val)); _zz_bytes_to_buf(&b, data_out, 10); assert(memcmp(data_in, data_out, 10) == 0); - _zz_bytes_clear(&b); + _zz_bytes_drop(&b); +} + +void test_reader_read() { + uint8_t data1[5] = {1, 2, 3, 4, 5}; + uint8_t data2[5] = {1, 2, 6, 7, 8}; + uint8_t data3[3] = {3, 9, 10}; + uint8_t data_in[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + uint8_t data_out[10] = {}; + _z_arc_slice_t s1 = _z_arc_slice_wrap(_z_slice_wrap_copy(data1, 5), 0, 5); + _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_wrap_copy(data2, 5), 2, 3); + _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); + + _zz_bytes_t b = _zz_bytes_null(); + + _zz_bytes_append_slice(&b, &s1); + _zz_bytes_append_slice(&b, &s2); + _zz_bytes_append_slice(&b, &s3); + + _zz_bytes_reader_t reader = _zz_bytes_get_reader(&b); + + uint8_t out; + assert(_zz_bytes_reader_tell(&reader) == 0); + _zz_bytes_reader_read(&reader, &out, 1); + assert(_zz_bytes_reader_tell(&reader) == 1); + assert(out == 1); + + _zz_bytes_reader_read(&reader, data_out, 3); + assert(_zz_bytes_reader_tell(&reader) == 4); + assert(memcmp(data_out, data_in + 1, 3) == 0); + + _zz_bytes_reader_read(&reader, data_out, 6); + assert(_zz_bytes_reader_tell(&reader) == 10); + assert(memcmp(data_out, data_in + 4, 6) == 0); + + _zz_bytes_drop(&b); +} + +void test_reader_seek() { + uint8_t data1[5] = {1, 2, 3, 4, 5}; + uint8_t data2[5] = {1, 2, 6, 7, 8}; + uint8_t data3[3] = {3, 9, 10}; + _z_arc_slice_t s1 = _z_arc_slice_wrap(_z_slice_wrap_copy(data1, 5), 0, 5); + _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_wrap_copy(data2, 5), 2, 3); + _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); + + _zz_bytes_t b = _zz_bytes_null(); + + _zz_bytes_append_slice(&b, &s1); + _zz_bytes_append_slice(&b, &s2); + _zz_bytes_append_slice(&b, &s3); + + _zz_bytes_reader_t reader = _zz_bytes_get_reader(&b); + + uint8_t out; + assert(_zz_bytes_reader_tell(&reader) == 0); + assert(_zz_bytes_reader_seek(&reader, 3, SEEK_CUR) == 0); + assert(_zz_bytes_reader_tell(&reader) == 3); + assert(_zz_bytes_reader_seek(&reader, 5, SEEK_CUR) == 0); + assert(_zz_bytes_reader_tell(&reader) == 8); + assert(_zz_bytes_reader_seek(&reader, 10, SEEK_CUR) != 0); + + assert(_zz_bytes_reader_seek(&reader, 0, SEEK_SET) == 0); + assert(_zz_bytes_reader_tell(&reader) == 0); + + assert(_zz_bytes_reader_seek(&reader, 3, SEEK_SET) == 0); + assert(_zz_bytes_reader_tell(&reader) == 3); + assert(_zz_bytes_reader_seek(&reader, 8, SEEK_SET) == 0); + assert(_zz_bytes_reader_tell(&reader) == 8); + assert(_zz_bytes_reader_seek(&reader, 20, SEEK_SET) != 0); + assert(_zz_bytes_reader_seek(&reader, -20, SEEK_SET) != 0); + + + assert(_zz_bytes_reader_seek(&reader, 0, SEEK_END) == 0); + assert(_zz_bytes_reader_tell(&reader) == 10); + assert(_zz_bytes_reader_seek(&reader, -3, SEEK_END) == 0); + assert(_zz_bytes_reader_tell(&reader) == 7); + assert(_zz_bytes_reader_seek(&reader, -8, SEEK_END) == 0); + assert(_zz_bytes_reader_tell(&reader) == 2); + assert(_zz_bytes_reader_seek(&reader, -10, SEEK_END) == 0); + assert(_zz_bytes_reader_tell(&reader) == 0); + assert(_zz_bytes_reader_seek(&reader, -20, SEEK_END) != 0); + assert(_zz_bytes_reader_seek(&reader, 5, SEEK_END) != 0); + + _zz_bytes_drop(&b); } int main(void) { test_null_bytes(); test_slice(); test_append(); + test_reader_read(); + test_reader_seek(); return 0; } From 99abc68af75028f328c013133eaca35e52184654 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Mon, 24 Jun 2024 10:39:27 +0200 Subject: [PATCH 03/25] merge --- .github/workflows/build-check.yaml | 2 +- .github/workflows/integration.yaml | 2 +- .github/workflows/multicast.yaml | 2 +- CMakeLists.txt | 73 ++- docs/api.rst | 15 +- examples/arduino/z_pub.ino | 7 +- examples/arduino/z_queryable.ino | 2 +- examples/espidf/z_pub.c | 7 +- examples/espidf/z_queryable.c | 2 +- examples/freertos_plus_tcp/z_pub.c | 6 +- examples/freertos_plus_tcp/z_pub_st.c | 7 +- examples/freertos_plus_tcp/z_put.c | 7 +- examples/freertos_plus_tcp/z_queryable.c | 2 +- examples/mbed/z_pub.cpp | 7 +- examples/mbed/z_queryable.cpp | 2 +- examples/unix/c11/z_get_channel.c | 11 +- examples/unix/c11/z_ping.c | 12 +- examples/unix/c11/z_pong.c | 4 +- examples/unix/c11/z_pub.c | 6 +- examples/unix/c11/z_pub_attachment.c | 12 +- examples/unix/c11/z_pub_st.c | 7 +- examples/unix/c11/z_pub_thr.c | 10 +- examples/unix/c11/z_pull.c | 13 +- examples/unix/c11/z_put.c | 6 +- examples/unix/c11/z_queryable.c | 2 +- examples/unix/c11/z_queryable_attachment.c | 2 +- examples/unix/c11/z_queryable_channel.c | 13 +- examples/unix/c11/z_sub_channel.c | 12 +- examples/unix/c99/z_ping.c | 13 +- examples/unix/c99/z_pong.c | 7 +- examples/unix/c99/z_pub.c | 6 +- examples/unix/c99/z_pub_st.c | 7 +- examples/unix/c99/z_put.c | 6 +- examples/unix/c99/z_queryable.c | 2 +- examples/windows/z_ping.c | 13 +- examples/windows/z_pong.c | 7 +- examples/windows/z_pub.c | 6 +- examples/windows/z_pub_st.c | 7 +- examples/windows/z_put.c | 7 +- examples/windows/z_queryable.c | 2 +- examples/zephyr/z_pub.c | 7 +- examples/zephyr/z_queryable.c | 2 +- include/zenoh-pico/api/handlers.h | 225 ++++--- include/zenoh-pico/api/macros.h | 602 +++++++++++++----- include/zenoh-pico/api/primitives.h | 63 +- include/zenoh-pico/api/types.h | 10 +- include/zenoh-pico/collections/fifo_mt.h | 6 +- include/zenoh-pico/collections/ring_mt.h | 4 +- include/zenoh-pico/config.h | 2 +- include/zenoh-pico/deprecated/platform.h | 88 --- include/zenoh-pico/link/config/bt.h | 2 +- include/zenoh-pico/link/config/serial.h | 2 +- .../zenoh-pico/protocol/definitions/core.h | 2 +- include/zenoh-pico/session/push.h | 2 +- include/zenoh-pico/session/reply.h | 2 +- include/zenoh-pico/session/resource.h | 2 +- include/zenoh-pico/system/link/raweth.h | 4 +- include/zenoh-pico/system/platform.h | 1 - include/zenoh-pico/utils/result.h | 1 + src/api/api.c | 73 ++- src/api/handlers.c | 79 --- src/collections/fifo.c | 2 +- src/collections/fifo_mt.c | 43 +- src/collections/lifo.c | 6 +- src/collections/ring.c | 6 +- src/collections/ring_mt.c | 30 +- src/collections/slice.c | 2 +- src/deprecated/platform.c | 63 -- src/link/config/ws.c | 3 +- src/link/multicast/udp.c | 4 +- src/link/unicast/tcp.c | 4 +- src/link/unicast/udp.c | 6 +- src/net/encoding.c | 2 +- src/net/primitives.c | 4 +- src/net/sample.c | 2 +- src/net/session.c | 4 +- src/protocol/codec.c | 4 +- src/protocol/codec/declarations.c | 2 + src/protocol/codec/message.c | 13 +- src/protocol/codec/network.c | 21 +- src/protocol/codec/transport.c | 6 +- src/protocol/core.c | 6 +- src/protocol/definitions/network.c | 2 +- src/protocol/definitions/transport.c | 12 +- src/protocol/iobuf.c | 12 +- src/protocol/keyexpr.c | 28 +- src/session/push.c | 7 +- src/session/reply.c | 6 + src/session/resource.c | 4 +- src/session/rx.c | 4 +- src/session/subscription.c | 1 + src/session/utils.c | 2 +- src/system/unix/link/raweth.c | 11 +- src/system/unix/network.c | 32 +- src/system/unix/system.c | 42 +- src/transport/multicast/lease.c | 85 +-- src/transport/raweth/link.c | 12 +- src/transport/raweth/rx.c | 6 +- src/transport/raweth/tx.c | 10 +- src/transport/unicast/lease.c | 18 +- src/transport/unicast/rx.c | 2 +- src/utils/encoding.c | 2 +- src/utils/pointers.c | 4 +- src/utils/string.c | 10 +- tests/z_api_alignment_test.c | 63 +- tests/z_channels_test.c | 118 ++-- tests/z_client_test.c | 28 +- tests/z_peer_multicast_test.c | 15 +- tests/z_perf_tx.c | 12 +- tests/z_test_fragment_tx.c | 6 +- 110 files changed, 1334 insertions(+), 968 deletions(-) delete mode 100644 include/zenoh-pico/deprecated/platform.h delete mode 100644 src/api/handlers.c delete mode 100644 src/deprecated/platform.c diff --git a/.github/workflows/build-check.yaml b/.github/workflows/build-check.yaml index 3e1f3d69d..37fab7d0b 100644 --- a/.github/workflows/build-check.yaml +++ b/.github/workflows/build-check.yaml @@ -77,7 +77,7 @@ jobs: uses: eclipse-zenoh/ci/build-crates-standalone@main with: repo: eclipse-zenoh/zenoh - branch: interests + branch: dev/1.0.0 artifact-patterns: | ^zenohd$ ^libzenoh_plugin_rest.so$ diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml index f6a6acb7f..7348f76c0 100644 --- a/.github/workflows/integration.yaml +++ b/.github/workflows/integration.yaml @@ -50,4 +50,4 @@ jobs: BUILD_TESTING: OFF # Workaround for Windows as it seems the previous step is being ignored BUILD_MULTICAST: OFF # Workaround for Windows as it seems the previous step is being ignored BUILD_INTEGRATION: ON # Workaround for Windows as it seems the previous step is being ignored - ZENOH_BRANCH: interests + ZENOH_BRANCH: dev/1.0.0 diff --git a/.github/workflows/multicast.yaml b/.github/workflows/multicast.yaml index 1cd04985c..c3b841fed 100644 --- a/.github/workflows/multicast.yaml +++ b/.github/workflows/multicast.yaml @@ -40,7 +40,7 @@ jobs: BUILD_TESTING: OFF BUILD_MULTICAST: ON BUILD_INTEGRATION: OFF - ZENOH_BRANCH: interests + ZENOH_BRANCH: dev/1.0.0 - name: Test debug run: make test diff --git a/CMakeLists.txt b/CMakeLists.txt index e873b396d..cba60c662 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,42 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") endif() endif() +# Language options +if(NOT CMAKE_C_STANDARD) + if(c_std_11 IN_LIST CMAKE_C_COMPILE_FEATURES) + set(CMAKE_C_STANDARD 11) + message(STATUS "Setting C11 as the C Standard") + else() + # C99 pedantic doesn't like unix header anonymous structure + set(CMAKE_C_STANDARD 99) + message(STATUS "Setting C99 as the C Standard") + endif() +endif() +set(CMAKE_C_STANDARD_REQUIRED TRUE) + +# Compile options +if(CMAKE_BUILD_TYPE MATCHES "RELEASE" OR "Release") + if(UNIX) + add_compile_options(-pipe -O3) + elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") + add_compile_options(-pipe -O3) + endif() +else() + if(UNIX) + add_compile_options(-c -Wall -Wextra -Werror -Wshadow -Wunused -Wstrict-prototypes -pipe -g -O0) + # C99 pedantic doesn't like struct anonymous in unix header + if (NOT CMAKE_C_STANDARD STREQUAL "99") + add_compile_options(-Wpedantic) + endif() + # add_compile_options(-Wconversion) + # add_link_options(-fsanitize=address) + elseif(MSVC) + add_compile_options(/W4 /WX /Od /wd4127) + elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") + add_compile_options(-Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes -pipe -g -O0) + endif() +endif() + set(Libname "zenohpico") if(BUILD_SHARED_LIBS) add_library(${Libname} SHARED) @@ -50,16 +86,6 @@ function(add_definition value) target_compile_definitions(${Libname} PUBLIC ${value}) endfunction() -if(NOT CMAKE_C_STANDARD) - if(c_std_11 IN_LIST CMAKE_C_COMPILE_FEATURES) - set(CMAKE_C_STANDARD 11) - message(STATUS "Setting C11 as the C Standard") - else() - set(CMAKE_C_STANDARD 99) - message(STATUS "Setting C99 as the C Standard") - endif() -endif() -set(CMAKE_C_STANDARD_REQUIRED TRUE) add_definition(ZENOH_C_STANDARD=${CMAKE_C_STANDARD}) # while in development, use timestamp for patch version: @@ -175,31 +201,11 @@ if(CHECK_THREADS) find_package(Threads REQUIRED) endif() -if(CMAKE_BUILD_TYPE MATCHES "DEBUG") - if(UNIX) - # FIXME: seems to not apply to library files, Issue #422 - add_compile_options(-c -Wall -Wextra -Werror -Wshadow -Wpedantic -Wunused -Wstrict-prototypes -pipe -g -O0) - # add_compile_options(-Wconversion) - # add_link_options(-fsanitize=address) - elseif(MSVC) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /std:c11 /experimental:c11atomics") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest /experimental:c11atomics") - add_compile_options(/W4 /WX /Od /wd4127) - elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") - add_compile_options(-Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes -pipe -g -O0) - endif() -elseif(CMAKE_BUILD_TYPE MATCHES "RELEASE") - if(UNIX) - add_compile_options(-pipe -O3) - elseif(MSVC) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /std:c11 /experimental:c11atomics") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest /experimental:c11atomics") - elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") - add_compile_options(-pipe -O3) - endif() +if(MSVC) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /std:c11 /experimental:c11atomics") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++latest /experimental:c11atomics") endif() - file(GLOB_RECURSE PublicHeaders "include/zenoh-pico/api/*.h" "include/zenoh-pico/collections/*.h" @@ -216,7 +222,6 @@ target_include_directories(${Libname} PUBLIC ${PROJECT_SOURCE_DIR}/include) file(GLOB_RECURSE Sources "src/api/*.c" "src/collections/*.c" - "src/deprecated/*.c" "src/link/*.c" "src/net/*.c" "src/protocol/*.c" diff --git a/docs/api.rst b/docs/api.rst index 60c969bd5..b0a076abf 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -109,9 +109,9 @@ TODO: owned type description Represents the encoding of a payload, in a MIME-like format. -.. c:type:: z_owned_value_t +.. c:type:: z_owned_reply_err_t - Represents a Zenoh value. + Represents a Zenoh reply error value. .. c:type:: z_owned_sample_t @@ -182,9 +182,9 @@ TODO: loaned type description Represents the encoding of a payload, in a MIME-like format. -.. c:type:: z_loaned_value_t +.. c:type:: z_loaned_reply_err_t - Represents a Zenoh value. + Represents a Zenoh reply error. .. c:type:: z_loaned_sample_t @@ -287,9 +287,11 @@ Primitives .. autocfunction:: primitives.h::z_encoding_check .. autocfunction:: primitives.h::z_encoding_drop .. autocfunction:: primitives.h::z_encoding_loan +.. autocfunction:: primitives.h::z_encoding_loan_mut .. autocfunction:: primitives.h::z_encoding_move .. autocfunction:: primitives.h::z_encoding_null -.. autocfunction:: primitives.h::z_value_payload +.. autocfunction:: primitives.h::z_reply_err_payload +.. autocfunction:: primitives.h::z_reply_err_encoding .. autocfunction:: primitives.h::z_slice_data .. autocfunction:: primitives.h::z_slice_len .. autocfunction:: primitives.h::z_bytes_deserialize_into_int8 @@ -326,7 +328,8 @@ Primitives .. autocfunction:: primitives.h::z_query_consolidation_monotonic .. autocfunction:: primitives.h::z_query_consolidation_none .. autocfunction:: primitives.h::z_query_parameters -.. autocfunction:: primitives.h::z_query_value +.. autocfunction:: primitives.h::z_query_payload +.. autocfunction:: primitives.h::z_query_encoding .. autocfunction:: primitives.h::z_query_attachment .. autocfunction:: primitives.h::z_query_keyexpr .. autocfunction:: primitives.h::z_closure_sample diff --git a/examples/arduino/z_pub.ino b/examples/arduino/z_pub.ino index df03b0bcf..c487c976b 100644 --- a/examples/arduino/z_pub.ino +++ b/examples/arduino/z_pub.ino @@ -99,13 +99,18 @@ void loop() { delay(1000); char buf[256]; sprintf(buf, "[%4d] %s", idx++, VALUE); + Serial.print("Writing Data ('"); Serial.print(KEYEXPR); Serial.print("': '"); Serial.print(buf); Serial.println("')"); - if (z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), NULL) < 0) { + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + if (z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL) < 0) { Serial.println("Error while publishing data"); } } diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index f3d8161c6..b9086e8fc 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -44,7 +44,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { // Process value z_owned_string_t payload_string; - z_bytes_deserialize_into_string(z_value_payload(z_query_value(query)), &payload_string); + z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); if (z_string_len(z_string_loan(&payload_string)) > 1) { Serial.print(" with value '"); Serial.print(z_string_data(z_string_loan(&payload_string))); diff --git a/examples/espidf/z_pub.c b/examples/espidf/z_pub.c index 53cb2be99..5d1407239 100644 --- a/examples/espidf/z_pub.c +++ b/examples/espidf/z_pub.c @@ -154,7 +154,12 @@ void app_main() { sleep(1); sprintf(buf, "[%4d] %s", idx, VALUE); printf("Putting Data ('%s': '%s')...\n", KEYEXPR, buf); - z_publisher_put(z_loan(pub), (const uint8_t*)buf, strlen(buf), NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); } printf("Closing Zenoh Session..."); diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index 6d08dc624..bd16db8e0 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -111,7 +111,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_loan(params)->val); // Process value z_owned_string_t payload_string; - z_bytes_deserialize_into_string(z_value_payload(z_query_value(query)), &payload_string); + z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); if (z_string_len(z_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } diff --git a/examples/freertos_plus_tcp/z_pub.c b/examples/freertos_plus_tcp/z_pub.c index f19ddc23e..e86ce07bd 100644 --- a/examples/freertos_plus_tcp/z_pub.c +++ b/examples/freertos_plus_tcp/z_pub.c @@ -97,9 +97,13 @@ void app_main(void) { snprintf(buf, 256, "[%4d] %s", idx, VALUE); printf("Putting Data ('%s': '%s')...\n", KEYEXPR, buf); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + z_publisher_put_options_t options; z_publisher_put_options_default(&options); - z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); + z_publisher_put(z_loan(pub), z_move(payload), &options); } // Clean-up diff --git a/examples/freertos_plus_tcp/z_pub_st.c b/examples/freertos_plus_tcp/z_pub_st.c index bfdd22692..6239171b7 100644 --- a/examples/freertos_plus_tcp/z_pub_st.c +++ b/examples/freertos_plus_tcp/z_pub_st.c @@ -62,7 +62,12 @@ void app_main(void) { if (z_clock_elapsed_ms(&now) > 1000) { snprintf(buf, 256, "[%4d] %s", idx, VALUE); printf("Putting Data ('%s': '%s')...\n", KEYEXPR, buf); - z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); ++idx; now = z_clock_now(); diff --git a/examples/freertos_plus_tcp/z_put.c b/examples/freertos_plus_tcp/z_put.c index 092ed503b..192b441bf 100644 --- a/examples/freertos_plus_tcp/z_put.c +++ b/examples/freertos_plus_tcp/z_put.c @@ -66,7 +66,12 @@ void app_main(void) { printf("Putting Data ('%s': '%s')...\n", KEYEXPR, VALUE); z_put_options_t options; z_put_options_default(&options); - if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)VALUE, strlen(VALUE), &options) < 0) { + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, VALUE); + + if (z_put(z_loan(s), z_loan(ke), z_move(payload), &options) < 0) { printf("Oh no! Put has failed...\n"); } diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index 9110eb380..185cabdc5 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -39,7 +39,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_loan(params)->val); // Process value z_owned_string_t payload_string; - z_bytes_deserialize_into_string(z_value_payload(z_query_value(query)), &payload_string); + z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); if (z_string_len(z_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } diff --git a/examples/mbed/z_pub.cpp b/examples/mbed/z_pub.cpp index 08bfd624b..c709e584a 100644 --- a/examples/mbed/z_pub.cpp +++ b/examples/mbed/z_pub.cpp @@ -74,7 +74,12 @@ int main(int argc, char **argv) { z_sleep_s(1); sprintf(buf, "[%4d] %s", idx, VALUE); printf("Putting Data ('%s': '%s')...\n", KEYEXPR, buf); - z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL); } printf("Closing Zenoh Session..."); diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index d00cee73d..8262829cf 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -41,7 +41,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (int)z_view_string_loan(&pred)->len, z_view_string_loan(&pred)->val); // Process value z_owned_string_t payload_string; - z_bytes_deserialize_into_string(z_value_payload(z_query_value(query)), &payload_string); + z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); if (z_string_len(z_string_loan(&payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_string_loan(&payload_string))); } diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index b7cdff16d..f66c59dc5 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -96,16 +96,17 @@ int main(int argc, char **argv) { z_bytes_serialize_from_string(&payload, value); opts.payload = &payload; } - z_owned_reply_ring_channel_t channel; - z_reply_ring_channel_new(&channel, 1); - if (z_get(z_loan(s), z_loan(ke), "", z_move(channel.send), &opts) < 0) { + z_owned_closure_reply_t closure; + z_owned_ring_handler_reply_t handler; + z_ring_channel_reply_new(&closure, &handler, 1); + if (z_get(z_loan(s), z_loan(ke), "", z_move(closure), &opts) < 0) { printf("Unable to send query.\n"); return -1; } z_owned_reply_t reply; z_null(&reply); - for (z_call(channel.recv, &reply); z_check(reply); z_call(channel.recv, &reply)) { + for (z_recv(z_loan(handler), &reply); z_check(reply); z_recv(z_loan(handler), &reply)) { if (z_reply_is_ok(z_loan(reply))) { const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply)); z_owned_string_t keystr; @@ -121,7 +122,7 @@ int main(int argc, char **argv) { } } - z_drop(z_move(channel)); + z_drop(z_move(handler)); // Stop read and lease tasks for zenoh-pico zp_stop_read_task(z_loan_mut(s)); diff --git a/examples/unix/c11/z_ping.c b/examples/unix/c11/z_ping.c index fa7744d64..f12e3b44f 100644 --- a/examples/unix/c11/z_ping.c +++ b/examples/unix/c11/z_ping.c @@ -106,7 +106,11 @@ int main(int argc, char** argv) { z_clock_t warmup_start = z_clock_now(); unsigned long elapsed_us = 0; while (elapsed_us < args.warmup_ms * 1000) { - z_publisher_put(z_loan(pub), data, args.size, NULL); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, args.size); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); z_condvar_wait(&cond, &mutex); elapsed_us = z_clock_elapsed_us(&warmup_start); } @@ -114,7 +118,11 @@ int main(int argc, char** argv) { unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings); for (unsigned int i = 0; i < args.number_of_pings; i++) { z_clock_t measure_start = z_clock_now(); - z_publisher_put(z_loan(pub), data, args.size, NULL); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, args.size); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); z_condvar_wait(&cond, &mutex); results[i] = z_clock_elapsed_us(&measure_start); } diff --git a/examples/unix/c11/z_pong.c b/examples/unix/c11/z_pong.c index c7e56e5d0..838da05d6 100644 --- a/examples/unix/c11/z_pong.c +++ b/examples/unix/c11/z_pong.c @@ -20,7 +20,9 @@ void callback(const z_loaned_sample_t* sample, void* context) { const z_loaned_publisher_t* pub = z_loan(*(z_owned_publisher_t*)context); z_owned_slice_t value; z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); - z_publisher_put(pub, z_slice_data(z_loan(value)), z_slice_len(z_loan(value)), NULL); + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, z_slice_data(z_loan(value)), z_slice_len(z_loan(value))); + z_publisher_put(pub, z_move(payload), NULL); z_drop(z_move(value)); } void drop(void* context) { diff --git a/examples/unix/c11/z_pub.c b/examples/unix/c11/z_pub.c index 576edf915..f65f7a172 100644 --- a/examples/unix/c11/z_pub.c +++ b/examples/unix/c11/z_pub.c @@ -112,6 +112,10 @@ int main(int argc, char **argv) { sprintf(buf, "[%4d] %s", idx, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + // Create encoding z_owned_encoding_t encoding; zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, "utf8"); @@ -119,7 +123,7 @@ int main(int argc, char **argv) { z_publisher_put_options_default(&options); options.encoding = z_move(encoding); - z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); + z_publisher_put(z_loan(pub), z_move(payload), &options); } // Clean up z_undeclare_publisher(z_move(pub)); diff --git a/examples/unix/c11/z_pub_attachment.c b/examples/unix/c11/z_pub_attachment.c index 5db64ca4e..2376ffd7f 100644 --- a/examples/unix/c11/z_pub_attachment.c +++ b/examples/unix/c11/z_pub_attachment.c @@ -151,13 +151,19 @@ int main(int argc, char **argv) { z_owned_bytes_t attachment; // Allocate buffer - char buf[256]; char buf_ind[16]; // Publish data printf("Press CTRL-C to quit...\n"); + char buf[256]; for (int idx = 0; idx < n; ++idx) { z_sleep_s(1); + sprintf(buf, "[%4d] %s", idx, value); + printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); // Add attachment value sprintf(buf_ind, "%d", idx); @@ -166,9 +172,7 @@ int main(int argc, char **argv) { zp_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx, kv_pairs_size(&ctx)); options.attachment = z_move(attachment); - sprintf(buf, "[%4d] %s", idx, value); - printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); - z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); + z_publisher_put(z_loan(pub), z_move(payload), &options); } // Clean up z_undeclare_publisher(z_move(pub)); diff --git a/examples/unix/c11/z_pub_st.c b/examples/unix/c11/z_pub_st.c index 51037f4a4..0c6fbe697 100644 --- a/examples/unix/c11/z_pub_st.c +++ b/examples/unix/c11/z_pub_st.c @@ -95,7 +95,12 @@ int main(int argc, char **argv) { sleep(1); sprintf(buf, "[%4d] %s", idx, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); - z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); zp_read(z_loan(s), NULL); zp_send_keep_alive(z_loan(s), NULL); diff --git a/examples/unix/c11/z_pub_thr.c b/examples/unix/c11/z_pub_thr.c index 228e70f01..a31911ee7 100644 --- a/examples/unix/c11/z_pub_thr.c +++ b/examples/unix/c11/z_pub_thr.c @@ -26,7 +26,7 @@ int main(int argc, char **argv) { } char *keyexpr = "test/thr"; size_t len = (size_t)atoi(argv[1]); - uint8_t *value = (uint8_t *)malloc(len); + uint8_t *value = (uint8_t *)z_malloc(len); memset(value, 1, len); // Set config @@ -61,14 +61,18 @@ int main(int argc, char **argv) { // Send packets while (1) { - z_publisher_put(z_loan(pub), (const uint8_t *)value, len, NULL); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, (char *)value); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); } // Clean up z_undeclare_publisher(z_move(pub)); zp_stop_read_task(z_loan_mut(s)); zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); - free(value); + z_free(value); exit(0); } #else diff --git a/examples/unix/c11/z_pull.c b/examples/unix/c11/z_pull.c index b301d6222..4c8083b29 100644 --- a/examples/unix/c11/z_pull.c +++ b/examples/unix/c11/z_pull.c @@ -73,12 +73,13 @@ int main(int argc, char **argv) { } printf("Declaring Subscriber on '%s'...\n", keyexpr); - z_owned_sample_ring_channel_t channel; - z_sample_ring_channel_new(&channel, size); + z_owned_closure_sample_t closure; + z_owned_ring_handler_sample_t handler; + z_ring_channel_sample_new(&closure, &handler, size); z_owned_subscriber_t sub; z_view_keyexpr_t ke; z_view_keyexpr_from_string(&ke, keyexpr); - if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(channel.send), NULL) < 0) { + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(closure), NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } @@ -87,7 +88,7 @@ int main(int argc, char **argv) { z_owned_sample_t sample; z_null(&sample); while (true) { - for (z_call(channel.try_recv, &sample); z_check(sample); z_call(channel.try_recv, &sample)) { + for (z_try_recv(z_loan(handler), &sample); z_check(sample); z_try_recv(z_loan(handler), &sample)) { z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr); z_owned_string_t value; @@ -99,11 +100,11 @@ int main(int argc, char **argv) { z_drop(z_move(sample)); } printf(">> [Subscriber] Nothing to pull... sleep for %zu ms\n", interval); - zp_sleep_ms(interval); + z_sleep_ms(interval); } z_undeclare_subscriber(z_move(sub)); - z_drop(z_move(channel)); + z_drop(z_move(handler)); // Stop read and lease tasks for zenoh-pico zp_stop_read_task(z_loan_mut(s)); diff --git a/examples/unix/c11/z_put.c b/examples/unix/c11/z_put.c index de8b39176..90efbb848 100644 --- a/examples/unix/c11/z_put.c +++ b/examples/unix/c11/z_put.c @@ -97,6 +97,10 @@ int main(int argc, char **argv) { return -1; } + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, value); + // Create encoding z_owned_encoding_t encoding; zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); @@ -105,7 +109,7 @@ int main(int argc, char **argv) { z_put_options_t options; z_put_options_default(&options); options.encoding = z_move(encoding); - if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)value, strlen(value), &options) < 0) { + if (z_put(z_loan(s), z_loan(ke), z_move(payload), &options) < 0) { printf("Oh no! Put has failed...\n"); } // Clean up diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index 3e7a273f9..394b6cd0e 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -33,7 +33,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_loan(params)->val); // Process value z_owned_string_t payload_string; - z_bytes_deserialize_into_string(z_value_payload(z_query_value(query)), &payload_string); + z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); if (z_string_len(z_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } diff --git a/examples/unix/c11/z_queryable_attachment.c b/examples/unix/c11/z_queryable_attachment.c index a000a58c0..ad3d7f693 100644 --- a/examples/unix/c11/z_queryable_attachment.c +++ b/examples/unix/c11/z_queryable_attachment.c @@ -111,7 +111,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_loan(params)->val); // Process value z_owned_string_t payload_string; - z_bytes_deserialize_into_string(z_value_payload(z_query_value(query)), &payload_string); + z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); if (z_string_len(z_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } diff --git a/examples/unix/c11/z_queryable_channel.c b/examples/unix/c11/z_queryable_channel.c index 181e32661..ed81b42b5 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -88,17 +88,18 @@ int main(int argc, char **argv) { } printf("Creating Queryable on '%s'...\n", keyexpr); - z_owned_query_ring_channel_t channel; - z_query_ring_channel_new(&channel, 10); + z_owned_closure_query_t closure; + z_owned_ring_handler_query_t handler; + z_ring_channel_query_new(&closure, &handler, 10); z_owned_queryable_t qable; - if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(channel.send), NULL) < 0) { + if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(closure), NULL) < 0) { printf("Unable to create queryable.\n"); return -1; } z_owned_query_t query; z_null(&query); - for (z_call(channel.recv, &query); z_check(query); z_call(channel.recv, &query)) { + for (z_recv(z_loan(handler), &query); z_check(query); z_recv(z_loan(handler), &query)) { const z_loaned_query_t *q = z_loan(query); z_owned_string_t keystr; z_keyexpr_to_string(z_query_keyexpr(q), &keystr); @@ -108,7 +109,7 @@ int main(int argc, char **argv) { (int)z_loan(params)->len, z_loan(params)->val); // Process value z_owned_string_t payload_string; - z_bytes_deserialize_into_string(z_value_payload(z_query_value(z_loan(query))), &payload_string); + z_bytes_deserialize_into_string(z_query_payload(z_loan(query)), &payload_string); if (z_string_len(z_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } @@ -125,7 +126,7 @@ int main(int argc, char **argv) { z_drop(z_move(query)); } - z_drop(z_move(channel)); + z_drop(z_move(handler)); z_undeclare_queryable(z_move(qable)); // Stop read and lease tasks for zenoh-pico diff --git a/examples/unix/c11/z_sub_channel.c b/examples/unix/c11/z_sub_channel.c index 8a00df929..375c66c2c 100644 --- a/examples/unix/c11/z_sub_channel.c +++ b/examples/unix/c11/z_sub_channel.c @@ -65,19 +65,21 @@ int main(int argc, char **argv) { } printf("Declaring Subscriber on '%s'...\n", keyexpr); - z_owned_sample_fifo_channel_t channel; - z_sample_fifo_channel_new(&channel, 3); + z_owned_closure_sample_t closure; + z_owned_fifo_handler_sample_t handler; + z_fifo_channel_sample_new(&closure, &handler, 3); + z_owned_subscriber_t sub; z_view_keyexpr_t ke; z_view_keyexpr_from_string(&ke, keyexpr); - if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(channel.send), NULL) < 0) { + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(closure), NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } z_owned_sample_t sample; z_null(&sample); - for (z_call(channel.recv, &sample); z_check(sample); z_call(channel.recv, &sample)) { + for (z_recv(z_loan(handler), &sample); z_check(sample); z_recv(z_loan(handler), &sample)) { z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr); z_owned_string_t value; @@ -90,7 +92,7 @@ int main(int argc, char **argv) { } z_undeclare_subscriber(z_move(sub)); - z_drop(z_move(channel)); + z_drop(z_move(handler)); // Stop read and lease tasks for zenoh-pico zp_stop_read_task(z_loan_mut(s)); diff --git a/examples/unix/c99/z_ping.c b/examples/unix/c99/z_ping.c index 17fe7833c..d74a5b988 100644 --- a/examples/unix/c99/z_ping.c +++ b/examples/unix/c99/z_ping.c @@ -109,7 +109,11 @@ int main(int argc, char** argv) { z_clock_t warmup_start = z_clock_now(); unsigned long elapsed_us = 0; while (elapsed_us < args.warmup_ms * 1000) { - z_publisher_put(z_publisher_loan(&pub), data, args.size, NULL); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, args.size); + + z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL); z_condvar_wait(&cond, &mutex); elapsed_us = z_clock_elapsed_us(&warmup_start); } @@ -117,7 +121,12 @@ int main(int argc, char** argv) { unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings); for (unsigned int i = 0; i < args.number_of_pings; i++) { z_clock_t measure_start = z_clock_now(); - z_publisher_put(z_publisher_loan(&pub), data, args.size, NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, args.size); + + z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL); z_condvar_wait(&cond, &mutex); results[i] = z_clock_elapsed_us(&measure_start); } diff --git a/examples/unix/c99/z_pong.c b/examples/unix/c99/z_pong.c index f5060147f..7ec6528f9 100644 --- a/examples/unix/c99/z_pong.c +++ b/examples/unix/c99/z_pong.c @@ -21,7 +21,12 @@ void callback(const z_loaned_sample_t* sample, void* context) { const z_loaned_publisher_t* pub = z_publisher_loan((z_owned_publisher_t*)context); z_owned_slice_t value; z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); - z_publisher_put(pub, z_slice_data(z_slice_loan(&value)), z_slice_len(z_slice_loan(&value)), NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, z_slice_data(z_slice_loan(&value)), z_slice_len(z_slice_loan(&value))); + + z_publisher_put(pub, z_bytes_move(&payload), NULL); z_slice_drop(z_slice_move(&value)); } void drop(void* context) { diff --git a/examples/unix/c99/z_pub.c b/examples/unix/c99/z_pub.c index 91e5799c2..5395865c3 100644 --- a/examples/unix/c99/z_pub.c +++ b/examples/unix/c99/z_pub.c @@ -102,6 +102,10 @@ int main(int argc, char **argv) { snprintf(buf, 256, "[%4d] %s", idx, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + // Create encoding z_owned_encoding_t encoding; zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); @@ -109,7 +113,7 @@ int main(int argc, char **argv) { z_publisher_put_options_default(&options); options.encoding = z_encoding_move(&encoding); - z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), &options); + z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), &options); } // Clean up z_undeclare_publisher(z_publisher_move(&pub)); diff --git a/examples/unix/c99/z_pub_st.c b/examples/unix/c99/z_pub_st.c index d5d8220d8..22fc17e79 100644 --- a/examples/unix/c99/z_pub_st.c +++ b/examples/unix/c99/z_pub_st.c @@ -95,7 +95,12 @@ int main(int argc, char **argv) { if (z_clock_elapsed_ms(&now) > 1000) { snprintf(buf, 256, "[%4d] %s", idx, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); - z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL); ++idx; now = z_clock_now(); diff --git a/examples/unix/c99/z_put.c b/examples/unix/c99/z_put.c index 701432fa6..dff0186e1 100644 --- a/examples/unix/c99/z_put.c +++ b/examples/unix/c99/z_put.c @@ -93,6 +93,10 @@ int main(int argc, char **argv) { return -1; } + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, value); + // Create encoding z_owned_encoding_t encoding; zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); @@ -101,7 +105,7 @@ int main(int argc, char **argv) { z_put_options_t options; z_put_options_default(&options); options.encoding = z_encoding_move(&encoding); - if (z_put(z_session_loan(&s), z_keyexpr_loan(&ke), (const uint8_t *)value, strlen(value), &options) < 0) { + if (z_put(z_session_loan(&s), z_keyexpr_loan(&ke), z_bytes_move(&payload), &options) < 0) { printf("Oh no! Put has failed...\n"); } diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index 88e2ae92b..22f1e4cd3 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -32,7 +32,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (int)z_view_string_loan(¶ms)->len, z_view_string_loan(¶ms)->val); // Process value z_owned_string_t payload_string; - z_bytes_deserialize_into_string(z_value_payload(z_query_value(query)), &payload_string); + z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); if (z_string_len(z_string_loan(&payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_string_loan(&payload_string))); } diff --git a/examples/windows/z_ping.c b/examples/windows/z_ping.c index 8dfd075ca..e45f7cdc6 100644 --- a/examples/windows/z_ping.c +++ b/examples/windows/z_ping.c @@ -105,7 +105,11 @@ int main(int argc, char** argv) { z_clock_t warmup_start = z_clock_now(); unsigned long elapsed_us = 0; while (elapsed_us < args.warmup_ms * 1000) { - z_publisher_put(z_loan(pub), data, args.size, NULL); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, args.size); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); z_condvar_wait(&cond, &mutex); elapsed_us = z_clock_elapsed_us(&warmup_start); } @@ -113,7 +117,12 @@ int main(int argc, char** argv) { unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings); for (unsigned int i = 0; i < args.number_of_pings; i++) { z_clock_t measure_start = z_clock_now(); - z_publisher_put(z_loan(pub), data, args.size, NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, args.size); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); z_condvar_wait(&cond, &mutex); results[i] = z_clock_elapsed_us(&measure_start); } diff --git a/examples/windows/z_pong.c b/examples/windows/z_pong.c index c7e56e5d0..dd4c23858 100644 --- a/examples/windows/z_pong.c +++ b/examples/windows/z_pong.c @@ -20,7 +20,12 @@ void callback(const z_loaned_sample_t* sample, void* context) { const z_loaned_publisher_t* pub = z_loan(*(z_owned_publisher_t*)context); z_owned_slice_t value; z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); - z_publisher_put(pub, z_slice_data(z_loan(value)), z_slice_len(z_loan(value)), NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, z_slice_data(z_loan(value)), z_slice_len(z_loan(value))); + + z_publisher_put(pub, z_move(payload), NULL); z_drop(z_move(value)); } void drop(void* context) { diff --git a/examples/windows/z_pub.c b/examples/windows/z_pub.c index 231edeb66..13e748a22 100644 --- a/examples/windows/z_pub.c +++ b/examples/windows/z_pub.c @@ -64,6 +64,10 @@ int main(int argc, char **argv) { snprintf(buf, 256, "[%4d] %s", idx, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + // Create encoding z_owned_encoding_t encoding; zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); @@ -71,7 +75,7 @@ int main(int argc, char **argv) { z_publisher_put_options_default(&options); options.encoding = z_move(encoding); - z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); + z_publisher_put(z_loan(pub), z_move(payload), &options); } // Clean-up diff --git a/examples/windows/z_pub_st.c b/examples/windows/z_pub_st.c index 959fb2b6f..75c19e6bd 100644 --- a/examples/windows/z_pub_st.c +++ b/examples/windows/z_pub_st.c @@ -59,7 +59,12 @@ int main(int argc, char **argv) { if (z_clock_elapsed_ms(&now) > 1000) { snprintf(buf, 256, "[%4d] %s", idx, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); - z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); ++idx; now = z_clock_now(); diff --git a/examples/windows/z_put.c b/examples/windows/z_put.c index e3c9b5982..4b3a76dda 100644 --- a/examples/windows/z_put.c +++ b/examples/windows/z_put.c @@ -59,6 +59,11 @@ int main(int argc, char **argv) { z_close(z_move(s)); return -1; } + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, value); + // Create encoding z_owned_encoding_t encoding; zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); @@ -67,7 +72,7 @@ int main(int argc, char **argv) { z_put_options_t options; z_put_options_default(&options); options.encoding = z_move(encoding); - if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)value, strlen(value), &options) < 0) { + if (z_put(z_loan(s), z_loan(ke), z_move(payload), &options) < 0) { printf("Oh no! Put has failed...\n"); } diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index 98d84b108..294afd37e 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -32,7 +32,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_loan(params)->val); // Process value z_owned_string_t payload_string; - z_bytes_deserialize_into_string(z_value_payload(z_query_value(query)), &payload_string); + z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); if (z_string_len(z_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } diff --git a/examples/zephyr/z_pub.c b/examples/zephyr/z_pub.c index 209f91dc5..441342e7a 100644 --- a/examples/zephyr/z_pub.c +++ b/examples/zephyr/z_pub.c @@ -71,7 +71,12 @@ int main(int argc, char **argv) { sleep(1); sprintf(buf, "[%4d] %s", idx, VALUE); printf("Putting Data ('%s': '%s')...\n", KEYEXPR, buf); - z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); } printf("Closing Zenoh Session..."); diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index 679493eb7..89cccb54a 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -41,7 +41,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_loan(params)->val); // Process value z_owned_string_t payload_string; - z_bytes_deserialize_into_string(z_value_payload(z_query_value(query)), &payload_string); + z_bytes_deserialize_into_string(z_query_payload(query), &payload_string); if (z_string_len(z_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); } diff --git a/include/zenoh-pico/api/handlers.h b/include/zenoh-pico/api/handlers.h index 99d4d15b9..473fa2477 100644 --- a/include/zenoh-pico/api/handlers.h +++ b/include/zenoh-pico/api/handlers.h @@ -23,102 +23,149 @@ #include "zenoh-pico/collections/ring_mt.h" #include "zenoh-pico/utils/logging.h" -// -- Samples handler -void _z_owned_sample_move(z_owned_sample_t *dst, z_owned_sample_t *src); -z_owned_sample_t *_z_sample_to_owned_ptr(const z_loaned_sample_t *src); - -// -- Queries handler -void _z_owned_query_move(z_owned_query_t *dst, z_owned_query_t *src); -z_owned_query_t *_z_query_to_owned_ptr(const z_loaned_query_t *src); - -// -- Reply handler -void _z_owned_reply_move(z_owned_reply_t *dst, z_owned_reply_t *src); -z_owned_reply_t *_z_reply_to_owned_ptr(const z_loaned_reply_t *src); - // -- Channel -#define _Z_CHANNEL_DEFINE(name, send_closure_name, recv_closure_name, send_type, recv_type, collection_type, \ - collection_new_f, collection_free_f, collection_push_f, collection_pull_f, \ - collection_try_pull_f, elem_move_f, elem_convert_f, elem_drop_f) \ - typedef struct { \ - z_owned_##send_closure_name##_t send; \ - z_owned_##recv_closure_name##_t recv; \ - z_owned_##recv_closure_name##_t try_recv; \ - collection_type *collection; \ - } z_owned_##name##_t; \ - \ - static inline void _z_##name##_elem_free(void **elem) { \ - elem_drop_f((recv_type *)*elem); \ - zp_free(*elem); \ - *elem = NULL; \ - } \ - static inline void _z_##name##_elem_move(void *dst, void *src) { \ - elem_move_f((recv_type *)dst, (recv_type *)src); \ - } \ - static inline void _z_##name##_send(send_type *elem, void *context) { \ - void *internal_elem = elem_convert_f(elem); \ - if (internal_elem == NULL) { \ - return; \ - } \ - int8_t ret = collection_push_f(internal_elem, context, _z_##name##_elem_free); \ - if (ret != _Z_RES_OK) { \ - _Z_ERROR("%s failed: %i", #collection_push_f, ret); \ - } \ - } \ - static inline void _z_##name##_recv(recv_type *elem, void *context) { \ - int8_t ret = collection_pull_f(elem, context, _z_##name##_elem_move); \ - if (ret != _Z_RES_OK) { \ - _Z_ERROR("%s failed: %i", #collection_pull_f, ret); \ - } \ - } \ - static inline void _z_##name##_try_recv(recv_type *elem, void *context) { \ - int8_t ret = collection_try_pull_f(elem, context, _z_##name##_elem_move); \ - if (ret != _Z_RES_OK) { \ - _Z_ERROR("%s failed: %i", #collection_try_pull_f, ret); \ - } \ - } \ - \ - static inline int8_t z_##name##_new(z_owned_##name##_t *channel, size_t capacity) { \ - channel->collection = collection_new_f(capacity); \ - z_##send_closure_name(&channel->send, _z_##name##_send, NULL, channel->collection); \ - z_##recv_closure_name(&channel->recv, _z_##name##_recv, NULL, channel->collection); \ - z_##recv_closure_name(&channel->try_recv, _z_##name##_try_recv, NULL, channel->collection); \ - return _Z_RES_OK; \ - } \ - static inline z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *val) { return val; } \ - static inline void z_##name##_drop(z_owned_##name##_t *channel) { \ - collection_free_f(channel->collection, _z_##name##_elem_free); \ - z_##send_closure_name##_drop(&channel->send); \ - z_##recv_closure_name##_drop(&channel->recv); \ +#define _Z_CHANNEL_DEFINE_IMPL(handler_type, handler_name, handler_new_f_name, callback_type, callback_new_f, \ + collection_type, collection_new_f, collection_free_f, collection_push_f, \ + collection_pull_f, collection_try_pull_f, elem_owned_type, elem_loaned_type, \ + elem_copy_f, elem_drop_f) \ + typedef struct { \ + collection_type *collection; \ + } handler_type; \ + \ + _Z_OWNED_TYPE_PTR(handler_type, handler_name) \ + _Z_LOANED_TYPE(handler_type, handler_name) \ + \ + static inline void _z_##handler_name##_elem_free(void **elem) { \ + elem_drop_f((elem_owned_type *)*elem); \ + z_free(*elem); \ + *elem = NULL; \ + } \ + static inline void _z_##handler_name##_elem_move(void *dst, void *src) { \ + memcpy(dst, src, sizeof(elem_owned_type)); \ + z_free(src); \ + } \ + static inline void _z_##handler_name##_send(const elem_loaned_type *elem, void *context) { \ + elem_owned_type *internal_elem = (elem_owned_type *)z_malloc(sizeof(elem_owned_type)); \ + if (internal_elem == NULL) { \ + _Z_ERROR("Out of memory"); \ + return; \ + } \ + if (elem == NULL) { \ + internal_elem->_rc.in = NULL; \ + } else { \ + elem_copy_f(&internal_elem->_rc, elem); \ + } \ + int8_t ret = collection_push_f(internal_elem, context, _z_##handler_name##_elem_free); \ + if (ret != _Z_RES_OK) { \ + _Z_ERROR("%s failed: %i", #collection_push_f, ret); \ + } \ + } \ + static inline void z_##handler_name##_recv(const z_loaned_##handler_name##_t *handler, elem_owned_type *elem) { \ + int8_t ret = collection_pull_f(elem, (collection_type *)handler->collection, _z_##handler_name##_elem_move); \ + if (ret != _Z_RES_OK) { \ + _Z_ERROR("%s failed: %i", #collection_pull_f, ret); \ + } \ + } \ + static inline void z_##handler_name##_try_recv(const z_loaned_##handler_name##_t *handler, \ + elem_owned_type *elem) { \ + int8_t ret = \ + collection_try_pull_f(elem, (collection_type *)handler->collection, _z_##handler_name##_elem_move); \ + if (ret != _Z_RES_OK) { \ + _Z_ERROR("%s failed: %i", #collection_try_pull_f, ret); \ + } \ + } \ + \ + static inline void _z_##handler_name##_free(handler_type **handler) { \ + handler_type *ptr = *handler; \ + if (ptr != NULL) { \ + collection_free_f(ptr->collection, _z_##handler_name##_elem_free); \ + z_free(ptr); \ + *handler = NULL; \ + } \ + } \ + static inline void _z_##handler_name##_copy(void *dst, const void *src) { \ + (void)(dst); \ + (void)(src); \ + } \ + \ + _Z_OWNED_FUNCTIONS_PTR_IMPL(handler_type, handler_name, _z_##handler_name##_copy, _z_##handler_name##_free) \ + \ + static inline int8_t handler_new_f_name(callback_type *callback, z_owned_##handler_name##_t *handler, \ + size_t capacity) { \ + handler->_val = (handler_type *)z_malloc(sizeof(handler_type)); \ + handler->_val->collection = collection_new_f(capacity); \ + callback_new_f(callback, _z_##handler_name##_send, NULL, handler->_val->collection); \ + return _Z_RES_OK; \ } -// z_owned_sample_ring_channel_t -_Z_CHANNEL_DEFINE(sample_ring_channel, closure_sample, closure_owned_sample, const z_loaned_sample_t, z_owned_sample_t, - _z_ring_mt_t, _z_ring_mt_new, _z_ring_mt_free, _z_ring_mt_push, _z_ring_mt_pull, _z_ring_mt_try_pull, - _z_owned_sample_move, _z_sample_to_owned_ptr, z_sample_drop) +#define _Z_CHANNEL_DEFINE(item_name, kind_name) \ + _Z_CHANNEL_DEFINE_IMPL(/* handler_type */ _z_##kind_name##_handler_##item_name##_t, \ + /* handler_name */ kind_name##_handler_##item_name, \ + /* handler_new_f_name */ z_##kind_name##_channel_##item_name##_new, \ + /* callback_type */ z_owned_closure_##item_name##_t, \ + /* callback_new_f */ z_closure_##item_name, \ + /* collection_type */ _z_##kind_name##_mt_t, \ + /* collection_new_f */ _z_##kind_name##_mt_new, \ + /* collection_free_f */ _z_##kind_name##_mt_free, \ + /* collection_push_f */ _z_##kind_name##_mt_push, \ + /* collection_pull_f */ _z_##kind_name##_mt_pull, \ + /* collection_try_pull_f */ _z_##kind_name##_mt_try_pull, \ + /* elem_owned_type */ z_owned_##item_name##_t, \ + /* elem_loaned_type */ z_loaned_##item_name##_t, \ + /* elem_copy_f */ _z_##item_name##_rc_copy, \ + /* elem_drop_f */ z_##item_name##_drop) + +#define _Z_CHANNEL_DEFINE_DUMMY(item_name, kind_name) \ + typedef struct { \ + uint8_t _foo; \ + } z_owned_##kind_name##_handler_##item_name##_t; \ + typedef struct { \ + uint8_t _foo; \ + } z_loaned_##kind_name##_handler_##item_name##_t; \ + void *z_##kind_name##_handler_##item_name##_loan(void); \ + void *z_##kind_name##_handler_##item_name##_move(void); \ + void *z_##kind_name##_handler_##item_name##_drop(void); \ + void *z_##kind_name##_handler_##item_name##_recv(void); \ + void *z_##kind_name##_handler_##item_name##_try_recv(void); + +// This macro defines: +// z_ring_channel_sample_new() +// z_owned_ring_handler_sample_t/z_loaned_ring_handler_sample_t +_Z_CHANNEL_DEFINE(sample, ring) -// z_owned_sample_fifo_channel_t -_Z_CHANNEL_DEFINE(sample_fifo_channel, closure_sample, closure_owned_sample, const z_loaned_sample_t, z_owned_sample_t, - _z_fifo_mt_t, _z_fifo_mt_new, _z_fifo_mt_free, _z_fifo_mt_push, _z_fifo_mt_pull, _z_fifo_mt_try_pull, - _z_owned_sample_move, _z_sample_to_owned_ptr, z_sample_drop) +// This macro defines: +// z_fifo_channel_sample_new() +// z_owned_fifo_handler_sample_t/z_loaned_fifo_handler_sample_t +_Z_CHANNEL_DEFINE(sample, fifo) -// z_owned_query_ring_channel_t -_Z_CHANNEL_DEFINE(query_ring_channel, closure_query, closure_owned_query, const z_loaned_query_t, z_owned_query_t, - _z_ring_mt_t, _z_ring_mt_new, _z_ring_mt_free, _z_ring_mt_push, _z_ring_mt_pull, _z_ring_mt_try_pull, - _z_owned_query_move, _z_query_to_owned_ptr, z_query_drop) +#if Z_FEATURE_QUERYABLE == 1 +// This macro defines: +// z_ring_channel_query_new() +// z_owned_ring_handler_query_t/z_loaned_ring_handler_query_t +_Z_CHANNEL_DEFINE(query, ring) -// z_owned_query_fifo_channel_t -_Z_CHANNEL_DEFINE(query_fifo_channel, closure_query, closure_owned_query, const z_loaned_query_t, z_owned_query_t, - _z_fifo_mt_t, _z_fifo_mt_new, _z_fifo_mt_free, _z_fifo_mt_push, _z_fifo_mt_pull, _z_fifo_mt_try_pull, - _z_owned_query_move, _z_query_to_owned_ptr, z_query_drop) +// This macro defines: +// z_fifo_channel_query_new() +// z_owned_fifo_handler_query_t/z_loaned_fifo_handler_query_t +_Z_CHANNEL_DEFINE(query, fifo) +#else // Z_FEATURE_QUERYABLE +_Z_CHANNEL_DEFINE_DUMMY(query, ring) +_Z_CHANNEL_DEFINE_DUMMY(query, fifo) +#endif // Z_FEATURE_QUERYABLE -// z_owned_reply_ring_channel_t -_Z_CHANNEL_DEFINE(reply_ring_channel, closure_reply, closure_owned_reply, const z_loaned_reply_t, z_owned_reply_t, - _z_ring_mt_t, _z_ring_mt_new, _z_ring_mt_free, _z_ring_mt_push, _z_ring_mt_pull, _z_ring_mt_try_pull, - _z_owned_reply_move, _z_reply_to_owned_ptr, z_reply_drop) +#if Z_FEATURE_QUERY == 1 +// This macro defines: +// z_ring_channel_reply_new() +// z_owned_ring_handler_reply_t/z_loaned_ring_handler_reply_t +_Z_CHANNEL_DEFINE(reply, ring) -// z_owned_reply_fifo_channel_t -_Z_CHANNEL_DEFINE(reply_fifo_channel, closure_reply, closure_owned_reply, const z_loaned_reply_t, z_owned_reply_t, - _z_fifo_mt_t, _z_fifo_mt_new, _z_fifo_mt_free, _z_fifo_mt_push, _z_fifo_mt_pull, _z_fifo_mt_try_pull, - _z_owned_reply_move, _z_reply_to_owned_ptr, z_reply_drop) +// This macro defines: +// z_fifo_channel_reply_new() +// z_owned_fifo_handler_reply_t/z_loaned_fifo_handler_reply_t +_Z_CHANNEL_DEFINE(reply, fifo) +#else // Z_FEATURE_QUERY +_Z_CHANNEL_DEFINE_DUMMY(reply, ring) +_Z_CHANNEL_DEFINE_DUMMY(reply, fifo) +#endif // Z_FEATURE_QUERY #endif // INCLUDE_ZENOH_PICO_API_HANDLERS_H diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 851f6f525..aad0dd4e9 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -34,22 +34,31 @@ */ #define z_loan(x) _Generic((x), \ - z_owned_keyexpr_t : z_keyexpr_loan, \ - z_view_keyexpr_t : z_view_keyexpr_loan, \ - z_owned_config_t : z_config_loan, \ - z_owned_scouting_config_t : z_scouting_config_loan, \ - z_owned_session_t : z_session_loan, \ - z_owned_publisher_t : z_publisher_loan, \ - z_owned_reply_t : z_reply_loan, \ - z_owned_hello_t : z_hello_loan, \ - z_owned_string_t : z_string_loan, \ - z_view_string_t : z_view_string_loan, \ - z_owned_string_array_t : z_string_array_loan, \ - z_owned_sample_t : z_sample_loan, \ - z_owned_query_t : z_query_loan, \ - z_owned_slice_t : z_slice_loan, \ - z_owned_bytes_t : z_bytes_loan, \ - z_owned_encoding_t : z_encoding_loan \ + z_owned_keyexpr_t : z_keyexpr_loan, \ + z_view_keyexpr_t : z_view_keyexpr_loan, \ + z_owned_config_t : z_config_loan, \ + z_owned_scouting_config_t : z_scouting_config_loan, \ + z_owned_session_t : z_session_loan, \ + z_owned_subscriber_t : z_subscriber_loan, \ + z_owned_publisher_t : z_publisher_loan, \ + z_owned_queryable_t : z_queryable_loan, \ + z_owned_reply_t : z_reply_loan, \ + z_owned_hello_t : z_hello_loan, \ + z_owned_string_t : z_string_loan, \ + z_view_string_t : z_view_string_loan, \ + z_owned_string_array_t : z_string_array_loan, \ + z_owned_sample_t : z_sample_loan, \ + z_owned_query_t : z_query_loan, \ + z_owned_slice_t : z_slice_loan, \ + z_owned_bytes_t : z_bytes_loan, \ + z_owned_encoding_t : z_encoding_loan, \ + z_owned_fifo_handler_query_t : z_fifo_handler_query_loan, \ + z_owned_fifo_handler_reply_t : z_fifo_handler_reply_loan, \ + z_owned_fifo_handler_sample_t : z_fifo_handler_sample_loan, \ + z_owned_ring_handler_query_t : z_ring_handler_query_loan, \ + z_owned_ring_handler_reply_t : z_ring_handler_reply_loan, \ + z_owned_ring_handler_sample_t : z_ring_handler_sample_loan, \ + z_owned_reply_err_t : z_reply_err_loan \ )(&x) #define z_loan_mut(x) _Generic((x), \ @@ -58,6 +67,7 @@ z_owned_scouting_config_t : z_scouting_config_loan_mut, \ z_owned_session_t : z_session_loan_mut, \ z_owned_publisher_t : z_publisher_loan_mut, \ + z_owned_queryable_t : z_queryable_loan_mut, \ z_owned_reply_t : z_reply_loan_mut, \ z_owned_hello_t : z_hello_loan_mut, \ z_owned_string_t : z_string_loan_mut, \ @@ -66,7 +76,8 @@ z_owned_sample_t : z_sample_loan_mut, \ z_owned_query_t : z_query_loan_mut, \ z_owned_slice_t : z_slice_loan_mut, \ - z_owned_bytes_t : z_bytes_loan_mut \ + z_owned_bytes_t : z_bytes_loan_mut, \ + z_owned_reply_err_t : z_reply_err_loan_mut \ )(&x) /** * Defines a generic function for dropping any of the ``z_owned_X_t`` types. @@ -97,12 +108,13 @@ z_owned_closure_reply_t * : z_closure_reply_drop, \ z_owned_closure_hello_t * : z_closure_hello_drop, \ z_owned_closure_zid_t * : z_closure_zid_drop, \ - z_owned_sample_ring_channel_t * : z_sample_ring_channel_drop, \ - z_owned_sample_fifo_channel_t * : z_sample_fifo_channel_drop, \ - z_owned_query_ring_channel_t * : z_query_ring_channel_drop, \ - z_owned_query_fifo_channel_t * : z_query_fifo_channel_drop, \ - z_owned_reply_ring_channel_t * : z_reply_ring_channel_drop, \ - z_owned_reply_fifo_channel_t * : z_reply_fifo_channel_drop \ + z_owned_fifo_handler_query_t * : z_fifo_handler_query_drop, \ + z_owned_fifo_handler_reply_t * : z_fifo_handler_reply_drop, \ + z_owned_fifo_handler_sample_t * : z_fifo_handler_sample_drop, \ + z_owned_ring_handler_query_t * : z_ring_handler_query_drop, \ + z_owned_ring_handler_reply_t * : z_ring_handler_reply_drop, \ + z_owned_ring_handler_sample_t * : z_ring_handler_sample_drop, \ + z_owned_reply_err_t : z_reply_err_drop \ )(x) /** @@ -118,7 +130,7 @@ #define z_check(x) _Generic((x), \ z_owned_keyexpr_t : z_keyexpr_check, \ z_view_keyexpr_t : z_keyexpr_is_initialized, \ - z_owned_value_t : z_value_check, \ + z_owned_reply_err_t : z_reply_err_check, \ z_owned_config_t : z_config_check, \ z_owned_scouting_config_t : z_scouting_config_check, \ z_owned_session_t : z_session_check, \ @@ -153,6 +165,26 @@ z_owned_closure_owned_reply_t : z_closure_owned_reply_call \ ) (&x, __VA_ARGS__) +#define z_try_recv(x, ...) \ + _Generic((x), \ + const z_loaned_fifo_handler_query_t* : z_fifo_handler_query_try_recv, \ + const z_loaned_fifo_handler_reply_t* : z_fifo_handler_reply_try_recv, \ + const z_loaned_fifo_handler_sample_t* : z_fifo_handler_sample_try_recv, \ + const z_loaned_ring_handler_query_t* : z_ring_handler_query_try_recv, \ + const z_loaned_ring_handler_reply_t* : z_ring_handler_reply_try_recv, \ + const z_loaned_ring_handler_sample_t* : z_ring_handler_sample_try_recv \ + )(x, __VA_ARGS__) + +#define z_recv(x, ...) \ + _Generic((x), \ + const z_loaned_fifo_handler_query_t* : z_fifo_handler_query_recv, \ + const z_loaned_fifo_handler_reply_t* : z_fifo_handler_reply_recv, \ + const z_loaned_fifo_handler_sample_t* : z_fifo_handler_sample_recv, \ + const z_loaned_ring_handler_query_t* : z_ring_handler_query_recv, \ + const z_loaned_ring_handler_reply_t* : z_ring_handler_reply_recv, \ + const z_loaned_ring_handler_sample_t* : z_ring_handler_sample_recv \ + )(x, __VA_ARGS__) + /** * Defines a generic function for moving any of the ``z_owned_X_t`` types. * @@ -186,12 +218,13 @@ z_owned_slice_t : z_slice_move, \ z_owned_bytes_t : z_bytes_move, \ z_owned_encoding_t : z_encoding_move, \ - z_owned_sample_ring_channel_t : z_sample_ring_channel_move, \ - z_owned_sample_fifo_channel_t : z_sample_fifo_channel_move, \ - z_owned_query_ring_channel_t : z_query_ring_channel_move, \ - z_owned_query_fifo_channel_t : z_query_fifo_channel_move, \ - z_owned_reply_ring_channel_t : z_reply_ring_channel_move, \ - z_owned_reply_fifo_channel_t : z_reply_fifo_channel_move \ + z_owned_ring_handler_query_t : z_ring_handler_query_move, \ + z_owned_ring_handler_reply_t : z_ring_handler_reply_move, \ + z_owned_ring_handler_sample_t : z_ring_handler_sample_move, \ + z_owned_fifo_handler_query_t : z_fifo_handler_query_move, \ + z_owned_fifo_handler_reply_t : z_fifo_handler_reply_move, \ + z_owned_fifo_handler_sample_t : z_fifo_handler_sample_move, \ + z_owned_reply_err_t : z_reply_err_move \ )(&x) /** @@ -210,6 +243,11 @@ z_owned_subscriber_t : z_subscriber_clone, \ z_owned_publisher_t : z_publisher_clone, \ z_owned_queryable_t : z_queryable_clone, \ + z_owned_query_t : z_query_clone, \ + z_owned_sample_t : z_sample_clone, \ + z_owned_bytes_t : z_bytes_clone, \ + z_owned_encoding_t : z_encoding_clone, \ + z_owned_reply_err_t : z_reply_err_clone, \ z_owned_reply_t : z_reply_clone, \ z_owned_hello_t : z_hello_clone, \ z_owned_string_t : z_string_clone, \ @@ -244,7 +282,8 @@ z_owned_closure_hello_t * : z_closure_hello_null, \ z_owned_closure_zid_t * : z_closure_zid_null, \ z_owned_sample_t * : z_sample_null, \ - z_owned_encoding_t * : z_encoding_null \ + z_owned_encoding_t * : z_encoding_null, \ + z_owned_reply_err_t * : z_reply_err_null \ )(x) // clang-format on @@ -275,126 +314,75 @@ // z_loan definition -template struct zenoh_loan_type { typedef T type; }; -template inline typename zenoh_loan_type::type z_loan(const T&); - -template <> struct zenoh_loan_type { typedef const z_loaned_keyexpr_t* type; }; -template <> struct zenoh_loan_type { typedef const z_loaned_keyexpr_t* type; }; -template <> struct zenoh_loan_type { typedef const z_loaned_config_t* type; }; -template <> struct zenoh_loan_type { const typedef z_loaned_scouting_config_t* type; }; -template <> struct zenoh_loan_type { typedef const z_loaned_session_t* type; }; -template <> struct zenoh_loan_type { typedef const z_loaned_publisher_t* type; }; -template <> struct zenoh_loan_type { typedef const z_loaned_reply_t* type; }; -template <> struct zenoh_loan_type { typedef const z_loaned_hello_t* type; }; -template <> struct zenoh_loan_type { typedef const z_loaned_string_t* type; }; -template <> struct zenoh_loan_type { typedef const z_loaned_string_t* type; }; -template <> struct zenoh_loan_type { typedef const z_loaned_string_array_t* type; }; -template <> struct zenoh_loan_type { typedef const z_loaned_sample_t* type; }; -template <> struct zenoh_loan_type { typedef const z_loaned_query_t* type; }; -template <> struct zenoh_loan_type { typedef const z_loaned_slice_t* type; }; -template <> struct zenoh_loan_type { typedef const z_loaned_bytes_t* type; }; - -template <> inline const z_loaned_keyexpr_t* z_loan(const z_owned_keyexpr_t& x) { return z_keyexpr_loan(&x); } -template <> inline const z_loaned_keyexpr_t* z_loan(const z_view_keyexpr_t& x) { return z_view_keyexpr_loan(&x); } -template <> inline const z_loaned_config_t* z_loan(const z_owned_config_t& x) { return z_config_loan(&x); } -template <> inline const z_loaned_scouting_config_t* z_loan(const z_owned_scouting_config_t& x) { return z_scouting_config_loan(&x); } -template <> inline const z_loaned_session_t* z_loan(const z_owned_session_t& x) { return z_session_loan(&x); } -template <> inline const z_loaned_publisher_t* z_loan(const z_owned_publisher_t& x) { return z_publisher_loan(&x); } -template <> inline const z_loaned_reply_t* z_loan(const z_owned_reply_t& x) { return z_reply_loan(&x); } -template <> inline const z_loaned_hello_t* z_loan(const z_owned_hello_t& x) { return z_hello_loan(&x); } -template <> inline const z_loaned_string_t* z_loan(const z_owned_string_t& x) { return z_string_loan(&x); } -template <> inline const z_loaned_string_t* z_loan(const z_view_string_t& x) { return z_view_string_loan(&x); } -template <> inline const z_loaned_string_array_t* z_loan(const z_owned_string_array_t& x) { return z_string_array_loan(&x); } -template <> inline const z_loaned_sample_t* z_loan(const z_owned_sample_t& x) { return z_sample_loan(&x); } -template <> inline const z_loaned_query_t* z_loan(const z_owned_query_t& x) { return z_query_loan(&x); } -template <> inline const z_loaned_slice_t* z_loan(const z_owned_slice_t& x) { return z_slice_loan(&x); } -template <> inline const z_loaned_bytes_t* z_loan(const z_owned_bytes_t& x) { return z_bytes_loan(&x); } +inline const z_loaned_keyexpr_t* z_loan(const z_owned_keyexpr_t& x) { return z_keyexpr_loan(&x); } +inline const z_loaned_keyexpr_t* z_loan(const z_view_keyexpr_t& x) { return z_view_keyexpr_loan(&x); } +inline const z_loaned_config_t* z_loan(const z_owned_config_t& x) { return z_config_loan(&x); } +inline const z_loaned_scouting_config_t* z_loan(const z_owned_scouting_config_t& x) { return z_scouting_config_loan(&x); } +inline const z_loaned_session_t* z_loan(const z_owned_session_t& x) { return z_session_loan(&x); } +inline const z_loaned_subscriber_t* z_loan(const z_owned_subscriber_t& x) { return z_subscriber_loan(&x); } +inline const z_loaned_publisher_t* z_loan(const z_owned_publisher_t& x) { return z_publisher_loan(&x); } +inline const z_loaned_queryable_t* z_loan(const z_owned_queryable_t& x) { return z_queryable_loan(&x); } +inline const z_loaned_reply_t* z_loan(const z_owned_reply_t& x) { return z_reply_loan(&x); } +inline const z_loaned_hello_t* z_loan(const z_owned_hello_t& x) { return z_hello_loan(&x); } +inline const z_loaned_string_t* z_loan(const z_owned_string_t& x) { return z_string_loan(&x); } +inline const z_loaned_string_t* z_loan(const z_view_string_t& x) { return z_view_string_loan(&x); } +inline const z_loaned_string_array_t* z_loan(const z_owned_string_array_t& x) { return z_string_array_loan(&x); } +inline const z_loaned_sample_t* z_loan(const z_owned_sample_t& x) { return z_sample_loan(&x); } +inline const z_loaned_query_t* z_loan(const z_owned_query_t& x) { return z_query_loan(&x); } +inline const z_loaned_slice_t* z_loan(const z_owned_slice_t& x) { return z_slice_loan(&x); } +inline const z_loaned_bytes_t* z_loan(const z_owned_bytes_t& x) { return z_bytes_loan(&x); } +inline const z_loaned_encoding_t* z_loan(const z_owned_encoding_t& x) { return z_encoding_loan(&x); } +inline const z_loaned_reply_err_t* z_loan(const z_owned_reply_err_t& x) { return z_reply_err_loan(&x); } // z_loan_mut definition - -template struct zenoh_loan_mut_type { typedef T type; }; -template inline typename zenoh_loan_mut_type::type z_loan_mut(T&); - -template <> struct zenoh_loan_mut_type { typedef z_loaned_keyexpr_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_keyexpr_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_config_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_scouting_config_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_session_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_publisher_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_reply_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_hello_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_string_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_string_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_string_array_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_sample_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_query_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_slice_t* type; }; -template <> struct zenoh_loan_mut_type { typedef z_loaned_bytes_t* type; }; - -template <> inline z_loaned_keyexpr_t* z_loan_mut(z_owned_keyexpr_t& x) { return z_keyexpr_loan_mut(&x); } -template <> inline z_loaned_keyexpr_t* z_loan_mut(z_view_keyexpr_t& x) { return z_view_keyexpr_loan_mut(&x); } -template <> inline z_loaned_config_t* z_loan_mut(z_owned_config_t& x) { return z_config_loan_mut(&x); } -template <> inline z_loaned_scouting_config_t* z_loan_mut(z_owned_scouting_config_t& x) { return z_scouting_config_loan_mut(&x); } -template <> inline z_loaned_session_t* z_loan_mut(z_owned_session_t& x) { return z_session_loan_mut(&x); } -template <> inline z_loaned_publisher_t* z_loan_mut(z_owned_publisher_t& x) { return z_publisher_loan_mut(&x); } -template <> inline z_loaned_reply_t* z_loan_mut(z_owned_reply_t& x) { return z_reply_loan_mut(&x); } -template <> inline z_loaned_hello_t* z_loan_mut(z_owned_hello_t& x) { return z_hello_loan_mut(&x); } -template <> inline z_loaned_string_t* z_loan_mut(z_owned_string_t& x) { return z_string_loan_mut(&x); } -template <> inline z_loaned_string_t* z_loan_mut(z_view_string_t& x) { return z_view_string_loan_mut(&x); } -template <> inline z_loaned_string_array_t* z_loan_mut(z_owned_string_array_t& x) { return z_string_array_loan_mut(&x); } -template <> inline z_loaned_sample_t* z_loan_mut(z_owned_sample_t& x) { return z_sample_loan_mut(&x); } -template <> inline z_loaned_query_t* z_loan_mut(z_owned_query_t& x) { return z_query_loan_mut(&x); } -template <> inline z_loaned_slice_t* z_loan_mut(z_owned_slice_t& x) { return z_slice_loan_mut(&x); } -template <> inline z_loaned_bytes_t* z_loan_mut(z_owned_bytes_t& x) { return z_bytes_loan_mut(&x); } +inline z_loaned_keyexpr_t* z_loan_mut(z_owned_keyexpr_t& x) { return z_keyexpr_loan_mut(&x); } +inline z_loaned_keyexpr_t* z_loan_mut(z_view_keyexpr_t& x) { return z_view_keyexpr_loan_mut(&x); } +inline z_loaned_config_t* z_loan_mut(z_owned_config_t& x) { return z_config_loan_mut(&x); } +inline z_loaned_scouting_config_t* z_loan_mut(z_owned_scouting_config_t& x) { return z_scouting_config_loan_mut(&x); } +inline z_loaned_session_t* z_loan_mut(z_owned_session_t& x) { return z_session_loan_mut(&x); } +inline z_loaned_publisher_t* z_loan_mut(z_owned_publisher_t& x) { return z_publisher_loan_mut(&x); } +inline z_loaned_queryable_t* z_loan_mut(z_owned_queryable_t& x) { return z_queryable_loan_mut(&x); } +inline z_loaned_reply_t* z_loan_mut(z_owned_reply_t& x) { return z_reply_loan_mut(&x); } +inline z_loaned_hello_t* z_loan_mut(z_owned_hello_t& x) { return z_hello_loan_mut(&x); } +inline z_loaned_string_t* z_loan_mut(z_owned_string_t& x) { return z_string_loan_mut(&x); } +inline z_loaned_string_t* z_loan_mut(z_view_string_t& x) { return z_view_string_loan_mut(&x); } +inline z_loaned_string_array_t* z_loan_mut(z_owned_string_array_t& x) { return z_string_array_loan_mut(&x); } +inline z_loaned_sample_t* z_loan_mut(z_owned_sample_t& x) { return z_sample_loan_mut(&x); } +inline z_loaned_query_t* z_loan_mut(z_owned_query_t& x) { return z_query_loan_mut(&x); } +inline z_loaned_slice_t* z_loan_mut(z_owned_slice_t& x) { return z_slice_loan_mut(&x); } +inline z_loaned_bytes_t* z_loan_mut(z_owned_bytes_t& x) { return z_bytes_loan_mut(&x); } +inline z_loaned_encoding_t* z_loan(z_owned_encoding_t& x) { return z_encoding_loan_mut(&x); } +inline z_loaned_reply_err_t* z_loan_mut(z_owned_reply_err_t& x) { return z_reply_err_loan_mut(&x); } // z_drop definition - -template struct zenoh_drop_type { typedef T type; }; -template inline typename zenoh_drop_type::type z_drop(T*); - -template<> struct zenoh_drop_type { typedef int8_t type; }; -template<> struct zenoh_drop_type { typedef int8_t type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef int8_t type; }; -template<> struct zenoh_drop_type { typedef int8_t type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; - -template<> inline int8_t z_drop(z_owned_session_t* v) { return z_close(v); } -template<> inline int8_t z_drop(z_owned_publisher_t* v) { return z_undeclare_publisher(v); } -template<> inline void z_drop(z_owned_keyexpr_t* v) { z_keyexpr_drop(v); } -template<> inline void z_drop(z_owned_config_t* v) { z_config_drop(v); } -template<> inline void z_drop(z_owned_scouting_config_t* v) { z_scouting_config_drop(v); } -template<> inline int8_t z_drop(z_owned_subscriber_t* v) { return z_undeclare_subscriber(v); } -template<> inline int8_t z_drop(z_owned_queryable_t* v) { return z_undeclare_queryable(v); } -template<> inline void z_drop(z_owned_reply_t* v) { z_reply_drop(v); } -template<> inline void z_drop(z_owned_hello_t* v) { z_hello_drop(v); } -template<> inline void z_drop(z_owned_string_t* v) { z_string_drop(v); } -template<> inline void z_drop(z_owned_closure_sample_t* v) { z_closure_sample_drop(v); } -template<> inline void z_drop(z_owned_closure_query_t* v) { z_closure_query_drop(v); } -template<> inline void z_drop(z_owned_closure_reply_t* v) { z_closure_reply_drop(v); } -template<> inline void z_drop(z_owned_closure_hello_t* v) { z_closure_hello_drop(v); } -template<> inline void z_drop(z_owned_closure_zid_t* v) { z_closure_zid_drop(v); } -template<> inline void z_drop(z_owned_sample_ring_channel_t* v) { z_sample_ring_channel_drop(v); } -template<> inline void z_drop(z_owned_sample_fifo_channel_t* v) { z_sample_fifo_channel_drop(v); } -template<> inline void z_drop(z_owned_query_ring_channel_t* v) { z_query_ring_channel_drop(v); } -template<> inline void z_drop(z_owned_query_fifo_channel_t* v) { z_query_fifo_channel_drop(v); } -template<> inline void z_drop(z_owned_reply_ring_channel_t* v) { z_reply_ring_channel_drop(v); } -template<> inline void z_drop(z_owned_reply_fifo_channel_t* v) { z_reply_fifo_channel_drop(v); } +inline int8_t z_drop(z_owned_session_t* v) { return z_close(v); } +inline int8_t z_drop(z_owned_publisher_t* v) { return z_undeclare_publisher(v); } +inline void z_drop(z_owned_keyexpr_t* v) { z_keyexpr_drop(v); } +inline void z_drop(z_owned_config_t* v) { z_config_drop(v); } +inline void z_drop(z_owned_scouting_config_t* v) { z_scouting_config_drop(v); } +inline int8_t z_drop(z_owned_subscriber_t* v) { return z_undeclare_subscriber(v); } +inline int8_t z_drop(z_owned_queryable_t* v) { return z_undeclare_queryable(v); } +inline void z_drop(z_owned_reply_t* v) { z_reply_drop(v); } +inline void z_drop(z_owned_hello_t* v) { z_hello_drop(v); } +inline void z_drop(z_owned_string_t* v) { z_string_drop(v); } +inline void z_drop(z_owned_slice_t* v) { z_slice_drop(v); } +inline void z_drop(z_owned_string_array_t* v) { z_string_array_drop(v); } +inline void z_drop(z_owned_sample_t* v) { z_sample_drop(v); } +inline void z_drop(z_owned_query_t* v) { z_query_drop(v); } +inline void z_drop(z_owned_bytes_t* v) { z_bytes_drop(v); } +inline void z_drop(z_owned_encoding_t* v) { z_encoding_drop(v); } +inline void z_drop(z_owned_reply_err_t* v) { z_reply_err_drop(v); } +inline void z_drop(z_owned_closure_sample_t* v) { z_closure_sample_drop(v); } +inline void z_drop(z_owned_closure_query_t* v) { z_closure_query_drop(v); } +inline void z_drop(z_owned_closure_reply_t* v) { z_closure_reply_drop(v); } +inline void z_drop(z_owned_closure_hello_t* v) { z_closure_hello_drop(v); } +inline void z_drop(z_owned_closure_zid_t* v) { z_closure_zid_drop(v); } +inline void z_drop(z_owned_sample_ring_channel_t* v) { z_sample_ring_channel_drop(v); } +inline void z_drop(z_owned_sample_fifo_channel_t* v) { z_sample_fifo_channel_drop(v); } +inline void z_drop(z_owned_query_ring_channel_t* v) { z_query_ring_channel_drop(v); } +inline void z_drop(z_owned_query_fifo_channel_t* v) { z_query_fifo_channel_drop(v); } +inline void z_drop(z_owned_reply_ring_channel_t* v) { z_reply_ring_channel_drop(v); } +inline void z_drop(z_owned_reply_fifo_channel_t* v) { z_reply_fifo_channel_drop(v); } // z_null definition @@ -405,9 +393,14 @@ inline void z_null(z_owned_config_t* v) { z_config_null(v); } inline void z_null(z_owned_scouting_config_t* v) { z_scouting_config_null(v); } inline void z_null(z_owned_subscriber_t* v) { z_subscriber_null(v); } inline void z_null(z_owned_queryable_t* v) { z_queryable_null(v); } +inline void z_null(z_owned_query_t* v) { z_query_null(v); } +inline void z_null(z_owned_sample_t* v) { z_sample_null(v); } inline void z_null(z_owned_reply_t* v) { z_reply_null(v); } inline void z_null(z_owned_hello_t* v) { z_hello_null(v); } inline void z_null(z_owned_string_t* v) { z_string_null(v); } +inline void z_null(z_owned_bytes_t* v) { z_bytes_null(v); } +inline void z_null(z_owned_encoding_t* v) { z_encoding_null(v); } +inline void z_null(z_owned_reply_err_t* v) { z_reply_err_null(v); } inline void z_null(z_owned_closure_sample_t* v) { z_closure_sample_null(v); } inline void z_null(z_owned_closure_query_t* v) { z_closure_query_null(v); } inline void z_null(z_owned_closure_reply_t* v) { z_closure_reply_null(v); } @@ -422,9 +415,13 @@ inline bool z_check(const z_owned_scouting_config_t& v) { return z_scouting_conf inline bool z_check(const z_owned_subscriber_t& v) { return z_subscriber_check(&v); } inline bool z_check(const z_owned_queryable_t& v) { return z_queryable_check(&v); } inline bool z_check(const z_owned_reply_t& v) { return z_reply_check(&v); } +inline bool z_check(const z_owned_query_t& v) { return z_query_check(&v); } inline bool z_check(const z_owned_hello_t& v) { return z_hello_check(&v); } inline bool z_check(const z_owned_string_t& v) { return z_string_check(&v); } inline bool z_check(const z_owned_sample_t& v) { return z_sample_check(&v); } +inline bool z_check(const z_owned_bytes_t& v) { return z_bytes_check(&v); } +inline bool z_check(const z_owned_encoding_t& v) { return z_encoding_check(&v); } +inline bool z_check(const z_owned_reply_err_t& v) { return z_reply_err_check(&v); } // z_call definition @@ -436,22 +433,313 @@ inline void z_call(const z_owned_closure_reply_t &closure, const z_loaned_reply_ { z_closure_reply_call(&closure, reply); } inline void z_call(const z_owned_closure_owned_reply_t &closure, z_owned_reply_t *reply) { z_closure_owned_reply_call(&closure, reply); } -inline void z_call(const z_owned_closure_hello_t &closure, z_owned_hello_t *hello) +inline void z_call(const z_owned_closure_hello_t &closure, const z_loaned_hello_t *hello) { z_closure_hello_call(&closure, hello); } inline void z_call(const z_owned_closure_zid_t &closure, const z_id_t *zid) { z_closure_zid_call(&closure, zid); } -// clang-format on -#define _z_closure_overloader(closure, callback, dropper, ctx, ...) \ - do { \ - (closure)->call = callback; \ - (closure)->drop = dropper; \ - (closure)->context = const_cast(static_cast(ctx)); \ - } while (0); +inline void z_closure( + z_owned_closure_hello_t* closure, + void (*call)(const z_loaned_hello_t*, void*), + void (*drop)(void*) = NULL, + void *context = NULL) { + closure->context = context; + closure->drop = drop; + closure->call = call; +}; +inline void z_closure( + z_owned_closure_owned_query_t* closure, + void (*call)(z_owned_query_t*, void*), + void (*drop)(void*) = NULL, + void *context = NULL) { + closure->context = context; + closure->drop = drop; + closure->call = call; +}; +inline void z_closure( + z_owned_closure_query_t* closure, + void (*call)(const z_loaned_query_t*, void*), + void (*drop)(void*) = NULL, + void *context = NULL) { + closure->context = context; + closure->drop = drop; + closure->call = call; +}; +inline void z_closure( + z_owned_closure_reply_t* closure, + void (*call)(const z_loaned_reply_t*, void*), + void (*drop)(void*) = NULL, + void *context = NULL) { + closure->context = context; + closure->drop = drop; + closure->call = call; +}; +inline void z_closure( + z_owned_closure_sample_t* closure, + void (*call)(const z_loaned_sample_t*, void*), + void (*drop)(void*) = NULL, + void *context = NULL) { + closure->context = context; + closure->drop = drop; + closure->call = call; +}; +inline void z_closure( + z_owned_closure_zid_t* closure, + void (*call)(const z_id_t*, void*), + void (*drop)(void*) = NULL, + void *context = NULL) { + closure->context = context; + closure->drop = drop; + closure->call = call; +}; + +inline bool z_try_recv(const z_loaned_fifo_handler_query_t* this_, z_owned_query_t* query) { + return z_fifo_handler_query_try_recv(this_, query); +}; +inline bool z_try_recv(const z_loaned_fifo_handler_reply_t* this_, z_owned_reply_t* reply) { + return z_fifo_handler_reply_try_recv(this_, reply); +}; +inline bool z_try_recv(const z_loaned_fifo_handler_sample_t* this_, z_owned_sample_t* sample) { + return z_fifo_handler_sample_try_recv(this_, sample); +}; +inline bool z_try_recv(const z_loaned_ring_handler_query_t* this_, z_owned_query_t* query) { + return z_ring_handler_query_try_recv(this_, query); +}; +inline bool z_try_recv(const z_loaned_ring_handler_reply_t* this_, z_owned_reply_t* reply) { + return z_ring_handler_reply_try_recv(this_, reply); +}; +inline bool z_try_recv(const z_loaned_ring_handler_sample_t* this_, z_owned_sample_t* sample) { + return z_ring_handler_sample_try_recv(this_, sample); +}; + + +inline bool z_recv(const z_loaned_fifo_handler_query_t* this_, z_owned_query_t* query) { + return z_fifo_handler_query_recv(this_, query); +}; +inline bool z_recv(const z_loaned_fifo_handler_reply_t* this_, z_owned_reply_t* reply) { + return z_fifo_handler_reply_recv(this_, reply); +}; +inline bool z_recv(const z_loaned_fifo_handler_sample_t* this_, z_owned_sample_t* sample) { + return z_fifo_handler_sample_recv(this_, sample); +}; +inline bool z_recv(const z_loaned_ring_handler_query_t* this_, z_owned_query_t* query) { + return z_ring_handler_query_recv(this_, query); +}; +inline bool z_recv(const z_loaned_ring_handler_reply_t* this_, z_owned_reply_t* reply) { + return z_ring_handler_reply_recv(this_, reply); +}; +inline bool z_recv(const z_loaned_ring_handler_sample_t* this_, z_owned_sample_t* sample) { + return z_ring_handler_sample_recv(this_, sample); +}; -#define z_closure(...) _z_closure_overloader(__VA_ARGS__, NULL, NULL) +// clang-format on -#define z_move(x) (&x) +inline z_owned_bytes_t* z_move(z_owned_bytes_t& x) { return z_bytes_move(&x); }; +inline z_owned_closure_hello_t* z_move(z_owned_closure_hello_t& closure) { return z_closure_hello_move(&closure); }; +inline z_owned_closure_owned_query_t* z_move(z_owned_closure_owned_query_t& closure) { + return z_closure_owned_query_move(&closure); +}; +inline z_owned_closure_query_t* z_move(z_owned_closure_query_t& closure) { return z_closure_query_move(&closure); }; +inline z_owned_closure_reply_t* z_move(z_owned_closure_reply_t& closure) { return z_closure_reply_move(&closure); }; +inline z_owned_closure_sample_t* z_move(z_owned_closure_sample_t& closure) { return z_closure_sample_move(&closure); }; +inline z_owned_closure_zid_t* z_move(z_owned_closure_zid_t& closure) { return z_closure_zid_move(&closure); }; +inline z_owned_config_t* z_move(z_owned_config_t& x) { return z_config_move(&x); }; +inline z_owned_encoding_t* z_move(z_owned_encoding_t& x) { return z_encoding_move(&x); }; +inline z_owned_reply_err_t* z_move(z_owned_reply_err_t& x) { return z_reply_err_move(&x); }; +inline z_owned_hello_t* z_move(z_owned_hello_t& x) { return z_hello_move(&x); }; +inline z_owned_keyexpr_t* z_move(z_owned_keyexpr_t& x) { return z_keyexpr_move(&x); }; +inline z_owned_publisher_t* z_move(z_owned_publisher_t& x) { return z_publisher_move(&x); }; +inline z_owned_query_t* z_move(z_owned_query_t& x) { return z_query_move(&x); }; +inline z_owned_queryable_t* z_move(z_owned_queryable_t& x) { return z_queryable_move(&x); }; +inline z_owned_reply_t* z_move(z_owned_reply_t& x) { return z_reply_move(&x); }; +inline z_owned_sample_t* z_move(z_owned_sample_t& x) { return z_sample_move(&x); }; +inline z_owned_session_t* z_move(z_owned_session_t& x) { return z_session_move(&x); }; +inline z_owned_slice_t* z_move(z_owned_slice_t& x) { return z_slice_move(&x); }; +inline z_owned_string_array_t* z_move(z_owned_string_array_t& x) { return z_string_array_move(&x); }; +inline z_owned_string_t* z_move(z_owned_string_t& x) { return z_string_move(&x); }; +inline z_owned_subscriber_t* z_move(z_owned_subscriber_t& x) { return z_subscriber_move(&x); }; + +template +struct z_loaned_to_owned_type_t {}; +template +struct z_owned_to_loaned_type_t {}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_bytes_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_bytes_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_closure_hello_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_closure_hello_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_closure_owned_query_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_closure_owned_query_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_closure_query_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_closure_query_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_closure_reply_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_closure_reply_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_closure_sample_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_closure_sample_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_closure_zid_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_closure_zid_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_config_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_config_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_encoding_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_encoding_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_reply_err_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_reply_err_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_hello_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_hello_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_keyexpr_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_keyexpr_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_publisher_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_publisher_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_query_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_query_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_queryable_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_queryable_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_reply_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_reply_t type; +}; +; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_sample_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_sample_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_session_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_session_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_slice_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_slice_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_string_array_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_string_array_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_string_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_string_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_subscriber_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_subscriber_t type; +}; #endif diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 1fb647705..7b5167615 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -375,6 +375,8 @@ _Bool z_encoding_check(const z_owned_encoding_t *encoding); */ void z_encoding_drop(z_owned_encoding_t *encoding); +int8_t z_encoding_clone(z_owned_encoding_t *dst, const z_loaned_encoding_t *src); + /** * Gets a loaned version of a :c:type:`z_owned_encoding_t`. * @@ -386,6 +388,17 @@ void z_encoding_drop(z_owned_encoding_t *encoding); */ const z_loaned_encoding_t *z_encoding_loan(const z_owned_encoding_t *encoding); +/** + * Gets a loaned version of a :c:type:`z_owned_encoding_t`. + * + * Parameters: + * encoding: Pointer to a :c:type:`z_owned_encoding_t` to loan. + * + * Return: + * Pointer to the loaned version. + */ +z_loaned_encoding_t *z_encoding_loan_mut(z_owned_encoding_t *encoding); + /** * Gets a moved version of a :c:type:`z_owned_encoding_t`. * @@ -409,15 +422,26 @@ z_owned_encoding_t *z_encoding_move(z_owned_encoding_t *encoding); int8_t z_encoding_null(z_owned_encoding_t *encoding); /** - * Gets the bytes data from a value payload by aliasing it. + * Gets the bytes data from a reply error payload by aliasing it. * * Parameters: - * value: Pointer to a :c:type:`z_loaned_value_t` to get data from. + * reply_err: Pointer to a :c:type:`z_loaned_reply_err_t` to get data from. * * Return: * Pointer to the data as a :c:type:`z_loaned_bytes_t`. */ -const z_loaned_bytes_t *z_value_payload(const z_loaned_value_t *value); +const z_loaned_bytes_t *z_reply_err_payload(const z_loaned_reply_err_t *reply_err); + +/** + * Gets a reply error encoding by aliasing it. + * + * Parameters: + * query: Pointer to the :c:type:`z_loaned_reply_err_t` to get the encoding from. + * + * Return: + * Pointer to the encoding as a :c:type:`z_loaned_encoding_t`. + */ +const z_loaned_encoding_t *z_reply_err_encoding(const z_loaned_reply_err_t *reply_err); /** * Gets date pointer of a bytes array. @@ -878,15 +902,26 @@ z_query_consolidation_t z_query_consolidation_none(void); void z_query_parameters(const z_loaned_query_t *query, z_view_string_t *parameters); /** - * Gets a query value payload by aliasing it. + * Gets a query payload by aliasing it. + * + * Parameters: + * query: Pointer to the :c:type:`z_loaned_query_t` to get the value from. + * + * Return: + * Pointer to the payload as a :c:type:`z_loaned_bytes_t`. + */ +const z_loaned_bytes_t *z_query_payload(const z_loaned_query_t *query); + +/** + * Gets a query encoding by aliasing it. * * Parameters: * query: Pointer to the :c:type:`z_loaned_query_t` to get the value from. * * Return: - * Pointer to the value payload as a :c:type:`z_loaned_value_t`. + * Pointer to the encoding as a :c:type:`z_loaned_encoding_t`. */ -const z_loaned_value_t *z_query_value(const z_loaned_query_t *query); +const z_loaned_encoding_t *z_query_encoding(const z_loaned_query_t *query); /** * Gets a query attachment value by aliasing it. @@ -1045,7 +1080,7 @@ _Z_OWNED_FUNCTIONS_DEF(z_loaned_sample_t, z_owned_sample_t, sample) _Z_OWNED_FUNCTIONS_DEF(z_loaned_query_t, z_owned_query_t, query) _Z_OWNED_FUNCTIONS_DEF(z_loaned_slice_t, z_owned_slice_t, slice) _Z_OWNED_FUNCTIONS_DEF(z_loaned_bytes_t, z_owned_bytes_t, bytes) -_Z_OWNED_FUNCTIONS_DEF(z_loaned_value_t, z_owned_value_t, value) +_Z_OWNED_FUNCTIONS_DEF(z_loaned_reply_err_t, z_owned_reply_err_t, reply_err) _Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_sample_t, closure_sample) _Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_owned_sample_t, closure_owned_sample) @@ -1272,15 +1307,14 @@ void z_delete_options_default(z_delete_options_t *options); * Parameters: * zs: Pointer to a :c:type:`z_loaned_session_t` to put the data through. * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to put the data for. - * payload: Pointer to the data to put. - * payload_len: The length of the ``payload``. + * payload: Pointer to a moved :c:type:`z_owned_bytes_t` containing the data to put. * options: Pointer to a :c:type:`z_put_options_t` to configure the operation. * * Return: * ``0`` if put operation successful, ``negative value`` otherwise. */ -int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, const uint8_t *payload, - z_zint_t payload_len, const z_put_options_t *options); +int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_bytes_t *payload, + const z_put_options_t *options); /** * Deletes data for a given keyexpr. @@ -1354,12 +1388,13 @@ void z_publisher_delete_options_default(z_publisher_delete_options_t *options); * * Parameters: * pub: Pointer to a :c:type:`z_loaned_publisher_t` from where to put the data. + * payload: Pointer to a moved :c:type:`z_owned_bytes_t` containing the data to put. * options: Pointer to a :c:type:`z_publisher_put_options_t` to configure the operation. * * Return: * ``0`` if put operation successful, ``negative value`` otherwise. */ -int8_t z_publisher_put(const z_loaned_publisher_t *pub, const uint8_t *payload, size_t len, +int8_t z_publisher_put(const z_loaned_publisher_t *pub, z_owned_bytes_t *payload, const z_publisher_put_options_t *options); /** @@ -1432,9 +1467,9 @@ const z_loaned_sample_t *z_reply_ok(const z_loaned_reply_t *reply); * reply: Pointer to a :c:type:`z_loaned_reply_t` to get content from. * * Return: - * The error reply content wrapped as a :c:type:`z_loaned_value_t`. + * The error reply content wrapped as a :c:type:`z_loaned_reply_err_t`. */ -const z_loaned_value_t *z_reply_err(const z_loaned_reply_t *reply); +const z_loaned_reply_err_t *z_reply_err(const z_loaned_reply_t *reply); #endif #if Z_FEATURE_QUERYABLE == 1 diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index d5291de93..77b2b0195 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -184,14 +184,14 @@ _Z_OWNED_TYPE_PTR(_z_encoding_t, encoding) _Z_LOANED_TYPE(_z_encoding_t, encoding) /** - * Represents a Zenoh value. + * Represents a Zenoh reply error. * * Members: - * z_loaned_encoding_t encoding: The encoding of the `payload`. - * z_loaned_bytes_t* payload: The payload of this zenoh value. + * z_loaned_encoding_t encoding: The encoding of the error `payload`. + * z_loaned_bytes_t* payload: The payload of this zenoh reply error. */ -_Z_OWNED_TYPE_PTR(_z_value_t, value) -_Z_LOANED_TYPE(_z_value_t, value) +_Z_OWNED_TYPE_PTR(_z_value_t, reply_err) +_Z_LOANED_TYPE(_z_value_t, reply_err) /** * Represents the configuration used to configure a subscriber upon declaration :c:func:`z_declare_subscriber`. diff --git a/include/zenoh-pico/collections/fifo_mt.h b/include/zenoh-pico/collections/fifo_mt.h index 7a302b1a2..14d2848db 100644 --- a/include/zenoh-pico/collections/fifo_mt.h +++ b/include/zenoh-pico/collections/fifo_mt.h @@ -24,9 +24,9 @@ typedef struct { _z_fifo_t _fifo; #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_t _mutex; - zp_condvar_t _cv_not_full; - zp_condvar_t _cv_not_empty; + z_mutex_t _mutex; + z_condvar_t _cv_not_full; + z_condvar_t _cv_not_empty; #endif } _z_fifo_mt_t; diff --git a/include/zenoh-pico/collections/ring_mt.h b/include/zenoh-pico/collections/ring_mt.h index 8d5be2e8f..a52ee62d6 100644 --- a/include/zenoh-pico/collections/ring_mt.h +++ b/include/zenoh-pico/collections/ring_mt.h @@ -24,8 +24,8 @@ typedef struct { _z_ring_t _ring; #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_t _mutex; - zp_condvar_t _cv_not_empty; + z_mutex_t _mutex; + z_condvar_t _cv_not_empty; #endif } _z_ring_mt_t; diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index 08d8933b6..d7137b3b2 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -271,7 +271,7 @@ * Default session lease expire factor. */ #ifndef Z_TRANSPORT_LEASE_EXPIRE_FACTOR -#define Z_TRANSPORT_LEASE_EXPIRE_FACTOR 3.5 +#define Z_TRANSPORT_LEASE_EXPIRE_FACTOR 3 #endif /** diff --git a/include/zenoh-pico/deprecated/platform.h b/include/zenoh-pico/deprecated/platform.h deleted file mode 100644 index 5e47d65c6..000000000 --- a/include/zenoh-pico/deprecated/platform.h +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright (c) 2024 ZettaScale Technology -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ZettaScale Zenoh Team, - -#ifndef ZENOH_PICO_DEPRECATED_PLATFORM_H -#define ZENOH_PICO_DEPRECATED_PLATFORM_H - -#include - -#include "zenoh-pico/config.h" -#include "zenoh-pico/system/platform-common.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/*------------------ Random ------------------*/ -uint8_t zp_random_u8(void); -uint16_t zp_random_u16(void); -uint32_t zp_random_u32(void); -uint64_t zp_random_u64(void); -void zp_random_fill(void *buf, size_t len); - -/*------------------ Memory ------------------*/ -void *zp_malloc(size_t size); -void *zp_realloc(void *ptr, size_t size); -void zp_free(void *ptr); - -#if Z_FEATURE_MULTI_THREAD == 1 -/*------------------ Thread ------------------*/ -typedef z_task_t zp_task_t; -typedef z_task_attr_t zp_task_attr_t; - -int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), void *arg); -int8_t zp_task_join(zp_task_t *task); -void zp_task_free(zp_task_t **task); - -/*------------------ Mutex ------------------*/ -typedef z_mutex_t zp_mutex_t; -int8_t zp_mutex_init(zp_mutex_t *m); -int8_t zp_mutex_free(zp_mutex_t *m); - -int8_t zp_mutex_lock(zp_mutex_t *m); -int8_t zp_mutex_trylock(zp_mutex_t *m); -int8_t zp_mutex_unlock(zp_mutex_t *m); - -/*------------------ CondVar ------------------*/ -typedef z_condvar_t zp_condvar_t; -int8_t zp_condvar_init(zp_condvar_t *cv); -int8_t zp_condvar_free(zp_condvar_t *cv); - -int8_t zp_condvar_signal(zp_condvar_t *cv); -int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m); -#endif // Z_FEATURE_MULTI_THREAD == 1 - -/*------------------ Sleep ------------------*/ -int zp_sleep_us(size_t time); -int zp_sleep_ms(size_t time); -int zp_sleep_s(size_t time); - -/*------------------ Clock ------------------*/ -typedef z_clock_t zp_clock_t; -zp_clock_t zp_clock_now(void); -unsigned long zp_clock_elapsed_us(zp_clock_t *time); -unsigned long zp_clock_elapsed_ms(zp_clock_t *time); -unsigned long zp_clock_elapsed_s(zp_clock_t *time); - -/*------------------ Time ------------------*/ -typedef z_time_t zp_time_t; -zp_time_t zp_time_now(void); -const char *zp_time_now_as_str(char *const buf, unsigned long buflen); -unsigned long zp_time_elapsed_us(zp_time_t *time); -unsigned long zp_time_elapsed_ms(zp_time_t *time); -unsigned long zp_time_elapsed_s(zp_time_t *time); - -#ifdef __cplusplus -} -#endif - -#endif /* ZENOH_PICO_DEPRECATED_PLATFORM_H */ diff --git a/include/zenoh-pico/link/config/bt.h b/include/zenoh-pico/link/config/bt.h index 0a68898b0..9910e9886 100644 --- a/include/zenoh-pico/link/config/bt.h +++ b/include/zenoh-pico/link/config/bt.h @@ -50,4 +50,4 @@ int8_t _z_bt_config_from_str(_z_str_intmap_t *strint, const char *s); int8_t _z_bt_config_from_strn(_z_str_intmap_t *strint, const char *s, size_t n); #endif -#endif /* ZENOH_PICO_LINK_CONFIG_BT_H */ \ No newline at end of file +#endif /* ZENOH_PICO_LINK_CONFIG_BT_H */ diff --git a/include/zenoh-pico/link/config/serial.h b/include/zenoh-pico/link/config/serial.h index 7eb4408ba..6960d2ea6 100644 --- a/include/zenoh-pico/link/config/serial.h +++ b/include/zenoh-pico/link/config/serial.h @@ -66,4 +66,4 @@ int8_t _z_serial_config_from_str(_z_str_intmap_t *strint, const char *s); int8_t _z_serial_config_from_strn(_z_str_intmap_t *strint, const char *s, size_t n); #endif -#endif /* ZENOH_PICO_LINK_CONFIG_SERIAL_H */ \ No newline at end of file +#endif /* ZENOH_PICO_LINK_CONFIG_SERIAL_H */ diff --git a/include/zenoh-pico/protocol/definitions/core.h b/include/zenoh-pico/protocol/definitions/core.h index 3fd9110b1..5d98e4baf 100644 --- a/include/zenoh-pico/protocol/definitions/core.h +++ b/include/zenoh-pico/protocol/definitions/core.h @@ -44,6 +44,6 @@ #define _Z_FLAGS(h) (_Z_FLAGS_MASK & (h)) #define _Z_HAS_FLAG(h, f) (((h) & (f)) != 0) #define _Z_SET_FLAG(h, f) (h |= f) -#define _Z_CLEAR_FLAG(h, f) (h &= ~(f)) +#define _Z_CLEAR_FLAG(h, f) (h &= (uint8_t)(~(f))) #endif /* INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_CORE_H */ diff --git a/include/zenoh-pico/session/push.h b/include/zenoh-pico/session/push.h index 583746c15..76c02b2f1 100644 --- a/include/zenoh-pico/session/push.h +++ b/include/zenoh-pico/session/push.h @@ -23,4 +23,4 @@ int8_t _z_trigger_push(_z_session_t *zn, _z_n_msg_push_t *push); -#endif /* ZENOH_PICO_SESSION_PUSH_H */ \ No newline at end of file +#endif /* ZENOH_PICO_SESSION_PUSH_H */ diff --git a/include/zenoh-pico/session/reply.h b/include/zenoh-pico/session/reply.h index e84fa5c7e..050f81ee5 100644 --- a/include/zenoh-pico/session/reply.h +++ b/include/zenoh-pico/session/reply.h @@ -26,4 +26,4 @@ int8_t _z_trigger_reply_partial(_z_session_t *zn, _z_zint_t id, _z_keyexpr_t key int8_t _z_trigger_reply_final(_z_session_t *zn, _z_n_msg_response_final_t *final); -#endif /* ZENOH_PICO_SESSION_REPLY_H */ \ No newline at end of file +#endif /* ZENOH_PICO_SESSION_REPLY_H */ diff --git a/include/zenoh-pico/session/resource.h b/include/zenoh-pico/session/resource.h index 7c6ef0cc1..3702a86cd 100644 --- a/include/zenoh-pico/session/resource.h +++ b/include/zenoh-pico/session/resource.h @@ -27,7 +27,7 @@ uint16_t _z_get_resource_id(_z_session_t *zn); _z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint16_t mapping, _z_zint_t rid); _z_resource_t *_z_get_resource_by_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr); _z_keyexpr_t _z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr); -int16_t _z_register_resource(_z_session_t *zn, const _z_keyexpr_t key, uint16_t id, uint16_t register_to_mapping); +uint16_t _z_register_resource(_z_session_t *zn, const _z_keyexpr_t key, uint16_t id, uint16_t register_to_mapping); void _z_unregister_resource(_z_session_t *zn, uint16_t id, uint16_t mapping); void _z_unregister_resources_for_peer(_z_session_t *zn, uint16_t mapping); void _z_flush_resources(_z_session_t *zn); diff --git a/include/zenoh-pico/system/link/raweth.h b/include/zenoh-pico/system/link/raweth.h index 418107f23..c5be5be6a 100644 --- a/include/zenoh-pico/system/link/raweth.h +++ b/include/zenoh-pico/system/link/raweth.h @@ -87,8 +87,8 @@ size_t _z_send_raweth(const _z_sys_net_socket_t *sock, const void *buff, size_t size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_slice_t *addr, const _zp_raweth_whitelist_array_t *whitelist); int8_t _z_close_raweth(_z_sys_net_socket_t *sock); -size_t _z_raweth_ntohs(size_t val); -size_t _z_raweth_htons(size_t val); +uint16_t _z_raweth_ntohs(uint16_t val); +uint16_t _z_raweth_htons(uint16_t val); #endif diff --git a/include/zenoh-pico/system/platform.h b/include/zenoh-pico/system/platform.h index 4fe70198d..a0bf54990 100644 --- a/include/zenoh-pico/system/platform.h +++ b/include/zenoh-pico/system/platform.h @@ -18,7 +18,6 @@ #include #include "zenoh-pico/config.h" -#include "zenoh-pico/deprecated/platform.h" #include "zenoh-pico/system/platform-common.h" #endif /* ZENOH_PICO_SYSTEM_PLATFORM_H */ diff --git a/include/zenoh-pico/utils/result.h b/include/zenoh-pico/utils/result.h index effa199ce..76d9dc583 100644 --- a/include/zenoh-pico/utils/result.h +++ b/include/zenoh-pico/utils/result.h @@ -26,6 +26,7 @@ #define _Z_ERR_SYSTEM_MASK 0xb0 #define _Z_ERR_GENERIC_MASK 0xb8 +typedef int8_t z_error_t; /*------------------ Result Enums ------------------*/ typedef enum { _Z_RES_OK = 0, diff --git a/src/api/api.c b/src/api/api.c index 4d4820db3..acd2da6ba 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -245,20 +245,18 @@ void z_encoding_drop(z_owned_encoding_t *encoding) { z_free(encoding->_val); } -const z_loaned_encoding_t *z_encoding_loan(const z_owned_encoding_t *encoding) { return encoding->_val; } - -// Convert a user owned encoding to an internal encoding, return default encoding if value invalid -static _z_encoding_t _z_encoding_from_owned(const z_owned_encoding_t *encoding) { - if (encoding == NULL) { - return _z_encoding_null(); - } - if (encoding->_val == NULL) { - return _z_encoding_null(); +int8_t z_encoding_clone(z_owned_encoding_t *dst, const z_loaned_encoding_t *src) { + dst->_val = (_z_encoding_t *)z_malloc(sizeof(_z_encoding_t)); + if (dst->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - return *encoding->_val; + _z_encoding_copy(dst->_val, src); + return _Z_RES_OK; } -const z_loaned_bytes_t *z_value_payload(const z_loaned_value_t *value) { return &value->payload; } +const z_loaned_encoding_t *z_encoding_loan(const z_owned_encoding_t *encoding) { return encoding->_val; } + +z_loaned_encoding_t *z_encoding_loan_mut(z_owned_encoding_t *encoding) { return encoding->_val; } const uint8_t *z_slice_data(const z_loaned_slice_t *slice) { return slice->start; } @@ -572,7 +570,6 @@ int8_t zp_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *fir // Calculate pair size size_t first_len = z_slice_len(&first->_val->_slice); size_t second_len = z_slice_len(&second->_val->_slice); - size_t len = 2 * sizeof(uint32_t) + first_len + second_len; // Copy data // FIXME: size endianness, Issue #420 memcpy((uint8_t *)&bytes->_val->_slice.start[*curr_idx], &first_len, sizeof(uint32_t)); @@ -616,12 +613,13 @@ void z_query_parameters(const z_loaned_query_t *query, z_view_string_t *paramete parameters->_val.len = strlen(query->in->val._parameters); } -const z_loaned_value_t *z_query_value(const z_loaned_query_t *query) { return &query->in->val._value; } - const z_loaned_bytes_t *z_query_attachment(const z_loaned_query_t *query) { return &query->in->val.attachment; } const z_loaned_keyexpr_t *z_query_keyexpr(const z_loaned_query_t *query) { return &query->in->val._key; } +const z_loaned_bytes_t *z_query_payload(const z_loaned_query_t *query) { return &query->in->val._value.payload; } +const z_loaned_encoding_t *z_query_encoding(const z_loaned_query_t *query) { return &query->in->val._value.encoding; } + void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_loaned_sample_t *sample) { if (closure->call != NULL) { (closure->call)(sample, closure->context); @@ -678,7 +676,7 @@ static inline void _z_owner_noop_copy(void *dst, const void *src) { _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_config_t, config, _z_owner_noop_copy, _z_config_free) _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_scouting_config_t, scouting_config, _z_owner_noop_copy, _z_scouting_config_free) _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_string_t, string, _z_string_copy, _z_string_free) -_Z_OWNED_FUNCTIONS_PTR_IMPL(_z_value_t, value, _z_value_copy, _z_value_free) +_Z_OWNED_FUNCTIONS_PTR_IMPL(_z_value_t, reply_err, _z_value_copy, _z_value_free) _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_keyexpr_t, keyexpr, _z_keyexpr_copy, _z_keyexpr_free) _Z_VIEW_FUNCTIONS_PTR_IMPL(_z_keyexpr_t, keyexpr) @@ -690,6 +688,8 @@ _Z_VIEW_FUNCTIONS_PTR_IMPL(_z_string_vec_t, string_array) _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_slice_t, slice, _z_slice_copy, _z_slice_free) _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_bytes_t, bytes, _z_bytes_copy, _z_bytes_free) +#if Z_FEATURE_PUBLICATION == 1 || Z_FEATURE_QUERYABLE == 1 || Z_FEATURE_QUERY == 1 +// Convert a user owned bytes payload to an internal bytes payload, returning an empty one if value invalid 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)) { @@ -698,6 +698,18 @@ static _z_bytes_t _z_bytes_from_owned_bytes(z_owned_bytes_t *bytes) { return b; } +// Convert a user owned encoding to an internal encoding, return default encoding if value invalid +static _z_encoding_t _z_encoding_from_owned(const z_owned_encoding_t *encoding) { + if (encoding == NULL) { + return _z_encoding_null(); + } + if (encoding->_val == NULL) { + return _z_encoding_null(); + } + return *encoding->_val; +} +#endif + _Z_OWNED_FUNCTIONS_RC_IMPL(sample) _Z_OWNED_FUNCTIONS_RC_IMPL(session) @@ -753,7 +765,7 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb if (opt_as_str == NULL) { opt_as_str = (char *)Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT; } - uint32_t timeout = strtoul(opt_as_str, NULL, 10); + uint32_t timeout = (uint32_t)strtoul(opt_as_str, NULL, 10); _z_id_t zid = _z_id_empty(); char *zid_str = _z_config_get(config->_val, Z_CONFIG_SESSION_ZID_KEY); @@ -854,6 +866,9 @@ const z_loaned_bytes_t *z_sample_attachment(const z_loaned_sample_t *sample) { return &_Z_RC_IN_VAL(sample).attachment; } +const z_loaned_bytes_t *z_reply_err_payload(const z_loaned_reply_err_t *reply_err) { return &reply_err->payload; } +const z_loaned_encoding_t *z_reply_err_encoding(const z_loaned_reply_err_t *reply_err) { return &reply_err->encoding; } + const char *z_string_data(const z_loaned_string_t *str) { return str->val; } size_t z_string_len(const z_loaned_string_t *str) { return str->len; } @@ -881,8 +896,8 @@ void z_delete_options_default(z_delete_options_t *options) { options->priority = Z_PRIORITY_DEFAULT; } -int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, const uint8_t *payload, - z_zint_t payload_len, const z_put_options_t *options) { +int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_bytes_t *payload, + const z_put_options_t *options) { int8_t ret = 0; z_put_options_t opt; @@ -894,17 +909,18 @@ int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co opt.attachment = options->attachment; } - ret = _z_write(&_Z_RC_IN_VAL(zs), *keyexpr, (const uint8_t *)payload, payload_len, + ret = _z_write(&_Z_RC_IN_VAL(zs), *keyexpr, payload->_val->_slice.start, payload->_val->_slice.len, _z_encoding_from_owned(opt.encoding), Z_SAMPLE_KIND_PUT, opt.congestion_control, opt.priority, _z_bytes_from_owned_bytes(opt.attachment)); // Trigger local subscriptions - _z_trigger_local_subscriptions(&_Z_RC_IN_VAL(zs), *keyexpr, payload, payload_len, + _z_trigger_local_subscriptions(&_Z_RC_IN_VAL(zs), *keyexpr, payload->_val->_slice.start, payload->_val->_slice.len, _z_n_qos_make(0, opt.congestion_control == Z_CONGESTION_CONTROL_BLOCK, opt.priority), _z_bytes_from_owned_bytes(opt.attachment)); // Clean-up z_encoding_drop(opt.encoding); z_bytes_drop(opt.attachment); + z_bytes_drop(payload); return ret; } @@ -979,7 +995,7 @@ void z_publisher_put_options_default(z_publisher_put_options_t *options) { void z_publisher_delete_options_default(z_publisher_delete_options_t *options) { options->__dummy = 0; } -int8_t z_publisher_put(const z_loaned_publisher_t *pub, const uint8_t *payload, size_t len, +int8_t z_publisher_put(const z_loaned_publisher_t *pub, z_owned_bytes_t *payload, const z_publisher_put_options_t *options) { int8_t ret = 0; // Build options @@ -992,16 +1008,17 @@ int8_t z_publisher_put(const z_loaned_publisher_t *pub, const uint8_t *payload, // Check if write filter is active before writing if (!_z_write_filter_active(pub)) { // Write value - ret = _z_write(&pub->_zn.in->val, pub->_key, payload, len, _z_encoding_from_owned(opt.encoding), - Z_SAMPLE_KIND_PUT, pub->_congestion_control, pub->_priority, - _z_bytes_from_owned_bytes(opt.attachment)); + ret = _z_write(&pub->_zn.in->val, pub->_key, payload->_val->_slice.start, payload->_val->_slice.len, + _z_encoding_from_owned(opt.encoding), Z_SAMPLE_KIND_PUT, pub->_congestion_control, + pub->_priority, _z_bytes_from_owned_bytes(opt.attachment)); } // Trigger local subscriptions - _z_trigger_local_subscriptions(&pub->_zn.in->val, pub->_key, payload, len, _Z_N_QOS_DEFAULT, - _z_bytes_from_owned_bytes(opt.attachment)); + _z_trigger_local_subscriptions(&pub->_zn.in->val, pub->_key, payload->_val->_slice.start, payload->_val->_slice.len, + _Z_N_QOS_DEFAULT, _z_bytes_from_owned_bytes(opt.attachment)); // Clean-up z_encoding_drop(opt.encoding); z_bytes_drop(opt.attachment); + z_bytes_drop(payload); return ret; } @@ -1081,7 +1098,7 @@ _Bool z_reply_is_ok(const z_loaned_reply_t *reply) { const z_loaned_sample_t *z_reply_ok(const z_loaned_reply_t *reply) { return &reply->in->val.data.sample; } -const z_loaned_value_t *z_reply_err(const z_loaned_reply_t *reply) { +const z_loaned_reply_err_t *z_reply_err(const z_loaned_reply_t *reply) { _ZP_UNUSED(reply); return NULL; } @@ -1230,7 +1247,7 @@ int8_t z_declare_subscriber(z_owned_subscriber_t *sub, const z_loaned_session_t _z_keyexpr_t resource_key = *keyexpr; if (wild != NULL && wild != resource_key._suffix) { wild -= 1; - size_t len = wild - resource_key._suffix; + size_t len = (size_t)(wild - resource_key._suffix); suffix = z_malloc(len + 1); if (suffix != NULL) { memcpy(suffix, resource_key._suffix, len); diff --git a/src/api/handlers.c b/src/api/handlers.c deleted file mode 100644 index c136e97b0..000000000 --- a/src/api/handlers.c +++ /dev/null @@ -1,79 +0,0 @@ -// -// Copyright (c) 2024 ZettaScale Technology -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ZettaScale Zenoh Team, -// - -#include "zenoh-pico/api/handlers.h" - -#include "zenoh-pico/net/sample.h" -#include "zenoh-pico/system/platform.h" - -// -- Sample -void _z_owned_sample_move(z_owned_sample_t *dst, z_owned_sample_t *src) { - memcpy(dst, src, sizeof(z_owned_sample_t)); - zp_free(src); -} - -z_owned_sample_t *_z_sample_to_owned_ptr(const z_loaned_sample_t *src) { - z_owned_sample_t *dst = (z_owned_sample_t *)zp_malloc(sizeof(z_owned_sample_t)); - if (dst == NULL) { - return NULL; - } - if (src == NULL) { - dst->_rc.in = NULL; - } else { - _z_sample_rc_copy(&dst->_rc, src); - } - return dst; -} - -#if Z_FEATURE_QUERYABLE == 1 -// -- Query -void _z_owned_query_move(z_owned_query_t *dst, z_owned_query_t *src) { - memcpy(dst, src, sizeof(z_owned_query_t)); - zp_free(src); -} - -z_owned_query_t *_z_query_to_owned_ptr(const z_loaned_query_t *src) { - z_owned_query_t *dst = (z_owned_query_t *)zp_malloc(sizeof(z_owned_query_t)); - if (dst == NULL) { - return NULL; - } - if (src == NULL) { - dst->_rc.in = NULL; - } else { - _z_query_rc_copy(&dst->_rc, src); - } - return dst; -} -#endif // Z_FEATURE_QUERYABLE - -#if Z_FEATURE_QUERY == 1 -// -- Reply -void _z_owned_reply_move(z_owned_reply_t *dst, z_owned_reply_t *src) { - memcpy(dst, src, sizeof(z_owned_reply_t)); - zp_free(src); -} - -z_owned_reply_t *_z_reply_to_owned_ptr(const z_loaned_reply_t *src) { - z_owned_reply_t *dst = (z_owned_reply_t *)zp_malloc(sizeof(z_owned_reply_t)); - if (dst == NULL) { - return NULL; - } - if (src == NULL) { - dst->_rc.in = NULL; - } else { - _z_reply_rc_copy(&dst->_rc, src); - } - return dst; -} -#endif // Z_FEATURE_QUERY diff --git a/src/collections/fifo.c b/src/collections/fifo.c index 463c7f9db..914cf2752 100644 --- a/src/collections/fifo.c +++ b/src/collections/fifo.c @@ -47,7 +47,7 @@ void _z_fifo_free(_z_fifo_t **r, z_element_free_f free_f) { _z_fifo_t *ptr = (_z_fifo_t *)*r; if (ptr != NULL) { _z_fifo_clear(ptr, free_f); - zp_free(ptr); + z_free(ptr); *r = NULL; } } diff --git a/src/collections/fifo_mt.c b/src/collections/fifo_mt.c index d41a6675d..072fd6cc2 100644 --- a/src/collections/fifo_mt.c +++ b/src/collections/fifo_mt.c @@ -23,25 +23,25 @@ int8_t _z_fifo_mt_init(_z_fifo_mt_t *fifo, size_t capacity) { _Z_RETURN_IF_ERR(_z_fifo_init(&fifo->_fifo, capacity)) #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(zp_mutex_init(&fifo->_mutex)) - _Z_RETURN_IF_ERR(zp_condvar_init(&fifo->_cv_not_full)) - _Z_RETURN_IF_ERR(zp_condvar_init(&fifo->_cv_not_empty)) + _Z_RETURN_IF_ERR(z_mutex_init(&fifo->_mutex)) + _Z_RETURN_IF_ERR(z_condvar_init(&fifo->_cv_not_full)) + _Z_RETURN_IF_ERR(z_condvar_init(&fifo->_cv_not_empty)) #endif return _Z_RES_OK; } _z_fifo_mt_t *_z_fifo_mt_new(size_t capacity) { - _z_fifo_mt_t *fifo = (_z_fifo_mt_t *)zp_malloc(sizeof(_z_fifo_mt_t)); + _z_fifo_mt_t *fifo = (_z_fifo_mt_t *)z_malloc(sizeof(_z_fifo_mt_t)); if (fifo == NULL) { - _Z_ERROR("zp_malloc failed"); + _Z_ERROR("z_malloc failed"); return NULL; } int8_t ret = _z_fifo_mt_init(fifo, capacity); if (ret != _Z_RES_OK) { _Z_ERROR("_z_fifo_mt_init failed: %i", ret); - zp_free(fifo); + z_free(fifo); return NULL; } @@ -50,9 +50,9 @@ _z_fifo_mt_t *_z_fifo_mt_new(size_t capacity) { void _z_fifo_mt_clear(_z_fifo_mt_t *fifo, z_element_free_f free_f) { #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_free(&fifo->_mutex); - zp_condvar_free(&fifo->_cv_not_full); - zp_condvar_free(&fifo->_cv_not_empty); + z_mutex_free(&fifo->_mutex); + z_condvar_free(&fifo->_cv_not_full); + z_condvar_free(&fifo->_cv_not_empty); #endif _z_fifo_clear(&fifo->_fifo, free_f); @@ -60,10 +60,11 @@ void _z_fifo_mt_clear(_z_fifo_mt_t *fifo, z_element_free_f free_f) { void _z_fifo_mt_free(_z_fifo_mt_t *fifo, z_element_free_f free_f) { _z_fifo_mt_clear(fifo, free_f); - zp_free(fifo); + z_free(fifo); } int8_t _z_fifo_mt_push(const void *elem, void *context, z_element_free_f element_free) { + _ZP_UNUSED(element_free); if (elem == NULL || context == NULL) { return _Z_ERR_GENERIC; } @@ -71,16 +72,16 @@ int8_t _z_fifo_mt_push(const void *elem, void *context, z_element_free_f element _z_fifo_mt_t *f = (_z_fifo_mt_t *)context; #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(zp_mutex_lock(&f->_mutex)) + _Z_RETURN_IF_ERR(z_mutex_lock(&f->_mutex)) while (elem != NULL) { elem = _z_fifo_push(&f->_fifo, (void *)elem); if (elem != NULL) { - _Z_RETURN_IF_ERR(zp_condvar_wait(&f->_cv_not_full, &f->_mutex)) + _Z_RETURN_IF_ERR(z_condvar_wait(&f->_cv_not_full, &f->_mutex)) } else { - _Z_RETURN_IF_ERR(zp_condvar_signal(&f->_cv_not_empty)) + _Z_RETURN_IF_ERR(z_condvar_signal(&f->_cv_not_empty)) } } - _Z_RETURN_IF_ERR(zp_mutex_unlock(&f->_mutex)) + _Z_RETURN_IF_ERR(z_mutex_unlock(&f->_mutex)) #else // Z_FEATURE_MULTI_THREAD == 1 _z_fifo_push_drop(&f->_fifo, (void *)elem, element_free); #endif // Z_FEATURE_MULTI_THREAD == 1 @@ -93,16 +94,16 @@ int8_t _z_fifo_mt_pull(void *dst, void *context, z_element_move_f element_move) #if Z_FEATURE_MULTI_THREAD == 1 void *src = NULL; - _Z_RETURN_IF_ERR(zp_mutex_lock(&f->_mutex)) + _Z_RETURN_IF_ERR(z_mutex_lock(&f->_mutex)) while (src == NULL) { src = _z_fifo_pull(&f->_fifo); if (src == NULL) { - _Z_RETURN_IF_ERR(zp_condvar_wait(&f->_cv_not_empty, &f->_mutex)) + _Z_RETURN_IF_ERR(z_condvar_wait(&f->_cv_not_empty, &f->_mutex)) } else { - _Z_RETURN_IF_ERR(zp_condvar_signal(&f->_cv_not_full)) + _Z_RETURN_IF_ERR(z_condvar_signal(&f->_cv_not_full)) } } - _Z_RETURN_IF_ERR(zp_mutex_unlock(&f->_mutex)) + _Z_RETURN_IF_ERR(z_mutex_unlock(&f->_mutex)) element_move(dst, src); #else // Z_FEATURE_MULTI_THREAD == 1 void *src = _z_fifo_pull(&f->_fifo); @@ -119,12 +120,12 @@ int8_t _z_fifo_mt_try_pull(void *dst, void *context, z_element_move_f element_mo #if Z_FEATURE_MULTI_THREAD == 1 void *src = NULL; - _Z_RETURN_IF_ERR(zp_mutex_lock(&f->_mutex)) + _Z_RETURN_IF_ERR(z_mutex_lock(&f->_mutex)) src = _z_fifo_pull(&f->_fifo); if (src != NULL) { - _Z_RETURN_IF_ERR(zp_condvar_signal(&f->_cv_not_full)) + _Z_RETURN_IF_ERR(z_condvar_signal(&f->_cv_not_full)) } - _Z_RETURN_IF_ERR(zp_mutex_unlock(&f->_mutex)) + _Z_RETURN_IF_ERR(z_mutex_unlock(&f->_mutex)) #else // Z_FEATURE_MULTI_THREAD == 1 void *src = _z_fifo_pull(&f->_fifo); #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/src/collections/lifo.c b/src/collections/lifo.c index 6cccca8fa..4f31a22cf 100644 --- a/src/collections/lifo.c +++ b/src/collections/lifo.c @@ -21,7 +21,7 @@ int8_t _z_lifo_init(_z_lifo_t *r, size_t capacity) { memset(r, 0, sizeof(_z_lifo_t)); if (capacity != (size_t)0) { - r->_val = (void **)zp_malloc(sizeof(void *) * capacity); + r->_val = (void **)z_malloc(sizeof(void *) * capacity); } if (r->_val != NULL) { memset(r->_val, 0, capacity); @@ -73,7 +73,7 @@ void _z_lifo_clear(_z_lifo_t *r, z_element_free_f free_f) { free_f(&e); e = _z_lifo_pull(r); } - zp_free(r->_val); + z_free(r->_val); r->_val = NULL; r->_capacity = (size_t)0; @@ -84,7 +84,7 @@ void _z_lifo_free(_z_lifo_t **r, z_element_free_f free_f) { _z_lifo_t *ptr = (_z_lifo_t *)*r; if (ptr != NULL) { _z_lifo_clear(ptr, free_f); - zp_free(ptr); + z_free(ptr); *r = NULL; } } diff --git a/src/collections/ring.c b/src/collections/ring.c index ce3e90894..66adb6c89 100644 --- a/src/collections/ring.c +++ b/src/collections/ring.c @@ -25,7 +25,7 @@ int8_t _z_ring_init(_z_ring_t *r, size_t capacity) { memset(r, 0, sizeof(_z_ring_t)); if (capacity != (size_t)0) { - r->_val = (void **)zp_malloc(sizeof(void *) * capacity); + r->_val = (void **)z_malloc(sizeof(void *) * capacity); } if (r->_val != NULL) { memset(r->_val, 0, capacity); @@ -96,7 +96,7 @@ void _z_ring_clear(_z_ring_t *r, z_element_free_f free_f) { free_f(&e); e = _z_ring_pull(r); } - zp_free(r->_val); + z_free(r->_val); r->_val = NULL; r->_capacity = (size_t)0; @@ -109,7 +109,7 @@ void _z_ring_free(_z_ring_t **r, z_element_free_f free_f) { _z_ring_t *ptr = (_z_ring_t *)*r; if (ptr != NULL) { _z_ring_clear(ptr, free_f); - zp_free(ptr); + z_free(ptr); *r = NULL; } } diff --git a/src/collections/ring_mt.c b/src/collections/ring_mt.c index f28c432c9..c3b68953f 100644 --- a/src/collections/ring_mt.c +++ b/src/collections/ring_mt.c @@ -23,16 +23,16 @@ int8_t _z_ring_mt_init(_z_ring_mt_t *ring, size_t capacity) { _Z_RETURN_IF_ERR(_z_ring_init(&ring->_ring, capacity)) #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(zp_mutex_init(&ring->_mutex)) - _Z_RETURN_IF_ERR(zp_condvar_init(&ring->_cv_not_empty)) + _Z_RETURN_IF_ERR(z_mutex_init(&ring->_mutex)) + _Z_RETURN_IF_ERR(z_condvar_init(&ring->_cv_not_empty)) #endif return _Z_RES_OK; } _z_ring_mt_t *_z_ring_mt_new(size_t capacity) { - _z_ring_mt_t *ring = (_z_ring_mt_t *)zp_malloc(sizeof(_z_ring_mt_t)); + _z_ring_mt_t *ring = (_z_ring_mt_t *)z_malloc(sizeof(_z_ring_mt_t)); if (ring == NULL) { - _Z_ERROR("zp_malloc failed"); + _Z_ERROR("z_malloc failed"); return NULL; } @@ -47,8 +47,8 @@ _z_ring_mt_t *_z_ring_mt_new(size_t capacity) { void _z_ring_mt_clear(_z_ring_mt_t *ring, z_element_free_f free_f) { #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_free(&ring->_mutex); - zp_condvar_free(&ring->_cv_not_empty); + z_mutex_free(&ring->_mutex); + z_condvar_free(&ring->_cv_not_empty); #endif _z_ring_clear(&ring->_ring, free_f); @@ -57,7 +57,7 @@ void _z_ring_mt_clear(_z_ring_mt_t *ring, z_element_free_f free_f) { void _z_ring_mt_free(_z_ring_mt_t *ring, z_element_free_f free_f) { _z_ring_mt_clear(ring, free_f); - zp_free(ring); + z_free(ring); } int8_t _z_ring_mt_push(const void *elem, void *context, z_element_free_f element_free) { @@ -68,14 +68,14 @@ int8_t _z_ring_mt_push(const void *elem, void *context, z_element_free_f element _z_ring_mt_t *r = (_z_ring_mt_t *)context; #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(zp_mutex_lock(&r->_mutex)) + _Z_RETURN_IF_ERR(z_mutex_lock(&r->_mutex)) #endif _z_ring_push_force_drop(&r->_ring, (void *)elem, element_free); #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(zp_condvar_signal(&r->_cv_not_empty)) - _Z_RETURN_IF_ERR(zp_mutex_unlock(&r->_mutex)) + _Z_RETURN_IF_ERR(z_condvar_signal(&r->_cv_not_empty)) + _Z_RETURN_IF_ERR(z_mutex_unlock(&r->_mutex)) #endif return _Z_RES_OK; } @@ -85,14 +85,14 @@ int8_t _z_ring_mt_pull(void *dst, void *context, z_element_move_f element_move) #if Z_FEATURE_MULTI_THREAD == 1 void *src = NULL; - _Z_RETURN_IF_ERR(zp_mutex_lock(&r->_mutex)) + _Z_RETURN_IF_ERR(z_mutex_lock(&r->_mutex)) while (src == NULL) { src = _z_ring_pull(&r->_ring); if (src == NULL) { - _Z_RETURN_IF_ERR(zp_condvar_wait(&r->_cv_not_empty, &r->_mutex)) + _Z_RETURN_IF_ERR(z_condvar_wait(&r->_cv_not_empty, &r->_mutex)) } } - _Z_RETURN_IF_ERR(zp_mutex_unlock(&r->_mutex)) + _Z_RETURN_IF_ERR(z_mutex_unlock(&r->_mutex)) element_move(dst, src); #else // Z_FEATURE_MULTI_THREAD == 1 void *src = _z_ring_pull(&r->_ring); @@ -108,13 +108,13 @@ int8_t _z_ring_mt_try_pull(void *dst, void *context, z_element_move_f element_mo _z_ring_mt_t *r = (_z_ring_mt_t *)context; #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(zp_mutex_lock(&r->_mutex)) + _Z_RETURN_IF_ERR(z_mutex_lock(&r->_mutex)) #endif void *src = _z_ring_pull(&r->_ring); #if Z_FEATURE_MULTI_THREAD == 1 - _Z_RETURN_IF_ERR(zp_mutex_unlock(&r->_mutex)) + _Z_RETURN_IF_ERR(z_mutex_unlock(&r->_mutex)) #endif if (src != NULL) { diff --git a/src/collections/slice.c b/src/collections/slice.c index 1fa0c2472..bd61078e4 100644 --- a/src/collections/slice.c +++ b/src/collections/slice.c @@ -276,4 +276,4 @@ _z_bytes_t _z_bytes_from_double(double val) { // Encode double memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); return ret; -} \ No newline at end of file +} diff --git a/src/deprecated/platform.c b/src/deprecated/platform.c deleted file mode 100644 index 4081d5b1e..000000000 --- a/src/deprecated/platform.c +++ /dev/null @@ -1,63 +0,0 @@ -// -// Copyright (c) 2024 ZettaScale Technology -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ZettaScale Zenoh Team, -// - -#include "zenoh-pico/deprecated/platform.h" - -#include "zenoh-pico/system/platform-common.h" - -uint8_t zp_random_u8(void) { return z_random_u8(); }; -uint16_t zp_random_u16(void) { return z_random_u16(); }; -uint32_t zp_random_u32(void) { return z_random_u32(); }; -uint64_t zp_random_u64(void) { return z_random_u64(); }; -void zp_random_fill(void *buf, size_t len) { return z_random_fill(buf, len); }; - -void *zp_malloc(size_t size) { return z_malloc(size); }; -void *zp_realloc(void *ptr, size_t size) { return z_realloc(ptr, size); }; -void zp_free(void *ptr) { return z_free(ptr); }; - -#if Z_FEATURE_MULTI_THREAD == 1 - -int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), void *arg) { - return z_task_init(task, attr, fun, arg); -}; -int8_t zp_task_join(zp_task_t *task) { return z_task_join(task); }; -void zp_task_free(zp_task_t **task) { return z_task_free(task); }; - -int8_t zp_mutex_init(zp_mutex_t *m) { return z_mutex_init(m); }; -int8_t zp_mutex_free(zp_mutex_t *m) { return z_mutex_free(m); }; -int8_t zp_mutex_lock(zp_mutex_t *m) { return z_mutex_lock(m); }; -int8_t zp_mutex_trylock(zp_mutex_t *m) { return z_mutex_trylock(m); }; -int8_t zp_mutex_unlock(zp_mutex_t *m) { return z_mutex_unlock(m); }; - -int8_t zp_condvar_init(zp_condvar_t *cv) { return z_condvar_init(cv); }; -int8_t zp_condvar_free(zp_condvar_t *cv) { return z_condvar_free(cv); }; - -int8_t zp_condvar_signal(zp_condvar_t *cv) { return z_condvar_signal(cv); }; -int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m) { return z_condvar_wait(cv, m); }; -#endif // Z_FEATURE_MULTI_THREAD == 1 - -int zp_sleep_us(size_t time) { return z_sleep_us(time); }; -int zp_sleep_ms(size_t time) { return z_sleep_ms(time); }; -int zp_sleep_s(size_t time) { return z_sleep_s(time); }; - -zp_clock_t zp_clock_now(void) { return z_clock_now(); }; -unsigned long zp_clock_elapsed_us(zp_clock_t *time) { return z_clock_elapsed_us(time); }; -unsigned long zp_clock_elapsed_ms(zp_clock_t *time) { return z_clock_elapsed_ms(time); }; -unsigned long zp_clock_elapsed_s(zp_clock_t *time) { return z_clock_elapsed_s(time); }; - -zp_time_t zp_time_now(void) { return z_time_now(); }; -const char *zp_time_now_as_str(char *const buf, unsigned long buflen) { return z_time_now_as_str(buf, buflen); }; -unsigned long zp_time_elapsed_us(zp_time_t *time) { return z_time_elapsed_us(time); }; -unsigned long zp_time_elapsed_ms(zp_time_t *time) { return z_time_elapsed_ms(time); }; -unsigned long zp_time_elapsed_s(zp_time_t *time) { return z_time_elapsed_s(time); }; diff --git a/src/link/config/ws.c b/src/link/config/ws.c index 14e4c113c..dd4378e6a 100644 --- a/src/link/config/ws.c +++ b/src/link/config/ws.c @@ -28,8 +28,7 @@ size_t _z_ws_config_strlen(const _z_str_intmap_t *s) { void _z_ws_config_onto_str(char *dst, size_t dst_len, const _z_str_intmap_t *s) { WS_CONFIG_MAPPING_BUILD - - return _z_str_intmap_onto_str(dst, dst_len, s, argc, args); + _z_str_intmap_onto_str(dst, dst_len, s, argc, args); } char *_z_ws_config_to_str(const _z_str_intmap_t *s) { diff --git a/src/link/multicast/udp.c b/src/link/multicast/udp.c index 0e08eff58..ef1aab5ca 100644 --- a/src/link/multicast/udp.c +++ b/src/link/multicast/udp.c @@ -93,7 +93,7 @@ int8_t _z_endpoint_udp_multicast_valid(_z_endpoint_t *endpoint) { if (s_port == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } else { - uint32_t port = strtoul(s_port, NULL, 10); + uint32_t port = (uint32_t)strtoul(s_port, NULL, 10); if ((port < (uint32_t)1) || (port > (uint32_t)65355)) { // Port numbers should range from 1 to 65355 ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } @@ -115,7 +115,7 @@ int8_t _z_f_link_open_udp_multicast(_z_link_t *self) { uint32_t tout = Z_CONFIG_SOCKET_TIMEOUT; char *tout_as_str = _z_str_intmap_get(&self->_endpoint._config, UDP_CONFIG_TOUT_KEY); if (tout_as_str != NULL) { - tout = strtoul(tout_as_str, NULL, 10); + tout = (uint32_t)strtoul(tout_as_str, NULL, 10); } const char *iface = _z_str_intmap_get(&self->_endpoint._config, UDP_CONFIG_IFACE_KEY); diff --git a/src/link/unicast/tcp.c b/src/link/unicast/tcp.c index fe006cdc8..c1b54becf 100644 --- a/src/link/unicast/tcp.c +++ b/src/link/unicast/tcp.c @@ -93,7 +93,7 @@ int8_t _z_endpoint_tcp_valid(_z_endpoint_t *endpoint) { if (s_port == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } else { - uint32_t port = strtoul(s_port, NULL, 10); + uint32_t port = (uint32_t)strtoul(s_port, NULL, 10); if ((port < (uint32_t)1) || (port > (uint32_t)65355)) { // Port numbers should range from 1 to 65355 ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } @@ -110,7 +110,7 @@ int8_t _z_f_link_open_tcp(_z_link_t *zl) { uint32_t tout = Z_CONFIG_SOCKET_TIMEOUT; char *tout_as_str = _z_str_intmap_get(&zl->_endpoint._config, TCP_CONFIG_TOUT_KEY); if (tout_as_str != NULL) { - tout = strtoul(tout_as_str, NULL, 10); + tout = (uint32_t)strtoul(tout_as_str, NULL, 10); } ret = _z_open_tcp(&zl->_socket._tcp._sock, zl->_socket._tcp._rep, tout); diff --git a/src/link/unicast/udp.c b/src/link/unicast/udp.c index b76b832e6..d30418727 100644 --- a/src/link/unicast/udp.c +++ b/src/link/unicast/udp.c @@ -93,7 +93,7 @@ int8_t _z_endpoint_udp_unicast_valid(_z_endpoint_t *endpoint) { if (s_port == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } else { - uint32_t port = strtoul(s_port, NULL, 10); + uint32_t port = (uint32_t)strtoul(s_port, NULL, 10); if ((port < (uint32_t)1) || (port > (uint32_t)65355)) { // Port numbers should range from 1 to 65355 ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } @@ -110,7 +110,7 @@ int8_t _z_f_link_open_udp_unicast(_z_link_t *self) { uint32_t tout = Z_CONFIG_SOCKET_TIMEOUT; char *tout_as_str = _z_str_intmap_get(&self->_endpoint._config, UDP_CONFIG_TOUT_KEY); if (tout_as_str != NULL) { - tout = strtoul(tout_as_str, NULL, 10); + tout = (uint32_t)strtoul(tout_as_str, NULL, 10); } ret = _z_open_udp_unicast(&self->_socket._udp._sock, self->_socket._udp._rep, tout); @@ -124,7 +124,7 @@ int8_t _z_f_link_listen_udp_unicast(_z_link_t *self) { uint32_t tout = Z_CONFIG_SOCKET_TIMEOUT; char *tout_as_str = _z_str_intmap_get(&self->_endpoint._config, UDP_CONFIG_TOUT_KEY); if (tout_as_str != NULL) { - tout = strtoul(tout_as_str, NULL, 10); + tout = (uint32_t)strtoul(tout_as_str, NULL, 10); } ret = _z_listen_udp_unicast(&self->_socket._udp._sock, self->_socket._udp._rep, tout); diff --git a/src/net/encoding.c b/src/net/encoding.c index 782d3dc3a..406d10437 100644 --- a/src/net/encoding.c +++ b/src/net/encoding.c @@ -41,7 +41,7 @@ _z_encoding_t _z_encoding_wrap(z_encoding_id_t id, const char *schema) { _z_encoding_t _z_encoding_null(void) { return _z_encoding_wrap(Z_ENCODING_ID_DEFAULT, NULL); } -void _z_encoding_clear(_z_encoding_t *encoding) { _z_slice_clear(&encoding->schema); }; +void _z_encoding_clear(_z_encoding_t *encoding) { _z_slice_clear(&encoding->schema); } _Bool _z_encoding_check(const _z_encoding_t *encoding) { return ((encoding->id != Z_ENCODING_ID_DEFAULT) || _z_slice_check(encoding->schema)); diff --git a/src/net/primitives.c b/src/net/primitives.c index 35476ccf0..f50cbd611 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -58,8 +58,8 @@ void _z_scout(const z_what_t what, const _z_id_t zid, const char *locator, const uint16_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr) { uint16_t ret = Z_RESOURCE_ID_NONE; - if (zn->_tp._type == - _Z_TRANSPORT_UNICAST_TYPE) { // FIXME: remove when resource declaration is implemented for multicast transport + // FIXME: remove this check when resource declaration is implemented for multicast transport + if (zn->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { uint16_t id = _z_register_resource(zn, keyexpr, 0, _Z_KEYEXPR_MAPPING_LOCAL); if (id != 0) { // Build the declare message to send on the wire diff --git a/src/net/sample.c b/src/net/sample.c index 7ceca0f2f..9db7355e3 100644 --- a/src/net/sample.c +++ b/src/net/sample.c @@ -88,7 +88,7 @@ _z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_slice_t *payload, s.kind = kind; s.timestamp = timestamp; s.qos = qos; - s.attachment = _z_bytes_duplicate(&att); + s.attachment._slice = _z_slice_steal((_z_slice_t *)&att._slice); return s; } #else diff --git a/src/net/session.c b/src/net/session.c index 0300584d2..373485da1 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -85,7 +85,7 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { if (opt_as_str == NULL) { opt_as_str = (char *)Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT; } - uint32_t timeout = strtoul(opt_as_str, NULL, 10); + uint32_t timeout = (uint32_t)strtoul(opt_as_str, NULL, 10); // Scout and return upon the first result _z_hello_list_t *hellos = _z_scout_inner(what, zid, mcast_locator, timeout, true); @@ -95,7 +95,7 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { } _z_hello_list_free(&hellos); } else { - int key = Z_CONFIG_CONNECT_KEY; + uint_fast8_t key = Z_CONFIG_CONNECT_KEY; if (listen != NULL) { if (connect == NULL) { key = Z_CONFIG_LISTEN_KEY; diff --git a/src/protocol/codec.c b/src/protocol/codec.c index e92ea138e..cb0c19a12 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -76,8 +76,8 @@ int8_t _z_uint8_decode(uint8_t *u8, _z_zbuf_t *zbf) { int8_t _z_uint16_encode(_z_wbuf_t *wbf, uint16_t u16) { int8_t ret = _Z_RES_OK; - ret |= _z_wbuf_write(wbf, (u16 & 0xFF)); - ret |= _z_wbuf_write(wbf, ((u16 >> 8) & 0xFF)); + ret |= _z_wbuf_write(wbf, (u16 & 0xFFU)); + ret |= _z_wbuf_write(wbf, ((u16 >> 8) & 0xFFU)); return ret; } diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index aec401587..fb11ef461 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -323,6 +323,8 @@ int8_t _z_undecl_token_decode(_z_undecl_token_t *decl, _z_zbuf_t *zbf, uint8_t h } int8_t _z_decl_final_decode(_z_decl_final_t *decl, _z_zbuf_t *zbf, uint8_t header) { + // Nothing to do + _ZP_UNUSED(decl); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_skip_non_mandatories(zbf, 0x13)); } diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 592323756..139395e2a 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -93,7 +93,9 @@ int8_t _z_timestamp_encode(_z_wbuf_t *wbf, const _z_timestamp_t *ts) { return ret; } int8_t _z_timestamp_encode_ext(_z_wbuf_t *wbf, const _z_timestamp_t *ts) { - _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, _z_zint_len(ts->time) + (uint8_t)1 + _z_id_len(ts->id))); + // Encode extension size then timestamp + size_t ext_size = (size_t)(_z_zint_len(ts->time) + 1 + _z_id_len(ts->id)); + _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, ext_size)); return _z_timestamp_encode(wbf, ts); } @@ -233,8 +235,8 @@ int8_t _z_source_info_encode(_z_wbuf_t *wbf, const _z_source_info_t *info) { int8_t _z_source_info_encode_ext(_z_wbuf_t *wbf, const _z_source_info_t *info) { int8_t ret = 0; uint8_t zidlen = _z_id_len(info->_id); - uint16_t len = 1 + zidlen + _z_zint_len(info->_entity_id) + _z_zint_len(info->_source_sn); - _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, len)); + size_t ext_size = 1u + zidlen + _z_zint_len(info->_entity_id) + _z_zint_len(info->_source_sn); + _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, ext_size)); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, zidlen << 4)); _z_slice_t zid = _z_slice_wrap(info->_id.id, zidlen); _Z_RETURN_IF_ERR(_z_slice_val_encode(wbf, &zid)); @@ -497,6 +499,7 @@ int8_t _z_reply_encode(_z_wbuf_t *wbf, const _z_msg_reply_t *reply) { return _Z_RES_OK; } int8_t _z_reply_decode_extension(_z_msg_ext_t *extension, void *ctx) { + _ZP_UNUSED(ctx); int8_t ret = _Z_RES_OK; switch (_Z_EXT_FULL_ID(extension->_header)) { default: @@ -595,7 +598,7 @@ int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_scout_t *m uint8_t zid_len = _z_id_len(msg->_zid); if (zid_len > 0) { _Z_SET_FLAG(cbyte, _Z_FLAG_T_SCOUT_I); - cbyte |= ((zid_len - 1) & 0x0F) << 4; + cbyte |= (uint8_t)(((zid_len - 1) & 0x0F) << 4); } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, cbyte)) @@ -633,7 +636,7 @@ int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *m uint8_t zidlen = _z_id_len(msg->_zid); uint8_t cbyte = 0; cbyte |= (msg->_whatami & 0x03); - cbyte |= ((zidlen - 1) & 0x0F) << 4; + cbyte |= (uint8_t)(((zidlen - 1) & 0x0F) << 4); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, cbyte)) _Z_RETURN_IF_ERR(_z_slice_val_encode(wbf, &(_z_slice_t){.start = msg->_zid.id, .len = zidlen, ._is_alloc = false})); diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index a676cb595..efee7bcfc 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -75,7 +75,7 @@ int8_t _z_push_decode_ext_cb(_z_msg_ext_t *extension, void *ctx) { if (extension->_body._zint._val > UINT32_MAX) { return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } - msg->_qos = (_z_n_qos_t){._val = (uint32_t)extension->_body._zint._val}; + msg->_qos = (_z_n_qos_t){._val = (uint8_t)extension->_body._zint._val}; break; } case _Z_MSG_EXT_ENC_ZBUF | 0x02: { // Timestamp ext @@ -246,7 +246,7 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { _Bool has_qos_ext = msg->_ext_qos._val != _Z_N_QOS_DEFAULT._val; _Bool has_ts_ext = _z_timestamp_check(&msg->_ext_timestamp); _Bool has_responder_ext = _z_id_check(msg->_ext_responder._zid) || msg->_ext_responder._eid != 0; - uint8_t n_ext = (has_qos_ext ? 1 : 0) + (has_ts_ext ? 1 : 0) + (has_responder_ext ? 1 : 0); + int n_ext = (has_qos_ext ? 1 : 0) + (has_ts_ext ? 1 : 0) + (has_responder_ext ? 1 : 0); _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); if (_z_keyexpr_is_local(&msg->_key)) { _Z_SET_FLAG(header, _Z_FLAG_N_RESPONSE_M); @@ -284,8 +284,9 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x03 | (n_ext != 0 ? _Z_FLAG_Z_Z : 0); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); uint8_t zidlen = _z_id_len(msg->_ext_responder._zid); - extheader = (zidlen - 1) << 4; - _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, zidlen + (uint8_t)1 + _z_zint_len(msg->_ext_responder._eid))); + extheader = (uint8_t)((zidlen - 1) << 4); + size_t ext_size = (size_t)(zidlen + 1 + _z_zint_len(msg->_ext_responder._eid)); + _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, ext_size)); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, msg->_ext_responder._zid.id, 0, zidlen)); _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, msg->_ext_responder._eid)); @@ -397,8 +398,8 @@ int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_n_msg_declare_t *decl) { uint8_t header = _Z_MID_N_DECLARE; _Bool has_qos_ext = decl->_ext_qos._val != _Z_N_QOS_DEFAULT._val; _Bool has_timestamp_ext = _z_timestamp_check(&decl->_ext_timestamp); - uint8_t n = (has_qos_ext ? 1 : 0) + (has_timestamp_ext ? 1 : 0); - if (n != 0) { + int n_ext = (has_qos_ext ? 1 : 0) + (has_timestamp_ext ? 1 : 0); + if (n_ext != 0) { header |= _Z_FLAG_N_Z; } if (decl->has_interest_id) { @@ -412,13 +413,13 @@ int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_n_msg_declare_t *decl) { } // Encode extensions if (has_qos_ext) { - n -= 1; - _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, 0x01 | _Z_MSG_EXT_ENC_ZINT | (n != 0 ? _Z_FLAG_Z_Z : 0))); + n_ext -= 1; + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, 0x01 | _Z_MSG_EXT_ENC_ZINT | (n_ext != 0 ? _Z_FLAG_Z_Z : 0))); _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, decl->_ext_qos._val)); } if (has_timestamp_ext) { - n -= 1; - _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, 0x02 | _Z_MSG_EXT_ENC_ZBUF | (n != 0 ? _Z_FLAG_Z_Z : 0))); + n_ext -= 1; + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, 0x02 | _Z_MSG_EXT_ENC_ZBUF | (n_ext != 0 ? _Z_FLAG_Z_Z : 0))); _Z_RETURN_IF_ERR(_z_timestamp_encode_ext(wbf, &decl->_ext_timestamp)); } // Encode declaration diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index 00cacad9c..5e3b455ce 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -37,12 +37,12 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg uint8_t cbyte = 0; cbyte |= (msg->_whatami & 0x03); uint8_t zidlen = _z_id_len(msg->_zid); - cbyte |= ((zidlen - 1) & 0x0F) << 4; + cbyte |= (uint8_t)(((zidlen - 1) & 0x0F) << 4); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, cbyte)); _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, msg->_zid.id, 0, zidlen)); if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S)) { - uint8_t cbyte = 0; + cbyte = 0; cbyte |= (msg->_seq_num_res & 0x03); cbyte |= ((msg->_req_id_res & 0x03) << 2); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, cbyte)); @@ -155,7 +155,7 @@ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg uint8_t cbyte = 0; cbyte |= (msg->_whatami & 0x03); uint8_t zidlen = _z_id_len(msg->_zid); - cbyte |= ((zidlen - 1) & 0x0F) << 4; // TODO[protocol]: check if ZID > 0 && <= 16 + cbyte |= (uint8_t)(((zidlen - 1) & 0x0F) << 4); // TODO[protocol]: check if ZID > 0 && <= 16 _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, cbyte)) _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, msg->_zid.id, 0, zidlen)) diff --git a/src/protocol/core.c b/src/protocol/core.c index 81e27b5a4..08cc542c4 100644 --- a/src/protocol/core.c +++ b/src/protocol/core.c @@ -42,7 +42,7 @@ _Bool _z_id_check(_z_id_t id) { } return ret; } -_z_id_t _z_id_empty() { +_z_id_t _z_id_empty(void) { return (_z_id_t){.id = { 0, 0, @@ -62,10 +62,10 @@ _z_id_t _z_id_empty() { 0, }}; } -_z_source_info_t _z_source_info_null() { +_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() { return (_z_timestamp_t){.id = _z_id_empty(), .time = 0}; } +_z_timestamp_t _z_timestamp_null(void) { return (_z_timestamp_t){.id = _z_id_empty(), .time = 0}; } _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; diff --git a/src/protocol/definitions/network.c b/src/protocol/definitions/network.c index 63341b68e..f7c08f34f 100644 --- a/src/protocol/definitions/network.c +++ b/src/protocol/definitions/network.c @@ -72,7 +72,7 @@ _z_push_body_t _z_push_body_steal(_z_push_body_t *msg) { *msg = _z_push_body_null(); return ret; } -_z_push_body_t _z_push_body_null() { +_z_push_body_t _z_push_body_null(void) { return (_z_push_body_t){ ._is_put = false, ._body._del._commons = {._timestamp = _z_timestamp_null(), ._source_info = _z_source_info_null()}}; diff --git a/src/protocol/definitions/transport.c b/src/protocol/definitions/transport.c index 2a774f758..39bbee9d1 100644 --- a/src/protocol/definitions/transport.c +++ b/src/protocol/definitions/transport.c @@ -17,14 +17,20 @@ #include "zenoh-pico/collections/slice.h" #include "zenoh-pico/utils/logging.h" -void _z_s_msg_scout_clear(_z_s_msg_scout_t *msg) {} +void _z_s_msg_scout_clear(_z_s_msg_scout_t *msg) { + // Nothing to do + _ZP_UNUSED(msg); +} /*------------------ Locators Field ------------------*/ void _z_locators_clear(_z_locator_array_t *ls) { _z_locator_array_clear(ls); } void _z_s_msg_hello_clear(_z_s_msg_hello_t *msg) { _z_locators_clear(&msg->_locators); } -void _z_t_msg_join_clear(_z_t_msg_join_t *msg) {} +void _z_t_msg_join_clear(_z_t_msg_join_t *msg) { + // Nothing to do + _ZP_UNUSED(msg); +} void _z_t_msg_init_clear(_z_t_msg_init_t *msg) { _z_slice_clear(&msg->_cookie); } @@ -414,4 +420,4 @@ void _z_s_msg_copy(_z_scouting_message_t *clone, _z_scouting_message_t *msg) { _Z_DEBUG("WARNING: Trying to copy session message with unknown ID(%d)", mid); } break; } -} \ No newline at end of file +} diff --git a/src/protocol/iobuf.c b/src/protocol/iobuf.c index f31511c4c..aa1f0346f 100644 --- a/src/protocol/iobuf.c +++ b/src/protocol/iobuf.c @@ -70,7 +70,7 @@ uint8_t _z_iosli_read(_z_iosli_t *ios) { void _z_iosli_read_bytes(_z_iosli_t *ios, uint8_t *dst, size_t offset, size_t length) { assert(_z_iosli_readable(ios) >= length); - uint8_t *w_pos = _z_ptr_u8_offset(dst, offset); + uint8_t *w_pos = _z_ptr_u8_offset(dst, (ptrdiff_t)offset); (void)memcpy(w_pos, ios->_buf + ios->_r_pos, length); ios->_r_pos = ios->_r_pos + length; } @@ -90,8 +90,8 @@ void _z_iosli_write(_z_iosli_t *ios, uint8_t b) { void _z_iosli_write_bytes(_z_iosli_t *ios, const uint8_t *bs, size_t offset, size_t length) { assert(_z_iosli_writable(ios) >= length); - uint8_t *w_pos = _z_ptr_u8_offset(ios->_buf, ios->_w_pos); - (void)memcpy(w_pos, _z_cptr_u8_offset(bs, offset), length); + uint8_t *w_pos = _z_ptr_u8_offset(ios->_buf, (ptrdiff_t)ios->_w_pos); + (void)memcpy(w_pos, _z_cptr_u8_offset(bs, (ptrdiff_t)offset), length); ios->_w_pos += length; } @@ -103,7 +103,7 @@ void _z_iosli_put(_z_iosli_t *ios, uint8_t b, size_t pos) { _z_slice_t _z_iosli_to_bytes(const _z_iosli_t *ios) { _z_slice_t a; a.len = _z_iosli_readable(ios); - a.start = _z_cptr_u8_offset(ios->_buf, ios->_r_pos); + a.start = _z_cptr_u8_offset(ios->_buf, (ptrdiff_t)ios->_r_pos); return a; } @@ -183,7 +183,9 @@ size_t _z_zbuf_capacity(const _z_zbuf_t *zbf) { return zbf->_ios._capacity; } size_t _z_zbuf_space_left(const _z_zbuf_t *zbf) { return _z_iosli_writable(&zbf->_ios); } -uint8_t const *_z_zbuf_start(const _z_zbuf_t *zbf) { return _z_ptr_u8_offset(zbf->_ios._buf, zbf->_ios._r_pos); } +uint8_t const *_z_zbuf_start(const _z_zbuf_t *zbf) { + return _z_ptr_u8_offset(zbf->_ios._buf, (ptrdiff_t)zbf->_ios._r_pos); +} size_t _z_zbuf_len(const _z_zbuf_t *zbf) { return _z_iosli_readable(&zbf->_ios); } _Bool _z_zbuf_can_read(const _z_zbuf_t *zbf) { return _z_zbuf_len(zbf) > (size_t)0; } diff --git a/src/protocol/keyexpr.c b/src/protocol/keyexpr.c index 49fefcbf7..09f128747 100644 --- a/src/protocol/keyexpr.c +++ b/src/protocol/keyexpr.c @@ -89,7 +89,7 @@ zp_keyexpr_canon_status_t __zp_canon_prefix(const char *start, size_t *len) { _Bool in_big_wild = false; char const *chunk_start = start; - const char *end = _z_cptr_char_offset(start, *len); + const char *end = _z_cptr_char_offset(start, (ptrdiff_t)(*len)); char const *next_slash; do { @@ -192,7 +192,7 @@ zp_keyexpr_canon_status_t __zp_canon_prefix(const char *start, size_t *len) { } void __zp_singleify(char *start, size_t *len, const char *needle) { - const char *end = _z_cptr_char_offset(start, *len); + const char *end = _z_cptr_char_offset(start, (ptrdiff_t)(*len)); _Bool right_after_needle = false; char *reader = start; @@ -203,7 +203,7 @@ void __zp_singleify(char *start, size_t *len, const char *needle) { break; } right_after_needle = true; - reader = _z_ptr_char_offset(reader, pos); + reader = _z_ptr_char_offset(reader, (ptrdiff_t)pos); } else { right_after_needle = false; reader = _z_ptr_char_offset(reader, 1); @@ -218,10 +218,10 @@ void __zp_singleify(char *start, size_t *len, const char *needle) { for (size_t i = 0; i < pos; i++) { writer[i] = reader[i]; } - writer = _z_ptr_char_offset(writer, pos); + writer = _z_ptr_char_offset(writer, (ptrdiff_t)pos); } right_after_needle = true; - reader = _z_ptr_char_offset(reader, pos); + reader = _z_ptr_char_offset(reader, (ptrdiff_t)pos); } else { right_after_needle = false; *writer = *reader; @@ -239,7 +239,7 @@ void __zp_ke_write_chunk(char **writer, const char *chunk, size_t len, const cha } (void)memcpy(writer[0], chunk, len); - writer[0] = _z_ptr_char_offset(writer[0], len); + writer[0] = _z_ptr_char_offset(writer[0], (ptrdiff_t)len); } /*------------------ Common helpers ------------------*/ @@ -291,7 +291,7 @@ _Bool _z_ke_isdoublestar(_z_str_se_t s) { /*------------------ Inclusion helpers ------------------*/ _Bool _z_ke_chunk_includes_nodsl(_z_str_se_t l, _z_str_se_t r) { - size_t llen = l.end - l.start; + size_t llen = (size_t)(l.end - l.start); _Bool result = !(r.start[0] == _Z_VERBATIM) && ((llen == (size_t)1) && (l.start[0] == '*') && (((_z_ptr_char_diff(r.end, r.start) == 2) && (r.start[0] == '*')) == false)); @@ -396,8 +396,8 @@ _Bool _z_keyexpr_includes_superwild(_z_str_se_t left, _z_str_se_t right, _z_ke_c _Bool _z_keyexpr_includes(const char *lstart, const size_t llen, const char *rstart, const size_t rlen) { _Bool result = ((llen == rlen) && (strncmp(lstart, rstart, llen) == 0)); if (result == false) { - _z_str_se_t l = {.start = lstart, .end = _z_cptr_char_offset(lstart, llen)}; - _z_str_se_t r = {.start = rstart, .end = _z_cptr_char_offset(rstart, rlen)}; + _z_str_se_t l = {.start = lstart, .end = _z_cptr_char_offset(lstart, (ptrdiff_t)llen)}; + _z_str_se_t r = {.start = rstart, .end = _z_cptr_char_offset(rstart, (ptrdiff_t)rlen)}; size_t ln_chunks = 0, ln_verbatim = 0; size_t rn_chunks = 0, rn_verbatim = 0; int8_t lwildness = _zp_ke_wildness(l, &ln_chunks, &ln_verbatim); @@ -630,8 +630,8 @@ _Bool _z_keyexpr_intersect_bothsuper(_z_str_se_t l, _z_str_se_t r, _z_ke_chunk_m _Bool _z_keyexpr_intersects(const char *lstart, const size_t llen, const char *rstart, const size_t rlen) { _Bool result = ((llen == rlen) && (strncmp(lstart, rstart, llen) == 0)); if (result == false) { - _z_str_se_t l = {.start = lstart, .end = _z_cptr_char_offset(lstart, llen)}; - _z_str_se_t r = {.start = rstart, .end = _z_cptr_char_offset(rstart, rlen)}; + _z_str_se_t l = {.start = lstart, .end = _z_cptr_char_offset(lstart, (ptrdiff_t)llen)}; + _z_str_se_t r = {.start = rstart, .end = _z_cptr_char_offset(rstart, (ptrdiff_t)rlen)}; size_t ln_chunks = 0, ln_verbatim = 0; size_t rn_chunks = 0, rn_verbatim = 0; int8_t lwildness = _zp_ke_wildness(l, &ln_chunks, &ln_verbatim); @@ -685,8 +685,8 @@ zp_keyexpr_canon_status_t _z_keyexpr_canonize(char *start, size_t *len) { (ret == Z_KEYEXPR_CANON_DOUBLE_STAR_AFTER_DOUBLE_STAR)) { ret = Z_KEYEXPR_CANON_SUCCESS; - const char *end = _z_cptr_char_offset(start, *len); - char *reader = _z_ptr_char_offset(start, canon_len); + const char *end = _z_cptr_char_offset(start, (ptrdiff_t)(*len)); + char *reader = _z_ptr_char_offset(start, (ptrdiff_t)canon_len); const char *write_start = reader; char *writer = reader; char *next_slash = strchr(reader, '/'); @@ -799,4 +799,4 @@ zp_keyexpr_canon_status_t _z_keyexpr_canonize(char *start, size_t *len) { return ret; } -zp_keyexpr_canon_status_t _z_keyexpr_is_canon(const char *start, size_t len) { return __zp_canon_prefix(start, &len); } \ No newline at end of file +zp_keyexpr_canon_status_t _z_keyexpr_is_canon(const char *start, size_t len) { return __zp_canon_prefix(start, &len); } diff --git a/src/session/push.c b/src/session/push.c index 0763d0bd4..4c2645f41 100644 --- a/src/session/push.c +++ b/src/session/push.c @@ -25,12 +25,15 @@ int8_t _z_trigger_push(_z_session_t *zn, _z_n_msg_push_t *push) { int8_t ret = _Z_RES_OK; // TODO check body to know where to dispatch +#if Z_FEATURE_SUBSCRIPTION == 1 _z_slice_t payload = push->_body._is_put ? push->_body._body._put._payload : _z_slice_empty(); _z_encoding_t encoding = push->_body._is_put ? push->_body._body._put._encoding : _z_encoding_null(); - int kind = push->_body._is_put ? Z_SAMPLE_KIND_PUT : Z_SAMPLE_KIND_DELETE; -#if Z_FEATURE_SUBSCRIPTION == 1 + size_t kind = push->_body._is_put ? Z_SAMPLE_KIND_PUT : Z_SAMPLE_KIND_DELETE; ret = _z_trigger_subscriptions(zn, push->_key, payload, encoding, kind, push->_timestamp, push->_qos, push->_body._body._put._attachment); +#else + _ZP_UNUSED(zn); + _ZP_UNUSED(push); #endif return ret; } diff --git a/src/session/reply.c b/src/session/reply.c index dc57530e3..5b756d8f5 100644 --- a/src/session/reply.c +++ b/src/session/reply.c @@ -30,6 +30,11 @@ int8_t _z_trigger_reply_partial(_z_session_t *zn, _z_zint_t id, _z_keyexpr_t key } else { ret = _Z_ERR_GENERIC; } +#else + _ZP_UNUSED(zn); + _ZP_UNUSED(id); + _ZP_UNUSED(key); + _ZP_UNUSED(reply); #endif return ret; } @@ -42,6 +47,7 @@ int8_t _z_trigger_reply_final(_z_session_t *zn, _z_n_msg_response_final_t *final _z_zint_t id = final->_request_id; _z_trigger_query_reply_final(zn, id); #else + _ZP_UNUSED(zn); _ZP_UNUSED(final); #endif return ret; diff --git a/src/session/resource.c b/src/session/resource.c index 0c2dc44ce..ca1305d41 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -213,8 +213,8 @@ _z_keyexpr_t _z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t * } /// Returns the ID of the registered keyexpr. Returns 0 if registration failed. -int16_t _z_register_resource(_z_session_t *zn, _z_keyexpr_t key, uint16_t id, uint16_t register_to_mapping) { - int16_t ret = Z_RESOURCE_ID_NONE; +uint16_t _z_register_resource(_z_session_t *zn, _z_keyexpr_t key, uint16_t id, uint16_t register_to_mapping) { + uint16_t ret = Z_RESOURCE_ID_NONE; key = _z_keyexpr_alias(key); uint16_t mapping = register_to_mapping; uint16_t parent_mapping = _z_keyexpr_mapping_id(&key); diff --git a/src/session/rx.c b/src/session/rx.c index c1a53ce1c..2f040a4fa 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -98,8 +98,8 @@ int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint #endif } break; case _Z_REQUEST_PUT: { - _z_msg_put_t put = req._body._put; #if Z_FEATURE_SUBSCRIPTION == 1 + _z_msg_put_t put = req._body._put; ret = _z_trigger_subscriptions(zn, req._key, put._payload, put._encoding, Z_SAMPLE_KIND_PUT, put._commons._timestamp, req._ext_qos, put._attachment); #endif @@ -109,8 +109,8 @@ int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint } } break; case _Z_REQUEST_DEL: { - _z_msg_del_t del = req._body._del; #if Z_FEATURE_SUBSCRIPTION == 1 + _z_msg_del_t del = req._body._del; ret = _z_trigger_subscriptions(zn, req._key, _z_slice_empty(), _z_encoding_null(), Z_SAMPLE_KIND_DELETE, del._commons._timestamp, req._ext_qos, _z_bytes_null()); diff --git a/src/session/subscription.c b/src/session/subscription.c index 66c8510a8..ec0980feb 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -215,6 +215,7 @@ void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr _ZP_UNUSED(payload); _ZP_UNUSED(payload_len); _ZP_UNUSED(qos); + _ZP_UNUSED(att); } #endif // Z_FEATURE_SUBSCRIPTION == 1 diff --git a/src/session/utils.c b/src/session/utils.c index 8600b5211..556694bb8 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -139,4 +139,4 @@ void _zp_session_unlock_mutex(_z_session_t *zn) { (void)z_mutex_unlock(&zn->_mut #else void _zp_session_lock_mutex(_z_session_t *zn) { _ZP_UNUSED(zn); } void _zp_session_unlock_mutex(_z_session_t *zn) { _ZP_UNUSED(zn); } -#endif \ No newline at end of file +#endif diff --git a/src/system/unix/link/raweth.c b/src/system/unix/link/raweth.c index 0b3131e35..9a97546f2 100644 --- a/src/system/unix/link/raweth.c +++ b/src/system/unix/link/raweth.c @@ -96,7 +96,7 @@ size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buf const _zp_raweth_whitelist_array_t *whitelist) { // Read from socket ssize_t bytesRead = recvfrom(sock->_fd, buff, buff_len, 0, NULL, NULL); - if ((bytesRead < 0) || (bytesRead < sizeof(_zp_eth_header_t))) { + if ((bytesRead < 0) || (bytesRead < (ssize_t)sizeof(_zp_eth_header_t))) { return SIZE_MAX; } _Bool is_valid = true; @@ -118,15 +118,16 @@ size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buf } // Copy sender mac if needed if (addr != NULL) { + uint8_t *header_addr = (uint8_t *)buff; *addr = _z_slice_make(sizeof(ETH_ALEN)); - (void)memcpy((uint8_t *)addr->start, (buff + ETH_ALEN), sizeof(ETH_ALEN)); + (void)memcpy((uint8_t *)addr->start, (header_addr + ETH_ALEN), sizeof(ETH_ALEN)); } - return bytesRead; + return (size_t)bytesRead; } -size_t _z_raweth_ntohs(size_t val) { return ntohs(val); } +uint16_t _z_raweth_ntohs(uint16_t val) { return ntohs(val); } -size_t _z_raweth_htons(size_t val) { return htons(val); } +uint16_t _z_raweth_htons(uint16_t val) { return htons(val); } #endif // defined(__linux) #endif // Z_FEATURE_RAWETH_TRANSPORT == 1 diff --git a/src/system/unix/network.c b/src/system/unix/network.c index e4a7a2af9..ca40c15a5 100644 --- a/src/system/unix/network.c +++ b/src/system/unix/network.c @@ -125,10 +125,10 @@ void _z_close_tcp(_z_sys_net_socket_t *sock) { size_t _z_read_tcp(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { ssize_t rb = recv(sock._fd, ptr, len, 0); if (rb < (ssize_t)0) { - rb = SIZE_MAX; + return SIZE_MAX; } - return rb; + return (size_t)rb; } size_t _z_read_exact_tcp(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { @@ -143,7 +143,7 @@ size_t _z_read_exact_tcp(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t le } n = n + rb; - pos = _z_ptr_u8_offset(pos, n); + pos = _z_ptr_u8_offset(pos, (ptrdiff_t)n); } while (n != len); return n; @@ -151,7 +151,7 @@ size_t _z_read_exact_tcp(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t le size_t _z_send_tcp(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len) { #if defined(ZENOH_LINUX) - return send(sock._fd, ptr, len, MSG_NOSIGNAL); + return (size_t)send(sock._fd, ptr, len, MSG_NOSIGNAL); #else return send(sock._fd, ptr, len, 0); #endif @@ -223,10 +223,9 @@ size_t _z_read_udp_unicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t ssize_t rb = recvfrom(sock._fd, ptr, len, 0, (struct sockaddr *)&raddr, &addrlen); if (rb < (ssize_t)0) { - rb = SIZE_MAX; + return SIZE_MAX; } - - return rb; + return (size_t)rb; } size_t _z_read_exact_udp_unicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { @@ -241,7 +240,7 @@ size_t _z_read_exact_udp_unicast(const _z_sys_net_socket_t sock, uint8_t *ptr, s } n = n + rb; - pos = _z_ptr_u8_offset(pos, n); + pos = _z_ptr_u8_offset(pos, (ptrdiff_t)n); } while (n != len); return n; @@ -249,7 +248,7 @@ size_t _z_read_exact_udp_unicast(const _z_sys_net_socket_t sock, uint8_t *ptr, s size_t _z_send_udp_unicast(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t rep) { - return sendto(sock._fd, ptr, len, 0, rep._iptcp->ai_addr, rep._iptcp->ai_addrlen); + return (size_t)sendto(sock._fd, ptr, len, 0, rep._iptcp->ai_addr, rep._iptcp->ai_addrlen); } #endif @@ -324,7 +323,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin ret = _Z_ERR_GENERIC; } } else if (lsockaddr->sa_family == AF_INET6) { - int ifindex = if_nametoindex(iface); + int ifindex = (int)if_nametoindex(iface); if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifindex, sizeof(ifindex)) < 0)) { ret = _Z_ERR_GENERIC; @@ -396,7 +395,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo if (rep._iptcp->ai_family == AF_INET) { struct sockaddr_in address; (void)memset(&address, 0, sizeof(address)); - address.sin_family = rep._iptcp->ai_family; + address.sin_family = (sa_family_t)rep._iptcp->ai_family; address.sin_port = ((struct sockaddr_in *)rep._iptcp->ai_addr)->sin_port; inet_pton(address.sin_family, "0.0.0.0", &address.sin_addr); if ((ret == _Z_RES_OK) && (bind(sock->_fd, (struct sockaddr *)&address, sizeof(address)) < 0)) { @@ -405,7 +404,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo } else if (rep._iptcp->ai_family == AF_INET6) { struct sockaddr_in6 address; (void)memset(&address, 0, sizeof(address)); - address.sin6_family = rep._iptcp->ai_family; + address.sin6_family = (sa_family_t)rep._iptcp->ai_family; address.sin6_port = ((struct sockaddr_in6 *)rep._iptcp->ai_addr)->sin6_port; inet_pton(address.sin6_family, "::", &address.sin6_addr); if ((ret == _Z_RES_OK) && (bind(sock->_fd, (struct sockaddr *)&address, sizeof(address)) < 0)) { @@ -517,8 +516,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ do { rb = recvfrom(sock._fd, ptr, len, 0, (struct sockaddr *)&raddr, &replen); if (rb < (ssize_t)0) { - rb = SIZE_MAX; - break; + return SIZE_MAX; } if (lep._iptcp->ai_family == AF_INET) { @@ -551,7 +549,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ } } while (1); - return rb; + return (size_t)rb; } size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, @@ -567,7 +565,7 @@ size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, } n = n + rb; - pos = _z_ptr_u8_offset(pos, n); + pos = _z_ptr_u8_offset(pos, (ptrdiff_t)n); } while (n != len); return n; @@ -575,7 +573,7 @@ size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t _z_send_udp_multicast(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t rep) { - return sendto(sock._fd, ptr, len, 0, rep._iptcp->ai_addr, rep._iptcp->ai_addrlen); + return (size_t)sendto(sock._fd, ptr, len, 0, rep._iptcp->ai_addr, rep._iptcp->ai_addrlen); } #endif diff --git a/src/system/unix/system.c b/src/system/unix/system.c index 799b60519..49857ac7e 100644 --- a/src/system/unix/system.c +++ b/src/system/unix/system.c @@ -102,12 +102,12 @@ void z_free(void *ptr) { free(ptr); } #if Z_FEATURE_MULTI_THREAD == 1 /*------------------ Task ------------------*/ int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { - return pthread_create(task, attr, fun, arg); + return (int8_t)pthread_create(task, attr, fun, arg); } -int8_t z_task_join(z_task_t *task) { return pthread_join(*task, NULL); } +int8_t z_task_join(z_task_t *task) { return (int8_t)pthread_join(*task, NULL); } -int8_t zp_task_cancel(z_task_t *task) { return pthread_cancel(*task); } +int8_t zp_task_cancel(z_task_t *task) { return (int8_t)pthread_cancel(*task); } void z_task_free(z_task_t **task) { z_task_t *ptr = *task; @@ -116,28 +116,28 @@ void z_task_free(z_task_t **task) { } /*------------------ Mutex ------------------*/ -int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, 0); } +int8_t z_mutex_init(z_mutex_t *m) { return (int8_t)pthread_mutex_init(m, 0); } -int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t z_mutex_free(z_mutex_t *m) { return (int8_t)pthread_mutex_destroy(m); } -int8_t z_mutex_lock(z_mutex_t *m) { return pthread_mutex_lock(m); } +int8_t z_mutex_lock(z_mutex_t *m) { return (int8_t)pthread_mutex_lock(m); } -int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t z_mutex_trylock(z_mutex_t *m) { return (int8_t)pthread_mutex_trylock(m); } -int8_t z_mutex_unlock(z_mutex_t *m) { return pthread_mutex_unlock(m); } +int8_t z_mutex_unlock(z_mutex_t *m) { return (int8_t)pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ -int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, 0); } +int8_t z_condvar_init(z_condvar_t *cv) { return (int8_t)pthread_cond_init(cv, 0); } -int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t z_condvar_free(z_condvar_t *cv) { return (int8_t)pthread_cond_destroy(cv); } -int8_t z_condvar_signal(z_condvar_t *cv) { return pthread_cond_signal(cv); } +int8_t z_condvar_signal(z_condvar_t *cv) { return (int8_t)pthread_cond_signal(cv); } -int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return pthread_cond_wait(cv, m); } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return (int8_t)pthread_cond_wait(cv, m); } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int z_sleep_us(size_t time) { return usleep(time); } +int z_sleep_us(size_t time) { return usleep((unsigned int)time); } int z_sleep_ms(size_t time) { z_time_t start = z_time_now(); @@ -152,7 +152,7 @@ int z_sleep_ms(size_t time) { return 0; } -int z_sleep_s(size_t time) { return sleep(time); } +int z_sleep_s(size_t time) { return (int)sleep((unsigned int)time); } /*------------------ Instant ------------------*/ z_clock_t z_clock_now(void) { @@ -165,7 +165,8 @@ unsigned long z_clock_elapsed_us(z_clock_t *instant) { z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); - unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000); + unsigned long elapsed = + (unsigned long)(1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000); return elapsed; } @@ -173,7 +174,8 @@ unsigned long z_clock_elapsed_ms(z_clock_t *instant) { z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); - unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000); + unsigned long elapsed = + (unsigned long)(1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000); return elapsed; } @@ -181,7 +183,7 @@ unsigned long z_clock_elapsed_s(z_clock_t *instant) { z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); - unsigned long elapsed = now.tv_sec - instant->tv_sec; + unsigned long elapsed = (unsigned long)(now.tv_sec - instant->tv_sec); return elapsed; } @@ -204,7 +206,7 @@ unsigned long z_time_elapsed_us(z_time_t *time) { z_time_t now; gettimeofday(&now, NULL); - unsigned long elapsed = (1000000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec)); + unsigned long elapsed = (unsigned long)(1000000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec)); return elapsed; } @@ -212,7 +214,7 @@ unsigned long z_time_elapsed_ms(z_time_t *time) { z_time_t now; gettimeofday(&now, NULL); - unsigned long elapsed = (1000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec) / 1000); + unsigned long elapsed = (unsigned long)(1000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec) / 1000); return elapsed; } @@ -220,6 +222,6 @@ unsigned long z_time_elapsed_s(z_time_t *time) { z_time_t now; gettimeofday(&now, NULL); - unsigned long elapsed = now.tv_sec - time->tv_sec; + unsigned long elapsed = (unsigned long)(now.tv_sec - time->tv_sec); return elapsed; } diff --git a/src/transport/multicast/lease.c b/src/transport/multicast/lease.c index f728535f6..5c313a8af 100644 --- a/src/transport/multicast/lease.c +++ b/src/transport/multicast/lease.c @@ -23,6 +23,37 @@ #if Z_FEATURE_MULTICAST_TRANSPORT == 1 || Z_FEATURE_RAWETH_TRANSPORT == 1 +int8_t _zp_multicast_send_join(_z_transport_multicast_t *ztm) { + _z_conduit_sn_list_t next_sn; + next_sn._is_qos = false; + next_sn._val._plain._best_effort = ztm->_sn_tx_best_effort; + next_sn._val._plain._reliable = ztm->_sn_tx_reliable; + + _z_id_t zid = ((_z_session_t *)ztm->_session)->_local_zid; + _z_transport_message_t jsm = _z_t_msg_make_join(Z_WHATAMI_PEER, Z_TRANSPORT_LEASE, zid, next_sn); + + return ztm->_send_f(ztm, &jsm); +} + +int8_t _zp_multicast_send_keep_alive(_z_transport_multicast_t *ztm) { + _z_transport_message_t t_msg = _z_t_msg_make_keep_alive(); + return ztm->_send_f(ztm, &t_msg); +} + +#else +int8_t _zp_multicast_send_join(_z_transport_multicast_t *ztm) { + _ZP_UNUSED(ztm); + return _Z_ERR_TRANSPORT_NOT_AVAILABLE; +} + +int8_t _zp_multicast_send_keep_alive(_z_transport_multicast_t *ztm) { + _ZP_UNUSED(ztm); + return _Z_ERR_TRANSPORT_NOT_AVAILABLE; +} +#endif // Z_FEATURE_MULTICAST_TRANSPORT == 1 || Z_FEATURE_RAWETH_TRANSPORT == 1 + +#if Z_FEATURE_MULTI_THREAD == 1 && (Z_FEATURE_MULTICAST_TRANSPORT == 1 || Z_FEATURE_RAWETH_TRANSPORT == 1) + static _z_zint_t _z_get_minimum_lease(_z_transport_peer_entry_list_t *peers, _z_zint_t local_lease) { _z_zint_t ret = local_lease; @@ -57,45 +88,14 @@ static _z_zint_t _z_get_next_lease(_z_transport_peer_entry_list_t *peers) { return ret; } -int8_t _zp_multicast_send_join(_z_transport_multicast_t *ztm) { - _z_conduit_sn_list_t next_sn; - next_sn._is_qos = false; - next_sn._val._plain._best_effort = ztm->_sn_tx_best_effort; - next_sn._val._plain._reliable = ztm->_sn_tx_reliable; - - _z_id_t zid = ((_z_session_t *)ztm->_session)->_local_zid; - _z_transport_message_t jsm = _z_t_msg_make_join(Z_WHATAMI_PEER, Z_TRANSPORT_LEASE, zid, next_sn); - - return ztm->_send_f(ztm, &jsm); -} - -int8_t _zp_multicast_send_keep_alive(_z_transport_multicast_t *ztm) { - _z_transport_message_t t_msg = _z_t_msg_make_keep_alive(); - return ztm->_send_f(ztm, &t_msg); -} - -#else -int8_t _zp_multicast_send_join(_z_transport_multicast_t *ztm) { - _ZP_UNUSED(ztm); - return _Z_ERR_TRANSPORT_NOT_AVAILABLE; -} - -int8_t _zp_multicast_send_keep_alive(_z_transport_multicast_t *ztm) { - _ZP_UNUSED(ztm); - return _Z_ERR_TRANSPORT_NOT_AVAILABLE; -} -#endif // Z_FEATURE_MULTICAST_TRANSPORT == 1 || Z_FEATURE_RAWETH_TRANSPORT == 1 - -#if Z_FEATURE_MULTI_THREAD == 1 && (Z_FEATURE_MULTICAST_TRANSPORT == 1 || Z_FEATURE_RAWETH_TRANSPORT == 1) - void *_zp_multicast_lease_task(void *ztm_arg) { _z_transport_multicast_t *ztm = (_z_transport_multicast_t *)ztm_arg; ztm->_transmitted = false; // From all peers, get the next lease time (minimum) - _z_zint_t next_lease = _z_get_minimum_lease(ztm->_peers, ztm->_lease); - _z_zint_t next_keep_alive = (_z_zint_t)(next_lease / Z_TRANSPORT_LEASE_EXPIRE_FACTOR); - _z_zint_t next_join = Z_JOIN_INTERVAL; + int next_lease = (int)_z_get_minimum_lease(ztm->_peers, ztm->_lease); + int next_keep_alive = (int)(next_lease / Z_TRANSPORT_LEASE_EXPIRE_FACTOR); + int next_join = Z_JOIN_INTERVAL; _z_transport_peer_entry_list_t *it = NULL; while (ztm->_lease_task_running == true) { @@ -137,12 +137,11 @@ void *_zp_multicast_lease_task(void *ztm_arg) { // Reset the keep alive parameters ztm->_transmitted = false; - next_keep_alive = - (_z_zint_t)(_z_get_minimum_lease(ztm->_peers, ztm->_lease) / Z_TRANSPORT_LEASE_EXPIRE_FACTOR); + next_keep_alive = (int)(_z_get_minimum_lease(ztm->_peers, ztm->_lease) / Z_TRANSPORT_LEASE_EXPIRE_FACTOR); } // Compute the target interval to sleep - _z_zint_t interval; + int interval; if (next_lease > 0) { interval = next_lease; if (next_keep_alive < interval) { @@ -161,7 +160,7 @@ void *_zp_multicast_lease_task(void *ztm_arg) { z_mutex_unlock(&ztm->_mutex_peer); // The keep alive and lease intervals are expressed in milliseconds - z_sleep_ms(interval); + z_sleep_ms((size_t)interval); // Decrement all intervals z_mutex_lock(&ztm->_mutex_peer); @@ -169,10 +168,16 @@ void *_zp_multicast_lease_task(void *ztm_arg) { it = ztm->_peers; while (it != NULL) { _z_transport_peer_entry_t *entry = _z_transport_peer_entry_list_head(it); - entry->_next_lease = entry->_next_lease - interval; + int entry_next_lease = (int)entry->_next_lease - interval; + if (entry_next_lease >= 0) { + entry->_next_lease = (size_t)entry_next_lease; + } else { + _Z_ERROR("Negative next lease value"); + entry->_next_lease = 0; + } it = _z_transport_peer_entry_list_tail(it); } - next_lease = _z_get_next_lease(ztm->_peers); + next_lease = (int)_z_get_next_lease(ztm->_peers); next_keep_alive = next_keep_alive - interval; next_join = next_join - interval; diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index 1970a3d34..a8c45492a 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -45,6 +45,9 @@ #define RAWETH_CONFIG_WHITELIST_KEY 0x04 #define RAWETH_CONFIG_WHITELIST_STR "whitelist" +// Ethtype must be at least 0x600 in network order +#define RAWETH_ETHTYPE_MIN_VALUE 0x600U + #define RAWETH_CONFIG_MAPPING_BUILD \ _z_str_intmapping_t args[RAWETH_CONFIG_ARGC]; \ args[0]._key = RAWETH_CONFIG_IFACE_KEY; \ @@ -99,7 +102,7 @@ static _Bool _z_valid_ethtype_raweth(_z_str_intmap_t *config) { return false; } long ethtype = strtol(s_ethtype, NULL, 16); - return (_z_raweth_htons(ethtype) > 0x600); // Ethtype must be at least 0x600 in network order + return (_z_raweth_htons((uint16_t)ethtype) > RAWETH_ETHTYPE_MIN_VALUE); } static long _z_get_ethtype_raweth(_z_str_intmap_t *config) { @@ -170,7 +173,7 @@ static int8_t _z_get_mapping_raweth(_z_str_intmap_t *config, _zp_raweth_mapping_ return _Z_RES_OK; } -static const size_t _z_valid_whitelist_raweth(_z_str_intmap_t *config) { +static size_t _z_valid_whitelist_raweth(_z_str_intmap_t *config) { // Retrieve data const char *cfg_str = _z_str_intmap_get(config, RAWETH_CONFIG_WHITELIST_KEY); if (cfg_str == NULL) { @@ -314,13 +317,13 @@ static _Bool _z_valid_address_raweth(const char *address) { return false; } // Check if the colons are at the correct positions - for (int i = 2; i < len; i += 3) { + for (size_t i = 2; i < len; i += 3) { if (address[i] != ':') { return false; } } // Check if each character is a valid hexadecimal digit - for (int i = 0; i < len; ++i) { + for (size_t i = 0; i < len; ++i) { if (i % 3 != 2) { char c = address[i]; if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))) { @@ -332,7 +335,6 @@ static _Bool _z_valid_address_raweth(const char *address) { } static uint8_t *_z_parse_address_raweth(const char *address) { - size_t len = strlen(address); // Allocate data uint8_t *ret = (uint8_t *)z_malloc(_ZP_MAC_ADDR_LENGTH); if (ret == NULL) { diff --git a/src/transport/raweth/rx.c b/src/transport/raweth/rx.c index db135c9cb..dfba004a6 100644 --- a/src/transport/raweth/rx.c +++ b/src/transport/raweth/rx.c @@ -47,9 +47,9 @@ static size_t _z_raweth_link_recv_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, _z } size_t data_length = 0; if (has_vlan) { - _zp_eth_vlan_header_t *header = (_zp_eth_vlan_header_t *)buff; + _zp_eth_vlan_header_t *vlan_header = (_zp_eth_vlan_header_t *)buff; // Retrieve data length - data_length = _z_raweth_ntohs(header->data_length); + data_length = _z_raweth_ntohs(vlan_header->data_length); if (rb < (data_length + sizeof(_zp_eth_vlan_header_t))) { // Invalid data_length return SIZE_MAX; @@ -58,7 +58,7 @@ static size_t _z_raweth_link_recv_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, _z _z_zbuf_set_wpos(zbf, _z_zbuf_get_wpos(zbf) + sizeof(_zp_eth_vlan_header_t) + data_length); _z_zbuf_set_rpos(zbf, _z_zbuf_get_rpos(zbf) + sizeof(_zp_eth_vlan_header_t)); } else { - _zp_eth_header_t *header = (_zp_eth_header_t *)buff; + header = (_zp_eth_header_t *)buff; // Retrieve data length data_length = _z_raweth_ntohs(header->data_length); if (rb < (data_length + sizeof(_zp_eth_header_t))) { diff --git a/src/transport/raweth/tx.c b/src/transport/raweth/tx.c index 9e75d3163..72784f6b6 100644 --- a/src/transport/raweth/tx.c +++ b/src/transport/raweth/tx.c @@ -37,13 +37,13 @@ static void _zp_raweth_unlock_tx_mutex(_z_transport_multicast_t *ztm) { _ZP_UNUS #endif static int _zp_raweth_find_map_entry(const _z_keyexpr_t *keyexpr, _z_raweth_socket_t *sock) { - for (int i = 0; i < _zp_raweth_mapping_array_len(&sock->_mapping); i++) { + for (size_t i = 0; i < _zp_raweth_mapping_array_len(&sock->_mapping); i++) { // Find matching keyexpr const _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, i); if (zp_keyexpr_intersect_null_terminated(keyexpr->_suffix, entry->_keyexpr._suffix) != _Z_RES_OK) { continue; } - return i; + return (int)i; } return -1; } @@ -72,7 +72,7 @@ static int8_t _zp_raweth_set_socket(const _z_keyexpr_t *keyexpr, _z_raweth_socke _Z_DEBUG("Key '%s' wasn't found in config mapping, sending to default address", keyexpr->_suffix); } // Store data into socket - const _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, idx); + const _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, (size_t)idx); memcpy(&sock->_dmac, &entry->_dmac, _ZP_MAC_ADDR_LENGTH); uint16_t vlan = entry->_vlan; sock->_has_vlan = entry->_has_vlan; @@ -130,7 +130,7 @@ static int8_t __unsafe_z_raweth_write_header(_z_link_t *zl, _z_wbuf_t *wbf) { header.vlan_type = _ZP_ETH_TYPE_VLAN; header.tag = resocket->_vlan; header.ethtype = resocket->_ethtype; - header.data_length = _z_raweth_htons(wpos - sizeof(header)); + header.data_length = _z_raweth_htons((uint16_t)(wpos - sizeof(header))); // Write header _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, (uint8_t *)&header, 0, sizeof(header))); } else { @@ -139,7 +139,7 @@ static int8_t __unsafe_z_raweth_write_header(_z_link_t *zl, _z_wbuf_t *wbf) { memcpy(&header.dmac, &resocket->_dmac, _ZP_MAC_ADDR_LENGTH); memcpy(&header.smac, &resocket->_smac, _ZP_MAC_ADDR_LENGTH); header.ethtype = resocket->_ethtype; - header.data_length = _z_raweth_htons(wpos - sizeof(header)); + header.data_length = _z_raweth_htons((uint16_t)(wpos - sizeof(header))); // Write header _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, (uint8_t *)&header, 0, sizeof(header))); } diff --git a/src/transport/unicast/lease.c b/src/transport/unicast/lease.c index 03d0ffd1b..9dfad2244 100644 --- a/src/transport/unicast/lease.c +++ b/src/transport/unicast/lease.c @@ -44,10 +44,11 @@ void *_zp_unicast_lease_task(void *ztu_arg) { ztu->_received = false; ztu->_transmitted = false; - _z_zint_t next_lease = ztu->_lease; - _z_zint_t next_keep_alive = (_z_zint_t)(ztu->_lease / Z_TRANSPORT_LEASE_EXPIRE_FACTOR); + int next_lease = (int)ztu->_lease; + int next_keep_alive = (int)(ztu->_lease / Z_TRANSPORT_LEASE_EXPIRE_FACTOR); while (ztu->_lease_task_running == true) { - if (next_lease == 0) { + // Next lease process + if (next_lease <= 0) { // Check if received data if (ztu->_received == true) { // Reset the lease parameters @@ -58,10 +59,9 @@ void *_zp_unicast_lease_task(void *ztu_arg) { _z_unicast_transport_close(ztu, _Z_CLOSE_EXPIRED); break; } - - next_lease = ztu->_lease; + next_lease = (int)ztu->_lease; } - + // Next keep alive process if (next_keep_alive <= 0) { // Check if need to send a keep alive if (ztu->_transmitted == false) { @@ -72,11 +72,11 @@ void *_zp_unicast_lease_task(void *ztu_arg) { // Reset the keep alive parameters ztu->_transmitted = false; - next_keep_alive = (_z_zint_t)(ztu->_lease / Z_TRANSPORT_LEASE_EXPIRE_FACTOR); + next_keep_alive = (int)(ztu->_lease / Z_TRANSPORT_LEASE_EXPIRE_FACTOR); } // Compute the target interval - _z_zint_t interval; + int interval; if (next_lease == 0) { interval = next_keep_alive; } else { @@ -87,7 +87,7 @@ void *_zp_unicast_lease_task(void *ztu_arg) { } // The keep alive and lease intervals are expressed in milliseconds - z_sleep_ms(interval); + z_sleep_ms((size_t)interval); next_lease = next_lease - interval; next_keep_alive = next_keep_alive - interval; diff --git a/src/transport/unicast/rx.c b/src/transport/unicast/rx.c index 6278ca735..33132b3ca 100644 --- a/src/transport/unicast/rx.c +++ b/src/transport/unicast/rx.c @@ -164,7 +164,7 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans _z_zbuf_t zbf = _z_wbuf_to_zbuf(dbuf); // Convert the defragmentation buffer into a decoding buffer _z_zenoh_message_t zm; - int8_t ret = _z_network_message_decode(&zm, &zbf); + ret = _z_network_message_decode(&zm, &zbf); if (ret == _Z_RES_OK) { _z_handle_network_message(ztu->_session, &zm, _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); _z_msg_clear(&zm); // Clear must be explicitly called for fragmented zenoh messages. Non-fragmented diff --git a/src/utils/encoding.c b/src/utils/encoding.c index 085511bdf..0fb93f978 100644 --- a/src/utils/encoding.c +++ b/src/utils/encoding.c @@ -51,7 +51,7 @@ size_t _z_cobs_decode(const uint8_t *input, size_t input_len, uint8_t *output) { uint8_t *pos = output; uint8_t code = (uint8_t)0xFF; - const uint8_t *input_end_ptr = _z_cptr_u8_offset(input, input_len); + const uint8_t *input_end_ptr = _z_cptr_u8_offset(input, (ptrdiff_t)input_len); for (uint8_t block = (uint8_t)0x00; byte < input_end_ptr; block--) { if (block != (uint8_t)0x00) { *pos = *byte; diff --git a/src/utils/pointers.c b/src/utils/pointers.c index 2e1bd4b08..77c05628f 100644 --- a/src/utils/pointers.c +++ b/src/utils/pointers.c @@ -14,13 +14,13 @@ #include "zenoh-pico/utils/pointers.h" -size_t _z_ptr_u8_diff(const uint8_t *l_ptr, const uint8_t *r_ptr) { return l_ptr - r_ptr; } +size_t _z_ptr_u8_diff(const uint8_t *l_ptr, const uint8_t *r_ptr) { return (size_t)(l_ptr - r_ptr); } const uint8_t *_z_cptr_u8_offset(const uint8_t *ptr, const ptrdiff_t off) { return ptr + off; } uint8_t *_z_ptr_u8_offset(uint8_t *ptr, const ptrdiff_t off) { return ptr + off; } -size_t _z_ptr_char_diff(const char *l_ptr, const char *r_ptr) { return l_ptr - r_ptr; } +size_t _z_ptr_char_diff(const char *l_ptr, const char *r_ptr) { return (size_t)(l_ptr - r_ptr); } const char *_z_cptr_char_offset(const char *ptr, const ptrdiff_t off) { return ptr + off; } diff --git a/src/utils/string.c b/src/utils/string.c index e95e3ab07..512ae2310 100644 --- a/src/utils/string.c +++ b/src/utils/string.c @@ -26,7 +26,7 @@ char const *_z_rstrstr(char const *haystack_start, char const *haystack_end, con char const *he = haystack_end; const char *needle_end = strchr(needle_start, 0); - hs = _z_cptr_char_offset(hs, _z_ptr_char_diff(needle_end, needle_start)); + hs = _z_cptr_char_offset(hs, (ptrdiff_t)_z_ptr_char_diff(needle_end, needle_start)); char const *result = NULL; while ((result == false) && (he >= hs)) { char found = 1; @@ -50,7 +50,7 @@ char const *_z_rstrstr(char const *haystack_start, char const *haystack_end, con } char const *_z_bstrstr(_z_str_se_t haystack, _z_str_se_t needle) { - haystack.end = _z_cptr_char_offset(haystack.end, -1 * _z_ptr_char_diff(needle.end, needle.start)); + haystack.end = _z_cptr_char_offset(haystack.end, -1 * (ptrdiff_t)_z_ptr_char_diff(needle.end, needle.start)); char const *result = NULL; for (; (result == false) && (haystack.start <= haystack.end); haystack.start = _z_cptr_char_offset(haystack.start, 1)) { @@ -86,7 +86,7 @@ char const *_z_strstr_skipneedle(char const *haystack_start, char const *haystac char const *_z_bstrstr_skipneedle(_z_str_se_t haystack, _z_str_se_t needle) { char const *result = _z_bstrstr(haystack, needle); if (result != NULL) { - result = _z_cptr_char_offset(result, _z_ptr_char_diff(needle.end, needle.start)); + result = _z_cptr_char_offset(result, (ptrdiff_t)_z_ptr_char_diff(needle.end, needle.start)); } return result; } @@ -102,7 +102,7 @@ _z_str_se_t _z_splitstr_next(_z_splitstr_t *str) { if (result.end == str->s.end) { str->s = (_z_str_se_t){.start = NULL, .end = NULL}; } else { - str->s.start = _z_cptr_char_offset(result.end, strlen(str->delimiter)); + str->s.start = _z_cptr_char_offset(result.end, (ptrdiff_t)strlen(str->delimiter)); } } return result; @@ -122,7 +122,7 @@ _z_str_se_t _z_splitstr_nextback(_z_splitstr_t *str) { if (result.start == str->s.start) { str->s = (_z_str_se_t){.start = NULL, .end = NULL}; } else { - str->s.end = _z_cptr_char_offset(result.start, -1 * strlen(str->delimiter)); + str->s.end = _z_cptr_char_offset(result.start, -1 * (ptrdiff_t)strlen(str->delimiter)); } } return result; diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 8ed72b264..be7bc2c36 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -22,7 +22,6 @@ #define URI "demo/example/**/*" #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) #include -#define sleep(x) Sleep(x * 1000) #else #include #endif @@ -77,8 +76,10 @@ void query_handler(const z_loaned_query_t *query, void *arg) { z_view_string_t pred; z_query_parameters(query, &pred); (void)(pred); - const z_loaned_value_t *payload_value = z_query_value(query); - (void)(payload_value); + const z_loaned_bytes_t *payload = z_query_payload(query); + const z_loaned_encoding_t *encoding = z_query_encoding(query); + (void)(payload); + (void)(encoding); z_query_reply_options_t _ret_qreply_opt; z_query_reply_options_default(&_ret_qreply_opt); @@ -108,8 +109,8 @@ void reply_handler(const z_loaned_reply_t *reply, void *arg) { #endif z_drop(z_move(k_str)); } else { - const z_loaned_value_t *_ret_zvalue = z_reply_err(reply); - (void)(_ret_zvalue); + const z_loaned_reply_err_t *_ret_zerr = z_reply_err(reply); + (void)(_ret_zerr); } } @@ -161,7 +162,7 @@ int main(int argc, char **argv) { assert(!_ret_bool); #endif - sleep(SLEEP); + z_sleep_s(SLEEP); size_t keyexpr_len = strlen(URI); char *keyexpr_str = (char *)z_malloc(keyexpr_len + 1); @@ -184,7 +185,7 @@ int main(int argc, char **argv) { #endif printf("Ok\n"); - sleep(SLEEP); + z_sleep_s(SLEEP); printf("Testing Configs..."); z_owned_config_t _ret_config; @@ -214,7 +215,7 @@ int main(int argc, char **argv) { z_drop(z_move(_ret_sconfig)); printf("Ok\n"); - sleep(SLEEP); + z_sleep_s(SLEEP); printf("Testing Scouting..."); z_scouting_config_from(&_ret_sconfig, z_loan(_ret_config)); @@ -224,7 +225,7 @@ int main(int argc, char **argv) { assert_eq(_ret_int8, 0); assert(hellos >= 1); - uint32_t _scouting_timeout = strtoul(SCOUTING_TIMEOUT, NULL, 10); + uint32_t _scouting_timeout = (uint32_t)strtoul(SCOUTING_TIMEOUT, NULL, 10); z_sleep_ms(_scouting_timeout); printf("Ok\n"); z_sleep_s(SLEEP); @@ -243,13 +244,13 @@ int main(int argc, char **argv) { z_closure(&_ret_closure_zid, zid_handler, NULL, NULL); _ret_int8 = z_info_peers_zid(z_loan(s1), z_move(_ret_closure_zid)); assert_eq(_ret_int8, 0); - sleep(SLEEP); + z_sleep_s(SLEEP); assert_eq(zids, 0); _ret_int8 = z_info_routers_zid(z_loan(s1), z_move(_ret_closure_zid)); assert_eq(_ret_int8, 0); - sleep(SLEEP); + z_sleep_s(SLEEP); assert_eq(zids, 1); #ifdef ZENOH_PICO @@ -261,7 +262,7 @@ int main(int argc, char **argv) { zp_start_lease_task(z_loan_mut(s1), &_ret_lease_opt); #endif - sleep(SLEEP); + z_sleep_s(SLEEP); z_config_default(&_ret_config); #ifdef ZENOH_PICO @@ -287,7 +288,7 @@ int main(int argc, char **argv) { zp_start_lease_task(z_loan_mut(s2), NULL); #endif - sleep(SLEEP); + z_sleep_s(SLEEP); const z_loaned_session_t *ls1 = z_loan(s1); printf("Declaring Subscriber..."); @@ -303,7 +304,7 @@ int main(int argc, char **argv) { assert(_ret_int8 == _Z_RES_OK); printf("Ok\n"); - sleep(SLEEP); + z_sleep_s(SLEEP); char s1_res[64]; sprintf(s1_res, "%s/chunk/%d", keyexpr_str, 1); @@ -321,11 +322,16 @@ int main(int argc, char **argv) { z_owned_encoding_t _ret_encoding; zp_encoding_make(&_ret_encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); _ret_put_opt.encoding = z_move(_ret_encoding); - _ret_int8 = z_put(z_loan(s1), z_loan(_ret_expr), (const uint8_t *)value, strlen(value), &_ret_put_opt); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, value); + + _ret_int8 = z_put(z_loan(s1), z_loan(_ret_expr), z_move(payload), &_ret_put_opt); assert_eq(_ret_int8, 0); printf("Ok\n"); - sleep(SLEEP); + z_sleep_s(SLEEP); assert_eq(datas, 1); printf("Session delete..."); @@ -336,7 +342,7 @@ int main(int argc, char **argv) { assert_eq(_ret_int8, 0); printf("Ok\n"); - sleep(SLEEP); + z_sleep_s(SLEEP); assert_eq(datas, 2); printf("Undeclaring Keyexpr..."); @@ -355,16 +361,19 @@ int main(int argc, char **argv) { assert(_ret_int8 == _Z_RES_OK); printf("Ok\n"); - sleep(SLEEP); + z_sleep_s(SLEEP); printf("Publisher Put..."); + // Create payload + z_bytes_serialize_from_string(&payload, value); + z_publisher_put_options_t _ret_pput_opt; z_publisher_put_options_default(&_ret_pput_opt); - _ret_int8 = z_publisher_put(z_loan(_ret_pub), (const uint8_t *)value, strlen(value), &_ret_pput_opt); + _ret_int8 = z_publisher_put(z_loan(_ret_pub), z_move(payload), &_ret_pput_opt); assert_eq(_ret_int8, 0); printf("Ok\n"); - sleep(SLEEP); + z_sleep_s(SLEEP); assert_eq(datas, 3); printf("Publisher Delete..."); @@ -374,7 +383,7 @@ int main(int argc, char **argv) { assert_eq(_ret_int8, 0); printf("Ok\n"); - sleep(SLEEP); + z_sleep_s(SLEEP); assert_eq(datas, 4); printf("Undeclaring Publisher..."); @@ -383,7 +392,7 @@ int main(int argc, char **argv) { assert(!z_check(_ret_pub)); printf("Ok\n"); - sleep(SLEEP); + z_sleep_s(SLEEP); printf("Undeclaring Subscriber..."); _ret_int8 = z_undeclare_subscriber(z_move(_ret_sub)); @@ -391,7 +400,7 @@ int main(int argc, char **argv) { assert(!z_check(_ret_sub)); printf("Ok\n"); - sleep(SLEEP); + z_sleep_s(SLEEP); printf("Declaring Queryable..."); z_owned_closure_query_t _ret_closure_query; @@ -403,7 +412,7 @@ int main(int argc, char **argv) { _Z_RES_OK); printf("Ok\n"); - sleep(SLEEP); + z_sleep_s(SLEEP); printf("Testing Consolidations..."); const z_loaned_session_t *ls2 = z_loan(s2); @@ -429,7 +438,7 @@ int main(int argc, char **argv) { assert_eq(_ret_int8, 0); printf("Ok\n"); - sleep(SLEEP); + z_sleep_s(SLEEP); assert_eq(queries, 1); assert_eq(replies, 1); @@ -455,9 +464,9 @@ int main(int argc, char **argv) { assert_eq(_ret_int8, 0); printf("Ok\n"); - sleep(SLEEP * 5); + z_sleep_s(SLEEP * 5); - free(keyexpr_str); + z_free(keyexpr_str); return 0; } diff --git a/tests/z_channels_test.c b/tests/z_channels_test.c index b9954cdcb..ef123ad0d 100644 --- a/tests/z_channels_test.c +++ b/tests/z_channels_test.c @@ -23,7 +23,7 @@ #undef NDEBUG #include -#define SEND(channel, v) \ +#define SEND(closure, v) \ do { \ _z_sample_t s = {.keyexpr = _z_rname("key"), \ .payload = {._slice = {.start = (const uint8_t *)v, .len = strlen(v)}}, \ @@ -32,14 +32,14 @@ .kind = 0, \ .qos = {0}}; \ z_loaned_sample_t sample = _z_sample_rc_new_from_val(s); \ - z_call(channel.send, &sample); \ + z_call(closure, &sample); \ } while (0); -#define _RECV(channel, method, buf) \ +#define _RECV(handler, method, buf) \ do { \ z_owned_sample_t sample; \ z_sample_null(&sample); \ - z_call(channel.method, &sample); \ + method(z_loan(handler), &sample); \ if (z_check(sample)) { \ z_owned_slice_t value; \ z_bytes_deserialize_into_slice(z_sample_payload(z_loan(sample)), &value); \ @@ -53,111 +53,119 @@ } \ } while (0); -#define RECV(channel, buf) _RECV(channel, recv, buf) -#define TRY_RECV(channel, buf) _RECV(channel, try_recv, buf) +#define RECV(handler, buf) _RECV(handler, z_recv, buf) +#define TRY_RECV(handler, buf) _RECV(handler, z_try_recv, buf) void sample_fifo_channel_test(void) { - z_owned_sample_fifo_channel_t channel; - z_sample_fifo_channel_new(&channel, 10); + z_owned_closure_sample_t closure; + z_owned_fifo_handler_sample_t handler; + z_fifo_channel_sample_new(&closure, &handler, 10); - SEND(channel, "v1") - SEND(channel, "v22") - SEND(channel, "v333") - SEND(channel, "v4444") + SEND(closure, "v1") + SEND(closure, "v22") + SEND(closure, "v333") + SEND(closure, "v4444") char buf[100]; - RECV(channel, buf) + RECV(handler, buf) assert(strcmp(buf, "v1") == 0); - RECV(channel, buf) + RECV(handler, buf) assert(strcmp(buf, "v22") == 0); - RECV(channel, buf) + RECV(handler, buf) assert(strcmp(buf, "v333") == 0); - RECV(channel, buf) + RECV(handler, buf) assert(strcmp(buf, "v4444") == 0); - z_drop(z_move(channel)); + z_drop(z_move(handler)); + z_drop(z_move(closure)); } void sample_fifo_channel_test_try_recv(void) { - z_owned_sample_fifo_channel_t channel; - z_sample_fifo_channel_new(&channel, 10); + z_owned_closure_sample_t closure; + z_owned_fifo_handler_sample_t handler; + z_fifo_channel_sample_new(&closure, &handler, 10); char buf[100]; - TRY_RECV(channel, buf) + TRY_RECV(handler, buf) assert(strcmp(buf, "") == 0); - SEND(channel, "v1") - SEND(channel, "v22") - SEND(channel, "v333") - SEND(channel, "v4444") + SEND(closure, "v1") + SEND(closure, "v22") + SEND(closure, "v333") + SEND(closure, "v4444") - TRY_RECV(channel, buf) + TRY_RECV(handler, buf) assert(strcmp(buf, "v1") == 0); - TRY_RECV(channel, buf) + TRY_RECV(handler, buf) assert(strcmp(buf, "v22") == 0); - TRY_RECV(channel, buf) + TRY_RECV(handler, buf) assert(strcmp(buf, "v333") == 0); - TRY_RECV(channel, buf) + TRY_RECV(handler, buf) assert(strcmp(buf, "v4444") == 0); - TRY_RECV(channel, buf) + TRY_RECV(handler, buf) assert(strcmp(buf, "") == 0); - z_drop(z_move(channel)); + z_drop(z_move(handler)); + z_drop(z_move(closure)); } void sample_ring_channel_test_in_size(void) { - z_owned_sample_ring_channel_t channel; - z_sample_ring_channel_new(&channel, 10); + z_owned_closure_sample_t closure; + z_owned_ring_handler_sample_t handler; + z_ring_channel_sample_new(&closure, &handler, 10); char buf[100]; - TRY_RECV(channel, buf) + TRY_RECV(handler, buf) assert(strcmp(buf, "") == 0); - SEND(channel, "v1") - SEND(channel, "v22") - SEND(channel, "v333") - SEND(channel, "v4444") + SEND(closure, "v1") + SEND(closure, "v22") + SEND(closure, "v333") + SEND(closure, "v4444") - RECV(channel, buf) + RECV(handler, buf) assert(strcmp(buf, "v1") == 0); - RECV(channel, buf) + RECV(handler, buf) assert(strcmp(buf, "v22") == 0); - RECV(channel, buf) + RECV(handler, buf) assert(strcmp(buf, "v333") == 0); - RECV(channel, buf) + RECV(handler, buf) assert(strcmp(buf, "v4444") == 0); - TRY_RECV(channel, buf) + TRY_RECV(handler, buf) assert(strcmp(buf, "") == 0); - z_drop(z_move(channel)); + z_drop(z_move(handler)); + z_drop(z_move(closure)); } void sample_ring_channel_test_over_size(void) { - z_owned_sample_ring_channel_t channel; - z_sample_ring_channel_new(&channel, 3); + z_owned_closure_sample_t closure; + z_owned_ring_handler_sample_t handler; + z_ring_channel_sample_new(&closure, &handler, 3); char buf[100]; - TRY_RECV(channel, buf) + TRY_RECV(handler, buf) assert(strcmp(buf, "") == 0); - SEND(channel, "v1") - SEND(channel, "v22") - SEND(channel, "v333") - SEND(channel, "v4444") + SEND(closure, "v1") + SEND(closure, "v22") + SEND(closure, "v333") + SEND(closure, "v4444") - RECV(channel, buf) + RECV(handler, buf) assert(strcmp(buf, "v22") == 0); - RECV(channel, buf) + RECV(handler, buf) assert(strcmp(buf, "v333") == 0); - RECV(channel, buf) + RECV(handler, buf) assert(strcmp(buf, "v4444") == 0); - TRY_RECV(channel, buf) + TRY_RECV(handler, buf) assert(strcmp(buf, "") == 0); - z_drop(z_move(channel)); + z_drop(z_move(handler)); + z_drop(z_move(closure)); } int main(void) { diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 4a8813e3d..206573196 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -231,8 +231,8 @@ int main(int argc, char **argv) { // Write data from first session size_t len = MSG_LEN; - uint8_t *payload = (uint8_t *)z_malloc(len); - memset(payload, 1, MSG_LEN); + uint8_t *value = (uint8_t *)z_malloc(len); + memset(value, 1, MSG_LEN); total = MSG * SET; for (unsigned int n = 0; n < MSG; n++) { @@ -240,7 +240,12 @@ int main(int argc, char **argv) { z_put_options_t opt; z_put_options_default(&opt); opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; - z_put(z_loan(s1), z_loan(rids1[i]), (const uint8_t *)payload, len, &opt); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, value, len); + + z_put(z_loan(s1), z_loan(rids1[i]), z_move(payload), &opt); printf("Wrote data from session 1: %u %zu b\t(%u/%u)\n", z_loan(rids1[i])->_id, len, n * SET + (i + 1), total); } @@ -264,10 +269,10 @@ int main(int argc, char **argv) { // Write fragment data from first session if (is_reliable) { - z_free((uint8_t *)payload); + z_free((uint8_t *)value); len = FRAGMENT_MSG_LEN; - payload = (uint8_t *)z_malloc(len); - memset(payload, 1, FRAGMENT_MSG_LEN); + value = (uint8_t *)z_malloc(len); + memset(value, 1, FRAGMENT_MSG_LEN); total = FRAGMENT_MSG_NB * SET; for (unsigned int n = 0; n < FRAGMENT_MSG_NB; n++) { @@ -275,7 +280,12 @@ int main(int argc, char **argv) { z_put_options_t opt; z_put_options_default(&opt); opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; - z_put(z_loan(s1), z_loan(rids1[i]), (const uint8_t *)payload, len, &opt); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, value, len); + + z_put(z_loan(s1), z_loan(rids1[i]), z_move(payload), &opt); printf("Wrote fragment data from session 1: %u %zu b\t(%u/%u)\n", z_loan(rids1[i])->_id, len, n * SET + (i + 1), total); } @@ -399,8 +409,8 @@ int main(int argc, char **argv) { printf("Closing session 2\n"); z_close(z_move(s2)); - z_free((uint8_t *)payload); - payload = NULL; + z_free((uint8_t *)value); + value = NULL; free(s1_res); diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 7413cfb0d..66b7034c1 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -128,8 +128,8 @@ int main(int argc, char **argv) { // Write data from first session size_t len = MSG_LEN; - uint8_t *payload = (uint8_t *)z_malloc(len); - memset(payload, 1, MSG_LEN); + uint8_t *value = (uint8_t *)z_malloc(len); + memset(value, 1, MSG_LEN); total = MSG * SET; for (unsigned int n = 0; n < MSG; n++) { @@ -141,7 +141,12 @@ int main(int argc, char **argv) { opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; z_view_keyexpr_t ke; z_view_keyexpr_from_string(&ke, s1_res); - z_put(z_loan(s1), z_loan(ke), (const uint8_t *)payload, len, &opt); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, value, len); + + z_put(z_loan(s1), z_loan(ke), z_move(payload), &opt); printf("Wrote data from session 1: %s %zu b\t(%u/%u)\n", s1_res, len, n * SET + (i + 1), total); } } @@ -191,8 +196,8 @@ int main(int argc, char **argv) { printf("Closing session 2\n"); z_close(z_move(s2)); - z_free((uint8_t *)payload); - payload = NULL; + z_free((uint8_t *)value); + value = NULL; free(s1_res); diff --git a/tests/z_perf_tx.c b/tests/z_perf_tx.c index be324d3b7..de02b06b5 100644 --- a/tests/z_perf_tx.c +++ b/tests/z_perf_tx.c @@ -26,7 +26,11 @@ int send_packets(unsigned long pkt_len, z_owned_publisher_t *pub, uint8_t *value z_clock_t test_start = z_clock_now(); unsigned long elapsed_us = 0; while (elapsed_us < TEST_DURATION_US) { - z_publisher_put(z_loan(*pub), (const uint8_t *)value, pkt_len, NULL); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, value, pkt_len); + + z_publisher_put(z_loan(*pub), z_move(payload), NULL); elapsed_us = z_clock_elapsed_us(&test_start); } return 0; @@ -97,7 +101,11 @@ int main(int argc, char **argv) { } // Send end packet printf("Sending end pkt\n"); - z_publisher_put(z_loan(pub), (const uint8_t *)value, 1, NULL); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, value, 1); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); // Clean up z_undeclare_publisher(z_move(pub)); zp_stop_read_task(z_loan_mut(s)); diff --git a/tests/z_test_fragment_tx.c b/tests/z_test_fragment_tx.c index 0e6658565..6e19e0eab 100644 --- a/tests/z_test_fragment_tx.c +++ b/tests/z_test_fragment_tx.c @@ -77,8 +77,12 @@ int main(int argc, char **argv) { z_view_keyexpr_t ke; z_view_keyexpr_from_string(&ke, keyexpr); for (int i = 0; i < 5; i++) { + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, value, size); + printf("[tx]: Sending packet on %s, len: %d\n", keyexpr, (int)size); - if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)value, size, NULL) < 0) { + if (z_put(z_loan(s), z_loan(ke), z_move(payload), NULL) < 0) { printf("Oh no! Put has failed...\n"); } z_sleep_s(1); From 61afacc94ed8cda19ec469861475557827b6ee47 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Mon, 24 Jun 2024 10:45:33 +0200 Subject: [PATCH 04/25] warning fixes --- include/zenoh-pico/collections/arc_slice.h | 4 ++-- include/zenoh-pico/collections/bytes.h | 4 ++-- src/collections/arc_slice.c | 2 +- src/collections/bytes.c | 3 +-- src/collections/vec.c | 20 ++++++++++---------- tests/z_bytes_test.c | 17 ++++++++--------- 6 files changed, 24 insertions(+), 26 deletions(-) diff --git a/include/zenoh-pico/collections/arc_slice.h b/include/zenoh-pico/collections/arc_slice.h index 8c9738918..c3f238dc0 100644 --- a/include/zenoh-pico/collections/arc_slice.h +++ b/include/zenoh-pico/collections/arc_slice.h @@ -25,7 +25,7 @@ #include "zenoh-pico/system/platform-common.h" -_Z_REFCOUNT_DEFINE(_z_slice, _z_slice); +_Z_REFCOUNT_DEFINE(_z_slice, _z_slice) /*-------- ArcSlice --------*/ /** @@ -53,4 +53,4 @@ int8_t _z_arc_slice_copy(_z_arc_slice_t *dst, const _z_arc_slice_t *src); int8_t _z_arc_slice_move(_z_arc_slice_t *dst, _z_arc_slice_t *src); int8_t _z_arc_slice_drop(_z_arc_slice_t *s); -#endif /* ZENOH_PICO_COLLECTIONS_ARC_SLICE_H */ \ No newline at end of file +#endif /* ZENOH_PICO_COLLECTIONS_ARC_SLICE_H */ diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index 471cbee8a..9ac6d7514 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -26,7 +26,7 @@ inline size_t _z_arc_slice_size(const _z_arc_slice_t *s) { (void)s; - return sizeof(_z_arc_slice_size); + return sizeof(_z_arc_slice_t); } static inline void _z_arc_slice_elem_move(void *dst, void *src) { _z_arc_slice_move((_z_arc_slice_t *)dst, (_z_arc_slice_t*)src); @@ -95,4 +95,4 @@ int8_t _zz_bytes_reader_read_slices(_zz_bytes_reader_t* reader, size_t len, _zz_ int8_t _zz_bytes_reader_read(_zz_bytes_reader_t *reader, uint8_t* buf, size_t len); int8_t _zz_bytes_reader_read_next(_zz_bytes_reader_t* reader, _zz_bytes_t* out); -#endif /* ZENOH_PICO_COLLECTIONS_BYTES_H */ \ No newline at end of file +#endif /* ZENOH_PICO_COLLECTIONS_BYTES_H */ diff --git a/src/collections/arc_slice.c b/src/collections/arc_slice.c index ce698bab6..eee2a9828 100644 --- a/src/collections/arc_slice.c +++ b/src/collections/arc_slice.c @@ -88,4 +88,4 @@ int8_t _z_arc_slice_drop(_z_arc_slice_t *s) { s->len = 0; s->start = 0; return _Z_RES_OK; -} \ No newline at end of file +} diff --git a/src/collections/bytes.c b/src/collections/bytes.c index e6a26fc28..fc466be50 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -424,7 +424,6 @@ int8_t _zz_bytes_reader_read_next(_zz_bytes_reader_t* reader, _zz_bytes_t* out) if (_zz_bytes_reader_read_zint(reader, &len) != _Z_RES_OK) { return _Z_ERR_DID_NOT_READ; } - size_t offset = _zz_bytes_reader_tell(reader); return _zz_bytes_reader_read_slices(reader, len, out); -} \ No newline at end of file +} diff --git a/src/collections/vec.c b/src/collections/vec.c index 51374a500..9488acda4 100644 --- a/src/collections/vec.c +++ b/src/collections/vec.c @@ -148,7 +148,7 @@ void __z_svec_copy_inner(void *dst, const void *src, z_element_copy_f copy, size } else { size_t offset = 0; for (size_t i = 0; i < num_elements; i++) { - copy(dst + offset, src + offset); + copy((uint8_t*)dst + offset, (uint8_t*)src + offset); offset += element_size; } } @@ -160,7 +160,7 @@ void __z_svec_move_inner(void *dst, void *src, z_element_move_f move, size_t num } else { size_t offset = 0; for (size_t i = 0; i < num_elements; i++) { - move(dst + offset, src + offset); + move((uint8_t*)dst + offset, (uint8_t*)src + offset); offset += element_size; } } @@ -181,7 +181,7 @@ void _z_svec_reset(_z_svec_t *v, z_element_clear_f clear, size_t element_size) { if (clear != NULL) { size_t offset = 0; for (size_t i = 0; i < v->_len; i++) { - clear(v->_val + offset); + clear((uint8_t*)v->_val + offset); offset += element_size; } } @@ -229,13 +229,13 @@ bool _z_svec_append(_z_svec_t *v, const void *e, z_element_move_f move, size_t e // Update the current vector v->_val = _val; v->_capacity = _capacity; - memcpy(v->_val + v->_len * element_size, e, element_size); + memcpy((uint8_t*)v->_val + v->_len * element_size, e, element_size); v->_len++; } else { return false; } } else { - memcpy(v->_val + v->_len * element_size, e, element_size); + memcpy((uint8_t*)v->_val + v->_len * element_size, e, element_size); v->_len++; } return true; @@ -243,20 +243,20 @@ bool _z_svec_append(_z_svec_t *v, const void *e, z_element_move_f move, size_t e void *_z_svec_get(const _z_svec_t *v, size_t i, size_t element_size) { assert(i < v->_len); - return v->_val + i * element_size; + return (uint8_t*)v->_val + i * element_size; } void _z_svec_set(_z_svec_t *v, size_t i, void *e, z_element_clear_f clear, size_t element_size) { assert(i < v->_len); - clear(v->_val + i * element_size); - memcpy(v->_val + i * element_size, e, element_size); + clear((uint8_t*)v->_val + i * element_size); + memcpy((uint8_t*)v->_val + i * element_size, e, element_size); } void _z_svec_remove(_z_svec_t *v, size_t pos, z_element_clear_f clear, z_element_move_f move, size_t element_size) { assert(pos < v->_len); - clear(v->_val + pos * element_size); + clear((uint8_t*)v->_val + pos * element_size); __z_svec_move_inner( - v->_val + pos * element_size, v->_val + (pos + 1) * element_size, + (uint8_t*)v->_val + pos * element_size, (uint8_t*)v->_val + (pos + 1) * element_size, move, (v->_len - pos - 1) * element_size, element_size ); diff --git a/tests/z_bytes_test.c b/tests/z_bytes_test.c index f14ba43af..874bb8ab1 100644 --- a/tests/z_bytes_test.c +++ b/tests/z_bytes_test.c @@ -21,7 +21,7 @@ #undef NDEBUG #include -void test_null_bytes() { +void test_null_bytes(void) { _zz_bytes_t b = _zz_bytes_null(); assert(_zz_bytes_len(&b) == 0); assert(_zz_bytes_is_empty(&b)); @@ -30,9 +30,9 @@ void test_null_bytes() { _zz_bytes_drop(&b); // no crush } -void test_slice() { +void test_slice(void) { uint8_t data[5] = {1, 2, 3, 4, 5}; - uint8_t data_out[5] = {}; + uint8_t data_out[5] = {0}; _z_slice_t s = _z_slice_wrap_copy(data, 5); _zz_bytes_t b = _zz_bytes_from_slice(s); @@ -48,12 +48,12 @@ void test_slice() { _zz_bytes_drop(&b); } -void test_append() { +void test_append(void) { uint8_t data1[5] = {1, 2, 3, 4, 5}; uint8_t data2[5] = {1, 2, 6, 7, 8}; uint8_t data3[3] = {3, 9, 10}; uint8_t data_in[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - uint8_t data_out[10] = {}; + uint8_t data_out[10] = {0}; _z_arc_slice_t s1 = _z_arc_slice_wrap(_z_slice_wrap_copy(data1, 5), 0, 5); _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_wrap_copy(data2, 5), 2, 3); _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); @@ -78,12 +78,12 @@ void test_append() { _zz_bytes_drop(&b); } -void test_reader_read() { +void test_reader_read(void) { uint8_t data1[5] = {1, 2, 3, 4, 5}; uint8_t data2[5] = {1, 2, 6, 7, 8}; uint8_t data3[3] = {3, 9, 10}; uint8_t data_in[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - uint8_t data_out[10] = {}; + uint8_t data_out[10] = {0}; _z_arc_slice_t s1 = _z_arc_slice_wrap(_z_slice_wrap_copy(data1, 5), 0, 5); _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_wrap_copy(data2, 5), 2, 3); _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); @@ -113,7 +113,7 @@ void test_reader_read() { _zz_bytes_drop(&b); } -void test_reader_seek() { +void test_reader_seek(void) { uint8_t data1[5] = {1, 2, 3, 4, 5}; uint8_t data2[5] = {1, 2, 6, 7, 8}; uint8_t data3[3] = {3, 9, 10}; @@ -129,7 +129,6 @@ void test_reader_seek() { _zz_bytes_reader_t reader = _zz_bytes_get_reader(&b); - uint8_t out; assert(_zz_bytes_reader_tell(&reader) == 0); assert(_zz_bytes_reader_seek(&reader, 3, SEEK_CUR) == 0); assert(_zz_bytes_reader_tell(&reader) == 3); From ed88be7215c56a9f9089a5757d91395bdf214309 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Mon, 24 Jun 2024 17:59:29 +0200 Subject: [PATCH 05/25] use new zz_bytes on user side --- examples/unix/c11/z_get_attachment.c | 25 +- examples/unix/c11/z_pub_attachment.c | 18 +- examples/unix/c11/z_queryable_attachment.c | 14 +- examples/unix/c11/z_sub_attachment.c | 8 +- include/zenoh-pico/api/primitives.h | 40 ++- include/zenoh-pico/api/types.h | 10 +- include/zenoh-pico/collections/arc_slice.h | 11 +- include/zenoh-pico/collections/bytes.h | 70 ++--- include/zenoh-pico/collections/slice.h | 35 +-- include/zenoh-pico/collections/vec.h | 50 ++-- include/zenoh-pico/net/primitives.h | 13 +- include/zenoh-pico/net/query.h | 5 +- include/zenoh-pico/net/reply.h | 4 +- include/zenoh-pico/net/sample.h | 8 +- include/zenoh-pico/protocol/codec/core.h | 17 +- include/zenoh-pico/protocol/core.h | 5 +- .../zenoh-pico/protocol/definitions/message.h | 8 +- .../zenoh-pico/protocol/definitions/network.h | 2 +- include/zenoh-pico/session/queryable.h | 2 +- include/zenoh-pico/session/subscription.h | 8 +- src/api/api.c | 274 +++++++----------- src/collections/arc_slice.c | 24 +- src/collections/bytes.c | 228 ++++++++------- src/collections/slice.c | 165 +---------- src/collections/vec.c | 30 +- src/net/memory.c | 2 +- src/net/primitives.c | 14 +- src/net/query.c | 6 +- src/net/reply.c | 14 +- src/net/sample.c | 30 +- src/protocol/codec.c | 62 +++- src/protocol/codec/message.c | 43 +-- src/protocol/core.c | 4 +- src/protocol/definitions/message.c | 8 +- src/protocol/definitions/network.c | 2 +- src/session/push.c | 2 +- src/session/query.c | 2 +- src/session/queryable.c | 4 +- src/session/rx.c | 6 +- src/session/subscription.c | 20 +- tests/z_bytes_test.c | 16 +- tests/z_channels_test.c | 22 +- tests/z_msgcodec_test.c | 72 +++-- 43 files changed, 600 insertions(+), 803 deletions(-) diff --git a/examples/unix/c11/z_get_attachment.c b/examples/unix/c11/z_get_attachment.c index f807ee51b..59c42c682 100644 --- a/examples/unix/c11/z_get_attachment.c +++ b/examples/unix/c11/z_get_attachment.c @@ -46,18 +46,7 @@ typedef struct kv_pairs_rx_t { static z_condvar_t cond; static z_mutex_t mutex; -size_t kv_pairs_size(kv_pairs_tx_t *kvp) { - size_t ret = 0; - for (size_t i = 0; i < kvp->len; i++) { - // Size fields - ret += 2 * sizeof(uint32_t); - // Data size - ret += strlen(kvp->data[i].key) + strlen(kvp->data[i].value); - } - return ret; -} - -_Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context, size_t *curr_idx) { +_Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context) { kv_pairs_tx_t *kvs = (kv_pairs_tx_t *)(context); z_owned_bytes_t k, v; if (kvs->current_idx >= kvs->len) { @@ -65,17 +54,17 @@ _Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context, size_t *cu } else { z_bytes_serialize_from_string(&k, kvs->data[kvs->current_idx].key); z_bytes_serialize_from_string(&v, kvs->data[kvs->current_idx].value); - zp_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v), curr_idx); + z_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v)); kvs->current_idx++; return true; } } void parse_attachment(kv_pairs_rx_t *kvp, const z_loaned_bytes_t *attachment) { - size_t curr_idx = 0; - z_owned_bytes_t first, second; - while ((kvp->current_idx < kvp->len) && - (zp_bytes_deserialize_into_pair(attachment, &first, &second, &curr_idx) == 0)) { + z_owned_bytes_t kv, first, second; + z_bytes_iterator_t iter = z_bytes_get_iterator(attachment); + + while (kvp->current_idx < kvp->len && z_bytes_iterator_next(&iter, &kv)) { z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key); z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value); z_bytes_drop(&first); @@ -220,7 +209,7 @@ int main(int argc, char **argv) { kvs[0] = (kv_pair_t){.key = "test_key", .value = "test_value"}; kv_pairs_tx_t ctx = (kv_pairs_tx_t){.data = kvs, .current_idx = 0, .len = 1}; z_owned_bytes_t attachment; - zp_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx, kv_pairs_size(&ctx)); + z_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx); opts.attachment = z_move(attachment); z_owned_closure_reply_t callback; diff --git a/examples/unix/c11/z_pub_attachment.c b/examples/unix/c11/z_pub_attachment.c index 2376ffd7f..023c31dea 100644 --- a/examples/unix/c11/z_pub_attachment.c +++ b/examples/unix/c11/z_pub_attachment.c @@ -35,19 +35,7 @@ typedef struct kv_pairs_t { #if Z_FEATURE_PUBLICATION == 1 -// Return the total serialized size of the key value pairs -size_t kv_pairs_size(kv_pairs_t *kvp) { - size_t ret = 0; - for (size_t i = 0; i < kvp->len; i++) { - // Size fields - ret += 2 * sizeof(uint32_t); - // Data size - ret += strlen(kvp->data[i].key) + strlen(kvp->data[i].value); - } - return ret; -} - -_Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context, size_t *curr_idx) { +_Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context) { kv_pairs_t *kvs = (kv_pairs_t *)(context); z_owned_bytes_t k, v; if (kvs->current_idx >= kvs->len) { @@ -55,7 +43,7 @@ _Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context, size_t *cu } else { z_bytes_serialize_from_string(&k, kvs->data[kvs->current_idx].key); z_bytes_serialize_from_string(&v, kvs->data[kvs->current_idx].value); - zp_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v), curr_idx); + z_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v)); kvs->current_idx++; return true; } @@ -169,7 +157,7 @@ int main(int argc, char **argv) { sprintf(buf_ind, "%d", idx); kvs[1] = (kv_pair_t){.key = "index", .value = buf_ind}; kv_pairs_t ctx = (kv_pairs_t){.data = kvs, .current_idx = 0, .len = 2}; - zp_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx, kv_pairs_size(&ctx)); + z_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx); options.attachment = z_move(attachment); z_publisher_put(z_loan(pub), z_move(payload), &options); diff --git a/examples/unix/c11/z_queryable_attachment.c b/examples/unix/c11/z_queryable_attachment.c index ad3d7f693..b0d872019 100644 --- a/examples/unix/c11/z_queryable_attachment.c +++ b/examples/unix/c11/z_queryable_attachment.c @@ -58,7 +58,7 @@ size_t kv_pairs_size(kv_pairs_tx_t *kvp) { return ret; } -_Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context, size_t *curr_idx) { +_Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context) { kv_pairs_tx_t *kvs = (kv_pairs_tx_t *)(context); z_owned_bytes_t k, v; if (kvs->current_idx >= kvs->len) { @@ -66,17 +66,17 @@ _Bool create_attachment_iter(z_owned_bytes_t *kv_pair, void *context, size_t *cu } else { z_bytes_serialize_from_string(&k, kvs->data[kvs->current_idx].key); z_bytes_serialize_from_string(&v, kvs->data[kvs->current_idx].value); - zp_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v), curr_idx); + z_bytes_serialize_from_pair(kv_pair, z_move(k), z_move(v)); kvs->current_idx++; return true; } } void parse_attachment(kv_pairs_rx_t *kvp, const z_loaned_bytes_t *attachment) { - size_t curr_idx = 0; - z_owned_bytes_t first, second; - while ((kvp->current_idx < kvp->len) && - (zp_bytes_deserialize_into_pair(attachment, &first, &second, &curr_idx) == 0)) { + z_owned_bytes_t kv, first, second; + z_bytes_iterator_t iter = z_bytes_get_iterator(attachment); + + while (kvp->current_idx < kvp->len && z_bytes_iterator_next(&iter, &kv)) { z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key); z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value); z_bytes_drop(&first); @@ -142,7 +142,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { kvs[0] = (kv_pair_t){.key = "reply_key", .value = "reply_value"}; kv_pairs_tx_t kv_ctx = (kv_pairs_tx_t){.data = kvs, .current_idx = 0, .len = 1}; z_owned_bytes_t attachment; - zp_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&kv_ctx, kv_pairs_size(&kv_ctx)); + z_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&kv_ctx); options.attachment = z_move(attachment); z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); diff --git a/examples/unix/c11/z_sub_attachment.c b/examples/unix/c11/z_sub_attachment.c index 32a7c0d0a..413532f05 100644 --- a/examples/unix/c11/z_sub_attachment.c +++ b/examples/unix/c11/z_sub_attachment.c @@ -38,10 +38,10 @@ typedef struct kv_pairs_t { static int msg_nb = 0; void parse_attachment(kv_pairs_t *kvp, const z_loaned_bytes_t *attachment) { - size_t curr_idx = 0; - z_owned_bytes_t first, second; - while ((kvp->current_idx < kvp->len) && - (zp_bytes_deserialize_into_pair(attachment, &first, &second, &curr_idx) == 0)) { + z_owned_bytes_t kv, first, second; + z_bytes_iterator_t iter = z_bytes_get_iterator(attachment); + + while (kvp->current_idx < kvp->len && z_bytes_iterator_next(&iter, &kv)) { z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key); z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value); z_bytes_drop(&first); diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 7b5167615..16fb380b3 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -616,13 +616,11 @@ int8_t z_bytes_deserialize_into_string(const z_loaned_bytes_t *bytes, z_owned_st * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. * first: Pointer to an uninitialized :c:type:`z_owned_bytes_t` to contain the first element. * second: Pointer to an uninitialized :c:type:`z_owned_bytes_t` to contain the second element. - * curr_idx: Pointer to the current decoding index. * * Return: * ``0`` if decode successful, or a ``negative value`` otherwise. */ -int8_t zp_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second, - size_t *curr_idx); +int8_t z_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second); /** * Encodes a signed integer into a :c:type:`z_owned_bytes_t` @@ -798,29 +796,49 @@ int8_t z_bytes_serialize_from_string_copy(z_owned_bytes_t *bytes, const char *s) * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded payload. * iterator_body: Iterator body function, providing data items. Returning false is treated as iteration end. * context: Arbitrary context that will be passed to iterator_body. - * total_len: The length of all the items to encode. * * Return: * ``0`` if encode successful, ``negative value`` otherwise. */ -int8_t zp_bytes_serialize_from_iter(z_owned_bytes_t *bytes, - _Bool (*iterator_body)(z_owned_bytes_t *data, void *context, size_t *curr_idx), - void *context, size_t total_len); +int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body)(z_owned_bytes_t *data, void *context), + void *context); /** * Append a pair of `z_owned_bytes` objects which are consumed in the process. * * Parameters: - * bytes: An pre-initialized :c:type:`z_owned_bytes_t` to contain the encoded pair. + * bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded pair. * first: Pointer to the first `z_owned_bytes` to encode. * second: Pointer to the second `z_owned_bytes` to encode. - * curr_idx: Pointer to the current encoding index value. * * Return: * ``0`` if encode successful, ``negative value`` otherwise. */ -int8_t zp_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second, - size_t *curr_idx); +int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second); + +/** + * Returns an iterator for multi-element serialized data. + * + * Parameters: + * bytes: Data to iterate over. + * + * Return: + * The constructed :c:type:`z_bytes_iterator_t`. + */ +z_bytes_iterator_t z_bytes_get_iterator(const z_loaned_bytes_t *bytes); + +/** + * Constructs :c:type:`z_owned_bytes_t` object corresponding to the next element of serialized data. + * + * Will construct null-state `z_owned_bytes_t` when iterator reaches the end (or in case of error). + * + * Parameters: + * iter: An iterator over multi-element serialized data. + * out: An uninitialized :c:type:`z_owned_bytes_t` that will contained next serialized element. + * Return: + * ``false`` when iterator reaches the end, ``true`` otherwise + */ +_Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *out); /** * Checks validity of a timestamp diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 77b2b0195..342d424d9 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -16,6 +16,7 @@ #define INCLUDE_ZENOH_PICO_API_TYPES_H #include "olv_macros.h" +#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/element.h" #include "zenoh-pico/collections/list.h" #include "zenoh-pico/collections/slice.h" @@ -71,8 +72,13 @@ _Z_LOANED_TYPE(_z_slice_t, slice) * Members: * _z_slice_t slice: content of the container. */ -_Z_OWNED_TYPE_PTR(_z_bytes_t, bytes) -_Z_LOANED_TYPE(_z_bytes_t, bytes) +_Z_OWNED_TYPE_PTR(_zz_bytes_t, bytes) +_Z_LOANED_TYPE(_zz_bytes_t, bytes) + +/** + * An iterator over multi-element serialized data + */ +typedef _zz_bytes_iterator_t z_bytes_iterator_t; /** * Represents a string without null-terminator. diff --git a/include/zenoh-pico/collections/arc_slice.h b/include/zenoh-pico/collections/arc_slice.h index c3f238dc0..2e6c1bf67 100644 --- a/include/zenoh-pico/collections/arc_slice.h +++ b/include/zenoh-pico/collections/arc_slice.h @@ -18,13 +18,12 @@ #include #include #include -#include +#include -#include "slice.h" #include "refcount.h" +#include "slice.h" #include "zenoh-pico/system/platform-common.h" - _Z_REFCOUNT_DEFINE(_z_slice, _z_slice) /*-------- ArcSlice --------*/ @@ -49,8 +48,8 @@ _z_arc_slice_t _z_arc_slice_get_subslice(const _z_arc_slice_t* s, size_t offset, size_t _z_arc_slice_len(const _z_arc_slice_t* s); _Bool _z_arc_slice_is_empty(const _z_arc_slice_t* s); const uint8_t* _z_arc_slice_data(const _z_arc_slice_t* s); -int8_t _z_arc_slice_copy(_z_arc_slice_t *dst, const _z_arc_slice_t *src); -int8_t _z_arc_slice_move(_z_arc_slice_t *dst, _z_arc_slice_t *src); -int8_t _z_arc_slice_drop(_z_arc_slice_t *s); +int8_t _z_arc_slice_copy(_z_arc_slice_t* dst, const _z_arc_slice_t* src); +int8_t _z_arc_slice_move(_z_arc_slice_t* dst, _z_arc_slice_t* src); +int8_t _z_arc_slice_drop(_z_arc_slice_t* s); #endif /* ZENOH_PICO_COLLECTIONS_ARC_SLICE_H */ diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index 9ac6d7514..cb4184f31 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -18,23 +18,21 @@ #include #include #include -#include "vec.h" + #include "arc_slice.h" +#include "vec.h" #include "zenoh-pico/protocol/iobuf.h" - - inline size_t _z_arc_slice_size(const _z_arc_slice_t *s) { (void)s; - return sizeof(_z_arc_slice_t); + return sizeof(_z_arc_slice_t); } static inline void _z_arc_slice_elem_move(void *dst, void *src) { - _z_arc_slice_move((_z_arc_slice_t *)dst, (_z_arc_slice_t*)src); + _z_arc_slice_move((_z_arc_slice_t *)dst, (_z_arc_slice_t *)src); } _Z_ELEM_DEFINE(_z_arc_slice, _z_arc_slice_t, _z_arc_slice_size, _z_arc_slice_drop, _z_arc_slice_copy) _Z_SVEC_DEFINE(_z_arc_slice, _z_arc_slice_t) - /*-------- Bytes --------*/ /** * A container for slices. @@ -47,52 +45,58 @@ typedef struct { _z_arc_slice_svec_t _slices; } _zz_bytes_t; - _Bool _zz_bytes_check(const _zz_bytes_t *bytes); _zz_bytes_t _zz_bytes_null(void); -int8_t _zz_bytes_append(_zz_bytes_t* dst, _zz_bytes_t* src); -int8_t _zz_bytes_append_slice(_zz_bytes_t* dst, _z_arc_slice_t* s); +int8_t _zz_bytes_append(_zz_bytes_t *dst, _zz_bytes_t *src); +int8_t _zz_bytes_append_slice(_zz_bytes_t *dst, _z_arc_slice_t *s); int8_t _zz_bytes_copy(_zz_bytes_t *dst, const _zz_bytes_t *src); _zz_bytes_t _zz_bytes_duplicate(const _zz_bytes_t *src); void _zz_bytes_move(_zz_bytes_t *dst, _zz_bytes_t *src); void _zz_bytes_drop(_zz_bytes_t *bytes); void _zz_bytes_free(_zz_bytes_t **bs); size_t _zz_bytes_num_slices(const _zz_bytes_t *bs); -_z_arc_slice_t* _zz_bytes_get_slice(const _zz_bytes_t *bs, size_t i); +_z_arc_slice_t *_zz_bytes_get_slice(const _zz_bytes_t *bs, size_t i); size_t _zz_bytes_len(const _zz_bytes_t *bs); _Bool _zz_bytes_is_empty(const _zz_bytes_t *bs); -uint8_t _zz_bytes_to_uint8(const _zz_bytes_t *bs); -uint16_t _zz_bytes_to_uint16(const _zz_bytes_t *bs); -uint32_t _zz_bytes_to_uint32(const _zz_bytes_t *bs); -uint64_t _zz_bytes_to_uint64(const _zz_bytes_t *bs); -float _zz_bytes_to_float(const _zz_bytes_t *bs); -double _zz_bytes_to_double(const _zz_bytes_t *bs); -_z_slice_t _zz_bytes_to_slice(const _zz_bytes_t *bytes); -_zz_bytes_t _zz_bytes_from_slice(_z_slice_t s); -_zz_bytes_t _zz_bytes_from_uint8(uint8_t val); -_zz_bytes_t _zz_bytes_from_uint16(uint16_t val); -_zz_bytes_t _zz_bytes_from_uint32(uint32_t val); -_zz_bytes_t _zz_bytes_from_uint64(uint64_t val); -_zz_bytes_t _zz_bytes_from_float(float val); -_zz_bytes_t _zz_bytes_from_double(double val); -size_t _zz_bytes_to_buf(const _zz_bytes_t *bytes, uint8_t* dst, size_t len); -_zz_bytes_t _zz_bytes_from_buf(uint8_t* src, size_t len); - +int8_t _zz_bytes_to_uint8(const _zz_bytes_t *bs, uint8_t *u); +int8_t _zz_bytes_to_uint16(const _zz_bytes_t *bs, uint16_t *u); +int8_t _zz_bytes_to_uint32(const _zz_bytes_t *bs, uint32_t *u); +int8_t _zz_bytes_to_uint64(const _zz_bytes_t *bs, uint64_t *u); +int8_t _zz_bytes_to_float(const _zz_bytes_t *bs, float *f); +int8_t _zz_bytes_to_double(const _zz_bytes_t *bs, double *d); +int8_t _zz_bytes_to_slice(const _zz_bytes_t *bytes, _z_slice_t *s); +int8_t _zz_bytes_from_slice(_zz_bytes_t *b, _z_slice_t s); +int8_t _zz_bytes_from_uint8(_zz_bytes_t *b, uint8_t val); +int8_t _zz_bytes_from_uint16(_zz_bytes_t *b, uint16_t val); +int8_t _zz_bytes_from_uint32(_zz_bytes_t *b, uint32_t val); +int8_t _zz_bytes_from_uint64(_zz_bytes_t *b, uint64_t val); +int8_t _zz_bytes_from_float(_zz_bytes_t *b, float val); +int8_t _zz_bytes_from_double(_zz_bytes_t *b, double val); +size_t _zz_bytes_to_buf(const _zz_bytes_t *bytes, uint8_t *dst, size_t len); +int8_t _zz_bytes_from_buf(_zz_bytes_t *b, uint8_t *src, size_t len); +int8_t _zz_bytes_serialize_from_pair(_zz_bytes_t *out, _zz_bytes_t *first, _zz_bytes_t *second); +int8_t _zz_bytes_deserialize_into_pair(const _zz_bytes_t *bs, _zz_bytes_t *first_out, _zz_bytes_t *second_out); +_z_slice_t _zz_bytes_try_get_contiguous(const _zz_bytes_t *bs); typedef struct { size_t slice_idx; size_t in_slice_idx; size_t byte_idx; - const _zz_bytes_t* bytes; + const _zz_bytes_t *bytes; } _zz_bytes_reader_t; - - _zz_bytes_reader_t _zz_bytes_get_reader(const _zz_bytes_t *bytes); int8_t _zz_bytes_reader_seek(_zz_bytes_reader_t *reader, int64_t offset, int origin); int64_t _zz_bytes_reader_tell(const _zz_bytes_reader_t *reader); -int8_t _zz_bytes_reader_read_slices(_zz_bytes_reader_t* reader, size_t len, _zz_bytes_t* out); -int8_t _zz_bytes_reader_read(_zz_bytes_reader_t *reader, uint8_t* buf, size_t len); -int8_t _zz_bytes_reader_read_next(_zz_bytes_reader_t* reader, _zz_bytes_t* out); +int8_t _zz_bytes_reader_read_slices(_zz_bytes_reader_t *reader, size_t len, _zz_bytes_t *out); +int8_t _zz_bytes_reader_read(_zz_bytes_reader_t *reader, uint8_t *buf, size_t len); +int8_t _zz_bytes_reader_read_next(_zz_bytes_reader_t *reader, _zz_bytes_t *out); + +typedef struct { + _zz_bytes_reader_t _reader; +} _zz_bytes_iterator_t; + +_zz_bytes_iterator_t _zz_bytes_get_iterator(const _zz_bytes_t *bytes); +_Bool _zz_bytes_iterator_next(_zz_bytes_iterator_t *iter, _zz_bytes_t *b); #endif /* ZENOH_PICO_COLLECTIONS_BYTES_H */ diff --git a/include/zenoh-pico/collections/slice.h b/include/zenoh-pico/collections/slice.h index 875771dbe..c9c25945a 100644 --- a/include/zenoh-pico/collections/slice.h +++ b/include/zenoh-pico/collections/slice.h @@ -41,7 +41,7 @@ _z_slice_t _z_slice_make(size_t capacity); _z_slice_t _z_slice_wrap(const uint8_t *bs, size_t len); _z_slice_t _z_slice_wrap_copy(const uint8_t *bs, size_t len); _z_slice_t _z_slice_steal(_z_slice_t *b); -void _z_slice_copy(_z_slice_t *dst, const _z_slice_t *src); +int8_t _z_slice_copy(_z_slice_t *dst, const _z_slice_t *src); _z_slice_t _z_slice_duplicate(const _z_slice_t *src); void _z_slice_move(_z_slice_t *dst, _z_slice_t *src); void _z_slice_reset(_z_slice_t *bs); @@ -50,37 +50,4 @@ _Bool _z_slice_eq(const _z_slice_t *left, const _z_slice_t *right); void _z_slice_clear(_z_slice_t *bs); void _z_slice_free(_z_slice_t **bs); -/*-------- Bytes --------*/ -/** - * A container for slices. - * - * Members: - * _z_slice_t slice: content of the container. - */ -typedef struct { - _z_slice_t _slice; -} _z_bytes_t; - -_Bool _z_bytes_check(_z_bytes_t bytes); -_z_bytes_t _z_bytes_null(void); -_z_bytes_t _z_bytes_make(size_t capacity); -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); -uint8_t _z_bytes_to_uint8(const _z_bytes_t *bs); -uint16_t _z_bytes_to_uint16(const _z_bytes_t *bs); -uint32_t _z_bytes_to_uint32(const _z_bytes_t *bs); -uint64_t _z_bytes_to_uint64(const _z_bytes_t *bs); -float _z_bytes_to_float(const _z_bytes_t *bs); -double _z_bytes_to_double(const _z_bytes_t *bs); -_z_slice_t _z_bytes_to_slice(const _z_bytes_t *bytes); -_z_bytes_t _z_bytes_from_uint8(uint8_t val); -_z_bytes_t _z_bytes_from_uint16(uint16_t val); -_z_bytes_t _z_bytes_from_uint32(uint32_t val); -_z_bytes_t _z_bytes_from_uint64(uint64_t val); -_z_bytes_t _z_bytes_from_float(float val); -_z_bytes_t _z_bytes_from_double(double val); - #endif /* ZENOH_PICO_COLLECTIONS_SLICE_H */ diff --git a/include/zenoh-pico/collections/vec.h b/include/zenoh-pico/collections/vec.h index 58874550b..07ecd9c68 100644 --- a/include/zenoh-pico/collections/vec.h +++ b/include/zenoh-pico/collections/vec.h @@ -64,7 +64,6 @@ void _z_vec_release(_z_vec_t *v); static inline void name##_vec_free(name##_vec_t **v) { _z_vec_free(v, name##_elem_free); } \ static inline void name##_vec_release(name##_vec_t *v) { _z_vec_release(v); } - /*-------- Dynamically allocated sized vector --------*/ /** * A dynamically allocate vector. @@ -91,31 +90,28 @@ void _z_svec_clear(_z_svec_t *v, z_element_clear_f f, size_t element_size); void _z_svec_free(_z_svec_t **v, z_element_clear_f f, size_t element_size); void _z_svec_release(_z_svec_t *v); -#define _Z_SVEC_DEFINE(name, type) \ - typedef _z_svec_t name##_svec_t; \ - static inline name##_svec_t name##_svec_make(size_t capacity) { \ - return _z_svec_make(capacity, sizeof(type)); \ - } \ - static inline size_t name##_svec_len(const name##_svec_t *v) { return _z_svec_len(v); } \ - static inline _Bool name##_svec_is_empty(const name##_svec_t *v) { return _z_svec_is_empty(v); } \ - static inline _Bool name##_svec_append(name##_svec_t *v, type *e) { \ - return _z_svec_append(v, e, name##_elem_move, sizeof(type)); \ - } \ - static inline type *name##_svec_get(const name##_svec_t *v, size_t pos) { \ - return (type *)_z_svec_get(v, pos, sizeof(type)); \ - } \ - static inline void name##_svec_set(name##_svec_t *v, size_t pos, type *e) { \ - _z_svec_set(v, pos, e, name##_elem_clear, sizeof(type)); \ - } \ - static inline void name##_svec_remove(name##_svec_t *v, size_t pos) { \ - _z_svec_remove(v, pos, name##_elem_clear, name##_elem_move, sizeof(type)); \ - } \ - static inline void name##_svec_copy(name##_svec_t *dst, const name##_svec_t *src) { \ - _z_svec_copy(dst, src, name##_elem_copy, sizeof(type)); \ - } \ - static inline void name##_svec_reset(name##_svec_t *v) { _z_svec_reset(v, name##_elem_clear, sizeof(type)); } \ - static inline void name##_svec_clear(name##_svec_t *v) { _z_svec_clear(v, name##_elem_clear, sizeof(type)); } \ - static inline void name##_svec_free(name##_svec_t **v) { _z_svec_free(v, name##_elem_clear, sizeof(type)); } \ - +#define _Z_SVEC_DEFINE(name, type) \ + typedef _z_svec_t name##_svec_t; \ + static inline name##_svec_t name##_svec_make(size_t capacity) { return _z_svec_make(capacity, sizeof(type)); } \ + static inline size_t name##_svec_len(const name##_svec_t *v) { return _z_svec_len(v); } \ + static inline _Bool name##_svec_is_empty(const name##_svec_t *v) { return _z_svec_is_empty(v); } \ + static inline _Bool name##_svec_append(name##_svec_t *v, type *e) { \ + return _z_svec_append(v, e, name##_elem_move, sizeof(type)); \ + } \ + static inline type *name##_svec_get(const name##_svec_t *v, size_t pos) { \ + return (type *)_z_svec_get(v, pos, sizeof(type)); \ + } \ + static inline void name##_svec_set(name##_svec_t *v, size_t pos, type *e) { \ + _z_svec_set(v, pos, e, name##_elem_clear, sizeof(type)); \ + } \ + static inline void name##_svec_remove(name##_svec_t *v, size_t pos) { \ + _z_svec_remove(v, pos, name##_elem_clear, name##_elem_move, sizeof(type)); \ + } \ + static inline void name##_svec_copy(name##_svec_t *dst, const name##_svec_t *src) { \ + _z_svec_copy(dst, src, name##_elem_copy, sizeof(type)); \ + } \ + static inline void name##_svec_reset(name##_svec_t *v) { _z_svec_reset(v, name##_elem_clear, sizeof(type)); } \ + static inline void name##_svec_clear(name##_svec_t *v) { _z_svec_clear(v, name##_elem_clear, sizeof(type)); } \ + static inline void name##_svec_free(name##_svec_t **v) { _z_svec_free(v, name##_elem_clear, sizeof(type)); } #endif /* ZENOH_PICO_COLLECTIONS_VECTOR_H */ diff --git a/include/zenoh-pico/net/primitives.h b/include/zenoh-pico/net/primitives.h index ed9ef6989..6aa07e93b 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -108,8 +108,7 @@ int8_t _z_undeclare_publisher(_z_publisher_t *pub); * Parameters: * zn: The zenoh-net session. The caller keeps its ownership. * keyexpr: The resource key to write. The caller keeps its ownership. - * payload: The value to write. - * len: The length of the value to write. + * payload: The data to write. * encoding: The encoding of the payload. The callee gets the ownership of * any allocated value. * kind: The kind of the value. @@ -119,9 +118,9 @@ int8_t _z_undeclare_publisher(_z_publisher_t *pub); * Returns: * ``0`` in case of success, ``-1`` in case of failure. */ -int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *payload, const size_t len, - const _z_encoding_t encoding, const z_sample_kind_t kind, const z_congestion_control_t cong_ctrl, - z_priority_t priority, const _z_bytes_t attachment); +int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, _zz_bytes_t payload, const _z_encoding_t encoding, + const z_sample_kind_t kind, const z_congestion_control_t cong_ctrl, z_priority_t priority, + const _zz_bytes_t attachment); #endif #if Z_FEATURE_SUBSCRIPTION == 1 @@ -200,7 +199,7 @@ int8_t _z_undeclare_queryable(_z_queryable_t *qle); * attachment: An optional attachment to the reply. */ int8_t _z_send_reply(const _z_query_t *query, const _z_keyexpr_t keyexpr, const _z_value_t payload, - const z_sample_kind_t kind, const _z_bytes_t attachment); + const z_sample_kind_t kind, const _zz_bytes_t attachment); #endif #if Z_FEATURE_QUERY == 1 @@ -225,7 +224,7 @@ int8_t _z_send_reply(const _z_query_t *query, const _z_keyexpr_t keyexpr, const */ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, const z_query_target_t target, const z_consolidation_mode_t consolidation, const _z_value_t value, _z_reply_handler_t callback, - _z_drop_handler_t dropper, void *arg, uint32_t timeout_ms, const _z_bytes_t attachment); + _z_drop_handler_t dropper, void *arg, uint32_t timeout_ms, const _zz_bytes_t attachment); #endif #if Z_FEATURE_INTEREST == 1 diff --git a/include/zenoh-pico/net/query.h b/include/zenoh-pico/net/query.h index e04cc6f88..90cd3b116 100644 --- a/include/zenoh-pico/net/query.h +++ b/include/zenoh-pico/net/query.h @@ -17,6 +17,7 @@ #include #include "zenoh-pico/api/constants.h" +#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/net/session.h" #include "zenoh-pico/protocol/core.h" @@ -28,7 +29,7 @@ typedef struct _z_query_t { _z_keyexpr_t _key; uint32_t _request_id; _z_session_t *_zn; - _z_bytes_t attachment; + _zz_bytes_t attachment; char *_parameters; _Bool _anyke; } _z_query_t; @@ -47,7 +48,7 @@ typedef struct { #if Z_FEATURE_QUERYABLE == 1 _z_query_t _z_query_create(const _z_value_t *value, const _z_keyexpr_t *key, const _z_slice_t *parameters, - _z_session_t *zn, uint32_t request_id, const _z_bytes_t attachment); + _z_session_t *zn, uint32_t request_id, const _zz_bytes_t attachment); void _z_queryable_clear(_z_queryable_t *qbl); void _z_queryable_free(_z_queryable_t **qbl); #endif diff --git a/include/zenoh-pico/net/reply.h b/include/zenoh-pico/net/reply.h index f8ad26449..438a7f19d 100644 --- a/include/zenoh-pico/net/reply.h +++ b/include/zenoh-pico/net/reply.h @@ -61,9 +61,9 @@ _z_reply_t _z_reply_null(void); void _z_reply_clear(_z_reply_t *src); void _z_reply_free(_z_reply_t **hello); void _z_reply_copy(_z_reply_t *dst, _z_reply_t *src); -_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_slice_t *payload, +_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _zz_bytes_t payload, const _z_timestamp_t *timestamp, _z_encoding_t encoding, z_sample_kind_t kind, - const _z_bytes_t attachment); + const _zz_bytes_t attachment); _Z_REFCOUNT_DEFINE(_z_reply, _z_reply) diff --git a/include/zenoh-pico/net/sample.h b/include/zenoh-pico/net/sample.h index 13b1023dd..a410b71f5 100644 --- a/include/zenoh-pico/net/sample.h +++ b/include/zenoh-pico/net/sample.h @@ -30,12 +30,12 @@ */ typedef struct _z_sample_t { _z_keyexpr_t keyexpr; - _z_bytes_t payload; + _zz_bytes_t payload; _z_timestamp_t timestamp; _z_encoding_t encoding; z_sample_kind_t kind; _z_qos_t qos; - _z_bytes_t attachment; + _zz_bytes_t attachment; } _z_sample_t; void _z_sample_clear(_z_sample_t *sample); @@ -56,8 +56,8 @@ void _z_sample_free(_z_sample_t **sample); void _z_sample_copy(_z_sample_t *dst, const _z_sample_t *src); _z_sample_t _z_sample_duplicate(const _z_sample_t *src); -_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_slice_t *payload, _z_timestamp_t timestamp, +_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _zz_bytes_t, _z_timestamp_t timestamp, const _z_encoding_t encoding, const z_sample_kind_t kind, const _z_qos_t qos, - const _z_bytes_t attachment); + const _zz_bytes_t attachment); #endif /* ZENOH_PICO_SAMPLE_NETAPI_H */ diff --git a/include/zenoh-pico/protocol/codec/core.h b/include/zenoh-pico/protocol/codec/core.h index 0431699de..a32270244 100644 --- a/include/zenoh-pico/protocol/codec/core.h +++ b/include/zenoh-pico/protocol/codec/core.h @@ -41,7 +41,7 @@ } \ } -typedef int8_t (*__z_single_byte_reader_t)(uint8_t*, void* context); +typedef int8_t (*__z_single_byte_reader_t)(uint8_t *, void *context); /*------------------ Internal Zenoh-net Macros ------------------*/ int8_t _z_consolidation_mode_encode(_z_wbuf_t *wbf, z_consolidation_mode_t en); int8_t _z_consolidation_mode_decode(z_consolidation_mode_t *en, _z_zbuf_t *zbf); @@ -57,10 +57,8 @@ int8_t _z_uint16_encode(_z_wbuf_t *buf, uint16_t v); int8_t _z_uint16_decode(uint16_t *u16, _z_zbuf_t *buf); uint8_t _z_zint_len(uint64_t v); -uint8_t _z_zint64_encode_buf(uint8_t* buf, uint64_t v); -static inline uint8_t _z_zsize_encode_buf(uint8_t* buf, _z_zint_t v) { - return _z_zint64_encode_buf(buf, (uint64_t)v); -} +uint8_t _z_zint64_encode_buf(uint8_t *buf, uint64_t v); +static inline uint8_t _z_zsize_encode_buf(uint8_t *buf, _z_zint_t v) { return _z_zint64_encode_buf(buf, (uint64_t)v); } int8_t _z_zsize_encode(_z_wbuf_t *buf, _z_zint_t v); int8_t _z_zint64_encode(_z_wbuf_t *buf, uint64_t v); @@ -68,7 +66,7 @@ int8_t _z_zint16_decode(uint16_t *zint, _z_zbuf_t *buf); int8_t _z_zint32_decode(uint32_t *zint, _z_zbuf_t *buf); int8_t _z_zint64_decode(uint64_t *zint, _z_zbuf_t *buf); int8_t _z_zint64_decode_with_reader(uint64_t *zint, __z_single_byte_reader_t reader, void *context); -int8_t _z_zsize_decode_with_reader(_z_zint_t *zint, __z_single_byte_reader_t reader, void* context); +int8_t _z_zsize_decode_with_reader(_z_zint_t *zint, __z_single_byte_reader_t reader, void *context); int8_t _z_zsize_decode(_z_zint_t *zint, _z_zbuf_t *buf); int8_t _z_slice_val_encode(_z_wbuf_t *buf, const _z_slice_t *bs); @@ -78,8 +76,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 _zz_bytes_decode(_zz_bytes_t *bs, _z_zbuf_t *zbf); +int8_t _zz_bytes_encode(_z_wbuf_t *wbf, const _zz_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); @@ -90,6 +88,9 @@ size_t _z_encoding_len(const _z_encoding_t *en); int8_t _z_encoding_encode(_z_wbuf_t *wbf, const _z_encoding_t *en); int8_t _z_encoding_decode(_z_encoding_t *en, _z_zbuf_t *zbf); +int8_t _z_value_encode(_z_wbuf_t *wbf, const _z_value_t *en); +int8_t _z_value_decode(_z_value_t *en, _z_zbuf_t *zbf); + int8_t _z_keyexpr_encode(_z_wbuf_t *buf, _Bool has_suffix, const _z_keyexpr_t *ke); int8_t _z_keyexpr_decode(_z_keyexpr_t *ke, _z_zbuf_t *buf, _Bool has_suffix); diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 99377f0e0..de841bd58 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -20,6 +20,7 @@ #include #include "zenoh-pico/api/constants.h" +#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/element.h" #include "zenoh-pico/collections/refcount.h" #include "zenoh-pico/collections/slice.h" @@ -157,11 +158,11 @@ typedef struct { * Represents a Zenoh value. * * Members: - * _z_bytes_t payload: The payload of this zenoh value. + * _zz_bytes_t payload: The payload of this zenoh value. * _z_encoding_t encoding: The encoding of the `payload`. */ typedef struct { - _z_bytes_t payload; + _zz_bytes_t payload; _z_encoding_t encoding; } _z_value_t; _z_value_t _z_value_null(void); diff --git a/include/zenoh-pico/protocol/definitions/message.h b/include/zenoh-pico/protocol/definitions/message.h index 36dd0f2bf..d7a148da5 100644 --- a/include/zenoh-pico/protocol/definitions/message.h +++ b/include/zenoh-pico/protocol/definitions/message.h @@ -55,7 +55,7 @@ typedef struct { _z_encoding_t encoding; _z_source_info_t _ext_source_info; - _z_slice_t _payload; + _zz_bytes_t _payload; } _z_msg_err_t; void _z_msg_err_clear(_z_msg_err_t *err); @@ -73,9 +73,9 @@ static inline void _z_msg_del_clear(_z_msg_del_t *del) { (void)del; } typedef struct { _z_m_push_commons_t _commons; - _z_slice_t _payload; + _zz_bytes_t _payload; _z_encoding_t _encoding; - _z_bytes_t _attachment; + _zz_bytes_t _attachment; } _z_msg_put_t; void _z_msg_put_clear(_z_msg_put_t *); #define _Z_M_PUT_ID 0x01 @@ -100,7 +100,7 @@ typedef struct { _z_source_info_t _ext_info; _z_value_t _ext_value; z_consolidation_mode_t _consolidation; - _z_bytes_t _ext_attachment; + _zz_bytes_t _ext_attachment; } _z_msg_query_t; typedef struct { _Bool info; diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index 1940a4660..6fec4e7d3 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -293,7 +293,7 @@ _Z_VEC_DEFINE(_z_network_message, _z_network_message_t) void _z_msg_fix_mapping(_z_zenoh_message_t *msg, uint16_t mapping); _z_network_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_slice_t) parameters, _z_zint_t qid, z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value, - uint32_t timeout_ms, _z_bytes_t attachment); + uint32_t timeout_ms, _zz_bytes_t attachment); _z_network_message_t _z_n_msg_make_reply(_z_zint_t rid, _Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_push_body_t) body); _z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid); _z_network_message_t _z_n_msg_make_declare(_z_declaration_t declaration, _Bool has_interest_id, uint32_t interest_id); diff --git a/include/zenoh-pico/session/queryable.h b/include/zenoh-pico/session/queryable.h index 78c5f3cad..e7692b647 100644 --- a/include/zenoh-pico/session/queryable.h +++ b/include/zenoh-pico/session/queryable.h @@ -29,7 +29,7 @@ _z_session_queryable_rc_list_t *_z_get_session_queryable_by_key(_z_session_t *zn _z_session_queryable_rc_t *_z_register_session_queryable(_z_session_t *zn, _z_session_queryable_t *q); int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, const _z_keyexpr_t q_key, uint32_t qid, - const _z_bytes_t attachment); + const _zz_bytes_t attachment); void _z_unregister_session_queryable(_z_session_t *zn, _z_session_queryable_rc_t *q); void _z_flush_session_queryable(_z_session_t *zn); #endif diff --git a/include/zenoh-pico/session/subscription.h b/include/zenoh-pico/session/subscription.h index 0635a9572..2529110cc 100644 --- a/include/zenoh-pico/session/subscription.h +++ b/include/zenoh-pico/session/subscription.h @@ -19,17 +19,17 @@ #include "zenoh-pico/net/session.h" /*------------------ Subscription ------------------*/ -void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *payload, - _z_zint_t payload_len, const _z_n_qos_t qos, const _z_bytes_t att); +void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _zz_bytes_t payload, + const _z_n_qos_t qos, const _zz_bytes_t attachment); #if Z_FEATURE_SUBSCRIPTION == 1 _z_subscription_rc_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id); _z_subscription_rc_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr); _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *sub); -int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_slice_t payload, +int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _zz_bytes_t payload, const _z_encoding_t encoding, const _z_zint_t kind, const _z_timestamp_t timestamp, - const _z_n_qos_t qos, const _z_bytes_t attachment); + const _z_n_qos_t qos, const _zz_bytes_t attachment); void _z_unregister_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_rc_t *sub); void _z_flush_subscriptions(_z_session_t *zn); #endif diff --git a/src/api/api.c b/src/api/api.c index acd2da6ba..c285627f3 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -263,53 +263,43 @@ 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_bytes_deserialize_into_int8(const z_loaned_bytes_t *bytes, int8_t *dst) { - *dst = (int8_t)_z_bytes_to_uint8(bytes); - return _Z_RES_OK; + return _zz_bytes_to_uint8(bytes, (uint8_t *)dst); } int8_t z_bytes_deserialize_into_int16(const z_loaned_bytes_t *bytes, int16_t *dst) { - *dst = (int16_t)_z_bytes_to_uint16(bytes); - return _Z_RES_OK; + return _zz_bytes_to_uint16(bytes, (uint16_t *)dst); } int8_t z_bytes_deserialize_into_int32(const z_loaned_bytes_t *bytes, int32_t *dst) { - *dst = (int32_t)_z_bytes_to_uint32(bytes); - return _Z_RES_OK; + return _zz_bytes_to_uint32(bytes, (uint32_t *)dst); } int8_t z_bytes_deserialize_into_int64(const z_loaned_bytes_t *bytes, int64_t *dst) { - *dst = (int64_t)_z_bytes_to_uint64(bytes); - return _Z_RES_OK; + return _zz_bytes_to_uint64(bytes, (uint64_t *)dst); } int8_t z_bytes_deserialize_into_uint8(const z_loaned_bytes_t *bytes, uint8_t *dst) { - *dst = _z_bytes_to_uint8(bytes); - return _Z_RES_OK; + return _zz_bytes_to_uint8(bytes, dst); } int8_t z_bytes_deserialize_into_uint16(const z_loaned_bytes_t *bytes, uint16_t *dst) { - *dst = _z_bytes_to_uint16(bytes); - return _Z_RES_OK; + return _zz_bytes_to_uint16(bytes, dst); } int8_t z_bytes_deserialize_into_uint32(const z_loaned_bytes_t *bytes, uint32_t *dst) { - *dst = _z_bytes_to_uint32(bytes); - return _Z_RES_OK; + return _zz_bytes_to_uint32(bytes, dst); } int8_t z_bytes_deserialize_into_uint64(const z_loaned_bytes_t *bytes, uint64_t *dst) { - *dst = _z_bytes_to_uint64(bytes); - return _Z_RES_OK; + return _zz_bytes_to_uint64(bytes, dst); } int8_t z_bytes_deserialize_into_float(const z_loaned_bytes_t *bytes, float *dst) { - *dst = _z_bytes_to_float(bytes); - return _Z_RES_OK; + return _zz_bytes_to_float(bytes, dst); } int8_t z_bytes_deserialize_into_double(const z_loaned_bytes_t *bytes, double *dst) { - *dst = _z_bytes_to_double(bytes); - return _Z_RES_OK; + return _zz_bytes_to_double(bytes, dst); } int8_t z_bytes_deserialize_into_slice(const z_loaned_bytes_t *bytes, z_owned_slice_t *dst) { @@ -320,8 +310,7 @@ int8_t z_bytes_deserialize_into_slice(const z_loaned_bytes_t *bytes, z_owned_sli return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Convert bytes to slice - *dst->_val = _z_bytes_to_slice(bytes); - return _Z_RES_OK; + return _zz_bytes_to_slice(bytes, dst->_val); } int8_t z_bytes_deserialize_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *s) { @@ -332,51 +321,23 @@ int8_t z_bytes_deserialize_into_string(const z_loaned_bytes_t *bytes, z_owned_st return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Convert bytes to string - *s->_val = _z_string_from_bytes(&bytes->_slice); + size_t len = _zz_bytes_len(bytes); + *s->_val = _z_string_preallocate(_zz_bytes_len(bytes)); + if (s->_val->len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + _zz_bytes_to_buf(bytes, (uint8_t *)s->_val->val, len); return _Z_RES_OK; } -int8_t zp_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second, - size_t *curr_idx) { - // Check bound size - if (*curr_idx >= bytes->_slice.len) { - return _Z_ERR_GENERIC; - } +int8_t z_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second) { // Init pair of owned bytes z_bytes_null(first); z_bytes_null(second); - first->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - second->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + first->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); + second->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); if ((first->_val == NULL) || (second->_val == NULL)) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - // Extract first item size - size_t first_len = 0; - // FIXME: size endianness, Issue #420 - memcpy(&first_len, &bytes->_slice.start[*curr_idx], sizeof(uint32_t)); - *curr_idx += sizeof(uint32_t); - // Allocate first item bytes - *first->_val = _z_bytes_make(first_len); - if (!_z_bytes_check(*first->_val)) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - // Extract first item data - memcpy((uint8_t *)first->_val->_slice.start, &bytes->_slice.start[*curr_idx], first_len); - *curr_idx += first_len; - - // Extract second item size - size_t second_len = 0; - memcpy(&second_len, &bytes->_slice.start[*curr_idx], sizeof(uint32_t)); - *curr_idx += sizeof(uint32_t); - // Allocate second item bytes - *second->_val = _z_bytes_make(second_len); - if (!_z_bytes_check(*second->_val)) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - // Extract second item data - memcpy((uint8_t *)second->_val->_slice.start, &bytes->_slice.start[*curr_idx], second_len); - *curr_idx += second_len; - return _Z_RES_OK; + return _zz_bytes_deserialize_into_pair(bytes, first->_val, second->_val); } int8_t z_bytes_serialize_from_int8(z_owned_bytes_t *bytes, int8_t val) { @@ -398,192 +359,171 @@ int8_t z_bytes_serialize_from_int64(z_owned_bytes_t *bytes, int64_t val) { int8_t z_bytes_serialize_from_uint8(z_owned_bytes_t *bytes, uint8_t val) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - *bytes->_val = _z_bytes_from_uint8(val); - if (!_z_bytes_check(*bytes->_val)) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - return _Z_RES_OK; + int8_t res = _zz_bytes_from_uint8(bytes->_val, val); + if (res != _Z_RES_OK) z_bytes_drop(bytes); + return res; } int8_t z_bytes_serialize_from_uint16(z_owned_bytes_t *bytes, uint16_t val) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - *bytes->_val = _z_bytes_from_uint16(val); - if (!_z_bytes_check(*bytes->_val)) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - return _Z_RES_OK; + int8_t res = _zz_bytes_from_uint16(bytes->_val, val); + if (res != _Z_RES_OK) z_bytes_drop(bytes); + return res; } int8_t z_bytes_serialize_from_uint32(z_owned_bytes_t *bytes, uint32_t val) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - *bytes->_val = _z_bytes_from_uint32(val); - if (!_z_bytes_check(*bytes->_val)) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - return _Z_RES_OK; + int8_t res = _zz_bytes_from_uint32(bytes->_val, val); + if (res != _Z_RES_OK) z_bytes_drop(bytes); + return res; } int8_t z_bytes_serialize_from_uint64(z_owned_bytes_t *bytes, uint64_t val) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - *bytes->_val = _z_bytes_from_uint64(val); - if (!_z_bytes_check(*bytes->_val)) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - return _Z_RES_OK; + int8_t res = _zz_bytes_from_uint64(bytes->_val, val); + if (res != _Z_RES_OK) z_bytes_drop(bytes); + return res; } int8_t z_bytes_serialize_from_float(z_owned_bytes_t *bytes, float val) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - *bytes->_val = _z_bytes_from_float(val); - if (!_z_bytes_check(*bytes->_val)) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - return _Z_RES_OK; + int8_t res = _zz_bytes_from_float(bytes->_val, val); + if (res != _Z_RES_OK) z_bytes_drop(bytes); + return res; } int8_t z_bytes_serialize_from_double(z_owned_bytes_t *bytes, double val) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - *bytes->_val = _z_bytes_from_double(val); - if (!_z_bytes_check(*bytes->_val)) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - return _Z_RES_OK; + int8_t res = _zz_bytes_from_double(bytes->_val, val); + if (res != _Z_RES_OK) z_bytes_drop(bytes); + return res; } int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - bytes->_val->_slice = _z_slice_wrap((uint8_t *)data, len); - return _Z_RES_OK; + _z_slice_t s = _z_slice_wrap((uint8_t *)data, len); + int8_t res = _zz_bytes_from_slice(bytes->_val, s); + if (res != _Z_RES_OK) z_bytes_drop(bytes); + return res; } int8_t z_bytes_serialize_from_slice_copy(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Allocate bytes - *bytes->_val = _z_bytes_make(len); - if (!_z_bytes_check(*bytes->_val)) { + _z_slice_t s = _z_slice_make(len); + if (!_z_slice_check(s) && len > 0) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Copy data - memcpy((uint8_t *)bytes->_val->_slice.start, data, len); - return _Z_RES_OK; + memcpy((uint8_t *)s.start, data, len); + int8_t res = _zz_bytes_from_slice(bytes->_val, s); + if (res != _Z_RES_OK) z_bytes_drop(bytes); + return res; } int8_t z_bytes_serialize_from_string(z_owned_bytes_t *bytes, const char *s) { - // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } // Encode string without null terminator size_t len = strlen(s); - bytes->_val->_slice = _z_slice_wrap((uint8_t *)s, len); - return _Z_RES_OK; + return z_bytes_serialize_from_slice(bytes, (uint8_t *)s, len); } int8_t z_bytes_serialize_from_string_copy(z_owned_bytes_t *bytes, const char *s) { + // Encode string without null terminator + size_t len = strlen(s); + return z_bytes_serialize_from_slice_copy(bytes, (uint8_t *)s, len); +} + +int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body)(z_owned_bytes_t *data, void *context), + void *context) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - // Allocate bytes - size_t len = strlen(s); - *bytes->_val = _z_bytes_make(len); - if (!_z_bytes_check(*bytes->_val)) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + z_owned_bytes_t data; + while (iterator_body(&data, context)) { + int8_t res = _zz_bytes_append(bytes->_val, data._val); + if (res != _Z_RES_OK) { + z_bytes_drop(bytes); + return res; + } } - // Copy string without null terminator - memcpy((uint8_t *)bytes->_val->_slice.start, s, len); return _Z_RES_OK; } -int8_t zp_bytes_serialize_from_iter(z_owned_bytes_t *bytes, - _Bool (*iterator_body)(z_owned_bytes_t *data, void *context, size_t *curr_idx), - void *context, size_t total_len) { - // Init owned bytes +int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second) { z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); if (bytes->_val == NULL) { + z_bytes_drop(first); + z_bytes_drop(second); return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - // Allocate bytes - *bytes->_val = _z_bytes_make(total_len); - if (!_z_bytes_check(*bytes->_val)) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - size_t curr_idx = 0; - while (iterator_body(bytes, context, &curr_idx)) - ; - return _Z_RES_OK; + int8_t res = _zz_bytes_serialize_from_pair(bytes->_val, first->_val, second->_val); + first->_val = NULL; + second->_val = NULL; + if (res != _Z_RES_OK) z_bytes_drop(bytes); + return res; } -int8_t zp_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second, - size_t *curr_idx) { - // Calculate pair size - size_t first_len = z_slice_len(&first->_val->_slice); - size_t second_len = z_slice_len(&second->_val->_slice); - // Copy data - // FIXME: size endianness, Issue #420 - memcpy((uint8_t *)&bytes->_val->_slice.start[*curr_idx], &first_len, sizeof(uint32_t)); - *curr_idx += sizeof(uint32_t); - memcpy((uint8_t *)&bytes->_val->_slice.start[*curr_idx], z_slice_data(&first->_val->_slice), first_len); - *curr_idx += first_len; - memcpy((uint8_t *)&bytes->_val->_slice.start[*curr_idx], &second_len, sizeof(uint32_t)); - *curr_idx += sizeof(uint32_t); - memcpy((uint8_t *)&bytes->_val->_slice.start[*curr_idx], z_slice_data(&second->_val->_slice), second_len); - *curr_idx += second_len; - // Clean up - z_bytes_drop(first); - z_bytes_drop(second); - return _Z_RES_OK; +z_bytes_iterator_t z_bytes_get_iterator(const z_loaned_bytes_t *bytes) { return _zz_bytes_get_iterator(bytes); } + +_Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *bytes) { + z_bytes_null(bytes); + bytes->_val = (z_loaned_bytes_t *)z_malloc(sizeof(z_loaned_bytes_t)); + if (bytes->_val == NULL) return false; + if (!_zz_bytes_iterator_next(iter, bytes->_val)) { + z_bytes_drop(bytes); + return false; + } + return true; } _Bool z_timestamp_check(z_timestamp_t ts) { return _z_timestamp_check(&ts); } @@ -686,16 +626,16 @@ _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_hello_t, hello, _z_owner_noop_copy, _z_hello_free _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_string_vec_t, string_array, _z_owner_noop_copy, _z_string_vec_free) _Z_VIEW_FUNCTIONS_PTR_IMPL(_z_string_vec_t, string_array) _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_slice_t, slice, _z_slice_copy, _z_slice_free) -_Z_OWNED_FUNCTIONS_PTR_IMPL(_z_bytes_t, bytes, _z_bytes_copy, _z_bytes_free) +_Z_OWNED_FUNCTIONS_PTR_IMPL(_zz_bytes_t, bytes, _zz_bytes_copy, _zz_bytes_free) #if Z_FEATURE_PUBLICATION == 1 || Z_FEATURE_QUERYABLE == 1 || Z_FEATURE_QUERY == 1 // Convert a user owned bytes payload to an internal bytes payload, returning an empty one if value invalid -static _z_bytes_t _z_bytes_from_owned_bytes(z_owned_bytes_t *bytes) { - _z_bytes_t b = _z_bytes_null(); +static _zz_bytes_t _z_bytes_from_owned_bytes(z_owned_bytes_t *bytes) { if ((bytes != NULL) && (bytes->_val != NULL)) { - b._slice = _z_slice_wrap(bytes->_val->_slice.start, bytes->_val->_slice.len); + return *bytes->_val; + } else { + return _zz_bytes_null(); } - return b; } // Convert a user owned encoding to an internal encoding, return default encoding if value invalid @@ -909,12 +849,12 @@ int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_ opt.attachment = options->attachment; } - ret = _z_write(&_Z_RC_IN_VAL(zs), *keyexpr, payload->_val->_slice.start, payload->_val->_slice.len, - _z_encoding_from_owned(opt.encoding), Z_SAMPLE_KIND_PUT, opt.congestion_control, opt.priority, - _z_bytes_from_owned_bytes(opt.attachment)); + ret = + _z_write(&_Z_RC_IN_VAL(zs), *keyexpr, _z_bytes_from_owned_bytes(payload), _z_encoding_from_owned(opt.encoding), + Z_SAMPLE_KIND_PUT, opt.congestion_control, opt.priority, _z_bytes_from_owned_bytes(opt.attachment)); // Trigger local subscriptions - _z_trigger_local_subscriptions(&_Z_RC_IN_VAL(zs), *keyexpr, payload->_val->_slice.start, payload->_val->_slice.len, + _z_trigger_local_subscriptions(&_Z_RC_IN_VAL(zs), *keyexpr, _z_bytes_from_owned_bytes(payload), _z_n_qos_make(0, opt.congestion_control == Z_CONGESTION_CONTROL_BLOCK, opt.priority), _z_bytes_from_owned_bytes(opt.attachment)); // Clean-up @@ -933,8 +873,8 @@ int8_t z_delete(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, opt.congestion_control = options->congestion_control; opt.priority = options->priority; } - ret = _z_write(&_Z_RC_IN_VAL(zs), *keyexpr, NULL, 0, _z_encoding_null(), Z_SAMPLE_KIND_DELETE, - opt.congestion_control, opt.priority, _z_bytes_null()); + ret = _z_write(&_Z_RC_IN_VAL(zs), *keyexpr, _zz_bytes_null(), _z_encoding_null(), Z_SAMPLE_KIND_DELETE, + opt.congestion_control, opt.priority, _zz_bytes_null()); return ret; } @@ -1008,13 +948,13 @@ int8_t z_publisher_put(const z_loaned_publisher_t *pub, z_owned_bytes_t *payload // Check if write filter is active before writing if (!_z_write_filter_active(pub)) { // Write value - ret = _z_write(&pub->_zn.in->val, pub->_key, payload->_val->_slice.start, payload->_val->_slice.len, + ret = _z_write(&pub->_zn.in->val, pub->_key, _z_bytes_from_owned_bytes(payload), _z_encoding_from_owned(opt.encoding), Z_SAMPLE_KIND_PUT, pub->_congestion_control, pub->_priority, _z_bytes_from_owned_bytes(opt.attachment)); } // Trigger local subscriptions - _z_trigger_local_subscriptions(&pub->_zn.in->val, pub->_key, payload->_val->_slice.start, payload->_val->_slice.len, - _Z_N_QOS_DEFAULT, _z_bytes_from_owned_bytes(opt.attachment)); + _z_trigger_local_subscriptions(&pub->_zn.in->val, pub->_key, _z_bytes_from_owned_bytes(payload), _Z_N_QOS_DEFAULT, + _z_bytes_from_owned_bytes(opt.attachment)); // Clean-up z_encoding_drop(opt.encoding); z_bytes_drop(opt.attachment); @@ -1024,8 +964,8 @@ int8_t z_publisher_put(const z_loaned_publisher_t *pub, z_owned_bytes_t *payload int8_t z_publisher_delete(const z_loaned_publisher_t *pub, const z_publisher_delete_options_t *options) { (void)(options); - return _z_write(&pub->_zn.in->val, pub->_key, NULL, 0, _z_encoding_null(), Z_SAMPLE_KIND_DELETE, - pub->_congestion_control, pub->_priority, _z_bytes_null()); + return _z_write(&pub->_zn.in->val, pub->_key, _zz_bytes_null(), _z_encoding_null(), Z_SAMPLE_KIND_DELETE, + pub->_congestion_control, pub->_priority, _zz_bytes_null()); } z_owned_keyexpr_t z_publisher_keyexpr(z_loaned_publisher_t *publisher) { diff --git a/src/collections/arc_slice.c b/src/collections/arc_slice.c index eee2a9828..b22804f8b 100644 --- a/src/collections/arc_slice.c +++ b/src/collections/arc_slice.c @@ -14,12 +14,10 @@ #include "zenoh-pico/collections/arc_slice.h" +#include #include -#include #include -#include - - +#include _z_arc_slice_t _z_arc_slice_empty(void) { _z_arc_slice_t s; @@ -54,26 +52,20 @@ _z_arc_slice_t _z_arc_slice_get_subslice(const _z_arc_slice_t* s, size_t offset, return out; } -size_t _z_arc_slice_len(const _z_arc_slice_t* s) { - return s->len; -} +size_t _z_arc_slice_len(const _z_arc_slice_t* s) { return s->len; } -_Bool _z_arc_slice_is_empty(const _z_arc_slice_t* s) { - return _z_arc_slice_len(s) == 0; -} +_Bool _z_arc_slice_is_empty(const _z_arc_slice_t* s) { return _z_arc_slice_len(s) == 0; } -const uint8_t* _z_arc_slice_data(const _z_arc_slice_t* s) { - return s->slice.in->val.start + s->start; -} +const uint8_t* _z_arc_slice_data(const _z_arc_slice_t* s) { return s->slice.in->val.start + s->start; } -int8_t _z_arc_slice_copy(_z_arc_slice_t *dst, const _z_arc_slice_t *src) { +int8_t _z_arc_slice_copy(_z_arc_slice_t* dst, const _z_arc_slice_t* src) { _z_slice_rc_copy(&dst->slice, &src->slice); dst->len = src->len; dst->start = src->start; return _Z_RES_OK; } -int8_t _z_arc_slice_move(_z_arc_slice_t *dst, _z_arc_slice_t *src) { +int8_t _z_arc_slice_move(_z_arc_slice_t* dst, _z_arc_slice_t* src) { dst->slice = src->slice; dst->len = src->len; dst->start = src->start; @@ -83,7 +75,7 @@ int8_t _z_arc_slice_move(_z_arc_slice_t *dst, _z_arc_slice_t *src) { return _Z_RES_OK; } -int8_t _z_arc_slice_drop(_z_arc_slice_t *s) { +int8_t _z_arc_slice_drop(_z_arc_slice_t* s) { _z_slice_rc_drop(&s->slice); s->len = 0; s->start = 0; diff --git a/src/collections/bytes.c b/src/collections/bytes.c index fc466be50..5bf39691a 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -15,17 +15,15 @@ #include "zenoh-pico/collections/bytes.h" #include +#include #include -#include +#include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/system/platform.h" #include "zenoh-pico/utils/result.h" -#include "zenoh-pico/protocol/codec/core.h" /*-------- Bytes --------*/ -_Bool _zz_bytes_check(const _zz_bytes_t *bytes) { - return !_zz_bytes_is_empty(bytes); -} +_Bool _zz_bytes_check(const _zz_bytes_t *bytes) { return !_zz_bytes_is_empty(bytes); } _zz_bytes_t _zz_bytes_null(void) { _zz_bytes_t b; @@ -60,16 +58,14 @@ size_t _zz_bytes_len(const _zz_bytes_t *bs) { _Bool _zz_bytes_is_empty(const _zz_bytes_t *bs) { for (size_t i = 0; i < _z_arc_slice_svec_len(&bs->_slices); i++) { const _z_arc_slice_t *s = _z_arc_slice_svec_get(&bs->_slices, i); - if ( _z_arc_slice_len(s) > 0) return false; + if (_z_arc_slice_len(s) > 0) return false; } return true; } -size_t _zz_bytes_num_slices(const _zz_bytes_t *bs) { - return _z_arc_slice_svec_len(&bs->_slices); -} +size_t _zz_bytes_num_slices(const _zz_bytes_t *bs) { return _z_arc_slice_svec_len(&bs->_slices); } -_z_arc_slice_t* _zz_bytes_get_slice(const _zz_bytes_t *bs, size_t i) { +_z_arc_slice_t *_zz_bytes_get_slice(const _zz_bytes_t *bs, size_t i) { if (i >= _zz_bytes_num_slices(bs)) return NULL; return _z_arc_slice_svec_get(&bs->_slices, i); } @@ -87,8 +83,8 @@ void _zz_bytes_free(_zz_bytes_t **bs) { } } -size_t _zz_bytes_to_buf(const _zz_bytes_t *bytes, uint8_t* dst, size_t len) { - uint8_t* start = dst; +size_t _zz_bytes_to_buf(const _zz_bytes_t *bytes, uint8_t *dst, size_t len) { + uint8_t *start = dst; for (size_t i = 0; i < _zz_bytes_num_slices(bytes) && len > 0; ++i) { // Recopy data @@ -102,41 +98,44 @@ size_t _zz_bytes_to_buf(const _zz_bytes_t *bytes, uint8_t* dst, size_t len) { return len; } -_zz_bytes_t _zz_bytes_from_slice(_z_slice_t s) { - _zz_bytes_t b = _zz_bytes_null(); +int8_t _zz_bytes_from_slice(_zz_bytes_t *b, _z_slice_t s) { + *b = _zz_bytes_null(); _z_arc_slice_t arc_s = _z_arc_slice_wrap(s, 0, s.len); - _z_arc_slice_svec_append(&b._slices, &arc_s); - return b; + if (_z_arc_slice_len(&arc_s) != s.len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + return _z_arc_slice_svec_append(&b->_slices, &arc_s) ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY; } -_zz_bytes_t _zz_bytes_from_buf(uint8_t* src, size_t len) { - _z_slice_t s = _z_slice_wrap(src, len); - return _zz_bytes_from_slice(s); +int8_t _zz_bytes_from_buf(_zz_bytes_t *b, uint8_t *src, size_t len) { + *b = _zz_bytes_null(); + _z_slice_t s = _z_slice_wrap_copy(src, len); + if (s.len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + return _zz_bytes_from_slice(b, s); } -_z_slice_t _zz_bytes_to_slice(const _zz_bytes_t *bytes) { +int8_t _zz_bytes_to_slice(const _zz_bytes_t *bytes, _z_slice_t *s) { // Allocate slice - _z_slice_t ret = _z_slice_make(_zz_bytes_len(bytes)); - if (!_z_slice_check(ret)) { - return ret; + size_t len = _zz_bytes_len(bytes); + *s = _z_slice_make(len); + if (!_z_slice_check(*s) && len > 0) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - uint8_t* start = (uint8_t*)ret.start; + uint8_t *start = (uint8_t *)s->start; for (size_t i = 0; i < _zz_bytes_num_slices(bytes); ++i) { // Recopy data - _z_arc_slice_t* s = _zz_bytes_get_slice(bytes, i); - size_t s_len = _z_arc_slice_len(s); - memcpy(start, _z_arc_slice_data(s), s_len); + _z_arc_slice_t *arc_s = _zz_bytes_get_slice(bytes, i); + size_t s_len = _z_arc_slice_len(arc_s); + memcpy(start, _z_arc_slice_data(arc_s), s_len); start += s_len; } - return ret; + return _Z_RES_OK; } -int8_t _zz_bytes_append_slice(_zz_bytes_t* dst, _z_arc_slice_t* s) { +int8_t _zz_bytes_append_slice(_zz_bytes_t *dst, _z_arc_slice_t *s) { return _z_arc_slice_svec_append(&dst->_slices, s); } -int8_t _zz_bytes_append_inner(_zz_bytes_t* dst, _zz_bytes_t* src) { +int8_t _zz_bytes_append_inner(_zz_bytes_t *dst, _zz_bytes_t *src) { _Bool success = true; for (size_t i = 0; i < _zz_bytes_num_slices(src); ++i) { _z_arc_slice_t *s = _zz_bytes_get_slice(src, i); @@ -150,8 +149,8 @@ int8_t _zz_bytes_append_inner(_zz_bytes_t* dst, _zz_bytes_t* src) { return _Z_RES_OK; } -int8_t _zz_bytes_append(_zz_bytes_t* dst, _zz_bytes_t* src) { - uint8_t l_buf[16]; +int8_t _zz_bytes_append(_zz_bytes_t *dst, _zz_bytes_t *src) { + uint8_t l_buf[16]; size_t l_len = _z_zsize_encode_buf(l_buf, _zz_bytes_len(src)); _z_slice_t s = _z_slice_wrap_copy(l_buf, l_len); if (!_z_slice_check(s)) { @@ -171,96 +170,93 @@ int8_t _zz_bytes_append(_zz_bytes_t* dst, _zz_bytes_t* src) { return _Z_RES_OK; } - - - -_zz_bytes_t _zz_bytes_serialize_from_pair(_zz_bytes_t* first, _zz_bytes_t* second) { - _zz_bytes_t out = _zz_bytes_null(); - - if (_zz_bytes_append(&out, first) != _Z_RES_OK) { - _zz_bytes_drop(&out); +int8_t _zz_bytes_serialize_from_pair(_zz_bytes_t *out, _zz_bytes_t *first, _zz_bytes_t *second) { + int8_t res = _zz_bytes_append(out, first); + if (res != _Z_RES_OK) { + _zz_bytes_drop(out); _zz_bytes_drop(first); - } else if (_zz_bytes_append(&out, second) != _Z_RES_OK) { - _zz_bytes_drop(&out); + return res; + } + res = _zz_bytes_append(out, second); + if (res != _Z_RES_OK) { + _zz_bytes_drop(out); _zz_bytes_drop(second); } - return out; + return res; } - -int8_t _zz_bytes_deserialize_into_pair(const _zz_bytes_t* bs, _zz_bytes_t* first_out, _zz_bytes_t* second_out) { +int8_t _zz_bytes_deserialize_into_pair(const _zz_bytes_t *bs, _zz_bytes_t *first_out, _zz_bytes_t *second_out) { _zz_bytes_reader_t reader = _zz_bytes_get_reader(bs); int8_t res = _zz_bytes_reader_read_next(&reader, first_out); if (res != _Z_RES_OK) return res; res = _zz_bytes_reader_read_next(&reader, second_out); if (res != _Z_RES_OK) { - _zz_bytes_drop(first_out); + _zz_bytes_drop(first_out); }; return res; } - - -uint8_t _zz_bytes_to_uint8(const _zz_bytes_t *bs) { - uint8_t val = 0; - _zz_bytes_to_buf(bs, &val, sizeof(val)); - return val; +int8_t _zz_bytes_to_uint8(const _zz_bytes_t *bs, uint8_t *val) { + *val = 0; + return _zz_bytes_to_buf(bs, val, sizeof(uint8_t)) == sizeof(uint8_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } // FIXME: int16+ endianness, Issue #420 -uint16_t _zz_bytes_to_uint16(const _zz_bytes_t *bs) { - uint16_t val = 0; - _zz_bytes_to_buf(bs, (uint8_t*)&val, sizeof(val)); - return val; +int8_t _zz_bytes_to_uint16(const _zz_bytes_t *bs, uint16_t *val) { + *val = 0; + return _zz_bytes_to_buf(bs, (uint8_t *)val, sizeof(uint16_t)) == sizeof(uint16_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } -uint32_t _zz_bytes_to_uint32(const _zz_bytes_t *bs) { - uint32_t val = 0; - _zz_bytes_to_buf(bs, (uint8_t*)&val, sizeof(val)); - return val; +int8_t _zz_bytes_to_uint32(const _zz_bytes_t *bs, uint32_t *val) { + *val = 0; + return _zz_bytes_to_buf(bs, (uint8_t *)val, sizeof(uint32_t)) == sizeof(uint32_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } -uint64_t _zz_bytes_to_uint64(const _zz_bytes_t *bs) { - uint64_t val = 0; - _zz_bytes_to_buf(bs, (uint8_t*)&val, sizeof(val)); - return val; +int8_t _zz_bytes_to_uint64(const _zz_bytes_t *bs, uint64_t *val) { + *val = 0; + return _zz_bytes_to_buf(bs, (uint8_t *)val, sizeof(uint64_t)) == sizeof(uint64_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } -float _zz_bytes_to_float(const _zz_bytes_t *bs) { - float val = 0; - _zz_bytes_to_buf(bs, (uint8_t*)&val, sizeof(val)); - return val; +int8_t _zz_bytes_to_float(const _zz_bytes_t *bs, float *val) { + *val = 0; + return _zz_bytes_to_buf(bs, (uint8_t *)val, sizeof(float)) == sizeof(float) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } -double _zz_bytes_to_double(const _zz_bytes_t *bs) { - double val = 0; - _zz_bytes_to_buf(bs, (uint8_t*)&val, sizeof(val)); - return val; +int8_t _zz_bytes_to_double(const _zz_bytes_t *bs, double *val) { + *val = 0; + return _zz_bytes_to_buf(bs, (uint8_t *)val, sizeof(double)) == sizeof(double) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } -_zz_bytes_t _zz_bytes_from_uint8(uint8_t val) { - return _zz_bytes_from_buf(&val, sizeof(val)); -} +int8_t _zz_bytes_from_uint8(_zz_bytes_t *b, uint8_t val) { return _zz_bytes_from_buf(b, &val, sizeof(val)); } -_zz_bytes_t _zz_bytes_from_uint16(uint16_t val) { - return _zz_bytes_from_buf((uint8_t*)&val, sizeof(val)); +int8_t _zz_bytes_from_uint16(_zz_bytes_t *b, uint16_t val) { + return _zz_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); } -_zz_bytes_t _zz_bytes_from_uint32(uint32_t val) { - return _zz_bytes_from_buf((uint8_t*)&val, sizeof(val)); +int8_t _zz_bytes_from_uint32(_zz_bytes_t *b, uint32_t val) { + return _zz_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); } -_zz_bytes_t _zz_bytes_from_uint64(uint64_t val) { - return _zz_bytes_from_buf((uint8_t*)&val, sizeof(val)); +int8_t _zz_bytes_from_uint64(_zz_bytes_t *b, uint64_t val) { + return _zz_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); } -_zz_bytes_t _zz_bytes_from_float(float val) { - return _zz_bytes_from_buf((uint8_t*)&val, sizeof(val)); +int8_t _zz_bytes_from_float(_zz_bytes_t *b, float val) { return _zz_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); } + +int8_t _zz_bytes_from_double(_zz_bytes_t *b, double val) { return _zz_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); } + +_z_slice_t _zz_bytes_try_get_contiguous(const _zz_bytes_t *bs) { + if (_zz_bytes_num_slices(bs) == 1) { + _z_arc_slice_t *arc_s = _zz_bytes_get_slice(bs, 0); + return _z_slice_wrap(_z_arc_slice_data(arc_s), _z_arc_slice_len(arc_s)); + } + return _z_slice_empty(); } -_zz_bytes_t _zz_bytes_from_double(double val) { - return _zz_bytes_from_buf((uint8_t*)&val, sizeof(val)); +void _zz_bytes_move(_zz_bytes_t *dst, _zz_bytes_t *src) { + dst->_slices = src->_slices; + *src = _zz_bytes_null(); } _zz_bytes_reader_t _zz_bytes_get_reader(const _zz_bytes_t *bytes) { @@ -275,8 +271,8 @@ _zz_bytes_reader_t _zz_bytes_get_reader(const _zz_bytes_t *bytes) { int8_t _zz_bytes_reader_seek_forward(_zz_bytes_reader_t *reader, size_t offset) { size_t start_slice = reader->slice_idx; for (size_t i = start_slice; i < _zz_bytes_num_slices(reader->bytes); ++i) { - _z_arc_slice_t* s = _zz_bytes_get_slice(reader->bytes, i); - size_t remaining = _z_arc_slice_len(s) - reader->in_slice_idx; + _z_arc_slice_t *s = _zz_bytes_get_slice(reader->bytes, i); + size_t remaining = _z_arc_slice_len(s) - reader->in_slice_idx; if (offset >= remaining) { reader->slice_idx += 1; reader->in_slice_idx = 0; @@ -299,10 +295,10 @@ int8_t _zz_bytes_reader_seek_backward(_zz_bytes_reader_t *reader, size_t offset) if (reader->in_slice_idx == 0) { if (reader->slice_idx == 0) return _Z_ERR_DID_NOT_READ; reader->slice_idx--; - _z_arc_slice_t* s = _zz_bytes_get_slice(reader->bytes, reader->slice_idx); + _z_arc_slice_t *s = _zz_bytes_get_slice(reader->bytes, reader->slice_idx); reader->in_slice_idx = _z_arc_slice_len(s); } - + if (offset > reader->in_slice_idx) { offset -= reader->in_slice_idx; reader->byte_idx -= reader->in_slice_idx; @@ -316,41 +312,43 @@ int8_t _zz_bytes_reader_seek_backward(_zz_bytes_reader_t *reader, size_t offset) return _Z_RES_OK; } - int8_t _zz_bytes_reader_seek(_zz_bytes_reader_t *reader, int64_t offset, int origin) { switch (origin) { case SEEK_SET: { reader->byte_idx = 0; reader->in_slice_idx = 0; reader->slice_idx = 0; - if (offset < 0) return _Z_ERR_DID_NOT_READ; + if (offset < 0) return _Z_ERR_DID_NOT_READ; return _zz_bytes_reader_seek_forward(reader, offset); } case SEEK_CUR: { - if (offset >= 0) return _zz_bytes_reader_seek_forward(reader, offset); - else return _zz_bytes_reader_seek_backward(reader, -offset); + if (offset >= 0) + return _zz_bytes_reader_seek_forward(reader, offset); + else + return _zz_bytes_reader_seek_backward(reader, -offset); } case SEEK_END: { reader->byte_idx = _zz_bytes_len(reader->bytes); reader->in_slice_idx = 0; reader->slice_idx = _zz_bytes_num_slices(reader->bytes); - if (offset > 0) return _Z_ERR_DID_NOT_READ; - else return _zz_bytes_reader_seek_backward(reader, -offset); + if (offset > 0) + return _Z_ERR_DID_NOT_READ; + else + return _zz_bytes_reader_seek_backward(reader, -offset); } - default: return _Z_ERR_GENERIC; + default: + return _Z_ERR_GENERIC; } } -int64_t _zz_bytes_reader_tell(const _zz_bytes_reader_t *reader) { - return reader->byte_idx; -} +int64_t _zz_bytes_reader_tell(const _zz_bytes_reader_t *reader) { return reader->byte_idx; } + +int8_t _zz_bytes_reader_read(_zz_bytes_reader_t *reader, uint8_t *buf, size_t len) { + uint8_t *buf_start = buf; -int8_t _zz_bytes_reader_read(_zz_bytes_reader_t *reader, uint8_t* buf, size_t len) { - uint8_t* buf_start = buf; - for (size_t i = reader->slice_idx; i < _zz_bytes_num_slices(reader->bytes); ++i) { - _z_arc_slice_t* s = _zz_bytes_get_slice(reader->bytes, i); - size_t remaining = _z_arc_slice_len(s) - reader->in_slice_idx; + _z_arc_slice_t *s = _zz_bytes_get_slice(reader->bytes, i); + size_t remaining = _z_arc_slice_len(s) - reader->in_slice_idx; if (len >= remaining) { memcpy(buf_start, _z_arc_slice_data(s) + reader->in_slice_idx, remaining); reader->slice_idx += 1; @@ -371,9 +369,8 @@ int8_t _zz_bytes_reader_read(_zz_bytes_reader_t *reader, uint8_t* buf, size_t le return _Z_RES_OK; } - -int8_t __read_single_byte(uint8_t* b, void* context) { - _zz_bytes_reader_t* reader = (_zz_bytes_reader_t*)context; +int8_t __read_single_byte(uint8_t *b, void *context) { + _zz_bytes_reader_t *reader = (_zz_bytes_reader_t *)context; return _zz_bytes_reader_read(reader, b, 1); } @@ -381,12 +378,12 @@ int8_t _zz_bytes_reader_read_zint(_zz_bytes_reader_t *reader, _z_zint_t *zint) { return _z_zsize_decode_with_reader(zint, __read_single_byte, reader); } -int8_t _zz_bytes_reader_read_slices(_zz_bytes_reader_t* reader, size_t len, _zz_bytes_t* out) { +int8_t _zz_bytes_reader_read_slices(_zz_bytes_reader_t *reader, size_t len, _zz_bytes_t *out) { *out = _zz_bytes_null(); int8_t res = _Z_RES_OK; - + for (size_t i = reader->slice_idx; i < _zz_bytes_num_slices(reader->bytes) && len > 0; ++i) { - _z_arc_slice_t* s = _zz_bytes_get_slice(reader->bytes, i); + _z_arc_slice_t *s = _zz_bytes_get_slice(reader->bytes, i); size_t s_len = _z_arc_slice_len(s); size_t remaining = s_len - reader->in_slice_idx; @@ -415,10 +412,9 @@ int8_t _zz_bytes_reader_read_slices(_zz_bytes_reader_t* reader, size_t len, _zz_ if (res != _Z_RES_OK) _zz_bytes_drop(out); return res; - } -int8_t _zz_bytes_reader_read_next(_zz_bytes_reader_t* reader, _zz_bytes_t* out) { +int8_t _zz_bytes_reader_read_next(_zz_bytes_reader_t *reader, _zz_bytes_t *out) { *out = _zz_bytes_null(); _z_zint_t len; if (_zz_bytes_reader_read_zint(reader, &len) != _Z_RES_OK) { @@ -427,3 +423,11 @@ int8_t _zz_bytes_reader_read_next(_zz_bytes_reader_t* reader, _zz_bytes_t* out) return _zz_bytes_reader_read_slices(reader, len, out); } + +_zz_bytes_iterator_t _zz_bytes_get_iterator(const _zz_bytes_t *bytes) { + return (_zz_bytes_iterator_t){._reader = _zz_bytes_get_reader(bytes)}; +} + +_Bool _zz_bytes_iterator_next(_zz_bytes_iterator_t *iter, _zz_bytes_t *b) { + return _zz_bytes_reader_read_next(&iter->_reader, b) == _Z_RES_OK; +} diff --git a/src/collections/slice.c b/src/collections/slice.c index bd61078e4..97b97746e 100644 --- a/src/collections/slice.c +++ b/src/collections/slice.c @@ -34,7 +34,7 @@ int8_t _z_slice_init(_z_slice_t *bs, size_t capacity) { bs->len = 0; bs->_is_alloc = false; } - + if (bs->len != capacity) { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; } @@ -85,12 +85,13 @@ void _z_slice_free(_z_slice_t **bs) { } } -void _z_slice_copy(_z_slice_t *dst, const _z_slice_t *src) { +int8_t _z_slice_copy(_z_slice_t *dst, const _z_slice_t *src) { int8_t ret = _z_slice_init(dst, src->len); // FIXME: it should check if dst is already initialized. Otherwise it will leak if (ret == _Z_RES_OK) { (void)memcpy((uint8_t *)dst->start, src->start, src->len); } + return ret; } void _z_slice_move(_z_slice_t *dst, _z_slice_t *src) { @@ -117,163 +118,3 @@ _z_slice_t _z_slice_steal(_z_slice_t *b) { _Bool _z_slice_eq(const _z_slice_t *left, const _z_slice_t *right) { return left->len == right->len && memcmp(left->start, right->start, left->len) == 0; } - -/*-------- 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(), - }; -} - -_z_bytes_t _z_bytes_make(size_t capacity) { - return (_z_bytes_t){ - ._slice = _z_slice_make(capacity), - }; -} - -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) { - return; - } - } - (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_bytes_clear(ptr); - - z_free(ptr); - *bs = NULL; - } -} - -uint8_t _z_bytes_to_uint8(const _z_bytes_t *bs) { - uint8_t val = 0; - memcpy(&val, bs->_slice.start, sizeof(val)); - return val; -} - -// FIXME: int16+ endianness, Issue #420 -uint16_t _z_bytes_to_uint16(const _z_bytes_t *bs) { - uint16_t val = 0; - memcpy(&val, bs->_slice.start, sizeof(val)); - return val; -} - -uint32_t _z_bytes_to_uint32(const _z_bytes_t *bs) { - uint32_t val = 0; - memcpy(&val, bs->_slice.start, sizeof(val)); - return val; -} - -uint64_t _z_bytes_to_uint64(const _z_bytes_t *bs) { - uint64_t val = 0; - memcpy(&val, bs->_slice.start, sizeof(val)); - return val; -} - -float _z_bytes_to_float(const _z_bytes_t *bs) { - float val = 0; - memcpy(&val, bs->_slice.start, sizeof(val)); - return val; -} - -double _z_bytes_to_double(const _z_bytes_t *bs) { - double val = 0; - memcpy(&val, bs->_slice.start, sizeof(val)); - return val; -} - -_z_slice_t _z_bytes_to_slice(const _z_bytes_t *bytes) { - // Allocate slice - _z_slice_t ret = _z_slice_make(bytes->_slice.len); - if (!_z_slice_check(ret)) { - return ret; - } - // Recopy data - memcpy((uint8_t *)ret.start, bytes->_slice.start, bytes->_slice.len); - return ret; -} - -_z_bytes_t _z_bytes_from_uint8(uint8_t val) { - _z_bytes_t ret = _z_bytes_null(); - // Init bytes array - if (_z_slice_init(&ret._slice, sizeof(val)) != _Z_RES_OK) { - return ret; - } - // Encode int - memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); - return ret; -} - -_z_bytes_t _z_bytes_from_uint16(uint16_t val) { - _z_bytes_t ret = _z_bytes_null(); - // Init bytes array - if (_z_slice_init(&ret._slice, sizeof(val)) != _Z_RES_OK) { - return ret; - } - // Encode int - memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); - return ret; -} - -_z_bytes_t _z_bytes_from_uint32(uint32_t val) { - _z_bytes_t ret = _z_bytes_null(); - // Init bytes array - if (_z_slice_init(&ret._slice, sizeof(val)) != _Z_RES_OK) { - return ret; - } - // Encode int - memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); - return ret; -} - -_z_bytes_t _z_bytes_from_uint64(uint64_t val) { - _z_bytes_t ret = _z_bytes_null(); - // Init bytes array - if (_z_slice_init(&ret._slice, sizeof(val)) != _Z_RES_OK) { - return ret; - } - // Encode int - memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); - return ret; -} - -_z_bytes_t _z_bytes_from_float(float val) { - _z_bytes_t ret = _z_bytes_null(); - // Init bytes array - if (_z_slice_init(&ret._slice, sizeof(val)) != _Z_RES_OK) { - return ret; - } - // Encode float - memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); - return ret; -} - -_z_bytes_t _z_bytes_from_double(double val) { - _z_bytes_t ret = _z_bytes_null(); - // Init bytes array - if (_z_slice_init(&ret._slice, sizeof(val)) != _Z_RES_OK) { - return ret; - } - // Encode double - memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); - return ret; -} diff --git a/src/collections/vec.c b/src/collections/vec.c index 9488acda4..af58c42c0 100644 --- a/src/collections/vec.c +++ b/src/collections/vec.c @@ -61,7 +61,7 @@ void _z_vec_release(_z_vec_t *v) { v->_val = NULL; v->_capacity = 0; - v->_len = 0; + v->_len = 0; } void _z_vec_free(_z_vec_t **v, z_element_free_f free_f) { @@ -128,8 +128,6 @@ void _z_vec_remove(_z_vec_t *v, size_t pos, z_element_free_f free_f) { v->_len = v->_len - 1; } - - /*-------- svec --------*/ _z_svec_t _z_svec_make(size_t capacity, size_t element_size) { _z_svec_t v = {._capacity = 0, ._len = 0, ._val = NULL}; @@ -148,7 +146,7 @@ void __z_svec_copy_inner(void *dst, const void *src, z_element_copy_f copy, size } else { size_t offset = 0; for (size_t i = 0; i < num_elements; i++) { - copy((uint8_t*)dst + offset, (uint8_t*)src + offset); + copy((uint8_t *)dst + offset, (uint8_t *)src + offset); offset += element_size; } } @@ -160,7 +158,7 @@ void __z_svec_move_inner(void *dst, void *src, z_element_move_f move, size_t num } else { size_t offset = 0; for (size_t i = 0; i < num_elements; i++) { - move((uint8_t*)dst + offset, (uint8_t*)src + offset); + move((uint8_t *)dst + offset, (uint8_t *)src + offset); offset += element_size; } } @@ -181,7 +179,7 @@ void _z_svec_reset(_z_svec_t *v, z_element_clear_f clear, size_t element_size) { if (clear != NULL) { size_t offset = 0; for (size_t i = 0; i < v->_len; i++) { - clear((uint8_t*)v->_val + offset); + clear((uint8_t *)v->_val + offset); offset += element_size; } } @@ -198,7 +196,7 @@ void _z_svec_release(_z_svec_t *v) { v->_val = NULL; v->_capacity = 0; - v->_len = 0; + v->_len = 0; } void _z_svec_free(_z_svec_t **v, z_element_clear_f clear, size_t element_size) { @@ -229,13 +227,13 @@ bool _z_svec_append(_z_svec_t *v, const void *e, z_element_move_f move, size_t e // Update the current vector v->_val = _val; v->_capacity = _capacity; - memcpy((uint8_t*)v->_val + v->_len * element_size, e, element_size); + memcpy((uint8_t *)v->_val + v->_len * element_size, e, element_size); v->_len++; } else { return false; } } else { - memcpy((uint8_t*)v->_val + v->_len * element_size, e, element_size); + memcpy((uint8_t *)v->_val + v->_len * element_size, e, element_size); v->_len++; } return true; @@ -243,22 +241,20 @@ bool _z_svec_append(_z_svec_t *v, const void *e, z_element_move_f move, size_t e void *_z_svec_get(const _z_svec_t *v, size_t i, size_t element_size) { assert(i < v->_len); - return (uint8_t*)v->_val + i * element_size; + return (uint8_t *)v->_val + i * element_size; } void _z_svec_set(_z_svec_t *v, size_t i, void *e, z_element_clear_f clear, size_t element_size) { assert(i < v->_len); - clear((uint8_t*)v->_val + i * element_size); - memcpy((uint8_t*)v->_val + i * element_size, e, element_size); + clear((uint8_t *)v->_val + i * element_size); + memcpy((uint8_t *)v->_val + i * element_size, e, element_size); } void _z_svec_remove(_z_svec_t *v, size_t pos, z_element_clear_f clear, z_element_move_f move, size_t element_size) { assert(pos < v->_len); - clear((uint8_t*)v->_val + pos * element_size); - __z_svec_move_inner( - (uint8_t*)v->_val + pos * element_size, (uint8_t*)v->_val + (pos + 1) * element_size, - move, (v->_len - pos - 1) * element_size, element_size - ); + clear((uint8_t *)v->_val + pos * element_size); + __z_svec_move_inner((uint8_t *)v->_val + pos * element_size, (uint8_t *)v->_val + (pos + 1) * element_size, move, + (v->_len - pos - 1) * element_size, element_size); v->_len--; } diff --git a/src/net/memory.c b/src/net/memory.c index 1d60c338f..fbf6258b9 100644 --- a/src/net/memory.c +++ b/src/net/memory.c @@ -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_bytes_clear(&value->payload); + _zz_bytes_drop(&value->payload); } void _z_value_free(_z_value_t **value) { diff --git a/src/net/primitives.c b/src/net/primitives.c index f50cbd611..4f9db8786 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -130,9 +130,9 @@ int8_t _z_undeclare_publisher(_z_publisher_t *pub) { } /*------------------ Write ------------------*/ -int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *payload, const size_t len, - const _z_encoding_t encoding, const z_sample_kind_t kind, const z_congestion_control_t cong_ctrl, - z_priority_t priority, const _z_bytes_t attachment) { +int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _zz_bytes_t payload, const _z_encoding_t encoding, + const z_sample_kind_t kind, const z_congestion_control_t cong_ctrl, z_priority_t priority, + const _zz_bytes_t attachment) { int8_t ret = _Z_RES_OK; _z_network_message_t msg; switch (kind) { @@ -148,7 +148,7 @@ int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *pay ._body._body._put = { ._commons = {._timestamp = _z_timestamp_null(), ._source_info = _z_source_info_null()}, - ._payload = _z_slice_wrap(payload, len), + ._payload = payload, ._encoding = encoding, ._attachment = attachment, }, @@ -318,7 +318,7 @@ int8_t _z_undeclare_queryable(_z_queryable_t *qle) { } int8_t _z_send_reply(const _z_query_t *query, _z_keyexpr_t keyexpr, const _z_value_t payload, - const z_sample_kind_t kind, const _z_bytes_t att) { + const z_sample_kind_t kind, const _zz_bytes_t att) { int8_t ret = _Z_RES_OK; _z_keyexpr_t q_ke; @@ -353,7 +353,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._slice; + z_msg._body._response._body._reply._body._body._put._payload = payload.payload; 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(); @@ -375,7 +375,7 @@ int8_t _z_send_reply(const _z_query_t *query, _z_keyexpr_t keyexpr, const _z_val /*------------------ Query ------------------*/ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, const z_query_target_t target, const z_consolidation_mode_t consolidation, _z_value_t value, _z_reply_handler_t callback, - _z_drop_handler_t dropper, void *arg, uint32_t timeout_ms, const _z_bytes_t attachment) { + _z_drop_handler_t dropper, void *arg, uint32_t timeout_ms, const _zz_bytes_t attachment) { int8_t ret = _Z_RES_OK; // Create the pending query object diff --git a/src/net/query.c b/src/net/query.c index 798dbd2a1..8c5477272 100644 --- a/src/net/query.c +++ b/src/net/query.c @@ -38,12 +38,12 @@ void _z_query_clear(_z_query_t *q) { z_free(q->_parameters); _z_keyexpr_clear(&q->_key); _z_value_clear(&q->_value); - _z_bytes_clear(&q->attachment); + _zz_bytes_drop(&q->attachment); } #if Z_FEATURE_QUERYABLE == 1 _z_query_t _z_query_create(const _z_value_t *value, const _z_keyexpr_t *key, const _z_slice_t *parameters, - _z_session_t *zn, uint32_t request_id, const _z_bytes_t att) { + _z_session_t *zn, uint32_t request_id, const _zz_bytes_t attachment) { _z_query_t q = _z_query_null(); q._request_id = request_id; q._zn = zn; // Ideally would have been an rc @@ -51,7 +51,7 @@ _z_query_t _z_query_create(const _z_value_t *value, const _z_keyexpr_t *key, con memcpy(q._parameters, parameters->start, parameters->len); q._parameters[parameters->len] = 0; q._anyke = (strstr(q._parameters, Z_SELECTOR_QUERY_MATCH) == NULL) ? false : true; - q.attachment._slice = _z_slice_steal((_z_slice_t *)&att._slice); + q.attachment = attachment; _z_keyexpr_copy(&q._key, key); _z_value_copy(&q._value, value); diff --git a/src/net/reply.c b/src/net/reply.c index 2dff090e2..5df0544a1 100644 --- a/src/net/reply.c +++ b/src/net/reply.c @@ -84,9 +84,9 @@ void _z_pending_reply_clear(_z_pending_reply_t *pr) { _z_timestamp_clear(&pr->_tstamp); } -_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_slice_t *payload, +_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _zz_bytes_t payload, const _z_timestamp_t *timestamp, _z_encoding_t encoding, z_sample_kind_t kind, - const _z_bytes_t att) { + const _zz_bytes_t attachment) { _z_reply_t reply = _z_reply_null(); reply._tag = tag; if (tag == Z_REPLY_TAG_DATA) { @@ -95,10 +95,10 @@ _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._slice, payload); + sample.payload = payload; // FIXME: call z_bytes_move or copy sample.kind = kind; sample.timestamp = _z_timestamp_duplicate(timestamp); - sample.attachment._slice = _z_slice_steal((_z_slice_t *)&att._slice); + sample.attachment = attachment; // FIXME: call z_bytes_move or copy // Create sample rc from value reply.data.sample = _z_sample_rc_new_from_val(sample); @@ -106,9 +106,9 @@ _z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, return reply; } #else -_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_slice_t *payload, +_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _zz_bytes_t payload, const _z_timestamp_t *timestamp, _z_encoding_t encoding, z_sample_kind_t kind, - const _z_bytes_t att) { + const _zz_bytes_t attachment) { _ZP_UNUSED(keyexpr); _ZP_UNUSED(tag); _ZP_UNUSED(id); @@ -116,7 +116,7 @@ _z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, _ZP_UNUSED(timestamp); _ZP_UNUSED(encoding); _ZP_UNUSED(kind); - _ZP_UNUSED(att); + _ZP_UNUSED(attachment); return _z_reply_null(); } #endif diff --git a/src/net/sample.c b/src/net/sample.c index 9db7355e3..5e3809778 100644 --- a/src/net/sample.c +++ b/src/net/sample.c @@ -19,18 +19,18 @@ _z_sample_t _z_sample_null(void) { _z_sample_t s = { .keyexpr = _z_keyexpr_null(), - .payload = _z_bytes_null(), + .payload = _zz_bytes_null(), .encoding = _z_encoding_null(), .timestamp = _z_timestamp_null(), .kind = 0, .qos = {0}, - .attachment = _z_bytes_null(), + .attachment = _zz_bytes_null(), }; return s; } _Bool _z_sample_check(const _z_sample_t *sample) { - return _z_keyexpr_check(sample->keyexpr) && _z_bytes_check(sample->payload); + return _z_keyexpr_check(sample->keyexpr) && _zz_bytes_check(&sample->payload); } void _z_sample_move(_z_sample_t *dst, _z_sample_t *src) { @@ -38,7 +38,7 @@ void _z_sample_move(_z_sample_t *dst, _z_sample_t *src) { dst->keyexpr._suffix = src->keyexpr._suffix; // FIXME: call the z_keyexpr_move src->keyexpr._suffix = NULL; // FIXME: call the z_keyexpr_move - _z_bytes_move(&dst->payload, &src->payload); + _zz_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 @@ -47,10 +47,10 @@ 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_bytes_clear(&sample->payload); + _zz_bytes_drop(&sample->payload); _z_encoding_clear(&sample->encoding); _z_timestamp_clear(&sample->timestamp); - _z_bytes_clear(&sample->attachment); + _zz_bytes_drop(&sample->attachment); } void _z_sample_free(_z_sample_t **sample) { @@ -64,11 +64,11 @@ 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_bytes_duplicate(&src->payload); + _zz_bytes_copy(&dst->payload, &src->payload); dst->timestamp = _z_timestamp_duplicate(&src->timestamp); _z_encoding_copy(&dst->encoding, &src->encoding); dst->kind = src->kind; - dst->attachment = _z_bytes_duplicate(&src->attachment); + _zz_bytes_copy(&dst->attachment, &src->attachment); } _z_sample_t _z_sample_duplicate(const _z_sample_t *src) { @@ -78,30 +78,30 @@ _z_sample_t _z_sample_duplicate(const _z_sample_t *src) { } #if Z_FEATURE_SUBSCRIPTION == 1 -_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_slice_t *payload, const _z_timestamp_t timestamp, +_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _zz_bytes_t payload, const _z_timestamp_t timestamp, const _z_encoding_t encoding, const z_sample_kind_t kind, const _z_qos_t qos, - const _z_bytes_t att) { + const _zz_bytes_t attachment) { _z_sample_t s = _z_sample_null(); _z_keyexpr_copy(&s.keyexpr, key); - _z_slice_copy(&s.payload._slice, payload); + s.payload = payload; _z_encoding_copy(&s.encoding, &encoding); s.kind = kind; s.timestamp = timestamp; s.qos = qos; - s.attachment._slice = _z_slice_steal((_z_slice_t *)&att._slice); + s.attachment = attachment; return s; } #else -_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_slice_t *payload, const _z_timestamp_t timestamp, +_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _zz_bytes payload, const _z_timestamp_t timestamp, const _z_encoding_t encoding, const z_sample_kind_t kind, const _z_qos_t qos, - const _z_bytes_t att) { + const _zz_bytes_t attachment) { _ZP_UNUSED(key); _ZP_UNUSED(payload); _ZP_UNUSED(timestamp); _ZP_UNUSED(encoding); _ZP_UNUSED(kind); _ZP_UNUSED(qos); - _ZP_UNUSED(att); + _ZP_UNUSED(attachment); return _z_sample_null(); } #endif diff --git a/src/protocol/codec.c b/src/protocol/codec.c index cb0c19a12..c34ec645c 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -120,7 +120,7 @@ uint8_t _z_zint_len(uint64_t v) { } } -uint8_t _z_zint64_encode_buf(uint8_t* buf, uint64_t v) { +uint8_t _z_zint64_encode_buf(uint8_t *buf, uint64_t v) { uint64_t lv = v; uint8_t len = 0; size_t start = 0; @@ -153,7 +153,7 @@ int8_t _z_zint16_encode(_z_wbuf_t *wbf, uint16_t v) { return _z_zint64_encode(wb int8_t _z_zint32_encode(_z_wbuf_t *wbf, uint32_t v) { return _z_zint64_encode(wbf, (uint64_t)v); } int8_t _z_zsize_encode(_z_wbuf_t *wbf, _z_zint_t v) { return _z_zint64_encode(wbf, (uint64_t)v); } -int8_t _z_zint64_decode_with_reader(uint64_t *zint, __z_single_byte_reader_t reader, void* context) { +int8_t _z_zint64_decode_with_reader(uint64_t *zint, __z_single_byte_reader_t reader, void *context) { *zint = 0; uint8_t b = 0; @@ -167,10 +167,10 @@ int8_t _z_zint64_decode_with_reader(uint64_t *zint, __z_single_byte_reader_t rea } *zint = *zint | ((uint64_t)b << i); - return _Z_RES_OK; + return _Z_RES_OK; } -int8_t _z_zsize_decode_with_reader(_z_zint_t *zint, __z_single_byte_reader_t reader, void* context) { +int8_t _z_zsize_decode_with_reader(_z_zint_t *zint, __z_single_byte_reader_t reader, void *context) { uint64_t i = 0; int8_t res = _z_zint64_decode_with_reader(&i, reader, context); if (res == _Z_RES_OK && i > SIZE_MAX) { @@ -181,12 +181,10 @@ int8_t _z_zsize_decode_with_reader(_z_zint_t *zint, __z_single_byte_reader_t rea return res; } -int8_t _z_uint8_decode_reader(uint8_t* zint, void* context) { - return _z_uint8_decode(zint, (_z_zbuf_t*)context); -} +int8_t _z_uint8_decode_reader(uint8_t *zint, void *context) { return _z_uint8_decode(zint, (_z_zbuf_t *)context); } int8_t _z_zint64_decode(uint64_t *zint, _z_zbuf_t *zbf) { - return _z_zint64_decode_with_reader(zint, _z_uint8_decode_reader, (void*)zbf); + return _z_zint64_decode_with_reader(zint, _z_uint8_decode_reader, (void *)zbf); } int8_t _z_zint16_decode(uint16_t *zint, _z_zbuf_t *zbf) { @@ -214,22 +212,24 @@ int8_t _z_zint32_decode(uint32_t *zint, _z_zbuf_t *zbf) { } int8_t _z_zsize_decode(_z_zint_t *zint, _z_zbuf_t *zbf) { - return _z_zsize_decode_with_reader(zint, _z_uint8_decode_reader, (void*)zbf); + return _z_zsize_decode_with_reader(zint, _z_uint8_decode_reader, (void *)zbf); } /*------------------ uint8_array ------------------*/ -int8_t _z_slice_val_encode(_z_wbuf_t *wbf, const _z_slice_t *bs) { +int8_t _z_buf_encode(_z_wbuf_t *wbf, const uint8_t *buf, size_t len) { int8_t ret = _Z_RES_OK; - if ((wbf->_expansion_step != 0) && (bs->len > Z_TSID_LENGTH)) { - ret |= _z_wbuf_wrap_bytes(wbf, bs->start, 0, bs->len); + if ((wbf->_expansion_step != 0) && (len > Z_TSID_LENGTH)) { + ret |= _z_wbuf_wrap_bytes(wbf, buf, 0, len); } else { - ret |= _z_wbuf_write_bytes(wbf, bs->start, 0, bs->len); + ret |= _z_wbuf_write_bytes(wbf, buf, 0, len); } return ret; } +int8_t _z_slice_val_encode(_z_wbuf_t *wbf, const _z_slice_t *bs) { return _z_buf_encode(wbf, bs->start, bs->len); } + int8_t _z_slice_encode(_z_wbuf_t *wbf, const _z_slice_t *bs) { int8_t ret = _Z_RES_OK; @@ -274,9 +274,28 @@ 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 _zz_bytes_decode(_zz_bytes_t *bs, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + _z_slice_t s; + ret = _z_slice_decode(&s, zbf); + if (ret != _Z_RES_OK) return ret; + return _zz_bytes_from_slice(bs, s); +} -int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { return _z_slice_encode(wbf, &bs->_slice); } +int8_t _zz_bytes_encode_val(_z_wbuf_t *wbf, const _zz_bytes_t *bs) { + int8_t ret = _Z_RES_OK; + for (size_t i = 0; i < _zz_bytes_num_slices(bs); ++i) { + const _z_arc_slice_t *arc_s = _zz_bytes_get_slice(bs, i); + _Z_RETURN_IF_ERR(_z_buf_encode(wbf, _z_arc_slice_data(arc_s), _z_arc_slice_len(arc_s))) + } + + return ret; +} + +int8_t _zz_bytes_encode(_z_wbuf_t *wbf, const _zz_bytes_t *bs) { + _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, _zz_bytes_len(bs))) + return _zz_bytes_encode_val(wbf, bs); +} /*------------------ string with null terminator ------------------*/ int8_t _z_str_encode(_z_wbuf_t *wbf, const char *s) { @@ -358,3 +377,16 @@ int8_t _z_encoding_decode(_z_encoding_t *en, _z_zbuf_t *zbf) { } return _Z_RES_OK; } + +int8_t _z_value_encode(_z_wbuf_t *wbf, const _z_value_t *value) { + size_t total_len = _z_encoding_len(&value->encoding) + _zz_bytes_len(&value->payload); + _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, total_len)); + _Z_RETURN_IF_ERR(_z_encoding_encode(wbf, &value->encoding)); + return _zz_bytes_encode_val(wbf, &value->payload); +} + +int8_t _z_value_decode(_z_value_t *value, _z_zbuf_t *zbf) { + _Z_RETURN_IF_ERR(_z_encoding_decode(&value->encoding, zbf)); + _Z_RETURN_IF_ERR(_zz_bytes_from_buf(&value->payload, (uint8_t *)_z_zbuf_start(zbf), _z_zbuf_len(zbf))); + return _Z_RES_OK; +} diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 139395e2a..f64862d67 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -254,7 +254,7 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { pshb->_body._put._commons._source_info._source_sn != 0 || pshb->_body._put._commons._source_info._entity_id != 0; - _Bool has_attachment = pshb->_is_put && _z_bytes_check(pshb->_body._put._attachment); + _Bool has_attachment = pshb->_is_put && _zz_bytes_check(&pshb->_body._put._attachment); _Bool has_timestamp = _z_timestamp_check(&pshb->_body._put._commons._timestamp); _Bool has_encoding = false; if (has_source_info || has_attachment) { @@ -288,10 +288,10 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { } if (has_attachment) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x03)); - _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &pshb->_body._put._attachment._slice)); + _Z_RETURN_IF_ERR(_zz_bytes_encode(wbf, &pshb->_body._put._attachment)); } if (pshb->_is_put) { - _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &pshb->_body._put._payload)); + _Z_RETURN_IF_ERR(_zz_bytes_encode(wbf, &pshb->_body._put._payload)); } return 0; @@ -306,10 +306,13 @@ int8_t _z_push_body_decode_extensions(_z_msg_ext_t *extension, void *ctx) { break; } case _Z_MSG_EXT_ENC_ZBUF | 0x03: { // Attachment - pshb->_body._put._attachment._slice = extension->_body._zbuf._val._is_alloc - ? _z_slice_steal(&extension->_body._zbuf._val) - : _z_slice_duplicate(&extension->_body._zbuf._val); - break; + _z_slice_t s; + if (extension->_body._zbuf._val._is_alloc) { + s = _z_slice_steal(&extension->_body._zbuf._val); + } else { + _Z_RETURN_IF_ERR(_z_slice_copy(&s, &extension->_body._zbuf._val)); + } + ret = _zz_bytes_from_slice(&pshb->_body._put._attachment, s); } default: if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { @@ -336,7 +339,7 @@ int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf, uint8_t header) _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_push_body_decode_extensions, pshb)); } if (ret == _Z_RES_OK) { - _Z_RETURN_IF_ERR(_z_slice_decode(&pshb->_body._put._payload, zbf)); + _Z_RETURN_IF_ERR(_zz_bytes_decode(&pshb->_body._put._payload, zbf)); } break; } @@ -414,10 +417,7 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *msg) { extheader |= _Z_FLAG_Z_Z; } _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._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._slice)); + _Z_RETURN_IF_ERR(_z_value_encode(wbf, &msg->_ext_value)); } if (required_exts.info) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01; @@ -429,7 +429,7 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *msg) { } if (required_exts.attachment) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x05)); - _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &msg->_ext_attachment._slice)); + _Z_RETURN_IF_ERR(_zz_bytes_encode(wbf, &msg->_ext_attachment)); } return ret; } @@ -446,14 +446,17 @@ int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) { case _Z_MSG_EXT_ENC_ZBUF | 0x03: { // Payload _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._slice, &bytes); + ret |= _zz_bytes_from_buf(&msg->_ext_value.payload, (uint8_t *)_z_zbuf_start(&zbf), _z_zbuf_len(&zbf)); break; } case _Z_MSG_EXT_ENC_ZBUF | 0x05: { // Attachment - msg->_ext_attachment._slice = extension->_body._zbuf._val._is_alloc - ? _z_slice_steal(&extension->_body._zbuf._val) - : _z_slice_duplicate(&extension->_body._zbuf._val); + _z_slice_t s; + if (extension->_body._zbuf._val._is_alloc) { + s = _z_slice_steal(&extension->_body._zbuf._val); + } else { + _Z_RETURN_IF_ERR(_z_slice_copy(&s, &extension->_body._zbuf._val)); + } + ret = _zz_bytes_from_slice(&msg->_ext_attachment, s); break; } default: @@ -550,7 +553,7 @@ int8_t _z_err_encode(_z_wbuf_t *wbf, const _z_msg_err_t *err) { _Z_RETURN_IF_ERR(_z_source_info_encode_ext(wbf, &err->_ext_source_info)); } // Encode payload - _Z_RETURN_IF_ERR(_z_slice_encode(wbf, &err->_payload)); + _Z_RETURN_IF_ERR(_zz_bytes_encode(wbf, &err->_payload)); return ret; } int8_t _z_err_decode_extension(_z_msg_ext_t *extension, void *ctx) { @@ -578,7 +581,7 @@ int8_t _z_err_decode(_z_msg_err_t *err, _z_zbuf_t *zbf, uint8_t header) { if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_err_decode_extension, err)); } - _Z_RETURN_IF_ERR(_z_slice_decode(&err->_payload, zbf)); + _Z_RETURN_IF_ERR(_zz_bytes_decode(&err->_payload, zbf)); return _Z_RES_OK; } diff --git a/src/protocol/core.c b/src/protocol/core.c index 08cc542c4..6601285a9 100644 --- a/src/protocol/core.c +++ b/src/protocol/core.c @@ -66,7 +66,7 @@ _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}; } -_z_value_t _z_value_null(void) { return (_z_value_t){.payload = _z_bytes_null(), .encoding = _z_encoding_null()}; } +_z_value_t _z_value_null(void) { return (_z_value_t){.payload = _zz_bytes_null(), .encoding = _z_encoding_null()}; } _z_value_t _z_value_steal(_z_value_t *value) { _z_value_t ret = *value; *value = _z_value_null(); @@ -74,5 +74,5 @@ _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_bytes_copy(&dst->payload, &src->payload); + _zz_bytes_copy(&dst->payload, &src->payload); } diff --git a/src/protocol/definitions/message.c b/src/protocol/definitions/message.c index 1f448562c..94781d44f 100644 --- a/src/protocol/definitions/message.c +++ b/src/protocol/definitions/message.c @@ -24,15 +24,15 @@ void _z_msg_reply_clear(_z_msg_reply_t *msg) { _z_push_body_clear(&msg->_body); void _z_msg_put_clear(_z_msg_put_t *msg) { _z_encoding_clear(&msg->_encoding); - _z_slice_clear(&msg->_payload); + _zz_bytes_drop(&msg->_payload); _z_timestamp_clear(&msg->_commons._timestamp); } _z_msg_query_reqexts_t _z_msg_query_required_extensions(const _z_msg_query_t *msg) { return (_z_msg_query_reqexts_t){ - .body = msg->_ext_value.payload._slice.start != NULL || _z_encoding_check(&msg->_ext_value.encoding), + .body = _zz_bytes_check(&msg->_ext_value.payload) || _z_encoding_check(&msg->_ext_value.encoding), .info = _z_id_check(msg->_ext_info._id) || msg->_ext_info._entity_id != 0 || msg->_ext_info._source_sn != 0, - .attachment = _z_bytes_check(msg->_ext_attachment), + .attachment = _zz_bytes_check(&msg->_ext_attachment), }; } @@ -42,5 +42,5 @@ void _z_msg_query_clear(_z_msg_query_t *msg) { } void _z_msg_err_clear(_z_msg_err_t *err) { _z_encoding_clear(&err->encoding); - _z_slice_clear(&err->_payload); + _zz_bytes_drop(&err->_payload); } diff --git a/src/protocol/definitions/network.c b/src/protocol/definitions/network.c index f7c08f34f..da227de38 100644 --- a/src/protocol/definitions/network.c +++ b/src/protocol/definitions/network.c @@ -138,7 +138,7 @@ void _z_n_msg_free(_z_network_message_t **msg) { _z_zenoh_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_slice_t) parameters, _z_zint_t qid, z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value, - uint32_t timeout_ms, _z_bytes_t attachment) { + uint32_t timeout_ms, _zz_bytes_t attachment) { return (_z_zenoh_message_t){ ._tag = _Z_N_REQUEST, ._body._request = diff --git a/src/session/push.c b/src/session/push.c index 4c2645f41..3db6de57a 100644 --- a/src/session/push.c +++ b/src/session/push.c @@ -26,7 +26,7 @@ int8_t _z_trigger_push(_z_session_t *zn, _z_n_msg_push_t *push) { // TODO check body to know where to dispatch #if Z_FEATURE_SUBSCRIPTION == 1 - _z_slice_t payload = push->_body._is_put ? push->_body._body._put._payload : _z_slice_empty(); + _zz_bytes_t payload = push->_body._is_put ? push->_body._body._put._payload : _zz_bytes_null(); _z_encoding_t encoding = push->_body._is_put ? push->_body._body._put._encoding : _z_encoding_null(); size_t kind = push->_body._is_put ? Z_SAMPLE_KIND_PUT : Z_SAMPLE_KIND_DELETE; ret = _z_trigger_subscriptions(zn, push->_key, payload, encoding, kind, push->_timestamp, push->_qos, diff --git a/src/session/query.c b/src/session/query.c index e8dabd71c..c31f10f2f 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -114,7 +114,7 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_zint_t id, cons } // Build the reply - _z_reply_t reply = _z_reply_create(expanded_ke, Z_REPLY_TAG_DATA, zn->_local_zid, &msg->_payload, + _z_reply_t reply = _z_reply_create(expanded_ke, Z_REPLY_TAG_DATA, zn->_local_zid, msg->_payload, &msg->_commons._timestamp, msg->_encoding, Z_SAMPLE_KIND_PUT, msg->_attachment); // Verify if this is a newer reply, free the old one in case it is diff --git a/src/session/queryable.c b/src/session/queryable.c index a09afd6ab..93d85b353 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -133,7 +133,7 @@ _z_session_queryable_rc_t *_z_register_session_queryable(_z_session_t *zn, _z_se } int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *msgq, const _z_keyexpr_t q_key, uint32_t qid, - const _z_bytes_t att) { + const _zz_bytes_t attachment) { int8_t ret = _Z_RES_OK; _zp_session_lock_mutex(zn); @@ -146,7 +146,7 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *msgq, const // Build the z_query _z_query_rc_t query = _z_query_rc_new(); - query.in->val = _z_query_create(&msgq->_ext_value, &key, &msgq->_parameters, zn, qid, att); + query.in->val = _z_query_create(&msgq->_ext_value, &key, &msgq->_parameters, zn, qid, attachment); // Parse session_queryable list _z_session_queryable_rc_list_t *xs = qles; while (xs != NULL) { diff --git a/src/session/rx.c b/src/session/rx.c index 2f040a4fa..44bf0a3fa 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -111,9 +111,9 @@ int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint case _Z_REQUEST_DEL: { #if Z_FEATURE_SUBSCRIPTION == 1 _z_msg_del_t del = req._body._del; - ret = _z_trigger_subscriptions(zn, req._key, _z_slice_empty(), _z_encoding_null(), + ret = _z_trigger_subscriptions(zn, req._key, _zz_bytes_null(), _z_encoding_null(), Z_SAMPLE_KIND_DELETE, del._commons._timestamp, req._ext_qos, - _z_bytes_null()); + _zz_bytes_null()); #endif if (ret == _Z_RES_OK) { _z_network_message_t final = _z_n_msg_make_response_final(req._rid); @@ -133,7 +133,7 @@ int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint case _Z_RESPONSE_BODY_ERR: { // @TODO: expose zenoh errors to the user _z_msg_err_t error = response._body._err; - _z_slice_t payload = error._payload; + _z_slice_t payload = _zz_bytes_try_get_contiguous(&error._payload); _ZP_UNUSED(payload); // Unused when logs are deactivated _Z_ERROR("Received Err for query %zu: message=%.*s", response._request_id, (int)payload.len, payload.start); diff --git a/src/session/subscription.c b/src/session/subscription.c index ec0980feb..83062199d 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -138,17 +138,17 @@ _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_loca return ret; } -void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *payload, - _z_zint_t payload_len, const _z_n_qos_t qos, const _z_bytes_t att) { +void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _zz_bytes_t payload, + const _z_n_qos_t qos, const _zz_bytes_t attachment) { _z_encoding_t encoding = _z_encoding_null(); - int8_t ret = _z_trigger_subscriptions(zn, keyexpr, _z_slice_wrap(payload, payload_len), encoding, Z_SAMPLE_KIND_PUT, - _z_timestamp_null(), qos, att); + int8_t ret = _z_trigger_subscriptions(zn, keyexpr, payload, encoding, Z_SAMPLE_KIND_PUT, _z_timestamp_null(), qos, + attachment); (void)ret; } -int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_slice_t payload, +int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _zz_bytes_t payload, const _z_encoding_t encoding, const _z_zint_t kind, const _z_timestamp_t timestamp, - const _z_n_qos_t qos, const _z_bytes_t att) { + const _z_n_qos_t qos, const _zz_bytes_t attachment) { int8_t ret = _Z_RES_OK; _zp_session_lock_mutex(zn); @@ -163,7 +163,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co // Build the sample _z_sample_rc_t sample = _z_sample_rc_new(); - sample.in->val = _z_sample_create(&key, &payload, timestamp, encoding, kind, qos, att); + sample.in->val = _z_sample_create(&key, payload, timestamp, encoding, kind, qos, attachment); // Parse subscription list _z_subscription_rc_list_t *xs = subs; _Z_DEBUG("Triggering %ju subs", (uintmax_t)_z_subscription_rc_list_len(xs)); @@ -208,14 +208,14 @@ void _z_flush_subscriptions(_z_session_t *zn) { } #else // Z_FEATURE_SUBSCRIPTION == 0 -void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *payload, - _z_zint_t payload_len, _z_n_qos_t qos, const _z_bytes_t att) { +void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _zz_bytes_t attachment, + _z_n_qos_t qos, const _zz_bytes_t attachment) { _ZP_UNUSED(zn); _ZP_UNUSED(keyexpr); _ZP_UNUSED(payload); _ZP_UNUSED(payload_len); _ZP_UNUSED(qos); - _ZP_UNUSED(att); + _ZP_UNUSED(attachment); } #endif // Z_FEATURE_SUBSCRIPTION == 1 diff --git a/tests/z_bytes_test.c b/tests/z_bytes_test.c index 874bb8ab1..eeddbc5b0 100644 --- a/tests/z_bytes_test.c +++ b/tests/z_bytes_test.c @@ -27,14 +27,15 @@ void test_null_bytes(void) { assert(_zz_bytes_is_empty(&b)); assert(!_zz_bytes_check(&b)); assert(_zz_bytes_num_slices(&b) == 0); - _zz_bytes_drop(&b); // no crush + _zz_bytes_drop(&b); // no crush } void test_slice(void) { uint8_t data[5] = {1, 2, 3, 4, 5}; uint8_t data_out[5] = {0}; _z_slice_t s = _z_slice_wrap_copy(data, 5); - _zz_bytes_t b = _zz_bytes_from_slice(s); + _zz_bytes_t b; + _zz_bytes_from_slice(&b, s); assert(_zz_bytes_len(&b) == 5); assert(!_zz_bytes_is_empty(&b)); @@ -44,7 +45,7 @@ void test_slice(void) { _zz_bytes_to_buf(&b, data_out, 5); assert(memcmp(data, data_out, 5) == 0); - + _zz_bytes_drop(&b); } @@ -59,7 +60,7 @@ void test_append(void) { _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); _zz_bytes_t b = _zz_bytes_null(); - + _zz_bytes_append_slice(&b, &s1); _zz_bytes_append_slice(&b, &s2); _zz_bytes_append_slice(&b, &s3); @@ -74,7 +75,7 @@ void test_append(void) { _zz_bytes_to_buf(&b, data_out, 10); assert(memcmp(data_in, data_out, 10) == 0); - + _zz_bytes_drop(&b); } @@ -89,7 +90,7 @@ void test_reader_read(void) { _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); _zz_bytes_t b = _zz_bytes_null(); - + _zz_bytes_append_slice(&b, &s1); _zz_bytes_append_slice(&b, &s2); _zz_bytes_append_slice(&b, &s3); @@ -122,7 +123,7 @@ void test_reader_seek(void) { _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); _zz_bytes_t b = _zz_bytes_null(); - + _zz_bytes_append_slice(&b, &s1); _zz_bytes_append_slice(&b, &s2); _zz_bytes_append_slice(&b, &s3); @@ -146,7 +147,6 @@ void test_reader_seek(void) { assert(_zz_bytes_reader_seek(&reader, 20, SEEK_SET) != 0); assert(_zz_bytes_reader_seek(&reader, -20, SEEK_SET) != 0); - assert(_zz_bytes_reader_seek(&reader, 0, SEEK_END) == 0); assert(_zz_bytes_reader_tell(&reader) == 10); assert(_zz_bytes_reader_seek(&reader, -3, SEEK_END) == 0); diff --git a/tests/z_channels_test.c b/tests/z_channels_test.c index ef123ad0d..5bfef17a9 100644 --- a/tests/z_channels_test.c +++ b/tests/z_channels_test.c @@ -23,16 +23,18 @@ #undef NDEBUG #include -#define SEND(closure, v) \ - do { \ - _z_sample_t s = {.keyexpr = _z_rname("key"), \ - .payload = {._slice = {.start = (const uint8_t *)v, .len = strlen(v)}}, \ - .timestamp = _z_timestamp_null(), \ - .encoding = _z_encoding_null(), \ - .kind = 0, \ - .qos = {0}}; \ - z_loaned_sample_t sample = _z_sample_rc_new_from_val(s); \ - z_call(closure, &sample); \ +#define SEND(closure, v) \ + do { \ + _zz_bytes_t payload; \ + _zz_bytes_from_slice(&payload, (_z_slice_t){.start = (const uint8_t *)v, .len = strlen(v)}); \ + _z_sample_t s = {.keyexpr = _z_rname("key"), \ + .payload = payload, \ + .timestamp = _z_timestamp_null(), \ + .encoding = _z_encoding_null(), \ + .kind = 0, \ + .qos = {0}}; \ + z_loaned_sample_t sample = _z_sample_rc_new_from_val(s); \ + z_call(closure, &sample); \ } while (0); #define _RECV(handler, method, buf) \ diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index ab7a421e7..9229783b0 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -170,16 +170,6 @@ _z_wbuf_t gen_wbuf(size_t len) { return _z_wbuf_make(len, is_expandable); } -_z_slice_t gen_payload(size_t len) { - _z_slice_t pld; - pld._is_alloc = true; - pld.len = len; - pld.start = (uint8_t *)z_malloc(len); - z_random_fill((uint8_t *)pld.start, pld.len); - - return pld; -} - _z_slice_t gen_slice(size_t len) { _z_slice_t arr; arr._is_alloc = true; @@ -195,10 +185,19 @@ _z_slice_t gen_slice(size_t len) { return arr; } -_z_bytes_t gen_bytes(size_t len) { - return (_z_bytes_t){ - ._slice = gen_slice(len), - }; +_zz_bytes_t gen_payload(size_t len) { + _z_slice_t pld = gen_slice(len); + _zz_bytes_t b; + _zz_bytes_from_slice(&b, pld); + + return b; +} + +_zz_bytes_t gen_bytes(size_t len) { + _z_slice_t s = gen_slice(len); + _zz_bytes_t b; + _zz_bytes_from_slice(&b, s); + return b; } _z_id_t gen_zid(void) { @@ -261,7 +260,7 @@ _z_value_t gen_value(void) { _z_value_t val; val.encoding = gen_encoding(); if (gen_bool()) { - val.payload = _z_bytes_null(); + val.payload = _zz_bytes_null(); } else { val.payload = gen_bytes(16); } @@ -508,8 +507,27 @@ void zbuf_extension(void) { /*------------------ Payload field ------------------*/ void assert_eq_slice(const _z_slice_t *left, const _z_slice_t *right) { assert_eq_uint8_array(left, right); } -void assert_eq_bytes(const _z_bytes_t *left, const _z_bytes_t *right) { - assert_eq_slice(&left->_slice, &right->_slice); +void assert_eq_bytes(const _zz_bytes_t *left, const _zz_bytes_t *right) { + size_t len_left = _zz_bytes_len(left); + size_t len_right = _zz_bytes_len(right); + printf("Array -> "); + printf("Length (%zu:%zu), ", len_left, len_right); + + assert(len_left == len_right); + printf("Content ("); + _zz_bytes_reader_t reader_left = _zz_bytes_get_reader(left); + _zz_bytes_reader_t reader_right = _zz_bytes_get_reader(right); + for (size_t i = 0; i < len_left; i++) { + uint8_t l = 0, r = 0; + _zz_bytes_reader_read(&reader_left, &l, 1); + _zz_bytes_reader_read(&reader_right, &r, 1); + + printf("%02x:%02x", l, r); + if (i < len_left - 1) printf(" "); + + assert(l == r); + } + printf(")"); } void payload_field(void) { @@ -517,26 +535,26 @@ void payload_field(void) { _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); // Initialize - _z_slice_t e_pld = gen_payload(64); + _zz_bytes_t e_pld = gen_payload(64); // Encode - int8_t res = _z_slice_encode(&wbf, &e_pld); + int8_t res = _zz_bytes_encode(&wbf, &e_pld); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_slice_t d_pld; - res = _z_slice_decode(&d_pld, &zbf); + _zz_bytes_t d_pld; + res = _zz_bytes_decode(&d_pld, &zbf); assert(res == _Z_RES_OK); printf(" "); - assert_eq_slice(&e_pld, &d_pld); + assert_eq_bytes(&e_pld, &d_pld); printf("\n"); // Free - _z_slice_clear(&e_pld); - _z_slice_clear(&d_pld); + _zz_bytes_drop(&e_pld); + _zz_bytes_drop(&d_pld); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } @@ -1153,7 +1171,7 @@ _z_push_body_t gen_push_body(void) { return (_z_push_body_t){._is_put = true, ._body._put = { ._commons = commons, - ._payload = gen_slice(64), + ._payload = gen_bytes(64), ._encoding = gen_encoding(), }}; } else { @@ -1164,7 +1182,7 @@ _z_push_body_t gen_push_body(void) { void assert_eq_push_body(const _z_push_body_t *left, const _z_push_body_t *right) { assert(left->_is_put == right->_is_put); if (left->_is_put) { - assert_eq_slice(&left->_body._put._payload, &right->_body._put._payload); + assert_eq_bytes(&left->_body._put._payload, &right->_body._put._payload); assert_eq_encoding(&left->_body._put._encoding, &right->_body._put._encoding); assert_eq_timestamp(&left->_body._put._commons._timestamp, &right->_body._put._commons._timestamp); assert_eq_source_info(&left->_body._put._commons._source_info, &right->_body._put._commons._source_info); @@ -1247,7 +1265,7 @@ _z_msg_err_t gen_err(void) { void assert_eq_err(const _z_msg_err_t *left, const _z_msg_err_t *right) { assert_eq_encoding(&left->encoding, &right->encoding); assert_eq_source_info(&left->_ext_source_info, &right->_ext_source_info); - assert_eq_slice(&left->_payload, &right->_payload); + assert_eq_bytes(&left->_payload, &right->_payload); } void err_message(void) { From 38cbee065106a717ed6cb35b5b50c5ddd0f099bc Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Mon, 24 Jun 2024 18:12:16 +0200 Subject: [PATCH 06/25] rename zz_bytes -> z_bytes --- include/zenoh-pico/api/types.h | 6 +- include/zenoh-pico/collections/bytes.h | 90 ++++---- include/zenoh-pico/net/primitives.h | 8 +- include/zenoh-pico/net/query.h | 4 +- include/zenoh-pico/net/reply.h | 4 +- include/zenoh-pico/net/sample.h | 8 +- include/zenoh-pico/protocol/codec/core.h | 4 +- include/zenoh-pico/protocol/core.h | 4 +- .../zenoh-pico/protocol/definitions/message.h | 8 +- .../zenoh-pico/protocol/definitions/network.h | 2 +- include/zenoh-pico/session/queryable.h | 2 +- include/zenoh-pico/session/subscription.h | 8 +- src/api/api.c | 92 ++++---- src/collections/bytes.c | 214 +++++++++--------- src/net/memory.c | 2 +- src/net/primitives.c | 8 +- src/net/query.c | 4 +- src/net/reply.c | 8 +- src/net/sample.c | 24 +- src/protocol/codec.c | 22 +- src/protocol/codec/message.c | 20 +- src/protocol/core.c | 4 +- src/protocol/definitions/message.c | 8 +- src/protocol/definitions/network.c | 2 +- src/session/push.c | 2 +- src/session/queryable.c | 2 +- src/session/rx.c | 6 +- src/session/subscription.c | 12 +- tests/z_bytes_test.c | 156 ++++++------- tests/z_channels_test.c | 4 +- tests/z_msgcodec_test.c | 40 ++-- 31 files changed, 389 insertions(+), 389 deletions(-) diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 342d424d9..71e918f59 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -72,13 +72,13 @@ _Z_LOANED_TYPE(_z_slice_t, slice) * Members: * _z_slice_t slice: content of the container. */ -_Z_OWNED_TYPE_PTR(_zz_bytes_t, bytes) -_Z_LOANED_TYPE(_zz_bytes_t, bytes) +_Z_OWNED_TYPE_PTR(_z_bytes_t, bytes) +_Z_LOANED_TYPE(_z_bytes_t, bytes) /** * An iterator over multi-element serialized data */ -typedef _zz_bytes_iterator_t z_bytes_iterator_t; +typedef _z_bytes_iterator_t z_bytes_iterator_t; /** * Represents a string without null-terminator. diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index cb4184f31..878396d82 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -43,60 +43,60 @@ _Z_SVEC_DEFINE(_z_arc_slice, _z_arc_slice_t) typedef struct { _z_arc_slice_svec_t _slices; -} _zz_bytes_t; +} _z_bytes_t; -_Bool _zz_bytes_check(const _zz_bytes_t *bytes); -_zz_bytes_t _zz_bytes_null(void); -int8_t _zz_bytes_append(_zz_bytes_t *dst, _zz_bytes_t *src); -int8_t _zz_bytes_append_slice(_zz_bytes_t *dst, _z_arc_slice_t *s); -int8_t _zz_bytes_copy(_zz_bytes_t *dst, const _zz_bytes_t *src); -_zz_bytes_t _zz_bytes_duplicate(const _zz_bytes_t *src); -void _zz_bytes_move(_zz_bytes_t *dst, _zz_bytes_t *src); -void _zz_bytes_drop(_zz_bytes_t *bytes); -void _zz_bytes_free(_zz_bytes_t **bs); -size_t _zz_bytes_num_slices(const _zz_bytes_t *bs); -_z_arc_slice_t *_zz_bytes_get_slice(const _zz_bytes_t *bs, size_t i); -size_t _zz_bytes_len(const _zz_bytes_t *bs); -_Bool _zz_bytes_is_empty(const _zz_bytes_t *bs); -int8_t _zz_bytes_to_uint8(const _zz_bytes_t *bs, uint8_t *u); -int8_t _zz_bytes_to_uint16(const _zz_bytes_t *bs, uint16_t *u); -int8_t _zz_bytes_to_uint32(const _zz_bytes_t *bs, uint32_t *u); -int8_t _zz_bytes_to_uint64(const _zz_bytes_t *bs, uint64_t *u); -int8_t _zz_bytes_to_float(const _zz_bytes_t *bs, float *f); -int8_t _zz_bytes_to_double(const _zz_bytes_t *bs, double *d); -int8_t _zz_bytes_to_slice(const _zz_bytes_t *bytes, _z_slice_t *s); -int8_t _zz_bytes_from_slice(_zz_bytes_t *b, _z_slice_t s); -int8_t _zz_bytes_from_uint8(_zz_bytes_t *b, uint8_t val); -int8_t _zz_bytes_from_uint16(_zz_bytes_t *b, uint16_t val); -int8_t _zz_bytes_from_uint32(_zz_bytes_t *b, uint32_t val); -int8_t _zz_bytes_from_uint64(_zz_bytes_t *b, uint64_t val); -int8_t _zz_bytes_from_float(_zz_bytes_t *b, float val); -int8_t _zz_bytes_from_double(_zz_bytes_t *b, double val); -size_t _zz_bytes_to_buf(const _zz_bytes_t *bytes, uint8_t *dst, size_t len); -int8_t _zz_bytes_from_buf(_zz_bytes_t *b, uint8_t *src, size_t len); -int8_t _zz_bytes_serialize_from_pair(_zz_bytes_t *out, _zz_bytes_t *first, _zz_bytes_t *second); -int8_t _zz_bytes_deserialize_into_pair(const _zz_bytes_t *bs, _zz_bytes_t *first_out, _zz_bytes_t *second_out); -_z_slice_t _zz_bytes_try_get_contiguous(const _zz_bytes_t *bs); +_Bool _z_bytes_check(const _z_bytes_t *bytes); +_z_bytes_t _z_bytes_null(void); +int8_t _z_bytes_append(_z_bytes_t *dst, _z_bytes_t *src); +int8_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s); +int8_t _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_drop(_z_bytes_t *bytes); +void _z_bytes_free(_z_bytes_t **bs); +size_t _z_bytes_num_slices(const _z_bytes_t *bs); +_z_arc_slice_t *_z_bytes_get_slice(const _z_bytes_t *bs, size_t i); +size_t _z_bytes_len(const _z_bytes_t *bs); +_Bool _z_bytes_is_empty(const _z_bytes_t *bs); +int8_t _z_bytes_to_uint8(const _z_bytes_t *bs, uint8_t *u); +int8_t _z_bytes_to_uint16(const _z_bytes_t *bs, uint16_t *u); +int8_t _z_bytes_to_uint32(const _z_bytes_t *bs, uint32_t *u); +int8_t _z_bytes_to_uint64(const _z_bytes_t *bs, uint64_t *u); +int8_t _z_bytes_to_float(const _z_bytes_t *bs, float *f); +int8_t _z_bytes_to_double(const _z_bytes_t *bs, double *d); +int8_t _z_bytes_to_slice(const _z_bytes_t *bytes, _z_slice_t *s); +int8_t _z_bytes_from_slice(_z_bytes_t *b, _z_slice_t s); +int8_t _z_bytes_from_uint8(_z_bytes_t *b, uint8_t val); +int8_t _z_bytes_from_uint16(_z_bytes_t *b, uint16_t val); +int8_t _z_bytes_from_uint32(_z_bytes_t *b, uint32_t val); +int8_t _z_bytes_from_uint64(_z_bytes_t *b, uint64_t val); +int8_t _z_bytes_from_float(_z_bytes_t *b, float val); +int8_t _z_bytes_from_double(_z_bytes_t *b, double val); +size_t _z_bytes_to_buf(const _z_bytes_t *bytes, uint8_t *dst, size_t len); +int8_t _z_bytes_from_buf(_z_bytes_t *b, uint8_t *src, size_t len); +int8_t _z_bytes_serialize_from_pair(_z_bytes_t *out, _z_bytes_t *first, _z_bytes_t *second); +int8_t _z_bytes_deserialize_into_pair(const _z_bytes_t *bs, _z_bytes_t *first_out, _z_bytes_t *second_out); +_z_slice_t _z_bytes_try_get_contiguous(const _z_bytes_t *bs); typedef struct { size_t slice_idx; size_t in_slice_idx; size_t byte_idx; - const _zz_bytes_t *bytes; -} _zz_bytes_reader_t; + const _z_bytes_t *bytes; +} _z_bytes_reader_t; -_zz_bytes_reader_t _zz_bytes_get_reader(const _zz_bytes_t *bytes); -int8_t _zz_bytes_reader_seek(_zz_bytes_reader_t *reader, int64_t offset, int origin); -int64_t _zz_bytes_reader_tell(const _zz_bytes_reader_t *reader); -int8_t _zz_bytes_reader_read_slices(_zz_bytes_reader_t *reader, size_t len, _zz_bytes_t *out); -int8_t _zz_bytes_reader_read(_zz_bytes_reader_t *reader, uint8_t *buf, size_t len); -int8_t _zz_bytes_reader_read_next(_zz_bytes_reader_t *reader, _zz_bytes_t *out); +_z_bytes_reader_t _z_bytes_get_reader(const _z_bytes_t *bytes); +int8_t _z_bytes_reader_seek(_z_bytes_reader_t *reader, int64_t offset, int origin); +int64_t _z_bytes_reader_tell(const _z_bytes_reader_t *reader); +int8_t _z_bytes_reader_read_slices(_z_bytes_reader_t *reader, size_t len, _z_bytes_t *out); +int8_t _z_bytes_reader_read(_z_bytes_reader_t *reader, uint8_t *buf, size_t len); +int8_t _z_bytes_reader_read_next(_z_bytes_reader_t *reader, _z_bytes_t *out); typedef struct { - _zz_bytes_reader_t _reader; -} _zz_bytes_iterator_t; + _z_bytes_reader_t _reader; +} _z_bytes_iterator_t; -_zz_bytes_iterator_t _zz_bytes_get_iterator(const _zz_bytes_t *bytes); -_Bool _zz_bytes_iterator_next(_zz_bytes_iterator_t *iter, _zz_bytes_t *b); +_z_bytes_iterator_t _z_bytes_get_iterator(const _z_bytes_t *bytes); +_Bool _z_bytes_iterator_next(_z_bytes_iterator_t *iter, _z_bytes_t *b); #endif /* ZENOH_PICO_COLLECTIONS_BYTES_H */ diff --git a/include/zenoh-pico/net/primitives.h b/include/zenoh-pico/net/primitives.h index 6aa07e93b..46342bac8 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -118,9 +118,9 @@ int8_t _z_undeclare_publisher(_z_publisher_t *pub); * Returns: * ``0`` in case of success, ``-1`` in case of failure. */ -int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, _zz_bytes_t payload, const _z_encoding_t encoding, +int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, _z_bytes_t payload, const _z_encoding_t encoding, const z_sample_kind_t kind, const z_congestion_control_t cong_ctrl, z_priority_t priority, - const _zz_bytes_t attachment); + const _z_bytes_t attachment); #endif #if Z_FEATURE_SUBSCRIPTION == 1 @@ -199,7 +199,7 @@ int8_t _z_undeclare_queryable(_z_queryable_t *qle); * attachment: An optional attachment to the reply. */ int8_t _z_send_reply(const _z_query_t *query, const _z_keyexpr_t keyexpr, const _z_value_t payload, - const z_sample_kind_t kind, const _zz_bytes_t attachment); + const z_sample_kind_t kind, const _z_bytes_t attachment); #endif #if Z_FEATURE_QUERY == 1 @@ -224,7 +224,7 @@ int8_t _z_send_reply(const _z_query_t *query, const _z_keyexpr_t keyexpr, const */ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, const z_query_target_t target, const z_consolidation_mode_t consolidation, const _z_value_t value, _z_reply_handler_t callback, - _z_drop_handler_t dropper, void *arg, uint32_t timeout_ms, const _zz_bytes_t attachment); + _z_drop_handler_t dropper, void *arg, uint32_t timeout_ms, const _z_bytes_t attachment); #endif #if Z_FEATURE_INTEREST == 1 diff --git a/include/zenoh-pico/net/query.h b/include/zenoh-pico/net/query.h index 90cd3b116..e34d587a2 100644 --- a/include/zenoh-pico/net/query.h +++ b/include/zenoh-pico/net/query.h @@ -29,7 +29,7 @@ typedef struct _z_query_t { _z_keyexpr_t _key; uint32_t _request_id; _z_session_t *_zn; - _zz_bytes_t attachment; + _z_bytes_t attachment; char *_parameters; _Bool _anyke; } _z_query_t; @@ -48,7 +48,7 @@ typedef struct { #if Z_FEATURE_QUERYABLE == 1 _z_query_t _z_query_create(const _z_value_t *value, const _z_keyexpr_t *key, const _z_slice_t *parameters, - _z_session_t *zn, uint32_t request_id, const _zz_bytes_t attachment); + _z_session_t *zn, uint32_t request_id, const _z_bytes_t attachment); void _z_queryable_clear(_z_queryable_t *qbl); void _z_queryable_free(_z_queryable_t **qbl); #endif diff --git a/include/zenoh-pico/net/reply.h b/include/zenoh-pico/net/reply.h index 438a7f19d..3a14a58a3 100644 --- a/include/zenoh-pico/net/reply.h +++ b/include/zenoh-pico/net/reply.h @@ -61,9 +61,9 @@ _z_reply_t _z_reply_null(void); void _z_reply_clear(_z_reply_t *src); void _z_reply_free(_z_reply_t **hello); void _z_reply_copy(_z_reply_t *dst, _z_reply_t *src); -_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _zz_bytes_t payload, +_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_bytes_t payload, const _z_timestamp_t *timestamp, _z_encoding_t encoding, z_sample_kind_t kind, - const _zz_bytes_t attachment); + const _z_bytes_t attachment); _Z_REFCOUNT_DEFINE(_z_reply, _z_reply) diff --git a/include/zenoh-pico/net/sample.h b/include/zenoh-pico/net/sample.h index a410b71f5..969e91215 100644 --- a/include/zenoh-pico/net/sample.h +++ b/include/zenoh-pico/net/sample.h @@ -30,12 +30,12 @@ */ typedef struct _z_sample_t { _z_keyexpr_t keyexpr; - _zz_bytes_t payload; + _z_bytes_t payload; _z_timestamp_t timestamp; _z_encoding_t encoding; z_sample_kind_t kind; _z_qos_t qos; - _zz_bytes_t attachment; + _z_bytes_t attachment; } _z_sample_t; void _z_sample_clear(_z_sample_t *sample); @@ -56,8 +56,8 @@ void _z_sample_free(_z_sample_t **sample); void _z_sample_copy(_z_sample_t *dst, const _z_sample_t *src); _z_sample_t _z_sample_duplicate(const _z_sample_t *src); -_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _zz_bytes_t, _z_timestamp_t timestamp, +_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes_t, _z_timestamp_t timestamp, const _z_encoding_t encoding, const z_sample_kind_t kind, const _z_qos_t qos, - const _zz_bytes_t attachment); + const _z_bytes_t attachment); #endif /* ZENOH_PICO_SAMPLE_NETAPI_H */ diff --git a/include/zenoh-pico/protocol/codec/core.h b/include/zenoh-pico/protocol/codec/core.h index a32270244..59ccadc0d 100644 --- a/include/zenoh-pico/protocol/codec/core.h +++ b/include/zenoh-pico/protocol/codec/core.h @@ -76,8 +76,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 _zz_bytes_decode(_zz_bytes_t *bs, _z_zbuf_t *zbf); -int8_t _zz_bytes_encode(_z_wbuf_t *wbf, const _zz_bytes_t *bs); +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); diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index de841bd58..b24c933c4 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -158,11 +158,11 @@ typedef struct { * Represents a Zenoh value. * * Members: - * _zz_bytes_t payload: The payload of this zenoh value. + * _z_bytes_t payload: The payload of this zenoh value. * _z_encoding_t encoding: The encoding of the `payload`. */ typedef struct { - _zz_bytes_t payload; + _z_bytes_t payload; _z_encoding_t encoding; } _z_value_t; _z_value_t _z_value_null(void); diff --git a/include/zenoh-pico/protocol/definitions/message.h b/include/zenoh-pico/protocol/definitions/message.h index d7a148da5..2dec35a53 100644 --- a/include/zenoh-pico/protocol/definitions/message.h +++ b/include/zenoh-pico/protocol/definitions/message.h @@ -55,7 +55,7 @@ typedef struct { _z_encoding_t encoding; _z_source_info_t _ext_source_info; - _zz_bytes_t _payload; + _z_bytes_t _payload; } _z_msg_err_t; void _z_msg_err_clear(_z_msg_err_t *err); @@ -73,9 +73,9 @@ static inline void _z_msg_del_clear(_z_msg_del_t *del) { (void)del; } typedef struct { _z_m_push_commons_t _commons; - _zz_bytes_t _payload; + _z_bytes_t _payload; _z_encoding_t _encoding; - _zz_bytes_t _attachment; + _z_bytes_t _attachment; } _z_msg_put_t; void _z_msg_put_clear(_z_msg_put_t *); #define _Z_M_PUT_ID 0x01 @@ -100,7 +100,7 @@ typedef struct { _z_source_info_t _ext_info; _z_value_t _ext_value; z_consolidation_mode_t _consolidation; - _zz_bytes_t _ext_attachment; + _z_bytes_t _ext_attachment; } _z_msg_query_t; typedef struct { _Bool info; diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index 6fec4e7d3..1940a4660 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -293,7 +293,7 @@ _Z_VEC_DEFINE(_z_network_message, _z_network_message_t) void _z_msg_fix_mapping(_z_zenoh_message_t *msg, uint16_t mapping); _z_network_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_slice_t) parameters, _z_zint_t qid, z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value, - uint32_t timeout_ms, _zz_bytes_t attachment); + uint32_t timeout_ms, _z_bytes_t attachment); _z_network_message_t _z_n_msg_make_reply(_z_zint_t rid, _Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_push_body_t) body); _z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid); _z_network_message_t _z_n_msg_make_declare(_z_declaration_t declaration, _Bool has_interest_id, uint32_t interest_id); diff --git a/include/zenoh-pico/session/queryable.h b/include/zenoh-pico/session/queryable.h index e7692b647..78c5f3cad 100644 --- a/include/zenoh-pico/session/queryable.h +++ b/include/zenoh-pico/session/queryable.h @@ -29,7 +29,7 @@ _z_session_queryable_rc_list_t *_z_get_session_queryable_by_key(_z_session_t *zn _z_session_queryable_rc_t *_z_register_session_queryable(_z_session_t *zn, _z_session_queryable_t *q); int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, const _z_keyexpr_t q_key, uint32_t qid, - const _zz_bytes_t attachment); + const _z_bytes_t attachment); void _z_unregister_session_queryable(_z_session_t *zn, _z_session_queryable_rc_t *q); void _z_flush_session_queryable(_z_session_t *zn); #endif diff --git a/include/zenoh-pico/session/subscription.h b/include/zenoh-pico/session/subscription.h index 2529110cc..03e0535d9 100644 --- a/include/zenoh-pico/session/subscription.h +++ b/include/zenoh-pico/session/subscription.h @@ -19,17 +19,17 @@ #include "zenoh-pico/net/session.h" /*------------------ Subscription ------------------*/ -void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _zz_bytes_t payload, - const _z_n_qos_t qos, const _zz_bytes_t attachment); +void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_bytes_t payload, + const _z_n_qos_t qos, const _z_bytes_t attachment); #if Z_FEATURE_SUBSCRIPTION == 1 _z_subscription_rc_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id); _z_subscription_rc_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr); _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *sub); -int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _zz_bytes_t payload, +int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_bytes_t payload, const _z_encoding_t encoding, const _z_zint_t kind, const _z_timestamp_t timestamp, - const _z_n_qos_t qos, const _zz_bytes_t attachment); + const _z_n_qos_t qos, const _z_bytes_t attachment); void _z_unregister_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_rc_t *sub); void _z_flush_subscriptions(_z_session_t *zn); #endif diff --git a/src/api/api.c b/src/api/api.c index c285627f3..4f18e2656 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -263,43 +263,43 @@ 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_bytes_deserialize_into_int8(const z_loaned_bytes_t *bytes, int8_t *dst) { - return _zz_bytes_to_uint8(bytes, (uint8_t *)dst); + return _z_bytes_to_uint8(bytes, (uint8_t *)dst); } int8_t z_bytes_deserialize_into_int16(const z_loaned_bytes_t *bytes, int16_t *dst) { - return _zz_bytes_to_uint16(bytes, (uint16_t *)dst); + return _z_bytes_to_uint16(bytes, (uint16_t *)dst); } int8_t z_bytes_deserialize_into_int32(const z_loaned_bytes_t *bytes, int32_t *dst) { - return _zz_bytes_to_uint32(bytes, (uint32_t *)dst); + return _z_bytes_to_uint32(bytes, (uint32_t *)dst); } int8_t z_bytes_deserialize_into_int64(const z_loaned_bytes_t *bytes, int64_t *dst) { - return _zz_bytes_to_uint64(bytes, (uint64_t *)dst); + return _z_bytes_to_uint64(bytes, (uint64_t *)dst); } int8_t z_bytes_deserialize_into_uint8(const z_loaned_bytes_t *bytes, uint8_t *dst) { - return _zz_bytes_to_uint8(bytes, dst); + return _z_bytes_to_uint8(bytes, dst); } int8_t z_bytes_deserialize_into_uint16(const z_loaned_bytes_t *bytes, uint16_t *dst) { - return _zz_bytes_to_uint16(bytes, dst); + return _z_bytes_to_uint16(bytes, dst); } int8_t z_bytes_deserialize_into_uint32(const z_loaned_bytes_t *bytes, uint32_t *dst) { - return _zz_bytes_to_uint32(bytes, dst); + return _z_bytes_to_uint32(bytes, dst); } int8_t z_bytes_deserialize_into_uint64(const z_loaned_bytes_t *bytes, uint64_t *dst) { - return _zz_bytes_to_uint64(bytes, dst); + return _z_bytes_to_uint64(bytes, dst); } int8_t z_bytes_deserialize_into_float(const z_loaned_bytes_t *bytes, float *dst) { - return _zz_bytes_to_float(bytes, dst); + return _z_bytes_to_float(bytes, dst); } int8_t z_bytes_deserialize_into_double(const z_loaned_bytes_t *bytes, double *dst) { - return _zz_bytes_to_double(bytes, dst); + return _z_bytes_to_double(bytes, dst); } int8_t z_bytes_deserialize_into_slice(const z_loaned_bytes_t *bytes, z_owned_slice_t *dst) { @@ -310,7 +310,7 @@ int8_t z_bytes_deserialize_into_slice(const z_loaned_bytes_t *bytes, z_owned_sli return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Convert bytes to slice - return _zz_bytes_to_slice(bytes, dst->_val); + return _z_bytes_to_slice(bytes, dst->_val); } int8_t z_bytes_deserialize_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *s) { @@ -321,10 +321,10 @@ int8_t z_bytes_deserialize_into_string(const z_loaned_bytes_t *bytes, z_owned_st return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Convert bytes to string - size_t len = _zz_bytes_len(bytes); - *s->_val = _z_string_preallocate(_zz_bytes_len(bytes)); + size_t len = _z_bytes_len(bytes); + *s->_val = _z_string_preallocate(_z_bytes_len(bytes)); if (s->_val->len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - _zz_bytes_to_buf(bytes, (uint8_t *)s->_val->val, len); + _z_bytes_to_buf(bytes, (uint8_t *)s->_val->val, len); return _Z_RES_OK; } @@ -332,12 +332,12 @@ int8_t z_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_byte // Init pair of owned bytes z_bytes_null(first); z_bytes_null(second); - first->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); - second->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); + first->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + second->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); if ((first->_val == NULL) || (second->_val == NULL)) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - return _zz_bytes_deserialize_into_pair(bytes, first->_val, second->_val); + return _z_bytes_deserialize_into_pair(bytes, first->_val, second->_val); } int8_t z_bytes_serialize_from_int8(z_owned_bytes_t *bytes, int8_t val) { @@ -359,12 +359,12 @@ int8_t z_bytes_serialize_from_int64(z_owned_bytes_t *bytes, int64_t val) { int8_t z_bytes_serialize_from_uint8(z_owned_bytes_t *bytes, uint8_t val) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - int8_t res = _zz_bytes_from_uint8(bytes->_val, val); + int8_t res = _z_bytes_from_uint8(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); return res; } @@ -372,12 +372,12 @@ int8_t z_bytes_serialize_from_uint8(z_owned_bytes_t *bytes, uint8_t val) { int8_t z_bytes_serialize_from_uint16(z_owned_bytes_t *bytes, uint16_t val) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - int8_t res = _zz_bytes_from_uint16(bytes->_val, val); + int8_t res = _z_bytes_from_uint16(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); return res; } @@ -385,12 +385,12 @@ int8_t z_bytes_serialize_from_uint16(z_owned_bytes_t *bytes, uint16_t val) { int8_t z_bytes_serialize_from_uint32(z_owned_bytes_t *bytes, uint32_t val) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - int8_t res = _zz_bytes_from_uint32(bytes->_val, val); + int8_t res = _z_bytes_from_uint32(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); return res; } @@ -398,12 +398,12 @@ int8_t z_bytes_serialize_from_uint32(z_owned_bytes_t *bytes, uint32_t val) { int8_t z_bytes_serialize_from_uint64(z_owned_bytes_t *bytes, uint64_t val) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - int8_t res = _zz_bytes_from_uint64(bytes->_val, val); + int8_t res = _z_bytes_from_uint64(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); return res; } @@ -411,12 +411,12 @@ int8_t z_bytes_serialize_from_uint64(z_owned_bytes_t *bytes, uint64_t val) { int8_t z_bytes_serialize_from_float(z_owned_bytes_t *bytes, float val) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - int8_t res = _zz_bytes_from_float(bytes->_val, val); + int8_t res = _z_bytes_from_float(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); return res; } @@ -424,12 +424,12 @@ int8_t z_bytes_serialize_from_float(z_owned_bytes_t *bytes, float val) { int8_t z_bytes_serialize_from_double(z_owned_bytes_t *bytes, double val) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data - int8_t res = _zz_bytes_from_double(bytes->_val, val); + int8_t res = _z_bytes_from_double(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); return res; } @@ -437,13 +437,13 @@ int8_t z_bytes_serialize_from_double(z_owned_bytes_t *bytes, double val) { int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Encode data _z_slice_t s = _z_slice_wrap((uint8_t *)data, len); - int8_t res = _zz_bytes_from_slice(bytes->_val, s); + int8_t res = _z_bytes_from_slice(bytes->_val, s); if (res != _Z_RES_OK) z_bytes_drop(bytes); return res; } @@ -451,7 +451,7 @@ int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, int8_t z_bytes_serialize_from_slice_copy(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } @@ -462,7 +462,7 @@ int8_t z_bytes_serialize_from_slice_copy(z_owned_bytes_t *bytes, const uint8_t * } // Copy data memcpy((uint8_t *)s.start, data, len); - int8_t res = _zz_bytes_from_slice(bytes->_val, s); + int8_t res = _z_bytes_from_slice(bytes->_val, s); if (res != _Z_RES_OK) z_bytes_drop(bytes); return res; } @@ -483,13 +483,13 @@ int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body void *context) { // Init owned bytes z_bytes_null(bytes); - bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); if (bytes->_val == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } z_owned_bytes_t data; while (iterator_body(&data, context)) { - int8_t res = _zz_bytes_append(bytes->_val, data._val); + int8_t res = _z_bytes_append(bytes->_val, data._val); if (res != _Z_RES_OK) { z_bytes_drop(bytes); return res; @@ -500,26 +500,26 @@ int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second) { z_bytes_null(bytes); - bytes->_val = (_zz_bytes_t *)z_malloc(sizeof(_zz_bytes_t)); + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); if (bytes->_val == NULL) { z_bytes_drop(first); z_bytes_drop(second); return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - int8_t res = _zz_bytes_serialize_from_pair(bytes->_val, first->_val, second->_val); + int8_t res = _z_bytes_serialize_from_pair(bytes->_val, first->_val, second->_val); first->_val = NULL; second->_val = NULL; if (res != _Z_RES_OK) z_bytes_drop(bytes); return res; } -z_bytes_iterator_t z_bytes_get_iterator(const z_loaned_bytes_t *bytes) { return _zz_bytes_get_iterator(bytes); } +z_bytes_iterator_t z_bytes_get_iterator(const z_loaned_bytes_t *bytes) { return _z_bytes_get_iterator(bytes); } _Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *bytes) { z_bytes_null(bytes); bytes->_val = (z_loaned_bytes_t *)z_malloc(sizeof(z_loaned_bytes_t)); if (bytes->_val == NULL) return false; - if (!_zz_bytes_iterator_next(iter, bytes->_val)) { + if (!_z_bytes_iterator_next(iter, bytes->_val)) { z_bytes_drop(bytes); return false; } @@ -626,15 +626,15 @@ _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_hello_t, hello, _z_owner_noop_copy, _z_hello_free _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_string_vec_t, string_array, _z_owner_noop_copy, _z_string_vec_free) _Z_VIEW_FUNCTIONS_PTR_IMPL(_z_string_vec_t, string_array) _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_slice_t, slice, _z_slice_copy, _z_slice_free) -_Z_OWNED_FUNCTIONS_PTR_IMPL(_zz_bytes_t, bytes, _zz_bytes_copy, _zz_bytes_free) +_Z_OWNED_FUNCTIONS_PTR_IMPL(_z_bytes_t, bytes, _z_bytes_copy, _z_bytes_free) #if Z_FEATURE_PUBLICATION == 1 || Z_FEATURE_QUERYABLE == 1 || Z_FEATURE_QUERY == 1 // Convert a user owned bytes payload to an internal bytes payload, returning an empty one if value invalid -static _zz_bytes_t _z_bytes_from_owned_bytes(z_owned_bytes_t *bytes) { +static _z_bytes_t _z_bytes_from_owned_bytes(z_owned_bytes_t *bytes) { if ((bytes != NULL) && (bytes->_val != NULL)) { return *bytes->_val; } else { - return _zz_bytes_null(); + return _z_bytes_null(); } } @@ -873,8 +873,8 @@ int8_t z_delete(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, opt.congestion_control = options->congestion_control; opt.priority = options->priority; } - ret = _z_write(&_Z_RC_IN_VAL(zs), *keyexpr, _zz_bytes_null(), _z_encoding_null(), Z_SAMPLE_KIND_DELETE, - opt.congestion_control, opt.priority, _zz_bytes_null()); + ret = _z_write(&_Z_RC_IN_VAL(zs), *keyexpr, _z_bytes_null(), _z_encoding_null(), Z_SAMPLE_KIND_DELETE, + opt.congestion_control, opt.priority, _z_bytes_null()); return ret; } @@ -964,8 +964,8 @@ int8_t z_publisher_put(const z_loaned_publisher_t *pub, z_owned_bytes_t *payload int8_t z_publisher_delete(const z_loaned_publisher_t *pub, const z_publisher_delete_options_t *options) { (void)(options); - return _z_write(&pub->_zn.in->val, pub->_key, _zz_bytes_null(), _z_encoding_null(), Z_SAMPLE_KIND_DELETE, - pub->_congestion_control, pub->_priority, _zz_bytes_null()); + return _z_write(&pub->_zn.in->val, pub->_key, _z_bytes_null(), _z_encoding_null(), Z_SAMPLE_KIND_DELETE, + pub->_congestion_control, pub->_priority, _z_bytes_null()); } z_owned_keyexpr_t z_publisher_keyexpr(z_loaned_publisher_t *publisher) { diff --git a/src/collections/bytes.c b/src/collections/bytes.c index 5bf39691a..8bdc3fa96 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -23,15 +23,15 @@ #include "zenoh-pico/utils/result.h" /*-------- Bytes --------*/ -_Bool _zz_bytes_check(const _zz_bytes_t *bytes) { return !_zz_bytes_is_empty(bytes); } +_Bool _z_bytes_check(const _z_bytes_t *bytes) { return !_z_bytes_is_empty(bytes); } -_zz_bytes_t _zz_bytes_null(void) { - _zz_bytes_t b; +_z_bytes_t _z_bytes_null(void) { + _z_bytes_t b; b._slices = _z_arc_slice_svec_make(0); return b; } -int8_t _zz_bytes_copy(_zz_bytes_t *dst, const _zz_bytes_t *src) { +int8_t _z_bytes_copy(_z_bytes_t *dst, const _z_bytes_t *src) { _z_arc_slice_svec_copy(&dst->_slices, &src->_slices); if (_z_arc_slice_svec_len(&dst->_slices) == _z_arc_slice_svec_len(&dst->_slices)) { return _Z_RES_OK; @@ -40,13 +40,13 @@ int8_t _zz_bytes_copy(_zz_bytes_t *dst, const _zz_bytes_t *src) { } } -_zz_bytes_t _zz_bytes_duplicate(const _zz_bytes_t *src) { - _zz_bytes_t dst = _zz_bytes_null(); - _zz_bytes_copy(&dst, src); +_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; } -size_t _zz_bytes_len(const _zz_bytes_t *bs) { +size_t _z_bytes_len(const _z_bytes_t *bs) { size_t len = 0; for (size_t i = 0; i < _z_arc_slice_svec_len(&bs->_slices); ++i) { const _z_arc_slice_t *s = _z_arc_slice_svec_get(&bs->_slices, i); @@ -55,7 +55,7 @@ size_t _zz_bytes_len(const _zz_bytes_t *bs) { return len; } -_Bool _zz_bytes_is_empty(const _zz_bytes_t *bs) { +_Bool _z_bytes_is_empty(const _z_bytes_t *bs) { for (size_t i = 0; i < _z_arc_slice_svec_len(&bs->_slices); i++) { const _z_arc_slice_t *s = _z_arc_slice_svec_get(&bs->_slices, i); if (_z_arc_slice_len(s) > 0) return false; @@ -63,32 +63,32 @@ _Bool _zz_bytes_is_empty(const _zz_bytes_t *bs) { return true; } -size_t _zz_bytes_num_slices(const _zz_bytes_t *bs) { return _z_arc_slice_svec_len(&bs->_slices); } +size_t _z_bytes_num_slices(const _z_bytes_t *bs) { return _z_arc_slice_svec_len(&bs->_slices); } -_z_arc_slice_t *_zz_bytes_get_slice(const _zz_bytes_t *bs, size_t i) { - if (i >= _zz_bytes_num_slices(bs)) return NULL; +_z_arc_slice_t *_z_bytes_get_slice(const _z_bytes_t *bs, size_t i) { + if (i >= _z_bytes_num_slices(bs)) return NULL; return _z_arc_slice_svec_get(&bs->_slices, i); } -void _zz_bytes_drop(_zz_bytes_t *bytes) { _z_arc_slice_svec_clear(&bytes->_slices); } +void _z_bytes_drop(_z_bytes_t *bytes) { _z_arc_slice_svec_clear(&bytes->_slices); } -void _zz_bytes_free(_zz_bytes_t **bs) { - _zz_bytes_t *ptr = *bs; +void _z_bytes_free(_z_bytes_t **bs) { + _z_bytes_t *ptr = *bs; if (ptr != NULL) { - _zz_bytes_drop(ptr); + _z_bytes_drop(ptr); z_free(ptr); *bs = NULL; } } -size_t _zz_bytes_to_buf(const _zz_bytes_t *bytes, uint8_t *dst, size_t len) { +size_t _z_bytes_to_buf(const _z_bytes_t *bytes, uint8_t *dst, size_t len) { uint8_t *start = dst; - for (size_t i = 0; i < _zz_bytes_num_slices(bytes) && len > 0; ++i) { + for (size_t i = 0; i < _z_bytes_num_slices(bytes) && len > 0; ++i) { // Recopy data - _z_arc_slice_t *s = _zz_bytes_get_slice(bytes, i); + _z_arc_slice_t *s = _z_bytes_get_slice(bytes, i); size_t s_len = _z_arc_slice_len(s); size_t len_to_copy = len >= s_len ? s_len : len; memcpy(start, _z_arc_slice_data(s), len_to_copy); @@ -98,31 +98,31 @@ size_t _zz_bytes_to_buf(const _zz_bytes_t *bytes, uint8_t *dst, size_t len) { return len; } -int8_t _zz_bytes_from_slice(_zz_bytes_t *b, _z_slice_t s) { - *b = _zz_bytes_null(); +int8_t _z_bytes_from_slice(_z_bytes_t *b, _z_slice_t s) { + *b = _z_bytes_null(); _z_arc_slice_t arc_s = _z_arc_slice_wrap(s, 0, s.len); if (_z_arc_slice_len(&arc_s) != s.len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; return _z_arc_slice_svec_append(&b->_slices, &arc_s) ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY; } -int8_t _zz_bytes_from_buf(_zz_bytes_t *b, uint8_t *src, size_t len) { - *b = _zz_bytes_null(); +int8_t _z_bytes_from_buf(_z_bytes_t *b, uint8_t *src, size_t len) { + *b = _z_bytes_null(); _z_slice_t s = _z_slice_wrap_copy(src, len); if (s.len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - return _zz_bytes_from_slice(b, s); + return _z_bytes_from_slice(b, s); } -int8_t _zz_bytes_to_slice(const _zz_bytes_t *bytes, _z_slice_t *s) { +int8_t _z_bytes_to_slice(const _z_bytes_t *bytes, _z_slice_t *s) { // Allocate slice - size_t len = _zz_bytes_len(bytes); + size_t len = _z_bytes_len(bytes); *s = _z_slice_make(len); if (!_z_slice_check(*s) && len > 0) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } uint8_t *start = (uint8_t *)s->start; - for (size_t i = 0; i < _zz_bytes_num_slices(bytes); ++i) { + for (size_t i = 0; i < _z_bytes_num_slices(bytes); ++i) { // Recopy data - _z_arc_slice_t *arc_s = _zz_bytes_get_slice(bytes, i); + _z_arc_slice_t *arc_s = _z_bytes_get_slice(bytes, i); size_t s_len = _z_arc_slice_len(arc_s); memcpy(start, _z_arc_slice_data(arc_s), s_len); start += s_len; @@ -131,14 +131,14 @@ int8_t _zz_bytes_to_slice(const _zz_bytes_t *bytes, _z_slice_t *s) { return _Z_RES_OK; } -int8_t _zz_bytes_append_slice(_zz_bytes_t *dst, _z_arc_slice_t *s) { +int8_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s) { return _z_arc_slice_svec_append(&dst->_slices, s); } -int8_t _zz_bytes_append_inner(_zz_bytes_t *dst, _zz_bytes_t *src) { +int8_t _z_bytes_append_inner(_z_bytes_t *dst, _z_bytes_t *src) { _Bool success = true; - for (size_t i = 0; i < _zz_bytes_num_slices(src); ++i) { - _z_arc_slice_t *s = _zz_bytes_get_slice(src, i); + for (size_t i = 0; i < _z_bytes_num_slices(src); ++i) { + _z_arc_slice_t *s = _z_bytes_get_slice(src, i); success = success && _z_arc_slice_svec_append(&dst->_slices, s); } if (!success) { @@ -149,9 +149,9 @@ int8_t _zz_bytes_append_inner(_zz_bytes_t *dst, _zz_bytes_t *src) { return _Z_RES_OK; } -int8_t _zz_bytes_append(_zz_bytes_t *dst, _zz_bytes_t *src) { +int8_t _z_bytes_append(_z_bytes_t *dst, _z_bytes_t *src) { uint8_t l_buf[16]; - size_t l_len = _z_zsize_encode_buf(l_buf, _zz_bytes_len(src)); + size_t l_len = _z_zsize_encode_buf(l_buf, _z_bytes_len(src)); _z_slice_t s = _z_slice_wrap_copy(l_buf, l_len); if (!_z_slice_check(s)) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; @@ -163,104 +163,104 @@ int8_t _zz_bytes_append(_zz_bytes_t *dst, _zz_bytes_t *src) { _z_arc_slice_drop(&arc_s); return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - int8_t res = _zz_bytes_append_inner(dst, src); + int8_t res = _z_bytes_append_inner(dst, src); if (res != _Z_RES_OK) { return res; } return _Z_RES_OK; } -int8_t _zz_bytes_serialize_from_pair(_zz_bytes_t *out, _zz_bytes_t *first, _zz_bytes_t *second) { - int8_t res = _zz_bytes_append(out, first); +int8_t _z_bytes_serialize_from_pair(_z_bytes_t *out, _z_bytes_t *first, _z_bytes_t *second) { + int8_t res = _z_bytes_append(out, first); if (res != _Z_RES_OK) { - _zz_bytes_drop(out); - _zz_bytes_drop(first); + _z_bytes_drop(out); + _z_bytes_drop(first); return res; } - res = _zz_bytes_append(out, second); + res = _z_bytes_append(out, second); if (res != _Z_RES_OK) { - _zz_bytes_drop(out); - _zz_bytes_drop(second); + _z_bytes_drop(out); + _z_bytes_drop(second); } return res; } -int8_t _zz_bytes_deserialize_into_pair(const _zz_bytes_t *bs, _zz_bytes_t *first_out, _zz_bytes_t *second_out) { - _zz_bytes_reader_t reader = _zz_bytes_get_reader(bs); - int8_t res = _zz_bytes_reader_read_next(&reader, first_out); +int8_t _z_bytes_deserialize_into_pair(const _z_bytes_t *bs, _z_bytes_t *first_out, _z_bytes_t *second_out) { + _z_bytes_reader_t reader = _z_bytes_get_reader(bs); + int8_t res = _z_bytes_reader_read_next(&reader, first_out); if (res != _Z_RES_OK) return res; - res = _zz_bytes_reader_read_next(&reader, second_out); + res = _z_bytes_reader_read_next(&reader, second_out); if (res != _Z_RES_OK) { - _zz_bytes_drop(first_out); + _z_bytes_drop(first_out); }; return res; } -int8_t _zz_bytes_to_uint8(const _zz_bytes_t *bs, uint8_t *val) { +int8_t _z_bytes_to_uint8(const _z_bytes_t *bs, uint8_t *val) { *val = 0; - return _zz_bytes_to_buf(bs, val, sizeof(uint8_t)) == sizeof(uint8_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; + return _z_bytes_to_buf(bs, val, sizeof(uint8_t)) == sizeof(uint8_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } // FIXME: int16+ endianness, Issue #420 -int8_t _zz_bytes_to_uint16(const _zz_bytes_t *bs, uint16_t *val) { +int8_t _z_bytes_to_uint16(const _z_bytes_t *bs, uint16_t *val) { *val = 0; - return _zz_bytes_to_buf(bs, (uint8_t *)val, sizeof(uint16_t)) == sizeof(uint16_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; + return _z_bytes_to_buf(bs, (uint8_t *)val, sizeof(uint16_t)) == sizeof(uint16_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } -int8_t _zz_bytes_to_uint32(const _zz_bytes_t *bs, uint32_t *val) { +int8_t _z_bytes_to_uint32(const _z_bytes_t *bs, uint32_t *val) { *val = 0; - return _zz_bytes_to_buf(bs, (uint8_t *)val, sizeof(uint32_t)) == sizeof(uint32_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; + return _z_bytes_to_buf(bs, (uint8_t *)val, sizeof(uint32_t)) == sizeof(uint32_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } -int8_t _zz_bytes_to_uint64(const _zz_bytes_t *bs, uint64_t *val) { +int8_t _z_bytes_to_uint64(const _z_bytes_t *bs, uint64_t *val) { *val = 0; - return _zz_bytes_to_buf(bs, (uint8_t *)val, sizeof(uint64_t)) == sizeof(uint64_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; + return _z_bytes_to_buf(bs, (uint8_t *)val, sizeof(uint64_t)) == sizeof(uint64_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } -int8_t _zz_bytes_to_float(const _zz_bytes_t *bs, float *val) { +int8_t _z_bytes_to_float(const _z_bytes_t *bs, float *val) { *val = 0; - return _zz_bytes_to_buf(bs, (uint8_t *)val, sizeof(float)) == sizeof(float) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; + return _z_bytes_to_buf(bs, (uint8_t *)val, sizeof(float)) == sizeof(float) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } -int8_t _zz_bytes_to_double(const _zz_bytes_t *bs, double *val) { +int8_t _z_bytes_to_double(const _z_bytes_t *bs, double *val) { *val = 0; - return _zz_bytes_to_buf(bs, (uint8_t *)val, sizeof(double)) == sizeof(double) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; + return _z_bytes_to_buf(bs, (uint8_t *)val, sizeof(double)) == sizeof(double) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } -int8_t _zz_bytes_from_uint8(_zz_bytes_t *b, uint8_t val) { return _zz_bytes_from_buf(b, &val, sizeof(val)); } +int8_t _z_bytes_from_uint8(_z_bytes_t *b, uint8_t val) { return _z_bytes_from_buf(b, &val, sizeof(val)); } -int8_t _zz_bytes_from_uint16(_zz_bytes_t *b, uint16_t val) { - return _zz_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); +int8_t _z_bytes_from_uint16(_z_bytes_t *b, uint16_t val) { + return _z_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); } -int8_t _zz_bytes_from_uint32(_zz_bytes_t *b, uint32_t val) { - return _zz_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); +int8_t _z_bytes_from_uint32(_z_bytes_t *b, uint32_t val) { + return _z_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); } -int8_t _zz_bytes_from_uint64(_zz_bytes_t *b, uint64_t val) { - return _zz_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); +int8_t _z_bytes_from_uint64(_z_bytes_t *b, uint64_t val) { + return _z_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); } -int8_t _zz_bytes_from_float(_zz_bytes_t *b, float val) { return _zz_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); } +int8_t _z_bytes_from_float(_z_bytes_t *b, float val) { return _z_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); } -int8_t _zz_bytes_from_double(_zz_bytes_t *b, double val) { return _zz_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); } +int8_t _z_bytes_from_double(_z_bytes_t *b, double val) { return _z_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); } -_z_slice_t _zz_bytes_try_get_contiguous(const _zz_bytes_t *bs) { - if (_zz_bytes_num_slices(bs) == 1) { - _z_arc_slice_t *arc_s = _zz_bytes_get_slice(bs, 0); +_z_slice_t _z_bytes_try_get_contiguous(const _z_bytes_t *bs) { + if (_z_bytes_num_slices(bs) == 1) { + _z_arc_slice_t *arc_s = _z_bytes_get_slice(bs, 0); return _z_slice_wrap(_z_arc_slice_data(arc_s), _z_arc_slice_len(arc_s)); } return _z_slice_empty(); } -void _zz_bytes_move(_zz_bytes_t *dst, _zz_bytes_t *src) { +void _z_bytes_move(_z_bytes_t *dst, _z_bytes_t *src) { dst->_slices = src->_slices; - *src = _zz_bytes_null(); + *src = _z_bytes_null(); } -_zz_bytes_reader_t _zz_bytes_get_reader(const _zz_bytes_t *bytes) { - _zz_bytes_reader_t r; +_z_bytes_reader_t _z_bytes_get_reader(const _z_bytes_t *bytes) { + _z_bytes_reader_t r; r.bytes = bytes; r.slice_idx = 0; r.byte_idx = 0; @@ -268,10 +268,10 @@ _zz_bytes_reader_t _zz_bytes_get_reader(const _zz_bytes_t *bytes) { return r; } -int8_t _zz_bytes_reader_seek_forward(_zz_bytes_reader_t *reader, size_t offset) { +int8_t _z_bytes_reader_seek_forward(_z_bytes_reader_t *reader, size_t offset) { size_t start_slice = reader->slice_idx; - for (size_t i = start_slice; i < _zz_bytes_num_slices(reader->bytes); ++i) { - _z_arc_slice_t *s = _zz_bytes_get_slice(reader->bytes, i); + for (size_t i = start_slice; i < _z_bytes_num_slices(reader->bytes); ++i) { + _z_arc_slice_t *s = _z_bytes_get_slice(reader->bytes, i); size_t remaining = _z_arc_slice_len(s) - reader->in_slice_idx; if (offset >= remaining) { reader->slice_idx += 1; @@ -290,12 +290,12 @@ int8_t _zz_bytes_reader_seek_forward(_zz_bytes_reader_t *reader, size_t offset) return _Z_RES_OK; } -int8_t _zz_bytes_reader_seek_backward(_zz_bytes_reader_t *reader, size_t offset) { +int8_t _z_bytes_reader_seek_backward(_z_bytes_reader_t *reader, size_t offset) { while (offset != 0) { if (reader->in_slice_idx == 0) { if (reader->slice_idx == 0) return _Z_ERR_DID_NOT_READ; reader->slice_idx--; - _z_arc_slice_t *s = _zz_bytes_get_slice(reader->bytes, reader->slice_idx); + _z_arc_slice_t *s = _z_bytes_get_slice(reader->bytes, reader->slice_idx); reader->in_slice_idx = _z_arc_slice_len(s); } @@ -312,42 +312,42 @@ int8_t _zz_bytes_reader_seek_backward(_zz_bytes_reader_t *reader, size_t offset) return _Z_RES_OK; } -int8_t _zz_bytes_reader_seek(_zz_bytes_reader_t *reader, int64_t offset, int origin) { +int8_t _z_bytes_reader_seek(_z_bytes_reader_t *reader, int64_t offset, int origin) { switch (origin) { case SEEK_SET: { reader->byte_idx = 0; reader->in_slice_idx = 0; reader->slice_idx = 0; if (offset < 0) return _Z_ERR_DID_NOT_READ; - return _zz_bytes_reader_seek_forward(reader, offset); + return _z_bytes_reader_seek_forward(reader, offset); } case SEEK_CUR: { if (offset >= 0) - return _zz_bytes_reader_seek_forward(reader, offset); + return _z_bytes_reader_seek_forward(reader, offset); else - return _zz_bytes_reader_seek_backward(reader, -offset); + return _z_bytes_reader_seek_backward(reader, -offset); } case SEEK_END: { - reader->byte_idx = _zz_bytes_len(reader->bytes); + reader->byte_idx = _z_bytes_len(reader->bytes); reader->in_slice_idx = 0; - reader->slice_idx = _zz_bytes_num_slices(reader->bytes); + reader->slice_idx = _z_bytes_num_slices(reader->bytes); if (offset > 0) return _Z_ERR_DID_NOT_READ; else - return _zz_bytes_reader_seek_backward(reader, -offset); + return _z_bytes_reader_seek_backward(reader, -offset); } default: return _Z_ERR_GENERIC; } } -int64_t _zz_bytes_reader_tell(const _zz_bytes_reader_t *reader) { return reader->byte_idx; } +int64_t _z_bytes_reader_tell(const _z_bytes_reader_t *reader) { return reader->byte_idx; } -int8_t _zz_bytes_reader_read(_zz_bytes_reader_t *reader, uint8_t *buf, size_t len) { +int8_t _z_bytes_reader_read(_z_bytes_reader_t *reader, uint8_t *buf, size_t len) { uint8_t *buf_start = buf; - for (size_t i = reader->slice_idx; i < _zz_bytes_num_slices(reader->bytes); ++i) { - _z_arc_slice_t *s = _zz_bytes_get_slice(reader->bytes, i); + for (size_t i = reader->slice_idx; i < _z_bytes_num_slices(reader->bytes); ++i) { + _z_arc_slice_t *s = _z_bytes_get_slice(reader->bytes, i); size_t remaining = _z_arc_slice_len(s) - reader->in_slice_idx; if (len >= remaining) { memcpy(buf_start, _z_arc_slice_data(s) + reader->in_slice_idx, remaining); @@ -370,20 +370,20 @@ int8_t _zz_bytes_reader_read(_zz_bytes_reader_t *reader, uint8_t *buf, size_t le } int8_t __read_single_byte(uint8_t *b, void *context) { - _zz_bytes_reader_t *reader = (_zz_bytes_reader_t *)context; - return _zz_bytes_reader_read(reader, b, 1); + _z_bytes_reader_t *reader = (_z_bytes_reader_t *)context; + return _z_bytes_reader_read(reader, b, 1); } -int8_t _zz_bytes_reader_read_zint(_zz_bytes_reader_t *reader, _z_zint_t *zint) { +int8_t _z_bytes_reader_read_zint(_z_bytes_reader_t *reader, _z_zint_t *zint) { return _z_zsize_decode_with_reader(zint, __read_single_byte, reader); } -int8_t _zz_bytes_reader_read_slices(_zz_bytes_reader_t *reader, size_t len, _zz_bytes_t *out) { - *out = _zz_bytes_null(); +int8_t _z_bytes_reader_read_slices(_z_bytes_reader_t *reader, size_t len, _z_bytes_t *out) { + *out = _z_bytes_null(); int8_t res = _Z_RES_OK; - for (size_t i = reader->slice_idx; i < _zz_bytes_num_slices(reader->bytes) && len > 0; ++i) { - _z_arc_slice_t *s = _zz_bytes_get_slice(reader->bytes, i); + for (size_t i = reader->slice_idx; i < _z_bytes_num_slices(reader->bytes) && len > 0; ++i) { + _z_arc_slice_t *s = _z_bytes_get_slice(reader->bytes, i); size_t s_len = _z_arc_slice_len(s); size_t remaining = s_len - reader->in_slice_idx; @@ -409,25 +409,25 @@ int8_t _zz_bytes_reader_read_slices(_zz_bytes_reader_t *reader, size_t len, _zz_ } if (len > 0 && res == _Z_RES_OK) res = _Z_ERR_DID_NOT_READ; - if (res != _Z_RES_OK) _zz_bytes_drop(out); + if (res != _Z_RES_OK) _z_bytes_drop(out); return res; } -int8_t _zz_bytes_reader_read_next(_zz_bytes_reader_t *reader, _zz_bytes_t *out) { - *out = _zz_bytes_null(); +int8_t _z_bytes_reader_read_next(_z_bytes_reader_t *reader, _z_bytes_t *out) { + *out = _z_bytes_null(); _z_zint_t len; - if (_zz_bytes_reader_read_zint(reader, &len) != _Z_RES_OK) { + if (_z_bytes_reader_read_zint(reader, &len) != _Z_RES_OK) { return _Z_ERR_DID_NOT_READ; } - return _zz_bytes_reader_read_slices(reader, len, out); + return _z_bytes_reader_read_slices(reader, len, out); } -_zz_bytes_iterator_t _zz_bytes_get_iterator(const _zz_bytes_t *bytes) { - return (_zz_bytes_iterator_t){._reader = _zz_bytes_get_reader(bytes)}; +_z_bytes_iterator_t _z_bytes_get_iterator(const _z_bytes_t *bytes) { + return (_z_bytes_iterator_t){._reader = _z_bytes_get_reader(bytes)}; } -_Bool _zz_bytes_iterator_next(_zz_bytes_iterator_t *iter, _zz_bytes_t *b) { - return _zz_bytes_reader_read_next(&iter->_reader, b) == _Z_RES_OK; +_Bool _z_bytes_iterator_next(_z_bytes_iterator_t *iter, _z_bytes_t *b) { + return _z_bytes_reader_read_next(&iter->_reader, b) == _Z_RES_OK; } diff --git a/src/net/memory.c b/src/net/memory.c index fbf6258b9..2a5096845 100644 --- a/src/net/memory.c +++ b/src/net/memory.c @@ -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); - _zz_bytes_drop(&value->payload); + _z_bytes_drop(&value->payload); } void _z_value_free(_z_value_t **value) { diff --git a/src/net/primitives.c b/src/net/primitives.c index 4f9db8786..4385c7799 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -130,9 +130,9 @@ int8_t _z_undeclare_publisher(_z_publisher_t *pub) { } /*------------------ Write ------------------*/ -int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _zz_bytes_t payload, const _z_encoding_t encoding, +int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_bytes_t payload, const _z_encoding_t encoding, const z_sample_kind_t kind, const z_congestion_control_t cong_ctrl, z_priority_t priority, - const _zz_bytes_t attachment) { + const _z_bytes_t attachment) { int8_t ret = _Z_RES_OK; _z_network_message_t msg; switch (kind) { @@ -318,7 +318,7 @@ int8_t _z_undeclare_queryable(_z_queryable_t *qle) { } int8_t _z_send_reply(const _z_query_t *query, _z_keyexpr_t keyexpr, const _z_value_t payload, - const z_sample_kind_t kind, const _zz_bytes_t att) { + const z_sample_kind_t kind, const _z_bytes_t att) { int8_t ret = _Z_RES_OK; _z_keyexpr_t q_ke; @@ -375,7 +375,7 @@ int8_t _z_send_reply(const _z_query_t *query, _z_keyexpr_t keyexpr, const _z_val /*------------------ Query ------------------*/ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, const z_query_target_t target, const z_consolidation_mode_t consolidation, _z_value_t value, _z_reply_handler_t callback, - _z_drop_handler_t dropper, void *arg, uint32_t timeout_ms, const _zz_bytes_t attachment) { + _z_drop_handler_t dropper, void *arg, uint32_t timeout_ms, const _z_bytes_t attachment) { int8_t ret = _Z_RES_OK; // Create the pending query object diff --git a/src/net/query.c b/src/net/query.c index 8c5477272..2b87c0e03 100644 --- a/src/net/query.c +++ b/src/net/query.c @@ -38,12 +38,12 @@ void _z_query_clear(_z_query_t *q) { z_free(q->_parameters); _z_keyexpr_clear(&q->_key); _z_value_clear(&q->_value); - _zz_bytes_drop(&q->attachment); + _z_bytes_drop(&q->attachment); } #if Z_FEATURE_QUERYABLE == 1 _z_query_t _z_query_create(const _z_value_t *value, const _z_keyexpr_t *key, const _z_slice_t *parameters, - _z_session_t *zn, uint32_t request_id, const _zz_bytes_t attachment) { + _z_session_t *zn, uint32_t request_id, const _z_bytes_t attachment) { _z_query_t q = _z_query_null(); q._request_id = request_id; q._zn = zn; // Ideally would have been an rc diff --git a/src/net/reply.c b/src/net/reply.c index 5df0544a1..48bff2c6c 100644 --- a/src/net/reply.c +++ b/src/net/reply.c @@ -84,9 +84,9 @@ void _z_pending_reply_clear(_z_pending_reply_t *pr) { _z_timestamp_clear(&pr->_tstamp); } -_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _zz_bytes_t payload, +_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_bytes_t payload, const _z_timestamp_t *timestamp, _z_encoding_t encoding, z_sample_kind_t kind, - const _zz_bytes_t attachment) { + const _z_bytes_t attachment) { _z_reply_t reply = _z_reply_null(); reply._tag = tag; if (tag == Z_REPLY_TAG_DATA) { @@ -106,9 +106,9 @@ _z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, return reply; } #else -_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _zz_bytes_t payload, +_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_bytes_t payload, const _z_timestamp_t *timestamp, _z_encoding_t encoding, z_sample_kind_t kind, - const _zz_bytes_t attachment) { + const _z_bytes_t attachment) { _ZP_UNUSED(keyexpr); _ZP_UNUSED(tag); _ZP_UNUSED(id); diff --git a/src/net/sample.c b/src/net/sample.c index 5e3809778..c601783ea 100644 --- a/src/net/sample.c +++ b/src/net/sample.c @@ -19,18 +19,18 @@ _z_sample_t _z_sample_null(void) { _z_sample_t s = { .keyexpr = _z_keyexpr_null(), - .payload = _zz_bytes_null(), + .payload = _z_bytes_null(), .encoding = _z_encoding_null(), .timestamp = _z_timestamp_null(), .kind = 0, .qos = {0}, - .attachment = _zz_bytes_null(), + .attachment = _z_bytes_null(), }; return s; } _Bool _z_sample_check(const _z_sample_t *sample) { - return _z_keyexpr_check(sample->keyexpr) && _zz_bytes_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) { @@ -38,7 +38,7 @@ void _z_sample_move(_z_sample_t *dst, _z_sample_t *src) { dst->keyexpr._suffix = src->keyexpr._suffix; // FIXME: call the z_keyexpr_move src->keyexpr._suffix = NULL; // FIXME: call the z_keyexpr_move - _zz_bytes_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 @@ -47,10 +47,10 @@ 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); - _zz_bytes_drop(&sample->payload); + _z_bytes_drop(&sample->payload); _z_encoding_clear(&sample->encoding); _z_timestamp_clear(&sample->timestamp); - _zz_bytes_drop(&sample->attachment); + _z_bytes_drop(&sample->attachment); } void _z_sample_free(_z_sample_t **sample) { @@ -64,11 +64,11 @@ 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); - _zz_bytes_copy(&dst->payload, &src->payload); + _z_bytes_copy(&dst->payload, &src->payload); dst->timestamp = _z_timestamp_duplicate(&src->timestamp); _z_encoding_copy(&dst->encoding, &src->encoding); dst->kind = src->kind; - _zz_bytes_copy(&dst->attachment, &src->attachment); + _z_bytes_copy(&dst->attachment, &src->attachment); } _z_sample_t _z_sample_duplicate(const _z_sample_t *src) { @@ -78,9 +78,9 @@ _z_sample_t _z_sample_duplicate(const _z_sample_t *src) { } #if Z_FEATURE_SUBSCRIPTION == 1 -_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _zz_bytes_t payload, const _z_timestamp_t timestamp, +_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes_t payload, const _z_timestamp_t timestamp, const _z_encoding_t encoding, const z_sample_kind_t kind, const _z_qos_t qos, - const _zz_bytes_t attachment) { + const _z_bytes_t attachment) { _z_sample_t s = _z_sample_null(); _z_keyexpr_copy(&s.keyexpr, key); s.payload = payload; @@ -92,9 +92,9 @@ _z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _zz_bytes_t payload, return s; } #else -_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _zz_bytes payload, const _z_timestamp_t timestamp, +_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes payload, const _z_timestamp_t timestamp, const _z_encoding_t encoding, const z_sample_kind_t kind, const _z_qos_t qos, - const _zz_bytes_t attachment) { + const _z_bytes_t attachment) { _ZP_UNUSED(key); _ZP_UNUSED(payload); _ZP_UNUSED(timestamp); diff --git a/src/protocol/codec.c b/src/protocol/codec.c index c34ec645c..7b4d3efa3 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -274,27 +274,27 @@ 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 _zz_bytes_decode(_zz_bytes_t *bs, _z_zbuf_t *zbf) { +int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; _z_slice_t s; ret = _z_slice_decode(&s, zbf); if (ret != _Z_RES_OK) return ret; - return _zz_bytes_from_slice(bs, s); + return _z_bytes_from_slice(bs, s); } -int8_t _zz_bytes_encode_val(_z_wbuf_t *wbf, const _zz_bytes_t *bs) { +int8_t _z_bytes_encode_val(_z_wbuf_t *wbf, const _z_bytes_t *bs) { int8_t ret = _Z_RES_OK; - for (size_t i = 0; i < _zz_bytes_num_slices(bs); ++i) { - const _z_arc_slice_t *arc_s = _zz_bytes_get_slice(bs, i); + for (size_t i = 0; i < _z_bytes_num_slices(bs); ++i) { + const _z_arc_slice_t *arc_s = _z_bytes_get_slice(bs, i); _Z_RETURN_IF_ERR(_z_buf_encode(wbf, _z_arc_slice_data(arc_s), _z_arc_slice_len(arc_s))) } return ret; } -int8_t _zz_bytes_encode(_z_wbuf_t *wbf, const _zz_bytes_t *bs) { - _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, _zz_bytes_len(bs))) - return _zz_bytes_encode_val(wbf, bs); +int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { + _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, _z_bytes_len(bs))) + return _z_bytes_encode_val(wbf, bs); } /*------------------ string with null terminator ------------------*/ @@ -379,14 +379,14 @@ int8_t _z_encoding_decode(_z_encoding_t *en, _z_zbuf_t *zbf) { } int8_t _z_value_encode(_z_wbuf_t *wbf, const _z_value_t *value) { - size_t total_len = _z_encoding_len(&value->encoding) + _zz_bytes_len(&value->payload); + size_t total_len = _z_encoding_len(&value->encoding) + _z_bytes_len(&value->payload); _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, total_len)); _Z_RETURN_IF_ERR(_z_encoding_encode(wbf, &value->encoding)); - return _zz_bytes_encode_val(wbf, &value->payload); + return _z_bytes_encode_val(wbf, &value->payload); } int8_t _z_value_decode(_z_value_t *value, _z_zbuf_t *zbf) { _Z_RETURN_IF_ERR(_z_encoding_decode(&value->encoding, zbf)); - _Z_RETURN_IF_ERR(_zz_bytes_from_buf(&value->payload, (uint8_t *)_z_zbuf_start(zbf), _z_zbuf_len(zbf))); + _Z_RETURN_IF_ERR(_z_bytes_from_buf(&value->payload, (uint8_t *)_z_zbuf_start(zbf), _z_zbuf_len(zbf))); return _Z_RES_OK; } diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index f64862d67..e78fe27d4 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -254,7 +254,7 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { pshb->_body._put._commons._source_info._source_sn != 0 || pshb->_body._put._commons._source_info._entity_id != 0; - _Bool has_attachment = pshb->_is_put && _zz_bytes_check(&pshb->_body._put._attachment); + _Bool has_attachment = pshb->_is_put && _z_bytes_check(&pshb->_body._put._attachment); _Bool has_timestamp = _z_timestamp_check(&pshb->_body._put._commons._timestamp); _Bool has_encoding = false; if (has_source_info || has_attachment) { @@ -288,10 +288,10 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { } if (has_attachment) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x03)); - _Z_RETURN_IF_ERR(_zz_bytes_encode(wbf, &pshb->_body._put._attachment)); + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &pshb->_body._put._attachment)); } if (pshb->_is_put) { - _Z_RETURN_IF_ERR(_zz_bytes_encode(wbf, &pshb->_body._put._payload)); + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &pshb->_body._put._payload)); } return 0; @@ -312,7 +312,7 @@ int8_t _z_push_body_decode_extensions(_z_msg_ext_t *extension, void *ctx) { } else { _Z_RETURN_IF_ERR(_z_slice_copy(&s, &extension->_body._zbuf._val)); } - ret = _zz_bytes_from_slice(&pshb->_body._put._attachment, s); + ret = _z_bytes_from_slice(&pshb->_body._put._attachment, s); } default: if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { @@ -339,7 +339,7 @@ int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf, uint8_t header) _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_push_body_decode_extensions, pshb)); } if (ret == _Z_RES_OK) { - _Z_RETURN_IF_ERR(_zz_bytes_decode(&pshb->_body._put._payload, zbf)); + _Z_RETURN_IF_ERR(_z_bytes_decode(&pshb->_body._put._payload, zbf)); } break; } @@ -429,7 +429,7 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *msg) { } if (required_exts.attachment) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x05)); - _Z_RETURN_IF_ERR(_zz_bytes_encode(wbf, &msg->_ext_attachment)); + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_ext_attachment)); } return ret; } @@ -446,7 +446,7 @@ int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) { case _Z_MSG_EXT_ENC_ZBUF | 0x03: { // Payload _z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); ret = _z_encoding_decode(&msg->_ext_value.encoding, &zbf); - ret |= _zz_bytes_from_buf(&msg->_ext_value.payload, (uint8_t *)_z_zbuf_start(&zbf), _z_zbuf_len(&zbf)); + ret |= _z_bytes_from_buf(&msg->_ext_value.payload, (uint8_t *)_z_zbuf_start(&zbf), _z_zbuf_len(&zbf)); break; } case _Z_MSG_EXT_ENC_ZBUF | 0x05: { // Attachment @@ -456,7 +456,7 @@ int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) { } else { _Z_RETURN_IF_ERR(_z_slice_copy(&s, &extension->_body._zbuf._val)); } - ret = _zz_bytes_from_slice(&msg->_ext_attachment, s); + ret = _z_bytes_from_slice(&msg->_ext_attachment, s); break; } default: @@ -553,7 +553,7 @@ int8_t _z_err_encode(_z_wbuf_t *wbf, const _z_msg_err_t *err) { _Z_RETURN_IF_ERR(_z_source_info_encode_ext(wbf, &err->_ext_source_info)); } // Encode payload - _Z_RETURN_IF_ERR(_zz_bytes_encode(wbf, &err->_payload)); + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &err->_payload)); return ret; } int8_t _z_err_decode_extension(_z_msg_ext_t *extension, void *ctx) { @@ -581,7 +581,7 @@ int8_t _z_err_decode(_z_msg_err_t *err, _z_zbuf_t *zbf, uint8_t header) { if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_err_decode_extension, err)); } - _Z_RETURN_IF_ERR(_zz_bytes_decode(&err->_payload, zbf)); + _Z_RETURN_IF_ERR(_z_bytes_decode(&err->_payload, zbf)); return _Z_RES_OK; } diff --git a/src/protocol/core.c b/src/protocol/core.c index 6601285a9..08cc542c4 100644 --- a/src/protocol/core.c +++ b/src/protocol/core.c @@ -66,7 +66,7 @@ _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}; } -_z_value_t _z_value_null(void) { return (_z_value_t){.payload = _zz_bytes_null(), .encoding = _z_encoding_null()}; } +_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; *value = _z_value_null(); @@ -74,5 +74,5 @@ _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); - _zz_bytes_copy(&dst->payload, &src->payload); + _z_bytes_copy(&dst->payload, &src->payload); } diff --git a/src/protocol/definitions/message.c b/src/protocol/definitions/message.c index 94781d44f..e9e5fe0af 100644 --- a/src/protocol/definitions/message.c +++ b/src/protocol/definitions/message.c @@ -24,15 +24,15 @@ void _z_msg_reply_clear(_z_msg_reply_t *msg) { _z_push_body_clear(&msg->_body); void _z_msg_put_clear(_z_msg_put_t *msg) { _z_encoding_clear(&msg->_encoding); - _zz_bytes_drop(&msg->_payload); + _z_bytes_drop(&msg->_payload); _z_timestamp_clear(&msg->_commons._timestamp); } _z_msg_query_reqexts_t _z_msg_query_required_extensions(const _z_msg_query_t *msg) { return (_z_msg_query_reqexts_t){ - .body = _zz_bytes_check(&msg->_ext_value.payload) || _z_encoding_check(&msg->_ext_value.encoding), + .body = _z_bytes_check(&msg->_ext_value.payload) || _z_encoding_check(&msg->_ext_value.encoding), .info = _z_id_check(msg->_ext_info._id) || msg->_ext_info._entity_id != 0 || msg->_ext_info._source_sn != 0, - .attachment = _zz_bytes_check(&msg->_ext_attachment), + .attachment = _z_bytes_check(&msg->_ext_attachment), }; } @@ -42,5 +42,5 @@ void _z_msg_query_clear(_z_msg_query_t *msg) { } void _z_msg_err_clear(_z_msg_err_t *err) { _z_encoding_clear(&err->encoding); - _zz_bytes_drop(&err->_payload); + _z_bytes_drop(&err->_payload); } diff --git a/src/protocol/definitions/network.c b/src/protocol/definitions/network.c index da227de38..f7c08f34f 100644 --- a/src/protocol/definitions/network.c +++ b/src/protocol/definitions/network.c @@ -138,7 +138,7 @@ void _z_n_msg_free(_z_network_message_t **msg) { _z_zenoh_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_slice_t) parameters, _z_zint_t qid, z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value, - uint32_t timeout_ms, _zz_bytes_t attachment) { + uint32_t timeout_ms, _z_bytes_t attachment) { return (_z_zenoh_message_t){ ._tag = _Z_N_REQUEST, ._body._request = diff --git a/src/session/push.c b/src/session/push.c index 3db6de57a..b8a1cab9a 100644 --- a/src/session/push.c +++ b/src/session/push.c @@ -26,7 +26,7 @@ int8_t _z_trigger_push(_z_session_t *zn, _z_n_msg_push_t *push) { // TODO check body to know where to dispatch #if Z_FEATURE_SUBSCRIPTION == 1 - _zz_bytes_t payload = push->_body._is_put ? push->_body._body._put._payload : _zz_bytes_null(); + _z_bytes_t payload = push->_body._is_put ? push->_body._body._put._payload : _z_bytes_null(); _z_encoding_t encoding = push->_body._is_put ? push->_body._body._put._encoding : _z_encoding_null(); size_t kind = push->_body._is_put ? Z_SAMPLE_KIND_PUT : Z_SAMPLE_KIND_DELETE; ret = _z_trigger_subscriptions(zn, push->_key, payload, encoding, kind, push->_timestamp, push->_qos, diff --git a/src/session/queryable.c b/src/session/queryable.c index 93d85b353..53d9747ca 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -133,7 +133,7 @@ _z_session_queryable_rc_t *_z_register_session_queryable(_z_session_t *zn, _z_se } int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *msgq, const _z_keyexpr_t q_key, uint32_t qid, - const _zz_bytes_t attachment) { + const _z_bytes_t attachment) { int8_t ret = _Z_RES_OK; _zp_session_lock_mutex(zn); diff --git a/src/session/rx.c b/src/session/rx.c index 44bf0a3fa..aaa95eead 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -111,9 +111,9 @@ int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint case _Z_REQUEST_DEL: { #if Z_FEATURE_SUBSCRIPTION == 1 _z_msg_del_t del = req._body._del; - ret = _z_trigger_subscriptions(zn, req._key, _zz_bytes_null(), _z_encoding_null(), + ret = _z_trigger_subscriptions(zn, req._key, _z_bytes_null(), _z_encoding_null(), Z_SAMPLE_KIND_DELETE, del._commons._timestamp, req._ext_qos, - _zz_bytes_null()); + _z_bytes_null()); #endif if (ret == _Z_RES_OK) { _z_network_message_t final = _z_n_msg_make_response_final(req._rid); @@ -133,7 +133,7 @@ int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint case _Z_RESPONSE_BODY_ERR: { // @TODO: expose zenoh errors to the user _z_msg_err_t error = response._body._err; - _z_slice_t payload = _zz_bytes_try_get_contiguous(&error._payload); + _z_slice_t payload = _z_bytes_try_get_contiguous(&error._payload); _ZP_UNUSED(payload); // Unused when logs are deactivated _Z_ERROR("Received Err for query %zu: message=%.*s", response._request_id, (int)payload.len, payload.start); diff --git a/src/session/subscription.c b/src/session/subscription.c index 83062199d..1db666dfc 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -138,17 +138,17 @@ _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_loca return ret; } -void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _zz_bytes_t payload, - const _z_n_qos_t qos, const _zz_bytes_t attachment) { +void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_bytes_t payload, + const _z_n_qos_t qos, const _z_bytes_t attachment) { _z_encoding_t encoding = _z_encoding_null(); int8_t ret = _z_trigger_subscriptions(zn, keyexpr, payload, encoding, Z_SAMPLE_KIND_PUT, _z_timestamp_null(), qos, attachment); (void)ret; } -int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _zz_bytes_t payload, +int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_bytes_t payload, const _z_encoding_t encoding, const _z_zint_t kind, const _z_timestamp_t timestamp, - const _z_n_qos_t qos, const _zz_bytes_t attachment) { + const _z_n_qos_t qos, const _z_bytes_t attachment) { int8_t ret = _Z_RES_OK; _zp_session_lock_mutex(zn); @@ -208,8 +208,8 @@ void _z_flush_subscriptions(_z_session_t *zn) { } #else // Z_FEATURE_SUBSCRIPTION == 0 -void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _zz_bytes_t attachment, - _z_n_qos_t qos, const _zz_bytes_t attachment) { +void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_bytes_t attachment, + _z_n_qos_t qos, const _z_bytes_t attachment) { _ZP_UNUSED(zn); _ZP_UNUSED(keyexpr); _ZP_UNUSED(payload); diff --git a/tests/z_bytes_test.c b/tests/z_bytes_test.c index eeddbc5b0..d03755c7a 100644 --- a/tests/z_bytes_test.c +++ b/tests/z_bytes_test.c @@ -22,31 +22,31 @@ #include void test_null_bytes(void) { - _zz_bytes_t b = _zz_bytes_null(); - assert(_zz_bytes_len(&b) == 0); - assert(_zz_bytes_is_empty(&b)); - assert(!_zz_bytes_check(&b)); - assert(_zz_bytes_num_slices(&b) == 0); - _zz_bytes_drop(&b); // no crush + _z_bytes_t b = _z_bytes_null(); + assert(_z_bytes_len(&b) == 0); + assert(_z_bytes_is_empty(&b)); + assert(!_z_bytes_check(&b)); + assert(_z_bytes_num_slices(&b) == 0); + _z_bytes_drop(&b); // no crush } void test_slice(void) { uint8_t data[5] = {1, 2, 3, 4, 5}; uint8_t data_out[5] = {0}; _z_slice_t s = _z_slice_wrap_copy(data, 5); - _zz_bytes_t b; - _zz_bytes_from_slice(&b, s); + _z_bytes_t b; + _z_bytes_from_slice(&b, s); - assert(_zz_bytes_len(&b) == 5); - assert(!_zz_bytes_is_empty(&b)); - assert(_zz_bytes_check(&b)); - assert(_zz_bytes_num_slices(&b) == 1); - assert(_z_slice_eq(&_zz_bytes_get_slice(&b, 0)->slice.in->val, &s)); + assert(_z_bytes_len(&b) == 5); + assert(!_z_bytes_is_empty(&b)); + assert(_z_bytes_check(&b)); + assert(_z_bytes_num_slices(&b) == 1); + assert(_z_slice_eq(&_z_bytes_get_slice(&b, 0)->slice.in->val, &s)); - _zz_bytes_to_buf(&b, data_out, 5); + _z_bytes_to_buf(&b, data_out, 5); assert(memcmp(data, data_out, 5) == 0); - _zz_bytes_drop(&b); + _z_bytes_drop(&b); } void test_append(void) { @@ -59,24 +59,24 @@ void test_append(void) { _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_wrap_copy(data2, 5), 2, 3); _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); - _zz_bytes_t b = _zz_bytes_null(); + _z_bytes_t b = _z_bytes_null(); - _zz_bytes_append_slice(&b, &s1); - _zz_bytes_append_slice(&b, &s2); - _zz_bytes_append_slice(&b, &s3); + _z_bytes_append_slice(&b, &s1); + _z_bytes_append_slice(&b, &s2); + _z_bytes_append_slice(&b, &s3); - assert(_zz_bytes_len(&b) == 10); - assert(!_zz_bytes_is_empty(&b)); - assert(_zz_bytes_check(&b)); - assert(_zz_bytes_num_slices(&b) == 3); - assert(_z_slice_eq(&_zz_bytes_get_slice(&b, 0)->slice.in->val, &s1.slice.in->val)); - assert(_z_slice_eq(&_zz_bytes_get_slice(&b, 1)->slice.in->val, &s2.slice.in->val)); - assert(_z_slice_eq(&_zz_bytes_get_slice(&b, 2)->slice.in->val, &s3.slice.in->val)); + assert(_z_bytes_len(&b) == 10); + assert(!_z_bytes_is_empty(&b)); + assert(_z_bytes_check(&b)); + assert(_z_bytes_num_slices(&b) == 3); + assert(_z_slice_eq(&_z_bytes_get_slice(&b, 0)->slice.in->val, &s1.slice.in->val)); + assert(_z_slice_eq(&_z_bytes_get_slice(&b, 1)->slice.in->val, &s2.slice.in->val)); + assert(_z_slice_eq(&_z_bytes_get_slice(&b, 2)->slice.in->val, &s3.slice.in->val)); - _zz_bytes_to_buf(&b, data_out, 10); + _z_bytes_to_buf(&b, data_out, 10); assert(memcmp(data_in, data_out, 10) == 0); - _zz_bytes_drop(&b); + _z_bytes_drop(&b); } void test_reader_read(void) { @@ -89,29 +89,29 @@ void test_reader_read(void) { _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_wrap_copy(data2, 5), 2, 3); _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); - _zz_bytes_t b = _zz_bytes_null(); + _z_bytes_t b = _z_bytes_null(); - _zz_bytes_append_slice(&b, &s1); - _zz_bytes_append_slice(&b, &s2); - _zz_bytes_append_slice(&b, &s3); + _z_bytes_append_slice(&b, &s1); + _z_bytes_append_slice(&b, &s2); + _z_bytes_append_slice(&b, &s3); - _zz_bytes_reader_t reader = _zz_bytes_get_reader(&b); + _z_bytes_reader_t reader = _z_bytes_get_reader(&b); uint8_t out; - assert(_zz_bytes_reader_tell(&reader) == 0); - _zz_bytes_reader_read(&reader, &out, 1); - assert(_zz_bytes_reader_tell(&reader) == 1); + assert(_z_bytes_reader_tell(&reader) == 0); + _z_bytes_reader_read(&reader, &out, 1); + assert(_z_bytes_reader_tell(&reader) == 1); assert(out == 1); - _zz_bytes_reader_read(&reader, data_out, 3); - assert(_zz_bytes_reader_tell(&reader) == 4); + _z_bytes_reader_read(&reader, data_out, 3); + assert(_z_bytes_reader_tell(&reader) == 4); assert(memcmp(data_out, data_in + 1, 3) == 0); - _zz_bytes_reader_read(&reader, data_out, 6); - assert(_zz_bytes_reader_tell(&reader) == 10); + _z_bytes_reader_read(&reader, data_out, 6); + assert(_z_bytes_reader_tell(&reader) == 10); assert(memcmp(data_out, data_in + 4, 6) == 0); - _zz_bytes_drop(&b); + _z_bytes_drop(&b); } void test_reader_seek(void) { @@ -122,43 +122,43 @@ void test_reader_seek(void) { _z_arc_slice_t s2 = _z_arc_slice_wrap(_z_slice_wrap_copy(data2, 5), 2, 3); _z_arc_slice_t s3 = _z_arc_slice_wrap(_z_slice_wrap_copy(data3, 3), 1, 2); - _zz_bytes_t b = _zz_bytes_null(); - - _zz_bytes_append_slice(&b, &s1); - _zz_bytes_append_slice(&b, &s2); - _zz_bytes_append_slice(&b, &s3); - - _zz_bytes_reader_t reader = _zz_bytes_get_reader(&b); - - assert(_zz_bytes_reader_tell(&reader) == 0); - assert(_zz_bytes_reader_seek(&reader, 3, SEEK_CUR) == 0); - assert(_zz_bytes_reader_tell(&reader) == 3); - assert(_zz_bytes_reader_seek(&reader, 5, SEEK_CUR) == 0); - assert(_zz_bytes_reader_tell(&reader) == 8); - assert(_zz_bytes_reader_seek(&reader, 10, SEEK_CUR) != 0); - - assert(_zz_bytes_reader_seek(&reader, 0, SEEK_SET) == 0); - assert(_zz_bytes_reader_tell(&reader) == 0); - - assert(_zz_bytes_reader_seek(&reader, 3, SEEK_SET) == 0); - assert(_zz_bytes_reader_tell(&reader) == 3); - assert(_zz_bytes_reader_seek(&reader, 8, SEEK_SET) == 0); - assert(_zz_bytes_reader_tell(&reader) == 8); - assert(_zz_bytes_reader_seek(&reader, 20, SEEK_SET) != 0); - assert(_zz_bytes_reader_seek(&reader, -20, SEEK_SET) != 0); - - assert(_zz_bytes_reader_seek(&reader, 0, SEEK_END) == 0); - assert(_zz_bytes_reader_tell(&reader) == 10); - assert(_zz_bytes_reader_seek(&reader, -3, SEEK_END) == 0); - assert(_zz_bytes_reader_tell(&reader) == 7); - assert(_zz_bytes_reader_seek(&reader, -8, SEEK_END) == 0); - assert(_zz_bytes_reader_tell(&reader) == 2); - assert(_zz_bytes_reader_seek(&reader, -10, SEEK_END) == 0); - assert(_zz_bytes_reader_tell(&reader) == 0); - assert(_zz_bytes_reader_seek(&reader, -20, SEEK_END) != 0); - assert(_zz_bytes_reader_seek(&reader, 5, SEEK_END) != 0); - - _zz_bytes_drop(&b); + _z_bytes_t b = _z_bytes_null(); + + _z_bytes_append_slice(&b, &s1); + _z_bytes_append_slice(&b, &s2); + _z_bytes_append_slice(&b, &s3); + + _z_bytes_reader_t reader = _z_bytes_get_reader(&b); + + assert(_z_bytes_reader_tell(&reader) == 0); + assert(_z_bytes_reader_seek(&reader, 3, SEEK_CUR) == 0); + assert(_z_bytes_reader_tell(&reader) == 3); + assert(_z_bytes_reader_seek(&reader, 5, SEEK_CUR) == 0); + assert(_z_bytes_reader_tell(&reader) == 8); + assert(_z_bytes_reader_seek(&reader, 10, SEEK_CUR) != 0); + + assert(_z_bytes_reader_seek(&reader, 0, SEEK_SET) == 0); + assert(_z_bytes_reader_tell(&reader) == 0); + + assert(_z_bytes_reader_seek(&reader, 3, SEEK_SET) == 0); + assert(_z_bytes_reader_tell(&reader) == 3); + assert(_z_bytes_reader_seek(&reader, 8, SEEK_SET) == 0); + assert(_z_bytes_reader_tell(&reader) == 8); + assert(_z_bytes_reader_seek(&reader, 20, SEEK_SET) != 0); + assert(_z_bytes_reader_seek(&reader, -20, SEEK_SET) != 0); + + assert(_z_bytes_reader_seek(&reader, 0, SEEK_END) == 0); + assert(_z_bytes_reader_tell(&reader) == 10); + assert(_z_bytes_reader_seek(&reader, -3, SEEK_END) == 0); + assert(_z_bytes_reader_tell(&reader) == 7); + assert(_z_bytes_reader_seek(&reader, -8, SEEK_END) == 0); + assert(_z_bytes_reader_tell(&reader) == 2); + assert(_z_bytes_reader_seek(&reader, -10, SEEK_END) == 0); + assert(_z_bytes_reader_tell(&reader) == 0); + assert(_z_bytes_reader_seek(&reader, -20, SEEK_END) != 0); + assert(_z_bytes_reader_seek(&reader, 5, SEEK_END) != 0); + + _z_bytes_drop(&b); } int main(void) { diff --git a/tests/z_channels_test.c b/tests/z_channels_test.c index 5bfef17a9..3c6702d84 100644 --- a/tests/z_channels_test.c +++ b/tests/z_channels_test.c @@ -25,8 +25,8 @@ #define SEND(closure, v) \ do { \ - _zz_bytes_t payload; \ - _zz_bytes_from_slice(&payload, (_z_slice_t){.start = (const uint8_t *)v, .len = strlen(v)}); \ + _z_bytes_t payload; \ + _z_bytes_from_slice(&payload, (_z_slice_t){.start = (const uint8_t *)v, .len = strlen(v)}); \ _z_sample_t s = {.keyexpr = _z_rname("key"), \ .payload = payload, \ .timestamp = _z_timestamp_null(), \ diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 9229783b0..d6720e7fe 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -185,18 +185,18 @@ _z_slice_t gen_slice(size_t len) { return arr; } -_zz_bytes_t gen_payload(size_t len) { +_z_bytes_t gen_payload(size_t len) { _z_slice_t pld = gen_slice(len); - _zz_bytes_t b; - _zz_bytes_from_slice(&b, pld); + _z_bytes_t b; + _z_bytes_from_slice(&b, pld); return b; } -_zz_bytes_t gen_bytes(size_t len) { +_z_bytes_t gen_bytes(size_t len) { _z_slice_t s = gen_slice(len); - _zz_bytes_t b; - _zz_bytes_from_slice(&b, s); + _z_bytes_t b; + _z_bytes_from_slice(&b, s); return b; } @@ -260,7 +260,7 @@ _z_value_t gen_value(void) { _z_value_t val; val.encoding = gen_encoding(); if (gen_bool()) { - val.payload = _zz_bytes_null(); + val.payload = _z_bytes_null(); } else { val.payload = gen_bytes(16); } @@ -507,20 +507,20 @@ void zbuf_extension(void) { /*------------------ Payload field ------------------*/ void assert_eq_slice(const _z_slice_t *left, const _z_slice_t *right) { assert_eq_uint8_array(left, right); } -void assert_eq_bytes(const _zz_bytes_t *left, const _zz_bytes_t *right) { - size_t len_left = _zz_bytes_len(left); - size_t len_right = _zz_bytes_len(right); +void assert_eq_bytes(const _z_bytes_t *left, const _z_bytes_t *right) { + size_t len_left = _z_bytes_len(left); + size_t len_right = _z_bytes_len(right); printf("Array -> "); printf("Length (%zu:%zu), ", len_left, len_right); assert(len_left == len_right); printf("Content ("); - _zz_bytes_reader_t reader_left = _zz_bytes_get_reader(left); - _zz_bytes_reader_t reader_right = _zz_bytes_get_reader(right); + _z_bytes_reader_t reader_left = _z_bytes_get_reader(left); + _z_bytes_reader_t reader_right = _z_bytes_get_reader(right); for (size_t i = 0; i < len_left; i++) { uint8_t l = 0, r = 0; - _zz_bytes_reader_read(&reader_left, &l, 1); - _zz_bytes_reader_read(&reader_right, &r, 1); + _z_bytes_reader_read(&reader_left, &l, 1); + _z_bytes_reader_read(&reader_right, &r, 1); printf("%02x:%02x", l, r); if (i < len_left - 1) printf(" "); @@ -535,26 +535,26 @@ void payload_field(void) { _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); // Initialize - _zz_bytes_t e_pld = gen_payload(64); + _z_bytes_t e_pld = gen_payload(64); // Encode - int8_t res = _zz_bytes_encode(&wbf, &e_pld); + int8_t res = _z_bytes_encode(&wbf, &e_pld); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _zz_bytes_t d_pld; - res = _zz_bytes_decode(&d_pld, &zbf); + _z_bytes_t d_pld; + res = _z_bytes_decode(&d_pld, &zbf); assert(res == _Z_RES_OK); printf(" "); assert_eq_bytes(&e_pld, &d_pld); printf("\n"); // Free - _zz_bytes_drop(&e_pld); - _zz_bytes_drop(&d_pld); + _z_bytes_drop(&e_pld); + _z_bytes_drop(&d_pld); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } From f04f70299004c6f85eb12e24312399b2450a750e Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Mon, 24 Jun 2024 18:35:18 +0200 Subject: [PATCH 07/25] merge --- include/zenoh-pico/transport/common/rx.h | 1 + include/zenoh-pico/utils/endianness.h | 51 ++++++++ src/api/api.c | 1 + src/collections/bytes.c | 39 ++++-- src/collections/slice.c | 1 + src/protocol/codec.c | 44 ++++--- src/transport/common/rx.c | 13 +- src/transport/common/tx.c | 7 +- src/transport/multicast/read.c | 9 +- src/transport/multicast/rx.c | 7 +- src/transport/unicast/read.c | 9 +- src/transport/unicast/rx.c | 7 +- src/utils/endianness.c | 146 +++++++++++++++++++++++ 13 files changed, 289 insertions(+), 46 deletions(-) create mode 100644 include/zenoh-pico/utils/endianness.h create mode 100644 src/utils/endianness.c diff --git a/include/zenoh-pico/transport/common/rx.h b/include/zenoh-pico/transport/common/rx.h index 950f9dcb1..11e6cc1fe 100644 --- a/include/zenoh-pico/transport/common/rx.h +++ b/include/zenoh-pico/transport/common/rx.h @@ -19,6 +19,7 @@ #include "zenoh-pico/transport/transport.h" /*------------------ Transmission and Reception helpers ------------------*/ +size_t _z_read_stream_size(_z_zbuf_t *zbuf); int8_t _z_link_recv_t_msg(_z_transport_message_t *t_msg, const _z_link_t *zl); #endif /* ZENOH_PICO_TRANSPORT_RX_H */ diff --git a/include/zenoh-pico/utils/endianness.h b/include/zenoh-pico/utils/endianness.h new file mode 100644 index 000000000..85436070d --- /dev/null +++ b/include/zenoh-pico/utils/endianness.h @@ -0,0 +1,51 @@ +// +// Copyright (c) 2022 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#ifndef ZENOH_PICO_UTILS_ENDIANNESS_H +#define ZENOH_PICO_UTILS_ENDIANNESS_H + +#include +#include + +// Load int from memory with specified endianness +uint16_t _z_le_load16(const uint8_t *src); +uint32_t _z_le_load32(const uint8_t *src); +uint64_t _z_le_load64(const uint8_t *src); +uint16_t _z_be_load16(const uint8_t *src); +uint32_t _z_be_load32(const uint8_t *src); +uint64_t _z_be_load64(const uint8_t *src); + +// Store int to memory with specified endianness +void _z_le_store16(const uint16_t val, uint8_t *dest); +void _z_le_store32(const uint32_t val, uint8_t *dest); +void _z_le_store64(const uint64_t val, uint8_t *dest); +void _z_be_store16(const uint16_t val, uint8_t *dest); +void _z_be_store32(const uint32_t val, uint8_t *dest); +void _z_be_store64(const uint64_t val, uint8_t *dest); + +// Load little-endian int from memory to host order +uint16_t _z_host_le_load16(const uint8_t *src); +uint32_t _z_host_le_load32(const uint8_t *src); +uint64_t _z_host_le_load64(const uint8_t *src); + +// Store little-endian int to memory from host order +void _z_host_le_store16(const uint16_t val, uint8_t *dst); +void _z_host_le_store32(const uint32_t val, uint8_t *dst); +void _z_host_le_store64(const uint64_t val, uint8_t *dst); + +// Return u16 individual bytes +uint8_t _z_get_u16_lsb(uint_fast16_t val); +uint8_t _z_get_u16_msb(uint_fast16_t val); + +#endif /* ZENOH_PICO_UTILS_ENDIANNESS_H */ diff --git a/src/api/api.c b/src/api/api.c index 4f18e2656..8a5c779e4 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -38,6 +38,7 @@ #include "zenoh-pico/system/platform.h" #include "zenoh-pico/transport/multicast.h" #include "zenoh-pico/transport/unicast.h" +#include "zenoh-pico/utils/endianness.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" #include "zenoh-pico/utils/uuid.h" diff --git a/src/collections/bytes.c b/src/collections/bytes.c index 8bdc3fa96..dd04e491a 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -21,6 +21,7 @@ #include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/system/platform.h" #include "zenoh-pico/utils/result.h" +#include "zenoh-pico/utils/endianness.h" /*-------- Bytes --------*/ _Bool _z_bytes_check(const _z_bytes_t *bytes) { return !_z_bytes_is_empty(bytes); } @@ -202,20 +203,34 @@ int8_t _z_bytes_to_uint8(const _z_bytes_t *bs, uint8_t *val) { return _z_bytes_to_buf(bs, val, sizeof(uint8_t)) == sizeof(uint8_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } -// FIXME: int16+ endianness, Issue #420 int8_t _z_bytes_to_uint16(const _z_bytes_t *bs, uint16_t *val) { *val = 0; - return _z_bytes_to_buf(bs, (uint8_t *)val, sizeof(uint16_t)) == sizeof(uint16_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; + uint8_t buf[sizeof(uint16_t)]; + if (_z_bytes_to_buf(bs, buf, sizeof(uint16_t)) != sizeof(uint16_t)) { + return _Z_ERR_DID_NOT_READ; + } + *val = _z_host_le_load16(buf); + return _Z_RES_OK; } int8_t _z_bytes_to_uint32(const _z_bytes_t *bs, uint32_t *val) { *val = 0; - return _z_bytes_to_buf(bs, (uint8_t *)val, sizeof(uint32_t)) == sizeof(uint32_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; + uint8_t buf[sizeof(uint32_t)]; + if (_z_bytes_to_buf(bs, buf, sizeof(uint32_t)) != sizeof(uint32_t)) { + return _Z_ERR_DID_NOT_READ; + } + *val = _z_host_le_load32(buf); + return _Z_RES_OK; } int8_t _z_bytes_to_uint64(const _z_bytes_t *bs, uint64_t *val) { *val = 0; - return _z_bytes_to_buf(bs, (uint8_t *)val, sizeof(uint64_t)) == sizeof(uint64_t) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; + uint8_t buf[sizeof(uint64_t)]; + if (_z_bytes_to_buf(bs, buf, sizeof(uint64_t)) != sizeof(uint64_t)) { + return _Z_ERR_DID_NOT_READ; + } + *val = _z_host_le_load64(buf); + return _Z_RES_OK; } int8_t _z_bytes_to_float(const _z_bytes_t *bs, float *val) { @@ -228,18 +243,26 @@ int8_t _z_bytes_to_double(const _z_bytes_t *bs, double *val) { return _z_bytes_to_buf(bs, (uint8_t *)val, sizeof(double)) == sizeof(double) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } -int8_t _z_bytes_from_uint8(_z_bytes_t *b, uint8_t val) { return _z_bytes_from_buf(b, &val, sizeof(val)); } +int8_t _z_bytes_from_uint8(_z_bytes_t *b, uint8_t val) { + return _z_bytes_from_buf(b, &val, sizeof(val)); +} int8_t _z_bytes_from_uint16(_z_bytes_t *b, uint16_t val) { - return _z_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); + uint8_t buf[sizeof(uint16_t)]; + _z_host_le_store16(val, buf); + return _z_bytes_from_buf(b, buf, sizeof(uint16_t)); } int8_t _z_bytes_from_uint32(_z_bytes_t *b, uint32_t val) { - return _z_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); + uint8_t buf[sizeof(uint32_t)]; + _z_host_le_store32(val, buf); + return _z_bytes_from_buf(b, buf, sizeof(uint32_t)); } int8_t _z_bytes_from_uint64(_z_bytes_t *b, uint64_t val) { - return _z_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); + uint8_t buf[sizeof(uint64_t)]; + _z_host_le_store64(val, buf); + return _z_bytes_from_buf(b, buf, sizeof(uint64_t)); } int8_t _z_bytes_from_float(_z_bytes_t *b, float val) { return _z_bytes_from_buf(b, (uint8_t *)&val, sizeof(val)); } diff --git a/src/collections/slice.c b/src/collections/slice.c index 97b97746e..1a059a8cb 100644 --- a/src/collections/slice.c +++ b/src/collections/slice.c @@ -18,6 +18,7 @@ #include #include "zenoh-pico/system/platform.h" +#include "zenoh-pico/utils/endianness.h" #include "zenoh-pico/utils/result.h" /*-------- Slice --------*/ diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 7b4d3efa3..ee159c192 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -17,6 +17,7 @@ #include #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/utils/endianness.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" @@ -76,44 +77,52 @@ int8_t _z_uint8_decode(uint8_t *u8, _z_zbuf_t *zbf) { int8_t _z_uint16_encode(_z_wbuf_t *wbf, uint16_t u16) { int8_t ret = _Z_RES_OK; - ret |= _z_wbuf_write(wbf, (u16 & 0xFFU)); - ret |= _z_wbuf_write(wbf, ((u16 >> 8) & 0xFFU)); + ret |= _z_wbuf_write(wbf, _z_get_u16_lsb(u16)); + ret |= _z_wbuf_write(wbf, _z_get_u16_msb(u16)); return ret; } int8_t _z_uint16_decode(uint16_t *u16, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; - *u16 = 0; + uint8_t enc_u16[2]; - uint8_t u8; - ret |= _z_uint8_decode(&u8, zbf); - *u16 |= u8; - ret |= _z_uint8_decode(&u8, zbf); - *u16 |= u8 << 8; + ret |= _z_uint8_decode(&enc_u16[0], zbf); + ret |= _z_uint8_decode(&enc_u16[1], zbf); + *u16 = _z_host_le_load16(enc_u16); return ret; } /*------------------ z_zint ------------------*/ +// Zint is a variable int composed of up to 9 bytes. +// The msb of the 8 first bytes has meaning: (1: the zint continue, 0: end of the zint) #define VLE_LEN 9 +#define VLE_LEN1_MASK (UINT64_MAX << (7 * 1)) +#define VLE_LEN2_MASK (UINT64_MAX << (7 * 2)) +#define VLE_LEN3_MASK (UINT64_MAX << (7 * 3)) +#define VLE_LEN4_MASK (UINT64_MAX << (7 * 4)) +#define VLE_LEN5_MASK (UINT64_MAX << (7 * 5)) +#define VLE_LEN6_MASK (UINT64_MAX << (7 * 6)) +#define VLE_LEN7_MASK (UINT64_MAX << (7 * 7)) +#define VLE_LEN8_MASK (UINT64_MAX << (7 * 8)) uint8_t _z_zint_len(uint64_t v) { - if ((v & (UINT64_MAX << (7 * 1))) == 0) { + if ((v & VLE_LEN1_MASK) == 0) { return 1; - } else if ((v & (UINT64_MAX << (7 * 2))) == 0) { + } else if ((v & VLE_LEN2_MASK) == 0) { return 2; - } else if ((v & (UINT64_MAX << (7 * 3))) == 0) { + } else if ((v & VLE_LEN3_MASK) == 0) { return 3; - } else if ((v & (UINT64_MAX << (7 * 4))) == 0) { + } else if ((v & VLE_LEN4_MASK) == 0) { return 4; - } else if ((v & (UINT64_MAX << (7 * 5))) == 0) { + } else if ((v & VLE_LEN5_MASK) == 0) { return 5; - } else if ((v & (UINT64_MAX << (7 * 6))) == 0) { + } else if ((v & VLE_LEN6_MASK) == 0) { return 6; - } else if ((v & (UINT64_MAX << (7 * 7))) == 0) { + } else if ((v & VLE_LEN7_MASK) == 0) { return 7; - } else if ((v & (UINT64_MAX << (7 * 8))) == 0) { + } else if ((v & VLE_LEN8_MASK) == 0) { return 8; } else { return 9; @@ -124,7 +133,7 @@ uint8_t _z_zint64_encode_buf(uint8_t *buf, uint64_t v) { uint64_t lv = v; uint8_t len = 0; size_t start = 0; - while ((lv & (uint64_t)~0x7f) != 0) { + while ((lv & VLE_LEN1_MASK) != 0) { uint8_t c = (lv & 0x7f) | 0x80; buf[start++] = c; len++; @@ -145,7 +154,6 @@ int8_t _z_zint64_encode(_z_wbuf_t *wbf, uint64_t v) { for (size_t i = 0; i < len; i++) { _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, buf[i])); } - return _Z_RES_OK; } diff --git a/src/transport/common/rx.c b/src/transport/common/rx.c index 8f25fff0f..fd8343f8b 100644 --- a/src/transport/common/rx.c +++ b/src/transport/common/rx.c @@ -12,15 +12,26 @@ // ZettaScale Zenoh Team, // -#include "zenoh-pico/transport/multicast/rx.h" +#include "zenoh-pico/transport/common/rx.h" #include #include "zenoh-pico/protocol/codec/transport.h" +#include "zenoh-pico/transport/multicast/rx.h" #include "zenoh-pico/transport/unicast/rx.h" +#include "zenoh-pico/utils/endianness.h" #include "zenoh-pico/utils/logging.h" /*------------------ Reception helper ------------------*/ +size_t _z_read_stream_size(_z_zbuf_t *zbuf) { + uint8_t stream_size[_Z_MSG_LEN_ENC_SIZE]; + // Read the bytes from stream + for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) { + stream_size[i] = _z_zbuf_read(zbuf); + } + return _z_host_le_load16(stream_size); +} + int8_t _z_link_recv_t_msg(_z_transport_message_t *t_msg, const _z_link_t *zl) { int8_t ret = _Z_RES_OK; diff --git a/src/transport/common/tx.c b/src/transport/common/tx.c index 3508493f4..15020cbe3 100644 --- a/src/transport/common/tx.c +++ b/src/transport/common/tx.c @@ -21,6 +21,7 @@ #include "zenoh-pico/transport/multicast/tx.h" #include "zenoh-pico/transport/raweth/tx.h" #include "zenoh-pico/transport/unicast/tx.h" +#include "zenoh-pico/utils/endianness.h" #include "zenoh-pico/utils/logging.h" /*------------------ Transmission helper ------------------*/ @@ -57,9 +58,9 @@ void __unsafe_z_finalize_wbuf(_z_wbuf_t *buf, uint8_t link_flow_capability) { // Stream capable links case Z_LINK_CAP_FLOW_STREAM: { size_t len = _z_wbuf_len(buf) - _Z_MSG_LEN_ENC_SIZE; - for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) { - _z_wbuf_put(buf, (uint8_t)((len >> (uint8_t)8 * i) & (uint8_t)0xFF), i); - } + // Encode the u16 size as little endian + _z_wbuf_put(buf, _z_get_u16_lsb((uint_fast16_t)len), 0); + _z_wbuf_put(buf, _z_get_u16_msb((uint_fast16_t)len), 1); break; } // Datagram capable links diff --git a/src/transport/multicast/read.c b/src/transport/multicast/read.c index 1082a579a..b4de50730 100644 --- a/src/transport/multicast/read.c +++ b/src/transport/multicast/read.c @@ -19,6 +19,7 @@ #include "zenoh-pico/config.h" #include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/protocol/iobuf.h" +#include "zenoh-pico/transport/common/rx.h" #include "zenoh-pico/transport/multicast/rx.h" #include "zenoh-pico/transport/unicast/rx.h" #include "zenoh-pico/utils/logging.h" @@ -71,11 +72,9 @@ void *_zp_multicast_read_task(void *ztm_arg) { continue; } } - - for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) { - to_read |= _z_zbuf_read(&ztm->_zbuf) << (i * (uint8_t)8); - } - + // Get stream size + to_read = _z_read_stream_size(&ztm->_zbuf); + // Read data if (_z_zbuf_len(&ztm->_zbuf) < to_read) { _z_link_recv_zbuf(&ztm->_link, &ztm->_zbuf, NULL); if (_z_zbuf_len(&ztm->_zbuf) < to_read) { diff --git a/src/transport/multicast/rx.c b/src/transport/multicast/rx.c index ffcbf0701..6e9c5a098 100644 --- a/src/transport/multicast/rx.c +++ b/src/transport/multicast/rx.c @@ -24,6 +24,7 @@ #include "zenoh-pico/protocol/definitions/transport.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/session/utils.h" +#include "zenoh-pico/transport/common/rx.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" @@ -50,9 +51,9 @@ static int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_trans break; } } - for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) { - to_read |= _z_zbuf_read(&ztm->_zbuf) << (i * (uint8_t)8); - } + // Get stream size + to_read = _z_read_stream_size(&ztm->_zbuf); + // Read data if (_z_zbuf_len(&ztm->_zbuf) < to_read) { _z_link_recv_zbuf(&ztm->_link, &ztm->_zbuf, addr); if (_z_zbuf_len(&ztm->_zbuf) < to_read) { diff --git a/src/transport/unicast/read.c b/src/transport/unicast/read.c index 80e35180c..eb19fca2c 100644 --- a/src/transport/unicast/read.c +++ b/src/transport/unicast/read.c @@ -18,6 +18,7 @@ #include "zenoh-pico/config.h" #include "zenoh-pico/protocol/codec/transport.h" +#include "zenoh-pico/transport/common/rx.h" #include "zenoh-pico/transport/unicast/rx.h" #include "zenoh-pico/utils/logging.h" @@ -66,11 +67,9 @@ void *_zp_unicast_read_task(void *ztu_arg) { continue; } } - - for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) { - to_read |= _z_zbuf_read(&ztu->_zbuf) << (i * (uint8_t)8); - } - + // Get stream size + to_read = _z_read_stream_size(&ztu->_zbuf); + // Read data if (_z_zbuf_len(&ztu->_zbuf) < to_read) { _z_link_recv_zbuf(&ztu->_link, &ztu->_zbuf, NULL); if (_z_zbuf_len(&ztu->_zbuf) < to_read) { diff --git a/src/transport/unicast/rx.c b/src/transport/unicast/rx.c index 33132b3ca..d8049f79d 100644 --- a/src/transport/unicast/rx.c +++ b/src/transport/unicast/rx.c @@ -22,6 +22,7 @@ #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/session/utils.h" +#include "zenoh-pico/transport/common/rx.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" @@ -48,9 +49,9 @@ int8_t _z_unicast_recv_t_msg_na(_z_transport_unicast_t *ztu, _z_transport_messag continue; } } - for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) { - to_read |= _z_zbuf_read(&ztu->_zbuf) << (i * (uint8_t)8); - } + // Get stream size + to_read = _z_read_stream_size(&ztu->_zbuf); + // Read data if (_z_zbuf_len(&ztu->_zbuf) < to_read) { _z_link_recv_zbuf(&ztu->_link, &ztu->_zbuf, NULL); if (_z_zbuf_len(&ztu->_zbuf) < to_read) { diff --git a/src/utils/endianness.c b/src/utils/endianness.c new file mode 100644 index 000000000..8085a3117 --- /dev/null +++ b/src/utils/endianness.c @@ -0,0 +1,146 @@ +// +// Copyright (c) 2024 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#include "zenoh-pico/utils/endianness.h" + +#if !defined(ZENOH_ENDIANNNESS_BIG) && !defined(ZENOH_ENDIANNNESS_LITTLE) +// Gcc/clang test +#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +#define ZENOH_ENDIANNNESS_BIG +#else +#define ZENOH_ENDIANNNESS_LITTLE +#endif +#endif + +// *** Little endian *** +uint16_t _z_le_load16(const uint8_t *src) { return (uint16_t)(src[0] << 0) | (uint16_t)(src[1] << 8); } + +uint32_t _z_le_load32(const uint8_t *src) { + return ((uint32_t)src[0] << 0) | ((uint32_t)src[1] << 8) | ((uint32_t)src[2] << 16) | ((uint32_t)src[3] << 24); +} + +uint64_t _z_le_load64(const uint8_t *src) { + return ((uint64_t)src[0] << 0) | ((uint64_t)src[1] << 8) | ((uint64_t)src[2] << 16) | ((uint64_t)src[3] << 24) | + ((uint64_t)src[4] << 32) | ((uint64_t)src[5] << 40) | ((uint64_t)src[6] << 48) | ((uint64_t)src[7] << 56); +} + +void _z_le_store16(const uint16_t val, uint8_t *dst) { + dst[0] = (uint8_t)(val >> 0); + dst[1] = (uint8_t)(val >> 8); +} + +void _z_le_store32(const uint32_t val, uint8_t *dst) { + dst[0] = (uint8_t)(val >> 0); + dst[1] = (uint8_t)(val >> 8); + dst[2] = (uint8_t)(val >> 16); + dst[3] = (uint8_t)(val >> 24); +} + +void _z_le_store64(const uint64_t val, uint8_t *dst) { + dst[0] = (uint8_t)(val >> 0); + dst[1] = (uint8_t)(val >> 8); + dst[2] = (uint8_t)(val >> 16); + dst[3] = (uint8_t)(val >> 24); + dst[4] = (uint8_t)(val >> 32); + dst[5] = (uint8_t)(val >> 40); + dst[6] = (uint8_t)(val >> 48); + dst[7] = (uint8_t)(val >> 56); +} + +// *** Big endian *** +uint16_t _z_be_load16(const uint8_t *src) { return (uint16_t)(src[0] << 8) | (uint16_t)(src[1] << 0); } + +uint32_t _z_be_load32(const uint8_t *src) { + return ((uint32_t)src[0] << 24) | ((uint32_t)src[1] << 16) | ((uint32_t)src[2] << 8) | ((uint32_t)src[3] << 0); +} + +uint64_t _z_be_load64(const uint8_t *src) { + return ((uint64_t)src[0] << 56) | ((uint64_t)src[1] << 48) | ((uint64_t)src[2] << 40) | ((uint64_t)src[3] << 32) | + ((uint64_t)src[4] << 24) | ((uint64_t)src[5] << 16) | ((uint64_t)src[6] << 8) | ((uint64_t)src[7] << 0); +} + +void _z_be_store16(const uint16_t val, uint8_t *dst) { + dst[0] = (uint8_t)(val >> 8); + dst[1] = (uint8_t)(val >> 0); +} + +void _z_be_store32(const uint32_t val, uint8_t *dst) { + dst[0] = (uint8_t)(val >> 24); + dst[1] = (uint8_t)(val >> 16); + dst[2] = (uint8_t)(val >> 8); + dst[3] = (uint8_t)(val >> 0); +} + +void _z_be_store64(const uint64_t val, uint8_t *dst) { + dst[0] = (uint8_t)(val >> 56); + dst[1] = (uint8_t)(val >> 48); + dst[2] = (uint8_t)(val >> 40); + dst[3] = (uint8_t)(val >> 32); + dst[4] = (uint8_t)(val >> 24); + dst[5] = (uint8_t)(val >> 16); + dst[6] = (uint8_t)(val >> 8); + dst[7] = (uint8_t)(val >> 0); +} + +// *** Host *** +uint16_t _z_host_le_load16(const uint8_t *src) { +#if defined(ZENOH_ENDIANNNESS_BIG) + return _z_be_load16(src); +#elif defined(ZENOH_ENDIANNNESS_LITTLE) + return _z_le_load16(src); +#endif +} + +uint32_t _z_host_le_load32(const uint8_t *src) { +#if defined(ZENOH_ENDIANNNESS_BIG) + return _z_be_load32(src); +#elif defined(ZENOH_ENDIANNNESS_LITTLE) + return _z_le_load32(src); +#endif +} + +uint64_t _z_host_le_load64(const uint8_t *src) { +#if defined(ZENOH_ENDIANNNESS_BIG) + return _z_be_load64(src); +#elif defined(ZENOH_ENDIANNNESS_LITTLE) + return _z_le_load64(src); +#endif +} + +void _z_host_le_store16(const uint16_t val, uint8_t *dst) { +#if defined(ZENOH_ENDIANNNESS_BIG) + _z_be_store16(val, dst); +#elif defined(ZENOH_ENDIANNNESS_LITTLE) + _z_le_store16(val, dst); +#endif +} +void _z_host_le_store32(const uint32_t val, uint8_t *dst) { +#if defined(ZENOH_ENDIANNNESS_BIG) + _z_be_store32(val, dst); +#elif defined(ZENOH_ENDIANNNESS_LITTLE) + _z_le_store32(val, dst); +#endif +} + +void _z_host_le_store64(const uint64_t val, uint8_t *dst) { +#if defined(ZENOH_ENDIANNNESS_BIG) + _z_be_store64(val, dst); +#elif defined(ZENOH_ENDIANNNESS_LITTLE) + _z_le_store64(val, dst); +#endif +} + +uint8_t _z_get_u16_lsb(uint_fast16_t val) { return (uint8_t)(val >> 0); } + +uint8_t _z_get_u16_msb(uint_fast16_t val) { return (uint8_t)(val >> 8); } From efdad63bc0b9480248dcd5af2d996d1f1a99e929 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Mon, 24 Jun 2024 19:18:17 +0200 Subject: [PATCH 08/25] fmt --- src/collections/bytes.c | 10 +++------- src/protocol/codec/message.c | 1 + tests/z_channels_test.c | 20 ++++++++++---------- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/collections/bytes.c b/src/collections/bytes.c index dd04e491a..dfd8d0664 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -20,8 +20,8 @@ #include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/system/platform.h" -#include "zenoh-pico/utils/result.h" #include "zenoh-pico/utils/endianness.h" +#include "zenoh-pico/utils/result.h" /*-------- Bytes --------*/ _Bool _z_bytes_check(const _z_bytes_t *bytes) { return !_z_bytes_is_empty(bytes); } @@ -132,9 +132,7 @@ int8_t _z_bytes_to_slice(const _z_bytes_t *bytes, _z_slice_t *s) { return _Z_RES_OK; } -int8_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s) { - return _z_arc_slice_svec_append(&dst->_slices, s); -} +int8_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s) { return _z_arc_slice_svec_append(&dst->_slices, s); } int8_t _z_bytes_append_inner(_z_bytes_t *dst, _z_bytes_t *src) { _Bool success = true; @@ -243,9 +241,7 @@ int8_t _z_bytes_to_double(const _z_bytes_t *bs, double *val) { return _z_bytes_to_buf(bs, (uint8_t *)val, sizeof(double)) == sizeof(double) ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } -int8_t _z_bytes_from_uint8(_z_bytes_t *b, uint8_t val) { - return _z_bytes_from_buf(b, &val, sizeof(val)); -} +int8_t _z_bytes_from_uint8(_z_bytes_t *b, uint8_t val) { return _z_bytes_from_buf(b, &val, sizeof(val)); } int8_t _z_bytes_from_uint16(_z_bytes_t *b, uint16_t val) { uint8_t buf[sizeof(uint16_t)]; diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index e78fe27d4..90688369d 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -313,6 +313,7 @@ int8_t _z_push_body_decode_extensions(_z_msg_ext_t *extension, void *ctx) { _Z_RETURN_IF_ERR(_z_slice_copy(&s, &extension->_body._zbuf._val)); } ret = _z_bytes_from_slice(&pshb->_body._put._attachment, s); + break; } default: if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { diff --git a/tests/z_channels_test.c b/tests/z_channels_test.c index 3c6702d84..aff99a581 100644 --- a/tests/z_channels_test.c +++ b/tests/z_channels_test.c @@ -23,18 +23,18 @@ #undef NDEBUG #include -#define SEND(closure, v) \ - do { \ +#define SEND(closure, v) \ + do { \ _z_bytes_t payload; \ _z_bytes_from_slice(&payload, (_z_slice_t){.start = (const uint8_t *)v, .len = strlen(v)}); \ - _z_sample_t s = {.keyexpr = _z_rname("key"), \ - .payload = payload, \ - .timestamp = _z_timestamp_null(), \ - .encoding = _z_encoding_null(), \ - .kind = 0, \ - .qos = {0}}; \ - z_loaned_sample_t sample = _z_sample_rc_new_from_val(s); \ - z_call(closure, &sample); \ + _z_sample_t s = {.keyexpr = _z_rname("key"), \ + .payload = payload, \ + .timestamp = _z_timestamp_null(), \ + .encoding = _z_encoding_null(), \ + .kind = 0, \ + .qos = {0}}; \ + z_loaned_sample_t sample = _z_sample_rc_new_from_val(s); \ + z_call(closure, &sample); \ } while (0); #define _RECV(handler, method, buf) \ From b175b3537b91ff2b44cb0acce0ebcaf930c465c4 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Tue, 25 Jun 2024 10:46:56 +0200 Subject: [PATCH 09/25] fixes --- include/zenoh-pico/collections/bytes.h | 2 +- src/collections/arc_slice.c | 6 ++++-- src/collections/bytes.c | 18 +++++++++--------- src/net/query.c | 2 +- src/net/reply.c | 8 ++++---- src/protocol/codec.c | 4 ++-- src/protocol/codec/message.c | 5 +++-- tests/z_bytes_test.c | 4 ++-- 8 files changed, 26 insertions(+), 23 deletions(-) diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index 878396d82..82a09aa64 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -48,7 +48,7 @@ typedef struct { _Bool _z_bytes_check(const _z_bytes_t *bytes); _z_bytes_t _z_bytes_null(void); int8_t _z_bytes_append(_z_bytes_t *dst, _z_bytes_t *src); -int8_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s); +_Bool _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s); int8_t _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); diff --git a/src/collections/arc_slice.c b/src/collections/arc_slice.c index b22804f8b..663cb0ae6 100644 --- a/src/collections/arc_slice.c +++ b/src/collections/arc_slice.c @@ -42,12 +42,14 @@ _z_arc_slice_t _z_arc_slice_get_subslice(const _z_arc_slice_t* s, size_t offset, assert(s->slice.in != NULL || (len == 0 && offset == 0)); _z_arc_slice_t out; - out.len = len; - out.start = offset; if (s->slice.in == NULL) { out.slice.in = NULL; + out.start = 0; + out.len = 0; } else { out.slice = _z_slice_rc_clone(&s->slice); + out.len = len; + out.start = out.start + offset; } return out; } diff --git a/src/collections/bytes.c b/src/collections/bytes.c index dfd8d0664..03c112e47 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -86,18 +86,18 @@ void _z_bytes_free(_z_bytes_t **bs) { size_t _z_bytes_to_buf(const _z_bytes_t *bytes, uint8_t *dst, size_t len) { uint8_t *start = dst; - - for (size_t i = 0; i < _z_bytes_num_slices(bytes) && len > 0; ++i) { + size_t remaining = len; + for (size_t i = 0; i < _z_bytes_num_slices(bytes) && remaining > 0; ++i) { // Recopy data _z_arc_slice_t *s = _z_bytes_get_slice(bytes, i); size_t s_len = _z_arc_slice_len(s); - size_t len_to_copy = len >= s_len ? s_len : len; + size_t len_to_copy = remaining >= s_len ? s_len : remaining; memcpy(start, _z_arc_slice_data(s), len_to_copy); start += s_len; - len -= len_to_copy; + remaining -= len_to_copy; } - return len; + return len - remaining; } int8_t _z_bytes_from_slice(_z_bytes_t *b, _z_slice_t s) { *b = _z_bytes_null(); @@ -132,13 +132,13 @@ int8_t _z_bytes_to_slice(const _z_bytes_t *bytes, _z_slice_t *s) { return _Z_RES_OK; } -int8_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s) { return _z_arc_slice_svec_append(&dst->_slices, s); } +_Bool _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s) { return _z_arc_slice_svec_append(&dst->_slices, s); } int8_t _z_bytes_append_inner(_z_bytes_t *dst, _z_bytes_t *src) { _Bool success = true; for (size_t i = 0; i < _z_bytes_num_slices(src); ++i) { _z_arc_slice_t *s = _z_bytes_get_slice(src, i); - success = success && _z_arc_slice_svec_append(&dst->_slices, s); + success = success && _z_bytes_append_slice(dst, s); } if (!success) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; @@ -156,7 +156,7 @@ int8_t _z_bytes_append(_z_bytes_t *dst, _z_bytes_t *src) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } _z_arc_slice_t arc_s = _z_arc_slice_wrap(s, 0, l_len); - _z_arc_slice_svec_append(&dst->_slices, &arc_s); + _z_bytes_append_slice(dst, &arc_s); if (dst->_slices._val == NULL) { _z_arc_slice_drop(&arc_s); @@ -419,7 +419,7 @@ int8_t _z_bytes_reader_read_slices(_z_bytes_reader_t *reader, size_t len, _z_byt res = _Z_ERR_SYSTEM_OUT_OF_MEMORY; break; } - res = _z_arc_slice_svec_append(&out->_slices, &ss); + res = _z_bytes_append_slice(out, &ss) ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY; if (res != _Z_RES_OK) { _z_arc_slice_drop(&ss); break; diff --git a/src/net/query.c b/src/net/query.c index 2b87c0e03..fb79133a7 100644 --- a/src/net/query.c +++ b/src/net/query.c @@ -51,7 +51,7 @@ _z_query_t _z_query_create(const _z_value_t *value, const _z_keyexpr_t *key, con memcpy(q._parameters, parameters->start, parameters->len); q._parameters[parameters->len] = 0; q._anyke = (strstr(q._parameters, Z_SELECTOR_QUERY_MATCH) == NULL) ? false : true; - q.attachment = attachment; + _z_bytes_copy(&q.attachment, &attachment); _z_keyexpr_copy(&q._key, key); _z_value_copy(&q._value, value); diff --git a/src/net/reply.c b/src/net/reply.c index 48bff2c6c..3df4b9fe2 100644 --- a/src/net/reply.c +++ b/src/net/reply.c @@ -93,12 +93,12 @@ _z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, reply.data.replier_id = id; // Create sample _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 - sample.payload = payload; // FIXME: call z_bytes_move or copy + sample.keyexpr = keyexpr; // FIXME: call z_keyexpr_move or copy + sample.encoding = encoding; // FIXME: call z_encoding_move or copy + _z_bytes_copy(&sample.payload, &payload); // FIXME: call z_bytes_move or copy sample.kind = kind; sample.timestamp = _z_timestamp_duplicate(timestamp); - sample.attachment = attachment; // FIXME: call z_bytes_move or copy + _z_bytes_copy(&sample.attachment, &attachment); // FIXME: call z_bytes_move or copy // Create sample rc from value reply.data.sample = _z_sample_rc_new_from_val(sample); diff --git a/src/protocol/codec.c b/src/protocol/codec.c index ee159c192..53b15a1a0 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -144,7 +144,7 @@ uint8_t _z_zint64_encode_buf(uint8_t *buf, uint64_t v) { buf[start++] = c; } - return start; + return (uint8_t)start; } int8_t _z_zint64_encode(_z_wbuf_t *wbf, uint64_t v) { @@ -181,7 +181,7 @@ int8_t _z_zint64_decode_with_reader(uint64_t *zint, __z_single_byte_reader_t rea int8_t _z_zsize_decode_with_reader(_z_zint_t *zint, __z_single_byte_reader_t reader, void *context) { uint64_t i = 0; int8_t res = _z_zint64_decode_with_reader(&i, reader, context); - if (res == _Z_RES_OK && i > SIZE_MAX) { + if (res != _Z_RES_OK || i > SIZE_MAX) { res = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } else { *zint = (_z_zint_t)i; diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 90688369d..7c8952295 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -446,8 +446,9 @@ int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) { } case _Z_MSG_EXT_ENC_ZBUF | 0x03: { // Payload _z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); - ret = _z_encoding_decode(&msg->_ext_value.encoding, &zbf); - ret |= _z_bytes_from_buf(&msg->_ext_value.payload, (uint8_t *)_z_zbuf_start(&zbf), _z_zbuf_len(&zbf)); + _Z_RETURN_IF_ERR(_z_encoding_decode(&msg->_ext_value.encoding, &zbf)); + _Z_RETURN_IF_ERR( + _z_bytes_from_buf(&msg->_ext_value.payload, (uint8_t *)_z_zbuf_start(&zbf), _z_zbuf_len(&zbf))); break; } case _Z_MSG_EXT_ENC_ZBUF | 0x05: { // Attachment diff --git a/tests/z_bytes_test.c b/tests/z_bytes_test.c index d03755c7a..07a37bf84 100644 --- a/tests/z_bytes_test.c +++ b/tests/z_bytes_test.c @@ -43,7 +43,7 @@ void test_slice(void) { assert(_z_bytes_num_slices(&b) == 1); assert(_z_slice_eq(&_z_bytes_get_slice(&b, 0)->slice.in->val, &s)); - _z_bytes_to_buf(&b, data_out, 5); + assert(_z_bytes_to_buf(&b, data_out, 5) == 5); assert(memcmp(data, data_out, 5) == 0); _z_bytes_drop(&b); @@ -73,7 +73,7 @@ void test_append(void) { assert(_z_slice_eq(&_z_bytes_get_slice(&b, 1)->slice.in->val, &s2.slice.in->val)); assert(_z_slice_eq(&_z_bytes_get_slice(&b, 2)->slice.in->val, &s3.slice.in->val)); - _z_bytes_to_buf(&b, data_out, 10); + assert(_z_bytes_to_buf(&b, data_out, 15) == 10); assert(memcmp(data_in, data_out, 10) == 0); _z_bytes_drop(&b); From ea30ca12f835acfcf06f2b75b7bb09f09e0b6405 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Tue, 25 Jun 2024 12:14:19 +0200 Subject: [PATCH 10/25] fixes --- src/collections/arc_slice.c | 2 +- src/collections/string.c | 4 ++-- src/net/sample.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/collections/arc_slice.c b/src/collections/arc_slice.c index 663cb0ae6..922ac9ca6 100644 --- a/src/collections/arc_slice.c +++ b/src/collections/arc_slice.c @@ -49,7 +49,7 @@ _z_arc_slice_t _z_arc_slice_get_subslice(const _z_arc_slice_t* s, size_t offset, } else { out.slice = _z_slice_rc_clone(&s->slice); out.len = len; - out.start = out.start + offset; + out.start = s->start + offset; } return out; } diff --git a/src/collections/string.c b/src/collections/string.c index 253feb55d..52352b3d3 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -123,8 +123,8 @@ _z_string_t _z_string_from_bytes(const _z_slice_t *bs) { _z_string_t _z_string_preallocate(size_t len) { _z_string_t s = _z_string_null(); // Allocate string - s.len = len + (size_t)1; // bytes data + null terminator - char *str_val = (char *)z_malloc(s.len * sizeof(char)); + s.len = len; + char *str_val = (char *)z_malloc((s.len + (size_t)1) * sizeof(char)); // bytes data + null terminator if (str_val == NULL) { s.len = 0; return s; diff --git a/src/net/sample.c b/src/net/sample.c index c601783ea..9651075a2 100644 --- a/src/net/sample.c +++ b/src/net/sample.c @@ -83,12 +83,12 @@ _z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes_t payload, const _z_bytes_t attachment) { _z_sample_t s = _z_sample_null(); _z_keyexpr_copy(&s.keyexpr, key); - s.payload = payload; + _z_bytes_copy(&s.payload, &payload); _z_encoding_copy(&s.encoding, &encoding); s.kind = kind; s.timestamp = timestamp; s.qos = qos; - s.attachment = attachment; + _z_bytes_copy(&s.attachment, &attachment); return s; } #else From 5f8e54f9ce0af2bb4b0818bba150bd6102fbf16f Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Tue, 25 Jun 2024 17:42:19 +0200 Subject: [PATCH 11/25] fixed svec implementation; fixed z_client_test checking string length; minor readability fixes; --- src/api/api.c | 2 +- src/collections/arc_slice.c | 3 +-- src/collections/bytes.c | 4 ++-- src/collections/vec.c | 2 +- src/net/reply.c | 8 ++++---- src/protocol/codec.c | 12 +++--------- src/protocol/codec/message.c | 4 +--- tests/z_client_test.c | 2 +- 8 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 8a5c779e4..f4821b457 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -323,7 +323,7 @@ int8_t z_bytes_deserialize_into_string(const z_loaned_bytes_t *bytes, z_owned_st } // Convert bytes to string size_t len = _z_bytes_len(bytes); - *s->_val = _z_string_preallocate(_z_bytes_len(bytes)); + *s->_val = _z_string_preallocate(len); if (s->_val->len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; _z_bytes_to_buf(bytes, (uint8_t *)s->_val->val, len); return _Z_RES_OK; diff --git a/src/collections/arc_slice.c b/src/collections/arc_slice.c index 922ac9ca6..ac0e7bc9d 100644 --- a/src/collections/arc_slice.c +++ b/src/collections/arc_slice.c @@ -79,7 +79,6 @@ int8_t _z_arc_slice_move(_z_arc_slice_t* dst, _z_arc_slice_t* src) { int8_t _z_arc_slice_drop(_z_arc_slice_t* s) { _z_slice_rc_drop(&s->slice); - s->len = 0; - s->start = 0; + *s = _z_arc_slice_empty(); return _Z_RES_OK; } diff --git a/src/collections/bytes.c b/src/collections/bytes.c index 03c112e47..1d9695559 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -34,7 +34,7 @@ _z_bytes_t _z_bytes_null(void) { int8_t _z_bytes_copy(_z_bytes_t *dst, const _z_bytes_t *src) { _z_arc_slice_svec_copy(&dst->_slices, &src->_slices); - if (_z_arc_slice_svec_len(&dst->_slices) == _z_arc_slice_svec_len(&dst->_slices)) { + if (_z_arc_slice_svec_len(&dst->_slices) == _z_arc_slice_svec_len(&src->_slices)) { return _Z_RES_OK; } else { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; @@ -93,7 +93,7 @@ size_t _z_bytes_to_buf(const _z_bytes_t *bytes, uint8_t *dst, size_t len) { size_t s_len = _z_arc_slice_len(s); size_t len_to_copy = remaining >= s_len ? s_len : remaining; memcpy(start, _z_arc_slice_data(s), len_to_copy); - start += s_len; + start += len_to_copy; remaining -= len_to_copy; } diff --git a/src/collections/vec.c b/src/collections/vec.c index af58c42c0..9acd13f85 100644 --- a/src/collections/vec.c +++ b/src/collections/vec.c @@ -167,7 +167,7 @@ void __z_svec_move_inner(void *dst, void *src, z_element_move_f move, size_t num void _z_svec_copy(_z_svec_t *dst, const _z_svec_t *src, z_element_copy_f copy, size_t element_size) { dst->_capacity = 0; dst->_len = 0; - dst->_val = z_malloc(sizeof(void *) * src->_capacity); + dst->_val = z_malloc(element_size * src->_capacity); if (dst->_val != NULL) { dst->_capacity = src->_capacity; dst->_len = src->_len; diff --git a/src/net/reply.c b/src/net/reply.c index 3df4b9fe2..43de427ba 100644 --- a/src/net/reply.c +++ b/src/net/reply.c @@ -93,12 +93,12 @@ _z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, reply.data.replier_id = id; // Create sample _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_bytes_copy(&sample.payload, &payload); // FIXME: call z_bytes_move or copy + sample.keyexpr = keyexpr; // FIXME: call z_keyexpr_move or copy + sample.encoding = encoding; // FIXME: call z_encoding_move or copy + _z_bytes_copy(&sample.payload, &payload); sample.kind = kind; sample.timestamp = _z_timestamp_duplicate(timestamp); - _z_bytes_copy(&sample.attachment, &attachment); // FIXME: call z_bytes_move or copy + _z_bytes_copy(&sample.attachment, &attachment); // Create sample rc from value reply.data.sample = _z_sample_rc_new_from_val(sample); diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 53b15a1a0..839ef0e42 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -270,12 +270,8 @@ int8_t _z_slice_val_decode_na(_z_slice_t *bs, _z_zbuf_t *zbf) { } int8_t _z_slice_decode_na(_z_slice_t *bs, _z_zbuf_t *zbf) { - int8_t ret = _Z_RES_OK; - - ret |= _z_zsize_decode(&bs->len, zbf); - ret |= _z_slice_val_decode_na(bs, zbf); - - return ret; + _Z_RETURN_IF_ERR(_z_zsize_decode(&bs->len, zbf)); + return _z_slice_val_decode_na(bs, zbf); } int8_t _z_slice_val_decode(_z_slice_t *bs, _z_zbuf_t *zbf) { return _z_slice_val_decode_na(bs, zbf); } @@ -283,10 +279,8 @@ 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) { - int8_t ret = _Z_RES_OK; _z_slice_t s; - ret = _z_slice_decode(&s, zbf); - if (ret != _Z_RES_OK) return ret; + _Z_RETURN_IF_ERR(_z_slice_decode(&s, zbf)); return _z_bytes_from_slice(bs, s); } diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 7c8952295..608f9ba0f 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -446,9 +446,7 @@ int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) { } case _Z_MSG_EXT_ENC_ZBUF | 0x03: { // Payload _z_zbuf_t zbf = _z_slice_as_zbuf(extension->_body._zbuf._val); - _Z_RETURN_IF_ERR(_z_encoding_decode(&msg->_ext_value.encoding, &zbf)); - _Z_RETURN_IF_ERR( - _z_bytes_from_buf(&msg->_ext_value.payload, (uint8_t *)_z_zbuf_start(&zbf), _z_zbuf_len(&zbf))); + ret = _z_value_decode(&msg->_ext_value, &zbf); break; } case _Z_MSG_EXT_ENC_ZBUF | 0x05: { // Attachment diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 206573196..fea41db83 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -83,7 +83,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *arg) { z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); - assert(z_string_len(z_loan(value)) == strlen(res) + 1); + assert(z_string_len(z_loan(value)) == strlen(res)); assert(strncmp(res, z_string_data(z_loan(value)), strlen(res)) == 0); assert(_z_str_eq(z_loan(k_str)->val, res) == true); From 13d222486f26d88688dbfc203e8bd24692f54615 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Tue, 25 Jun 2024 17:53:09 +0200 Subject: [PATCH 12/25] fixe _z_sample_create signature --- include/zenoh-pico/net/sample.h | 2 +- src/net/sample.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/zenoh-pico/net/sample.h b/include/zenoh-pico/net/sample.h index 969e91215..0817d9d1e 100644 --- a/include/zenoh-pico/net/sample.h +++ b/include/zenoh-pico/net/sample.h @@ -56,7 +56,7 @@ void _z_sample_free(_z_sample_t **sample); void _z_sample_copy(_z_sample_t *dst, const _z_sample_t *src); _z_sample_t _z_sample_duplicate(const _z_sample_t *src); -_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes_t, _z_timestamp_t timestamp, +_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes_t payload, _z_timestamp_t timestamp, const _z_encoding_t encoding, const z_sample_kind_t kind, const _z_qos_t qos, const _z_bytes_t attachment); diff --git a/src/net/sample.c b/src/net/sample.c index 9651075a2..14196a668 100644 --- a/src/net/sample.c +++ b/src/net/sample.c @@ -92,7 +92,7 @@ _z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes_t payload, return s; } #else -_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes payload, const _z_timestamp_t timestamp, +_z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes_t payload, const _z_timestamp_t timestamp, const _z_encoding_t encoding, const z_sample_kind_t kind, const _z_qos_t qos, const _z_bytes_t attachment) { _ZP_UNUSED(key); From cd95ad2f02b350307a9384f92b1621a160fe8748 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Tue, 25 Jun 2024 18:01:58 +0200 Subject: [PATCH 13/25] fix _z_trigger_local_subscriptions signature --- src/session/subscription.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/session/subscription.c b/src/session/subscription.c index 1db666dfc..b146cdaa4 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -208,12 +208,11 @@ void _z_flush_subscriptions(_z_session_t *zn) { } #else // Z_FEATURE_SUBSCRIPTION == 0 -void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_bytes_t attachment, +void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_bytes_t payload, _z_n_qos_t qos, const _z_bytes_t attachment) { _ZP_UNUSED(zn); _ZP_UNUSED(keyexpr); _ZP_UNUSED(payload); - _ZP_UNUSED(payload_len); _ZP_UNUSED(qos); _ZP_UNUSED(attachment); } From c673cd49c39ec06158d19379784add6c8da8abfa Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Tue, 25 Jun 2024 18:35:10 +0200 Subject: [PATCH 14/25] fix attachment examples --- examples/unix/c11/z_get_attachment.c | 1 + examples/unix/c11/z_queryable_attachment.c | 1 + examples/unix/c11/z_sub_attachment.c | 1 + 3 files changed, 3 insertions(+) diff --git a/examples/unix/c11/z_get_attachment.c b/examples/unix/c11/z_get_attachment.c index 59c42c682..83fab15ea 100644 --- a/examples/unix/c11/z_get_attachment.c +++ b/examples/unix/c11/z_get_attachment.c @@ -65,6 +65,7 @@ void parse_attachment(kv_pairs_rx_t *kvp, const z_loaned_bytes_t *attachment) { z_bytes_iterator_t iter = z_bytes_get_iterator(attachment); while (kvp->current_idx < kvp->len && z_bytes_iterator_next(&iter, &kv)) { + z_bytes_deserialize_into_pair(z_loan(kv), &first, &second); z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key); z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value); z_bytes_drop(&first); diff --git a/examples/unix/c11/z_queryable_attachment.c b/examples/unix/c11/z_queryable_attachment.c index b0d872019..807870ec4 100644 --- a/examples/unix/c11/z_queryable_attachment.c +++ b/examples/unix/c11/z_queryable_attachment.c @@ -77,6 +77,7 @@ void parse_attachment(kv_pairs_rx_t *kvp, const z_loaned_bytes_t *attachment) { z_bytes_iterator_t iter = z_bytes_get_iterator(attachment); while (kvp->current_idx < kvp->len && z_bytes_iterator_next(&iter, &kv)) { + z_bytes_deserialize_into_pair(z_loan(kv), &first, &second); z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key); z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value); z_bytes_drop(&first); diff --git a/examples/unix/c11/z_sub_attachment.c b/examples/unix/c11/z_sub_attachment.c index 413532f05..c5dd40782 100644 --- a/examples/unix/c11/z_sub_attachment.c +++ b/examples/unix/c11/z_sub_attachment.c @@ -42,6 +42,7 @@ void parse_attachment(kv_pairs_t *kvp, const z_loaned_bytes_t *attachment) { z_bytes_iterator_t iter = z_bytes_get_iterator(attachment); while (kvp->current_idx < kvp->len && z_bytes_iterator_next(&iter, &kv)) { + z_bytes_deserialize_into_pair(z_loan(kv), &first, &second); z_bytes_deserialize_into_string(z_loan(first), &kvp->data[kvp->current_idx].key); z_bytes_deserialize_into_string(z_loan(second), &kvp->data[kvp->current_idx].value); z_bytes_drop(&first); From acc3f597c2e28f64d6f0f8591e94ecbe5f0bf7d7 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 26 Jun 2024 15:58:14 +0000 Subject: [PATCH 15/25] fixed uninitialized owned_bytes pointer --- include/zenoh-pico/api/primitives.h | 8 +++ include/zenoh-pico/protocol/codec/core.h | 17 ----- include/zenoh-pico/utils/result.h | 28 ++++++++ src/api/api.c | 86 ++++++------------------ zenohpico.pc | 2 +- 5 files changed, 59 insertions(+), 82 deletions(-) diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 16fb380b3..a78862a65 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -816,6 +816,14 @@ int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body */ int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second); +/** + * Parameters: + * bytes: Pointer to an unitialized :c:type:`z_lowned_bytes_t` instance. + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. + */ +int8_t z_bytes_empty(z_owned_bytes_t *bytes); + /** * Returns an iterator for multi-element serialized data. * diff --git a/include/zenoh-pico/protocol/codec/core.h b/include/zenoh-pico/protocol/codec/core.h index 59ccadc0d..706e7d9ab 100644 --- a/include/zenoh-pico/protocol/codec/core.h +++ b/include/zenoh-pico/protocol/codec/core.h @@ -24,23 +24,6 @@ #include "zenoh-pico/utils/config.h" #include "zenoh-pico/utils/result.h" -#define _Z_RETURN_IF_ERR(expr) \ - { \ - int8_t __res = expr; \ - if (__res != _Z_RES_OK) { \ - return __res; \ - } \ - } - -#define _Z_CLEAN_RETURN_IF_ERR(base_expr, clean_expr) \ - { \ - int8_t __res = base_expr; \ - if (__res != _Z_RES_OK) { \ - clean_expr; \ - return __res; \ - } \ - } - typedef int8_t (*__z_single_byte_reader_t)(uint8_t *, void *context); /*------------------ Internal Zenoh-net Macros ------------------*/ int8_t _z_consolidation_mode_encode(_z_wbuf_t *wbf, z_consolidation_mode_t en); diff --git a/include/zenoh-pico/utils/result.h b/include/zenoh-pico/utils/result.h index 76d9dc583..82e91d4cc 100644 --- a/include/zenoh-pico/utils/result.h +++ b/include/zenoh-pico/utils/result.h @@ -73,4 +73,32 @@ typedef enum { _Z_ERR_GENERIC = -128 } _z_res_t; +#define _Z_RETURN_IF_ERR(expr) \ + { \ + int8_t __res = expr; \ + if (__res != _Z_RES_OK) { \ + return __res; \ + } \ + } + +#define _Z_CLEAN_RETURN_IF_ERR(base_expr, clean_expr) \ + { \ + int8_t __res = base_expr; \ + if (__res != _Z_RES_OK) { \ + clean_expr; \ + return __res; \ + } \ + } + +#define _Z_IS_OK(expr) (expr == _Z_RES_OK) +#define _Z_IS_ERR(expr) (expr == _Z_RES_OK) +#define _Z_DO_AND_RETURN_IF_ERR(expr, action) \ + { \ + int8_t __res = expr; \ + if (__res != _Z_RES_OK) { \ + { action; } \ + return __res; \ + } \ + } + #endif /* ZENOH_PICO_UTILS_RESULT_H */ diff --git a/src/api/api.c b/src/api/api.c index f4821b457..ccc9d3b4d 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -331,13 +331,8 @@ int8_t z_bytes_deserialize_into_string(const z_loaned_bytes_t *bytes, z_owned_st int8_t z_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second) { // Init pair of owned bytes - z_bytes_null(first); - z_bytes_null(second); - first->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - second->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if ((first->_val == NULL) || (second->_val == NULL)) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(first)); + _Z_DO_AND_RETURN_IF_ERR(z_bytes_empty(second), z_bytes_drop(second)); return _z_bytes_deserialize_into_pair(bytes, first->_val, second->_val); } @@ -359,11 +354,7 @@ int8_t z_bytes_serialize_from_int64(z_owned_bytes_t *bytes, int64_t val) { int8_t z_bytes_serialize_from_uint8(z_owned_bytes_t *bytes, uint8_t val) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data int8_t res = _z_bytes_from_uint8(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); @@ -372,11 +363,7 @@ int8_t z_bytes_serialize_from_uint8(z_owned_bytes_t *bytes, uint8_t val) { int8_t z_bytes_serialize_from_uint16(z_owned_bytes_t *bytes, uint16_t val) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data int8_t res = _z_bytes_from_uint16(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); @@ -385,11 +372,7 @@ int8_t z_bytes_serialize_from_uint16(z_owned_bytes_t *bytes, uint16_t val) { int8_t z_bytes_serialize_from_uint32(z_owned_bytes_t *bytes, uint32_t val) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data int8_t res = _z_bytes_from_uint32(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); @@ -398,11 +381,7 @@ int8_t z_bytes_serialize_from_uint32(z_owned_bytes_t *bytes, uint32_t val) { int8_t z_bytes_serialize_from_uint64(z_owned_bytes_t *bytes, uint64_t val) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data int8_t res = _z_bytes_from_uint64(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); @@ -411,11 +390,7 @@ int8_t z_bytes_serialize_from_uint64(z_owned_bytes_t *bytes, uint64_t val) { int8_t z_bytes_serialize_from_float(z_owned_bytes_t *bytes, float val) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data int8_t res = _z_bytes_from_float(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); @@ -424,11 +399,7 @@ int8_t z_bytes_serialize_from_float(z_owned_bytes_t *bytes, float val) { int8_t z_bytes_serialize_from_double(z_owned_bytes_t *bytes, double val) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data int8_t res = _z_bytes_from_double(bytes->_val, val); if (res != _Z_RES_OK) z_bytes_drop(bytes); @@ -437,11 +408,7 @@ int8_t z_bytes_serialize_from_double(z_owned_bytes_t *bytes, double val) { int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data _z_slice_t s = _z_slice_wrap((uint8_t *)data, len); int8_t res = _z_bytes_from_slice(bytes->_val, s); @@ -451,11 +418,7 @@ int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, int8_t z_bytes_serialize_from_slice_copy(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Allocate bytes _z_slice_t s = _z_slice_make(len); if (!_z_slice_check(s) && len > 0) { @@ -483,30 +446,16 @@ int8_t z_bytes_serialize_from_string_copy(z_owned_bytes_t *bytes, const char *s) int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body)(z_owned_bytes_t *data, void *context), void *context) { // Init owned bytes - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); z_owned_bytes_t data; while (iterator_body(&data, context)) { - int8_t res = _z_bytes_append(bytes->_val, data._val); - if (res != _Z_RES_OK) { - z_bytes_drop(bytes); - return res; - } + _Z_DO_AND_RETURN_IF_ERR(_z_bytes_append(bytes->_val, data._val), z_bytes_drop(bytes)); } return _Z_RES_OK; } int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second) { - z_bytes_null(bytes); - bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); - if (bytes->_val == NULL) { - z_bytes_drop(first); - z_bytes_drop(second); - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } + _Z_DO_AND_RETURN_IF_ERR(z_bytes_empty(bytes), z_bytes_drop(first); z_bytes_drop(second)); int8_t res = _z_bytes_serialize_from_pair(bytes->_val, first->_val, second->_val); first->_val = NULL; second->_val = NULL; @@ -514,6 +463,15 @@ int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *firs return res; } +int8_t z_bytes_empty(z_owned_bytes_t *bytes) { + bytes->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + if (bytes->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + *bytes->_val = _z_bytes_null(); + return _Z_RES_OK; +} + z_bytes_iterator_t z_bytes_get_iterator(const z_loaned_bytes_t *bytes) { return _z_bytes_get_iterator(bytes); } _Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *bytes) { diff --git a/zenohpico.pc b/zenohpico.pc index 129afce02..8116ade1e 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.11.20240620dev +Version: 0.11.20240626dev Cflags: -I${prefix}/include Libs: -L${prefix}/lib -lzenohpico From c33bf7694ba4478ce06ce900144ea22dbf566075 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 26 Jun 2024 18:24:09 +0200 Subject: [PATCH 16/25] added z_bytes_len and z_bytes_is_empty functions; updated docs --- docs/api.rst | 7 +++++++ include/zenoh-pico/api/primitives.h | 22 +++++++++++++++++++++- include/zenoh-pico/api/types.h | 2 +- src/api/api.c | 4 ++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index b0a076abf..630ef8fe1 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -55,6 +55,8 @@ Data Structures .. autoctype:: types.h::zp_send_keep_alive_options_t .. autoctype:: types.h::zp_send_join_options_t .. autoctype:: types.h::z_qos_t +.. autoctype:: types.h::z_bytes_iterator_t + Owned Types ~~~~~~~~~~~ @@ -320,6 +322,11 @@ Primitives .. autocfunction:: primitives.h::z_bytes_serialize_from_slice_copy .. autocfunction:: primitives.h::z_bytes_serialize_from_string .. autocfunction:: primitives.h::z_bytes_serialize_from_string_copy +.. autocfunction:: primitives.h::z_bytes_empty +.. autocfunction:: primitives.h::z_bytes_len +.. autocfunction:: primitives.h::z_bytes_is_empty +.. autocfunction:: primitives.h::z_bytes_get_iterator +.. autocfunction:: primitives.h::z_bytes_iterator_next .. autocfunction:: primitives.h::z_timestamp_check .. autocfunction:: primitives.h::z_query_target_default .. autocfunction:: primitives.h::z_query_consolidation_auto diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index a78862a65..938765e6a 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -844,10 +844,30 @@ z_bytes_iterator_t z_bytes_get_iterator(const z_loaned_bytes_t *bytes); * iter: An iterator over multi-element serialized data. * out: An uninitialized :c:type:`z_owned_bytes_t` that will contained next serialized element. * Return: - * ``false`` when iterator reaches the end, ``true`` otherwise + * ``false`` when iterator reaches the end, ``true`` otherwise. */ _Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *out); +/** + * Returns total number of bytes in the container. + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * Return: + * Number of bytes in the container. + */ +size_t z_bytes_len(const z_loaned_bytes_t *bytes); + +/** + * Checks if container is empty + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * Return: + * ``true`` if conainer is empty, ``false`` otherwise. + */ +_Bool z_bytes_is_empty(const z_loaned_bytes_t *bytes); + /** * Checks validity of a timestamp * diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 71e918f59..3899671de 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -76,7 +76,7 @@ _Z_OWNED_TYPE_PTR(_z_bytes_t, bytes) _Z_LOANED_TYPE(_z_bytes_t, bytes) /** - * An iterator over multi-element serialized data + * An iterator over multi-element serialized data. */ typedef _z_bytes_iterator_t z_bytes_iterator_t; diff --git a/src/api/api.c b/src/api/api.c index ccc9d3b4d..1a73df43b 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -472,6 +472,10 @@ int8_t z_bytes_empty(z_owned_bytes_t *bytes) { return _Z_RES_OK; } +size_t z_bytes_len(const z_loaned_bytes_t *bytes) { return _z_bytes_len(bytes); } + +_Bool z_bytes_is_empty(const z_loaned_bytes_t *bytes) { return _z_bytes_is_empty(bytes); } + z_bytes_iterator_t z_bytes_get_iterator(const z_loaned_bytes_t *bytes) { return _z_bytes_get_iterator(bytes); } _Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *bytes) { From 1c202904c86129846379c5510950e2bd73426811 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 26 Jun 2024 18:45:13 +0200 Subject: [PATCH 17/25] exposed z_bytes_reader_t --- docs/api.rst | 5 ++ include/zenoh-pico/api/primitives.h | 79 ++++++++++++++++++++++++++--- include/zenoh-pico/api/types.h | 5 ++ src/api/api.c | 12 +++++ 4 files changed, 93 insertions(+), 8 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 630ef8fe1..a43691cd4 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -55,6 +55,7 @@ Data Structures .. autoctype:: types.h::zp_send_keep_alive_options_t .. autoctype:: types.h::zp_send_join_options_t .. autoctype:: types.h::z_qos_t +.. autoctype:: types.h::z_bytes_reader_t .. autoctype:: types.h::z_bytes_iterator_t @@ -327,6 +328,10 @@ Primitives .. autocfunction:: primitives.h::z_bytes_is_empty .. autocfunction:: primitives.h::z_bytes_get_iterator .. autocfunction:: primitives.h::z_bytes_iterator_next +.. autocfunction:: primitives.h::z_bytes_get_reader +.. autocfunction:: primitives.h::z_bytes_reader_read +.. autocfunction:: primitives.h::z_bytes_reader_seek +.. autocfunction:: primitives.h::z_bytes_reader_tell .. autocfunction:: primitives.h::z_timestamp_check .. autocfunction:: primitives.h::z_query_target_default .. autocfunction:: primitives.h::z_query_consolidation_auto diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 938765e6a..9f3775d7b 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -824,6 +824,26 @@ int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *firs */ int8_t z_bytes_empty(z_owned_bytes_t *bytes); +/** + * Returns total number of bytes in the container. + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * Return: + * Number of bytes in the container. + */ +size_t z_bytes_len(const z_loaned_bytes_t *bytes); + +/** + * Checks if container is empty + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * Return: + * ``true`` if conainer is empty, ``false`` otherwise. + */ +_Bool z_bytes_is_empty(const z_loaned_bytes_t *bytes); + /** * Returns an iterator for multi-element serialized data. * @@ -849,24 +869,67 @@ z_bytes_iterator_t z_bytes_get_iterator(const z_loaned_bytes_t *bytes); _Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *out); /** - * Returns total number of bytes in the container. + * Returns a reader for the `bytes`. + * + * The `bytes` should outlive the reader and should not be modified, while reader is in use. * * Parameters: - * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * bytes: Data to read. + * * Return: - * Number of bytes in the container. + * The constructed :c:type:`z_bytes_reader_t`. */ -size_t z_bytes_len(const z_loaned_bytes_t *bytes); +z_bytes_reader_t z_bytes_get_reader(const z_loaned_bytes_t *bytes); /** - * Checks if container is empty + * Reads data into specified destination. * * Parameters: - * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * reader: Data reader to read from. + * dst: Buffer where the read data is written. + * len: Maximum number of bytes to read. + * * Return: - * ``true`` if conainer is empty, ``false`` otherwise. + * Number of bytes read. If return value is smaller than `len`, it means that the end of the data was reached. */ -_Bool z_bytes_is_empty(const z_loaned_bytes_t *bytes); +size_t z_bytes_reader_read(z_bytes_reader_t *reader, uint8_t *dst, size_t len); +/** + * Sets the `reader` position indicator for the payload to the value pointed to by offset. + * The new position is exactly `offset` bytes measured from the beginning of the payload if origin is `SEEK_SET`, + * from the current reader position if origin is `SEEK_CUR`, and from the end of the payload if origin is `SEEK_END`. + * + * Parameters: + * reader: Data reader to reposition. + * offset: New position ffset in bytes. + * origin: Origin for the new position. + * + * Return: + * ​0​ upon success, negative error code otherwise. + */ +int8_t z_bytes_reader_seek(z_bytes_reader_t *reader, int64_t offset, int origin); +/** + * Gets the read position indicator. + * + * Parameters: + * reader: Data reader to get position of. + * + * Return: + * Read position indicator on success or -1L if failure occurs. + */ +int64_t z_bytes_reader_tell(z_bytes_reader_t *reader); + +/** + * Constructs :c:type:`z_owned_bytes_t` object corresponding to the next element of serialized data. + * + * Will construct null-state `z_owned_bytes_t` when iterator reaches the end (or in case of error). + * + * Parameters: + * iter: An iterator over multi-element serialized data. + * out: An uninitialized :c:type:`z_owned_bytes_t` that will contained next serialized element. + * Return: + * ``false`` when iterator reaches the end, ``true`` otherwise. + */ +_Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *out); /** * Checks validity of a timestamp diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 3899671de..ef77fcd56 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -80,6 +80,11 @@ _Z_LOANED_TYPE(_z_bytes_t, bytes) */ typedef _z_bytes_iterator_t z_bytes_iterator_t; +/** + * A reader for serialized data. + */ +typedef _z_bytes_reader_t z_bytes_reader_t; + /** * Represents a string without null-terminator. * diff --git a/src/api/api.c b/src/api/api.c index 1a73df43b..ea39db291 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -476,6 +476,18 @@ size_t z_bytes_len(const z_loaned_bytes_t *bytes) { return _z_bytes_len(bytes); _Bool z_bytes_is_empty(const z_loaned_bytes_t *bytes) { return _z_bytes_is_empty(bytes); } +z_bytes_reader_t z_bytes_get_reader(const z_loaned_bytes_t *bytes) { return _z_bytes_get_reader(bytes); } + +size_t z_bytes_reader_read(z_bytes_reader_t *reader, uint8_t *dst, size_t len) { + return _z_bytes_reader_read(reader, dst, len); +} + +int8_t z_bytes_reader_seek(z_bytes_reader_t *reader, int64_t offset, int origin) { + return _z_bytes_reader_seek(reader, offset, origin); +} + +int64_t z_bytes_reader_tell(z_bytes_reader_t *reader) { return _z_bytes_reader_tell(reader); } + z_bytes_iterator_t z_bytes_get_iterator(const z_loaned_bytes_t *bytes) { return _z_bytes_get_iterator(bytes); } _Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *bytes) { From 00f9577408059484bd3f93914e3f9e75f7cf151b Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 27 Jun 2024 10:40:37 +0200 Subject: [PATCH 18/25] remove _Z_DO_AND_RETURN_IF_ERR since _Z_CLEAN_RETURN_IF_ERR does the same --- include/zenoh-pico/utils/result.h | 10 +--------- src/api/api.c | 6 +++--- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/include/zenoh-pico/utils/result.h b/include/zenoh-pico/utils/result.h index 82e91d4cc..af31d8db8 100644 --- a/include/zenoh-pico/utils/result.h +++ b/include/zenoh-pico/utils/result.h @@ -91,14 +91,6 @@ typedef enum { } #define _Z_IS_OK(expr) (expr == _Z_RES_OK) -#define _Z_IS_ERR(expr) (expr == _Z_RES_OK) -#define _Z_DO_AND_RETURN_IF_ERR(expr, action) \ - { \ - int8_t __res = expr; \ - if (__res != _Z_RES_OK) { \ - { action; } \ - return __res; \ - } \ - } +#define _Z_IS_ERR(expr) (expr != _Z_RES_OK) #endif /* ZENOH_PICO_UTILS_RESULT_H */ diff --git a/src/api/api.c b/src/api/api.c index ea39db291..05064c029 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -332,7 +332,7 @@ int8_t z_bytes_deserialize_into_string(const z_loaned_bytes_t *bytes, z_owned_st int8_t z_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second) { // Init pair of owned bytes _Z_RETURN_IF_ERR(z_bytes_empty(first)); - _Z_DO_AND_RETURN_IF_ERR(z_bytes_empty(second), z_bytes_drop(second)); + _Z_CLEAN_RETURN_IF_ERR(z_bytes_empty(second), z_bytes_drop(second)); return _z_bytes_deserialize_into_pair(bytes, first->_val, second->_val); } @@ -449,13 +449,13 @@ int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); z_owned_bytes_t data; while (iterator_body(&data, context)) { - _Z_DO_AND_RETURN_IF_ERR(_z_bytes_append(bytes->_val, data._val), z_bytes_drop(bytes)); + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_append(bytes->_val, data._val), z_bytes_drop(bytes)); } return _Z_RES_OK; } int8_t z_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second) { - _Z_DO_AND_RETURN_IF_ERR(z_bytes_empty(bytes), z_bytes_drop(first); z_bytes_drop(second)); + _Z_CLEAN_RETURN_IF_ERR(z_bytes_empty(bytes), z_bytes_drop(first); z_bytes_drop(second)); int8_t res = _z_bytes_serialize_from_pair(bytes->_val, first->_val, second->_val); first->_val = NULL; second->_val = NULL; From 57e2022fa1ed9729e554391af529b2f03564dbeb Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 27 Jun 2024 11:18:11 +0200 Subject: [PATCH 19/25] serialization fixes --- src/api/api.c | 51 +++++++++++++++++++++------------------------------ zenohpico.pc | 2 +- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 05064c029..52e2a6bc1 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -332,7 +332,7 @@ int8_t z_bytes_deserialize_into_string(const z_loaned_bytes_t *bytes, z_owned_st int8_t z_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_bytes_t *first, z_owned_bytes_t *second) { // Init pair of owned bytes _Z_RETURN_IF_ERR(z_bytes_empty(first)); - _Z_CLEAN_RETURN_IF_ERR(z_bytes_empty(second), z_bytes_drop(second)); + _Z_CLEAN_RETURN_IF_ERR(z_bytes_empty(second), z_bytes_drop(first)); return _z_bytes_deserialize_into_pair(bytes, first->_val, second->_val); } @@ -356,54 +356,48 @@ int8_t z_bytes_serialize_from_uint8(z_owned_bytes_t *bytes, uint8_t val) { // Init owned bytes _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data - int8_t res = _z_bytes_from_uint8(bytes->_val, val); - if (res != _Z_RES_OK) z_bytes_drop(bytes); - return res; + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_from_uint8(bytes->_val, val), z_bytes_drop(bytes)); + return _Z_RES_OK; } int8_t z_bytes_serialize_from_uint16(z_owned_bytes_t *bytes, uint16_t val) { // Init owned bytes _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data - int8_t res = _z_bytes_from_uint16(bytes->_val, val); - if (res != _Z_RES_OK) z_bytes_drop(bytes); - return res; + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_from_uint16(bytes->_val, val), z_bytes_drop(bytes)); + return _Z_RES_OK; } int8_t z_bytes_serialize_from_uint32(z_owned_bytes_t *bytes, uint32_t val) { // Init owned bytes _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data - int8_t res = _z_bytes_from_uint32(bytes->_val, val); - if (res != _Z_RES_OK) z_bytes_drop(bytes); - return res; + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_from_uint32(bytes->_val, val), z_bytes_drop(bytes)); + return _Z_RES_OK; } int8_t z_bytes_serialize_from_uint64(z_owned_bytes_t *bytes, uint64_t val) { // Init owned bytes _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data - int8_t res = _z_bytes_from_uint64(bytes->_val, val); - if (res != _Z_RES_OK) z_bytes_drop(bytes); - return res; + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_from_uint64(bytes->_val, val), z_bytes_drop(bytes)); + return _Z_RES_OK; } int8_t z_bytes_serialize_from_float(z_owned_bytes_t *bytes, float val) { // Init owned bytes _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data - int8_t res = _z_bytes_from_float(bytes->_val, val); - if (res != _Z_RES_OK) z_bytes_drop(bytes); - return res; + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_from_float(bytes->_val, val), z_bytes_drop(bytes)); + return _Z_RES_OK; } int8_t z_bytes_serialize_from_double(z_owned_bytes_t *bytes, double val) { // Init owned bytes _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data - int8_t res = _z_bytes_from_double(bytes->_val, val); - if (res != _Z_RES_OK) z_bytes_drop(bytes); - return res; + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_from_double(bytes->_val, val), z_bytes_drop(bytes)); + return _Z_RES_OK; } int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { @@ -411,24 +405,21 @@ int8_t z_bytes_serialize_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Encode data _z_slice_t s = _z_slice_wrap((uint8_t *)data, len); - int8_t res = _z_bytes_from_slice(bytes->_val, s); - if (res != _Z_RES_OK) z_bytes_drop(bytes); - return res; + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_from_slice(bytes->_val, s), z_bytes_drop(bytes)); + return _Z_RES_OK; } int8_t z_bytes_serialize_from_slice_copy(z_owned_bytes_t *bytes, const uint8_t *data, size_t len) { - // Init owned bytes - _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); // Allocate bytes - _z_slice_t s = _z_slice_make(len); + _z_slice_t s = _z_slice_wrap_copy((uint8_t *)data, len); if (!_z_slice_check(s) && len > 0) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - // Copy data - memcpy((uint8_t *)s.start, data, len); - int8_t res = _z_bytes_from_slice(bytes->_val, s); - if (res != _Z_RES_OK) z_bytes_drop(bytes); - return res; + + // Init owned bytes + _Z_CLEAN_RETURN_IF_ERR(z_bytes_empty(bytes), _z_slice_clear(&s)); + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_from_slice(bytes->_val, s), _z_slice_clear(&s); z_bytes_drop(bytes)); + return _Z_RES_OK; } int8_t z_bytes_serialize_from_string(z_owned_bytes_t *bytes, const char *s) { diff --git a/zenohpico.pc b/zenohpico.pc index 8116ade1e..c4b0e4a08 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.11.20240626dev +Version: 0.11.20240627dev Cflags: -I${prefix}/include Libs: -L${prefix}/lib -lzenohpico From baf0d008bb26281c66307459291b0f4ac7971ced Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 27 Jun 2024 13:40:01 +0200 Subject: [PATCH 20/25] ensure that decoded z_bytes never contanin non-_is_alloc slice --- include/zenoh-pico/collections/bytes.h | 2 +- src/collections/bytes.c | 2 +- src/protocol/codec.c | 6 +++++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index 82a09aa64..624e1381d 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -73,7 +73,7 @@ int8_t _z_bytes_from_uint64(_z_bytes_t *b, uint64_t val); int8_t _z_bytes_from_float(_z_bytes_t *b, float val); int8_t _z_bytes_from_double(_z_bytes_t *b, double val); size_t _z_bytes_to_buf(const _z_bytes_t *bytes, uint8_t *dst, size_t len); -int8_t _z_bytes_from_buf(_z_bytes_t *b, uint8_t *src, size_t len); +int8_t _z_bytes_from_buf(_z_bytes_t *b, const uint8_t *src, size_t len); int8_t _z_bytes_serialize_from_pair(_z_bytes_t *out, _z_bytes_t *first, _z_bytes_t *second); int8_t _z_bytes_deserialize_into_pair(const _z_bytes_t *bs, _z_bytes_t *first_out, _z_bytes_t *second_out); _z_slice_t _z_bytes_try_get_contiguous(const _z_bytes_t *bs); diff --git a/src/collections/bytes.c b/src/collections/bytes.c index 1d9695559..20177626b 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -106,7 +106,7 @@ int8_t _z_bytes_from_slice(_z_bytes_t *b, _z_slice_t s) { return _z_arc_slice_svec_append(&b->_slices, &arc_s) ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY; } -int8_t _z_bytes_from_buf(_z_bytes_t *b, uint8_t *src, size_t len) { +int8_t _z_bytes_from_buf(_z_bytes_t *b, const uint8_t *src, size_t len) { *b = _z_bytes_null(); _z_slice_t s = _z_slice_wrap_copy(src, len); if (s.len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 839ef0e42..cac24c15f 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -281,7 +281,11 @@ int8_t _z_slice_decode(_z_slice_t *bs, _z_zbuf_t *zbf) { return _z_slice_decode_ int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *zbf) { _z_slice_t s; _Z_RETURN_IF_ERR(_z_slice_decode(&s, zbf)); - return _z_bytes_from_slice(bs, s); + if (s._is_alloc) { + return _z_bytes_from_slice(bs, s); + } else { + return _z_bytes_from_buf(bs, s.start, s.len); + } } int8_t _z_bytes_encode_val(_z_wbuf_t *wbf, const _z_bytes_t *bs) { From f36e2e4034cd69c1a5097f9cedbc0e42ab8a346e Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 27 Jun 2024 18:39:46 +0200 Subject: [PATCH 21/25] added z_bytes_writer implementation --- docs/api.rst | 10 ++ include/zenoh-pico/api/olv_macros.h | 26 +++++ include/zenoh-pico/api/primitives.h | 24 +++- include/zenoh-pico/api/types.h | 6 + include/zenoh-pico/collections/bytes.h | 22 +++- src/api/api.c | 16 ++- src/collections/bytes.c | 148 +++++++++++++++++-------- tests/z_bytes_test.c | 55 +++++++++ 8 files changed, 249 insertions(+), 58 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index a43691cd4..7d75a8ac1 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -132,6 +132,10 @@ TODO: owned type description Represents an array of non null-terminated string. +.. c:type:: z_owned_bytes_writer_t + + Represents a writer for serialized data. + Loaned Types ~~~~~~~~~~~ @@ -205,6 +209,10 @@ TODO: loaned type description Represents an array of non null-terminated string. +.. c:type:: z_loaned_bytes_writer_t + + Represents a writer for serialized data. + View Types ~~~~~~~~~~~ @@ -332,6 +340,8 @@ Primitives .. autocfunction:: primitives.h::z_bytes_reader_read .. autocfunction:: primitives.h::z_bytes_reader_seek .. autocfunction:: primitives.h::z_bytes_reader_tell +.. autocfunction:: primitives.h::z_bytes_get_writer +.. autocfunction:: primitives.h::z_bytes_writer_write .. autocfunction:: primitives.h::z_timestamp_check .. autocfunction:: primitives.h::z_query_target_default .. autocfunction:: primitives.h::z_query_consolidation_auto diff --git a/include/zenoh-pico/api/olv_macros.h b/include/zenoh-pico/api/olv_macros.h index bae04c382..3f6cee474 100644 --- a/include/zenoh-pico/api/olv_macros.h +++ b/include/zenoh-pico/api/olv_macros.h @@ -73,6 +73,28 @@ } \ } +#define _Z_OWNED_FUNCTIONS_PTR_TRIVIAL_IMPL(type, name) \ + _Bool z_##name##_check(const z_owned_##name##_t *obj) { return obj->_val != NULL; } \ + const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj) { return obj->_val; } \ + z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj) { return obj->_val; } \ + void z_##name##_null(z_owned_##name##_t *obj) { obj->_val = NULL; } \ + z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj) { return obj; } \ + int8_t z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src) { \ + int8_t ret = _Z_RES_OK; \ + obj->_val = (type *)z_malloc(sizeof(type)); \ + if (obj->_val != NULL) { \ + *obj->_val = *src; \ + } else { \ + ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; \ + } \ + return ret; \ + } \ + void z_##name##_drop(z_owned_##name##_t *obj) { \ + if ((obj != NULL) && (obj->_val != NULL)) { \ + z_free(&obj->_val); \ + } \ + } + #define _Z_OWNED_FUNCTIONS_RC_IMPL(name) \ _Bool z_##name##_check(const z_owned_##name##_t *val) { return val->_rc.in != NULL; } \ const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *val) { return &val->_rc; } \ @@ -132,7 +154,11 @@ // Gets internal value from refcounted type (e.g. z_loaned_session_t, z_query_t) #define _Z_RC_IN_VAL(arg) ((arg)->in->val) +// Checks if refcounted type is initialized +#define _Z_RC_IS_NULL(arg) ((arg)->in == NULL) + // Gets internal value from refcounted owned type (e.g. z_owned_session_t, z_owned_query_t) #define _Z_OWNED_RC_IN_VAL(arg) ((arg)->_rc.in->val) + #endif /* INCLUDE_ZENOH_PICO_API_OLV_MACROS_H */ diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 9f3775d7b..e5ea3541e 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -919,17 +919,29 @@ int8_t z_bytes_reader_seek(z_bytes_reader_t *reader, int64_t offset, int origin) int64_t z_bytes_reader_tell(z_bytes_reader_t *reader); /** - * Constructs :c:type:`z_owned_bytes_t` object corresponding to the next element of serialized data. + * Constructs writer for :c:type:`z_loaned_bytes_t`. * - * Will construct null-state `z_owned_bytes_t` when iterator reaches the end (or in case of error). + * Parameters: + * bytes: Data container to write to. + * writer: Uninitialized memory location where writer is to be constructed. * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. + */ +int8_t z_bytes_get_writer(z_loaned_bytes_t *bytes, z_owned_bytes_writer_t* writer); + +/** + * Writes `len` bytes from `src` into underlying :c:type:`z_loaned_bytes_t. + * * Parameters: - * iter: An iterator over multi-element serialized data. - * out: An uninitialized :c:type:`z_owned_bytes_t` that will contained next serialized element. + * writer: A data writer + * src: Buffer to write from. + * len: Number of bytes to write. + * * Return: - * ``false`` when iterator reaches the end, ``true`` otherwise. + * ``0`` if encode successful, ``negative value`` otherwise. */ -_Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *out); +int8_t z_bytes_writer_write(z_loaned_bytes_writer_t *writer, const uint8_t *src, size_t len); /** * Checks validity of a timestamp diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index ef77fcd56..4cfb71e14 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -75,6 +75,12 @@ _Z_LOANED_TYPE(_z_slice_t, slice) _Z_OWNED_TYPE_PTR(_z_bytes_t, bytes) _Z_LOANED_TYPE(_z_bytes_t, bytes) +/** + * Represents a writer for serialized data. + */ +_Z_OWNED_TYPE_PTR(_z_bytes_writer_t, bytes_writer) +_Z_LOANED_TYPE(_z_bytes_writer_t, bytes_writer) + /** * An iterator over multi-element serialized data. */ diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index 624e1381d..32db38de7 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -47,8 +47,8 @@ typedef struct { _Bool _z_bytes_check(const _z_bytes_t *bytes); _z_bytes_t _z_bytes_null(void); -int8_t _z_bytes_append(_z_bytes_t *dst, _z_bytes_t *src); -_Bool _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s); +int8_t _z_bytes_append_bytes(_z_bytes_t *dst, _z_bytes_t *src); +int8_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s); int8_t _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); @@ -90,7 +90,6 @@ int8_t _z_bytes_reader_seek(_z_bytes_reader_t *reader, int64_t offset, int origi int64_t _z_bytes_reader_tell(const _z_bytes_reader_t *reader); int8_t _z_bytes_reader_read_slices(_z_bytes_reader_t *reader, size_t len, _z_bytes_t *out); int8_t _z_bytes_reader_read(_z_bytes_reader_t *reader, uint8_t *buf, size_t len); -int8_t _z_bytes_reader_read_next(_z_bytes_reader_t *reader, _z_bytes_t *out); typedef struct { _z_bytes_reader_t _reader; @@ -99,4 +98,21 @@ typedef struct { _z_bytes_iterator_t _z_bytes_get_iterator(const _z_bytes_t *bytes); _Bool _z_bytes_iterator_next(_z_bytes_iterator_t *iter, _z_bytes_t *b); + +typedef struct { + uint8_t* cache; + size_t cache_size; + _z_bytes_t *bytes; +} _z_bytes_writer_t; + +_z_bytes_writer_t _z_bytes_get_writer(_z_bytes_t *bytes, size_t cache_size); +int8_t _z_bytes_writer_write(_z_bytes_writer_t *writer, const uint8_t *src, size_t len); +int8_t _z_bytes_writer_ensure_cache(_z_bytes_writer_t *writer); + +typedef struct { + _z_bytes_writer_t writer; +} _z_bytes_iterator_writer_t; + +_z_bytes_iterator_writer_t _z_bytes_get_iterator_writer(_z_bytes_t *bytes); +int8_t _z_bytes_iterator_writer_write(_z_bytes_iterator_writer_t *writer, _z_bytes_t *bytes); #endif /* ZENOH_PICO_COLLECTIONS_BYTES_H */ diff --git a/src/api/api.c b/src/api/api.c index 52e2a6bc1..ba6c92bf2 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -439,8 +439,9 @@ int8_t z_bytes_serialize_from_iter(z_owned_bytes_t *bytes, _Bool (*iterator_body // Init owned bytes _Z_RETURN_IF_ERR(z_bytes_empty(bytes)); z_owned_bytes_t data; + _z_bytes_iterator_writer_t iter_writer = _z_bytes_get_iterator_writer(bytes->_val); while (iterator_body(&data, context)) { - _Z_CLEAN_RETURN_IF_ERR(_z_bytes_append(bytes->_val, data._val), z_bytes_drop(bytes)); + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_iterator_writer_write(&iter_writer, data._val), z_bytes_drop(bytes)); } return _Z_RES_OK; } @@ -492,6 +493,17 @@ _Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *bytes) { return true; } +int8_t z_bytes_get_writer(z_loaned_bytes_t *bytes, z_owned_bytes_writer_t* writer) { + writer->_val = (z_loaned_bytes_writer_t *)z_malloc(sizeof(z_loaned_bytes_writer_t)); + if (writer->_val == NULL) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + *writer->_val = _z_bytes_get_writer(bytes, Z_IOSLICE_SIZE); + return _Z_RES_OK; +} + +int8_t z_bytes_writer_write(z_loaned_bytes_writer_t *writer, const uint8_t *src, size_t len) { + return _z_bytes_writer_write(writer, src, len); +} + _Bool z_timestamp_check(z_timestamp_t ts) { return _z_timestamp_check(&ts); } z_query_target_t z_query_target_default(void) { return Z_QUERY_TARGET_DEFAULT; } @@ -593,6 +605,8 @@ _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_string_vec_t, string_array, _z_owner_noop_copy, _ _Z_VIEW_FUNCTIONS_PTR_IMPL(_z_string_vec_t, string_array) _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_slice_t, slice, _z_slice_copy, _z_slice_free) _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_bytes_t, bytes, _z_bytes_copy, _z_bytes_free) +_Z_OWNED_FUNCTIONS_PTR_TRIVIAL_IMPL(_z_bytes_writer_t, bytes_writer) + #if Z_FEATURE_PUBLICATION == 1 || Z_FEATURE_QUERYABLE == 1 || Z_FEATURE_QUERY == 1 // Convert a user owned bytes payload to an internal bytes payload, returning an empty one if value invalid diff --git a/src/collections/bytes.c b/src/collections/bytes.c index 20177626b..86ef2f959 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -22,6 +22,7 @@ #include "zenoh-pico/system/platform.h" #include "zenoh-pico/utils/endianness.h" #include "zenoh-pico/utils/result.h" +#include "zenoh-pico/api/olv_macros.h" /*-------- Bytes --------*/ _Bool _z_bytes_check(const _z_bytes_t *bytes) { return !_z_bytes_is_empty(bytes); } @@ -132,13 +133,19 @@ int8_t _z_bytes_to_slice(const _z_bytes_t *bytes, _z_slice_t *s) { return _Z_RES_OK; } -_Bool _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s) { return _z_arc_slice_svec_append(&dst->_slices, s); } +int8_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s) { + _Z_CLEAN_RETURN_IF_ERR( + _z_arc_slice_svec_append(&dst->_slices, s) ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY, + _z_arc_slice_drop(s) + ); + return _Z_RES_OK; +} -int8_t _z_bytes_append_inner(_z_bytes_t *dst, _z_bytes_t *src) { +int8_t _z_bytes_append_bytes(_z_bytes_t *dst, _z_bytes_t *src) { _Bool success = true; for (size_t i = 0; i < _z_bytes_num_slices(src); ++i) { _z_arc_slice_t *s = _z_bytes_get_slice(src, i); - success = success && _z_bytes_append_slice(dst, s); + success = success && _z_bytes_append_slice(dst, s) == _Z_RES_OK; } if (!success) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; @@ -148,48 +155,20 @@ int8_t _z_bytes_append_inner(_z_bytes_t *dst, _z_bytes_t *src) { return _Z_RES_OK; } -int8_t _z_bytes_append(_z_bytes_t *dst, _z_bytes_t *src) { - uint8_t l_buf[16]; - size_t l_len = _z_zsize_encode_buf(l_buf, _z_bytes_len(src)); - _z_slice_t s = _z_slice_wrap_copy(l_buf, l_len); - if (!_z_slice_check(s)) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - _z_arc_slice_t arc_s = _z_arc_slice_wrap(s, 0, l_len); - _z_bytes_append_slice(dst, &arc_s); - - if (dst->_slices._val == NULL) { - _z_arc_slice_drop(&arc_s); - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - int8_t res = _z_bytes_append_inner(dst, src); - if (res != _Z_RES_OK) { - return res; - } - return _Z_RES_OK; -} - int8_t _z_bytes_serialize_from_pair(_z_bytes_t *out, _z_bytes_t *first, _z_bytes_t *second) { - int8_t res = _z_bytes_append(out, first); - if (res != _Z_RES_OK) { - _z_bytes_drop(out); - _z_bytes_drop(first); - return res; - } - res = _z_bytes_append(out, second); - if (res != _Z_RES_OK) { - _z_bytes_drop(out); - _z_bytes_drop(second); - } + *out = _z_bytes_null(); + _z_bytes_iterator_writer_t writer = _z_bytes_get_iterator_writer(out); + _Z_RETURN_IF_ERR(_z_bytes_iterator_writer_write(&writer, first)); + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_iterator_writer_write(&writer, second), _z_bytes_drop(out)); - return res; + return _Z_RES_OK; } int8_t _z_bytes_deserialize_into_pair(const _z_bytes_t *bs, _z_bytes_t *first_out, _z_bytes_t *second_out) { - _z_bytes_reader_t reader = _z_bytes_get_reader(bs); - int8_t res = _z_bytes_reader_read_next(&reader, first_out); + _z_bytes_iterator_t iter = _z_bytes_get_iterator(bs); + int8_t res = _z_bytes_iterator_next(&iter, first_out); if (res != _Z_RES_OK) return res; - res = _z_bytes_reader_read_next(&reader, second_out); + res = _z_bytes_iterator_next(&iter, second_out); if (res != _Z_RES_OK) { _z_bytes_drop(first_out); }; @@ -419,7 +398,8 @@ int8_t _z_bytes_reader_read_slices(_z_bytes_reader_t *reader, size_t len, _z_byt res = _Z_ERR_SYSTEM_OUT_OF_MEMORY; break; } - res = _z_bytes_append_slice(out, &ss) ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY; + + res = _z_bytes_append_slice(out, &ss); if (res != _Z_RES_OK) { _z_arc_slice_drop(&ss); break; @@ -433,20 +413,92 @@ int8_t _z_bytes_reader_read_slices(_z_bytes_reader_t *reader, size_t len, _z_byt return res; } -int8_t _z_bytes_reader_read_next(_z_bytes_reader_t *reader, _z_bytes_t *out) { - *out = _z_bytes_null(); +_z_bytes_iterator_t _z_bytes_get_iterator(const _z_bytes_t *bytes) { + return (_z_bytes_iterator_t){._reader = _z_bytes_get_reader(bytes)}; +} + +_Bool _z_bytes_iterator_next(_z_bytes_iterator_t *iter, _z_bytes_t *b) { + *b = _z_bytes_null(); _z_zint_t len; - if (_z_bytes_reader_read_zint(reader, &len) != _Z_RES_OK) { + if (_z_bytes_reader_read_zint(&iter->_reader, &len) != _Z_RES_OK) { return _Z_ERR_DID_NOT_READ; } - return _z_bytes_reader_read_slices(reader, len, out); + return _z_bytes_reader_read_slices(&iter->_reader, len, b); } -_z_bytes_iterator_t _z_bytes_get_iterator(const _z_bytes_t *bytes) { - return (_z_bytes_iterator_t){._reader = _z_bytes_get_reader(bytes)}; +_z_bytes_writer_t _z_bytes_get_writer(_z_bytes_t *bytes, size_t cache_size) { + return (_z_bytes_writer_t) { + .cache = NULL, + .cache_size = cache_size, + .bytes = bytes + }; } -_Bool _z_bytes_iterator_next(_z_bytes_iterator_t *iter, _z_bytes_t *b) { - return _z_bytes_reader_read_next(&iter->_reader, b) == _Z_RES_OK; +int8_t _z_bytes_writer_ensure_cache(_z_bytes_writer_t *writer) { + // first we check if cache stayed untouched since previous write operation + if (writer->cache != NULL) { + _z_arc_slice_t *arc_s = _z_bytes_get_slice(writer->bytes, _z_bytes_num_slices(writer->bytes) - 1); + if (_Z_RC_IN_VAL(&arc_s->slice).start + arc_s->len == writer->cache) { + size_t remaining_in_cache = _Z_RC_IN_VAL(&arc_s->slice).len - arc_s->len; + if (remaining_in_cache > 0) return _Z_RES_OK; + } + } + // otherwise we allocate a new cache + assert(writer->cache_size > 0); + _z_slice_t s = _z_slice_make(writer->cache_size); + if (s.start == NULL) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + _z_arc_slice_t cache = _z_arc_slice_wrap(s, 0, 0); + if (_Z_RC_IS_NULL(&cache.slice)) { + _z_slice_clear(&s); + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + + _Z_CLEAN_RETURN_IF_ERR( + _z_bytes_append_slice(writer->bytes, &cache), + _z_arc_slice_drop(&cache) + ); + writer->cache = (uint8_t*)_Z_RC_IN_VAL(&cache.slice).start; + return _Z_RES_OK; +} + +int8_t _z_bytes_writer_write(_z_bytes_writer_t *writer, const uint8_t *src, size_t len) { + if (writer->cache_size == 0) { // no cache append data as a single slice + _z_slice_t s = _z_slice_wrap_copy(src, len); + if (s.len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + _z_arc_slice_t arc_s = _z_arc_slice_wrap(s, 0, len); + if _Z_RC_IS_NULL(&arc_s.slice) { + _z_slice_clear(&s); + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + return _z_bytes_append_slice(writer->bytes, &arc_s); + } + + while (len > 0) { + _Z_RETURN_IF_ERR(_z_bytes_writer_ensure_cache(writer)); + _z_arc_slice_t *arc_s = _z_bytes_get_slice(writer->bytes, _z_bytes_num_slices(writer->bytes) - 1); + size_t remaining_in_cache = _Z_RC_IN_VAL(&arc_s->slice).len - arc_s->len; + size_t to_copy = remaining_in_cache < len ? remaining_in_cache : len; + memcpy(writer->cache, src, to_copy); + len -= to_copy; + arc_s->len += to_copy; + src += to_copy; + writer->cache += to_copy; + } + return _Z_RES_OK; +} + + +_z_bytes_iterator_writer_t _z_bytes_get_iterator_writer(_z_bytes_t *bytes) { + return (_z_bytes_iterator_writer_t){ + .writer = _z_bytes_get_writer(bytes, 0) + }; +} +int8_t _z_bytes_iterator_writer_write(_z_bytes_iterator_writer_t *writer, _z_bytes_t *src) { + uint8_t l_buf[16]; + size_t l_len = _z_zsize_encode_buf(l_buf, _z_bytes_len(src)); + _Z_RETURN_IF_ERR(_z_bytes_writer_write(&writer->writer, l_buf, l_len)); + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_append_bytes(writer->writer.bytes, src), _z_bytes_drop(src)); + + return _Z_RES_OK; } diff --git a/tests/z_bytes_test.c b/tests/z_bytes_test.c index 07a37bf84..01bfd113e 100644 --- a/tests/z_bytes_test.c +++ b/tests/z_bytes_test.c @@ -161,11 +161,66 @@ void test_reader_seek(void) { _z_bytes_drop(&b); } + +void test_writer_no_cache(void) { + uint8_t data1[5] = {1, 2, 3, 4, 5}; + uint8_t data2[5] = {1, 2, 6, 7, 8}; + uint8_t data3[3] = {3, 9, 10}; + + _z_bytes_t b = _z_bytes_null(); + _z_bytes_writer_t writer = _z_bytes_get_writer(&b, 0); + + _z_bytes_writer_write(&writer, data1, 5); + assert(_z_bytes_len(&b) == 5); + assert(_z_bytes_num_slices(&b) == 1); + _z_bytes_writer_write(&writer, data2, 5); + assert(_z_bytes_len(&b) == 10); + assert(_z_bytes_num_slices(&b) == 2); + _z_bytes_writer_write(&writer, data3, 3); + assert(_z_bytes_len(&b) == 13); + assert(_z_bytes_num_slices(&b) == 3); + + assert(memcmp(data1, _z_arc_slice_data(_z_bytes_get_slice(&b, 0)), 5) == 0); + assert(memcmp(data2, _z_arc_slice_data(_z_bytes_get_slice(&b, 1)), 5) == 0); + assert(memcmp(data3, _z_arc_slice_data(_z_bytes_get_slice(&b, 2)), 3) == 0); + _z_bytes_drop(&b); +} + +void test_writer_with_cache(void) { + uint8_t data1[5] = {1, 2, 3, 4, 5}; + uint8_t data2[5] = {1, 2, 6, 7, 8}; + uint8_t data3[3] = {3, 9, 10}; + + uint8_t data1_out[7] = {1, 2, 3, 4, 5, 1, 2}; + uint8_t data2_out[6] = {6, 7, 8, 3, 9, 10}; + _z_bytes_t b = _z_bytes_null(); + _z_bytes_writer_t writer = _z_bytes_get_writer(&b, 7); + + _z_bytes_writer_write(&writer, data1, 5); + assert(_z_bytes_len(&b) == 5); + assert(_z_bytes_num_slices(&b) == 1); + _z_bytes_writer_write(&writer, data2, 5); + assert(_z_bytes_len(&b) == 10); + assert(_z_bytes_num_slices(&b) == 2); + _z_bytes_writer_write(&writer, data3, 3); + assert(_z_bytes_len(&b) == 13); + assert(_z_bytes_num_slices(&b) == 2); + + assert(_z_arc_slice_len(_z_bytes_get_slice(&b, 0)) == 7); + assert(_z_arc_slice_len(_z_bytes_get_slice(&b, 1)) == 6); + assert(memcmp(data1_out, _z_arc_slice_data(_z_bytes_get_slice(&b, 0)), 7) == 0); + assert(memcmp(data2_out, _z_arc_slice_data(_z_bytes_get_slice(&b, 1)), 6) == 0); + _z_bytes_drop(&b); +} + + int main(void) { test_null_bytes(); test_slice(); test_append(); test_reader_read(); test_reader_seek(); + test_writer_no_cache(); + test_writer_with_cache(); return 0; } From f0c17b7be4cf858128258fb1981fe4119ab8207b Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 27 Jun 2024 18:42:00 +0200 Subject: [PATCH 22/25] format --- include/zenoh-pico/api/olv_macros.h | 1 - include/zenoh-pico/api/primitives.h | 4 +-- include/zenoh-pico/collections/bytes.h | 3 +-- src/api/api.c | 5 ++-- src/collections/bytes.c | 34 +++++++++----------------- tests/z_bytes_test.c | 4 +-- 6 files changed, 17 insertions(+), 34 deletions(-) diff --git a/include/zenoh-pico/api/olv_macros.h b/include/zenoh-pico/api/olv_macros.h index 3f6cee474..aee26849f 100644 --- a/include/zenoh-pico/api/olv_macros.h +++ b/include/zenoh-pico/api/olv_macros.h @@ -160,5 +160,4 @@ // Gets internal value from refcounted owned type (e.g. z_owned_session_t, z_owned_query_t) #define _Z_OWNED_RC_IN_VAL(arg) ((arg)->_rc.in->val) - #endif /* INCLUDE_ZENOH_PICO_API_OLV_MACROS_H */ diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index e5ea3541e..fe3979f1c 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -928,11 +928,11 @@ int64_t z_bytes_reader_tell(z_bytes_reader_t *reader); * Return: * ``0`` if encode successful, ``negative value`` otherwise. */ -int8_t z_bytes_get_writer(z_loaned_bytes_t *bytes, z_owned_bytes_writer_t* writer); +int8_t z_bytes_get_writer(z_loaned_bytes_t *bytes, z_owned_bytes_writer_t *writer); /** * Writes `len` bytes from `src` into underlying :c:type:`z_loaned_bytes_t. - * + * * Parameters: * writer: A data writer * src: Buffer to write from. diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index 32db38de7..764a6df12 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -98,9 +98,8 @@ typedef struct { _z_bytes_iterator_t _z_bytes_get_iterator(const _z_bytes_t *bytes); _Bool _z_bytes_iterator_next(_z_bytes_iterator_t *iter, _z_bytes_t *b); - typedef struct { - uint8_t* cache; + uint8_t *cache; size_t cache_size; _z_bytes_t *bytes; } _z_bytes_writer_t; diff --git a/src/api/api.c b/src/api/api.c index ba6c92bf2..0545ae3bd 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -493,11 +493,11 @@ _Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *bytes) { return true; } -int8_t z_bytes_get_writer(z_loaned_bytes_t *bytes, z_owned_bytes_writer_t* writer) { +int8_t z_bytes_get_writer(z_loaned_bytes_t *bytes, z_owned_bytes_writer_t *writer) { writer->_val = (z_loaned_bytes_writer_t *)z_malloc(sizeof(z_loaned_bytes_writer_t)); if (writer->_val == NULL) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; *writer->_val = _z_bytes_get_writer(bytes, Z_IOSLICE_SIZE); - return _Z_RES_OK; + return _Z_RES_OK; } int8_t z_bytes_writer_write(z_loaned_bytes_writer_t *writer, const uint8_t *src, size_t len) { @@ -607,7 +607,6 @@ _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_slice_t, slice, _z_slice_copy, _z_slice_free) _Z_OWNED_FUNCTIONS_PTR_IMPL(_z_bytes_t, bytes, _z_bytes_copy, _z_bytes_free) _Z_OWNED_FUNCTIONS_PTR_TRIVIAL_IMPL(_z_bytes_writer_t, bytes_writer) - #if Z_FEATURE_PUBLICATION == 1 || Z_FEATURE_QUERYABLE == 1 || Z_FEATURE_QUERY == 1 // Convert a user owned bytes payload to an internal bytes payload, returning an empty one if value invalid static _z_bytes_t _z_bytes_from_owned_bytes(z_owned_bytes_t *bytes) { diff --git a/src/collections/bytes.c b/src/collections/bytes.c index 86ef2f959..144c59f36 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -18,11 +18,11 @@ #include #include +#include "zenoh-pico/api/olv_macros.h" #include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/system/platform.h" #include "zenoh-pico/utils/endianness.h" #include "zenoh-pico/utils/result.h" -#include "zenoh-pico/api/olv_macros.h" /*-------- Bytes --------*/ _Bool _z_bytes_check(const _z_bytes_t *bytes) { return !_z_bytes_is_empty(bytes); } @@ -133,11 +133,9 @@ int8_t _z_bytes_to_slice(const _z_bytes_t *bytes, _z_slice_t *s) { return _Z_RES_OK; } -int8_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s) { - _Z_CLEAN_RETURN_IF_ERR( - _z_arc_slice_svec_append(&dst->_slices, s) ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY, - _z_arc_slice_drop(s) - ); +int8_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s) { + _Z_CLEAN_RETURN_IF_ERR(_z_arc_slice_svec_append(&dst->_slices, s) ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY, + _z_arc_slice_drop(s)); return _Z_RES_OK; } @@ -428,11 +426,7 @@ _Bool _z_bytes_iterator_next(_z_bytes_iterator_t *iter, _z_bytes_t *b) { } _z_bytes_writer_t _z_bytes_get_writer(_z_bytes_t *bytes, size_t cache_size) { - return (_z_bytes_writer_t) { - .cache = NULL, - .cache_size = cache_size, - .bytes = bytes - }; + return (_z_bytes_writer_t){.cache = NULL, .cache_size = cache_size, .bytes = bytes}; } int8_t _z_bytes_writer_ensure_cache(_z_bytes_writer_t *writer) { @@ -454,27 +448,24 @@ int8_t _z_bytes_writer_ensure_cache(_z_bytes_writer_t *writer) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - _Z_CLEAN_RETURN_IF_ERR( - _z_bytes_append_slice(writer->bytes, &cache), - _z_arc_slice_drop(&cache) - ); - writer->cache = (uint8_t*)_Z_RC_IN_VAL(&cache.slice).start; + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_append_slice(writer->bytes, &cache), _z_arc_slice_drop(&cache)); + writer->cache = (uint8_t *)_Z_RC_IN_VAL(&cache.slice).start; return _Z_RES_OK; } int8_t _z_bytes_writer_write(_z_bytes_writer_t *writer, const uint8_t *src, size_t len) { - if (writer->cache_size == 0) { // no cache append data as a single slice + if (writer->cache_size == 0) { // no cache append data as a single slice _z_slice_t s = _z_slice_wrap_copy(src, len); if (s.len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; _z_arc_slice_t arc_s = _z_arc_slice_wrap(s, 0, len); - if _Z_RC_IS_NULL(&arc_s.slice) { + if _Z_RC_IS_NULL (&arc_s.slice) { _z_slice_clear(&s); return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } return _z_bytes_append_slice(writer->bytes, &arc_s); } - while (len > 0) { + while (len > 0) { _Z_RETURN_IF_ERR(_z_bytes_writer_ensure_cache(writer)); _z_arc_slice_t *arc_s = _z_bytes_get_slice(writer->bytes, _z_bytes_num_slices(writer->bytes) - 1); size_t remaining_in_cache = _Z_RC_IN_VAL(&arc_s->slice).len - arc_s->len; @@ -488,11 +479,8 @@ int8_t _z_bytes_writer_write(_z_bytes_writer_t *writer, const uint8_t *src, size return _Z_RES_OK; } - _z_bytes_iterator_writer_t _z_bytes_get_iterator_writer(_z_bytes_t *bytes) { - return (_z_bytes_iterator_writer_t){ - .writer = _z_bytes_get_writer(bytes, 0) - }; + return (_z_bytes_iterator_writer_t){.writer = _z_bytes_get_writer(bytes, 0)}; } int8_t _z_bytes_iterator_writer_write(_z_bytes_iterator_writer_t *writer, _z_bytes_t *src) { uint8_t l_buf[16]; diff --git a/tests/z_bytes_test.c b/tests/z_bytes_test.c index 01bfd113e..11a095114 100644 --- a/tests/z_bytes_test.c +++ b/tests/z_bytes_test.c @@ -161,7 +161,6 @@ void test_reader_seek(void) { _z_bytes_drop(&b); } - void test_writer_no_cache(void) { uint8_t data1[5] = {1, 2, 3, 4, 5}; uint8_t data2[5] = {1, 2, 6, 7, 8}; @@ -190,7 +189,7 @@ void test_writer_with_cache(void) { uint8_t data1[5] = {1, 2, 3, 4, 5}; uint8_t data2[5] = {1, 2, 6, 7, 8}; uint8_t data3[3] = {3, 9, 10}; - + uint8_t data1_out[7] = {1, 2, 3, 4, 5, 1, 2}; uint8_t data2_out[6] = {6, 7, 8, 3, 9, 10}; _z_bytes_t b = _z_bytes_null(); @@ -213,7 +212,6 @@ void test_writer_with_cache(void) { _z_bytes_drop(&b); } - int main(void) { test_null_bytes(); test_slice(); From 1bb55e530506453a084f5c0e486d53943e88dbc6 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 27 Jun 2024 22:09:48 +0200 Subject: [PATCH 23/25] fixed attachments --- include/zenoh-pico/collections/bytes.h | 2 +- src/api/api.c | 2 +- src/collections/bytes.c | 27 +++++++++++++------------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index 764a6df12..643c39acf 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -96,7 +96,7 @@ typedef struct { } _z_bytes_iterator_t; _z_bytes_iterator_t _z_bytes_get_iterator(const _z_bytes_t *bytes); -_Bool _z_bytes_iterator_next(_z_bytes_iterator_t *iter, _z_bytes_t *b); +int8_t _z_bytes_iterator_next(_z_bytes_iterator_t *iter, _z_bytes_t *b); typedef struct { uint8_t *cache; diff --git a/src/api/api.c b/src/api/api.c index 0545ae3bd..4ad100328 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -486,7 +486,7 @@ _Bool z_bytes_iterator_next(z_bytes_iterator_t *iter, z_owned_bytes_t *bytes) { z_bytes_null(bytes); bytes->_val = (z_loaned_bytes_t *)z_malloc(sizeof(z_loaned_bytes_t)); if (bytes->_val == NULL) return false; - if (!_z_bytes_iterator_next(iter, bytes->_val)) { + if (_z_bytes_iterator_next(iter, bytes->_val) != _Z_RES_OK) { z_bytes_drop(bytes); return false; } diff --git a/src/collections/bytes.c b/src/collections/bytes.c index 144c59f36..1dbd5168d 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -140,23 +140,24 @@ int8_t _z_bytes_append_slice(_z_bytes_t *dst, _z_arc_slice_t *s) { } int8_t _z_bytes_append_bytes(_z_bytes_t *dst, _z_bytes_t *src) { - _Bool success = true; + int8_t res = _Z_RES_OK; for (size_t i = 0; i < _z_bytes_num_slices(src); ++i) { - _z_arc_slice_t *s = _z_bytes_get_slice(src, i); - success = success && _z_bytes_append_slice(dst, s) == _Z_RES_OK; - } - if (!success) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + _z_arc_slice_t s; + _z_arc_slice_move(&s, _z_bytes_get_slice(src, i)); + res = _z_bytes_append_slice(dst, &s); + if (res != _Z_RES_OK) { + break; + } } - _z_svec_release(&src->_slices); - return _Z_RES_OK; + _z_bytes_drop(src); + return res; } int8_t _z_bytes_serialize_from_pair(_z_bytes_t *out, _z_bytes_t *first, _z_bytes_t *second) { *out = _z_bytes_null(); _z_bytes_iterator_writer_t writer = _z_bytes_get_iterator_writer(out); - _Z_RETURN_IF_ERR(_z_bytes_iterator_writer_write(&writer, first)); + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_iterator_writer_write(&writer, first), _z_bytes_drop(second)); _Z_CLEAN_RETURN_IF_ERR(_z_bytes_iterator_writer_write(&writer, second), _z_bytes_drop(out)); return _Z_RES_OK; @@ -341,7 +342,6 @@ int64_t _z_bytes_reader_tell(const _z_bytes_reader_t *reader) { return reader->b int8_t _z_bytes_reader_read(_z_bytes_reader_t *reader, uint8_t *buf, size_t len) { uint8_t *buf_start = buf; - for (size_t i = reader->slice_idx; i < _z_bytes_num_slices(reader->bytes); ++i) { _z_arc_slice_t *s = _z_bytes_get_slice(reader->bytes, i); size_t remaining = _z_arc_slice_len(s) - reader->in_slice_idx; @@ -415,13 +415,12 @@ _z_bytes_iterator_t _z_bytes_get_iterator(const _z_bytes_t *bytes) { return (_z_bytes_iterator_t){._reader = _z_bytes_get_reader(bytes)}; } -_Bool _z_bytes_iterator_next(_z_bytes_iterator_t *iter, _z_bytes_t *b) { +int8_t _z_bytes_iterator_next(_z_bytes_iterator_t *iter, _z_bytes_t *b) { *b = _z_bytes_null(); _z_zint_t len; if (_z_bytes_reader_read_zint(&iter->_reader, &len) != _Z_RES_OK) { return _Z_ERR_DID_NOT_READ; } - return _z_bytes_reader_read_slices(&iter->_reader, len, b); } @@ -454,7 +453,7 @@ int8_t _z_bytes_writer_ensure_cache(_z_bytes_writer_t *writer) { } int8_t _z_bytes_writer_write(_z_bytes_writer_t *writer, const uint8_t *src, size_t len) { - if (writer->cache_size == 0) { // no cache append data as a single slice + if (writer->cache_size == 0) { // no cache - append data as a single slice _z_slice_t s = _z_slice_wrap_copy(src, len); if (s.len != len) return _Z_ERR_SYSTEM_OUT_OF_MEMORY; _z_arc_slice_t arc_s = _z_arc_slice_wrap(s, 0, len); @@ -485,7 +484,7 @@ _z_bytes_iterator_writer_t _z_bytes_get_iterator_writer(_z_bytes_t *bytes) { int8_t _z_bytes_iterator_writer_write(_z_bytes_iterator_writer_t *writer, _z_bytes_t *src) { uint8_t l_buf[16]; size_t l_len = _z_zsize_encode_buf(l_buf, _z_bytes_len(src)); - _Z_RETURN_IF_ERR(_z_bytes_writer_write(&writer->writer, l_buf, l_len)); + _Z_CLEAN_RETURN_IF_ERR(_z_bytes_writer_write(&writer->writer, l_buf, l_len), _z_bytes_drop(src)); _Z_CLEAN_RETURN_IF_ERR(_z_bytes_append_bytes(writer->writer.bytes, src), _z_bytes_drop(src)); return _Z_RES_OK; From 6c1cc65b6be414a8b6b463aac4a27d5a4dd64475 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 27 Jun 2024 22:25:08 +0200 Subject: [PATCH 24/25] use _ZP_UNUSED instead of (void)(...) --- include/zenoh-pico/collections/element.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/include/zenoh-pico/collections/element.h b/include/zenoh-pico/collections/element.h index 8f7dc507d..94a71403d 100644 --- a/include/zenoh-pico/collections/element.h +++ b/include/zenoh-pico/collections/element.h @@ -19,6 +19,7 @@ #include #include "zenoh-pico/system/platform.h" +#include "zenoh-pico/utils/result.h" /*-------- element functions --------*/ typedef size_t (*z_element_size_f)(void *e); @@ -62,13 +63,13 @@ static inline void _z_noop_clear(void *s) { (void)(s); } static inline void _z_noop_free(void **s) { (void)(s); } static inline void _z_noop_copy(void *dst, const void *src) { - (void)(dst); - (void)(src); + _ZP_UNUSED(dst); + _ZP_UNUSED(src); } static inline void _z_noop_move(void *dst, void *src) { - (void)(dst); - (void)(src); + _ZP_UNUSED(dst); + _ZP_UNUSED(src); } _Z_ELEM_DEFINE(_z_noop, _z_noop_t, _z_noop_size, _z_noop_clear, _z_noop_copy) From 8cb3ef02ca8566b4bcd6dc0856a0aea6664fe982 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 27 Jun 2024 23:37:49 +0200 Subject: [PATCH 25/25] fixed _z_bytes_reader_read return value; fixed public z_bytes api; added public z_bytes_api tests; --- CMakeLists.txt | 3 + include/zenoh-pico/api/olv_macros.h | 3 +- include/zenoh-pico/api/primitives.h | 1 + include/zenoh-pico/collections/bytes.h | 2 +- src/collections/bytes.c | 8 +- tests/z_api_bytes_test.c | 214 +++++++++++++++++++++++++ 6 files changed, 225 insertions(+), 6 deletions(-) create mode 100644 tests/z_api_bytes_test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index cba60c662..0da6badd7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -348,6 +348,7 @@ if(UNIX OR MSVC) add_executable(z_perf_tx ${PROJECT_SOURCE_DIR}/tests/z_perf_tx.c) add_executable(z_perf_rx ${PROJECT_SOURCE_DIR}/tests/z_perf_rx.c) add_executable(z_bytes_test ${PROJECT_SOURCE_DIR}/tests/z_bytes_test.c) + add_executable(z_api_bytes_test ${PROJECT_SOURCE_DIR}/tests/z_api_bytes_test.c) target_link_libraries(z_data_struct_test ${Libname}) target_link_libraries(z_channels_test ${Libname}) @@ -363,6 +364,7 @@ if(UNIX OR MSVC) target_link_libraries(z_perf_tx ${Libname}) target_link_libraries(z_perf_rx ${Libname}) target_link_libraries(z_bytes_test ${Libname}) + target_link_libraries(z_api_bytes_test ${Libname}) configure_file(${PROJECT_SOURCE_DIR}/tests/modularity.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/modularity.py COPYONLY) configure_file(${PROJECT_SOURCE_DIR}/tests/raweth.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/raweth.py COPYONLY) @@ -381,6 +383,7 @@ if(UNIX OR MSVC) add_test(z_api_null_drop_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_null_drop_test) add_test(z_api_double_drop_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_double_drop_test) add_test(z_bytes_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_bytes_test) + add_test(z_api_bytes_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_bytes_test) endif() if(BUILD_MULTICAST) diff --git a/include/zenoh-pico/api/olv_macros.h b/include/zenoh-pico/api/olv_macros.h index aee26849f..8c93f8010 100644 --- a/include/zenoh-pico/api/olv_macros.h +++ b/include/zenoh-pico/api/olv_macros.h @@ -91,7 +91,8 @@ } \ void z_##name##_drop(z_owned_##name##_t *obj) { \ if ((obj != NULL) && (obj->_val != NULL)) { \ - z_free(&obj->_val); \ + z_free(obj->_val); \ + obj->_val = NULL; \ } \ } diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index fe3979f1c..dfbd7ba55 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -1201,6 +1201,7 @@ _Z_OWNED_FUNCTIONS_DEF(z_loaned_sample_t, z_owned_sample_t, sample) _Z_OWNED_FUNCTIONS_DEF(z_loaned_query_t, z_owned_query_t, query) _Z_OWNED_FUNCTIONS_DEF(z_loaned_slice_t, z_owned_slice_t, slice) _Z_OWNED_FUNCTIONS_DEF(z_loaned_bytes_t, z_owned_bytes_t, bytes) +_Z_OWNED_FUNCTIONS_DEF(z_loaned_bytes_writer_t, z_owned_bytes_writer_t, bytes_writer) _Z_OWNED_FUNCTIONS_DEF(z_loaned_reply_err_t, z_owned_reply_err_t, reply_err) _Z_OWNED_FUNCTIONS_CLOSURE_DEF(z_owned_closure_sample_t, closure_sample) diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index 643c39acf..56672879c 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -89,7 +89,7 @@ _z_bytes_reader_t _z_bytes_get_reader(const _z_bytes_t *bytes); int8_t _z_bytes_reader_seek(_z_bytes_reader_t *reader, int64_t offset, int origin); int64_t _z_bytes_reader_tell(const _z_bytes_reader_t *reader); int8_t _z_bytes_reader_read_slices(_z_bytes_reader_t *reader, size_t len, _z_bytes_t *out); -int8_t _z_bytes_reader_read(_z_bytes_reader_t *reader, uint8_t *buf, size_t len); +size_t _z_bytes_reader_read(_z_bytes_reader_t *reader, uint8_t *buf, size_t len); typedef struct { _z_bytes_reader_t _reader; diff --git a/src/collections/bytes.c b/src/collections/bytes.c index 1dbd5168d..2298090d4 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -340,8 +340,9 @@ int8_t _z_bytes_reader_seek(_z_bytes_reader_t *reader, int64_t offset, int origi int64_t _z_bytes_reader_tell(const _z_bytes_reader_t *reader) { return reader->byte_idx; } -int8_t _z_bytes_reader_read(_z_bytes_reader_t *reader, uint8_t *buf, size_t len) { +size_t _z_bytes_reader_read(_z_bytes_reader_t *reader, uint8_t *buf, size_t len) { uint8_t *buf_start = buf; + size_t to_read = len; for (size_t i = reader->slice_idx; i < _z_bytes_num_slices(reader->bytes); ++i) { _z_arc_slice_t *s = _z_bytes_get_slice(reader->bytes, i); size_t remaining = _z_arc_slice_len(s) - reader->in_slice_idx; @@ -361,13 +362,12 @@ int8_t _z_bytes_reader_read(_z_bytes_reader_t *reader, uint8_t *buf, size_t len) if (len == 0) break; } - if (len > 0) return _Z_ERR_DID_NOT_READ; - return _Z_RES_OK; + return to_read - len; } int8_t __read_single_byte(uint8_t *b, void *context) { _z_bytes_reader_t *reader = (_z_bytes_reader_t *)context; - return _z_bytes_reader_read(reader, b, 1); + return _z_bytes_reader_read(reader, b, 1) == 1 ? _Z_RES_OK : _Z_ERR_DID_NOT_READ; } int8_t _z_bytes_reader_read_zint(_z_bytes_reader_t *reader, _z_zint_t *zint) { diff --git a/tests/z_api_bytes_test.c b/tests/z_api_bytes_test.c new file mode 100644 index 000000000..ca08dfd85 --- /dev/null +++ b/tests/z_api_bytes_test.c @@ -0,0 +1,214 @@ +// +// Copyright (c) 2024 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#include +#include +#include + +#include "zenoh-pico/api/primitives.h" +#include "zenoh-pico/api/types.h" + +#undef NDEBUG +#include + +void test_reader_seek(void) { + uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, 10); + + z_bytes_reader_t reader = z_bytes_get_reader(z_bytes_loan(&payload)); + assert(z_bytes_reader_tell(&reader) == 0); + + assert(0 == z_bytes_reader_seek(&reader, 5, SEEK_CUR)); + assert(z_bytes_reader_tell(&reader) == 5); + + assert(0 == z_bytes_reader_seek(&reader, 7, SEEK_SET)); + assert(z_bytes_reader_tell(&reader) == 7); + + assert(0 == z_bytes_reader_seek(&reader, -1, SEEK_END)); + assert(z_bytes_reader_tell(&reader) == 9); + + assert(z_bytes_reader_seek(&reader, 20, SEEK_SET) < 0); + + assert(0 == z_bytes_reader_seek(&reader, 5, SEEK_SET)); + assert(z_bytes_reader_tell(&reader) == 5); + + assert(z_bytes_reader_seek(&reader, 10, SEEK_CUR) < 0); + assert(z_bytes_reader_seek(&reader, 10, SEEK_END) < 0); + assert(z_bytes_reader_seek(&reader, -20, SEEK_END) < 0); + + z_bytes_drop(z_bytes_move(&payload)); +} + +void test_reader_read(void) { + uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + uint8_t data_out[10] = {0}; + + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, 10); + z_bytes_reader_t reader = z_bytes_get_reader(z_bytes_loan(&payload)); + + assert(5 == z_bytes_reader_read(&reader, data_out, 5)); + + z_bytes_reader_seek(&reader, 2, SEEK_CUR); + assert(2 == z_bytes_reader_read(&reader, data_out + 7, 2)); + + z_bytes_reader_seek(&reader, 5, SEEK_SET); + assert(2 == z_bytes_reader_read(&reader, data_out + 5, 2)); + + z_bytes_reader_seek(&reader, -1, SEEK_END); + assert(1 == z_bytes_reader_read(&reader, data_out + 9, 10)); + + assert(0 == z_bytes_reader_read(&reader, data_out, 10)); + + assert(!memcmp(data, data_out, 10)); + + z_bytes_drop(z_bytes_move(&payload)); +} + +void test_writer(void) { + uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + uint8_t data_out[10] = {0}; + + z_owned_bytes_t payload; + z_bytes_empty(&payload); + + z_owned_bytes_writer_t writer; + z_bytes_get_writer(z_bytes_loan_mut(&payload), &writer); + + assert(z_bytes_writer_write(z_bytes_writer_loan_mut(&writer), data, 3) == 0); + assert(z_bytes_writer_write(z_bytes_writer_loan_mut(&writer), data + 3, 5) == 0); + assert(z_bytes_writer_write(z_bytes_writer_loan_mut(&writer), data + 8, 2) == 0); + + z_bytes_writer_drop(z_bytes_writer_move(&writer)); + + z_bytes_reader_t reader = z_bytes_get_reader(z_bytes_loan(&payload)); + + assert(10 == z_bytes_reader_read(&reader, data_out, 10)); + assert(0 == memcmp(data, data_out, 10)); + + z_bytes_drop(z_bytes_move(&payload)); +} + +void test_slice(void) { + uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, 10); + + z_owned_slice_t out; + data[5] = 0; + z_bytes_deserialize_into_slice(z_bytes_loan(&payload), &out); + + assert(!memcmp(data, z_slice_data(z_slice_loan(&out)), 10)); + + z_owned_bytes_t payload2; + z_bytes_serialize_from_slice_copy(&payload2, data, 10); + data[5] = 5; + z_owned_slice_t out2; + z_bytes_deserialize_into_slice(z_bytes_loan(&payload2), &out2); + data[5] = 0; + assert(!memcmp(data, z_slice_data(z_slice_loan(&out2)), 10)); + + z_bytes_drop(z_bytes_move(&payload)); + z_bytes_drop(z_bytes_move(&payload2)); + z_slice_drop(z_slice_move(&out)); + z_slice_drop(z_slice_move(&out2)); +} + +#define TEST_ARITHMETIC(TYPE, EXT, VAL) \ + { \ + TYPE in = VAL, out; \ + z_owned_bytes_t payload; \ + z_bytes_serialize_from_##EXT(&payload, in); \ + z_bytes_deserialize_into_##EXT(z_bytes_loan(&payload), &out); \ + assert(in == out); \ + z_bytes_drop(z_bytes_move(&payload)); \ + } + +void test_arithmetic(void) { + TEST_ARITHMETIC(uint8_t, uint8, 5); + TEST_ARITHMETIC(uint16_t, uint16, 1000); + TEST_ARITHMETIC(uint32_t, uint32, 51000000); + TEST_ARITHMETIC(uint64_t, uint64, 1000000000005); + + TEST_ARITHMETIC(int8_t, int8, 5); + TEST_ARITHMETIC(int16_t, int16, -1000); + TEST_ARITHMETIC(int32_t, int32, 51000000); + TEST_ARITHMETIC(int64_t, int64, -1000000000005); + + TEST_ARITHMETIC(float, float, 10.1f); + TEST_ARITHMETIC(double, double, -105.001); +} + +bool iter_body(z_owned_bytes_t* b, void* context) { + uint8_t* val = (uint8_t*)context; + if (*val >= 10) { + return false; + } else { + z_bytes_serialize_from_uint8(b, *val); + } + *val = *val + 1; + return true; +} + +void test_iter(void) { + uint8_t data_out[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + + z_owned_bytes_t payload; + uint8_t context = 0; + z_bytes_serialize_from_iter(&payload, iter_body, (void*)(&context)); + + z_bytes_iterator_t it = z_bytes_get_iterator(z_bytes_loan(&payload)); + + size_t i = 0; + z_owned_bytes_t out; + while (z_bytes_iterator_next(&it, &out)) { + uint8_t res; + z_bytes_deserialize_into_uint8(z_bytes_loan(&out), &res); + assert(res == data_out[i]); + i++; + z_bytes_drop(z_bytes_move(&out)); + } + assert(i == 10); + z_bytes_drop(z_bytes_move(&payload)); +} + +void test_pair(void) { + z_owned_bytes_t payload, payload1, payload2, payload1_out, payload2_out; + z_bytes_serialize_from_int16(&payload1, -500); + z_bytes_serialize_from_double(&payload2, 123.45); + z_bytes_serialize_from_pair(&payload, z_bytes_move(&payload1), z_bytes_move(&payload2)); + + z_bytes_deserialize_into_pair(z_bytes_loan(&payload), &payload1_out, &payload2_out); + + int16_t i; + double d; + z_bytes_deserialize_into_int16(z_bytes_loan(&payload1_out), &i); + z_bytes_deserialize_into_double(z_bytes_loan(&payload2_out), &d); + + assert(i == -500); + assert(d == 123.45); +} + +int main(void) { + test_reader_seek(); + test_reader_read(); + test_writer(); + test_slice(); + test_arithmetic(); + test_iter(); + test_pair(); +}