From 7d3f8b328ed3162f58d13dab338d159a78e93ae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 27 Jan 2023 06:28:32 +0100 Subject: [PATCH 001/181] Fix static build (#154) (#155) * Avoid multiple definition on static builds * Allow static or shared libs to be user defined in Makefile * CI building static and shared libraries --- .../{build.yaml => build-shared.yaml} | 9 ++-- .github/workflows/build-static.yaml | 51 +++++++++++++++++++ BSDmakefile | 2 +- GNUmakefile | 2 +- tests/z_msgcodec_test.c | 12 ++--- 5 files changed, 65 insertions(+), 11 deletions(-) rename .github/workflows/{build.yaml => build-shared.yaml} (86%) create mode 100644 .github/workflows/build-static.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build-shared.yaml similarity index 86% rename from .github/workflows/build.yaml rename to .github/workflows/build-shared.yaml index 113981c75..acfae70c1 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build-shared.yaml @@ -11,7 +11,7 @@ # Contributors: # ZettaScale Zenoh Team, # -name: build +name: build-shared on: push: @@ -33,9 +33,11 @@ jobs: run: make all env: BUILD_TYPE: Debug + BUILD_SHARED_LIBS: ON BUILD_TESTING: ON - BUILD_MULTICAST: OFF - BUILD_INTEGRATION: OFF + BUILD_MULTICAST: ON + BUILD_INTEGRATION: ON + BUILD_TOOLS: ON crossbuilds: name: Build on ubuntu-latest @@ -46,3 +48,4 @@ jobs: run: make crossbuilds env: BUILD_TYPE: Debug + BUILD_SHARED_LIBS: ON diff --git a/.github/workflows/build-static.yaml b/.github/workflows/build-static.yaml new file mode 100644 index 000000000..0bc1cb99f --- /dev/null +++ b/.github/workflows/build-static.yaml @@ -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, +# +name: build-static + +on: + push: + branches: [ '**' ] + pull_request: + branches: [ '**' ] + +jobs: + build: + name: Build on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macOS-latest] + steps: + - uses: actions/checkout@v2 + - name: Compile debug + run: make all + env: + BUILD_TYPE: Debug + BUILD_SHARED_LIBS: OFF + BUILD_TESTING: ON + BUILD_MULTICAST: ON + BUILD_INTEGRATION: ON + BUILD_TOOLS: ON + + crossbuilds: + name: Build on ubuntu-latest + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Crosscompile debug + run: make crossbuilds + env: + BUILD_TYPE: Debug + BUILD_SHARED_LIBS: OFF diff --git a/BSDmakefile b/BSDmakefile index 51cb587f4..c1ffe3d5a 100644 --- a/BSDmakefile +++ b/BSDmakefile @@ -58,7 +58,7 @@ CROSSIMG_PREFIX=zenoh-pico_ # NOTES: # - ARM: old versions of dockcross/dockcross were creating some issues since they used an old GCC (4.8.3) which lacks (even using -std=gnu11) -CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_MULTICAST=$(BUILD_MULTICAST) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -H. +CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_MULTICAST=$(BUILD_MULTICAST) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H. all: make diff --git a/GNUmakefile b/GNUmakefile index 300a84316..062fc7261 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -58,7 +58,7 @@ CROSSIMG_PREFIX=zenoh-pico_ # NOTES: # - ARM: old versions of dockcross/dockcross were creating some issues since they used an old GCC (4.8.3) which lacks (even using -std=gnu11) -CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_MULTICAST=$(BUILD_MULTICAST) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -H. +CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_MULTICAST=$(BUILD_MULTICAST) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H. all: make diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index d8f5923bc..c821dd119 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -2464,7 +2464,7 @@ void batch(void) { } /*------------------ Fragmentation ------------------*/ -_z_transport_message_t _z_frame_header(_Bool is_reliable, _Bool is_fragment, _Bool is_final, _z_zint_t sn) { +_z_transport_message_t z_frame_header(_Bool is_reliable, _Bool is_fragment, _Bool is_final, _z_zint_t sn) { // Create the frame session message that carries the zenoh message _z_transport_message_t t_msg; t_msg._attachment = NULL; @@ -2493,21 +2493,21 @@ _z_transport_message_t _z_frame_header(_Bool is_reliable, _Bool is_fragment, _Bo return t_msg; } -void _z_wbuf_prepare(_z_wbuf_t *wbf) { +void z_wbuf_prepare(_z_wbuf_t *wbf) { // Clear the buffer for serialization _z_wbuf_reset(wbf); for (size_t i = 0; i < _z_wbuf_space_left(wbf); i++) _z_wbuf_put(wbf, 0xff, i); } -int8_t _z_serialize_zenoh_fragment(_z_wbuf_t *dst, _z_wbuf_t *src, _Bool is_reliable, size_t sn) { +int8_t z_serialize_zenoh_fragment(_z_wbuf_t *dst, _z_wbuf_t *src, _Bool is_reliable, size_t sn) { // Assume first that this is not the final fragment _Bool is_final = false; do { // Mark the buffer for the writing operation size_t w_pos = _z_wbuf_get_wpos(dst); // Get the frame header - _z_transport_message_t f_hdr = _z_frame_header(is_reliable, true, is_final, sn); + _z_transport_message_t f_hdr = z_frame_header(is_reliable, true, is_final, sn); // Encode the frame header int8_t res = _z_transport_message_encode(dst, &f_hdr); if (res == _Z_RES_OK) { @@ -2565,13 +2565,13 @@ void fragmentation(void) { printf(" - Start fragmenting\n"); while (_z_wbuf_len(&fbf) > 0) { // Clear the buffer for serialization - _z_wbuf_prepare(&wbf); + z_wbuf_prepare(&wbf); // Get the fragment sequence number sn = (sn + 1) % sn_resolution; size_t written = _z_wbuf_len(&fbf); - int8_t res = _z_serialize_zenoh_fragment(&wbf, &fbf, is_reliable, sn); + int8_t res = z_serialize_zenoh_fragment(&wbf, &fbf, is_reliable, sn); assert(res == _Z_RES_OK); (void)(res); written = written - _z_wbuf_len(&fbf); From c824c8ddc3a92ef1be5152073bd70e1667b4bd00 Mon Sep 17 00:00:00 2001 From: David Corbeil Date: Mon, 6 Feb 2023 07:28:03 -0800 Subject: [PATCH 002/181] Fix build on newer Zephyr commits (#161) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Handle situation where no extra stack is needed when Zephyr is not in testing mode. * Keep backwards compatibility with older Zephyr versions still using CONFIG_TEST_EXTRA_STACKSIZE * Simplify ifdef-elif-else --------- Co-authored-by: Carlos Guimarães --- src/system/zephyr/system.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index 5b124e549..33ec88c22 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -52,7 +52,15 @@ void z_free(void *ptr) { k_free(ptr); } #if Z_MULTI_THREAD == 1 #define Z_THREADS_NUM 4 -#define Z_PTHREAD_STACK_SIZE_DEFAULT CONFIG_MAIN_STACK_SIZE + CONFIG_TEST_EXTRA_STACKSIZE + +#ifdef CONFIG_TEST_EXTRA_STACK_SIZE + #define Z_PTHREAD_STACK_SIZE_DEFAULT CONFIG_MAIN_STACK_SIZE + CONFIG_TEST_EXTRA_STACK_SIZE +#elif CONFIG_TEST_EXTRA_STACKSIZE + #define Z_PTHREAD_STACK_SIZE_DEFAULT CONFIG_MAIN_STACK_SIZE + CONFIG_TEST_EXTRA_STACKSIZE +#else + #define Z_PTHREAD_STACK_SIZE_DEFAULT CONFIG_MAIN_STACK_SIZE +#endif + K_THREAD_STACK_ARRAY_DEFINE(thread_stack_area, Z_THREADS_NUM, Z_PTHREAD_STACK_SIZE_DEFAULT); static int thread_index = 0; From 62e6a08aaba1226360ccea1544d44fb331239dda Mon Sep 17 00:00:00 2001 From: David Corbeil Date: Mon, 6 Feb 2023 07:29:10 -0800 Subject: [PATCH 003/181] Replaced deprecated zephyr.h with kernel.h (#160) --- include/zenoh-pico/net/zenoh-pico.h | 2 +- include/zenoh-pico/system/platform/zephyr.h | 2 +- src/system/zephyr/system.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/zenoh-pico/net/zenoh-pico.h b/include/zenoh-pico/net/zenoh-pico.h index 7b4ced88f..80d1f89c2 100644 --- a/include/zenoh-pico/net/zenoh-pico.h +++ b/include/zenoh-pico/net/zenoh-pico.h @@ -25,7 +25,7 @@ #include "zenoh-pico/net/subscribe.h" #if defined(ZENOH_ZEPHYR) -#include +#include #endif #endif /* ZENOH_PICO_NET_H */ diff --git a/include/zenoh-pico/system/platform/zephyr.h b/include/zenoh-pico/system/platform/zephyr.h index bd4c78284..19c8795c3 100644 --- a/include/zenoh-pico/system/platform/zephyr.h +++ b/include/zenoh-pico/system/platform/zephyr.h @@ -16,7 +16,7 @@ #define ZENOH_PICO_SYSTEM_ZEPHYR_TYPES_H #include -#include +#include #include "zenoh-pico/config.h" diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index 33ec88c22..a864b05a1 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include "zenoh-pico/config.h" #include "zenoh-pico/system/platform.h" From 8b980d9f7c8492081847499ae5d6442162f053b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Mon, 6 Feb 2023 16:29:48 +0100 Subject: [PATCH 004/181] Fix C99 examples failed build introduced in #150 (#162) --- examples/unix/c99/z_get.c | 4 ++-- examples/unix/c99/z_pull.c | 4 ++-- examples/unix/c99/z_queryable.c | 4 ++-- examples/unix/c99/z_sub.c | 4 ++-- examples/unix/c99/z_sub_st.c | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 12fa8db82..1fa6e2aa5 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -28,8 +28,8 @@ void reply_handler(z_owned_reply_t *reply, void *ctx) { if (z_reply_is_ok(reply)) { z_sample_t sample = z_reply_ok(reply); z_owned_str_t keystr = z_keyexpr_to_string(sample.keyexpr); - printf(">> Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample.payload.len, sample.payload.start); - z_drop(z_move(keystr)); + printf(">> Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)sample.payload.len, sample.payload.start); + z_str_drop(z_str_move(&keystr)); } else { printf(">> Received an error\n"); } diff --git a/examples/unix/c99/z_pull.c b/examples/unix/c99/z_pull.c index dbc1c51e7..bb21dd313 100644 --- a/examples/unix/c99/z_pull.c +++ b/examples/unix/c99/z_pull.c @@ -21,9 +21,9 @@ void data_handler(const z_sample_t *sample, void *ctx) { (void)(ctx); z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample->payload.len, + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)sample->payload.len, sample->payload.start); - z_drop(z_move(keystr)); + z_str_drop(z_str_move(&keystr)); } int main(int argc, char **argv) { diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index d48ed1c0a..1a40967c4 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -25,11 +25,11 @@ void query_handler(const z_query_t *query, void *ctx) { (void)(ctx); z_owned_str_t keystr = z_keyexpr_to_string(z_query_keyexpr(query)); z_bytes_t pred = z_query_parameters(query); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_loan(keystr), (int)pred.len, pred.start); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_loan(&keystr), (int)pred.len, pred.start); z_query_reply_options_t options = z_query_reply_options_default(); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); z_query_reply(query, z_keyexpr(keyexpr), (const unsigned char *)value, strlen(value), &options); - z_drop(z_move(keystr)); + z_str_drop(z_str_move(&keystr)); } int main(int argc, char **argv) { diff --git a/examples/unix/c99/z_sub.c b/examples/unix/c99/z_sub.c index 160092628..01aa4ebb8 100644 --- a/examples/unix/c99/z_sub.c +++ b/examples/unix/c99/z_sub.c @@ -22,9 +22,9 @@ void data_handler(const z_sample_t *sample, void *arg) { (void)(arg); z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample->payload.len, + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)sample->payload.len, sample->payload.start); - z_drop(z_move(keystr)); + z_str_drop(z_str_move(&keystr)); } int main(int argc, char **argv) { diff --git a/examples/unix/c99/z_sub_st.c b/examples/unix/c99/z_sub_st.c index 9813487fe..cdc216623 100644 --- a/examples/unix/c99/z_sub_st.c +++ b/examples/unix/c99/z_sub_st.c @@ -22,9 +22,9 @@ void data_handler(const z_sample_t *sample, void *arg) { (void)(arg); z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample->payload.len, + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)sample->payload.len, sample->payload.start); - z_drop(z_move(keystr)); + z_str_drop(z_str_move(&keystr)); } int main(int argc, char **argv) { From e308e2dff158f4fdb0a98bb4f4842848842f9e7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 8 Feb 2023 09:42:19 +0100 Subject: [PATCH 005/181] Ease the integration of Zenoh-Pico into cpp projects (#163) (#164) * Be aware of cplusplus * Remove extern C directives * Extern C internally * Fix imcompatibility with Zephyr * Make which type is atonic clearer * Finer definition of external C * Fix linkage from cpp * Also on system-specific --- examples/arduino/z_get.ino | 3 - examples/arduino/z_pub.ino | 3 - examples/arduino/z_pull.ino | 3 - examples/arduino/z_queryable.ino | 3 - examples/arduino/z_scout.ino | 3 - examples/arduino/z_sub.ino | 3 - examples/mbed/z_get.cpp | 3 - examples/mbed/z_pub.cpp | 3 - examples/mbed/z_pull.cpp | 3 - examples/mbed/z_queryable.cpp | 3 - examples/mbed/z_scout.cpp | 3 - examples/mbed/z_sub.cpp | 3 - include/zenoh-pico/api/constants.h | 8 + include/zenoh-pico/api/macros.h | 191 ++++++++++++++++++++++- include/zenoh-pico/api/primitives.h | 22 ++- include/zenoh-pico/api/types.h | 21 ++- include/zenoh-pico/collections/pointer.h | 11 +- include/zenoh-pico/collections/string.h | 2 +- include/zenoh-pico/system/platform.h | 8 + src/api/api.c | 27 ++-- src/collections/string.c | 2 +- src/net/config.c | 10 +- 22 files changed, 273 insertions(+), 65 deletions(-) diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index f05a4e694..f099fa3c2 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -14,10 +14,7 @@ #include #include - -extern "C" { #include -} // WiFi-specific parameters #define SSID "SSID" diff --git a/examples/arduino/z_pub.ino b/examples/arduino/z_pub.ino index a3d4a29e8..c56bc0adc 100644 --- a/examples/arduino/z_pub.ino +++ b/examples/arduino/z_pub.ino @@ -14,10 +14,7 @@ #include #include - -extern "C" { #include -} // WiFi-specific parameters #define SSID "SSID" diff --git a/examples/arduino/z_pull.ino b/examples/arduino/z_pull.ino index 821f20e36..90b6fdf02 100644 --- a/examples/arduino/z_pull.ino +++ b/examples/arduino/z_pull.ino @@ -14,10 +14,7 @@ #include #include - -extern "C" { #include -} // WiFi-specific parameters #define SSID "SSID" diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index bbb0162e0..732f8c55b 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -14,10 +14,7 @@ #include #include - -extern "C" { #include -} // WiFi-specific parameters #define SSID "SSID" diff --git a/examples/arduino/z_scout.ino b/examples/arduino/z_scout.ino index 149eaf282..bc7e2f17d 100644 --- a/examples/arduino/z_scout.ino +++ b/examples/arduino/z_scout.ino @@ -14,10 +14,7 @@ #include #include - -extern "C" { #include -} // WiFi-specific parameters #define SSID "SSID" diff --git a/examples/arduino/z_sub.ino b/examples/arduino/z_sub.ino index 1c0e59114..83719af1c 100644 --- a/examples/arduino/z_sub.ino +++ b/examples/arduino/z_sub.ino @@ -14,10 +14,7 @@ #include #include - -extern "C" { #include -} // WiFi-specific parameters #define SSID "SSID" diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index 68acd6dd3..d77f82aeb 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -14,10 +14,7 @@ #include #include #include - -extern "C" { #include -} #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 diff --git a/examples/mbed/z_pub.cpp b/examples/mbed/z_pub.cpp index e4f5a79be..4b44ae93e 100644 --- a/examples/mbed/z_pub.cpp +++ b/examples/mbed/z_pub.cpp @@ -14,10 +14,7 @@ #include #include #include - -extern "C" { #include -} #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 diff --git a/examples/mbed/z_pull.cpp b/examples/mbed/z_pull.cpp index cf908ebc7..767fa595b 100644 --- a/examples/mbed/z_pull.cpp +++ b/examples/mbed/z_pull.cpp @@ -14,10 +14,7 @@ #include #include #include - -extern "C" { #include -} #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index 29dae14fd..50c230aef 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -14,10 +14,7 @@ #include #include #include - -extern "C" { #include -} #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 diff --git a/examples/mbed/z_scout.cpp b/examples/mbed/z_scout.cpp index a4f5ec947..8d3ee55cc 100644 --- a/examples/mbed/z_scout.cpp +++ b/examples/mbed/z_scout.cpp @@ -14,10 +14,7 @@ #include #include #include - -extern "C" { #include -} void fprintzid(FILE *stream, z_bytes_t zid) { if (zid.start == NULL) { diff --git a/examples/mbed/z_sub.cpp b/examples/mbed/z_sub.cpp index 64fae94fa..903abc816 100644 --- a/examples/mbed/z_sub.cpp +++ b/examples/mbed/z_sub.cpp @@ -14,10 +14,7 @@ #include #include #include - -extern "C" { #include -} #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 diff --git a/include/zenoh-pico/api/constants.h b/include/zenoh-pico/api/constants.h index 4b2327c42..221379489 100644 --- a/include/zenoh-pico/api/constants.h +++ b/include/zenoh-pico/api/constants.h @@ -17,6 +17,10 @@ #define Z_SELECTOR_TIME "_time=" #define Z_SELECTOR_QUERY_MATCH "_anyke" +#ifdef __cplusplus +extern "C" { +#endif + /** * Whatami values, defined as a bitmask. * @@ -225,4 +229,8 @@ typedef enum { } z_query_target_t; #define Z_QUERY_TARGET_DEFAULT Z_QUERY_TARGET_BEST_MATCHING +#ifdef __cplusplus +} +#endif + #endif /* ZENOH_PICO_API_CONSTANTS_H */ diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index e9346fcaf..61ee5030f 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -19,6 +19,8 @@ #if ZENOH_C_STANDARD != 99 +#ifndef __cplusplus + /** * Defines a generic function for loaning any of the ``z_owned_X_t`` types. * @@ -156,6 +158,193 @@ */ #define z_closure(...) _z_closure_overloader(__VA_ARGS__, 0, 0) +#else + +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 z_session_t type; +}; +template <> +struct zenoh_loan_type { + typedef z_keyexpr_t type; +}; +template <> +struct zenoh_loan_type { + typedef z_config_t type; +}; +template <> +struct zenoh_loan_type { + typedef z_publisher_t type; +}; +template <> +struct zenoh_loan_type { + typedef z_pull_subscriber_t type; +}; +template <> +struct zenoh_loan_type { + typedef z_hello_t type; +}; + +template <> +inline z_session_t z_loan(const z_owned_session_t& x) { + return z_session_loan(&x); +} +template <> +inline z_keyexpr_t z_loan(const z_owned_keyexpr_t& x) { + return z_keyexpr_loan(&x); +} +template <> +inline z_config_t z_loan(const z_owned_config_t& x) { + return z_config_loan(&x); +} +template <> +inline z_publisher_t z_loan(const z_owned_publisher_t& x) { + return z_publisher_loan(&x); +} +template <> +inline z_pull_subscriber_t z_loan(const z_owned_pull_subscriber_t& x) { + return z_pull_subscriber_loan(&x); +} +template <> +inline z_hello_t z_loan(const z_owned_hello_t& x) { + return z_hello_loan(&x); +} + +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 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 <> +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_pull_subscriber_t* v) { + return z_undeclare_pull_subscriber(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); +} + +inline _Bool z_check(const z_owned_session_t& v) { return z_session_check(&v); } +inline _Bool z_check(const z_owned_publisher_t& v) { return z_publisher_check(&v); } +inline _Bool z_check(const z_owned_keyexpr_t& v) { return z_keyexpr_check(&v); } +inline _Bool z_check(const z_keyexpr_t& v) { return z_keyexpr_is_initialized(&v); } +inline _Bool z_check(const z_owned_config_t& v) { return z_config_check(&v); } +inline _Bool z_check(const z_owned_scouting_config_t& v) { return z_scouting_config_check(&v); } +inline _Bool z_check(const z_owned_subscriber_t& v) { return z_subscriber_check(&v); } +inline _Bool z_check(const z_owned_pull_subscriber_t& v) { return z_pull_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_hello_t& v) { return z_hello_check(&v); } + +#define _z_closure_overloader(callback, droper, ctx, ...) \ + { .context = const_cast(static_cast(ctx)), .call = callback, .drop = droper } +#define z_closure(...) _z_closure_overloader(__VA_ARGS__, NULL, NULL) +#define z_move(x) (&x) + +#endif + #endif /* ZENOH_C_STANDARD != 99 */ -#endif /* ZENOH_PICO_API_MACROS_H */ +#endif /* ZENOH_PICO_API_MACROS_H */ \ No newline at end of file diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 4e79d9965..b34a6be83 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -23,7 +23,23 @@ #include "zenoh-pico/net/session.h" #include "zenoh-pico/net/subscribe.h" +#ifdef __cplusplus +extern "C" { +#endif + /********* Data Types Handlers *********/ +/** + * Constructs a :c:type:`z_string_t` departing from a ``const char *``. + * It is a loaned key expression that aliases ``value``. + * + * Parameters: + * value: Pointer to null terminated string. + * + * Returns: + * The :c:type:`z_string_t` corresponding to the given string. + */ +z_string_t z_string_make(const char *value); + /** * Constructs a :c:type:`z_keyexpr_t` departing from a string. * It is a loaned key expression that aliases ``name``. @@ -73,7 +89,7 @@ z_owned_str_t zp_keyexpr_resolve(z_session_t zs, z_keyexpr_t keyexpr); * Returns: * Returns ``true`` if the keyexpr is valid, or ``false`` otherwise. */ -_Bool z_keyexpr_is_initialized(z_keyexpr_t *keyexpr); +_Bool z_keyexpr_is_initialized(const z_keyexpr_t *keyexpr); /** * Check if a given keyexpr is valid and in its canonical form. @@ -1335,4 +1351,8 @@ zp_send_join_options_t zp_send_join_options_default(void); */ int8_t zp_send_join(z_session_t zs, const zp_send_join_options_t *options); +#ifdef __cplusplus +} +#endif + #endif /* ZENOH_PICO_API_PRIMITIVES_H */ diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 0e993123e..0953764a1 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -20,6 +20,10 @@ #include "zenoh-pico/net/subscribe.h" #include "zenoh-pico/protocol/core.h" +#ifdef __cplusplus +extern "C" { +#endif + /* Owned types */ #define _OWNED_TYPE_PTR(type, name) \ typedef struct { \ @@ -413,14 +417,8 @@ typedef _z_reply_data_t z_reply_data_t; typedef _z_reply_t z_reply_t; _OWNED_TYPE_PTR(z_reply_t, reply) -#define _TYPEDEF_ARRAY(type, alias, elem, name) \ - typedef type alias; \ - static inline elem *z_##name##_array_get(const alias *a, size_t k) { return _z_##name##_array_get(a, k); } \ - static inline size_t z_##name##_array_len(const alias *a) { return _z_##name##_array_len(a); } \ - static inline _Bool z_##name##_array_is_empty(const alias *a) { return _z_##name##_array_is_empty(a); } - /** - * Represents an array of ``char *``. + * Represents an array of ``z_str_t``. * * Operations over :c:type:`z_str_array_t` must be done using the provided functions: * @@ -428,7 +426,10 @@ _OWNED_TYPE_PTR(z_reply_t, reply) * - ``size_t z_str_array_len(z_str_array_t *a);`` * - ``_Bool z_str_array_array_is_empty(z_str_array_t *a);`` */ -_TYPEDEF_ARRAY(_z_str_array_t, z_str_array_t, char *, str) +typedef _z_str_array_t z_str_array_t; +z_str_t *z_str_array_get(const z_str_array_t *a, size_t k); +size_t z_str_array_len(const z_str_array_t *a); +_Bool z_str_array_is_empty(const z_str_array_t *a); _OWNED_TYPE_PTR(z_str_array_t, str_array) typedef void (*_z_dropper_handler_t)(void *arg); @@ -522,4 +523,8 @@ typedef struct { void *context; } z_owned_closure_zid_t; +#ifdef __cplusplus +} +#endif + #endif /* ZENOH_PICO_API_TYPES_H */ diff --git a/include/zenoh-pico/collections/pointer.h b/include/zenoh-pico/collections/pointer.h index 88eb7563b..a1b7730b2 100644 --- a/include/zenoh-pico/collections/pointer.h +++ b/include/zenoh-pico/collections/pointer.h @@ -19,19 +19,26 @@ #include #if ZENOH_C_STANDARD != 99 +#ifndef __cplusplus #include +#define z_atomic(X) _Atomic X +#else +#include +#define z_atomic(X) std::atomic +using namespace std; +#endif /*------------------ Internal Array Macros ------------------*/ #define _Z_POINTER_DEFINE(name, type) \ typedef struct { \ type##_t *ptr; \ - atomic_uint *_cnt; \ + z_atomic(unsigned int) * _cnt; \ } name##_sptr_t; \ static inline name##_sptr_t name##_sptr_new(type##_t val) { \ name##_sptr_t p; \ p.ptr = (type##_t *)z_malloc(sizeof(type##_t)); \ if (p.ptr != NULL) { \ - p._cnt = (atomic_uint *)z_malloc(sizeof(atomic_uint)); \ + p._cnt = (z_atomic(unsigned int) *)z_malloc(sizeof(z_atomic(unsigned int) *)); \ if (p._cnt != NULL) { \ *p.ptr = val; \ atomic_store_explicit(p._cnt, 1, memory_order_relaxed); \ diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index 764bd282d..04fcbd3ff 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -72,7 +72,7 @@ typedef struct { char *val; } _z_string_t; -_z_string_t z_string_make(const char *value); +_z_string_t _z_string_make(const char *value); void _z_string_copy(_z_string_t *dst, const _z_string_t *src); void _z_string_move(_z_string_t *dst, _z_string_t *src); void _z_string_move_str(_z_string_t *dst, char *src); diff --git a/include/zenoh-pico/system/platform.h b/include/zenoh-pico/system/platform.h index 3c7ff25d1..3a909b7a0 100644 --- a/include/zenoh-pico/system/platform.h +++ b/include/zenoh-pico/system/platform.h @@ -38,6 +38,10 @@ #error "Unknown platform" #endif +#ifdef __cplusplus +extern "C" { +#endif + /*------------------ Random ------------------*/ uint8_t z_random_u8(void); uint16_t z_random_u16(void); @@ -90,4 +94,8 @@ unsigned long z_time_elapsed_us(z_time_t *time); unsigned long z_time_elapsed_ms(z_time_t *time); unsigned long z_time_elapsed_s(z_time_t *time); +#ifdef __cplusplus +} +#endif + #endif /* ZENOH_PICO_SYSTEM_COMMON_H */ diff --git a/src/api/api.c b/src/api/api.c index 07dfae225..702c76c05 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -17,6 +17,7 @@ #include #include "zenoh-pico/api/primitives.h" +#include "zenoh-pico/api/types.h" #include "zenoh-pico/config.h" #include "zenoh-pico/net/config.h" #include "zenoh-pico/net/logger.h" @@ -30,6 +31,12 @@ #include "zenoh-pico/session/utils.h" /********* Data Types Handlers *********/ +z_string_t z_string_make(const char *value) { return _z_string_make(value); } + +z_str_t *z_str_array_get(const z_str_array_t *a, size_t k) { return _z_str_array_get(a, k); } +size_t z_str_array_len(const z_str_array_t *a) { return _z_str_array_len(a); } +_Bool z_str_array_is_empty(const z_str_array_t *a) { return _z_str_array_is_empty(a); } + z_keyexpr_t z_keyexpr(const char *name) { return _z_rname(name); } z_owned_str_t z_keyexpr_to_string(z_keyexpr_t keyexpr) { @@ -57,7 +64,7 @@ z_owned_str_t zp_keyexpr_resolve(z_session_t zs, z_keyexpr_t keyexpr) { return ret; } -_Bool z_keyexpr_is_initialized(z_keyexpr_t *keyexpr) { +_Bool z_keyexpr_is_initialized(const z_keyexpr_t *keyexpr) { _Bool ret = false; if ((keyexpr->_id != Z_RESOURCE_ID_NONE) || (keyexpr->_suffix != NULL)) { @@ -160,9 +167,9 @@ int8_t zp_config_insert(z_config_t config, uint8_t key, z_string_t value) { z_owned_scouting_config_t z_scouting_config_default(void) { _z_scouting_config_t *sc = _z_config_empty(); - _zp_config_insert(sc, Z_CONFIG_MULTICAST_LOCATOR_KEY, z_string_make(Z_CONFIG_MULTICAST_LOCATOR_DEFAULT)); - _zp_config_insert(sc, Z_CONFIG_SCOUTING_TIMEOUT_KEY, z_string_make(Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT)); - _zp_config_insert(sc, Z_CONFIG_SCOUTING_WHAT_KEY, z_string_make(Z_CONFIG_SCOUTING_WHAT_DEFAULT)); + _zp_config_insert(sc, Z_CONFIG_MULTICAST_LOCATOR_KEY, _z_string_make(Z_CONFIG_MULTICAST_LOCATOR_DEFAULT)); + _zp_config_insert(sc, Z_CONFIG_SCOUTING_TIMEOUT_KEY, _z_string_make(Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT)); + _zp_config_insert(sc, Z_CONFIG_SCOUTING_WHAT_KEY, _z_string_make(Z_CONFIG_SCOUTING_WHAT_DEFAULT)); return (z_owned_scouting_config_t){._value = sc}; } @@ -173,23 +180,23 @@ z_owned_scouting_config_t z_scouting_config_from(z_config_t c) { char *opt; opt = _z_config_get(c._val, Z_CONFIG_MULTICAST_LOCATOR_KEY); if (opt == NULL) { - _zp_config_insert(sc, Z_CONFIG_MULTICAST_LOCATOR_KEY, z_string_make(Z_CONFIG_MULTICAST_LOCATOR_DEFAULT)); + _zp_config_insert(sc, Z_CONFIG_MULTICAST_LOCATOR_KEY, _z_string_make(Z_CONFIG_MULTICAST_LOCATOR_DEFAULT)); } else { - _zp_config_insert(sc, Z_CONFIG_MULTICAST_LOCATOR_KEY, z_string_make(opt)); + _zp_config_insert(sc, Z_CONFIG_MULTICAST_LOCATOR_KEY, _z_string_make(opt)); } opt = _z_config_get(c._val, Z_CONFIG_SCOUTING_TIMEOUT_KEY); if (opt == NULL) { - _zp_config_insert(sc, Z_CONFIG_SCOUTING_TIMEOUT_KEY, z_string_make(Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT)); + _zp_config_insert(sc, Z_CONFIG_SCOUTING_TIMEOUT_KEY, _z_string_make(Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT)); } else { - _zp_config_insert(sc, Z_CONFIG_SCOUTING_TIMEOUT_KEY, z_string_make(opt)); + _zp_config_insert(sc, Z_CONFIG_SCOUTING_TIMEOUT_KEY, _z_string_make(opt)); } opt = _z_config_get(c._val, Z_CONFIG_SCOUTING_WHAT_KEY); if (opt == NULL) { - _zp_config_insert(sc, Z_CONFIG_SCOUTING_WHAT_KEY, z_string_make(Z_CONFIG_SCOUTING_WHAT_DEFAULT)); + _zp_config_insert(sc, Z_CONFIG_SCOUTING_WHAT_KEY, _z_string_make(Z_CONFIG_SCOUTING_WHAT_DEFAULT)); } else { - _zp_config_insert(sc, Z_CONFIG_SCOUTING_WHAT_KEY, z_string_make(opt)); + _zp_config_insert(sc, Z_CONFIG_SCOUTING_WHAT_KEY, _z_string_make(opt)); } return (z_owned_scouting_config_t){._value = sc}; diff --git a/src/collections/string.c b/src/collections/string.c index 962e19bca..e5d77f69a 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -18,7 +18,7 @@ #include /*-------- string --------*/ -_z_string_t z_string_make(const char *value) { +_z_string_t _z_string_make(const char *value) { _z_string_t s; s.val = _z_str_clone(value); s.len = strlen(value); diff --git a/src/net/config.c b/src/net/config.c index 74ec814ae..ae6ab4c65 100644 --- a/src/net/config.c +++ b/src/net/config.c @@ -30,15 +30,15 @@ _z_config_t *_z_config_default(void) { return _z_config_client(NULL); } _z_config_t *_z_config_client(const char *locator) { _z_config_t *ps = _z_config_empty(); if (ps != NULL) { - _zp_config_insert(ps, Z_CONFIG_MODE_KEY, z_string_make(Z_CONFIG_MODE_CLIENT)); + _zp_config_insert(ps, Z_CONFIG_MODE_KEY, _z_string_make(Z_CONFIG_MODE_CLIENT)); if (locator != NULL) { // Connect only to the provided locator - _zp_config_insert(ps, Z_CONFIG_PEER_KEY, z_string_make(locator)); + _zp_config_insert(ps, Z_CONFIG_PEER_KEY, _z_string_make(locator)); } else { // The locator is not provided, we should perform scouting - _zp_config_insert(ps, Z_CONFIG_MULTICAST_SCOUTING_KEY, z_string_make(Z_CONFIG_MULTICAST_SCOUTING_DEFAULT)); - _zp_config_insert(ps, Z_CONFIG_MULTICAST_LOCATOR_KEY, z_string_make(Z_CONFIG_MULTICAST_LOCATOR_DEFAULT)); - _zp_config_insert(ps, Z_CONFIG_SCOUTING_TIMEOUT_KEY, z_string_make(Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT)); + _zp_config_insert(ps, Z_CONFIG_MULTICAST_SCOUTING_KEY, _z_string_make(Z_CONFIG_MULTICAST_SCOUTING_DEFAULT)); + _zp_config_insert(ps, Z_CONFIG_MULTICAST_LOCATOR_KEY, _z_string_make(Z_CONFIG_MULTICAST_LOCATOR_DEFAULT)); + _zp_config_insert(ps, Z_CONFIG_SCOUTING_TIMEOUT_KEY, _z_string_make(Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT)); } } return ps; From 21259693215c5119af811177c82832eb94ea21bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 8 Feb 2023 09:42:50 +0100 Subject: [PATCH 006/181] Align log output with Zenoh Rust (#159) * Set timestamp in logs * CI build logs by default * Fix inline to static inline * Fix size_t format on print * CI build logs by default in emscripten * Explicitly add includes * Make ownership more explicit --- .github/workflows/arduino_esp32.yaml | 2 +- .github/workflows/build-shared.yaml | 4 +-- .github/workflows/build-static.yaml | 4 +-- .github/workflows/emscripten.yaml | 2 +- .github/workflows/espidf.yaml | 2 +- .github/workflows/mbed.yaml | 2 +- .github/workflows/zephyr.yaml | 2 +- include/zenoh-pico/system/platform.h | 1 + include/zenoh-pico/utils/logging.h | 38 +++++++++++++++++++++++----- src/session/query.c | 2 +- src/session/queryable.c | 2 +- src/session/resource.c | 2 +- src/session/subscription.c | 2 +- src/system/arduino/esp32/system.c | 8 ++++++ src/system/arduino/opencr/system.c | 8 ++++++ src/system/emscripten/system.c | 6 +++++ src/system/espidf/system.c | 8 ++++++ src/system/mbed/system.cpp | 16 +++++++++--- src/system/unix/system.c | 9 +++++++ src/system/zephyr/system.c | 8 ++++++ tests/z_client_test.c | 16 ++++++------ tests/z_peer_multicast_test.c | 2 +- 22 files changed, 114 insertions(+), 32 deletions(-) diff --git a/.github/workflows/arduino_esp32.yaml b/.github/workflows/arduino_esp32.yaml index 0f71e3770..d54477ee0 100644 --- a/.github/workflows/arduino_esp32.yaml +++ b/.github/workflows/arduino_esp32.yaml @@ -51,7 +51,7 @@ jobs: mkdir -p $ARDUINO_BASE cd $ARDUINO_BASE - platformio init -b esp32thing_plus --project-option="build_flags=-DZ_LINK_BLUETOOTH=1" + platformio init -b esp32thing_plus --project-option="build_flags=-DZ_LINK_BLUETOOTH=1 -DZENOH_DEBUG=3" cd $ARDUINO_BASE/lib ln -s $ZENOH_PICO_BASE diff --git a/.github/workflows/build-shared.yaml b/.github/workflows/build-shared.yaml index acfae70c1..efbd0483f 100644 --- a/.github/workflows/build-shared.yaml +++ b/.github/workflows/build-shared.yaml @@ -30,7 +30,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Compile debug - run: make all + run: ZENOH_DEBUG=3 make all env: BUILD_TYPE: Debug BUILD_SHARED_LIBS: ON @@ -45,7 +45,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Crosscompile debug - run: make crossbuilds + run: ZENOH_DEBUG=3 make crossbuilds env: BUILD_TYPE: Debug BUILD_SHARED_LIBS: ON diff --git a/.github/workflows/build-static.yaml b/.github/workflows/build-static.yaml index 0bc1cb99f..9546c34d7 100644 --- a/.github/workflows/build-static.yaml +++ b/.github/workflows/build-static.yaml @@ -30,7 +30,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Compile debug - run: make all + run: ZENOH_DEBUG=3 make all env: BUILD_TYPE: Debug BUILD_SHARED_LIBS: OFF @@ -45,7 +45,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Crosscompile debug - run: make crossbuilds + run: ZENOH_DEBUG=3 make crossbuilds env: BUILD_TYPE: Debug BUILD_SHARED_LIBS: OFF diff --git a/.github/workflows/emscripten.yaml b/.github/workflows/emscripten.yaml index 37c920b4d..b995749d9 100644 --- a/.github/workflows/emscripten.yaml +++ b/.github/workflows/emscripten.yaml @@ -34,7 +34,7 @@ jobs: - name: Compile debug run: | mkdir build - emcmake cmake -E env CFLAGS="-DZ_LINK_WS=1 -DZ_LINK_TCP=0 -DZ_LINK_UDP_MULTICAST=0 -DZ_LINK_UDP_UNICAST=0 -DZ_SCOUTING_UDP=0" cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_STANDARD=11 -DBUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=OFF -DBUILD_MULTICAST=OFF -DBUILD_INTEGRATION=OFF -DBUILD_TOOLS=OFF -H. -Bbuild + emcmake cmake -E env CFLAGS="-DZ_LINK_WS=1 -DZ_LINK_TCP=0 -DZ_LINK_UDP_MULTICAST=0 -DZ_LINK_UDP_UNICAST=0 -DZ_SCOUTING_UDP=0" cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_STANDARD=11 -DBUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=OFF -DBUILD_MULTICAST=OFF -DBUILD_INTEGRATION=OFF -DBUILD_TOOLS=OFF -DZENOH_DEBUG=3 -H. -Bbuild make -C build diff --git a/.github/workflows/espidf.yaml b/.github/workflows/espidf.yaml index dd3f50373..181b60af4 100644 --- a/.github/workflows/espidf.yaml +++ b/.github/workflows/espidf.yaml @@ -51,7 +51,7 @@ jobs: mkdir -p $ESPIDF_BASE cd $ESPIDF_BASE - platformio init -b az-delivery-devkit-v4 --project-option="framework=espidf" --project-option="build_flags=-DZENOH_ESPIDF" + platformio init -b az-delivery-devkit-v4 --project-option="framework=espidf" --project-option="build_flags=-DZENOH_ESPIDF -DZENOH_DEBUG=3" cd $ESPIDF_BASE/lib ln -s $ZENOH_PICO_BASE diff --git a/.github/workflows/mbed.yaml b/.github/workflows/mbed.yaml index 4a70aadad..3c691cd5e 100644 --- a/.github/workflows/mbed.yaml +++ b/.github/workflows/mbed.yaml @@ -51,7 +51,7 @@ jobs: mkdir -p $MBED_BASE cd $MBED_BASE - pio init -b nucleo_f767zi --project-option="framework=mbed" --project-option="build_flags=-DZ_LINK_SERIAL=1" + pio init -b nucleo_f767zi --project-option="framework=mbed" --project-option="build_flags=-DZ_LINK_SERIAL=1 -DZENOH_DEBUG=3" cd $MBED_BASE/lib ln -s $ZENOH_PICO_BASE diff --git a/.github/workflows/zephyr.yaml b/.github/workflows/zephyr.yaml index 83acd7cc0..acc52a51d 100644 --- a/.github/workflows/zephyr.yaml +++ b/.github/workflows/zephyr.yaml @@ -51,7 +51,7 @@ jobs: mkdir -p $ZEPHYR_BASE cd $ZEPHYR_BASE - pio init -b nucleo_f767zi --project-option="framework=zephyr" --project-option="build_flags=-DZ_LINK_SERIAL=1" + pio init -b nucleo_f767zi --project-option="framework=zephyr" --project-option="build_flags=-DZ_LINK_SERIAL=1 -DZENOH_DEBUG=3" cd $ZEPHYR_BASE/lib ln -s $ZENOH_PICO_BASE diff --git a/include/zenoh-pico/system/platform.h b/include/zenoh-pico/system/platform.h index 3a909b7a0..42048388f 100644 --- a/include/zenoh-pico/system/platform.h +++ b/include/zenoh-pico/system/platform.h @@ -90,6 +90,7 @@ unsigned long z_clock_elapsed_s(z_clock_t *time); /*------------------ Time ------------------*/ z_time_t z_time_now(void); +const char *z_time_now_as_str(char *const buf, unsigned long buflen); unsigned long z_time_elapsed_us(z_time_t *time); unsigned long z_time_elapsed_ms(z_time_t *time); unsigned long z_time_elapsed_s(z_time_t *time); diff --git a/include/zenoh-pico/utils/logging.h b/include/zenoh-pico/utils/logging.h index f24faa81c..359f70806 100644 --- a/include/zenoh-pico/utils/logging.h +++ b/include/zenoh-pico/utils/logging.h @@ -17,18 +17,44 @@ #include +static inline void __z_print_timestamp(void) { + char ret[64]; + printf("[%s ", z_time_now_as_str(ret, sizeof(ret))); +} + #if (ZENOH_DEBUG == 3) -#define _Z_DEBUG(x, ...) printf(x, ##__VA_ARGS__) -#define _Z_INFO(x, ...) printf(x, ##__VA_ARGS__) -#define _Z_ERROR(x, ...) printf(x, ##__VA_ARGS__) +#define _Z_DEBUG(x, ...) \ + __z_print_timestamp(); \ + printf("DEBUG ::%s] ", __func__); \ + printf(x, ##__VA_ARGS__); +#define _Z_INFO(x, ...) \ + __z_print_timestamp(); \ + printf("INFO ::%s] ", __func__); \ + printf(x, ##__VA_ARGS__); +#define _Z_ERROR(x, ...) \ + __z_print_timestamp(); \ + printf("ERROR ::%s] ", __func__); \ + printf(x, ##__VA_ARGS__); + #elif (ZENOH_DEBUG == 2) #define _Z_DEBUG(x, ...) (void)(0) -#define _Z_INFO(x, ...) printf(x, ##__VA_ARGS__) -#define _Z_ERROR(x, ...) printf(x, ##__VA_ARGS__) +#define _Z_INFO(x, ...) \ + __z_print_timestamp(); \ + printf("INFO ::%s] ", __func__); \ + printf(x, ##__VA_ARGS__); +#define _Z_ERROR(x, ...) \ + __z_print_timestamp(); \ + printf("ERROR ::%s] ", __func__); \ + printf(x, ##__VA_ARGS__); + #elif (ZENOH_DEBUG == 1) #define _Z_DEBUG(x, ...) (void)(0) #define _Z_INFO(x, ...) (void)(0) -#define _Z_ERROR(x, ...) printf(x, ##__VA_ARGS__) +#define _Z_ERROR(x, ...) \ + __z_print_timestamp(); \ + printf("ERROR ::%s] ", __func__); \ + printf(x, ##__VA_ARGS__); + #elif (ZENOH_DEBUG == 0) #define _Z_DEBUG(x, ...) (void)(0) #define _Z_INFO(x, ...) (void)(0) diff --git a/src/session/query.c b/src/session/query.c index 99f923c89..aae6d3cee 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -108,7 +108,7 @@ _z_pending_query_t *_z_get_pending_query_by_id(_z_session_t *zn, const _z_zint_t int8_t _z_register_pending_query(_z_session_t *zn, _z_pending_query_t *pen_qry) { int8_t ret = _Z_RES_OK; - _Z_DEBUG(">>> Allocating query for (%lu:%s,%s)\n", pen_qry->_key._id, pen_qry->_key._suffix, pen_qry->_parameters); + _Z_DEBUG(">>> Allocating query for (%zu:%s,%s)\n", pen_qry->_key._id, pen_qry->_key._suffix, pen_qry->_parameters); #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); diff --git a/src/session/queryable.c b/src/session/queryable.c index d38b38a92..928481a23 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -115,7 +115,7 @@ _z_questionable_sptr_list_t *_z_get_questionable_by_key(_z_session_t *zn, const } _z_questionable_sptr_t *_z_register_questionable(_z_session_t *zn, _z_questionable_t *q) { - _Z_DEBUG(">>> Allocating queryable for (%lu:%s)\n", q->_key._id, q->_key._suffix); + _Z_DEBUG(">>> Allocating queryable for (%zu:%s)\n", q->_key._id, q->_key._suffix); _z_questionable_sptr_t *ret = NULL; #if Z_MULTI_THREAD == 1 diff --git a/src/session/resource.c b/src/session/resource.c index 7d136f973..efa3f3acc 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -209,7 +209,7 @@ _z_keyexpr_t _z_get_expanded_key_from_key(_z_session_t *zn, uint8_t is_local, co int8_t _z_register_resource(_z_session_t *zn, uint8_t is_local, _z_resource_t *res) { int8_t ret = _Z_RES_OK; - _Z_DEBUG(">>> Allocating res decl for (%zu,%lu:%s)\n", res->_id, res->_key._id, res->_key._suffix); + _Z_DEBUG(">>> Allocating res decl for (%zu,%zu:%s)\n", res->_id, res->_key._id, res->_key._suffix); #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); diff --git a/src/session/subscription.c b/src/session/subscription.c index 7fb00f66a..f7e002b33 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -122,7 +122,7 @@ _z_subscription_sptr_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8 } _z_subscription_sptr_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *s) { - _Z_DEBUG(">>> Allocating sub decl for (%lu:%s)\n", s->_key._id, s->_key._suffix); + _Z_DEBUG(">>> Allocating sub decl for (%zu:%s)\n", s->_key._id, s->_key._suffix); _z_subscription_sptr_t *ret = NULL; #if Z_MULTI_THREAD == 1 diff --git a/src/system/arduino/esp32/system.c b/src/system/arduino/esp32/system.c index 7d68ccaf8..f001ca02f 100644 --- a/src/system/arduino/esp32/system.c +++ b/src/system/arduino/esp32/system.c @@ -159,6 +159,14 @@ z_time_t z_time_now(void) { return now; } +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); + struct tm ts; + ts = *localtime(&tv.tv_sec); + strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); + return buf; +} + unsigned long z_time_elapsed_us(z_time_t *time) { z_time_t now; gettimeofday(&now, NULL); diff --git a/src/system/arduino/opencr/system.c b/src/system/arduino/opencr/system.c index 953dd0d1a..24e6d03c7 100644 --- a/src/system/arduino/opencr/system.c +++ b/src/system/arduino/opencr/system.c @@ -153,6 +153,14 @@ z_time_t z_time_now(void) { return now; } +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); + struct tm ts; + ts = *localtime(&tv.tv_sec); + strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); + return buf; +} + unsigned long z_time_elapsed_us(z_time_t *time) { z_time_t now; gettimeofday(&now, NULL); diff --git a/src/system/emscripten/system.c b/src/system/emscripten/system.c index 85a78384f..5734a526f 100644 --- a/src/system/emscripten/system.c +++ b/src/system/emscripten/system.c @@ -14,6 +14,7 @@ #include #include +#include #include #include "zenoh-pico/config.h" @@ -103,6 +104,11 @@ unsigned long z_clock_elapsed_s(z_clock_t *instant) { return z_time_elapsed_ms(i /*------------------ Time ------------------*/ z_time_t z_time_now(void) { return emscripten_get_now(); } +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + snprintf(buf, buflen, "%f", z_time_now()); + return buf; +} + unsigned long z_time_elapsed_us(z_time_t *time) { return z_time_elapsed_ms(time) * 1000; } unsigned long z_time_elapsed_ms(z_time_t *time) { diff --git a/src/system/espidf/system.c b/src/system/espidf/system.c index d7fe81f01..9b21665cf 100644 --- a/src/system/espidf/system.c +++ b/src/system/espidf/system.c @@ -160,6 +160,14 @@ z_time_t z_time_now(void) { return now; } +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); + struct tm ts; + ts = *localtime(&tv.tv_sec); + strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); + return buf; +} + unsigned long z_time_elapsed_us(z_time_t *time) { z_time_t now; gettimeofday(&now, NULL); diff --git a/src/system/mbed/system.cpp b/src/system/mbed/system.cpp index 4fcdf8c50..6c8417061 100644 --- a/src/system/mbed/system.cpp +++ b/src/system/mbed/system.cpp @@ -146,13 +146,21 @@ unsigned long z_clock_elapsed_s(z_clock_t *instant) { /*------------------ Time ------------------*/ z_time_t z_time_now(void) { - struct timeval now; + z_time_t now; gettimeofday(&now, NULL); return now; } +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); + struct tm ts; + ts = *localtime(&tv.tv_sec); + strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); + return buf; +} + unsigned long z_time_elapsed_us(z_time_t *time) { - struct timeval now; + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec)); @@ -160,7 +168,7 @@ unsigned long z_time_elapsed_us(z_time_t *time) { } unsigned long z_time_elapsed_ms(z_time_t *time) { - struct timeval now; + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec) / 1000); @@ -168,7 +176,7 @@ unsigned long z_time_elapsed_ms(z_time_t *time) { } unsigned long z_time_elapsed_s(z_time_t *time) { - struct timeval now; + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = now.tv_sec - time->tv_sec; diff --git a/src/system/unix/system.c b/src/system/unix/system.c index 2eea8a5a1..f6c9c8713 100644 --- a/src/system/unix/system.c +++ b/src/system/unix/system.c @@ -13,6 +13,7 @@ // #include +#include #include #if defined(ZENOH_LINUX) @@ -179,6 +180,14 @@ z_time_t z_time_now(void) { return now; } +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); + struct tm ts; + ts = *localtime(&tv.tv_sec); + strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); + return buf; +} + unsigned long z_time_elapsed_us(z_time_t *time) { z_time_t now; gettimeofday(&now, NULL); diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index a864b05a1..5848dd8c2 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -153,6 +153,14 @@ z_time_t z_time_now(void) { return now; } +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); + struct tm ts; + ts = *localtime(&tv.tv_sec); + strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); + return buf; +} + unsigned long z_time_elapsed_us(z_time_t *time) { z_time_t now; gettimeofday(&now, NULL); diff --git a/tests/z_client_test.c b/tests/z_client_test.c index ac5e9c283..64fcbeca6 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -140,7 +140,7 @@ int main(int argc, char **argv) { for (unsigned int i = 0; i < SET; i++) { snprintf(s1_res, 64, "%s%u", uri, i); z_owned_keyexpr_t expr = z_declare_keyexpr(z_loan(s1), z_keyexpr(s1_res)); - printf("Declared resource on session 1: %lu %s\n", z_loan(expr)._id, z_loan(expr)._suffix); + printf("Declared resource on session 1: %zu %s\n", z_loan(expr)._id, z_loan(expr)._suffix); rids1[i] = expr; } @@ -149,7 +149,7 @@ int main(int argc, char **argv) { for (unsigned int i = 0; i < SET; i++) { snprintf(s1_res, 64, "%s%u", uri, i); z_owned_keyexpr_t expr = z_declare_keyexpr(z_loan(s2), z_keyexpr(s1_res)); - printf("Declared resource on session 2: %lu %s\n", z_loan(expr)._id, z_loan(expr)._suffix); + printf("Declared resource on session 2: %zu %s\n", z_loan(expr)._id, z_loan(expr)._suffix); rids2[i] = expr; } @@ -161,7 +161,7 @@ int main(int argc, char **argv) { z_owned_subscriber_t *sub = (z_owned_subscriber_t *)z_malloc(sizeof(z_owned_subscriber_t)); *sub = z_declare_subscriber(z_loan(s2), z_loan(rids2[i]), &callback, NULL); assert(z_check(*sub)); - printf("Declared subscription on session 2: %zu %lu %s\n", z_subscriber_loan(sub)._val->_id, + printf("Declared subscription on session 2: %zu %zu %s\n", z_subscriber_loan(sub)._val->_id, z_loan(rids2[i])._id, ""); subs2 = _z_list_push(subs2, sub); } @@ -174,7 +174,7 @@ int main(int argc, char **argv) { z_owned_queryable_t *qle = (z_owned_queryable_t *)z_malloc(sizeof(z_owned_queryable_t)); *qle = z_declare_queryable(z_loan(s2), z_keyexpr(s1_res), &callback, NULL); assert(z_check(*qle)); - printf("Declared queryable on session 2: %zu %lu %s\n", qle->_value->_id, (z_zint_t)0, s1_res); + printf("Declared queryable on session 2: %zu %zu %s\n", qle->_value->_id, (z_zint_t)0, s1_res); qles2 = _z_list_push(qles2, qle); } @@ -201,7 +201,7 @@ int main(int argc, char **argv) { z_put_options_t opt = z_put_options_default(); opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; z_put(z_loan(s1), z_loan(rids1[i]), (const uint8_t *)payload, len, &opt); - printf("Wrote data from session 1: %lu %zu b\t(%u/%u)\n", z_loan(rids1[i])._id, len, n * SET + (i + 1), + printf("Wrote data from session 1: %zu %zu b\t(%u/%u)\n", z_loan(rids1[i])._id, len, n * SET + (i + 1), total); } } @@ -229,7 +229,7 @@ int main(int argc, char **argv) { snprintf(s1_res, 64, "%s%u", uri, i); z_owned_closure_reply_t callback = z_closure(reply_handler, NULL, &idx[i]); z_get(z_loan(s1), z_keyexpr(s1_res), "", &callback, NULL); - printf("Queried data from session 1: %lu %s\n", (z_zint_t)0, s1_res); + printf("Queried data from session 1: %zu %s\n", (z_zint_t)0, s1_res); } } @@ -293,14 +293,14 @@ int main(int argc, char **argv) { // Undeclare resources on both sessions for (unsigned int i = 0; i < SET; i++) { - printf("Undeclared resource on session 1: %lu\n", z_loan(rids1[i])._id); + printf("Undeclared resource on session 1: %zu\n", z_loan(rids1[i])._id); z_undeclare_keyexpr(z_loan(s1), z_move(rids1[i])); } z_sleep_s(SLEEP); for (unsigned int i = 0; i < SET; i++) { - printf("Undeclared resource on session 2: %lu\n", z_loan(rids2[i])._id); + printf("Undeclared resource on session 2: %zu\n", z_loan(rids2[i])._id); z_undeclare_keyexpr(z_loan(s2), z_move(rids2[i])); } diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 39e49e9a0..5658cff13 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -103,7 +103,7 @@ int main(int argc, char **argv) { z_owned_subscriber_t *sub = (z_owned_subscriber_t *)z_malloc(sizeof(z_owned_subscriber_t)); *sub = z_declare_subscriber(z_loan(s2), z_keyexpr(s1_res), &callback, NULL); assert(z_check(*sub)); - printf("Declared subscription on session 2: %zu %lu %s\n", z_subscriber_loan(sub)._val->_id, (z_zint_t)0, + printf("Declared subscription on session 2: %zu %zu %s\n", z_subscriber_loan(sub)._val->_id, (z_zint_t)0, s1_res); subs2 = _z_list_push(subs2, sub); } From e7f3f6d7002bd062a29820caaa66c74cc3fe6a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 8 Feb 2023 10:42:00 +0100 Subject: [PATCH 007/181] Improve sleep resilience considering only their granularity (#156) (#158) * z_sleep_ms use zephyr's nanosleep because the previously used usleep doesn't support sleeps for more than 1 second * Improve sleep resilience considering only their granularity * Add native zephyr sleep api instead of its posix compatible * Minimize oversleeping * Missing the fix on esp32 * Where the start gone? --------- Co-authored-by: David Corbeil --- src/system/arduino/esp32/system.c | 13 +++++++++++- src/system/arduino/opencr/system.c | 13 +++++++++++- src/system/emscripten/system.c | 13 +++++++++++- src/system/espidf/system.c | 13 +++++++++++- src/system/mbed/system.cpp | 3 ++- src/system/unix/system.c | 13 +++++++++++- src/system/zephyr/system.c | 32 +++++++++++++++++++++++++++--- 7 files changed, 91 insertions(+), 9 deletions(-) diff --git a/src/system/arduino/esp32/system.c b/src/system/arduino/esp32/system.c index f001ca02f..55b9e756b 100644 --- a/src/system/arduino/esp32/system.c +++ b/src/system/arduino/esp32/system.c @@ -117,7 +117,18 @@ int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wa /*------------------ Sleep ------------------*/ int z_sleep_us(unsigned int time) { return usleep(time); } -int z_sleep_ms(unsigned int time) { return usleep(time * 1000U); } +int z_sleep_ms(unsigned int time) { + z_time_t start = z_time_now(); + + // Most sleep APIs promise to sleep at least whatever you asked them to. + // This may compound, so this approach may make sleeps longer than expected. + // This extra check tries to minimize the amount of extra time it might sleep. + while (z_time_elapsed_ms(&start) < time) { + z_sleep_us(1000); + } + + return 0; +} int z_sleep_s(unsigned int time) { return sleep(time); } diff --git a/src/system/arduino/opencr/system.c b/src/system/arduino/opencr/system.c index 24e6d03c7..7f7a3bacf 100644 --- a/src/system/arduino/opencr/system.c +++ b/src/system/arduino/opencr/system.c @@ -107,7 +107,18 @@ int z_sleep_ms(unsigned int time) { return 0; } -int z_sleep_s(unsigned int time) { return z_sleep_ms(time * 1000U); } +int z_sleep_s(unsigned int time) { + z_time_t start = z_time_now(); + + // Most sleep APIs promise to sleep at least whatever you asked them to. + // This may compound, so this approach may make sleeps longer than expected. + // This extra check tries to minimize the amount of extra time it might sleep. + while (z_time_elapsed_s(&start) < time) { + z_sleep_ms(1000); + } + + return 0; +} /*------------------ Instant ------------------*/ void __z_clock_gettime(z_clock_t *ts) { diff --git a/src/system/emscripten/system.c b/src/system/emscripten/system.c index 5734a526f..38b31a16c 100644 --- a/src/system/emscripten/system.c +++ b/src/system/emscripten/system.c @@ -90,7 +90,18 @@ int z_sleep_ms(unsigned int time) { return 0; } -int z_sleep_s(unsigned int time) { return z_sleep_ms(time * 1000); } +int z_sleep_s(unsigned int time) { + z_time_t start = z_time_now(); + + // Most sleep APIs promise to sleep at least whatever you asked them to. + // This may compound, so this approach may make sleeps longer than expected. + // This extra check tries to minimize the amount of extra time it might sleep. + while (z_time_elapsed_s(&start) < time) { + z_sleep_ms(1000); + } + + return 0; +} /*------------------ Instant ------------------*/ z_clock_t z_clock_now(void) { return z_time_now(); } diff --git a/src/system/espidf/system.c b/src/system/espidf/system.c index 9b21665cf..9bfb3d270 100644 --- a/src/system/espidf/system.c +++ b/src/system/espidf/system.c @@ -118,7 +118,18 @@ int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wa /*------------------ Sleep ------------------*/ int z_sleep_us(unsigned int time) { return usleep(time); } -int z_sleep_ms(unsigned int time) { return usleep(time * 1000U); } +int z_sleep_ms(unsigned int time) { + z_time_t start = z_time_now(); + + // Most sleep APIs promise to sleep at least whatever you asked them to. + // This may compound, so this approach may make sleeps longer than expected. + // This extra check tries to minimize the amount of extra time it might sleep. + while (z_time_elapsed_ms(&start) < time) { + z_sleep_us(1000); + } + + return 0; +} int z_sleep_s(unsigned int time) { return sleep(time); } diff --git a/src/system/mbed/system.cpp b/src/system/mbed/system.cpp index 6c8417061..cf63aa7c1 100644 --- a/src/system/mbed/system.cpp +++ b/src/system/mbed/system.cpp @@ -110,7 +110,8 @@ int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { /*------------------ Sleep ------------------*/ int z_sleep_us(unsigned int time) { - return -1; // Not supported + ThisThread::sleep_for(chrono::milliseconds(((time / 1000) + (time % 1000 == 0 ? 0 : 1)))); + return 0; } int z_sleep_ms(unsigned int time) { diff --git a/src/system/unix/system.c b/src/system/unix/system.c index f6c9c8713..5faf15956 100644 --- a/src/system/unix/system.c +++ b/src/system/unix/system.c @@ -138,7 +138,18 @@ int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wa /*------------------ Sleep ------------------*/ int z_sleep_us(unsigned int time) { return usleep(time); } -int z_sleep_ms(unsigned int time) { return z_sleep_us(time * 1000U); } +int z_sleep_ms(unsigned int time) { + z_time_t start = z_time_now(); + + // Most sleep APIs promise to sleep at least whatever you asked them to. + // This may compound, so this approach may make sleeps longer than expected. + // This extra check tries to minimize the amount of extra time it might sleep. + while (z_time_elapsed_ms(&start) < time) { + z_sleep_us(1000); + } + + return 0; +} int z_sleep_s(unsigned int time) { return sleep(time); } diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index 5848dd8c2..31f65b846 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -109,11 +109,37 @@ int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wa #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int z_sleep_us(unsigned int time) { return usleep(time); } +int z_sleep_us(unsigned int time) { + int32_t rem = time; + while (rem > 0) { + rem = k_usleep(rem); // This function is unlikely to work as expected without kernel tuning. + // In particular, because the lower bound on the duration of a sleep is the + // duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted to + // achieve the resolution desired. The implications of doing this must be + // understood before attempting to use k_usleep(). Use with caution. + // From: https://docs.zephyrproject.org/apidoc/latest/group__thread__apis.html + } -int z_sleep_ms(unsigned int time) { return usleep(time * 1000U); } + return 0; +} -int z_sleep_s(unsigned int time) { return sleep(time); } +int z_sleep_ms(unsigned int time) { + int32_t rem = time; + while (rem > 0) { + rem = k_msleep(rem); + } + + return 0; +} + +int z_sleep_s(unsigned int time) { + int32_t rem = time; + while (rem > 0) { + rem = k_sleep(K_SECONDS(rem)); + } + + return 0; +} /*------------------ Instant ------------------*/ z_clock_t z_clock_now(void) { From aefa0ed5ec8ad56826f218fb2bff1fa303f422a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 8 Feb 2023 13:41:55 +0100 Subject: [PATCH 008/181] Scouting and Hello refactor --- include/zenoh-pico/api/constants.h | 14 +++++++ include/zenoh-pico/api/types.h | 2 +- include/zenoh-pico/net/primitives.h | 7 ++-- include/zenoh-pico/protocol/codec.h | 4 ++ include/zenoh-pico/protocol/core.h | 5 ++- include/zenoh-pico/protocol/msg.h | 59 ++++++++++++++++++++------ include/zenoh-pico/session/utils.h | 5 ++- src/api/api.c | 6 ++- src/net/primitives.c | 6 +-- src/net/session.c | 6 ++- src/protocol/codec.c | 26 ++++++++++-- src/protocol/msg.c | 24 +++++------ src/protocol/msgcodec.c | 64 ++++++++++++++++++----------- src/session/scout.c | 21 +++------- tests/z_msgcodec_test.c | 51 ++++++++++++++--------- 15 files changed, 198 insertions(+), 102 deletions(-) diff --git a/include/zenoh-pico/api/constants.h b/include/zenoh-pico/api/constants.h index 221379489..7887a36e9 100644 --- a/include/zenoh-pico/api/constants.h +++ b/include/zenoh-pico/api/constants.h @@ -21,6 +21,20 @@ extern "C" { #endif +/** + * What values. + * + * Enumerators: + * Z_WHAT_ROUTER: Router. + * Z_WHAT_PEER: Peer. + * Z_WHAT_CLIENT: Client. + */ +typedef enum { + Z_WHAT_ROUTER = 0x01, // Router + Z_WHAT_PEER = 0x02, // Peer + Z_WHAT_CLIENT = 0x03 // Client +} z_what_t; + /** * Whatami values, defined as a bitmask. * diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 0953764a1..7c4258dab 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -392,7 +392,7 @@ typedef _z_sample_t z_sample_t; * Represents the content of a `hello` message returned by a zenoh entity as a reply to a `scout` message. * * Members: - * uint8_t whatami: The kind of zenoh entity. + * z_whatami_t whatami: The kind of zenoh entity. * z_bytes_t zid: The Zenoh ID of the scouted entity (empty if absent). * z_str_array_t locators: The locators of the scouted entity. */ diff --git a/include/zenoh-pico/net/primitives.h b/include/zenoh-pico/net/primitives.h index f83b89adb..6e2613569 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -28,12 +28,13 @@ * Scout for routers and/or peers. * * Parameters: - * what: A whatami bitmask of zenoh entities kind to scout for. + * what: A what bitmask of zenoh entities kind to scout for. + * zid: The ZenohID of the scouting origin. * locator: The locator where to scout. * timeout: The time that should be spent scouting before returnng the results. */ -void _z_scout(const z_whatami_t what, const char *locator, const uint32_t timeout, _z_hello_handler_t callback, - void *arg_call, _z_drop_handler_t dropper, void *arg_drop); +void _z_scout(const z_what_t what, const _z_bytes_t zid, const char *locator, const uint32_t timeout, + _z_hello_handler_t callback, void *arg_call, _z_drop_handler_t dropper, void *arg_drop); /*------------------ Declarations ------------------*/ diff --git a/include/zenoh-pico/protocol/codec.h b/include/zenoh-pico/protocol/codec.h index 6d17c25b2..5f839dff3 100644 --- a/include/zenoh-pico/protocol/codec.h +++ b/include/zenoh-pico/protocol/codec.h @@ -43,6 +43,10 @@ int8_t _z_uint64_decode(uint64_t *u8, _z_zbuf_t *buf); int8_t _z_zint_encode(_z_wbuf_t *buf, _z_zint_t v); int8_t _z_zint_decode(_z_zint_t *zint, _z_zbuf_t *buf); +int8_t _z_bytes_val_encode(_z_wbuf_t *buf, const _z_bytes_t *bs); +int8_t _z_bytes_val_decode(_z_bytes_t *bs, _z_zbuf_t *buf); +int8_t _z_bytes_val_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf); + int8_t _z_bytes_encode(_z_wbuf_t *buf, const _z_bytes_t *bs); int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *buf); int8_t _z_bytes_decode_na(_z_bytes_t *bs, _z_zbuf_t *buf); diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 55ac8580a..6f04eeddc 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -102,12 +102,13 @@ typedef struct { * Members: * _z_bytes_t zid: The Zenoh ID of the scouted entity (empty if absent). * _z_str_array_t locators: The locators of the scouted entity. - * uint8_t whatami: The kind of zenoh entity. + * z_whatami_t whatami: The kind of zenoh entity. */ typedef struct { _z_bytes_t zid; _z_str_array_t locators; - uint8_t whatami; + z_whatami_t whatami; + uint8_t version; } _z_hello_t; void _z_hello_clear(_z_hello_t *src); void _z_hello_free(_z_hello_t **hello); diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 8fcbf32bd..414056a71 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -66,6 +66,18 @@ /*=============================*/ /* Message flags */ /*=============================*/ +// Scout message flags: +// I ZenohID if I==1 then the ZenohID is present +// Z Zenoh properties if Z==1 then Zenoh properties are present +#define _Z_FLAG_SCOUT_I 0x08 // 1 << 3 +#define _Z_FLAG_SCOUT_Z 0x80 // 1 << 7 + +// Hello message flags: +// L Locators if L==1 then Locators are present +// Z Zenoh properties if Z==1 then Zenoh properties are present +#define _Z_FLAG_HELLO_L 0x20 // 1 << 5 +#define _Z_FLAG_HELLO_Z 0x80 // 1 << 7 + /* Transport message flags */ #define _Z_FLAG_T_A 0x20 // 1 << 5 | Ack if A==1 then the message is an acknowledgment #define _Z_FLAG_T_C 0x40 // 1 << 6 | Count if C==1 then number of unacknowledged messages is present @@ -594,15 +606,29 @@ _z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_p // // 7 6 5 4 3 2 1 0 // +-+-+-+-+-+-+-+-+ -// |X|W|I| SCOUT | -// +-+-+-+-+-------+ -// ~ what ~ if W==1 -- Otherwise implicitly scouting for Routers +// |Z|X|X| SCOUT | +// +-+-+-+---------+ +// | version | // +---------------+ +// |zid_len|I| what| (#)(*) +// +-+-+-+-+-+-+-+-+ +// ~ [u8] ~ if Flag(I)==1 -- ZenohID +// +---------------+ +// +// (#) ZID length. If Flag(I)==1 it indicates how many bytes are used for the ZenohID bytes. +// A ZenohID is minimum 1 byte and maximum 16 bytes. Therefore, the actual lenght is computed as: +// real_zid_len := 1 + zid_len // -// - if I==1 then the sender is asking for hello replies that contain a Zenoh ID. +// (*) What. It indicates a bitmap of WhatAmI interests. +// The valid bitflags are: +// - 0b001: Router +// - 0b010: Peer +// - 0b100: Client // typedef struct { - z_whatami_t _what; + _z_bytes_t _zid; + z_what_t _what; + uint8_t _version; } _z_t_msg_scout_t; void _z_t_msg_clear_scout(_z_t_msg_scout_t *msg); @@ -627,19 +653,29 @@ void _z_t_msg_clear_scout(_z_t_msg_scout_t *msg); // // 7 6 5 4 3 2 1 0 // +-+-+-+-+-+-+-+-+ -// |L|W|I| HELLO | -// +-+-+-+-+-------+ -// ~ zenoh_id ~ if I==1 +// |Z|X|L| HELLO | +// +-+-+-+---------+ +// | version | // +---------------+ -// ~ whatami ~ if W==1 -- Otherwise it is from a Router +// |zid_len|X|X|wai| (*) +// +-+-+-+-+-+-+-+-+ +// ~ [u8] ~ -- ZenohID // +---------------+ -// ~ Locators ~ if L==1 -- Otherwise src-address is the locator +// ~ ~ if Flag(L)==1 -- List of locators // +---------------+ // +// (*) WhatAmI. It indicates the role of the zenoh node sending the HELLO message. +// The valid WhatAmI values are: +// - 0b00: Router +// - 0b01: Peer +// - 0b10: Client +// - 0b11: Reserved +// typedef struct { _z_bytes_t _zid; _z_locator_array_t _locators; z_whatami_t _whatami; + uint8_t _version; } _z_t_msg_hello_t; void _z_t_msg_clear_hello(_z_t_msg_hello_t *msg); @@ -969,7 +1005,7 @@ typedef struct { void _z_t_msg_clear(_z_transport_message_t *msg); /*------------------ Builders ------------------*/ -_z_transport_message_t _z_t_msg_make_scout(z_whatami_t what, _Bool request_zid); +_z_transport_message_t _z_t_msg_make_scout(z_what_t what, _z_bytes_t zid); _z_transport_message_t _z_t_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _z_locator_array_t locators); _z_transport_message_t _z_t_msg_make_join(uint8_t version, z_whatami_t whatami, _z_zint_t lease, _z_zint_t sn_resolution, _z_bytes_t zid, _z_conduit_sn_list_t next_sns); @@ -990,7 +1026,6 @@ _z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_frame_payload_t payl _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable, _Bool is_fragment, _Bool is_final); /*------------------ Copy ------------------*/ -// @TODO: implement the remaining copyers void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg); void _z_t_msg_copy_scout(_z_t_msg_scout_t *clone, _z_t_msg_scout_t *msg); void _z_t_msg_copy_hello(_z_t_msg_hello_t *clone, _z_t_msg_hello_t *msg); diff --git a/include/zenoh-pico/session/utils.h b/include/zenoh-pico/session/utils.h index 02a693945..562a71559 100644 --- a/include/zenoh-pico/session/utils.h +++ b/include/zenoh-pico/session/utils.h @@ -17,11 +17,12 @@ #include +#include "zenoh-pico/api/constants.h" #include "zenoh-pico/net/session.h" /*------------------ Session ------------------*/ -_z_hello_list_t *_z_scout_inner(const uint8_t what, const char *locator, const uint32_t timeout, - const _Bool exit_on_first); +_z_hello_list_t *_z_scout_inner(const z_what_t what, const _z_bytes_t zid, const char *locator, + const uint32_t timeout, const _Bool exit_on_first); int8_t _z_session_init(_z_session_t *zn, _z_bytes_t *zid); int8_t _z_session_close(_z_session_t *zn, uint8_t reason); diff --git a/src/api/api.c b/src/api/api.c index 702c76c05..a9abdca97 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -18,6 +18,7 @@ #include "zenoh-pico/api/primitives.h" #include "zenoh-pico/api/types.h" +#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/config.h" #include "zenoh-pico/net/config.h" #include "zenoh-pico/net/logger.h" @@ -397,11 +398,12 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb wrapped_ctx->ctx = ctx; char *what_str = _z_config_get(config->_value, Z_CONFIG_SCOUTING_WHAT_KEY); - z_whatami_t what = strtol(what_str, NULL, 10); + z_what_t what = strtol(what_str, NULL, 10); char *locator = _z_config_get(config->_value, Z_CONFIG_MULTICAST_LOCATOR_KEY); char *tout_str = _z_config_get(config->_value, Z_CONFIG_SCOUTING_TIMEOUT_KEY); uint32_t tout = strtoul(tout_str, NULL, 10); - _z_scout(what, locator, tout, __z_hello_handler, wrapped_ctx, callback->drop, ctx); + _z_bytes_t zid = _z_bytes_empty(); // TODO[protocol]: Check if a ZID is set in the config files + _z_scout(what, zid, locator, tout, __z_hello_handler, wrapped_ctx, callback->drop, ctx); z_free(wrapped_ctx); z_scouting_config_drop(config); diff --git a/src/net/primitives.c b/src/net/primitives.c index 9d0deaf52..7c0ab3a9a 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -26,9 +26,9 @@ #include "zenoh-pico/session/utils.h" /*------------------ Scouting ------------------*/ -void _z_scout(const z_whatami_t what, const char *locator, const uint32_t timeout, _z_hello_handler_t callback, - void *arg_call, _z_drop_handler_t dropper, void *arg_drop) { - _z_hello_list_t *hellos = _z_scout_inner(what, locator, timeout, false); +void _z_scout(const z_what_t what, const _z_bytes_t zid, const char *locator, const uint32_t timeout, + _z_hello_handler_t callback, void *arg_call, _z_drop_handler_t dropper, void *arg_drop) { + _z_hello_list_t *hellos = _z_scout_inner(what, zid, locator, timeout, false); while (hellos != NULL) { _z_hello_t *hello = _z_hello_list_head(hellos); diff --git a/src/net/session.c b/src/net/session.c index 23ebf5078..19347a767 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -58,7 +58,7 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { if (opt_as_str == NULL) { opt_as_str = Z_CONFIG_SCOUTING_WHAT_DEFAULT; } - z_whatami_t what = strtol(opt_as_str, NULL, 10); + z_what_t what = strtol(opt_as_str, NULL, 10); opt_as_str = _z_config_get(config, Z_CONFIG_MULTICAST_LOCATOR_KEY); if (opt_as_str == NULL) { @@ -72,8 +72,10 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { } uint32_t timeout = strtoul(opt_as_str, NULL, 10); + _z_bytes_t zid = _z_bytes_empty(); // TODO[protocol]: Check if a ZID is set in the config files + // Scout and return upon the first result - _z_hello_list_t *hellos = _z_scout_inner(what, mcast_locator, timeout, true); + _z_hello_list_t *hellos = _z_scout_inner(what, zid, mcast_locator, timeout, true); _z_hello_list_t *xs = hellos; while (xs != NULL) { _z_hello_t *hello = _z_hello_list_head(xs); diff --git a/src/protocol/codec.c b/src/protocol/codec.c index f2da9cd41..e7ca8ec91 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -176,10 +176,9 @@ int8_t _z_zint_decode(_z_zint_t *zint, _z_zbuf_t *zbf) { } /*------------------ uint8_array ------------------*/ -int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { +int8_t _z_bytes_val_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { int8_t ret = _Z_RES_OK; - _Z_EC(_z_zint_encode(wbf, bs->len)) if ((wbf->_is_expandable == true) && (bs->len > Z_TSID_LENGTH)) { ret |= _z_wbuf_wrap_bytes(wbf, bs->start, 0, bs->len); } else { @@ -189,10 +188,18 @@ int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { return ret; } -int8_t _z_bytes_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf) { +int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { + int8_t ret = _Z_RES_OK; + + _Z_EC(_z_zint_encode(wbf, bs->len)) + _Z_EC(_z_bytes_val_encode(wbf, bs)) + + return ret; +} + +int8_t _z_bytes_val_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; - ret |= _z_zint_decode(&bs->len, zbf); if (ret == _Z_RES_OK) { if (_z_zbuf_len(zbf) >= bs->len) { // Check if we have enought bytes to read *bs = _z_bytes_wrap(_z_zbuf_get_rptr(zbf), bs->len); // Decode without allocating @@ -211,6 +218,17 @@ int8_t _z_bytes_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf) { return ret; } +int8_t _z_bytes_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + + ret |= _z_zint_decode(&bs->len, zbf); + ret |= _z_bytes_val_decode_na(bs, zbf); + + return ret; +} + +int8_t _z_bytes_val_decode(_z_bytes_t *bs, _z_zbuf_t *zbf) { return _z_bytes_val_decode_na(bs, zbf); } + int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *zbf) { return _z_bytes_decode_na(bs, zbf); } /*------------------ string with null terminator ------------------*/ diff --git a/src/protocol/msg.c b/src/protocol/msg.c index c7ada5f41..2e6e2fd64 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -16,6 +16,7 @@ #include +#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/session/queryable.h" #include "zenoh-pico/utils/logging.h" @@ -446,32 +447,31 @@ void _z_msg_clear(_z_zenoh_message_t *msg) { /* Transport Messages */ /*=============================*/ /*------------------ Scout Message ------------------*/ -_z_transport_message_t _z_t_msg_make_scout(z_whatami_t what, _Bool request_zid) { +_z_transport_message_t _z_t_msg_make_scout(z_what_t what, _z_bytes_t zid) { _z_transport_message_t msg; + msg._header = _Z_MID_SCOUT; + msg._body._scout._version = Z_PROTO_VERSION; msg._body._scout._what = what; + msg._body._scout._zid = zid; - msg._header = _Z_MID_SCOUT; - if (request_zid == true) { + if (_z_bytes_is_empty(&zid) == false) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_I); } - if (what != Z_WHATAMI_ROUTER) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_W); - } - msg._attachment = NULL; return msg; } -void _z_t_msg_copy_scout(_z_t_msg_scout_t *clone, _z_t_msg_scout_t *msg) { clone->_what = msg->_what; } - -void _z_t_msg_clear_scout(_z_t_msg_scout_t *msg) { - // NOTE: scout does not involve any heap allocation - (void)(msg); +void _z_t_msg_copy_scout(_z_t_msg_scout_t *clone, _z_t_msg_scout_t *msg) { + clone->_what = msg->_what; + clone->_version = msg->_version; + _z_bytes_copy(&clone->_zid, &msg->_zid); } +void _z_t_msg_clear_scout(_z_t_msg_scout_t *msg) { _z_bytes_clear(&msg->_zid); } + /*------------------ Hello Message ------------------*/ _z_transport_message_t _z_t_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _z_locator_array_t locators) { _z_transport_message_t msg; diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index f58373be5..15efbc291 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -16,6 +16,7 @@ #include +#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/utils/logging.h" @@ -986,23 +987,40 @@ int8_t _z_zenoh_message_decode(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { return /*------------------ Scout Message ------------------*/ int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_scout_t *msg) { int8_t ret = _Z_RES_OK; + (void)(header); _Z_DEBUG("Encoding _Z_MID_SCOUT\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_W) == true) { - _Z_EC(_z_enum_encode(wbf, msg->_what)) + _Z_EC(_z_uint8_encode(wbf, msg->_version)) + + uint8_t cbyte = 0; + cbyte |= (msg->_what & 0x07); + uint8_t zid_len = msg->_zid.len; + if (zid_len > 0) { // TODO[protocol]: check if ZID > 0 && <= 16 + _Z_SET_FLAG(cbyte, _Z_FLAG_SCOUT_I); + cbyte |= ((zid_len - 1) & 0x0F) << 4; } + _Z_EC(_z_uint8_encode(wbf, cbyte)) + + _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) return ret; } int8_t _z_scout_decode_na(_z_t_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_SCOUT\n"); int8_t ret = _Z_RES_OK; + (void)(header); + _Z_DEBUG("Decoding _Z_MID_SCOUT\n"); + + ret |= _z_uint8_decode(&msg->_version, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_W) == true) { - ret |= _z_enum_decode((int *)&msg->_what, zbf); + uint8_t cbyte; + ret |= _z_uint8_decode(&cbyte, zbf); + msg->_what = cbyte & 0x07; + if (_Z_HAS_FLAG(cbyte, _Z_FLAG_SCOUT_I) == true) { + msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; + ret |= _z_bytes_val_decode(&msg->_zid, zbf); } else { - msg->_what = Z_WHATAMI_ROUTER; + msg->_zid = _z_bytes_empty(); } return ret; @@ -1017,13 +1035,15 @@ int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_hello_t *m int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_HELLO\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_I) == true) { - _Z_EC(_z_bytes_encode(wbf, &msg->_zid)) - } - if (_Z_HAS_FLAG(header, _Z_FLAG_T_W) == true) { - _Z_EC(_z_enum_encode(wbf, msg->_whatami)) - } - if (_Z_HAS_FLAG(header, _Z_FLAG_T_L) == true) { + _Z_EC(_z_uint8_encode(wbf, msg->_version)) + + uint8_t cbyte = 0; + cbyte |= (msg->_whatami & 0x03); + cbyte |= ((msg->_zid.len - 1) & 0x0F) << 4; // TODO[protocol]: check if ZID > 0 && <= 16 + _Z_EC(_z_uint8_encode(wbf, cbyte)) + _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) + + if (_Z_HAS_FLAG(header, _Z_FLAG_HELLO_L) == true) { _Z_EC(_z_locators_encode(wbf, &msg->_locators)) } @@ -1034,19 +1054,15 @@ int8_t _z_hello_decode_na(_z_t_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) _Z_DEBUG("Decoding _Z_MID_HELLO\n"); int8_t ret = _Z_RES_OK; - if (_Z_HAS_FLAG(header, _Z_FLAG_T_I) == true) { - ret |= _z_bytes_decode(&msg->_zid, zbf); - } else { - msg->_zid = _z_bytes_empty(); - } + ret |= _z_uint8_decode(&msg->_version, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_W) == true) { - ret |= _z_enum_decode((int *)&msg->_whatami, zbf); - } else { - msg->_whatami = Z_WHATAMI_ROUTER; - } + uint8_t cbyte; + ret |= _z_uint8_decode(&cbyte, zbf); + msg->_whatami = cbyte & 0x03; + msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; + ret |= _z_bytes_val_decode(&msg->_zid, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_L) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_HELLO_L) == true) { ret |= _z_locators_decode(&msg->_locators, zbf); } else { msg->_locators = _z_locator_array_make(0); diff --git a/src/session/scout.c b/src/session/scout.c index 6d39c8128..a5cc31cbe 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -73,17 +73,9 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig _Z_INFO("Received _Z_HELLO message\n"); _z_hello_t *hello = (_z_hello_t *)z_malloc(sizeof(_z_hello_t)); if (hello != NULL) { - if (_Z_HAS_FLAG(t_msg._header, _Z_FLAG_T_I) == true) { - _z_bytes_copy(&hello->zid, &t_msg._body._hello._zid); - } else { - _z_bytes_reset(&hello->zid); - } - - if (_Z_HAS_FLAG(t_msg._header, _Z_FLAG_T_W) == true) { - hello->whatami = t_msg._body._hello._whatami; - } else { - hello->whatami = Z_WHATAMI_ROUTER; // Default value is from a router - } + hello->version = t_msg._body._hello._version; + hello->whatami = t_msg._body._hello._whatami; + _z_bytes_copy(&hello->zid, &t_msg._body._hello._zid); if (_Z_HAS_FLAG(t_msg._header, _Z_FLAG_T_L) == true) { hello->locators = _z_str_array_make(t_msg._body._hello._locators._len); @@ -130,16 +122,15 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig return ret; } -_z_hello_list_t *_z_scout_inner(const z_whatami_t what, const char *locator, const uint32_t timeout, - const _Bool exit_on_first) { +_z_hello_list_t *_z_scout_inner(const z_what_t what, const _z_bytes_t zid, const char *locator, + const uint32_t timeout, const _Bool exit_on_first) { _z_hello_list_t *ret = NULL; // Create the buffer to serialize the scout message on _z_wbuf_t wbf = _z_wbuf_make(Z_BATCH_SIZE_TX, false); // Create and encode the scout message - _Bool request_zid = true; - _z_transport_message_t scout = _z_t_msg_make_scout(what, request_zid); + _z_transport_message_t scout = _z_t_msg_make_scout(what, zid); _z_transport_message_encode(&wbf, &scout); diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index c821dd119..3e5d8fe1e 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -1606,16 +1606,25 @@ void zenoh_message(void) { /*=============================*/ /*------------------ Scout Message ------------------*/ _z_transport_message_t gen_scout_message(void) { - z_whatami_t what = gen_uint8() % 7; - _Bool request_zid = gen_bool(); - return _z_t_msg_make_scout(what, request_zid); + z_what_t what = gen_uint8() % 7; + _z_bytes_t zid; + if (gen_bool()) { + zid = gen_bytes((gen_uint8() % 16) + 1); + } else { + zid = _z_bytes_empty(); + } + return _z_t_msg_make_scout(what, zid); } void assert_eq_scout_message(_z_t_msg_scout_t *left, _z_t_msg_scout_t *right, uint8_t header) { - if (_Z_HAS_FLAG(header, _Z_FLAG_T_W) == true) { - printf(" What (%u:%u)\n", left->_what, right->_what); - assert(left->_what == right->_what); - } + (void)(header); + printf(" Version (%u:%u)\n", left->_version, right->_version); + assert(left->_version == right->_version); + printf(" What (%u:%u)\n", left->_what, right->_what); + assert(left->_what == right->_what); + printf(" "); + assert_eq_uint8_array(&left->_zid, &right->_zid); + printf("\n"); } void scout_message(void) { @@ -1648,11 +1657,11 @@ void scout_message(void) { /*------------------ Hello Message ------------------*/ _z_transport_message_t gen_hello_message(void) { - z_whatami_t whatami = 0x04 >> (gen_uint8() % 3); - _z_bytes_t zid = gen_bool() ? gen_bytes(16) : gen_bytes(0); + z_whatami_t whatami = (gen_uint8() % 2) + 1; + _z_bytes_t zid = gen_bytes((gen_uint8() % 16) + 1); _z_locator_array_t locators; - if (gen_bool()) + if (gen_bool() == true) locators = gen_locator_array((gen_uint8() % 4) + 1); else locators = gen_locator_array(0); @@ -1661,16 +1670,18 @@ _z_transport_message_t gen_hello_message(void) { } void assert_eq_hello_message(_z_t_msg_hello_t *left, _z_t_msg_hello_t *right, uint8_t header) { - if (_Z_HAS_FLAG(header, _Z_FLAG_T_I) == true) { - printf(" "); - assert_eq_uint8_array(&left->_zid, &right->_zid); - printf("\n"); - } - if (_Z_HAS_FLAG(header, _Z_FLAG_T_W) == true) { - printf(" What (%u:%u)", left->_whatami, right->_whatami); - assert(left->_whatami == right->_whatami); - printf("\n"); - } + printf(" Version (%u:%u)", left->_version, right->_version); + assert(left->_version == right->_version); + printf("\n"); + + printf(" What (%u:%u)", left->_whatami, right->_whatami); + assert(left->_whatami == right->_whatami); + printf("\n"); + + printf(" "); + assert_eq_uint8_array(&left->_zid, &right->_zid); + printf("\n"); + if (_Z_HAS_FLAG(header, _Z_FLAG_T_L) == true) { printf(" "); assert_eq_locator_array(&left->_locators, &right->_locators); From a2c078a9ed5f2ee1b760df124417226e1a242a9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 10 Feb 2023 09:23:36 +0100 Subject: [PATCH 009/181] Add support for Windows platform (#137) (#136) * Template for windows port * Make core code to be windows compliant * Implement Windows functions and fix compilation warnings * Add Windows build on Github actions * Add windows examples * Fix makefile compatibility with windows * Fix include order in windows network system * Fix example warnings * Update README.md with Windows support * Properly set environment variables * Build static on windows * Avoid possible loss of data in type conversion * Add z_time_now_as_str for windows * Update Windows examples with z_str_t * Add missing time include * Avoid possible loss of data in type conversion * Random uint64 instead of time * Run integration tests on windows * Avoid possible loss of data in type conversion * Trigger tests in CI using ctest * Enable testing by default again --- .github/workflows/build-shared.yaml | 8 +- .github/workflows/build-static.yaml | 8 +- .github/workflows/integration.yaml | 5 +- BSDmakefile | 4 +- CMakeLists.txt | 86 ++- GNUmakefile | 6 +- README.md | 3 +- examples/windows/z_get.c | 96 +++ examples/windows/z_info.c | 73 +++ examples/windows/z_pub.c | 76 +++ examples/windows/z_pub_st.c | 72 +++ examples/windows/z_pull.c | 80 +++ examples/windows/z_put.c | 70 +++ examples/windows/z_queryable.c | 91 +++ examples/windows/z_scout.c | 94 +++ examples/windows/z_sub.c | 79 +++ examples/windows/z_sub_st.c | 74 +++ extra_script.py | 7 +- include/zenoh-pico/collections/pointer.h | 3 +- include/zenoh-pico/collections/vec.h | 36 +- include/zenoh-pico/link/config/bt.h | 17 +- include/zenoh-pico/link/config/serial.h | 9 +- include/zenoh-pico/link/config/tcp.h | 9 +- include/zenoh-pico/link/config/udp.h | 13 +- include/zenoh-pico/net/resource.h | 2 +- include/zenoh-pico/system/link/tcp.h | 2 +- include/zenoh-pico/system/link/udp.h | 15 +- include/zenoh-pico/system/platform.h | 8 +- include/zenoh-pico/system/platform/espidf.h | 1 - include/zenoh-pico/system/platform/windows.h | 51 ++ src/api/api.c | 12 +- src/collections/vec.c | 2 +- src/link/config/bt.c | 10 +- src/link/config/serial.c | 10 +- src/link/config/tcp.c | 8 +- src/link/config/udp.c | 8 +- src/link/multicast/udp.c | 12 +- src/link/unicast/tcp.c | 12 +- src/link/unicast/udp.c | 12 +- src/net/resource.c | 2 +- src/protocol/codec.c | 2 +- src/protocol/config.c | 2 +- src/system/arduino/esp32/network.cpp | 14 +- src/system/arduino/esp32/system.c | 6 +- src/system/arduino/opencr/network.cpp | 8 +- src/system/arduino/opencr/system.c | 6 +- src/system/emscripten/system.c | 6 +- src/system/espidf/network.c | 14 +- src/system/espidf/system.c | 6 +- src/system/mbed/network.cpp | 14 +- src/system/mbed/system.cpp | 6 +- src/system/unix/network.c | 14 +- src/system/unix/system.c | 8 +- src/system/windows/network.c | 611 +++++++++++++++++++ src/system/windows/system.c | 241 ++++++++ src/system/zephyr/network.c | 14 +- src/system/zephyr/system.c | 14 +- src/transport/common/rx.c | 2 +- src/transport/multicast/link/rx.c | 2 +- src/transport/multicast/link/task/lease.c | 5 +- src/transport/unicast/link/rx.c | 2 +- src/transport/unicast/link/task/lease.c | 4 +- src/utils/checksum.c | 6 +- src/utils/string.c | 4 +- tests/z_iobuf_test.c | 2 +- tests/z_msgcodec_test.c | 22 +- 66 files changed, 1992 insertions(+), 229 deletions(-) create mode 100644 examples/windows/z_get.c create mode 100644 examples/windows/z_info.c create mode 100644 examples/windows/z_pub.c create mode 100644 examples/windows/z_pub_st.c create mode 100644 examples/windows/z_pull.c create mode 100644 examples/windows/z_put.c create mode 100644 examples/windows/z_queryable.c create mode 100644 examples/windows/z_scout.c create mode 100644 examples/windows/z_sub.c create mode 100644 examples/windows/z_sub_st.c create mode 100644 include/zenoh-pico/system/platform/windows.h create mode 100644 src/system/windows/network.c create mode 100644 src/system/windows/system.c diff --git a/.github/workflows/build-shared.yaml b/.github/workflows/build-shared.yaml index efbd0483f..055524f04 100644 --- a/.github/workflows/build-shared.yaml +++ b/.github/workflows/build-shared.yaml @@ -26,11 +26,11 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, macOS-latest] + os: [ubuntu-latest, macOS-latest, windows-latest] steps: - uses: actions/checkout@v2 - name: Compile debug - run: ZENOH_DEBUG=3 make all + run: make all env: BUILD_TYPE: Debug BUILD_SHARED_LIBS: ON @@ -38,6 +38,7 @@ jobs: BUILD_MULTICAST: ON BUILD_INTEGRATION: ON BUILD_TOOLS: ON + ZENOH_DEBUG: 3 crossbuilds: name: Build on ubuntu-latest @@ -45,7 +46,8 @@ jobs: steps: - uses: actions/checkout@v2 - name: Crosscompile debug - run: ZENOH_DEBUG=3 make crossbuilds + run: make crossbuilds env: BUILD_TYPE: Debug BUILD_SHARED_LIBS: ON + ZENOH_DEBUG: 3 diff --git a/.github/workflows/build-static.yaml b/.github/workflows/build-static.yaml index 9546c34d7..9b08df766 100644 --- a/.github/workflows/build-static.yaml +++ b/.github/workflows/build-static.yaml @@ -26,11 +26,11 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, macOS-latest] + os: [ubuntu-latest, macOS-latest, windows-latest] steps: - uses: actions/checkout@v2 - name: Compile debug - run: ZENOH_DEBUG=3 make all + run: make all env: BUILD_TYPE: Debug BUILD_SHARED_LIBS: OFF @@ -38,6 +38,7 @@ jobs: BUILD_MULTICAST: ON BUILD_INTEGRATION: ON BUILD_TOOLS: ON + ZENOH_DEBUG: 3 crossbuilds: name: Build on ubuntu-latest @@ -45,7 +46,8 @@ jobs: steps: - uses: actions/checkout@v2 - name: Crosscompile debug - run: ZENOH_DEBUG=3 make crossbuilds + run: make crossbuilds env: BUILD_TYPE: Debug BUILD_SHARED_LIBS: OFF + ZENOH_DEBUG: 3 diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml index 6cbd4b7a7..00302ace2 100644 --- a/.github/workflows/integration.yaml +++ b/.github/workflows/integration.yaml @@ -29,7 +29,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ ubuntu-latest, macOS-latest ] + os: [ ubuntu-latest, macOS-latest, windows-latest ] steps: - uses: actions/checkout@v2 @@ -50,7 +50,6 @@ jobs: BUILD_INTEGRATION: ON - name: Test debug - run: make test + run: ctest --verbose env: - ARGS: -V ZENOH_BRANCH: master diff --git a/BSDmakefile b/BSDmakefile index c1ffe3d5a..505dabb90 100644 --- a/BSDmakefile +++ b/BSDmakefile @@ -67,10 +67,10 @@ $(BUILD_DIR)/Makefile: CMakeLists.txt cmake $(CMAKE_OPT) -B$(BUILD_DIR) make: $(BUILD_DIR)/Makefile - make -C$(BUILD_DIR) + cmake --build $(BUILD_DIR) install: $(BUILD_DIR)/Makefile - make -C$(BUILD_DIR) install + cmake --install $(BUILD_DIR) test: $(BUILD_DIR)/Makefile make -C$(BUILD_DIR) test diff --git a/CMakeLists.txt b/CMakeLists.txt index 780c3f129..d22098853 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,7 @@ message(STATUS "Produce Debian and RPM packages: ${PACKAGING}") option(BUILD_EXAMPLES "Use this to also build the examples." ON) message(STATUS "Build examples: ${BUILD_EXAMPLES}") -option(BUILD_TOOLS "Use this to also build the tools." ON) +option(BUILD_TOOLS "Use this to also build the tools." OFF) message(STATUS "Build examples: ${BUILD_TOOLS}") option(BUILD_TESTING "Use this to also build tests." ON) @@ -73,9 +73,9 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin") elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten") add_definitions(-DZENOH_EMSCRIPTEN) elseif(CMAKE_SYSTEM_NAME MATCHES "Windows") - message(FATAL_ERROR "zenoh-pico is not yet available on Windows platform") - add_definitions(-DZENOH_WIN) - return() + add_definitions(-DZENOH_WINDOWS -D_CRT_SECURE_NO_WARNINGS) + set(BUILD_SHARED_LIBS "OFF") + add_definitions(-DZENOH_NO_STDATOMIC) else() message(FATAL_ERROR "zenoh-pico is not yet available on ${CMAKE_SYSTEM_NAME} platform") return() @@ -94,9 +94,17 @@ set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) if(CMAKE_BUILD_TYPE MATCHES "DEBUG") - add_compile_options(-c -Wall -Wextra -Werror -Wunused -Wstrict-prototypes -pipe -g -O0) + if(UNIX) + add_compile_options(-c -Wall -Wextra -Werror -Wunused -Wstrict-prototypes -pipe -g -O0) + elseif (MSVC) + add_compile_options(/W4 /WX /Od) + endif() elseif(CMAKE_BUILD_TYPE MATCHES "RELEASE") - add_compile_options(-pipe -O3) + if(UNIX) + add_compile_options(-pipe -O3) + elseif (MSVC) + add_compile_options(/O2) + endif() endif() set(Libname "zenohpico") @@ -151,6 +159,11 @@ if(CMAKE_SYSTEM_NAME MATCHES "Emscripten") list(APPEND Sources ${Sources_Emscripten}) endif() +if(CMAKE_SYSTEM_NAME MATCHES "Windows") + file (GLOB Sources_Windows "src/system/windows/*.c") + list(APPEND Sources ${Sources_Windows}) +endif() + set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib) link_directories(${LIBRARY_OUTPUT_PATH}) @@ -162,6 +175,10 @@ if(CMAKE_SYSTEM_NAME MATCHES "Linux") target_link_libraries(${Libname} rt) endif() +if(CMAKE_SYSTEM_NAME MATCHES "Windows") + target_link_libraries(${Libname} Ws2_32 Iphlpapi) +endif() + install(TARGETS ${Libname} LIBRARY DESTINATION lib ARCHIVE DESTINATION lib @@ -179,28 +196,41 @@ install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/zenoh-pico if(BUILD_EXAMPLES) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/examples) - if(CMAKE_C_STANDARD MATCHES "99") - add_executable(z_put ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_put.c) - add_executable(z_pub ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_pub.c) - add_executable(z_pub_st ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_pub_st.c) - add_executable(z_sub ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_sub.c) - add_executable(z_sub_st ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_sub_st.c) - add_executable(z_pull ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_pull.c) - add_executable(z_get ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_get.c) - add_executable(z_queryable ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_queryable.c) - add_executable(z_info ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_info.c) - add_executable(z_scout ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_scout.c) - else() - add_executable(z_put ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_put.c) - add_executable(z_pub ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_pub.c) - add_executable(z_pub_st ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_pub_st.c) - add_executable(z_sub ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_sub.c) - add_executable(z_sub_st ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_sub_st.c) - add_executable(z_pull ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_pull.c) - add_executable(z_get ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_get.c) - add_executable(z_queryable ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_queryable.c) - add_executable(z_info ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_info.c) - add_executable(z_scout ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_scout.c) + if(UNIX) + if(CMAKE_C_STANDARD MATCHES "99") + add_executable(z_put ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_put.c) + add_executable(z_pub ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_pub.c) + add_executable(z_pub_st ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_pub_st.c) + add_executable(z_sub ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_sub.c) + add_executable(z_sub_st ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_sub_st.c) + add_executable(z_pull ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_pull.c) + add_executable(z_get ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_get.c) + add_executable(z_queryable ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_queryable.c) + add_executable(z_info ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_info.c) + add_executable(z_scout ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_scout.c) + else() + add_executable(z_put ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_put.c) + add_executable(z_pub ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_pub.c) + add_executable(z_pub_st ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_pub_st.c) + add_executable(z_sub ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_sub.c) + add_executable(z_sub_st ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_sub_st.c) + add_executable(z_pull ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_pull.c) + add_executable(z_get ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_get.c) + add_executable(z_queryable ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_queryable.c) + add_executable(z_info ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_info.c) + add_executable(z_scout ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_scout.c) + endif() + elseif (MSVC) + add_executable(z_put ${PROJECT_SOURCE_DIR}/examples/windows/z_put.c) + add_executable(z_pub ${PROJECT_SOURCE_DIR}/examples/windows/z_pub.c) + add_executable(z_pub_st ${PROJECT_SOURCE_DIR}/examples/windows/z_pub_st.c) + add_executable(z_sub ${PROJECT_SOURCE_DIR}/examples/windows/z_sub.c) + add_executable(z_sub_st ${PROJECT_SOURCE_DIR}/examples/windows/z_sub_st.c) + add_executable(z_pull ${PROJECT_SOURCE_DIR}/examples/windows/z_pull.c) + add_executable(z_get ${PROJECT_SOURCE_DIR}/examples/windows/z_get.c) + add_executable(z_queryable ${PROJECT_SOURCE_DIR}/examples/windows/z_queryable.c) + add_executable(z_info ${PROJECT_SOURCE_DIR}/examples/windows/z_info.c) + add_executable(z_scout ${PROJECT_SOURCE_DIR}/examples/windows/z_scout.c) endif() target_link_libraries(z_put ${Libname}) diff --git a/GNUmakefile b/GNUmakefile index 062fc7261..367b18ac3 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -64,13 +64,13 @@ all: make $(BUILD_DIR)/Makefile: CMakeLists.txt mkdir -p $(BUILD_DIR) - cmake $(CMAKE_OPT) -B$(BUILD_DIR) + cmake $(CMAKE_OPT) -B $(BUILD_DIR) make: $(BUILD_DIR)/Makefile - make -C$(BUILD_DIR) + cmake --build $(BUILD_DIR) install: $(BUILD_DIR)/Makefile - make -C$(BUILD_DIR) install + cmake --install $(BUILD_DIR) test: $(BUILD_DIR)/Makefile make -C$(BUILD_DIR) test diff --git a/README.md b/README.md index 01738220b..f5b725997 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Currently, zenoh-pico provides support for the following (RT)OSs and protocols: | **(RT)OS** | **Transport Layer** | **Network Layer** | **Data Link Layer** | |:---------------:|:--------------------------------:|:-------------------:|:--------------------------------------------------:| | **Unix** | UDP (unicast and multicast), TCP | IPv4, IPv6, 6LoWPAN | WiFi, Ethernet, Thread | +| **Windows** | UDP (unicast and multicast), TCP | IPv4, IPv6 | WiFi, Ethernet | | **Zephyr** | UDP (unicast and multicast), TCP | IPv4, IPv6, 6LoWPAN | WiFi, Ethernet, Thread, Serial | | **Arduino** | UDP (unicast and multicast), TCP | IPv4, IPv6 | WiFi, Ethernet, Bluetooth (Serial profile), Serial | | **ESP-IDF** | UDP (unicast and multicast), TCP | IPv4, IPv6 | WiFi, Ethernet, Serial | @@ -48,8 +49,6 @@ There are 2 kind of packages: For other platforms - like RTOS for embedded systems / microcontrollers -, you will need to clone and build the sources. Check [below](#how-to-build-for-microcontrollers) for more details. -WARNING: Note that zenoh-pico has not been ported on Windows yet! - ------------------------------- ## 2. How to build it diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c new file mode 100644 index 000000000..a8e73652c --- /dev/null +++ b/examples/windows/z_get.c @@ -0,0 +1,96 @@ +// +// 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, + +#include +#include +#include +#include +#include + +void reply_dropper(void *ctx) { + (void)(ctx); + printf(">> Received query final notification\n"); +} + +void reply_handler(z_owned_reply_t *reply, void *ctx) { + (void)(ctx); + if (z_reply_is_ok(reply)) { + z_sample_t sample = z_reply_ok(reply); + z_owned_str_t keystr = z_keyexpr_to_string(sample.keyexpr); + printf(">> Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample.payload.len, sample.payload.start); + z_drop(z_move(keystr)); + } else { + printf(">> Received an error\n"); + } +} + +int main(int argc, char **argv) { + (void)(argc); + (void)(argv); + const char *keyexpr = "demo/example/**"; + const char *locator = NULL; + const char *value = NULL; + + z_owned_config_t config = z_config_default(); + if (locator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + return -1; + } + + // Start read and lease tasks for zenoh-pico + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + + z_keyexpr_t ke = z_keyexpr(keyexpr); + if (!z_check(ke)) { + printf("%s is not a valid key expression", keyexpr); + return -1; + } + + printf("Enter any key to pull data or 'q' to quit...\n"); + char c = '\0'; + while (1) { + fflush(stdin); + scanf("%c", &c); + if (c == 'q') { + break; + } + + printf("Sending Query '%s'...\n", keyexpr); + z_get_options_t opts = z_get_options_default(); + if (value != NULL) { + opts.with_value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + } + z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); + if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { + printf("Unable to send query.\n"); + return -1; + } + } + + // Stop read and lease tasks for zenoh-pico + zp_stop_read_task(z_loan(s)); + zp_stop_lease_task(z_loan(s)); + + z_close(z_move(s)); + + return 0; +} diff --git a/examples/windows/z_info.c b/examples/windows/z_info.c new file mode 100644 index 000000000..4df38b1e6 --- /dev/null +++ b/examples/windows/z_info.c @@ -0,0 +1,73 @@ +// +// 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, + +#include +#include +#include +#include +#include + +void print_zid(const z_id_t *id, void *ctx) { + (void)(ctx); + printf(" "); + for (int i = 15; i >= 0; i--) { + printf("%02X", id->id[i]); + } + printf("\n"); +} + +int main(int argc, char **argv) { + (void)(argc); + (void)(argv); + const char *mode = "client"; + char *locator = NULL; + + z_owned_config_t config = z_config_default(); + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + if (locator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + return -1; + } + + // Start read and lease tasks for zenoh-pico + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + + z_id_t self_id = z_info_zid(z_loan(s)); + printf("Own ID:"); + print_zid(&self_id, NULL); + + printf("Routers IDs:\n"); + z_owned_closure_zid_t callback = z_closure(print_zid); + z_info_routers_zid(z_loan(s), z_move(callback)); + + // `callback` has been `z_move`d just above, so it's safe to reuse the variable, + // we'll just have to make sure we `z_move` it again to avoid mem-leaks. + printf("Peers IDs:\n"); + z_owned_closure_zid_t callback2 = z_closure(print_zid); + z_info_peers_zid(z_loan(s), z_move(callback2)); + + // Stop read and lease tasks for zenoh-pico + zp_stop_read_task(z_loan(s)); + zp_stop_lease_task(z_loan(s)); + + z_close(z_move(s)); +} diff --git a/examples/windows/z_pub.c b/examples/windows/z_pub.c new file mode 100644 index 000000000..400872361 --- /dev/null +++ b/examples/windows/z_pub.c @@ -0,0 +1,76 @@ +// +// 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, +// + +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + (void)(argc); + (void)(argv); + const char *keyexpr = "demo/example/zenoh-pico-pub"; + const char *value = "Pub from Pico!"; + const char *mode = "client"; + char *locator = NULL; + + z_owned_config_t config = z_config_default(); + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + if (locator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + return -1; + } + + // Start read and lease tasks for zenoh-pico + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + + printf("Declaring publisher for '%s'...\n", keyexpr); + z_owned_publisher_t pub = z_declare_publisher(z_loan(s), z_keyexpr(keyexpr), NULL); + if (!z_check(pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + char *buf = (char *)malloc(256); + for (int idx = 0; 1; ++idx) { + Sleep(1); + snprintf(buf, 256, "[%4d] %s", idx, value); + printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); + + z_publisher_put_options_t options = z_publisher_put_options_default(); + options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); + } + + z_undeclare_publisher(z_move(pub)); + + // Stop read and lease tasks for zenoh-pico + zp_stop_read_task(z_loan(s)); + zp_stop_lease_task(z_loan(s)); + + z_close(z_move(s)); + + free(buf); + return 0; +} diff --git a/examples/windows/z_pub_st.c b/examples/windows/z_pub_st.c new file mode 100644 index 000000000..65ca61cfd --- /dev/null +++ b/examples/windows/z_pub_st.c @@ -0,0 +1,72 @@ +// +// 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, +// + +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + (void)(argc); + (void)(argv); + const char *keyexpr = "demo/example/zenoh-pico-pub"; + const char *value = "Pub from Pico!"; + const char *mode = "client"; + char *locator = NULL; + + z_owned_config_t config = z_config_default(); + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + if (locator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + return -1; + } + + printf("Declaring publisher for '%s'...\n", keyexpr); + z_owned_publisher_t pub = z_declare_publisher(z_loan(s), z_keyexpr(keyexpr), NULL); + if (!z_check(pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + char *buf = (char *)malloc(256); + z_clock_t now = z_clock_now(); + for (int idx = 0; 1;) { + 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); + ++idx; + + now = z_clock_now(); + } + + zp_read(z_loan(s), NULL); + zp_send_keep_alive(z_loan(s), NULL); + zp_send_join(z_loan(s), NULL); + } + + z_undeclare_publisher(z_move(pub)); + + z_close(z_move(s)); + + free(buf); + return 0; +} diff --git a/examples/windows/z_pull.c b/examples/windows/z_pull.c new file mode 100644 index 000000000..c98ead7fb --- /dev/null +++ b/examples/windows/z_pull.c @@ -0,0 +1,80 @@ +// +// 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, + +#include +#include +#include +#include +#include + +void data_handler(const z_sample_t *sample, void *ctx) { + (void)(ctx); + z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample->payload.len, + sample->payload.start); + z_drop(z_move(keystr)); +} + +int main(int argc, char **argv) { + (void)(argc); + (void)(argv); + const char *keyexpr = "demo/example/**"; + char *locator = NULL; + + z_owned_config_t config = z_config_default(); + if (locator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + return -1; + } + + // Start read and lease tasks for zenoh-pico + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + + z_owned_closure_sample_t callback = z_closure(data_handler); + printf("Declaring Subscriber on '%s'...\n", keyexpr); + z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL); + if (!z_check(sub)) { + printf("Unable to declare subscriber.\n"); + return -1; + } + + printf("Enter any key to pull data or 'q' to quit...\n"); + char c = '\0'; + while (1) { + fflush(stdin); + scanf("%c", &c); + if (c == 'q') { + break; + } + z_subscriber_pull(z_loan(sub)); + } + + z_undeclare_pull_subscriber(z_move(sub)); + + // Stop read and lease tasks for zenoh-pico + zp_stop_read_task(z_loan(s)); + zp_stop_lease_task(z_loan(s)); + + z_close(z_move(s)); + + return 0; +} diff --git a/examples/windows/z_put.c b/examples/windows/z_put.c new file mode 100644 index 000000000..7f71ae46d --- /dev/null +++ b/examples/windows/z_put.c @@ -0,0 +1,70 @@ +// +// 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, +// + +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + (void)(argc); + (void)(argv); + const char *keyexpr = "demo/example/zenoh-pico-put"; + const char *value = "Pub from Pico!"; + const char *mode = "client"; + char *locator = NULL; + + z_owned_config_t config = z_config_default(); + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + if (locator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + return -1; + } + + // Start read and lease tasks for zenoh-pico + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + + printf("Declaring key expression '%s'...\n", keyexpr); + z_owned_keyexpr_t ke = z_declare_keyexpr(z_loan(s), z_keyexpr(keyexpr)); + if (!z_check(ke)) { + printf("Unable to declare key expression!\n"); + return -1; + } + + printf("Putting Data ('%s': '%s')...\n", keyexpr, value); + z_put_options_t options = z_put_options_default(); + options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)value, strlen(value), &options) < 0) { + printf("Oh no! Put has failed...\n"); + } + + z_undeclare_keyexpr(z_loan(s), z_move(ke)); + + // Stop read and lease tasks for zenoh-pico + zp_stop_read_task(z_loan(s)); + zp_stop_lease_task(z_loan(s)); + + z_close(z_move(s)); + return 0; +} diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c new file mode 100644 index 000000000..c9f404540 --- /dev/null +++ b/examples/windows/z_queryable.c @@ -0,0 +1,91 @@ +// +// 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, + +#include +#include +#include +#include +#include + +const char *keyexpr = "demo/example/zenoh-pico-queryable"; +const char *value = "Queryable from Pico!"; + +void query_handler(const z_query_t *query, void *ctx) { + (void)(ctx); + z_owned_str_t keystr = z_keyexpr_to_string(z_query_keyexpr(query)); + z_bytes_t pred = z_query_parameters(query); + z_value_t payload_value = z_query_value(query); + printf(" >> [Queryable handler] Received Query '%s?%.*s'\n", z_loan(keystr), (int)pred.len, pred.start); + if (payload_value.payload.len > 0) { + printf(" with value '%.*s'\n", (int)payload_value.payload.len, payload_value.payload.start); + } + z_query_reply_options_t options = z_query_reply_options_default(); + options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + z_query_reply(query, z_keyexpr(keyexpr), (const unsigned char *)value, strlen(value), &options); + z_drop(z_move(keystr)); +} + +int main(int argc, char **argv) { + (void)(argc); + (void)(argv); + char *locator = NULL; + + z_owned_config_t config = z_config_default(); + if (locator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + return -1; + } + + // Start read and lease tasks for zenoh-pico + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + + z_keyexpr_t ke = z_keyexpr(keyexpr); + if (!z_check(ke)) { + printf("%s is not a valid key expression", keyexpr); + return -1; + } + + printf("Creating Queryable on '%s'...\n", keyexpr); + z_owned_closure_query_t callback = z_closure(query_handler); + z_owned_queryable_t qable = z_declare_queryable(z_loan(s), ke, z_move(callback), NULL); + if (!z_check(qable)) { + printf("Unable to create queryable.\n"); + return -1; + } + + printf("Enter 'q' to quit...\n"); + char c = '\0'; + while (c != 'q') { + fflush(stdin); + scanf("%c", &c); + } + + z_undeclare_queryable(z_move(qable)); + + // Stop read and lease tasks for zenoh-pico + zp_stop_read_task(z_loan(s)); + zp_stop_lease_task(z_loan(s)); + + z_close(z_move(s)); + + return 0; +} diff --git a/examples/windows/z_scout.c b/examples/windows/z_scout.c new file mode 100644 index 000000000..1d1590b02 --- /dev/null +++ b/examples/windows/z_scout.c @@ -0,0 +1,94 @@ + +// +// 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, +// + +#include +#include +#include +#include + +void fprintzid(FILE *stream, z_bytes_t zid) { + if (zid.start == NULL) { + fprintf(stream, "None"); + } else { + fprintf(stream, "Some("); + for (unsigned int i = 0; i < zid.len; i++) { + fprintf(stream, "%02X", (int)zid.start[i]); + } + fprintf(stream, ")"); + } +} + +void fprintwhatami(FILE *stream, unsigned int whatami) { + if (whatami == Z_WHATAMI_ROUTER) { + fprintf(stream, "\"Router\""); + } else if (whatami == Z_WHATAMI_PEER) { + fprintf(stream, "\"Peer\""); + } else { + fprintf(stream, "\"Other\""); + } +} + +void fprintlocators(FILE *stream, const z_str_array_t *locs) { + fprintf(stream, "["); + for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + fprintf(stream, "\""); + fprintf(stream, "%s", *z_str_array_get(locs, i)); + fprintf(stream, "\""); + if (i < z_str_array_len(locs) - 1) { + fprintf(stream, ", "); + } + } + fprintf(stream, "]"); +} + +void fprinthello(FILE *stream, const z_hello_t hello) { + fprintf(stream, "Hello { zid: "); + fprintzid(stream, hello.zid); + fprintf(stream, ", whatami: "); + fprintwhatami(stream, hello.whatami); + fprintf(stream, ", locators: "); + fprintlocators(stream, &hello.locators); + fprintf(stream, " }"); +} + +void callback(z_owned_hello_t *hello, void *context) { + fprinthello(stdout, z_loan(*hello)); + fprintf(stdout, "\n"); + (*(int *)context)++; +} + +void drop(void *context) { + int count = *(int *)context; + free(context); + if (!count) { + printf("Did not find any zenoh process.\n"); + } else { + printf("Dropping scout results.\n"); + } +} + +int main(int argc, char **argv) { + (void)(argc); + (void)(argv); + + int *context = (int *)malloc(sizeof(int)); + *context = 0; + z_owned_scouting_config_t config = z_scouting_config_default(); + z_owned_closure_hello_t closure = z_closure(callback, drop, context); + printf("Scouting...\n"); + z_scout(z_move(config), z_move(closure)); + Sleep(1); + return 0; +} diff --git a/examples/windows/z_sub.c b/examples/windows/z_sub.c new file mode 100644 index 000000000..61c41bf57 --- /dev/null +++ b/examples/windows/z_sub.c @@ -0,0 +1,79 @@ +// +// 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, +// + +#include +#include +#include +#include +#include + +void data_handler(const z_sample_t *sample, void *ctx) { + (void)(ctx); + z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample->payload.len, + sample->payload.start); + z_drop(z_move(keystr)); +} + +int main(int argc, char **argv) { + (void)(argc); + (void)(argv); + const char *keyexpr = "demo/example/**"; + const char *mode = "client"; + char *locator = NULL; + + z_owned_config_t config = z_config_default(); + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + if (locator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + return -1; + } + + // Start read and lease tasks for zenoh-pico + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + + z_owned_closure_sample_t callback = z_closure(data_handler); + printf("Declaring Subscriber on '%s'...\n", keyexpr); + z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL); + if (!z_check(sub)) { + printf("Unable to declare subscriber.\n"); + return -1; + } + + printf("Enter 'q' to quit...\n"); + char c = '\0'; + while (c != 'q') { + fflush(stdin); + scanf("%c", &c); + } + + z_undeclare_subscriber(z_move(sub)); + + // Stop read and lease tasks for zenoh-pico + zp_stop_read_task(z_loan(s)); + zp_stop_lease_task(z_loan(s)); + + z_close(z_move(s)); + + return 0; +} diff --git a/examples/windows/z_sub_st.c b/examples/windows/z_sub_st.c new file mode 100644 index 000000000..d89337c02 --- /dev/null +++ b/examples/windows/z_sub_st.c @@ -0,0 +1,74 @@ +// +// 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, +// + +#include +#include +#include +#include +#include + +void data_handler(const z_sample_t *sample, void *ctx) { + (void)(ctx); + z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample->payload.len, + sample->payload.start); + z_drop(z_move(keystr)); +} + +int main(int argc, char **argv) { + (void)(argc); + (void)(argv); + const char *keyexpr = "demo/example/**"; + const char *mode = "client"; + char *locator = NULL; + + z_owned_config_t config = z_config_default(); + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + if (locator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + return -1; + } + + // Start read and lease tasks for zenoh-pico + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + + z_owned_closure_sample_t callback = z_closure(data_handler); + printf("Declaring Subscriber on '%s'...\n", keyexpr); + z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL); + if (!z_check(sub)) { + printf("Unable to declare subscriber.\n"); + return -1; + } + + while (1) { + zp_read(z_loan(s), NULL); + zp_send_keep_alive(z_loan(s), NULL); + zp_send_join(z_loan(s), NULL); + } + + z_undeclare_subscriber(z_move(sub)); + + z_close(z_move(s)); + + return 0; +} diff --git a/extra_script.py b/extra_script.py index ba6a57e05..391eb8047 100644 --- a/extra_script.py +++ b/extra_script.py @@ -26,7 +26,8 @@ "-", "-", "-", - "-"] + "-", + "-"] CPPDEFINES = ["ZENOH_ZEPHYR"] elif FRAMEWORK == 'arduino': @@ -40,6 +41,7 @@ "-", "-", "-", + "-", "-"] CPPDEFINES = ["ZENOH_ARDUINO_ESP32", "ZENOH_C_STANDARD=99"] if PLATFORM == 'ststm32': @@ -53,6 +55,7 @@ "-", "-", "-", + "-", "-"] CPPDEFINES = ["ZENOH_ARDUINO_OPENCR", "ZENOH_C_STANDARD=99", "Z_MULTI_THREAD=0"] @@ -64,6 +67,7 @@ "-", "-", "-", + "-", "-"] CPPDEFINES = ["ZENOH_ESPIDF"] @@ -75,6 +79,7 @@ "-", "-", "-", + "-", "-"] CPPDEFINES = ["ZENOH_MBED", "ZENOH_C_STANDARD=99"] diff --git a/include/zenoh-pico/collections/pointer.h b/include/zenoh-pico/collections/pointer.h index a1b7730b2..8105be5be 100644 --- a/include/zenoh-pico/collections/pointer.h +++ b/include/zenoh-pico/collections/pointer.h @@ -18,7 +18,8 @@ #include #include -#if ZENOH_C_STANDARD != 99 +#if ZENOH_C_STANDARD != 99 && !defined(ZENOH_NO_STDATOMIC) + #ifndef __cplusplus #include #define z_atomic(X) _Atomic X diff --git a/include/zenoh-pico/collections/vec.h b/include/zenoh-pico/collections/vec.h index cb565d54a..a6bcdb71d 100644 --- a/include/zenoh-pico/collections/vec.h +++ b/include/zenoh-pico/collections/vec.h @@ -44,24 +44,22 @@ 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); -#define _Z_VEC_DEFINE(name, type) \ - typedef _z_vec_t name##_vec_t; \ - static inline name##_vec_t name##_vec_make(size_t capacity) { return (name##_vec_t)_z_vec_make(capacity); } \ - static inline size_t name##_vec_len(const name##_vec_t *v) { return _z_vec_len(v); } \ - static inline _Bool name##_vec_is_empty(const name##_vec_t *v) { return _z_vec_is_empty(v); } \ - static inline void name##_vec_append(name##_vec_t *v, type *e) { return _z_vec_append(v, e); } \ - static inline type *name##_vec_get(const name##_vec_t *v, size_t pos) { return (type *)_z_vec_get(v, pos); } \ - static inline void name##_vec_set(name##_vec_t *v, size_t pos, type *e) { \ - return _z_vec_set(v, pos, e, name##_elem_free); \ - } \ - static inline void name##_vec_remove(name##_vec_t *v, size_t pos) { \ - return _z_vec_remove(v, pos, name##_elem_free); \ - } \ - static inline void name##_vec_copy(name##_vec_t *dst, const name##_vec_t *src) { \ - return _z_vec_copy(dst, src, name##_elem_clone); \ - } \ - static inline void name##_vec_reset(name##_vec_t *v) { return _z_vec_reset(v, name##_elem_free); } \ - static inline void name##_vec_clear(name##_vec_t *v) { return _z_vec_clear(v, name##_elem_free); } \ - static inline void name##_vec_free(name##_vec_t **v) { return _z_vec_free(v, name##_elem_free); } +#define _Z_VEC_DEFINE(name, type) \ + typedef _z_vec_t name##_vec_t; \ + static inline name##_vec_t name##_vec_make(size_t capacity) { return _z_vec_make(capacity); } \ + static inline size_t name##_vec_len(const name##_vec_t *v) { return _z_vec_len(v); } \ + static inline _Bool name##_vec_is_empty(const name##_vec_t *v) { return _z_vec_is_empty(v); } \ + static inline void name##_vec_append(name##_vec_t *v, type *e) { _z_vec_append(v, e); } \ + static inline type *name##_vec_get(const name##_vec_t *v, size_t pos) { return (type *)_z_vec_get(v, pos); } \ + static inline void name##_vec_set(name##_vec_t *v, size_t pos, type *e) { \ + _z_vec_set(v, pos, e, name##_elem_free); \ + } \ + static inline void name##_vec_remove(name##_vec_t *v, size_t pos) { _z_vec_remove(v, pos, name##_elem_free); } \ + static inline void name##_vec_copy(name##_vec_t *dst, const name##_vec_t *src) { \ + _z_vec_copy(dst, src, name##_elem_clone); \ + } \ + 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); } #endif /* ZENOH_PICO_COLLECTIONS_VECTOR_H */ diff --git a/include/zenoh-pico/link/config/bt.h b/include/zenoh-pico/link/config/bt.h index 22cc7474b..ae24fa245 100644 --- a/include/zenoh-pico/link/config/bt.h +++ b/include/zenoh-pico/link/config/bt.h @@ -21,6 +21,8 @@ #if Z_LINK_BLUETOOTH == 1 +#define BT_CONFIG_ARGC 3 + #define BT_CONFIG_MODE_KEY 0x01 #define BT_CONFIG_MODE_STR "mode" @@ -30,14 +32,13 @@ #define BT_CONFIG_TOUT_KEY 0x03 #define BT_CONFIG_TOUT_STR "tout" -#define BT_CONFIG_MAPPING_BUILD \ - uint8_t argc = 3; \ - _z_str_intmapping_t args[argc]; \ - args[0]._key = BT_CONFIG_MODE_KEY; \ - args[0]._str = BT_CONFIG_MODE_STR; \ - args[1]._key = BT_CONFIG_PROFILE_KEY; \ - args[1]._str = BT_CONFIG_PROFILE_STR; \ - args[2]._key = BT_CONFIG_TOUT_KEY; \ +#define BT_CONFIG_MAPPING_BUILD \ + _z_str_intmapping_t args[BT_CONFIG_ARGC]; \ + args[0]._key = BT_CONFIG_MODE_KEY; \ + args[0]._str = BT_CONFIG_MODE_STR; \ + args[1]._key = BT_CONFIG_PROFILE_KEY; \ + args[1]._str = BT_CONFIG_PROFILE_STR; \ + args[2]._key = BT_CONFIG_TOUT_KEY; \ args[2]._str = BT_CONFIG_TOUT_STR; size_t _z_bt_config_strlen(const _z_str_intmap_t *s); diff --git a/include/zenoh-pico/link/config/serial.h b/include/zenoh-pico/link/config/serial.h index 946c740cd..3b5fd16a6 100644 --- a/include/zenoh-pico/link/config/serial.h +++ b/include/zenoh-pico/link/config/serial.h @@ -22,6 +22,8 @@ #if Z_LINK_SERIAL == 1 +#define SERIAL_CONFIG_ARGC 1 + #define SERIAL_CONFIG_BAUDRATE_KEY 0x01 #define SERIAL_CONFIG_BAUDRATE_STR "baudrate" @@ -40,10 +42,9 @@ // #define SERIAL_CONFIG_TOUT_KEY 0x06 // #define SERIAL_CONFIG_TOUT_STR "tout" -#define SERIAL_CONFIG_MAPPING_BUILD \ - uint8_t argc = 1; \ - _z_str_intmapping_t args[argc]; \ - args[0]._key = SERIAL_CONFIG_BAUDRATE_KEY; \ +#define SERIAL_CONFIG_MAPPING_BUILD \ + _z_str_intmapping_t args[SERIAL_CONFIG_ARGC]; \ + args[0]._key = SERIAL_CONFIG_BAUDRATE_KEY; \ args[0]._str = SERIAL_CONFIG_BAUDRATE_STR; // args[1]._key = SERIAL_CONFIG_DATABITS_KEY; // args[1]._str = SERIAL_CONFIG_DATABITS_STR; diff --git a/include/zenoh-pico/link/config/tcp.h b/include/zenoh-pico/link/config/tcp.h index 8e8aaa378..a5d758544 100644 --- a/include/zenoh-pico/link/config/tcp.h +++ b/include/zenoh-pico/link/config/tcp.h @@ -21,13 +21,14 @@ #if Z_LINK_TCP == 1 +#define TCP_CONFIG_ARGC 1 + #define TCP_CONFIG_TOUT_KEY 0x01 #define TCP_CONFIG_TOUT_STR "tout" -#define TCP_CONFIG_MAPPING_BUILD \ - uint8_t argc = 1; \ - _z_str_intmapping_t args[argc]; \ - args[0]._key = TCP_CONFIG_TOUT_KEY; \ +#define TCP_CONFIG_MAPPING_BUILD \ + _z_str_intmapping_t args[TCP_CONFIG_ARGC]; \ + args[0]._key = TCP_CONFIG_TOUT_KEY; \ args[0]._str = TCP_CONFIG_TOUT_STR; size_t _z_tcp_config_strlen(const _z_str_intmap_t *s); diff --git a/include/zenoh-pico/link/config/udp.h b/include/zenoh-pico/link/config/udp.h index f3272ee89..e7a4dffc9 100644 --- a/include/zenoh-pico/link/config/udp.h +++ b/include/zenoh-pico/link/config/udp.h @@ -20,18 +20,19 @@ #if Z_LINK_UDP_UNICAST == 1 || Z_LINK_UDP_MULTICAST == 1 +#define UDP_CONFIG_ARGC 2 + #define UDP_CONFIG_IFACE_KEY 0x01 #define UDP_CONFIG_IFACE_STR "iface" #define UDP_CONFIG_TOUT_KEY 0x02 #define UDP_CONFIG_TOUT_STR "tout" -#define UDP_CONFIG_MAPPING_BUILD \ - uint8_t argc = 2; \ - _z_str_intmapping_t args[argc]; \ - args[0]._key = UDP_CONFIG_IFACE_KEY; \ - args[0]._str = UDP_CONFIG_IFACE_STR; \ - args[1]._key = UDP_CONFIG_TOUT_KEY; \ +#define UDP_CONFIG_MAPPING_BUILD \ + _z_str_intmapping_t args[UDP_CONFIG_ARGC]; \ + args[0]._key = UDP_CONFIG_IFACE_KEY; \ + args[0]._str = UDP_CONFIG_IFACE_STR; \ + args[1]._key = UDP_CONFIG_TOUT_KEY; \ args[1]._str = UDP_CONFIG_TOUT_STR; size_t _z_udp_config_strlen(const _z_str_intmap_t *s); diff --git a/include/zenoh-pico/net/resource.h b/include/zenoh-pico/net/resource.h index abf2b88b9..566c649d4 100644 --- a/include/zenoh-pico/net/resource.h +++ b/include/zenoh-pico/net/resource.h @@ -39,6 +39,6 @@ _z_keyexpr_t _z_rname(const char *rname); * Returns: * A :c:type:`_z_keyexpr_t` containing a new resource key. */ -_z_keyexpr_t _z_rid_with_suffix(unsigned long rid, const char *suffix); +_z_keyexpr_t _z_rid_with_suffix(_z_zint_t rid, const char *suffix); #endif /* ZENOH_PICO_RESOURCE_NETAPI_H */ diff --git a/include/zenoh-pico/system/link/tcp.h b/include/zenoh-pico/system/link/tcp.h index 8b102c8bb..382b8b7de 100644 --- a/include/zenoh-pico/system/link/tcp.h +++ b/include/zenoh-pico/system/link/tcp.h @@ -27,7 +27,7 @@ typedef struct { _z_sys_net_endpoint_t _rep; } _z_tcp_socket_t; -int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port); +int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port); void _z_free_endpoint_tcp(_z_sys_net_endpoint_t *ep); int8_t _z_open_tcp(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, uint32_t tout); diff --git a/include/zenoh-pico/system/link/udp.h b/include/zenoh-pico/system/link/udp.h index 8c04754dc..33a466180 100644 --- a/include/zenoh-pico/system/link/udp.h +++ b/include/zenoh-pico/system/link/udp.h @@ -29,7 +29,7 @@ typedef struct { _z_sys_net_endpoint_t _lep; } _z_udp_socket_t; -int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port); +int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port); void _z_free_endpoint_udp(_z_sys_net_endpoint_t *ep); // Unicast @@ -44,13 +44,16 @@ size_t _z_send_udp_unicast(const _z_sys_net_socket_t sock, const uint8_t *ptr, s // Multicast int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, _z_sys_net_endpoint_t *lep, uint32_t tout, const char *iface); -int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, uint32_t tout, const char *iface); -void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t *socksend, const _z_sys_net_endpoint_t rep); -size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t lep, - _z_bytes_t *ep); +int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, uint32_t tout, + const char *iface); +void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t *socksend, + const _z_sys_net_endpoint_t rep); +size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, + const _z_sys_net_endpoint_t lep, _z_bytes_t *ep); size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t lep, _z_bytes_t *ep); -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); +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); #endif #endif /* ZENOH_PICO_SYSTEM_LINK_UDP_H */ diff --git a/include/zenoh-pico/system/platform.h b/include/zenoh-pico/system/platform.h index 42048388f..07e1f009e 100644 --- a/include/zenoh-pico/system/platform.h +++ b/include/zenoh-pico/system/platform.h @@ -21,6 +21,8 @@ #if defined(ZENOH_LINUX) || defined(ZENOH_MACOS) || defined(ZENOH_BSD) #include "zenoh-pico/system/platform/unix.h" +#elif defined(ZENOH_WINDOWS) +#include "zenoh-pico/system/platform/windows.h" #elif defined(ZENOH_ESPIDF) #include "zenoh-pico/system/platform/espidf.h" #elif defined(ZENOH_MBED) @@ -78,9 +80,9 @@ int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m); #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int z_sleep_us(unsigned int time); -int z_sleep_ms(unsigned int time); -int z_sleep_s(unsigned int time); +int z_sleep_us(size_t time); +int z_sleep_ms(size_t time); +int z_sleep_s(size_t time); /*------------------ Clock ------------------*/ z_clock_t z_clock_now(void); diff --git a/include/zenoh-pico/system/platform/espidf.h b/include/zenoh-pico/system/platform/espidf.h index 8c7a1b80a..6c35dd1f0 100644 --- a/include/zenoh-pico/system/platform/espidf.h +++ b/include/zenoh-pico/system/platform/espidf.h @@ -25,7 +25,6 @@ #if Z_MULTI_THREAD == 1 typedef TaskHandle_t _z_task_t; typedef void *_z_task_attr_t; // Not used in ESP32 - typedef pthread_mutex_t _z_mutex_t; typedef pthread_cond_t _z_condvar_t; #endif // Z_MULTI_THREAD == 1 diff --git a/include/zenoh-pico/system/platform/windows.h b/include/zenoh-pico/system/platform/windows.h new file mode 100644 index 000000000..f7c7d1ccb --- /dev/null +++ b/include/zenoh-pico/system/platform/windows.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_SYSTEM_WINDOWS_H +#define ZENOH_PICO_SYSTEM_WINDOWS_H + +#include +#include + +#include "zenoh-pico/config.h" + +#if Z_MULTI_THREAD == 1 +typedef HANDLE *_z_task_t; +typedef void *_z_task_attr_t; // Not used in Windows +typedef SRWLOCK _z_mutex_t; +typedef CONDITION_VARIABLE _z_condvar_t; +#endif // Z_MULTI_THREAD == 1 + +typedef struct timeb z_clock_t; +typedef struct timeb z_time_t; + +typedef struct { + union { +#if Z_LINK_TCP == 1 || Z_LINK_UDP_MULTICAST == 1 || Z_LINK_UDP_UNICAST == 1 + SOCKET _fd; +#endif + } _sock; +} _z_sys_net_socket_t; + +typedef struct { + union { +#if Z_LINK_TCP == 1 || Z_LINK_UDP_MULTICAST == 1 || Z_LINK_UDP_UNICAST == 1 + struct addrinfo *_iptcp; +#endif + } _ep; +} _z_sys_net_endpoint_t; + +inline void __asm__(char *asm) { (void)(asm); } + +#endif /* ZENOH_PICO_SYSTEM_VOID_H */ diff --git a/src/api/api.c b/src/api/api.c index 702c76c05..09ff661a9 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -673,7 +673,7 @@ z_publisher_put_options_t z_publisher_put_options_default(void) { return (z_publisher_put_options_t){.encoding = z_encoding_default()}; } -z_publisher_delete_options_t z_publisher_delete_options_default(void) { return (z_publisher_delete_options_t){}; } +z_publisher_delete_options_t z_publisher_delete_options_default(void) { return (z_publisher_delete_options_t){.__dummy = 0}; } int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t len, const z_publisher_put_options_t *options) { @@ -837,7 +837,7 @@ z_value_t z_reply_err(const z_owned_reply_t *reply) { z_sample_t z_reply_ok(z_owned_reply_t *reply) { return reply->_value->data.sample; } /**************** Tasks ****************/ -zp_task_read_options_t zp_task_read_options_default(void) { return (zp_task_read_options_t){}; } +zp_task_read_options_t zp_task_read_options_default(void) { return (zp_task_read_options_t){ .__dummy = 0 }; } int8_t zp_start_read_task(z_session_t zs, const zp_task_read_options_t *options) { (void)(options); @@ -858,7 +858,7 @@ int8_t zp_stop_read_task(z_session_t zs) { #endif } -zp_task_lease_options_t zp_task_lease_options_default(void) { return (zp_task_lease_options_t){}; } +zp_task_lease_options_t zp_task_lease_options_default(void) { return (zp_task_lease_options_t){ .__dummy = 0 }; } int8_t zp_start_lease_task(z_session_t zs, const zp_task_lease_options_t *options) { (void)(options); @@ -879,21 +879,21 @@ int8_t zp_stop_lease_task(z_session_t zs) { #endif } -zp_read_options_t zp_read_options_default(void) { return (zp_read_options_t){}; } +zp_read_options_t zp_read_options_default(void) { return (zp_read_options_t){ .__dummy = 0 }; } int8_t zp_read(z_session_t zs, const zp_read_options_t *options) { (void)(options); return _zp_read(zs._val); } -zp_send_keep_alive_options_t zp_send_keep_alive_options_default(void) { return (zp_send_keep_alive_options_t){}; } +zp_send_keep_alive_options_t zp_send_keep_alive_options_default(void) { return (zp_send_keep_alive_options_t){ .__dummy = 0 }; } int8_t zp_send_keep_alive(z_session_t zs, const zp_send_keep_alive_options_t *options) { (void)(options); return _zp_send_keep_alive(zs._val); } -zp_send_join_options_t zp_send_join_options_default(void) { return (zp_send_join_options_t){}; } +zp_send_join_options_t zp_send_join_options_default(void) { return (zp_send_join_options_t){ .__dummy = 0 }; } int8_t zp_send_join(z_session_t zs, const zp_send_join_options_t *options) { (void)(options); diff --git a/src/collections/vec.c b/src/collections/vec.c index 709c02840..d4e73b4bb 100644 --- a/src/collections/vec.c +++ b/src/collections/vec.c @@ -19,7 +19,7 @@ #include /*-------- vec --------*/ -inline _z_vec_t _z_vec_make(size_t capacity) { +_z_vec_t _z_vec_make(size_t capacity) { _z_vec_t v; v._val = (void **)z_malloc(sizeof(void *) * capacity); if (v._val != NULL) { diff --git a/src/link/config/bt.c b/src/link/config/bt.c index 2db4e732e..989817cac 100644 --- a/src/link/config/bt.c +++ b/src/link/config/bt.c @@ -20,30 +20,30 @@ size_t _z_bt_config_strlen(const _z_str_intmap_t *s) { BT_CONFIG_MAPPING_BUILD - return _z_str_intmap_strlen(s, argc, args); + return _z_str_intmap_strlen(s, BT_CONFIG_ARGC, args); } void _z_bt_config_onto_str(char *dst, size_t dst_len, const _z_str_intmap_t *s) { BT_CONFIG_MAPPING_BUILD - return _z_str_intmap_onto_str(dst, dst_len, s, argc, args); + _z_str_intmap_onto_str(dst, dst_len, s, BT_CONFIG_ARGC, args); } char *_z_bt_config_to_str(const _z_str_intmap_t *s) { BT_CONFIG_MAPPING_BUILD - return _z_str_intmap_to_str(s, argc, args); + return _z_str_intmap_to_str(s, BT_CONFIG_ARGC, args); } int8_t _z_bt_config_from_strn(_z_str_intmap_t *strint, const char *s, size_t n) { BT_CONFIG_MAPPING_BUILD - return _z_str_intmap_from_strn(strint, s, argc, args, n); + return _z_str_intmap_from_strn(strint, s, BT_CONFIG_ARGC, args, n); } int8_t _z_bt_config_from_str(_z_str_intmap_t *strint, const char *s) { BT_CONFIG_MAPPING_BUILD - return _z_str_intmap_from_str(strint, s, argc, args); + return _z_str_intmap_from_str(strint, s, BT_CONFIG_ARGC, args); } #endif diff --git a/src/link/config/serial.c b/src/link/config/serial.c index fc9c2fbf9..c0608195d 100644 --- a/src/link/config/serial.c +++ b/src/link/config/serial.c @@ -21,30 +21,30 @@ size_t _z_serial_config_strlen(const _z_str_intmap_t *s) { SERIAL_CONFIG_MAPPING_BUILD - return _z_str_intmap_strlen(s, argc, args); + return _z_str_intmap_strlen(s, SERIAL_CONFIG_ARGC, args); } void _z_serial_config_onto_str(char *dst, size_t dst_len, const _z_str_intmap_t *s) { SERIAL_CONFIG_MAPPING_BUILD - return _z_str_intmap_onto_str(dst, dst_len, s, argc, args); + _z_str_intmap_onto_str(dst, dst_len, s, SERIAL_CONFIG_ARGC, args); } char *_z_serial_config_to_str(const _z_str_intmap_t *s) { SERIAL_CONFIG_MAPPING_BUILD - return _z_str_intmap_to_str(s, argc, args); + return _z_str_intmap_to_str(s, SERIAL_CONFIG_ARGC, args); } int8_t _z_serial_config_from_strn(_z_str_intmap_t *strint, const char *s, size_t n) { SERIAL_CONFIG_MAPPING_BUILD - return _z_str_intmap_from_strn(strint, s, argc, args, n); + return _z_str_intmap_from_strn(strint, s, SERIAL_CONFIG_ARGC, args, n); } int8_t _z_serial_config_from_str(_z_str_intmap_t *strint, const char *s) { SERIAL_CONFIG_MAPPING_BUILD - return _z_str_intmap_from_str(strint, s, argc, args); + return _z_str_intmap_from_str(strint, s, SERIAL_CONFIG_ARGC, args); } #endif diff --git a/src/link/config/tcp.c b/src/link/config/tcp.c index 082a19d55..e5ad017ae 100644 --- a/src/link/config/tcp.c +++ b/src/link/config/tcp.c @@ -23,25 +23,25 @@ size_t _z_tcp_config_strlen(const _z_str_intmap_t *s) { TCP_CONFIG_MAPPING_BUILD - return _z_str_intmap_strlen(s, argc, args); + return _z_str_intmap_strlen(s, TCP_CONFIG_ARGC, args); } void _z_tcp_config_onto_str(char *dst, size_t dst_len, const _z_str_intmap_t *s) { TCP_CONFIG_MAPPING_BUILD - return _z_str_intmap_onto_str(dst, dst_len, s, argc, args); + _z_str_intmap_onto_str(dst, dst_len, s, TCP_CONFIG_ARGC, args); } char *_z_tcp_config_to_str(const _z_str_intmap_t *s) { TCP_CONFIG_MAPPING_BUILD - return _z_str_intmap_to_str(s, argc, args); + return _z_str_intmap_to_str(s, TCP_CONFIG_ARGC, args); } int8_t _z_tcp_config_from_strn(_z_str_intmap_t *strint, const char *s, size_t n) { TCP_CONFIG_MAPPING_BUILD - return _z_str_intmap_from_strn(strint, s, argc, args, n); + return _z_str_intmap_from_strn(strint, s, TCP_CONFIG_ARGC, args, n); } int8_t _z_tcp_config_from_str(_z_str_intmap_t *strint, const char *s) { diff --git a/src/link/config/udp.c b/src/link/config/udp.c index e1028ff95..17761dda8 100644 --- a/src/link/config/udp.c +++ b/src/link/config/udp.c @@ -23,25 +23,25 @@ size_t _z_udp_config_strlen(const _z_str_intmap_t *s) { UDP_CONFIG_MAPPING_BUILD - return _z_str_intmap_strlen(s, argc, args); + return _z_str_intmap_strlen(s, UDP_CONFIG_ARGC, args); } void _z_udp_config_onto_str(char *dst, size_t dst_len, const _z_str_intmap_t *s) { UDP_CONFIG_MAPPING_BUILD - return _z_str_intmap_onto_str(dst, dst_len, s, argc, args); + _z_str_intmap_onto_str(dst, dst_len, s, UDP_CONFIG_ARGC, args); } char *_z_udp_config_to_str(const _z_str_intmap_t *s) { UDP_CONFIG_MAPPING_BUILD - return _z_str_intmap_to_str(s, argc, args); + return _z_str_intmap_to_str(s, UDP_CONFIG_ARGC, args); } int8_t _z_udp_config_from_strn(_z_str_intmap_t *strint, const char *s, size_t n) { UDP_CONFIG_MAPPING_BUILD - return _z_str_intmap_from_strn(strint, s, argc, args, n); + return _z_str_intmap_from_strn(strint, s, UDP_CONFIG_ARGC, args, n); } int8_t _z_udp_config_from_str(_z_str_intmap_t *strint, const char *s) { diff --git a/src/link/multicast/udp.c b/src/link/multicast/udp.c index a1be73237..5c3f510fc 100644 --- a/src/link/multicast/udp.c +++ b/src/link/multicast/udp.c @@ -79,11 +79,11 @@ int8_t _z_endpoint_udp_multicast_valid(_z_endpoint_t *endpoint) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } - char *s_addr = __z_parse_address_segment_udp_multicast(endpoint->_locator._address); - if (s_addr == NULL) { + char *s_address = __z_parse_address_segment_udp_multicast(endpoint->_locator._address); + if (s_address == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } else { - z_free(s_addr); + z_free(s_address); } char *s_port = __z_parse_port_segment_udp_multicast(endpoint->_locator._address); @@ -169,10 +169,10 @@ int8_t _z_new_link_udp_multicast(_z_link_t *zl, _z_endpoint_t endpoint) { zl->_mtu = _z_get_link_mtu_udp_multicast(); zl->_endpoint = endpoint; - char *s_addr = __z_parse_address_segment_udp_multicast(endpoint._locator._address); + char *s_address = __z_parse_address_segment_udp_multicast(endpoint._locator._address); char *s_port = __z_parse_port_segment_udp_multicast(endpoint._locator._address); - ret = _z_create_endpoint_udp(&zl->_socket._udp._rep, s_addr, s_port); - z_free(s_addr); + ret = _z_create_endpoint_udp(&zl->_socket._udp._rep, s_address, s_port); + z_free(s_address); z_free(s_port); zl->_open_f = _z_f_link_open_udp_multicast; diff --git a/src/link/unicast/tcp.c b/src/link/unicast/tcp.c index a7d801b0e..23344ee3e 100644 --- a/src/link/unicast/tcp.c +++ b/src/link/unicast/tcp.c @@ -79,11 +79,11 @@ int8_t _z_endpoint_tcp_valid(_z_endpoint_t *endpoint) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } - char *s_addr = __z_parse_address_segment_tcp(endpoint->_locator._address); - if (s_addr == NULL) { + char *s_address = __z_parse_address_segment_tcp(endpoint->_locator._address); + if (s_address == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } else { - z_free(s_addr); + z_free(s_address); } char *s_port = __z_parse_port_segment_tcp(endpoint->_locator._address); @@ -156,10 +156,10 @@ int8_t _z_new_link_tcp(_z_link_t *zl, _z_endpoint_t *endpoint) { zl->_mtu = _z_get_link_mtu_tcp(); zl->_endpoint = *endpoint; - char *s_addr = __z_parse_address_segment_tcp(endpoint->_locator._address); + char *s_address = __z_parse_address_segment_tcp(endpoint->_locator._address); char *s_port = __z_parse_port_segment_tcp(endpoint->_locator._address); - ret = _z_create_endpoint_tcp(&zl->_socket._tcp._rep, s_addr, s_port); - z_free(s_addr); + ret = _z_create_endpoint_tcp(&zl->_socket._tcp._rep, s_address, s_port); + z_free(s_address); z_free(s_port); zl->_open_f = _z_f_link_open_tcp; diff --git a/src/link/unicast/udp.c b/src/link/unicast/udp.c index 9354044e3..71ee861d7 100644 --- a/src/link/unicast/udp.c +++ b/src/link/unicast/udp.c @@ -79,11 +79,11 @@ int8_t _z_endpoint_udp_unicast_valid(_z_endpoint_t *endpoint) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } - char *s_addr = __z_parse_address_segment_udp_unicast(endpoint->_locator._address); - if (s_addr == NULL) { + char *s_address = __z_parse_address_segment_udp_unicast(endpoint->_locator._address); + if (s_address == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } else { - z_free(s_addr); + z_free(s_address); } char *s_port = __z_parse_port_segment_udp_unicast(endpoint->_locator._address); @@ -162,10 +162,10 @@ int8_t _z_new_link_udp_unicast(_z_link_t *zl, _z_endpoint_t endpoint) { zl->_mtu = _z_get_link_mtu_udp_unicast(); zl->_endpoint = endpoint; - char *s_addr = __z_parse_address_segment_udp_unicast(endpoint._locator._address); + char *s_address = __z_parse_address_segment_udp_unicast(endpoint._locator._address); char *s_port = __z_parse_port_segment_udp_unicast(endpoint._locator._address); - ret = _z_create_endpoint_udp(&zl->_socket._udp._rep, s_addr, s_port); - z_free(s_addr); + ret = _z_create_endpoint_udp(&zl->_socket._udp._rep, s_address, s_port); + z_free(s_address); z_free(s_port); zl->_open_f = _z_f_link_open_udp_unicast; diff --git a/src/net/resource.c b/src/net/resource.c index fc3a7ad11..8ce48333f 100644 --- a/src/net/resource.c +++ b/src/net/resource.c @@ -27,7 +27,7 @@ _z_keyexpr_t _z_rname(const char *rname) { return rk; } -_z_keyexpr_t _z_rid_with_suffix(unsigned long rid, const char *suffix) { +_z_keyexpr_t _z_rid_with_suffix(_z_zint_t rid, const char *suffix) { _z_keyexpr_t rk; rk._id = rid; rk._suffix = NULL; diff --git a/src/protocol/codec.c b/src/protocol/codec.c index f2da9cd41..9e71bf1fc 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -49,7 +49,7 @@ int8_t _z_period_decode_na(_z_period_t *p, _z_zbuf_t *buf) { int8_t _z_period_decode(_z_period_t *p, _z_zbuf_t *buf) { return _z_period_decode_na(p, buf); } /*------------------ uint8 -------------------*/ -int8_t _z_enum_encode(_z_wbuf_t *wbf, int en) { return _z_wbuf_write(wbf, en); } +int8_t _z_enum_encode(_z_wbuf_t *wbf, int en) { return _z_wbuf_write(wbf, (uint8_t)en); } int8_t _z_enum_decode(int *en, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; diff --git a/src/protocol/config.c b/src/protocol/config.c index 6b08b74e9..3c70a27e6 100644 --- a/src/protocol/config.c +++ b/src/protocol/config.c @@ -55,7 +55,7 @@ int8_t _z_str_intmap_from_strn(_z_str_intmap_t *strint, const char *s, uint8_t a // Verify the key is valid based on the provided mapping size_t p_key_len = _z_ptr_char_diff(p_key_end, p_key_start); _Bool found = false; - uint8_t key; + uint8_t key = 0; for (uint8_t i = 0; i < argc; i++) { if (p_key_len != strlen(argv[i]._str)) { continue; diff --git a/src/system/arduino/esp32/network.cpp b/src/system/arduino/esp32/network.cpp index c540a112f..80462b794 100644 --- a/src/system/arduino/esp32/network.cpp +++ b/src/system/arduino/esp32/network.cpp @@ -34,7 +34,7 @@ extern "C" { #if Z_LINK_TCP == 1 /*------------------ TCP sockets ------------------*/ -int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port) { +int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { int8_t ret = _Z_RES_OK; struct addrinfo hints; @@ -44,7 +44,7 @@ int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_addr, con hints.ai_flags = 0; hints.ai_protocol = IPPROTO_TCP; - if (getaddrinfo(s_addr, s_port, &hints, &ep->_iptcp) < 0) { + if (getaddrinfo(s_address, s_port, &hints, &ep->_iptcp) < 0) { ret = _Z_ERR_GENERIC; } @@ -113,7 +113,7 @@ 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 < 0) { + if (rb < (ssize_t)0) { rb = SIZE_MAX; } @@ -145,7 +145,7 @@ size_t _z_send_tcp(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t le #if Z_LINK_UDP_UNICAST == 1 || Z_LINK_UDP_MULTICAST == 1 /*------------------ UDP sockets ------------------*/ -int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port) { +int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { int8_t ret = _Z_RES_OK; struct addrinfo hints; @@ -155,7 +155,7 @@ int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_addr, con hints.ai_flags = 0; hints.ai_protocol = IPPROTO_UDP; - if (getaddrinfo(s_addr, s_port, &hints, &ep->_iptcp) < 0) { + if (getaddrinfo(s_address, s_port, &hints, &ep->_iptcp) < 0) { ret = _Z_ERR_GENERIC; } @@ -207,7 +207,7 @@ size_t _z_read_udp_unicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t unsigned int addrlen = sizeof(struct sockaddr_storage); ssize_t rb = recvfrom(sock._fd, ptr, len, 0, (struct sockaddr *)&raddr, &addrlen); - if (rb < 0) { + if (rb < (ssize_t)0) { rb = SIZE_MAX; } @@ -480,7 +480,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ ssize_t rb = 0; do { rb = recvfrom(sock._fd, ptr, len, 0, (struct sockaddr *)&raddr, &raddrlen); - if (rb < 0) { + if (rb < (ssize_t)0) { rb = SIZE_MAX; break; } diff --git a/src/system/arduino/esp32/system.c b/src/system/arduino/esp32/system.c index 55b9e756b..1d823c14c 100644 --- a/src/system/arduino/esp32/system.c +++ b/src/system/arduino/esp32/system.c @@ -115,9 +115,9 @@ int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wa #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int z_sleep_us(unsigned int time) { return usleep(time); } +int z_sleep_us(size_t time) { return usleep(time); } -int z_sleep_ms(unsigned int time) { +int z_sleep_ms(size_t time) { z_time_t start = z_time_now(); // Most sleep APIs promise to sleep at least whatever you asked them to. @@ -130,7 +130,7 @@ int z_sleep_ms(unsigned int time) { return 0; } -int z_sleep_s(unsigned int time) { return sleep(time); } +int z_sleep_s(size_t time) { return sleep(time); } /*------------------ Instant ------------------*/ z_clock_t z_clock_now(void) { diff --git a/src/system/arduino/opencr/network.cpp b/src/system/arduino/opencr/network.cpp index 51ed5f240..ed3042985 100644 --- a/src/system/arduino/opencr/network.cpp +++ b/src/system/arduino/opencr/network.cpp @@ -29,12 +29,12 @@ extern "C" { #if Z_LINK_TCP == 1 /*------------------ UDP sockets ------------------*/ -int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port) { +int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { int8_t ret = _Z_RES_OK; // Parse and check the validity of the IP address ep->_iptcp._addr = new IPAddress(); - if (!ep->_iptcp._addr->fromString(s_addr)) { + if (!ep->_iptcp._addr->fromString(s_address)) { ret = _Z_ERR_GENERIC; } @@ -121,12 +121,12 @@ size_t _z_send_tcp(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t le #if Z_LINK_UDP_UNICAST == 1 || Z_LINK_UDP_MULTICAST == 1 /*------------------ UDP sockets ------------------*/ -int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port) { +int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { int8_t ret = _Z_RES_OK; // Parse and check the validity of the IP address ep->_iptcp._addr = new IPAddress(); - if (!ep->_iptcp._addr->fromString(s_addr)) { + if (!ep->_iptcp._addr->fromString(s_address)) { ret = _Z_ERR_GENERIC; } diff --git a/src/system/arduino/opencr/system.c b/src/system/arduino/opencr/system.c index 7f7a3bacf..946392bb3 100644 --- a/src/system/arduino/opencr/system.c +++ b/src/system/arduino/opencr/system.c @@ -97,17 +97,17 @@ int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return -1; } #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int z_sleep_us(unsigned int time) { +int z_sleep_us(size_t time) { delay_us(time); return 0; } -int z_sleep_ms(unsigned int time) { +int z_sleep_ms(size_t time) { delay_ms(time); return 0; } -int z_sleep_s(unsigned int time) { +int z_sleep_s(size_t time) { z_time_t start = z_time_now(); // Most sleep APIs promise to sleep at least whatever you asked them to. diff --git a/src/system/emscripten/system.c b/src/system/emscripten/system.c index 38b31a16c..89242bd72 100644 --- a/src/system/emscripten/system.c +++ b/src/system/emscripten/system.c @@ -80,17 +80,17 @@ int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wa #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int z_sleep_us(unsigned int time) { +int z_sleep_us(size_t time) { emscripten_sleep((time / 1000) + (time % 1000 == 0 ? 0 : 1)); return 0; } -int z_sleep_ms(unsigned int time) { +int z_sleep_ms(size_t time) { emscripten_sleep(time); return 0; } -int z_sleep_s(unsigned int time) { +int z_sleep_s(size_t time) { z_time_t start = z_time_now(); // Most sleep APIs promise to sleep at least whatever you asked them to. diff --git a/src/system/espidf/network.c b/src/system/espidf/network.c index 3e3e86888..f6a24bd0b 100644 --- a/src/system/espidf/network.c +++ b/src/system/espidf/network.c @@ -30,7 +30,7 @@ #if Z_LINK_TCP == 1 /*------------------ TCP sockets ------------------*/ -int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port) { +int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { int8_t ret = _Z_RES_OK; struct addrinfo hints; @@ -40,7 +40,7 @@ int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_addr, con hints.ai_flags = 0; hints.ai_protocol = IPPROTO_TCP; - if (getaddrinfo(s_addr, s_port, &hints, &ep->_iptcp) < 0) { + if (getaddrinfo(s_address, s_port, &hints, &ep->_iptcp) < 0) { ret = _Z_ERR_GENERIC; } @@ -109,7 +109,7 @@ 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 < 0) { + if (rb < (ssize_t)0) { rb = SIZE_MAX; } @@ -141,7 +141,7 @@ size_t _z_send_tcp(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t le #if Z_LINK_UDP_UNICAST == 1 || Z_LINK_UDP_MULTICAST == 1 /*------------------ UDP sockets ------------------*/ -int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port) { +int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { int8_t ret = _Z_RES_OK; struct addrinfo hints; @@ -151,7 +151,7 @@ int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_addr, con hints.ai_flags = 0; hints.ai_protocol = IPPROTO_UDP; - if (getaddrinfo(s_addr, s_port, &hints, &ep->_iptcp) < 0) { + if (getaddrinfo(s_address, s_port, &hints, &ep->_iptcp) < 0) { ret = _Z_ERR_GENERIC; } @@ -203,7 +203,7 @@ size_t _z_read_udp_unicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t socklen_t addrlen = sizeof(struct sockaddr_storage); ssize_t rb = recvfrom(sock._fd, ptr, len, 0, (struct sockaddr *)&raddr, &addrlen); - if (rb < 0) { + if (rb < (ssize_t)0) { rb = SIZE_MAX; } @@ -476,7 +476,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ ssize_t rb = 0; do { rb = recvfrom(sock._fd, ptr, len, 0, (struct sockaddr *)&raddr, &raddrlen); - if (rb < 0) { + if (rb < (ssize_t)0) { rb = SIZE_MAX; break; } diff --git a/src/system/espidf/system.c b/src/system/espidf/system.c index 9bfb3d270..a3a4dd472 100644 --- a/src/system/espidf/system.c +++ b/src/system/espidf/system.c @@ -116,9 +116,9 @@ int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wa #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int z_sleep_us(unsigned int time) { return usleep(time); } +int z_sleep_us(size_t time) { return usleep(time); } -int z_sleep_ms(unsigned int time) { +int z_sleep_ms(size_t time) { z_time_t start = z_time_now(); // Most sleep APIs promise to sleep at least whatever you asked them to. @@ -131,7 +131,7 @@ int z_sleep_ms(unsigned int time) { return 0; } -int z_sleep_s(unsigned int time) { return sleep(time); } +int z_sleep_s(size_t time) { return sleep(time); } /*------------------ Instant ------------------*/ z_clock_t z_clock_now(void) { diff --git a/src/system/mbed/network.cpp b/src/system/mbed/network.cpp index 2742b3c4d..fa189f4b8 100644 --- a/src/system/mbed/network.cpp +++ b/src/system/mbed/network.cpp @@ -33,13 +33,13 @@ extern "C" { #if Z_LINK_TCP == 1 /*------------------ TCP sockets ------------------*/ -int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port) { +int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { int8_t ret = _Z_RES_OK; // Parse and check the validity of the port uint32_t port = strtoul(s_port, NULL, 10); if ((port > (uint32_t)0) && (port <= (uint32_t)65355)) { // Port numbers should range from 1 to 65355 - ep->_iptcp = new SocketAddress(s_addr, port); + ep->_iptcp = new SocketAddress(s_address, port); } else { ret = _Z_ERR_GENERIC; } @@ -87,7 +87,7 @@ 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) { nsapi_size_or_error_t rb = sock._tcp->recv(ptr, len); - if (rb < 0) { + if (rb < (nsapi_size_or_error_t)0) { rb = SIZE_MAX; } @@ -117,13 +117,13 @@ size_t _z_send_tcp(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t le #if Z_LINK_UDP_UNICAST == 1 || Z_LINK_UDP_MULTICAST == 1 /*------------------ UDP sockets ------------------*/ -int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port) { +int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { int8_t ret = _Z_RES_OK; // Parse and check the validity of the port uint32_t port = strtoul(s_port, NULL, 10); if ((port > (uint32_t)0) && (port <= (uint32_t)65355)) { // Port numbers should range from 1 to 65355 - ep->_iptcp = new SocketAddress(s_addr, port); + ep->_iptcp = new SocketAddress(s_address, port); } else { ret = _Z_ERR_GENERIC; } @@ -173,7 +173,7 @@ void _z_close_udp_unicast(_z_sys_net_socket_t *sock) { size_t _z_read_udp_unicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { SocketAddress raddr; nsapi_size_or_error_t rb = sock._udp->recvfrom(&raddr, ptr, len); - if (rb < 0) { + if (rb < (nsapi_size_or_error_t)0) { rb = SIZE_MAX; } @@ -266,7 +266,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ do { rb = sock._udp->recvfrom(&raddr, ptr, len); - if (rb < 0) { + if (rb < (nsapi_size_or_error_t)0) { rb = SIZE_MAX; break; } diff --git a/src/system/mbed/system.cpp b/src/system/mbed/system.cpp index cf63aa7c1..68dc20b01 100644 --- a/src/system/mbed/system.cpp +++ b/src/system/mbed/system.cpp @@ -109,17 +109,17 @@ int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int z_sleep_us(unsigned int time) { +int z_sleep_us(size_t time) { ThisThread::sleep_for(chrono::milliseconds(((time / 1000) + (time % 1000 == 0 ? 0 : 1)))); return 0; } -int z_sleep_ms(unsigned int time) { +int z_sleep_ms(size_t time) { ThisThread::sleep_for(chrono::milliseconds(time)); return 0; } -int z_sleep_s(unsigned int time) { +int z_sleep_s(size_t time) { ThisThread::sleep_for(chrono::seconds(time)); return 0; } diff --git a/src/system/unix/network.c b/src/system/unix/network.c index cc723a8e1..8a71d1d82 100644 --- a/src/system/unix/network.c +++ b/src/system/unix/network.c @@ -34,7 +34,7 @@ #if Z_LINK_TCP == 1 /*------------------ TCP sockets ------------------*/ -int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port) { +int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { int8_t ret = _Z_RES_OK; struct addrinfo hints; @@ -44,7 +44,7 @@ int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_addr, con hints.ai_flags = 0; hints.ai_protocol = IPPROTO_TCP; - if (getaddrinfo(s_addr, s_port, &hints, &ep->_iptcp) < 0) { + if (getaddrinfo(s_address, s_port, &hints, &ep->_iptcp) < 0) { ret = _Z_ERR_GENERIC; } @@ -124,7 +124,7 @@ 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 < 0) { + if (rb < (ssize_t)0) { rb = SIZE_MAX; } @@ -160,7 +160,7 @@ size_t _z_send_tcp(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t le #if Z_LINK_UDP_UNICAST == 1 || Z_LINK_UDP_MULTICAST == 1 /*------------------ UDP sockets ------------------*/ -int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port) { +int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { int8_t ret = _Z_RES_OK; struct addrinfo hints; @@ -170,7 +170,7 @@ int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_addr, con hints.ai_flags = 0; hints.ai_protocol = IPPROTO_UDP; - if (getaddrinfo(s_addr, s_port, &hints, &ep->_iptcp) < 0) { + if (getaddrinfo(s_address, s_port, &hints, &ep->_iptcp) < 0) { ret = _Z_ERR_GENERIC; } @@ -222,7 +222,7 @@ size_t _z_read_udp_unicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t unsigned int addrlen = sizeof(struct sockaddr_storage); ssize_t rb = recvfrom(sock._fd, ptr, len, 0, (struct sockaddr *)&raddr, &addrlen); - if (rb < 0) { + if (rb < (ssize_t)0) { rb = SIZE_MAX; } @@ -489,7 +489,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ ssize_t rb = 0; do { rb = recvfrom(sock._fd, ptr, len, 0, (struct sockaddr *)&raddr, &replen); - if (rb < 0) { + if (rb < (ssize_t)0) { rb = SIZE_MAX; break; } diff --git a/src/system/unix/system.c b/src/system/unix/system.c index 5faf15956..e5dca90e9 100644 --- a/src/system/unix/system.c +++ b/src/system/unix/system.c @@ -100,7 +100,7 @@ void z_free(void *ptr) { free(ptr); } #if Z_MULTI_THREAD == 1 /*------------------ Task ------------------*/ -int8_t _z_task_init(_z_task_t *task, pthread_attr_t *attr, void *(*fun)(void *), void *arg) { +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); } @@ -136,9 +136,9 @@ int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wa #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int z_sleep_us(unsigned int time) { return usleep(time); } +int z_sleep_us(size_t time) { return usleep(time); } -int z_sleep_ms(unsigned int time) { +int z_sleep_ms(size_t time) { z_time_t start = z_time_now(); // Most sleep APIs promise to sleep at least whatever you asked them to. @@ -151,7 +151,7 @@ int z_sleep_ms(unsigned int time) { return 0; } -int z_sleep_s(unsigned int time) { return sleep(time); } +int z_sleep_s(size_t time) { return sleep(time); } /*------------------ Instant ------------------*/ z_clock_t z_clock_now(void) { diff --git a/src/system/windows/network.c b/src/system/windows/network.c new file mode 100644 index 000000000..8895b68ea --- /dev/null +++ b/src/system/windows/network.c @@ -0,0 +1,611 @@ +// +// 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, +// + +#include +// The following includes must come after winsock2 +#include +#include + +#include "zenoh-pico/collections/string.h" +#include "zenoh-pico/config.h" +#include "zenoh-pico/system/platform.h" +#include "zenoh-pico/utils/logging.h" +#include "zenoh-pico/utils/pointers.h" + +WSADATA wsaData; + +#if Z_LINK_TCP == 1 + +/*------------------ TCP sockets ------------------*/ +int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { + int8_t ret = _Z_RES_OK; + + if (WSAStartup(MAKEWORD(2, 2), &wsaData) == 0) { + ADDRINFOA hints; + (void)memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; // Allow IPv4 or IPv6 + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = 0; + hints.ai_protocol = IPPROTO_TCP; + + if (getaddrinfo(s_address, s_port, &hints, &ep->_ep._iptcp) != 0) { + ret = _Z_ERR_GENERIC; + } + } else { + ret = _Z_ERR_GENERIC; + } + + return ret; +} + +void _z_free_endpoint_tcp(_z_sys_net_endpoint_t *ep) { + freeaddrinfo(ep->_ep._iptcp); + WSACleanup(); +} + +/*------------------ TCP sockets ------------------*/ +int8_t _z_open_tcp(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, uint32_t tout) { + int8_t ret = _Z_RES_OK; + + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { + ret = _Z_ERR_GENERIC; + } + + sock->_sock._fd = socket(rep._ep._iptcp->ai_family, rep._ep._iptcp->ai_socktype, rep._ep._iptcp->ai_protocol); + if (sock->_sock._fd != INVALID_SOCKET) { + z_time_t tv; + tv.time = tout / (uint32_t)1000; + tv.millitm = tout % (uint32_t)1000; + if ((ret == _Z_RES_OK) && (setsockopt(sock->_sock._fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { + ret = _Z_ERR_GENERIC; + } + + int flags = 1; + if ((ret == _Z_RES_OK) && + (setsockopt(sock->_sock._fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags)) < 0)) { + ret = _Z_ERR_GENERIC; + } + + struct linger ling; + ling.l_onoff = 1; + ling.l_linger = Z_TRANSPORT_LEASE / 1000; + if ((ret == _Z_RES_OK) && + (setsockopt(sock->_sock._fd, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(struct linger)) < 0)) { + ret = _Z_ERR_GENERIC; + } + +#if defined(ZENOH_MACOS) || defined(ZENOH_BSD) + setsockopt(sock->_sock._fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)0, sizeof(int)); +#endif + + ADDRINFOA *it = NULL; + for (it = rep._ep._iptcp; it != NULL; it = it->ai_next) { + if ((ret == _Z_RES_OK) && (connect(sock->_sock._fd, it->ai_addr, (int)it->ai_addrlen) < 0)) { + if (it->ai_next == NULL) { + ret = _Z_ERR_GENERIC; + break; + } + } else { + break; + } + } + + if (ret != _Z_RES_OK) { + closesocket(sock->_sock._fd); + WSACleanup(); + } + } else { + ret = _Z_ERR_GENERIC; + } + + return ret; +} + +int8_t _z_listen_tcp(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t lep) { + int8_t ret = _Z_RES_OK; + (void)sock; + (void)lep; + + // @TODO: To be implemented + ret = _Z_ERR_GENERIC; + + return ret; +} + +void _z_close_tcp(_z_sys_net_socket_t *sock) { + shutdown(sock->_sock._fd, SD_BOTH); + closesocket(sock->_sock._fd); + WSACleanup(); +} + +size_t _z_read_tcp(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { + size_t rb = recv(sock._sock._fd, (char *)ptr, (int)len, 0); + if (rb < (size_t)0) { + rb = SIZE_MAX; + } + + return rb; +} + +size_t _z_read_exact_tcp(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { + size_t n = 0; + uint8_t *pos = &ptr[0]; + + do { + size_t rb = _z_read_tcp(sock, pos, len - n); + if (rb == SIZE_MAX) { + n = rb; + break; + } + + n = n + rb; + pos = _z_ptr_u8_offset(pos, n); + } while (n != len); + + return n; +} + +size_t _z_send_tcp(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len) { + return send(sock._sock._fd, (const char *)ptr, (int)len, 0); +} +#endif + +#if Z_LINK_UDP_UNICAST == 1 || Z_LINK_UDP_MULTICAST == 1 +/*------------------ UDP sockets ------------------*/ +int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { + int8_t ret = _Z_RES_OK; + + if (WSAStartup(MAKEWORD(2, 2), &wsaData) == 0) { + ADDRINFOA hints; + (void)memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; // Allow IPv4 or IPv6 + hints.ai_socktype = SOCK_DGRAM; + hints.ai_flags = 0; + hints.ai_protocol = IPPROTO_UDP; + + if (getaddrinfo(s_address, s_port, &hints, &ep->_ep._iptcp) != 0) { + ret = _Z_ERR_GENERIC; + } + } else { + ret = _Z_ERR_GENERIC; + } + + return ret; +} + +void _z_free_endpoint_udp(_z_sys_net_endpoint_t *ep) { + freeaddrinfo(ep->_ep._iptcp); + WSACleanup(); +} +#endif + +#if Z_LINK_UDP_UNICAST == 1 +int8_t _z_open_udp_unicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, uint32_t tout) { + int8_t ret = _Z_RES_OK; + + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { + ret = _Z_ERR_GENERIC; + } + + sock->_sock._fd = socket(rep._ep._iptcp->ai_family, rep._ep._iptcp->ai_socktype, rep._ep._iptcp->ai_protocol); + if (sock->_sock._fd != INVALID_SOCKET) { + z_time_t tv; + tv.time = tout / (uint32_t)1000; + tv.millitm = tout % (uint32_t)1000; + if ((ret == _Z_RES_OK) && (setsockopt(sock->_sock._fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { + ret = _Z_ERR_GENERIC; + } + + if (ret != _Z_RES_OK) { + closesocket(sock->_sock._fd); + WSACleanup(); + } + } else { + ret = _Z_ERR_GENERIC; + } + + return ret; +} + +int8_t _z_listen_udp_unicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t lep, uint32_t tout) { + (void)sock; + (void)lep; + (void)tout; + int8_t ret = _Z_RES_OK; + + // @TODO: To be implemented + ret = _Z_ERR_GENERIC; + + return ret; +} + +void _z_close_udp_unicast(_z_sys_net_socket_t *sock) { + closesocket(sock->_sock._fd); + WSACleanup(); +} + +size_t _z_read_udp_unicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { + SOCKADDR_STORAGE raddr; + unsigned int addrlen = sizeof(SOCKADDR_STORAGE); + + size_t rb = recvfrom(sock._sock._fd, (char *)ptr, (int)len, 0, (SOCKADDR *)&raddr, (int *)&addrlen); + if (rb < (size_t)0) { + rb = SIZE_MAX; + } + + return rb; +} + +size_t _z_read_exact_udp_unicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { + size_t n = 0; + uint8_t *pos = &ptr[0]; + + do { + size_t rb = _z_read_udp_unicast(sock, pos, len - n); + if (rb == SIZE_MAX) { + n = rb; + break; + } + + n = n + rb; + pos = _z_ptr_u8_offset(pos, n); + } while (n != len); + + return n; +} + +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._sock._fd, (const char *)ptr, (int)len, 0, rep._ep._iptcp->ai_addr, + (int)rep._ep._iptcp->ai_addrlen); +} +#endif + +#if Z_LINK_UDP_MULTICAST == 1 +unsigned int __get_ip_from_iface(const char *iface, int sa_family, SOCKADDR **lsockaddr) { + unsigned int addrlen = 0U; + + unsigned long outBufLen = 15000; + IP_ADAPTER_ADDRESSES *l_ifaddr = (IP_ADAPTER_ADDRESSES *)z_malloc(outBufLen); + if (l_ifaddr != NULL) { + if (GetAdaptersAddresses(sa_family, 0, NULL, l_ifaddr, &outBufLen) != ERROR_BUFFER_OVERFLOW) { + for (IP_ADAPTER_ADDRESSES *tmp = l_ifaddr; tmp != NULL; tmp = tmp->Next) { + if (_z_str_eq(tmp->AdapterName, iface) == true) { + if (sa_family == AF_INET) { + *lsockaddr = (SOCKADDR *)z_malloc(sizeof(SOCKADDR_IN)); + if (lsockaddr != NULL) { + (void)memset(*lsockaddr, 0, sizeof(SOCKADDR_IN)); + (void)memcpy(*lsockaddr, tmp->FirstUnicastAddress->Address.lpSockaddr, sizeof(SOCKADDR_IN)); + addrlen = sizeof(SOCKADDR_IN); + } + } else if (sa_family == AF_INET6) { + *lsockaddr = (SOCKADDR *)z_malloc(sizeof(SOCKADDR_IN6)); + if (lsockaddr != NULL) { + (void)memset(*lsockaddr, 0, sizeof(SOCKADDR_IN6)); + (void)memcpy(*lsockaddr, tmp->FirstUnicastAddress->Address.lpSockaddr, + sizeof(SOCKADDR_IN6)); + addrlen = sizeof(SOCKADDR_IN6); + } + } else { + continue; + } + + break; + } + } + } + + z_free(l_ifaddr); + l_ifaddr = NULL; + } + + return addrlen; +} + +int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, _z_sys_net_endpoint_t *lep, + uint32_t tout, const char *iface) { + int8_t ret = _Z_RES_OK; + + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { + ret = _Z_ERR_GENERIC; + } + + SOCKADDR *lsockaddr = NULL; + unsigned int addrlen = __get_ip_from_iface(iface, rep._ep._iptcp->ai_family, &lsockaddr); + if (addrlen != 0U) { + sock->_sock._fd = socket(rep._ep._iptcp->ai_family, rep._ep._iptcp->ai_socktype, rep._ep._iptcp->ai_protocol); + if (sock->_sock._fd != INVALID_SOCKET) { + z_time_t tv; + tv.time = tout / (uint32_t)1000; + tv.millitm = tout % (uint32_t)1000; + if ((ret == _Z_RES_OK) && + (setsockopt(sock->_sock._fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { + ret = _Z_ERR_GENERIC; + } + + if (rep._ep._iptcp->ai_family == AF_INET) { + SOCKADDR_IN address = { + .sin_family = AF_INET, .sin_port = htons(0), .sin_addr.s_addr = htonl(INADDR_ANY), .sin_zero = {0}}; + if ((ret == _Z_RES_OK) && + (bind(sock->_sock._fd, (SOCKADDR *)&address, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)) { + ret = _Z_ERR_GENERIC; + } + } else if (rep._ep._iptcp->ai_family == AF_INET6) { + SOCKADDR_IN6 address = {.sin6_family = AF_INET6, .sin6_port = htons(0), 0, .sin6_addr = {{{0}}}, 0}; + if ((ret == _Z_RES_OK) && + (bind(sock->_sock._fd, (SOCKADDR *)&address, sizeof(SOCKADDR_IN6)) == SOCKET_ERROR)) { + ret = _Z_ERR_GENERIC; + } + } else { + ret = _Z_ERR_GENERIC; + } + + // Get the randomly assigned port used to discard loopback messages + if ((ret == _Z_RES_OK) && (getsockname(sock->_sock._fd, lsockaddr, (int *)&addrlen) < 0)) { + ret = _Z_ERR_GENERIC; + } + + if (rep._ep._iptcp->ai_family == AF_INET) { + if ((ret == _Z_RES_OK) && + (setsockopt(sock->_sock._fd, IPPROTO_IP, IP_MULTICAST_IF, + (const char *)&((SOCKADDR_IN *)lsockaddr)->sin_addr.s_addr, addrlen) < 0)) { + ret = _Z_ERR_GENERIC; + } + } else if (rep._ep._iptcp->ai_family == AF_INET6) { + if ((ret == _Z_RES_OK) && + (setsockopt(sock->_sock._fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, + (const char *)&((SOCKADDR_IN6 *)lsockaddr)->sin6_scope_id, sizeof(ULONG)) < 0)) { + ret = _Z_ERR_GENERIC; + } + } else { + ret = _Z_ERR_GENERIC; + } + + // Create lep endpoint + if (ret == _Z_RES_OK) { + ADDRINFOA *laddr = (ADDRINFOA *)z_malloc(sizeof(ADDRINFOA)); + if (laddr != NULL) { + laddr->ai_flags = 0; + laddr->ai_family = rep._ep._iptcp->ai_family; + laddr->ai_socktype = rep._ep._iptcp->ai_socktype; + laddr->ai_protocol = rep._ep._iptcp->ai_family; + laddr->ai_addrlen = addrlen; + laddr->ai_addr = lsockaddr; + laddr->ai_canonname = NULL; + laddr->ai_next = NULL; + lep->_ep._iptcp = laddr; + + // This is leaking 16 bytes according to valgrind, but it is a know problem on some libc6 + // implementations: + // https://lists.debian.org/debian-glibc/2016/03/msg00241.html + // To avoid a fix to break zenoh-pico, we are let it leak for the moment. + //#if defined(ZENOH_LINUX) + // z_free(lsockaddr); + //#endif + } else { + ret = _Z_ERR_GENERIC; + } + } + + if (ret != _Z_RES_OK) { + closesocket(sock->_sock._fd); + WSACleanup(); + } + } else { + ret = _Z_ERR_GENERIC; + } + + if (ret != _Z_RES_OK) { + z_free(lsockaddr); + } + } else { + ret = _Z_ERR_GENERIC; + } + + return ret; +} + +int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, uint32_t tout, + const char *iface) { + int8_t ret = _Z_RES_OK; + + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { + ret = _Z_ERR_GENERIC; + } + + SOCKADDR *lsockaddr = NULL; + unsigned int addrlen = __get_ip_from_iface(iface, rep._ep._iptcp->ai_family, &lsockaddr); + if (addrlen != 0U) { + sock->_sock._fd = socket(rep._ep._iptcp->ai_family, rep._ep._iptcp->ai_socktype, rep._ep._iptcp->ai_protocol); + if (sock->_sock._fd != INVALID_SOCKET) { + z_time_t tv; + tv.time = tout / (uint32_t)1000; + tv.millitm = tout % (uint32_t)1000; + if ((ret == _Z_RES_OK) && + (setsockopt(sock->_sock._fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { + ret = _Z_ERR_GENERIC; + } + + int optflag = 1; + if ((ret == _Z_RES_OK) && + (setsockopt(sock->_sock._fd, SOL_SOCKET, SO_REUSEADDR, (char *)&optflag, sizeof(optflag)) < 0)) { + ret = _Z_ERR_GENERIC; + } + + if (rep._ep._iptcp->ai_family == AF_INET) { + SOCKADDR_IN address = {.sin_family = AF_INET, + .sin_port = ((SOCKADDR_IN *)rep._ep._iptcp->ai_addr)->sin_port, + .sin_addr = {0}, + .sin_zero = {0}}; + if ((ret == _Z_RES_OK) && (bind(sock->_sock._fd, (SOCKADDR *)&address, sizeof address) < 0)) { + ret = _Z_ERR_GENERIC; + } + } else if (rep._ep._iptcp->ai_family == AF_INET6) { + SOCKADDR_IN6 address = {.sin6_family = AF_INET6, + .sin6_port = ((SOCKADDR_IN6 *)rep._ep._iptcp->ai_addr)->sin6_port, + 0, + .sin6_addr = {{{0}}}, + 0}; + if ((ret == _Z_RES_OK) && (bind(sock->_sock._fd, (SOCKADDR *)&address, sizeof address) < 0)) { + ret = _Z_ERR_GENERIC; + } + } else { + ret = _Z_ERR_GENERIC; + } + + // Join the multicast group + if (rep._ep._iptcp->ai_family == AF_INET) { + struct ip_mreq mreq; + (void)memset(&mreq, 0, sizeof(mreq)); + mreq.imr_multiaddr.s_addr = ((SOCKADDR_IN *)rep._ep._iptcp->ai_addr)->sin_addr.s_addr; + mreq.imr_interface.s_addr = ((SOCKADDR_IN *)lsockaddr)->sin_addr.s_addr; + if ((ret == _Z_RES_OK) && (setsockopt(sock->_sock._fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, + (const char *)&mreq, sizeof(mreq)) < 0)) { + ret = _Z_ERR_GENERIC; + } + } else if (rep._ep._iptcp->ai_family == AF_INET6) { + struct ipv6_mreq mreq; + (void)memset(&mreq, 0, sizeof(mreq)); + (void)memcpy(&mreq.ipv6mr_multiaddr, &((SOCKADDR_IN6 *)rep._ep._iptcp->ai_addr)->sin6_addr, + sizeof(IN6_ADDR)); + mreq.ipv6mr_interface = if_nametoindex(iface); + if ((ret == _Z_RES_OK) && (setsockopt(sock->_sock._fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, + (const char *)&mreq, sizeof(mreq)) < 0)) { + ret = _Z_ERR_GENERIC; + } + } else { + ret = _Z_ERR_GENERIC; + } + + if (ret != _Z_RES_OK) { + closesocket(sock->_sock._fd); + WSACleanup(); + } + } else { + ret = _Z_ERR_GENERIC; + } + + z_free(lsockaddr); + } else { + ret = _Z_ERR_GENERIC; + } + + return ret; +} + +void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t *socksend, + const _z_sys_net_endpoint_t rep) { + if (rep._ep._iptcp->ai_family == AF_INET) { + struct ip_mreq mreq; + (void)memset(&mreq, 0, sizeof(mreq)); + mreq.imr_multiaddr.s_addr = ((SOCKADDR_IN *)rep._ep._iptcp->ai_addr)->sin_addr.s_addr; + mreq.imr_interface.s_addr = htonl(INADDR_ANY); + setsockopt(sockrecv->_sock._fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const char *)&mreq, sizeof(mreq)); + } else if (rep._ep._iptcp->ai_family == AF_INET6) { + struct ipv6_mreq mreq; + (void)memset(&mreq, 0, sizeof(mreq)); + (void)memcpy(&mreq.ipv6mr_multiaddr, &((SOCKADDR_IN6 *)rep._ep._iptcp->ai_addr)->sin6_addr, + sizeof(struct in6_addr)); + // mreq.ipv6mr_interface = ifindex; + setsockopt(sockrecv->_sock._fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, (const char *)&mreq, sizeof(mreq)); + } else { + // Do nothing. It must never not enter here. + // Required to be compliant with MISRA 15.7 rule + } + + closesocket(sockrecv->_sock._fd); + WSACleanup(); + closesocket(socksend->_sock._fd); + WSACleanup(); +} + +size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, const _z_sys_net_endpoint_t lep, + _z_bytes_t *addr) { + SOCKADDR_STORAGE raddr; + unsigned int replen = sizeof(SOCKADDR_STORAGE); + + size_t rb = 0; + do { + rb = recvfrom(sock._sock._fd, (char *)ptr, (int)len, 0, (SOCKADDR *)&raddr, (int *)&replen); + if (rb < (size_t)0) { + rb = SIZE_MAX; + break; + } + + if (lep._ep._iptcp->ai_family == AF_INET) { + SOCKADDR_IN *a = ((SOCKADDR_IN *)lep._ep._iptcp->ai_addr); + SOCKADDR_IN *b = ((SOCKADDR_IN *)&raddr); + if (!((a->sin_port == b->sin_port) && (a->sin_addr.s_addr == b->sin_addr.s_addr))) { + // If addr is not NULL, it means that the rep was requested by the upper-layers + if (addr != NULL) { + *addr = _z_bytes_make(sizeof(IN_ADDR) + sizeof(USHORT)); + (void)memcpy((uint8_t *)addr->start, &b->sin_addr.s_addr, sizeof(IN_ADDR)); + (void)memcpy((uint8_t *)(addr->start + sizeof(IN_ADDR)), &b->sin_port, sizeof(USHORT)); + } + break; + } + } else if (lep._ep._iptcp->ai_family == AF_INET6) { + SOCKADDR_IN6 *a = ((SOCKADDR_IN6 *)lep._ep._iptcp->ai_addr); + SOCKADDR_IN6 *b = ((SOCKADDR_IN6 *)&raddr); + if (!((a->sin6_port == b->sin6_port) && + (memcmp(a->sin6_addr.s6_addr, b->sin6_addr.s6_addr, sizeof(struct in6_addr)) == 0))) { + // If addr is not NULL, it means that the rep was requested by the upper-layers + if (addr != NULL) { + *addr = _z_bytes_make(sizeof(struct in6_addr) + sizeof(USHORT)); + (void)memcpy((uint8_t *)addr->start, &b->sin6_addr.s6_addr, sizeof(struct in6_addr)); + (void)memcpy((uint8_t *)(addr->start + sizeof(struct in6_addr)), &b->sin6_port, sizeof(USHORT)); + } + break; + } + } else { + continue; // FIXME: support error report on invalid packet to the upper layer + } + } while (1); + + return rb; +} + +size_t _z_read_exact_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len, + const _z_sys_net_endpoint_t lep, _z_bytes_t *addr) { + size_t n = 0; + uint8_t *pos = &ptr[0]; + + do { + size_t rb = _z_read_udp_multicast(sock, pos, len - n, lep, addr); + if (rb == SIZE_MAX) { + n = rb; + break; + } + + n = n + rb; + pos = _z_ptr_u8_offset(pos, n); + } while (n != len); + + return n; +} + +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._sock._fd, (const char *)ptr, (int)len, 0, rep._ep._iptcp->ai_addr, + (int)rep._ep._iptcp->ai_addrlen); +} +#endif + +#if Z_LINK_BLUETOOTH == 1 +#error "Bluetooth not supported yet on Windows port of Zenoh-Pico" +#endif + +#if Z_LINK_SERIAL == 1 +#error "Serial not supported yet on Windows port of Zenoh-Pico" +#endif diff --git a/src/system/windows/system.c b/src/system/windows/system.c new file mode 100644 index 000000000..519a9ba01 --- /dev/null +++ b/src/system/windows/system.c @@ -0,0 +1,241 @@ +// +// 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, +// + +#include +// The following includes must come after winsock2 +#include +#include + +#include "zenoh-pico/config.h" +#include "zenoh-pico/system/platform.h" +#include "zenoh-pico/utils/result.h" + +/*------------------ Random ------------------*/ +uint8_t z_random_u8(void) { + uint8_t ret = 0; + z_random_fill(&ret, sizeof(ret)); + return ret; +} + +uint16_t z_random_u16(void) { + uint16_t ret = 0; + z_random_fill(&ret, sizeof(ret)); + return ret; +} + +uint32_t z_random_u32(void) { + uint32_t ret = 0; + z_random_fill(&ret, sizeof(ret)); + return ret; +} + +uint64_t z_random_u64(void) { + uint64_t ret = 0; + z_random_fill(&ret, sizeof(ret)); + return ret; +} + +void z_random_fill(void *buf, size_t len) { + HCRYPTPROV crypt_ctx; + if (CryptAcquireContext(&crypt_ctx, NULL, (LPCSTR)L"Microsoft Base Cryptographic Provider v1.0", PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT)) { + for (size_t i = 0; i < len; i++) { + CryptGenRandom(crypt_ctx, 1, &((uint8_t *)buf)[i]); + } + CryptReleaseContext(crypt_ctx, 0); + } +} + +/*------------------ Memory ------------------*/ +// #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) +// #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) +void *z_malloc(size_t size) { return malloc(size); } + +void *z_realloc(void *ptr, size_t size) { return realloc(ptr, size); } + +void z_free(void *ptr) { free(ptr); } + +#if Z_MULTI_THREAD == 1 +/*------------------ Task ------------------*/ +int8_t _z_task_init(_z_task_t *task, _z_task_attr_t *attr, void *(*fun)(void *), void *arg) { + (void)(attr); + int8_t ret = _Z_RES_OK; + *task = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)fun, arg, 0, NULL); + if (*task == NULL) { + ret = _Z_ERR_SYSTEM_TASK_FAILED; + } + return ret; +} + +int8_t _z_task_join(_z_task_t *task) { + int8_t ret = _Z_RES_OK; + WaitForSingleObject(*task, INFINITE); + return ret; +} + +int8_t _z_task_cancel(_z_task_t *task) { + int8_t ret = _Z_RES_OK; + TerminateThread(*task, 0); + return ret; +} + +void _z_task_free(_z_task_t **task) { + _z_task_t *ptr = *task; + CloseHandle(*ptr); + z_free(ptr); + *task = NULL; +} + +/*------------------ Mutex ------------------*/ +int8_t _z_mutex_init(_z_mutex_t *m) { + int8_t ret = _Z_RES_OK; + InitializeSRWLock(m); + return ret; +} + +int8_t _z_mutex_free(_z_mutex_t *m) { + (void)(m); + int8_t ret = _Z_RES_OK; + return ret; +} + +int8_t _z_mutex_lock(_z_mutex_t *m) { + int8_t ret = _Z_RES_OK; + AcquireSRWLockExclusive(m); + return ret; +} + +int8_t _z_mutex_trylock(_z_mutex_t *m) { + int8_t ret = _Z_RES_OK; + if (TryAcquireSRWLockExclusive(m) == 0) { + ret = _Z_ERR_GENERIC; + } + return ret; +} + +int8_t _z_mutex_unlock(_z_mutex_t *m) { + int8_t ret = _Z_RES_OK; + ReleaseSRWLockExclusive(m); + return ret; +} + +/*------------------ Condvar ------------------*/ +int8_t _z_condvar_init(_z_condvar_t *cv) { + int8_t ret = _Z_RES_OK; + InitializeConditionVariable(cv); + return ret; +} + +int8_t _z_condvar_free(_z_condvar_t *cv) { + (void)(cv); + int8_t ret = _Z_RES_OK; + return ret; +} + +int8_t _z_condvar_signal(_z_condvar_t *cv) { + int8_t ret = _Z_RES_OK; + WakeConditionVariable(cv); + return ret; +} + +int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { + int8_t ret = _Z_RES_OK; + SleepConditionVariableSRW(cv, m, INFINITE, 0); + return ret; +} +#endif // Z_MULTI_THREAD == 1 + +/*------------------ Sleep ------------------*/ +int z_sleep_us(size_t time) { return z_sleep_ms((time / 1000) + (time % 1000 == 0 ? 0 : 1)); } + +int z_sleep_ms(size_t time) { + // Guarantees that size_t is split into DWORD segments for Sleep + uint8_t ratio = sizeof(size_t) / sizeof(DWORD); + DWORD ratio_time = (DWORD)((time / ratio) + (time % ratio == 0 ? 0 : 1)); + for (uint8_t i = 0; i < ratio; i++) { + Sleep(ratio_time); + } + return 0; +} + +int z_sleep_s(size_t time) { + z_time_t start = z_time_now(); + + // Most sleep APIs promise to sleep at least whatever you asked them to. + // This may compound, so this approach may make sleeps longer than expected. + // This extra check tries to minimize the amount of extra time it might sleep. + while (z_time_elapsed_s(&start) < time) { + z_sleep_ms(1000); + } + + return 0; +} + +/*------------------ Instant ------------------*/ +z_clock_t z_clock_now(void) { + z_clock_t now; + ftime(&now); + return now; +} + +unsigned long z_clock_elapsed_us(z_clock_t *instant) { return z_clock_elapsed_ms(instant) * 1000; } + +unsigned long z_clock_elapsed_ms(z_clock_t *instant) { + z_time_t now; + ftime(&now); + + unsigned long elapsed = ((unsigned long)(now.time - instant->time) * 1000) + (now.millitm - instant->millitm); + return elapsed; +} + +unsigned long z_clock_elapsed_s(z_clock_t *instant) { + z_time_t now; + ftime(&now); + + unsigned long elapsed = (unsigned long)(now.time - instant->time); + return elapsed; +} + +/*------------------ Time ------------------*/ +z_time_t z_time_now(void) { + z_time_t now; + ftime(&now); + return now; +} + +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); + struct tm ts; + ts = *localtime(&tv.time); + strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); + return buf; +} + +unsigned long z_time_elapsed_us(z_time_t *time) { return z_time_elapsed_ms(time) * 1000; } + +unsigned long z_time_elapsed_ms(z_time_t *time) { + z_time_t now; + ftime(&now); + + unsigned long elapsed = ((unsigned long)(now.time - time->time) * 1000) + (now.millitm - time->millitm); + return elapsed; +} + +unsigned long z_time_elapsed_s(z_time_t *time) { + z_time_t now; + ftime(&now); + + unsigned long elapsed = (unsigned long)(now.time - time->time); + return elapsed; +} diff --git a/src/system/zephyr/network.c b/src/system/zephyr/network.c index 3470b2cee..4c8f7574e 100644 --- a/src/system/zephyr/network.c +++ b/src/system/zephyr/network.c @@ -29,7 +29,7 @@ #if Z_LINK_TCP == 1 /*------------------ TCP sockets ------------------*/ -int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port) { +int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { int8_t ret = _Z_RES_OK; struct addrinfo hints; @@ -39,7 +39,7 @@ int8_t _z_create_endpoint_tcp(_z_sys_net_endpoint_t *ep, const char *s_addr, con hints.ai_flags = 0; hints.ai_protocol = IPPROTO_TCP; - if (getaddrinfo(s_addr, s_port, &hints, &ep->_iptcp) < 0) { + if (getaddrinfo(s_address, s_port, &hints, &ep->_iptcp) < 0) { ret = _Z_ERR_GENERIC; } @@ -110,7 +110,7 @@ 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 < 0) { + if (rb < (ssize_t)0) { rb = SIZE_MAX; } @@ -142,7 +142,7 @@ size_t _z_send_tcp(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t le #if Z_LINK_UDP_UNICAST == 1 || Z_LINK_UDP_MULTICAST == 1 /*------------------ UDP sockets ------------------*/ -int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_addr, const char *s_port) { +int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_address, const char *s_port) { int8_t ret = _Z_RES_OK; struct addrinfo hints; @@ -152,7 +152,7 @@ int8_t _z_create_endpoint_udp(_z_sys_net_endpoint_t *ep, const char *s_addr, con hints.ai_flags = 0; hints.ai_protocol = IPPROTO_UDP; - if (getaddrinfo(s_addr, s_port, &hints, &ep->_iptcp) < 0) { + if (getaddrinfo(s_address, s_port, &hints, &ep->_iptcp) < 0) { ret = _Z_ERR_GENERIC; } @@ -205,7 +205,7 @@ size_t _z_read_udp_unicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t unsigned int addrlen = sizeof(struct sockaddr_storage); ssize_t rb = recvfrom(sock._fd, ptr, len, 0, (struct sockaddr *)&raddr, &addrlen); - if (rb < 0) { + if (rb < (ssize_t)0) { rb = SIZE_MAX; } @@ -466,7 +466,7 @@ size_t _z_read_udp_multicast(const _z_sys_net_socket_t sock, uint8_t *ptr, size_ ssize_t rb = 0; do { rb = recvfrom(sock._fd, ptr, len, 0, (struct sockaddr *)&raddr, &raddrlen); - if (rb < 0) { + if (rb < (ssize_t)0) { rb = SIZE_MAX; break; } diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index 31f65b846..d34dc2581 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -12,11 +12,11 @@ // ZettaScale Zenoh Team, // +#include #include #include #include #include -#include #include "zenoh-pico/config.h" #include "zenoh-pico/system/platform.h" @@ -54,11 +54,11 @@ void z_free(void *ptr) { k_free(ptr); } #define Z_THREADS_NUM 4 #ifdef CONFIG_TEST_EXTRA_STACK_SIZE - #define Z_PTHREAD_STACK_SIZE_DEFAULT CONFIG_MAIN_STACK_SIZE + CONFIG_TEST_EXTRA_STACK_SIZE +#define Z_PTHREAD_STACK_SIZE_DEFAULT CONFIG_MAIN_STACK_SIZE + CONFIG_TEST_EXTRA_STACK_SIZE #elif CONFIG_TEST_EXTRA_STACKSIZE - #define Z_PTHREAD_STACK_SIZE_DEFAULT CONFIG_MAIN_STACK_SIZE + CONFIG_TEST_EXTRA_STACKSIZE +#define Z_PTHREAD_STACK_SIZE_DEFAULT CONFIG_MAIN_STACK_SIZE + CONFIG_TEST_EXTRA_STACKSIZE #else - #define Z_PTHREAD_STACK_SIZE_DEFAULT CONFIG_MAIN_STACK_SIZE +#define Z_PTHREAD_STACK_SIZE_DEFAULT CONFIG_MAIN_STACK_SIZE #endif K_THREAD_STACK_ARRAY_DEFINE(thread_stack_area, Z_THREADS_NUM, Z_PTHREAD_STACK_SIZE_DEFAULT); @@ -109,7 +109,7 @@ int8_t _z_condvar_wait(_z_condvar_t *cv, _z_mutex_t *m) { return pthread_cond_wa #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int z_sleep_us(unsigned int time) { +int z_sleep_us(size_t time) { int32_t rem = time; while (rem > 0) { rem = k_usleep(rem); // This function is unlikely to work as expected without kernel tuning. @@ -123,7 +123,7 @@ int z_sleep_us(unsigned int time) { return 0; } -int z_sleep_ms(unsigned int time) { +int z_sleep_ms(size_t time) { int32_t rem = time; while (rem > 0) { rem = k_msleep(rem); @@ -132,7 +132,7 @@ int z_sleep_ms(unsigned int time) { return 0; } -int z_sleep_s(unsigned int time) { +int z_sleep_s(size_t time) { int32_t rem = time; while (rem > 0) { rem = k_sleep(K_SECONDS(rem)); diff --git a/src/transport/common/rx.c b/src/transport/common/rx.c index 99a8c1ab3..c617efe86 100644 --- a/src/transport/common/rx.c +++ b/src/transport/common/rx.c @@ -20,7 +20,7 @@ /*------------------ Reception helper ------------------*/ int8_t _z_link_recv_t_msg(_z_transport_message_t *t_msg, const _z_link_t *zl) { - uint8_t ret = _Z_RES_OK; + int8_t ret = _Z_RES_OK; // Create and prepare the buffer _z_zbuf_t zbf = _z_zbuf_make(Z_BATCH_SIZE_RX); diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index fadaa7021..987a3ac2b 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -44,7 +44,7 @@ _z_transport_peer_entry_t *_z_find_peer_entry(_z_transport_peer_entry_list_t *l, /*------------------ Reception helper ------------------*/ int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_message_t *t_msg, _z_bytes_t *addr) { _Z_DEBUG(">> recv session msg\n"); - uint8_t ret = _Z_RES_OK; + int8_t ret = _Z_RES_OK; #if Z_MULTI_THREAD == 1 // Acquire the lock diff --git a/src/transport/multicast/link/task/lease.c b/src/transport/multicast/link/task/lease.c index 5c3a4fef0..2bec9330f 100644 --- a/src/transport/multicast/link/task/lease.c +++ b/src/transport/multicast/link/task/lease.c @@ -78,7 +78,7 @@ void *_zp_multicast_lease_task(void *ztm_arg) { // 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 = next_lease / Z_TRANSPORT_LEASE_EXPIRE_FACTOR; + _z_zint_t next_keep_alive = (_z_zint_t)(next_lease / Z_TRANSPORT_LEASE_EXPIRE_FACTOR); _z_zint_t next_join = Z_JOIN_INTERVAL; _z_transport_peer_entry_list_t *it = NULL; @@ -121,7 +121,8 @@ void *_zp_multicast_lease_task(void *ztm_arg) { // Reset the keep alive parameters ztm->_transmitted = false; - next_keep_alive = _z_get_minimum_lease(ztm->_peers, ztm->_lease) / Z_TRANSPORT_LEASE_EXPIRE_FACTOR; + next_keep_alive = + (_z_zint_t)(_z_get_minimum_lease(ztm->_peers, ztm->_lease) / Z_TRANSPORT_LEASE_EXPIRE_FACTOR); } // Compute the target interval to sleep diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index 83073b2b7..d8cd12a9e 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -25,7 +25,7 @@ int8_t _z_unicast_recv_t_msg_na(_z_transport_unicast_t *ztu, _z_transport_message_t *t_msg) { _Z_DEBUG(">> recv session msg\n"); - uint8_t ret = _Z_RES_OK; + int8_t ret = _Z_RES_OK; #if Z_MULTI_THREAD == 1 // Acquire the lock diff --git a/src/transport/unicast/link/task/lease.c b/src/transport/unicast/link/task/lease.c index d0c07cc3b..933d9aa52 100644 --- a/src/transport/unicast/link/task/lease.c +++ b/src/transport/unicast/link/task/lease.c @@ -41,7 +41,7 @@ void *_zp_unicast_lease_task(void *ztu_arg) { ztu->_transmitted = false; _z_zint_t next_lease = ztu->_lease; - _z_zint_t next_keep_alive = ztu->_lease / Z_TRANSPORT_LEASE_EXPIRE_FACTOR; + _z_zint_t next_keep_alive = (_z_zint_t)(ztu->_lease / Z_TRANSPORT_LEASE_EXPIRE_FACTOR); while (ztu->_lease_task_running == true) { if (next_lease == 0) { // Check if received data @@ -68,7 +68,7 @@ void *_zp_unicast_lease_task(void *ztu_arg) { // Reset the keep alive parameters ztu->_transmitted = false; - next_keep_alive = ztu->_lease / Z_TRANSPORT_LEASE_EXPIRE_FACTOR; + next_keep_alive = (_z_zint_t)(ztu->_lease / Z_TRANSPORT_LEASE_EXPIRE_FACTOR); } // Compute the target interval diff --git a/src/utils/checksum.c b/src/utils/checksum.c index 8ca67d2a3..c1054045c 100644 --- a/src/utils/checksum.c +++ b/src/utils/checksum.c @@ -15,11 +15,11 @@ #include "zenoh-pico/utils/checksum.h" uint32_t _z_crc32(const uint8_t *message, size_t len) { - uint32_t crc = 0xFFFFFFFF; + int32_t crc = 0xFFFFFFFF; for (size_t i = 0; i < len; i++) { - crc = crc ^ (uint32_t)message[i]; + crc = crc ^ (int32_t)message[i]; for (int j = 0; j < 8; j++) { - crc = (crc >> 1) ^ ((uint32_t)0x04C11DB7 & (-(crc & (uint32_t)1))); + crc = (crc >> 1) ^ ((int32_t)0x04C11DB7 & (-(crc & (int32_t)1))); } } return ~crc; diff --git a/src/utils/string.c b/src/utils/string.c index ab74bb63d..21436681a 100644 --- a/src/utils/string.c +++ b/src/utils/string.c @@ -36,7 +36,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, -_z_ptr_char_diff(needle.end, needle.start)); + haystack.end = _z_cptr_char_offset(haystack.end, -1 * _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)) { @@ -103,7 +103,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, -strlen(str->delimiter)); + str->s.end = _z_cptr_char_offset(result.start, -1 * strlen(str->delimiter)); } } return result; diff --git a/tests/z_iobuf_test.c b/tests/z_iobuf_test.c index 55d5c56a5..f7512e5db 100644 --- a/tests/z_iobuf_test.c +++ b/tests/z_iobuf_test.c @@ -512,7 +512,7 @@ void wbuf_add_iosli(void) { printf("\n>>> WBuf => Add IOSli\n"); print_wbuf_overview(&wbf); - size_t written = 0; + uint8_t written = 0; uint8_t counter = 0; while (written < 255) { uint8_t remaining = 255 - written; diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index c821dd119..0ec8280c5 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -110,6 +110,18 @@ _Bool gen_bool(void) { return z_random_u8() % 2; } uint8_t gen_uint8(void) { return z_random_u8() % 255; } +uint64_t gen_uint64(void) { + uint64_t ret = 0; + z_random_fill(&ret, sizeof(ret)); + return ret; +} + +unsigned int gen_uint(void) { + unsigned int ret = 0; + z_random_fill(&ret, sizeof(ret)); + return ret; +} + _z_zint_t gen_zint(void) { _z_zint_t ret = 0; z_random_fill(&ret, sizeof(ret)); @@ -150,8 +162,6 @@ _z_bytes_t gen_bytes(size_t len) { return arr; } -uint64_t gen_time(void) { return (uint64_t)time(NULL); } - char *gen_str(size_t size) { char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; char *str = (char *)z_malloc(size + 1); @@ -320,7 +330,7 @@ void payload_field(void) { /*------------------ Timestamp field ------------------*/ _z_timestamp_t gen_timestamp(void) { _z_timestamp_t ts; - ts._time = (uint64_t)time(NULL); + ts._time = gen_uint64(); ts._id = gen_bytes(16); return ts; @@ -373,9 +383,9 @@ _z_subinfo_t gen_subinfo(void) { sm.mode = gen_bool() ? Z_SUBMODE_PUSH : Z_SUBMODE_PULL; sm.reliability = gen_bool() ? Z_RELIABILITY_RELIABLE : Z_RELIABILITY_BEST_EFFORT; if (gen_bool()) { - sm.period.origin = gen_zint(); - sm.period.period = gen_zint(); - sm.period.duration = gen_zint(); + sm.period.origin = gen_uint(); + sm.period.period = gen_uint(); + sm.period.duration = gen_uint(); } else { sm.period.origin = 0; sm.period.period = 0; From 41ba7488b07ec37e1f5dff0cdb6745c48f3347ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 10 Feb 2023 11:55:37 +0100 Subject: [PATCH 010/181] Skeleton for message extensions --- CMakeLists.txt | 3 + include/zenoh-pico/protocol/ext.h | 94 ++++++ include/zenoh-pico/protocol/extcodec.h | 44 +++ src/protocol/ext.c | 109 +++++++ src/protocol/extcodec.c | 123 ++++++++ tests/z_extcodec_test.c | 384 +++++++++++++++++++++++++ 6 files changed, 757 insertions(+) create mode 100644 include/zenoh-pico/protocol/ext.h create mode 100644 include/zenoh-pico/protocol/extcodec.h create mode 100644 src/protocol/ext.c create mode 100644 src/protocol/extcodec.c create mode 100644 tests/z_extcodec_test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 780c3f129..330394c92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -228,12 +228,14 @@ if(BUILD_TESTING) add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c) add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c) add_executable(z_msgcodec_test ${PROJECT_SOURCE_DIR}/tests/z_msgcodec_test.c) + add_executable(z_extcodec_test ${PROJECT_SOURCE_DIR}/tests/z_extcodec_test.c) add_executable(z_keyexpr_test ${PROJECT_SOURCE_DIR}/tests/z_keyexpr_test.c) target_link_libraries(z_data_struct_test ${Libname}) target_link_libraries(z_endpoint_test ${Libname}) target_link_libraries(z_iobuf_test ${Libname}) target_link_libraries(z_msgcodec_test ${Libname}) + target_link_libraries(z_extcodec_test ${Libname}) target_link_libraries(z_keyexpr_test ${Libname}) enable_testing() @@ -241,6 +243,7 @@ if(BUILD_TESTING) add_test(z_endpoint_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_endpoint_test) add_test(z_iobuf_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iobuf_test) add_test(z_msgcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_msgcodec_test) + add_test(z_extcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_extcodec_test) add_test(z_keyexpr_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_keyexpr_test) endif() diff --git a/include/zenoh-pico/protocol/ext.h b/include/zenoh-pico/protocol/ext.h new file mode 100644 index 000000000..25cf7de5b --- /dev/null +++ b/include/zenoh-pico/protocol/ext.h @@ -0,0 +1,94 @@ +// +// 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_PROTOCOL_EXTENSION_H +#define ZENOH_PICO_PROTOCOL_EXTENSION_H + +#include + +#include "zenoh-pico/protocol/core.h" + +/*=============================*/ +/* Message header */ +/*=============================*/ +#define _Z_EXT_ID_MASK 0x1F +#define _Z_EXT_ENC_MASK 0x60 + +/*=============================*/ +/* Message helpers */ +/*=============================*/ +#define _Z_EXT_ID(h) (_Z_EXT_ID_MASK & h) +#define _Z_EXT_ENC(h) (_Z_EXT_ENC_MASK & h) +#define _Z_EXT_HAS_FLAG(h, f) ((h & f) != 0) +#define _Z_EXT_SET_FLAG(h, f) (h |= f) + +/*=============================*/ +/* Extension IDs */ +/*=============================*/ +#define _Z_MSG_EXT_ID_EXT1 0x01 + +/*=============================*/ +/* Extension Encodings */ +/*=============================*/ +#define _Z_MSG_EXT_ENC_UNIT 0x00 // 0x00 << 5 +#define _Z_MSG_EXT_ENC_ZINT 0x20 // 0x01 << 5 +#define _Z_MSG_EXT_ENC_ZBUF 0x40 // 0x10 << 5 + +/*=============================*/ +/* Extension flags */ +/*=============================*/ +#define _Z_MSG_EXT_FLAG_Z 0x80 + +typedef struct { + uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior +} _z_msg_ext_unit_t; +void _z_msg_ext_clear_unit(_z_msg_ext_unit_t *ext); + +/*------------------ ZID Extension ------------------*/ +typedef struct { + _z_zint_t _val; +} _z_msg_ext_zint_t; +void _z_msg_ext_clear_zint(_z_msg_ext_zint_t *ext); + +/*------------------ Unknown Extension ------------------*/ +typedef struct { + _z_bytes_t _val; +} _z_msg_ext_zbuf_t; +void _z_msg_ext_clear_zbuf(_z_msg_ext_zbuf_t *ext); + +/*------------------ Message Extensions ------------------*/ +typedef union { + _z_msg_ext_unit_t _unit; + _z_msg_ext_zint_t _zint; + _z_msg_ext_zbuf_t _zbuf; +} _z_msg_ext_body_t; + +typedef struct { + _z_msg_ext_body_t _body; + uint8_t _header; +} _z_msg_ext_t; +void _z_msg_ext_clear(_z_msg_ext_t *ext); + +/*------------------ Builders ------------------*/ +_z_msg_ext_t _z_msg_ext_make_unit(uint8_t id); +_z_msg_ext_t _z_msg_ext_make_zint(uint8_t id, _z_zint_t zid); +_z_msg_ext_t _z_msg_ext_make_zbuf(uint8_t id, _z_bytes_t zbuf); + +/*------------------ Copy ------------------*/ +void _z_msg_ext_copy(_z_msg_ext_t *clone, _z_msg_ext_t *ext); +void _z_msg_ext_copy_unit(_z_msg_ext_unit_t *clone, _z_msg_ext_unit_t *ext); +void _z_msg_ext_copy_zint(_z_msg_ext_zint_t *clone, _z_msg_ext_zint_t *ext); +void _z_msg_ext_copy_zbuf(_z_msg_ext_zbuf_t *clone, _z_msg_ext_zbuf_t *ext); + +#endif /* ZENOH_PICO_PROTOCOL_EXTENSION_H */ diff --git a/include/zenoh-pico/protocol/extcodec.h b/include/zenoh-pico/protocol/extcodec.h new file mode 100644 index 000000000..05277523b --- /dev/null +++ b/include/zenoh-pico/protocol/extcodec.h @@ -0,0 +1,44 @@ +// +// 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_MSGEXTCODEC_H +#define ZENOH_PICO_MSGEXTCODEC_H + +#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/ext.h" +#include "zenoh-pico/protocol/iobuf.h" + +/*------------------ Message Extension ------------------*/ +int8_t _z_msg_ext_encode(_z_wbuf_t *wbf, const _z_msg_ext_t *ext); +int8_t _z_msg_ext_decode(_z_msg_ext_t *ext, _z_zbuf_t *zbf); +int8_t _z_msg_ext_decode_na(_z_msg_ext_t *ext, _z_zbuf_t *zbf); + +#endif /* ZENOH_PICO_MSGEXTCODEC_H */ + +// NOTE: the following headers are for unit testing only +#ifdef ZENOH_PICO_TEST_H +// ------------------ Message Fields ------------------ +int8_t _z_msg_ext_encode_unit(_z_wbuf_t *wbf, const _z_msg_ext_unit_t *pld); +int8_t _z_msg_ext_decode_unit(_z_msg_ext_unit_t *pld, _z_zbuf_t *zbf); +int8_t _z_msg_ext_decode_unit_na(_z_msg_ext_unit_t *pld, _z_zbuf_t *zbf); + +int8_t _z_msg_ext_encode_zint(_z_wbuf_t *wbf, const _z_msg_ext_zint_t *pld); +int8_t _z_msg_ext_decode_zint(_z_msg_ext_zint_t *pld, _z_zbuf_t *zbf); +int8_t _z_msg_ext_decode_zint_na(_z_msg_ext_zint_t *pld, _z_zbuf_t *zbf); + +int8_t _z_msg_ext_encode_zbuf(_z_wbuf_t *wbf, const _z_msg_ext_zbuf_t *pld); +int8_t _z_msg_ext_decode_zbuf(_z_msg_ext_zbuf_t *pld, _z_zbuf_t *zbf); +int8_t _z_msg_ext_decode_zbuf_na(_z_msg_ext_zbuf_t *pld, _z_zbuf_t *zbf); + +#endif /* ZENOH_PICO_TEST_H */ diff --git a/src/protocol/ext.c b/src/protocol/ext.c new file mode 100644 index 000000000..d864cf0ef --- /dev/null +++ b/src/protocol/ext.c @@ -0,0 +1,109 @@ +// +// 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, +// + +#include "zenoh-pico/protocol/ext.h" + +#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/utils/logging.h" + +_z_msg_ext_t _z_msg_ext_make_unit(uint8_t id) { + _z_msg_ext_t ext; + + ext._header = 0; + ext._header |= (id & 0x1F); + ext._header |= (_Z_MSG_EXT_ENC_UNIT & 0x03) << 5; + + return ext; +} + +void _z_msg_ext_clear_unit(_z_msg_ext_unit_t *ext) { (void)(ext); } + +void _z_msg_ext_copy_unit(_z_msg_ext_unit_t *clone, _z_msg_ext_unit_t *ext) { + (void)(clone); + (void)(ext); +} + +_z_msg_ext_t _z_msg_ext_make_zint(uint8_t id, _z_zint_t zid) { + _z_msg_ext_t ext; + + ext._header = 0; + ext._header |= (id & 0x1F); + ext._header |= (_Z_MSG_EXT_ENC_ZINT & 0x03) << 5; + + ext._body._zint._val = zid; + + return ext; +} + +void _z_msg_ext_clear_zint(_z_msg_ext_zint_t *ext) { (void)(ext); } + +void _z_msg_ext_copy_zint(_z_msg_ext_zint_t *clone, _z_msg_ext_zint_t *ext) { clone->_val = ext->_val; } + +_z_msg_ext_t _z_msg_ext_make_zbuf(uint8_t id, const _z_bytes_t zbuf) { + _z_msg_ext_t ext; + + ext._header = 0; + ext._header |= (id & 0x1F); + ext._header |= (_Z_MSG_EXT_ENC_ZBUF & 0x03) << 5; + + ext._body._zbuf._val = _z_bytes_wrap(zbuf.start, zbuf.len); + + return ext; +} + +void _z_msg_ext_clear_zbuf(_z_msg_ext_zbuf_t *ext) { _z_bytes_clear(&ext->_val); } + +void _z_msg_ext_copy_zbuf(_z_msg_ext_zbuf_t *clone, _z_msg_ext_zbuf_t *ext) { _z_bytes_copy(&clone->_val, &ext->_val); } + +void _z_msg_ext_copy(_z_msg_ext_t *clone, _z_msg_ext_t *ext) { + clone->_header = ext->_header; + + switch (_Z_EXT_ENC(clone->_header)) { + case _Z_MSG_EXT_ENC_UNIT: { + _z_msg_ext_copy_unit(&clone->_body._unit, &ext->_body._unit); + } break; + + case _Z_MSG_EXT_ENC_ZINT: { + _z_msg_ext_copy_zint(&clone->_body._zint, &ext->_body._zint); + } break; + + case _Z_MSG_EXT_ENC_ZBUF: { + _z_msg_ext_copy_zbuf(&clone->_body._zbuf, &ext->_body._zbuf); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to copy message extension with unknown ID(%d)\n", mid); + } break; + } +} + +void _z_msg_ext_clear(_z_msg_ext_t *ext) { + switch (_Z_EXT_ENC(ext->_header)) { + case _Z_MSG_EXT_ENC_UNIT: { + _z_msg_ext_clear_unit(&ext->_body._unit); + } break; + + case _Z_MSG_EXT_ENC_ZINT: { + _z_msg_ext_clear_zint(&ext->_body._zint); + } break; + + case _Z_MSG_EXT_ENC_ZBUF: { + _z_msg_ext_clear_zbuf(&ext->_body._zbuf); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to free message extension with unknown ID(%d)\n", mid); + } break; + } +} diff --git a/src/protocol/extcodec.c b/src/protocol/extcodec.c new file mode 100644 index 000000000..dd3c4d232 --- /dev/null +++ b/src/protocol/extcodec.c @@ -0,0 +1,123 @@ +// +// 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, +// + +#include "zenoh-pico/protocol/extcodec.h" + +#include "zenoh-pico/protocol/codec.h" +#include "zenoh-pico/protocol/ext.h" +#include "zenoh-pico/utils/logging.h" +#include "zenoh-pico/utils/result.h" + +int8_t _z_msg_ext_encode_unit(_z_wbuf_t *wbf, const _z_msg_ext_unit_t *ext) { + int8_t ret = _Z_RES_OK; + (void)(wbf); + (void)(ext); + return ret; +} + +int8_t _z_msg_ext_decode_unit(_z_msg_ext_unit_t *ext, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + (void)(zbf); + (void)(ext); + return ret; +} + +int8_t _z_msg_ext_decode_unit_na(_z_msg_ext_unit_t *ext, _z_zbuf_t *zbf) { return _z_msg_ext_decode_unit(ext, zbf); } + +int8_t _z_msg_ext_encode_zint(_z_wbuf_t *wbf, const _z_msg_ext_zint_t *ext) { + int8_t ret = _Z_RES_OK; + _Z_EC(_z_zint_encode(wbf, ext->_val)) + return ret; +} + +int8_t _z_msg_ext_decode_zint(_z_msg_ext_zint_t *ext, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + ret |= _z_zint_decode(&ext->_val, zbf); + return ret; +} + +int8_t _z_msg_ext_decode_zint_na(_z_msg_ext_zint_t *ext, _z_zbuf_t *zbf) { return _z_msg_ext_decode_zint(ext, zbf); } + +int8_t _z_msg_ext_encode_zbuf(_z_wbuf_t *wbf, const _z_msg_ext_zbuf_t *ext) { + int8_t ret = _Z_RES_OK; + _Z_EC(_z_bytes_encode(wbf, &ext->_val)) + return ret; +} + +int8_t _z_msg_ext_decode_zbuf(_z_msg_ext_zbuf_t *ext, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + ret |= _z_bytes_decode(&ext->_val, zbf); + return ret; +} + +int8_t _z_msg_ext_decode_zbuf_na(_z_msg_ext_zbuf_t *ext, _z_zbuf_t *zbf) { return _z_msg_ext_decode_zbuf(ext, zbf); } + +/*------------------ Message Extension ------------------*/ +int8_t _z_msg_ext_encode(_z_wbuf_t *wbf, const _z_msg_ext_t *ext) { + int8_t ret = _Z_RES_OK; + + // if (is_last == false) { + // _Z_EXT_SET_FLAG(ext->_header, _Z_MSG_EXT_FLAG_Z); + // } + _Z_EC(_z_wbuf_write(wbf, ext->_header)) + + switch (_Z_EXT_ENC(ext->_header)) { + case _Z_MSG_EXT_ENC_UNIT: { + _z_msg_ext_encode_unit(wbf, &ext->_body._unit); + } break; + + case _Z_MSG_EXT_ENC_ZINT: { + _z_msg_ext_encode_zint(wbf, &ext->_body._zint); + } break; + + case _Z_MSG_EXT_ENC_ZBUF: { + _z_msg_ext_encode_zbuf(wbf, &ext->_body._zbuf); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to copy message extension with unknown ID(%d)\n", mid); + } break; + } + + return ret; +} + +int8_t _z_msg_ext_decode(_z_msg_ext_t *ext, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + + ret |= _z_uint8_decode(&ext->_header, zbf); // Decode the header + if (ret == _Z_RES_OK) { + switch (_Z_EXT_ENC(ext->_header)) { + case _Z_MSG_EXT_ENC_UNIT: { + ret |= _z_msg_ext_decode_unit(&ext->_body._unit, zbf); + } break; + + case _Z_MSG_EXT_ENC_ZINT: { + ret |= _z_msg_ext_decode_zint(&ext->_body._zint, zbf); + } break; + + case _Z_MSG_EXT_ENC_ZBUF: { + ret |= _z_msg_ext_decode_zbuf(&ext->_body._zbuf, zbf); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to copy message extension with unknown ID(%d)\n", mid); + } break; + } + } + + return ret; +} + +int8_t _z_msg_ext_decode_na(_z_msg_ext_t *ext, _z_zbuf_t *zbf) { return _z_msg_ext_decode(ext, zbf); } diff --git a/tests/z_extcodec_test.c b/tests/z_extcodec_test.c new file mode 100644 index 000000000..952eb4e8f --- /dev/null +++ b/tests/z_extcodec_test.c @@ -0,0 +1,384 @@ +// +// 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, +// + +#define ZENOH_PICO_TEST_H + +#include +#include +#include +#include +#include + +#include "zenoh-pico/protocol/ext.h" +#include "zenoh-pico/protocol/extcodec.h" +#include "zenoh-pico/protocol/iobuf.h" + +#define RUNS 1000 + +#define _Z_MOCK_EXTENSION_UNIT 0x01 +#define _Z_MOCK_EXTENSION_ZINT 0x02 +#define _Z_MOCK_EXTENSION_ZBUF 0x03 + +/*=============================*/ +/* Helper functions */ +/*=============================*/ +void print_iosli(_z_iosli_t *ios) { + printf("IOSli: Capacity: %zu, Rpos: %zu, Wpos: %zu, Buffer: [", ios->_capacity, ios->_r_pos, ios->_w_pos); + for (size_t i = ios->_r_pos; i < ios->_w_pos; i++) { + printf("%02x", ios->_buf[i]); + if (i < ios->_w_pos - ios->_r_pos - 1) printf(" "); + } + printf("]"); +} + +void print_wbuf(_z_wbuf_t *wbf) { + printf(" WBuf: %zu, RIdx: %zu, WIdx: %zu\n", _z_wbuf_len_iosli(wbf), wbf->_r_idx, wbf->_w_idx); + for (size_t i = 0; i < _z_wbuf_len_iosli(wbf); i++) { + printf(" - "); + print_iosli(_z_wbuf_get_iosli(wbf, i)); + printf("\n"); + } +} + +void print_zbuf(_z_zbuf_t *zbf) { print_iosli(&zbf->_ios); } + +void print_uint8_array(_z_bytes_t *arr) { + printf("Length: %zu, Buffer: [", arr->len); + for (size_t i = 0; i < arr->len; i++) { + printf("%02x", arr->start[i]); + if (i < arr->len - 1) printf(" "); + } + printf("]"); +} + +void print_message_extension_type(uint8_t header) { + switch (_Z_EXT_ID(header)) { + case _Z_MOCK_EXTENSION_UNIT: + printf("Mock Extension UNIT"); + break; + case _Z_MOCK_EXTENSION_ZINT: + printf("Mock Extension ZINT"); + break; + case _Z_MOCK_EXTENSION_ZBUF: + printf("Mock Extension ZBUF"); + break; + default: + assert(0); + break; + } +} + +/*=============================*/ +/* Generating functions */ +/*=============================*/ +_Bool gen_bool(void) { return z_random_u8() % 2; } + +uint8_t gen_uint8(void) { return z_random_u8() % 255; } + +_z_zint_t gen_zint(void) { + _z_zint_t ret = 0; + z_random_fill(&ret, sizeof(ret)); + return ret; +} + +_z_wbuf_t gen_wbuf(size_t len) { + _Bool is_expandable = false; + + if (gen_bool()) { + is_expandable = true; + len = 1 + (gen_zint() % len); + } + + return _z_wbuf_make(len, is_expandable); +} + +_z_bytes_t gen_bytes(size_t len) { + _z_bytes_t arr; + arr._is_alloc = true; + arr.len = len; + arr.start = NULL; + if (len == 0) return arr; + + arr.start = (uint8_t *)z_malloc(sizeof(uint8_t) * len); + for (_z_zint_t i = 0; i < len; i++) ((uint8_t *)arr.start)[i] = gen_uint8(); + + return arr; +} + +/*=============================*/ +/* Asserting functions */ +/*=============================*/ +void assert_eq_iosli(_z_iosli_t *left, _z_iosli_t *right) { + printf("IOSli -> "); + printf("Capacity (%zu:%zu), ", left->_capacity, right->_capacity); + + assert(left->_capacity == right->_capacity); + + printf("Content ("); + for (_z_zint_t i = 0; i < left->_capacity; i++) { + uint8_t l = left->_buf[i]; + uint8_t r = right->_buf[i]; + + printf("%02x:%02x", l, r); + if (i < left->_capacity - 1) printf(" "); + + assert(l == r); + } + printf(")"); +} + +void assert_eq_uint8_array(_z_bytes_t *left, _z_bytes_t *right) { + printf("Array -> "); + printf("Length (%zu:%zu), ", left->len, right->len); + + assert(left->len == right->len); + printf("Content ("); + for (size_t i = 0; i < left->len; i++) { + uint8_t l = left->start[i]; + uint8_t r = right->start[i]; + + printf("%02x:%02x", l, r); + if (i < left->len - 1) printf(" "); + + assert(l == r); + } + printf(")"); +} + +/*=============================*/ +/* Zenoh Messages Extensions */ +/*=============================*/ +/*------------------ UNIT extension ------------------*/ +_z_msg_ext_t gen_unit_extension(void) { return _z_msg_ext_make_unit(_Z_MOCK_EXTENSION_UNIT); } + +void assert_eq_unit_extension(_z_msg_ext_unit_t *left, _z_msg_ext_unit_t *right) { + (void)(left); + (void)(right); +} + +void unit_extension(void) { + printf("\n>> UNIT Extension\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_msg_ext_t ext = gen_unit_extension(); + assert(_Z_EXT_ENC(ext._header) == _Z_MSG_EXT_ENC_UNIT); + + _z_msg_ext_unit_t e_u = ext._body._unit; + + // Encode + int8_t res = _z_msg_ext_encode_unit(&wbf, &e_u); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_msg_ext_unit_t d_u; + res = _z_msg_ext_decode_unit(&d_u, &zbf); + assert(res == _Z_RES_OK); + + assert_eq_unit_extension(&e_u, &d_u); + + // Free + _z_msg_ext_clear_unit(&d_u); + _z_msg_ext_clear(&ext); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +/*------------------ ZINT extension ------------------*/ +_z_msg_ext_t gen_zint_extension(void) { + _z_zint_t val = gen_zint(); + return _z_msg_ext_make_zint(_Z_MOCK_EXTENSION_ZINT, val); +} + +void assert_eq_zint_extension(_z_msg_ext_zint_t *left, _z_msg_ext_zint_t *right) { assert(left->_val == right->_val); } + +void zint_extension(void) { + printf("\n>> ZINT Extension\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_msg_ext_t ext = gen_zint_extension(); + assert(_Z_EXT_ENC(ext._header) == _Z_MSG_EXT_ENC_UNIT); + + _z_msg_ext_zint_t e_u = ext._body._zint; + + // Encode + int8_t res = _z_msg_ext_encode_zint(&wbf, &e_u); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_msg_ext_zint_t d_u; + res = _z_msg_ext_decode_zint(&d_u, &zbf); + assert(res == _Z_RES_OK); + + assert_eq_zint_extension(&e_u, &d_u); + + // Free + _z_msg_ext_clear_zint(&d_u); + _z_msg_ext_clear(&ext); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +/*------------------ Unit extension ------------------*/ +_z_msg_ext_t gen_zbuf_extension(void) { + _z_bytes_t val = gen_bytes(gen_uint8()); + return _z_msg_ext_make_zbuf(_Z_MOCK_EXTENSION_ZBUF, val); +} + +void assert_eq_zbuf_extension(_z_msg_ext_zbuf_t *left, _z_msg_ext_zbuf_t *right) { + assert_eq_uint8_array(&left->_val, &right->_val); +} + +void zbuf_extension(void) { + printf("\n>> ZBUF Extension\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_msg_ext_t ext = gen_zbuf_extension(); + assert(_Z_EXT_ENC(ext._header) == _Z_MSG_EXT_ENC_UNIT); + + _z_msg_ext_zbuf_t e_u = ext._body._zbuf; + + // Encode + int8_t res = _z_msg_ext_encode_zbuf(&wbf, &e_u); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_msg_ext_zbuf_t d_u; + res = _z_msg_ext_decode_zbuf(&d_u, &zbf); + assert(res == _Z_RES_OK); + + assert_eq_zbuf_extension(&e_u, &d_u); + + // Free + _z_msg_ext_clear_zbuf(&d_u); + _z_msg_ext_clear(&ext); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +_z_msg_ext_t gen_message_extension(void) { + _z_msg_ext_t p_zm; + + uint8_t mids[] = {_Z_MOCK_EXTENSION_UNIT, _Z_MOCK_EXTENSION_ZINT, _Z_MOCK_EXTENSION_ZBUF}; + uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); + + switch (mids[i]) { + case _Z_MOCK_EXTENSION_UNIT: + p_zm = gen_unit_extension(); + break; + case _Z_MOCK_EXTENSION_ZINT: + p_zm = gen_zint_extension(); + break; + case _Z_MOCK_EXTENSION_ZBUF: + p_zm = gen_zbuf_extension(); + break; + default: + assert(0); + break; + } + + return p_zm; +} + +void assert_eq_message_extension(_z_msg_ext_t *left, _z_msg_ext_t *right) { + printf(" Header (%x:%x)", left->_header, right->_header); + assert(left->_header == right->_header); + printf("\n"); + + switch (_Z_EXT_ID(left->_header)) { + case _Z_MOCK_EXTENSION_UNIT: + assert_eq_unit_extension(&left->_body._unit, &left->_body._unit); + break; + case _Z_MOCK_EXTENSION_ZINT: + assert_eq_zint_extension(&left->_body._zint, &left->_body._zint); + break; + case _Z_MOCK_EXTENSION_ZBUF: + assert_eq_zbuf_extension(&left->_body._zbuf, &left->_body._zbuf); + break; + default: + assert(0); + break; + } +} + +void message_extension(void) { + printf("\n>> Message extension\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_msg_ext_t e_me = gen_message_extension(); + + printf(" - "); + switch (_Z_EXT_ID(e_me._header)) { + case _Z_MOCK_EXTENSION_UNIT: + printf("UNIT extension"); + break; + case _Z_MOCK_EXTENSION_ZINT: + printf("ZINT extension"); + break; + case _Z_MOCK_EXTENSION_ZBUF: + printf("ZBUF extension"); + break; + default: + assert(0); + break; + } + printf("\n"); + + // Encode + int8_t res = _z_msg_ext_encode(&wbf, &e_me); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_msg_ext_t d_me; + res = _z_msg_ext_decode(&d_me, &zbf); + assert(res == _Z_RES_OK); + + assert_eq_message_extension(&e_me, &d_me); + + // Free + _z_msg_ext_clear(&e_me); + _z_msg_ext_clear(&d_me); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +/*=============================*/ +/* Main */ +/*=============================*/ +int main(void) { + setvbuf(stdout, NULL, _IOLBF, 1024); + + for (unsigned int i = 0; i < RUNS; i++) { + printf("\n\n== RUN %u", i); + + unit_extension(); + zint_extension(); + zbuf_extension(); + + message_extension(); + } + + return 0; +} From 254f9faa942bc90317948ba14489af7ee62b4dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 10 Feb 2023 14:41:53 +0100 Subject: [PATCH 011/181] Add extensions to transport messages --- CMakeLists.txt | 3 - include/zenoh-pico/protocol/ext.h | 11 +- include/zenoh-pico/protocol/msg.h | 3 + src/protocol/ext.c | 22 +- src/protocol/extcodec.c | 13 +- src/protocol/msg.c | 17 ++ src/protocol/msgcodec.c | 33 ++- tests/z_extcodec_test.c | 384 ------------------------------ tests/z_msgcodec_test.c | 251 ++++++++++++++++++- 9 files changed, 325 insertions(+), 412 deletions(-) delete mode 100644 tests/z_extcodec_test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 330394c92..780c3f129 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -228,14 +228,12 @@ if(BUILD_TESTING) add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c) add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c) add_executable(z_msgcodec_test ${PROJECT_SOURCE_DIR}/tests/z_msgcodec_test.c) - add_executable(z_extcodec_test ${PROJECT_SOURCE_DIR}/tests/z_extcodec_test.c) add_executable(z_keyexpr_test ${PROJECT_SOURCE_DIR}/tests/z_keyexpr_test.c) target_link_libraries(z_data_struct_test ${Libname}) target_link_libraries(z_endpoint_test ${Libname}) target_link_libraries(z_iobuf_test ${Libname}) target_link_libraries(z_msgcodec_test ${Libname}) - target_link_libraries(z_extcodec_test ${Libname}) target_link_libraries(z_keyexpr_test ${Libname}) enable_testing() @@ -243,7 +241,6 @@ if(BUILD_TESTING) add_test(z_endpoint_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_endpoint_test) add_test(z_iobuf_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iobuf_test) add_test(z_msgcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_msgcodec_test) - add_test(z_extcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_extcodec_test) add_test(z_keyexpr_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_keyexpr_test) endif() diff --git a/include/zenoh-pico/protocol/ext.h b/include/zenoh-pico/protocol/ext.h index 25cf7de5b..3538267a6 100644 --- a/include/zenoh-pico/protocol/ext.h +++ b/include/zenoh-pico/protocol/ext.h @@ -86,9 +86,12 @@ _z_msg_ext_t _z_msg_ext_make_zint(uint8_t id, _z_zint_t zid); _z_msg_ext_t _z_msg_ext_make_zbuf(uint8_t id, _z_bytes_t zbuf); /*------------------ Copy ------------------*/ -void _z_msg_ext_copy(_z_msg_ext_t *clone, _z_msg_ext_t *ext); -void _z_msg_ext_copy_unit(_z_msg_ext_unit_t *clone, _z_msg_ext_unit_t *ext); -void _z_msg_ext_copy_zint(_z_msg_ext_zint_t *clone, _z_msg_ext_zint_t *ext); -void _z_msg_ext_copy_zbuf(_z_msg_ext_zbuf_t *clone, _z_msg_ext_zbuf_t *ext); +void _z_msg_ext_copy(_z_msg_ext_t *clone, const _z_msg_ext_t *ext); +void _z_msg_ext_copy_unit(_z_msg_ext_unit_t *clone, const _z_msg_ext_unit_t *ext); +void _z_msg_ext_copy_zint(_z_msg_ext_zint_t *clone, const _z_msg_ext_zint_t *ext); +void _z_msg_ext_copy_zbuf(_z_msg_ext_zbuf_t *clone, const _z_msg_ext_zbuf_t *ext); + +_Z_ELEM_DEFINE(_z_msg_ext, _z_msg_ext_t, _z_noop_size, _z_msg_ext_clear, _z_msg_ext_copy) +_Z_VEC_DEFINE(_z_msg_ext, _z_msg_ext_t) #endif /* ZENOH_PICO_PROTOCOL_EXTENSION_H */ diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 414056a71..e5477157b 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -23,6 +23,7 @@ #include "zenoh-pico/link/endpoint.h" #include "zenoh-pico/net/query.h" #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/ext.h" #define _Z_DECLARE_CLEAR(layer, name) void _z_##layer##_msg_clear_##name(_z_##name##_t *m, uint8_t header) @@ -997,9 +998,11 @@ typedef union { _z_t_msg_ping_pong_t _ping_pong; _z_t_msg_frame_t _frame; } _z_transport_body_t; + typedef struct { _z_transport_body_t _body; _z_attachment_t *_attachment; + _z_msg_ext_vec_t _extensions; uint8_t _header; } _z_transport_message_t; void _z_t_msg_clear(_z_transport_message_t *msg); diff --git a/src/protocol/ext.c b/src/protocol/ext.c index d864cf0ef..4652e7772 100644 --- a/src/protocol/ext.c +++ b/src/protocol/ext.c @@ -14,6 +14,8 @@ #include "zenoh-pico/protocol/ext.h" +#include + #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/utils/logging.h" @@ -29,7 +31,7 @@ _z_msg_ext_t _z_msg_ext_make_unit(uint8_t id) { void _z_msg_ext_clear_unit(_z_msg_ext_unit_t *ext) { (void)(ext); } -void _z_msg_ext_copy_unit(_z_msg_ext_unit_t *clone, _z_msg_ext_unit_t *ext) { +void _z_msg_ext_copy_unit(_z_msg_ext_unit_t *clone, const _z_msg_ext_unit_t *ext) { (void)(clone); (void)(ext); } @@ -48,7 +50,7 @@ _z_msg_ext_t _z_msg_ext_make_zint(uint8_t id, _z_zint_t zid) { void _z_msg_ext_clear_zint(_z_msg_ext_zint_t *ext) { (void)(ext); } -void _z_msg_ext_copy_zint(_z_msg_ext_zint_t *clone, _z_msg_ext_zint_t *ext) { clone->_val = ext->_val; } +void _z_msg_ext_copy_zint(_z_msg_ext_zint_t *clone, const _z_msg_ext_zint_t *ext) { clone->_val = ext->_val; } _z_msg_ext_t _z_msg_ext_make_zbuf(uint8_t id, const _z_bytes_t zbuf) { _z_msg_ext_t ext; @@ -64,12 +66,15 @@ _z_msg_ext_t _z_msg_ext_make_zbuf(uint8_t id, const _z_bytes_t zbuf) { void _z_msg_ext_clear_zbuf(_z_msg_ext_zbuf_t *ext) { _z_bytes_clear(&ext->_val); } -void _z_msg_ext_copy_zbuf(_z_msg_ext_zbuf_t *clone, _z_msg_ext_zbuf_t *ext) { _z_bytes_copy(&clone->_val, &ext->_val); } +void _z_msg_ext_copy_zbuf(_z_msg_ext_zbuf_t *clone, const _z_msg_ext_zbuf_t *ext) { + _z_bytes_copy(&clone->_val, &ext->_val); +} -void _z_msg_ext_copy(_z_msg_ext_t *clone, _z_msg_ext_t *ext) { +void _z_msg_ext_copy(_z_msg_ext_t *clone, const _z_msg_ext_t *ext) { clone->_header = ext->_header; - switch (_Z_EXT_ENC(clone->_header)) { + uint8_t enc = _Z_EXT_ENC(clone->_header); + switch (enc) { case _Z_MSG_EXT_ENC_UNIT: { _z_msg_ext_copy_unit(&clone->_body._unit, &ext->_body._unit); } break; @@ -83,13 +88,14 @@ void _z_msg_ext_copy(_z_msg_ext_t *clone, _z_msg_ext_t *ext) { } break; default: { - _Z_DEBUG("WARNING: Trying to copy message extension with unknown ID(%d)\n", mid); + _Z_DEBUG("WARNING: Trying to copy message extension with unknown encoding(%d)\n", enc); } break; } } void _z_msg_ext_clear(_z_msg_ext_t *ext) { - switch (_Z_EXT_ENC(ext->_header)) { + uint8_t enc = _Z_EXT_ENC(ext->_header); + switch (enc) { case _Z_MSG_EXT_ENC_UNIT: { _z_msg_ext_clear_unit(&ext->_body._unit); } break; @@ -103,7 +109,7 @@ void _z_msg_ext_clear(_z_msg_ext_t *ext) { } break; default: { - _Z_DEBUG("WARNING: Trying to free message extension with unknown ID(%d)\n", mid); + _Z_DEBUG("WARNING: Trying to free message extension with unknown encoding(%d)\n", enc); } break; } } diff --git a/src/protocol/extcodec.c b/src/protocol/extcodec.c index dd3c4d232..b605aaef1 100644 --- a/src/protocol/extcodec.c +++ b/src/protocol/extcodec.c @@ -67,12 +67,10 @@ int8_t _z_msg_ext_decode_zbuf_na(_z_msg_ext_zbuf_t *ext, _z_zbuf_t *zbf) { retur int8_t _z_msg_ext_encode(_z_wbuf_t *wbf, const _z_msg_ext_t *ext) { int8_t ret = _Z_RES_OK; - // if (is_last == false) { - // _Z_EXT_SET_FLAG(ext->_header, _Z_MSG_EXT_FLAG_Z); - // } _Z_EC(_z_wbuf_write(wbf, ext->_header)) - switch (_Z_EXT_ENC(ext->_header)) { + uint8_t enc = _Z_EXT_ENC(ext->_header); + switch (enc) { case _Z_MSG_EXT_ENC_UNIT: { _z_msg_ext_encode_unit(wbf, &ext->_body._unit); } break; @@ -86,7 +84,7 @@ int8_t _z_msg_ext_encode(_z_wbuf_t *wbf, const _z_msg_ext_t *ext) { } break; default: { - _Z_DEBUG("WARNING: Trying to copy message extension with unknown ID(%d)\n", mid); + _Z_DEBUG("WARNING: Trying to copy message extension with unknown encoding(%d)\n", enc); } break; } @@ -98,7 +96,8 @@ int8_t _z_msg_ext_decode(_z_msg_ext_t *ext, _z_zbuf_t *zbf) { ret |= _z_uint8_decode(&ext->_header, zbf); // Decode the header if (ret == _Z_RES_OK) { - switch (_Z_EXT_ENC(ext->_header)) { + uint8_t enc = _Z_EXT_ENC(ext->_header); + switch (enc) { case _Z_MSG_EXT_ENC_UNIT: { ret |= _z_msg_ext_decode_unit(&ext->_body._unit, zbf); } break; @@ -112,7 +111,7 @@ int8_t _z_msg_ext_decode(_z_msg_ext_t *ext, _z_zbuf_t *zbf) { } break; default: { - _Z_DEBUG("WARNING: Trying to copy message extension with unknown ID(%d)\n", mid); + _Z_DEBUG("WARNING: Trying to copy message extension with unknown encoding(%d)\n", enc); } break; } } diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 2e6e2fd64..0d5489146 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -18,6 +18,7 @@ #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/session/queryable.h" #include "zenoh-pico/utils/logging.h" @@ -460,6 +461,7 @@ _z_transport_message_t _z_t_msg_make_scout(z_what_t what, _z_bytes_t zid) { } msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -492,6 +494,7 @@ _z_transport_message_t _z_t_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, } msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -535,6 +538,7 @@ _z_transport_message_t _z_t_msg_make_join(uint8_t version, z_whatami_t whatami, } msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -575,6 +579,7 @@ _z_transport_message_t _z_t_msg_make_init_syn(uint8_t version, z_whatami_t whata } msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -603,6 +608,7 @@ _z_transport_message_t _z_t_msg_make_init_ack(uint8_t version, z_whatami_t whata } msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -635,6 +641,7 @@ _z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial } msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -653,6 +660,7 @@ _z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial } msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -681,6 +689,7 @@ _z_transport_message_t _z_t_msg_make_close(uint8_t reason, _z_bytes_t zid, _Bool } msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -708,6 +717,7 @@ _z_transport_message_t _z_t_msg_make_sync(_z_zint_t sn, _Bool is_reliable, _z_zi } msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -735,6 +745,7 @@ _z_transport_message_t _z_t_msg_make_ack_nack(_z_zint_t sn, _z_zint_t mask) { } msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -761,6 +772,7 @@ _z_transport_message_t _z_t_msg_make_keep_alive(_z_bytes_t zid) { } msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -780,6 +792,7 @@ _z_transport_message_t _z_t_msg_make_ping(_z_zint_t hash) { msg._header = _Z_MID_PING_PONG; msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -793,6 +806,7 @@ _z_transport_message_t _z_t_msg_make_pong(_z_zint_t hash) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_P); msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -825,6 +839,7 @@ _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliabl } msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -848,6 +863,7 @@ _z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_frame_payload_t payl } msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -873,6 +889,7 @@ void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg, uint8_t header) { void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg) { clone->_header = msg->_header; clone->_attachment = msg->_attachment; + _z_msg_ext_vec_copy(&clone->_extensions, &msg->_extensions); uint8_t mid = _Z_MID(msg->_header); switch (mid) { diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 15efbc291..5e13e00b2 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -14,9 +14,11 @@ #include "zenoh-pico/protocol/msgcodec.h" +#include <_types/_uint8_t.h> #include #include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/protocol/extcodec.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/utils/logging.h" @@ -1465,8 +1467,13 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t _Z_EC(_z_attachment_encode(wbf, msg->_attachment)) } - _Z_EC(_z_wbuf_write(wbf, msg->_header)) + uint8_t header = msg->_header; + size_t n_ext = _z_msg_ext_vec_len(&msg->_extensions); + if (n_ext > 0) { + header |= 0x80; + } + _Z_EC(_z_wbuf_write(wbf, header)) switch (_Z_MID(msg->_header)) { case _Z_MID_FRAME: { ret |= _z_frame_encode(wbf, msg->_header, &msg->_body._frame); @@ -1518,6 +1525,16 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t } break; } + if (n_ext > 0) { + for (size_t i = 0; (i + 1) < n_ext; i++) { + _z_msg_ext_t *ext = _z_msg_ext_vec_get(&msg->_extensions, i); + _Z_EXT_SET_FLAG(ext->_header, _Z_MSG_EXT_FLAG_Z); + ret |= _z_msg_ext_encode(wbf, ext); + } + ret |= + _z_msg_ext_encode(wbf, _z_msg_ext_vec_get(&msg->_extensions, n_ext - 1)); // Last extension wo\ next flag + } + return ret; } @@ -1607,6 +1624,20 @@ int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *zb } } while ((ret == _Z_RES_OK) && (is_last == false)); + if (ret == _Z_RES_OK) { + if ((msg->_header & 0x80) == 0x80) { + msg->_extensions = _z_msg_ext_vec_make(10); + _z_msg_ext_t *ext = NULL; + do { + ext = (_z_msg_ext_t *)z_malloc(sizeof(_z_msg_ext_t)); + ret |= _z_msg_ext_decode(ext, zbf); + _z_msg_ext_vec_append(&msg->_extensions, ext); + } while ((ext->_header & 0x80) == 0x80); + } else { + msg->_extensions = _z_msg_ext_vec_make(0); + } + } + return ret; } diff --git a/tests/z_extcodec_test.c b/tests/z_extcodec_test.c deleted file mode 100644 index 952eb4e8f..000000000 --- a/tests/z_extcodec_test.c +++ /dev/null @@ -1,384 +0,0 @@ -// -// 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, -// - -#define ZENOH_PICO_TEST_H - -#include -#include -#include -#include -#include - -#include "zenoh-pico/protocol/ext.h" -#include "zenoh-pico/protocol/extcodec.h" -#include "zenoh-pico/protocol/iobuf.h" - -#define RUNS 1000 - -#define _Z_MOCK_EXTENSION_UNIT 0x01 -#define _Z_MOCK_EXTENSION_ZINT 0x02 -#define _Z_MOCK_EXTENSION_ZBUF 0x03 - -/*=============================*/ -/* Helper functions */ -/*=============================*/ -void print_iosli(_z_iosli_t *ios) { - printf("IOSli: Capacity: %zu, Rpos: %zu, Wpos: %zu, Buffer: [", ios->_capacity, ios->_r_pos, ios->_w_pos); - for (size_t i = ios->_r_pos; i < ios->_w_pos; i++) { - printf("%02x", ios->_buf[i]); - if (i < ios->_w_pos - ios->_r_pos - 1) printf(" "); - } - printf("]"); -} - -void print_wbuf(_z_wbuf_t *wbf) { - printf(" WBuf: %zu, RIdx: %zu, WIdx: %zu\n", _z_wbuf_len_iosli(wbf), wbf->_r_idx, wbf->_w_idx); - for (size_t i = 0; i < _z_wbuf_len_iosli(wbf); i++) { - printf(" - "); - print_iosli(_z_wbuf_get_iosli(wbf, i)); - printf("\n"); - } -} - -void print_zbuf(_z_zbuf_t *zbf) { print_iosli(&zbf->_ios); } - -void print_uint8_array(_z_bytes_t *arr) { - printf("Length: %zu, Buffer: [", arr->len); - for (size_t i = 0; i < arr->len; i++) { - printf("%02x", arr->start[i]); - if (i < arr->len - 1) printf(" "); - } - printf("]"); -} - -void print_message_extension_type(uint8_t header) { - switch (_Z_EXT_ID(header)) { - case _Z_MOCK_EXTENSION_UNIT: - printf("Mock Extension UNIT"); - break; - case _Z_MOCK_EXTENSION_ZINT: - printf("Mock Extension ZINT"); - break; - case _Z_MOCK_EXTENSION_ZBUF: - printf("Mock Extension ZBUF"); - break; - default: - assert(0); - break; - } -} - -/*=============================*/ -/* Generating functions */ -/*=============================*/ -_Bool gen_bool(void) { return z_random_u8() % 2; } - -uint8_t gen_uint8(void) { return z_random_u8() % 255; } - -_z_zint_t gen_zint(void) { - _z_zint_t ret = 0; - z_random_fill(&ret, sizeof(ret)); - return ret; -} - -_z_wbuf_t gen_wbuf(size_t len) { - _Bool is_expandable = false; - - if (gen_bool()) { - is_expandable = true; - len = 1 + (gen_zint() % len); - } - - return _z_wbuf_make(len, is_expandable); -} - -_z_bytes_t gen_bytes(size_t len) { - _z_bytes_t arr; - arr._is_alloc = true; - arr.len = len; - arr.start = NULL; - if (len == 0) return arr; - - arr.start = (uint8_t *)z_malloc(sizeof(uint8_t) * len); - for (_z_zint_t i = 0; i < len; i++) ((uint8_t *)arr.start)[i] = gen_uint8(); - - return arr; -} - -/*=============================*/ -/* Asserting functions */ -/*=============================*/ -void assert_eq_iosli(_z_iosli_t *left, _z_iosli_t *right) { - printf("IOSli -> "); - printf("Capacity (%zu:%zu), ", left->_capacity, right->_capacity); - - assert(left->_capacity == right->_capacity); - - printf("Content ("); - for (_z_zint_t i = 0; i < left->_capacity; i++) { - uint8_t l = left->_buf[i]; - uint8_t r = right->_buf[i]; - - printf("%02x:%02x", l, r); - if (i < left->_capacity - 1) printf(" "); - - assert(l == r); - } - printf(")"); -} - -void assert_eq_uint8_array(_z_bytes_t *left, _z_bytes_t *right) { - printf("Array -> "); - printf("Length (%zu:%zu), ", left->len, right->len); - - assert(left->len == right->len); - printf("Content ("); - for (size_t i = 0; i < left->len; i++) { - uint8_t l = left->start[i]; - uint8_t r = right->start[i]; - - printf("%02x:%02x", l, r); - if (i < left->len - 1) printf(" "); - - assert(l == r); - } - printf(")"); -} - -/*=============================*/ -/* Zenoh Messages Extensions */ -/*=============================*/ -/*------------------ UNIT extension ------------------*/ -_z_msg_ext_t gen_unit_extension(void) { return _z_msg_ext_make_unit(_Z_MOCK_EXTENSION_UNIT); } - -void assert_eq_unit_extension(_z_msg_ext_unit_t *left, _z_msg_ext_unit_t *right) { - (void)(left); - (void)(right); -} - -void unit_extension(void) { - printf("\n>> UNIT Extension\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_msg_ext_t ext = gen_unit_extension(); - assert(_Z_EXT_ENC(ext._header) == _Z_MSG_EXT_ENC_UNIT); - - _z_msg_ext_unit_t e_u = ext._body._unit; - - // Encode - int8_t res = _z_msg_ext_encode_unit(&wbf, &e_u); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_msg_ext_unit_t d_u; - res = _z_msg_ext_decode_unit(&d_u, &zbf); - assert(res == _Z_RES_OK); - - assert_eq_unit_extension(&e_u, &d_u); - - // Free - _z_msg_ext_clear_unit(&d_u); - _z_msg_ext_clear(&ext); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ ZINT extension ------------------*/ -_z_msg_ext_t gen_zint_extension(void) { - _z_zint_t val = gen_zint(); - return _z_msg_ext_make_zint(_Z_MOCK_EXTENSION_ZINT, val); -} - -void assert_eq_zint_extension(_z_msg_ext_zint_t *left, _z_msg_ext_zint_t *right) { assert(left->_val == right->_val); } - -void zint_extension(void) { - printf("\n>> ZINT Extension\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_msg_ext_t ext = gen_zint_extension(); - assert(_Z_EXT_ENC(ext._header) == _Z_MSG_EXT_ENC_UNIT); - - _z_msg_ext_zint_t e_u = ext._body._zint; - - // Encode - int8_t res = _z_msg_ext_encode_zint(&wbf, &e_u); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_msg_ext_zint_t d_u; - res = _z_msg_ext_decode_zint(&d_u, &zbf); - assert(res == _Z_RES_OK); - - assert_eq_zint_extension(&e_u, &d_u); - - // Free - _z_msg_ext_clear_zint(&d_u); - _z_msg_ext_clear(&ext); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ Unit extension ------------------*/ -_z_msg_ext_t gen_zbuf_extension(void) { - _z_bytes_t val = gen_bytes(gen_uint8()); - return _z_msg_ext_make_zbuf(_Z_MOCK_EXTENSION_ZBUF, val); -} - -void assert_eq_zbuf_extension(_z_msg_ext_zbuf_t *left, _z_msg_ext_zbuf_t *right) { - assert_eq_uint8_array(&left->_val, &right->_val); -} - -void zbuf_extension(void) { - printf("\n>> ZBUF Extension\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_msg_ext_t ext = gen_zbuf_extension(); - assert(_Z_EXT_ENC(ext._header) == _Z_MSG_EXT_ENC_UNIT); - - _z_msg_ext_zbuf_t e_u = ext._body._zbuf; - - // Encode - int8_t res = _z_msg_ext_encode_zbuf(&wbf, &e_u); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_msg_ext_zbuf_t d_u; - res = _z_msg_ext_decode_zbuf(&d_u, &zbf); - assert(res == _Z_RES_OK); - - assert_eq_zbuf_extension(&e_u, &d_u); - - // Free - _z_msg_ext_clear_zbuf(&d_u); - _z_msg_ext_clear(&ext); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -_z_msg_ext_t gen_message_extension(void) { - _z_msg_ext_t p_zm; - - uint8_t mids[] = {_Z_MOCK_EXTENSION_UNIT, _Z_MOCK_EXTENSION_ZINT, _Z_MOCK_EXTENSION_ZBUF}; - uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); - - switch (mids[i]) { - case _Z_MOCK_EXTENSION_UNIT: - p_zm = gen_unit_extension(); - break; - case _Z_MOCK_EXTENSION_ZINT: - p_zm = gen_zint_extension(); - break; - case _Z_MOCK_EXTENSION_ZBUF: - p_zm = gen_zbuf_extension(); - break; - default: - assert(0); - break; - } - - return p_zm; -} - -void assert_eq_message_extension(_z_msg_ext_t *left, _z_msg_ext_t *right) { - printf(" Header (%x:%x)", left->_header, right->_header); - assert(left->_header == right->_header); - printf("\n"); - - switch (_Z_EXT_ID(left->_header)) { - case _Z_MOCK_EXTENSION_UNIT: - assert_eq_unit_extension(&left->_body._unit, &left->_body._unit); - break; - case _Z_MOCK_EXTENSION_ZINT: - assert_eq_zint_extension(&left->_body._zint, &left->_body._zint); - break; - case _Z_MOCK_EXTENSION_ZBUF: - assert_eq_zbuf_extension(&left->_body._zbuf, &left->_body._zbuf); - break; - default: - assert(0); - break; - } -} - -void message_extension(void) { - printf("\n>> Message extension\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_msg_ext_t e_me = gen_message_extension(); - - printf(" - "); - switch (_Z_EXT_ID(e_me._header)) { - case _Z_MOCK_EXTENSION_UNIT: - printf("UNIT extension"); - break; - case _Z_MOCK_EXTENSION_ZINT: - printf("ZINT extension"); - break; - case _Z_MOCK_EXTENSION_ZBUF: - printf("ZBUF extension"); - break; - default: - assert(0); - break; - } - printf("\n"); - - // Encode - int8_t res = _z_msg_ext_encode(&wbf, &e_me); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_msg_ext_t d_me; - res = _z_msg_ext_decode(&d_me, &zbf); - assert(res == _Z_RES_OK); - - assert_eq_message_extension(&e_me, &d_me); - - // Free - _z_msg_ext_clear(&e_me); - _z_msg_ext_clear(&d_me); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*=============================*/ -/* Main */ -/*=============================*/ -int main(void) { - setvbuf(stdout, NULL, _IOLBF, 1024); - - for (unsigned int i = 0; i < RUNS; i++) { - printf("\n\n== RUN %u", i); - - unit_extension(); - zint_extension(); - zbuf_extension(); - - message_extension(); - } - - return 0; -} diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 3e5d8fe1e..9b523e682 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -22,6 +22,8 @@ #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/string.h" +#include "zenoh-pico/protocol/ext.h" +#include "zenoh-pico/protocol/extcodec.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/protocol/msg.h" @@ -30,6 +32,10 @@ #define RUNS 1000 +#define _Z_MOCK_EXTENSION_UNIT 0x01 +#define _Z_MOCK_EXTENSION_ZINT 0x02 +#define _Z_MOCK_EXTENSION_ZBUF 0x03 + /*=============================*/ /* Helper functions */ /*=============================*/ @@ -282,6 +288,219 @@ void assert_eq_locator_array(_z_locator_array_t *left, _z_locator_array_t *right printf(")"); } +/*=============================*/ +/* Zenoh Messages Extensions */ +/*=============================*/ +/*------------------ UNIT extension ------------------*/ +_z_msg_ext_t gen_unit_extension(void) { return _z_msg_ext_make_unit(_Z_MOCK_EXTENSION_UNIT); } + +void assert_eq_unit_extension(_z_msg_ext_unit_t *left, _z_msg_ext_unit_t *right) { + (void)(left); + (void)(right); +} + +void unit_extension(void) { + printf("\n>> UNIT Extension\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_msg_ext_t ext = gen_unit_extension(); + assert(_Z_EXT_ENC(ext._header) == _Z_MSG_EXT_ENC_UNIT); + + _z_msg_ext_unit_t e_u = ext._body._unit; + + // Encode + int8_t res = _z_msg_ext_encode_unit(&wbf, &e_u); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_msg_ext_unit_t d_u; + res = _z_msg_ext_decode_unit(&d_u, &zbf); + assert(res == _Z_RES_OK); + + assert_eq_unit_extension(&e_u, &d_u); + + // Free + _z_msg_ext_clear_unit(&d_u); + _z_msg_ext_clear(&ext); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +/*------------------ ZINT extension ------------------*/ +_z_msg_ext_t gen_zint_extension(void) { + _z_zint_t val = gen_zint(); + return _z_msg_ext_make_zint(_Z_MOCK_EXTENSION_ZINT, val); +} + +void assert_eq_zint_extension(_z_msg_ext_zint_t *left, _z_msg_ext_zint_t *right) { + printf(" ZINT (%zu:%zu), ", left->_val, right->_val); + assert(left->_val == right->_val); +} + +void zint_extension(void) { + printf("\n>> ZINT Extension\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_msg_ext_t ext = gen_zint_extension(); + assert(_Z_EXT_ENC(ext._header) == _Z_MSG_EXT_ENC_UNIT); + + _z_msg_ext_zint_t e_u = ext._body._zint; + + // Encode + int8_t res = _z_msg_ext_encode_zint(&wbf, &e_u); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_msg_ext_zint_t d_u; + res = _z_msg_ext_decode_zint(&d_u, &zbf); + assert(res == _Z_RES_OK); + + assert_eq_zint_extension(&e_u, &d_u); + + // Free + _z_msg_ext_clear_zint(&d_u); + _z_msg_ext_clear(&ext); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +/*------------------ Unit extension ------------------*/ +_z_msg_ext_t gen_zbuf_extension(void) { + _z_bytes_t val = gen_bytes(gen_uint8()); + return _z_msg_ext_make_zbuf(_Z_MOCK_EXTENSION_ZBUF, val); +} + +void assert_eq_zbuf_extension(_z_msg_ext_zbuf_t *left, _z_msg_ext_zbuf_t *right) { + printf(" ZBUF ("); + assert_eq_uint8_array(&left->_val, &right->_val); + printf(")"); +} + +void zbuf_extension(void) { + printf("\n>> ZBUF Extension\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_msg_ext_t ext = gen_zbuf_extension(); + assert(_Z_EXT_ENC(ext._header) == _Z_MSG_EXT_ENC_UNIT); + + _z_msg_ext_zbuf_t e_u = ext._body._zbuf; + + // Encode + int8_t res = _z_msg_ext_encode_zbuf(&wbf, &e_u); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_msg_ext_zbuf_t d_u; + res = _z_msg_ext_decode_zbuf(&d_u, &zbf); + assert(res == _Z_RES_OK); + + assert_eq_zbuf_extension(&e_u, &d_u); + + // Free + _z_msg_ext_clear_zbuf(&d_u); + _z_msg_ext_clear(&ext); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +_z_msg_ext_t gen_message_extension(void) { + _z_msg_ext_t p_zm; + + uint8_t mids[] = {_Z_MOCK_EXTENSION_UNIT, _Z_MOCK_EXTENSION_ZINT, _Z_MOCK_EXTENSION_ZBUF}; + uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); + + switch (mids[i]) { + case _Z_MOCK_EXTENSION_UNIT: + p_zm = gen_unit_extension(); + break; + case _Z_MOCK_EXTENSION_ZINT: + p_zm = gen_zint_extension(); + break; + case _Z_MOCK_EXTENSION_ZBUF: + p_zm = gen_zbuf_extension(); + break; + default: + assert(0); + break; + } + + return p_zm; +} + +void assert_eq_message_extension(_z_msg_ext_t *left, _z_msg_ext_t *right) { + printf(" Header (%x:%x)", left->_header, right->_header); + assert(left->_header == right->_header); + printf("\n"); + + switch (_Z_EXT_ID(left->_header)) { + case _Z_MOCK_EXTENSION_UNIT: + assert_eq_unit_extension(&left->_body._unit, &left->_body._unit); + break; + case _Z_MOCK_EXTENSION_ZINT: + assert_eq_zint_extension(&left->_body._zint, &left->_body._zint); + break; + case _Z_MOCK_EXTENSION_ZBUF: + assert_eq_zbuf_extension(&left->_body._zbuf, &left->_body._zbuf); + break; + default: + assert(0); + break; + } +} + +void message_extension(void) { + printf("\n>> Message extension\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_msg_ext_t e_me = gen_message_extension(); + + printf(" - "); + switch (_Z_EXT_ID(e_me._header)) { + case _Z_MOCK_EXTENSION_UNIT: + printf("UNIT extension"); + break; + case _Z_MOCK_EXTENSION_ZINT: + printf("ZINT extension"); + break; + case _Z_MOCK_EXTENSION_ZBUF: + printf("ZBUF extension"); + break; + default: + assert(0); + break; + } + printf("\n"); + + // Encode + int8_t res = _z_msg_ext_encode(&wbf, &e_me); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_msg_ext_t d_me; + res = _z_msg_ext_decode(&d_me, &zbf); + assert(res == _Z_RES_OK); + + assert_eq_message_extension(&e_me, &d_me); + + // Free + _z_msg_ext_clear(&e_me); + _z_msg_ext_clear(&d_me); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + /*=============================*/ /* Message Fields */ /*=============================*/ @@ -2214,9 +2433,8 @@ _z_transport_message_t gen_frame_message(_Bool can_be_fragment) { _z_zint_t num = (gen_zint() % 4) + 1; payload._messages = _z_vec_make(num); for (_z_zint_t i = 0; i < num; i++) { - _z_zenoh_message_t e_zm = gen_zenoh_message(); _z_zenoh_message_t *p_zm = (_z_zenoh_message_t *)z_malloc(sizeof(_z_zenoh_message_t)); - *p_zm = e_zm; + *p_zm = gen_zenoh_message(); _z_vec_append(&payload._messages, p_zm); } } @@ -2324,22 +2542,29 @@ _z_transport_message_t gen_transport_message(_Bool can_be_fragment) { break; } - if (gen_bool()) + if (gen_bool()) { e_tm._attachment = gen_attachment(); - else + } else { e_tm._attachment = NULL; + } return e_tm; } void assert_eq_transport_message(_z_transport_message_t *left, _z_transport_message_t *right) { + // FIXME: This is here to set the extensions flags that is only known at encoding time + if (_z_msg_ext_vec_len(&left->_extensions) > (size_t)0) { + left->_header |= 0x80; + } + // Test message decorators if (left->_attachment && right->_attachment) { printf(" "); assert_eq_attachment(left->_attachment, right->_attachment); printf("\n"); - } else + } else { assert(left->_attachment == right->_attachment); + } // Test message printf(" Header (%x:%x)", left->_header, right->_header); @@ -2384,6 +2609,15 @@ void assert_eq_transport_message(_z_transport_message_t *left, _z_transport_mess assert(0); break; } + + size_t left_n_ext = _z_msg_ext_vec_len(&left->_extensions); + size_t right_n_ext = _z_msg_ext_vec_len(&right->_extensions); + printf(" # of extensions (%zu:%zu)", left_n_ext, right_n_ext); + assert(left_n_ext == right_n_ext); + for (size_t i = 0; i < left_n_ext; i++) { + assert_eq_message_extension(_z_msg_ext_vec_get(&left->_extensions, i), + _z_msg_ext_vec_get(&right->_extensions, i)); + } } void transport_message(void) { @@ -2639,6 +2873,13 @@ int main(void) { for (unsigned int i = 0; i < RUNS; i++) { printf("\n\n== RUN %u", i); + + // Message extensions + unit_extension(); + zint_extension(); + zbuf_extension(); + message_extension(); + // Message fields payload_field(); timestamp_field(); From adb4f0f8b0af7a927d1191689174159db8617518 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 16 Feb 2023 12:19:07 +0100 Subject: [PATCH 012/181] Add UUID utils --- include/zenoh-pico/utils/uuid.h | 37 +++++++++++++++++++++++++++++++++ src/utils/uuid.c | 33 +++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 include/zenoh-pico/utils/uuid.h create mode 100644 src/utils/uuid.c diff --git a/include/zenoh-pico/utils/uuid.h b/include/zenoh-pico/utils/uuid.h new file mode 100644 index 000000000..32f620950 --- /dev/null +++ b/include/zenoh-pico/utils/uuid.h @@ -0,0 +1,37 @@ +// +// 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, +// + +#include +#include +#include + +/** + * Converts an UUID in string format to a byte array. + * + * Parameters: + * bytes: Pointer to an already allocated byte array of size (at least) 16 bytes. + * uuid_str: A valid UUID string. + */ +void _z_uuid_to_bytes(uint8_t *bytes, const char *uuid_str); + +/** + * Converts a byte array representing an UUID to its string representation. + * + * Parameters: + * bytes: Pointer to the byte array containing the UUID. + * + * Returns: + * The `char *` of the UUID in string format. + */ +char *_z_bytes_to_uuid(const uint8_t *uuid); diff --git a/src/utils/uuid.c b/src/utils/uuid.c new file mode 100644 index 000000000..e29596c74 --- /dev/null +++ b/src/utils/uuid.c @@ -0,0 +1,33 @@ +// +// 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, +// + +#include "zenoh-pico/utils/uuid.h" + +#include + +#include "zenoh-pico/utils/pointers.h" + +#define UUID_SIZE 16 + +void _z_uuid_to_bytes(uint8_t *bytes, const char *uuid_str) { + uint8_t n_dash = 0; + for (uint8_t i = 0; i < 32; i += 2) { + if (i == 8 || i == 12 || i == 16 || i == 18) { + n_dash += 1; + } + char val[5] = {'0', 'x', uuid_str[i + n_dash], uuid_str[i + 1 + n_dash], '\0'}; + *bytes = strtol(val, NULL, 0); + bytes = _z_ptr_u8_offset(bytes, 1); + } +} From e98aa7b6681894e98c94a81ed54692fe1363333c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 16 Feb 2023 12:19:43 +0100 Subject: [PATCH 013/181] Allow ZID to be set in configuration --- include/zenoh-pico/config.h | 6 ++++++ src/api/api.c | 36 +++++++++++++++++++++++++++++------- src/net/session.c | 25 ++++++++++++++++++------- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index 234e66fb3..b662d664f 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -88,6 +88,12 @@ #define Z_CONFIG_SCOUTING_WHAT_KEY 0x48 #define Z_CONFIG_SCOUTING_WHAT_DEFAULT "3" +/** + * A configurable and static Zenoh ID to be used on Zenoh Sessions. + * Accepted values : ``. + */ +#define Z_CONFIG_SESSION_ZID_KEY 0x49 + /** * Indicates if data messages should be timestamped. * Accepted values : `false`, `true`. diff --git a/src/api/api.c b/src/api/api.c index a9abdca97..745ffd1b8 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -30,6 +30,7 @@ #include "zenoh-pico/session/queryable.h" #include "zenoh-pico/session/resource.h" #include "zenoh-pico/session/utils.h" +#include "zenoh-pico/utils/uuid.h" /********* Data Types Handlers *********/ z_string_t z_string_make(const char *value) { return _z_string_make(value); } @@ -397,13 +398,34 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb wrapped_ctx->user_call = callback->call; wrapped_ctx->ctx = ctx; - char *what_str = _z_config_get(config->_value, Z_CONFIG_SCOUTING_WHAT_KEY); - z_what_t what = strtol(what_str, NULL, 10); - char *locator = _z_config_get(config->_value, Z_CONFIG_MULTICAST_LOCATOR_KEY); - char *tout_str = _z_config_get(config->_value, Z_CONFIG_SCOUTING_TIMEOUT_KEY); - uint32_t tout = strtoul(tout_str, NULL, 10); - _z_bytes_t zid = _z_bytes_empty(); // TODO[protocol]: Check if a ZID is set in the config files - _z_scout(what, zid, locator, tout, __z_hello_handler, wrapped_ctx, callback->drop, ctx); + char *opt_as_str = _z_config_get(config->_value, Z_CONFIG_SCOUTING_WHAT_KEY); + if (opt_as_str == NULL) { + opt_as_str = Z_CONFIG_SCOUTING_WHAT_DEFAULT; + } + z_what_t what = strtol(opt_as_str, NULL, 10); + + opt_as_str = _z_config_get(config->_value, Z_CONFIG_MULTICAST_LOCATOR_KEY); + if (opt_as_str == NULL) { + opt_as_str = Z_CONFIG_MULTICAST_LOCATOR_DEFAULT; + } + char *mcast_locator = opt_as_str; + + opt_as_str = _z_config_get(config->_value, Z_CONFIG_SCOUTING_TIMEOUT_KEY); + if (opt_as_str == NULL) { + opt_as_str = Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT; + } + uint32_t timeout = strtoul(opt_as_str, NULL, 10); + + _z_bytes_t zid; + char *zid_str = _z_config_get(config->_value, Z_CONFIG_SESSION_ZID_KEY); + if (zid_str != NULL) { + zid = _z_bytes_make(16); + _z_uuid_to_bytes((uint8_t *)zid.start, zid_str); + } else { + zid = _z_bytes_empty(); + } + + _z_scout(what, zid, mcast_locator, timeout, __z_hello_handler, wrapped_ctx, callback->drop, ctx); z_free(wrapped_ctx); z_scouting_config_drop(config); diff --git a/src/net/session.c b/src/net/session.c index 19347a767..a64445d96 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -16,19 +16,23 @@ #include #include +#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/net/memory.h" #include "zenoh-pico/session/utils.h" #include "zenoh-pico/transport/link/task/join.h" #include "zenoh-pico/transport/link/task/lease.h" #include "zenoh-pico/transport/link/task/read.h" #include "zenoh-pico/utils/logging.h" +#include "zenoh-pico/utils/uuid.h" -int8_t __z_open_inner(_z_session_t *zn, char *locator, z_whatami_t mode) { +int8_t __z_open_inner(_z_session_t *zn, char *locator, z_whatami_t mode, _z_bytes_t local_zid) { int8_t ret = _Z_RES_OK; #if Z_UNICAST_TRANSPORT == 1 || Z_MULTICAST_TRANSPORT == 1 - _z_bytes_t local_zid = _z_bytes_empty(); - ret = _z_session_generate_zid(&local_zid, Z_ZID_LENGTH); + if (_z_bytes_is_empty(&local_zid) == true) { + ret = _z_session_generate_zid(&local_zid, Z_ZID_LENGTH); + } + if (ret == _Z_RES_OK) { ret = _z_new_transport(&zn->_tp, &local_zid, locator, mode); if (ret != _Z_RES_OK) { @@ -51,10 +55,19 @@ int8_t __z_open_inner(_z_session_t *zn, char *locator, z_whatami_t mode) { int8_t _z_open(_z_session_t *zn, _z_config_t *config) { int8_t ret = _Z_RES_OK; + _z_bytes_t zid; + char *opt_as_str = _z_config_get(config, Z_CONFIG_SESSION_ZID_KEY); + if (opt_as_str != NULL) { + zid = _z_bytes_make(16); + _z_uuid_to_bytes((uint8_t *)zid.start, opt_as_str); + } else { + zid = _z_bytes_empty(); + } + if (config != NULL) { char *locator = NULL; if (_z_config_get(config, Z_CONFIG_PEER_KEY) == NULL) { // Scout if peer is not configured - char *opt_as_str = _z_config_get(config, Z_CONFIG_SCOUTING_WHAT_KEY); + opt_as_str = _z_config_get(config, Z_CONFIG_SCOUTING_WHAT_KEY); if (opt_as_str == NULL) { opt_as_str = Z_CONFIG_SCOUTING_WHAT_DEFAULT; } @@ -72,8 +85,6 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { } uint32_t timeout = strtoul(opt_as_str, NULL, 10); - _z_bytes_t zid = _z_bytes_empty(); // TODO[protocol]: Check if a ZID is set in the config files - // Scout and return upon the first result _z_hello_list_t *hellos = _z_scout_inner(what, zid, mcast_locator, timeout, true); _z_hello_list_t *xs = hellos; @@ -108,7 +119,7 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { } if (ret == _Z_RES_OK) { - ret = __z_open_inner(zn, locator, mode); + ret = __z_open_inner(zn, locator, mode, zid); } else { _Z_ERROR("Trying to configure an invalid mode.\n"); } From 2af5b6e655b2dce419a3c816ab0dca4eb53c2418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 16 Feb 2023 12:27:48 +0100 Subject: [PATCH 014/181] Add missing copyright --- include/zenoh-pico/utils/string.h | 14 ++++++++++++++ src/utils/string.c | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/include/zenoh-pico/utils/string.h b/include/zenoh-pico/utils/string.h index 96de0f729..89f8e7cf7 100644 --- a/include/zenoh-pico/utils/string.h +++ b/include/zenoh-pico/utils/string.h @@ -1,3 +1,17 @@ +// +// 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, +// + #include #include #include diff --git a/src/utils/string.c b/src/utils/string.c index ab74bb63d..cb181d615 100644 --- a/src/utils/string.c +++ b/src/utils/string.c @@ -1,3 +1,17 @@ +// +// 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, +// + #include "zenoh-pico/utils/string.h" #include From 228ca77203ae9bb2129b522cb470cf3c46d36dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 16 Feb 2023 13:10:38 +0100 Subject: [PATCH 015/181] Explicit set compressed byte to 0 --- src/protocol/msgcodec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 5e13e00b2..9b0e4571b 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1015,7 +1015,7 @@ int8_t _z_scout_decode_na(_z_t_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) ret |= _z_uint8_decode(&msg->_version, zbf); - uint8_t cbyte; + uint8_t cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); msg->_what = cbyte & 0x07; if (_Z_HAS_FLAG(cbyte, _Z_FLAG_SCOUT_I) == true) { @@ -1058,7 +1058,7 @@ int8_t _z_hello_decode_na(_z_t_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) ret |= _z_uint8_decode(&msg->_version, zbf); - uint8_t cbyte; + uint8_t cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); msg->_whatami = cbyte & 0x03; msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; From 7b5bd090a062228f9cfdac1c6881b42fc86aaed7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 16 Feb 2023 15:07:56 +0100 Subject: [PATCH 016/181] Refactor Init message --- include/zenoh-pico/config.h | 27 ++++--- include/zenoh-pico/protocol/codec.h | 3 + include/zenoh-pico/protocol/msg.h | 92 +++++++++++++++++------- include/zenoh-pico/transport/transport.h | 19 ++--- include/zenoh-pico/transport/utils.h | 8 +-- include/zenoh-pico/utils/math.h | 27 +++++++ include/zenoh-pico/utils/uuid.h | 2 - src/protocol/codec.c | 23 ++++++ src/protocol/msg.c | 76 +++++++++----------- src/protocol/msgcodec.c | 71 ++++++++++-------- src/session/scout.c | 8 +-- src/transport/common/rx.c | 2 +- src/transport/common/tx.c | 2 +- src/transport/multicast/link/rx.c | 16 ++--- src/transport/multicast/link/tx.c | 4 +- src/transport/peer_entry.c | 4 +- src/transport/transport.c | 67 ++++++++++------- src/transport/unicast/link/rx.c | 5 +- src/transport/unicast/link/tx.c | 4 +- src/transport/utils.c | 22 +++--- src/utils/math.c | 25 +++++++ tests/z_msgcodec_test.c | 84 ++++++++++++++-------- 22 files changed, 378 insertions(+), 213 deletions(-) create mode 100644 include/zenoh-pico/utils/math.h create mode 100644 src/utils/math.c diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index b662d664f..de63a64c5 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -157,7 +157,15 @@ * 2^28 = 268_435_456 => Max Seq Num = 268_435_455 */ #ifndef Z_SN_RESOLUTION -#define Z_SN_RESOLUTION 268435455 +#define Z_SN_RESOLUTION 0x02 +#endif + +#ifndef Z_KID_RESOLUTION +#define Z_KID_RESOLUTION 0x03 +#endif + +#ifndef Z_REQ_RESOLUTION +#define Z_REQ_RESOLUTION 0x03 #endif /** @@ -224,21 +232,10 @@ #endif /** - * Defaulf maximum batch size possible to be received. - */ -#ifndef Z_BATCH_SIZE_RX -#define Z_BATCH_SIZE_RX \ - 65535 // Warning: changing this value can break the communication - // with zenohd in the current protocol version. - // In the future, it will be possible to negotiate such value. - // Change it at your own risk. -#endif - -/** - * Defaulf maximum batch size possible to be sent. + * Defaulf maximum batch size possible to be received or sent. */ -#ifndef Z_BATCH_SIZE_TX -#define Z_BATCH_SIZE_TX 65535 +#ifndef Z_BATCH_SIZE +#define Z_BATCH_SIZE 65535 #endif /** diff --git a/include/zenoh-pico/protocol/codec.h b/include/zenoh-pico/protocol/codec.h index 5f839dff3..df5c53b10 100644 --- a/include/zenoh-pico/protocol/codec.h +++ b/include/zenoh-pico/protocol/codec.h @@ -37,6 +37,9 @@ int8_t _z_uint_decode(unsigned int *u, _z_zbuf_t *buf); int8_t _z_uint8_encode(_z_wbuf_t *buf, uint8_t v); int8_t _z_uint8_decode(uint8_t *u8, _z_zbuf_t *buf); +int8_t _z_uint16_encode(_z_wbuf_t *buf, uint16_t v); +int8_t _z_uint16_decode(uint16_t *u8, _z_zbuf_t *buf); + int8_t _z_uint64_encode(_z_wbuf_t *buf, uint64_t v); int8_t _z_uint64_decode(uint64_t *u8, _z_zbuf_t *buf); diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index e5477157b..e7f84caef 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -25,6 +25,9 @@ #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/ext.h" +#define _Z_DEFAULT_BATCH_SIZE 65535 +#define _Z_DEFAULT_SIZET_SIZE 8 // In bytes + #define _Z_DECLARE_CLEAR(layer, name) void _z_##layer##_msg_clear_##name(_z_##name##_t *m, uint8_t header) #define _Z_DECLARE_CLEAR_NOH(layer, name) void _z_##layer##_msg_clear_##name(_z_##name##_t *m) @@ -69,16 +72,24 @@ /*=============================*/ // Scout message flags: // I ZenohID if I==1 then the ZenohID is present -// Z Zenoh properties if Z==1 then Zenoh properties are present +// Z Extensions if Z==1 then Zenoh extensions are present #define _Z_FLAG_SCOUT_I 0x08 // 1 << 3 #define _Z_FLAG_SCOUT_Z 0x80 // 1 << 7 // Hello message flags: // L Locators if L==1 then Locators are present -// Z Zenoh properties if Z==1 then Zenoh properties are present +// Z Extensions if Z==1 then Zenoh extensions are present #define _Z_FLAG_HELLO_L 0x20 // 1 << 5 #define _Z_FLAG_HELLO_Z 0x80 // 1 << 7 +// Init message flags: +// A Ack if A==1 then the message is an acknowledgment (aka InitAck), otherwise InitSyn +// S Size params if S==1 then size parameters are exchanged +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_INIT_A 0x20 // 1 << 5 +#define _Z_FLAG_INIT_S 0x40 // 1 << 6 +#define _Z_FLAG_INIT_Z 0x80 // 1 << 7 + /* Transport message flags */ #define _Z_FLAG_T_A 0x20 // 1 << 5 | Ack if A==1 then the message is an acknowledgment #define _Z_FLAG_T_C 0x40 // 1 << 6 | Count if C==1 then number of unacknowledged messages is present @@ -705,7 +716,7 @@ void _z_t_msg_clear_hello(_z_t_msg_hello_t *msg); // +---------------+ // ~ lease ~ -- Lease period of the sender of the JOIN message(*) // +---------------+ -// ~ sn_resolution ~ if S==1(*) -- Otherwise 2^28 is assumed(**) +// ~ seq_num_res ~ if S==1(*) -- Otherwise 2^28 is assumed(**) // +---------------+ // ~ [next_sn] ~ (***) // +---------------+ @@ -732,7 +743,7 @@ typedef struct { _z_bytes_t _zid; _z_zint_t _options; _z_zint_t _lease; - _z_zint_t _sn_resolution; + _z_zint_t _seq_num_res; _z_conduit_sn_list_t _next_sns; z_whatami_t _whatami; uint8_t _version; @@ -753,34 +764,69 @@ void _z_t_msg_clear_join(_z_t_msg_join_t *msg); // corresponding peer deems appropriate to initialize a session with the initiator, the corresponding // peer MUST reply with an INIT message with the A flag set to 1. // +// Flags: +// - A: Ack if A==0 then the message is an InitSyn else it is an InitAck +// - S: Size params if S==1 then size parameters are exchanged +// - Z: Extensions if Z==1 then zenoh extensions will follow. +// // 7 6 5 4 3 2 1 0 // +-+-+-+-+-+-+-+-+ -// |O|S|A| INIT | -// +-+-+-+-+-------+ -// ~ |Q~ if O==1 +// |Z|S|A| INIT | +// +-+-+-+---------+ +// | version | // +---------------+ -// | v_maj | v_min | if A==0 -- Protocol Version VMaj.VMin -// +-------+-------+ -// ~ whatami ~ -- Client, Router, Peer or a combination of them +// |zid_len|x|x|wai| (#)(*) +// +-------+-+-+---+ +// ~ [u8] ~ -- ZenohID of the sender of the INIT message // +---------------+ -// ~ zenoh_id ~ -- PID of the sender of the INIT message +// |x|x|kid|rid|fsn| \ -- SN/ID resolution (+) +// +---------------+ | if Flag(S)==1 +// | u16 | | -- Batch Size ($) +// | | / // +---------------+ -// ~ sn_resolution ~ if S==1(*) -- Otherwise 2^28 is assumed(**) +// ~ ~ -- if Flag(A)==1 -- Cookie // +---------------+ -// ~ cookie ~ if A==1 +// ~ [InitExts] ~ -- if Flag(Z)==1 // +---------------+ // -// (*) if A==0 and S==0 then 2^28 is assumed. -// if A==1 and S==0 then the agreed resolution is the one communicated by the initiator. +// If A==1 and S==0 then size parameters are (ie. S flag) are accepted. // -// - if Q==1 then the initiator/responder supports QoS. +// (*) WhatAmI. It indicates the role of the zenoh node sending the INIT +// message. +// The valid WhatAmI values are: +// - 0b00: Router +// - 0b01: Peer +// - 0b10: Client +// - 0b11: Reserved +// +// (#) ZID length. It indicates how many bytes are used for the ZenohID bytes. +// A ZenohID is minimum 1 byte and maximum 16 bytes. Therefore, the actual +// lenght is computed as: +// real_zid_len := 1 + zid_len +// +// (+) Sequence Number/ID resolution. It indicates the resolution and +// consequently the wire overhead +// of various SN and ID in Zenoh. +// - fsn: frame/fragment sequence number resolution. Used in Frame/Fragment +// messages. +// - rid: request ID resolution. Used in Request/Response messages. +// - kid: key expression ID resolution. Used in Push/Request/Response +// messages. The valid SN/ID resolution values are: +// - 0b00: 8 bits +// - 0b01: 16 bits +// - 0b10: 32 bits +// - 0b11: 64 bits +// +// ($) Batch Size. It indicates the maximum size of a batch the sender of the // typedef struct { _z_bytes_t _zid; _z_bytes_t _cookie; - _z_zint_t _options; - _z_zint_t _sn_resolution; z_whatami_t _whatami; + uint16_t _batch_size; + uint8_t _key_id_res; + uint8_t _req_id_res; + uint8_t _seq_num_res; uint8_t _version; } _z_t_msg_init_t; void _z_t_msg_clear_init(_z_t_msg_init_t *msg); @@ -1010,12 +1056,10 @@ void _z_t_msg_clear(_z_transport_message_t *msg); /*------------------ Builders ------------------*/ _z_transport_message_t _z_t_msg_make_scout(z_what_t what, _z_bytes_t zid); _z_transport_message_t _z_t_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _z_locator_array_t locators); -_z_transport_message_t _z_t_msg_make_join(uint8_t version, z_whatami_t whatami, _z_zint_t lease, - _z_zint_t sn_resolution, _z_bytes_t zid, _z_conduit_sn_list_t next_sns); -_z_transport_message_t _z_t_msg_make_init_syn(uint8_t version, z_whatami_t whatami, _z_zint_t sn_resolution, - _z_bytes_t zid, _Bool is_qos); -_z_transport_message_t _z_t_msg_make_init_ack(uint8_t version, z_whatami_t whatami, _z_zint_t sn_resolution, - _z_bytes_t zid, _z_bytes_t cookie, _Bool is_qos); +_z_transport_message_t _z_t_msg_make_join(uint8_t version, z_whatami_t whatami, _z_zint_t lease, _z_zint_t seq_num_res, + _z_bytes_t zid, _z_conduit_sn_list_t next_sns); +_z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_bytes_t zid); +_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_bytes_t zid, _z_bytes_t cookie); _z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie); _z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial_sn); _z_transport_message_t _z_t_msg_make_close(uint8_t reason, _z_bytes_t zid, _Bool link_only); diff --git a/include/zenoh-pico/transport/transport.h b/include/zenoh-pico/transport/transport.h index b764657bb..8d1439122 100644 --- a/include/zenoh-pico/transport/transport.h +++ b/include/zenoh-pico/transport/transport.h @@ -31,8 +31,8 @@ typedef struct { _z_bytes_t _remote_addr; // SN numbers - _z_zint_t _sn_resolution; - _z_zint_t _sn_resolution_half; + _z_zint_t _seq_num_res; + _z_zint_t _seq_num_res_half; volatile _z_zint_t _lease; volatile _z_zint_t _next_lease; @@ -69,8 +69,8 @@ typedef struct { _z_bytes_t _remote_zid; // SN numbers - _z_zint_t _sn_resolution; - _z_zint_t _sn_resolution_half; + _z_zint_t _seq_num_res; + _z_zint_t _seq_num_res_half; _z_zint_t _sn_tx_reliable; _z_zint_t _sn_tx_best_effort; _z_zint_t _sn_rx_reliable; @@ -110,8 +110,8 @@ typedef struct { _z_zbuf_t _zbuf; // SN initial numbers - _z_zint_t _sn_resolution; - _z_zint_t _sn_resolution_half; + _z_zint_t _seq_num_res; + _z_zint_t _seq_num_res_half; _z_zint_t _sn_tx_reliable; _z_zint_t _sn_tx_best_effort; volatile _z_zint_t _lease; @@ -146,7 +146,10 @@ _Z_LIST_DEFINE(_z_transport, _z_transport_t) typedef struct { _z_bytes_t _remote_zid; - _z_zint_t _sn_resolution; + uint8_t _key_id_res; + uint8_t _req_id_res; + uint8_t _seq_num_res; + uint16_t _batch_size; _z_zint_t _initial_sn_rx; _z_zint_t _initial_sn_tx; _z_zint_t _lease; @@ -155,7 +158,7 @@ typedef struct { } _z_transport_unicast_establish_param_t; typedef struct { - _z_zint_t _sn_resolution; + _z_zint_t _seq_num_res; _z_zint_t _initial_sn_tx; _Bool _is_qos; } _z_transport_multicast_establish_param_t; diff --git a/include/zenoh-pico/transport/utils.h b/include/zenoh-pico/transport/utils.h index 98b201f6c..99aaee7bc 100644 --- a/include/zenoh-pico/transport/utils.h +++ b/include/zenoh-pico/transport/utils.h @@ -21,10 +21,10 @@ #include "zenoh-pico/protocol/msg.h" /*------------------ SN helpers ------------------*/ -_Bool _z_sn_precedes(const _z_zint_t sn_resolution_half, const _z_zint_t sn_left, const _z_zint_t sn_right); -_z_zint_t _z_sn_increment(const _z_zint_t sn_resolution, const _z_zint_t sn); -_z_zint_t _z_sn_decrement(const _z_zint_t sn_resolution, const _z_zint_t sn); +_Bool _z_sn_precedes(const _z_zint_t seq_num_res_half, const _z_zint_t sn_left, const _z_zint_t sn_right); +_z_zint_t _z_sn_increment(const _z_zint_t seq_num_res, const _z_zint_t sn); +_z_zint_t _z_sn_decrement(const _z_zint_t seq_num_res, const _z_zint_t sn); void _z_conduit_sn_list_copy(_z_conduit_sn_list_t *dst, const _z_conduit_sn_list_t *src); -void _z_conduit_sn_list_decrement(const _z_zint_t sn_resolution, _z_conduit_sn_list_t *sns); +void _z_conduit_sn_list_decrement(const _z_zint_t seq_num_res, _z_conduit_sn_list_t *sns); #endif /* ZENOH_PICO_TRANSPORT_UTILS_H */ diff --git a/include/zenoh-pico/utils/math.h b/include/zenoh-pico/utils/math.h new file mode 100644 index 000000000..2db655770 --- /dev/null +++ b/include/zenoh-pico/utils/math.h @@ -0,0 +1,27 @@ +// +// 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, +// + +#include +#include + +/** + * Compute the maximum representable value. + * + * Parameters: + * n_bytes: Number of bytes used to represent the value. + * + * Returns: + * The maximum representable value. + */ +size_t _z_max_value(uint8_t n_bytes); diff --git a/include/zenoh-pico/utils/uuid.h b/include/zenoh-pico/utils/uuid.h index 32f620950..b0e487235 100644 --- a/include/zenoh-pico/utils/uuid.h +++ b/include/zenoh-pico/utils/uuid.h @@ -12,8 +12,6 @@ // ZettaScale Zenoh Team, // -#include -#include #include /** diff --git a/src/protocol/codec.c b/src/protocol/codec.c index e7ca8ec91..0797e07fa 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -15,6 +15,7 @@ #include "zenoh-pico/protocol/codec.h" #include "zenoh-pico/utils/logging.h" +#include "zenoh-pico/utils/result.h" /*------------------ period ------------------*/ int8_t _z_period_encode(_z_wbuf_t *buf, const _z_period_t *tp) { @@ -112,6 +113,28 @@ int8_t _z_uint8_decode(uint8_t *u8, _z_zbuf_t *zbf) { return ret; } +int8_t _z_uint16_encode(_z_wbuf_t *wbf, uint16_t u16) { + int8_t ret = _Z_RES_OK; + + ret |= _z_wbuf_write(wbf, ((u16 >> 8) & 0xFF)); + ret |= _z_wbuf_write(wbf, (u16 & 0xFF)); + + return ret; +} + +int8_t _z_uint16_decode(uint16_t *u16, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + *u16 = 0; + + uint8_t u8; + ret |= _z_uint8_decode(&u8, zbf); + *u16 |= u8 << 8; + ret |= _z_uint8_decode(&u8, zbf); + *u16 |= u8; + + return ret; +} + int8_t _z_uint64_encode(_z_wbuf_t *wbf, uint64_t u64) { uint64_t lv = u64; diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 0d5489146..389e62c6d 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -456,10 +456,6 @@ _z_transport_message_t _z_t_msg_make_scout(z_what_t what, _z_bytes_t zid) { msg._body._scout._what = what; msg._body._scout._zid = zid; - if (_z_bytes_is_empty(&zid) == false) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_I); - } - msg._attachment = NULL; msg._extensions = _z_msg_ext_vec_make(0); @@ -477,12 +473,12 @@ void _z_t_msg_clear_scout(_z_t_msg_scout_t *msg) { _z_bytes_clear(&msg->_zid); } /*------------------ Hello Message ------------------*/ _z_transport_message_t _z_t_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _z_locator_array_t locators) { _z_transport_message_t msg; + msg._header = _Z_MID_HELLO; msg._body._hello._whatami = whatami; msg._body._hello._zid = zid; msg._body._hello._locators = locators; - msg._header = _Z_MID_HELLO; if (whatami != Z_WHATAMI_ROUTER) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_W); } @@ -511,8 +507,8 @@ void _z_t_msg_clear_hello(_z_t_msg_hello_t *msg) { } /*------------------ Join Message ------------------*/ -_z_transport_message_t _z_t_msg_make_join(uint8_t version, z_whatami_t whatami, _z_zint_t lease, - _z_zint_t sn_resolution, _z_bytes_t zid, _z_conduit_sn_list_t next_sns) { +_z_transport_message_t _z_t_msg_make_join(uint8_t version, z_whatami_t whatami, _z_zint_t lease, _z_zint_t seq_num_res, + _z_bytes_t zid, _z_conduit_sn_list_t next_sns) { _z_transport_message_t msg; msg._body._join._options = 0; @@ -522,7 +518,7 @@ _z_transport_message_t _z_t_msg_make_join(uint8_t version, z_whatami_t whatami, msg._body._join._version = version; msg._body._join._whatami = whatami; msg._body._join._lease = lease; - msg._body._join._sn_resolution = sn_resolution; + msg._body._join._seq_num_res = seq_num_res; msg._body._join._next_sns = next_sns; msg._body._join._zid = zid; @@ -530,7 +526,7 @@ _z_transport_message_t _z_t_msg_make_join(uint8_t version, z_whatami_t whatami, if ((lease % 1000) == 0) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_T1); } - if (sn_resolution != Z_SN_RESOLUTION) { + if (seq_num_res != Z_SN_RESOLUTION) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_S); } if (msg._body._join._options != 0) { @@ -548,7 +544,7 @@ void _z_t_msg_copy_join(_z_t_msg_join_t *clone, _z_t_msg_join_t *msg) { clone->_version = msg->_version; clone->_whatami = msg->_whatami; clone->_lease = msg->_lease; - clone->_sn_resolution = msg->_sn_resolution; + clone->_seq_num_res = msg->_seq_num_res; clone->_next_sns = msg->_next_sns; _z_bytes_copy(&clone->_zid, &msg->_zid); } @@ -556,26 +552,24 @@ void _z_t_msg_copy_join(_z_t_msg_join_t *clone, _z_t_msg_join_t *msg) { void _z_t_msg_clear_join(_z_t_msg_join_t *msg) { _z_bytes_clear(&msg->_zid); } /*------------------ Init Message ------------------*/ -_z_transport_message_t _z_t_msg_make_init_syn(uint8_t version, z_whatami_t whatami, _z_zint_t sn_resolution, - _z_bytes_t zid, _Bool is_qos) { +_z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_bytes_t zid) { _z_transport_message_t msg; + msg._header = _Z_MID_INIT; - msg._body._init._options = 0; - if (is_qos == true) { - _Z_SET_FLAG(msg._body._init._options, _Z_OPT_INIT_QOS); - } - msg._body._init._version = version; + msg._body._init._version = Z_PROTO_VERSION; msg._body._init._whatami = whatami; - msg._body._init._sn_resolution = sn_resolution; msg._body._init._zid = zid; + msg._body._init._seq_num_res = Z_SN_RESOLUTION; + msg._body._init._key_id_res = Z_KID_RESOLUTION; + msg._body._init._req_id_res = Z_REQ_RESOLUTION; + msg._body._init._batch_size = Z_BATCH_SIZE; _z_bytes_reset(&msg._body._init._cookie); - msg._header = _Z_MID_INIT; - if (sn_resolution != Z_SN_RESOLUTION) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_S); - } - if (msg._body._init._options != 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_O); + if ((msg._body._init._batch_size != _Z_DEFAULT_BATCH_SIZE) || + (msg._body._init._seq_num_res != _Z_DEFAULT_SIZET_SIZE) || + (msg._body._init._key_id_res != _Z_DEFAULT_SIZET_SIZE) || + (msg._body._init._req_id_res != _Z_DEFAULT_SIZET_SIZE)) { + _Z_SET_FLAG(msg._header, _Z_FLAG_INIT_S); } msg._attachment = NULL; @@ -584,27 +578,25 @@ _z_transport_message_t _z_t_msg_make_init_syn(uint8_t version, z_whatami_t whata return msg; } -_z_transport_message_t _z_t_msg_make_init_ack(uint8_t version, z_whatami_t whatami, _z_zint_t sn_resolution, - _z_bytes_t zid, _z_bytes_t cookie, _Bool is_qos) { +_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_bytes_t zid, _z_bytes_t cookie) { _z_transport_message_t msg; + msg._header = _Z_MID_INIT; + _Z_SET_FLAG(msg._header, _Z_FLAG_INIT_A); - msg._body._init._options = 0; - if (is_qos == true) { - _Z_SET_FLAG(msg._body._init._options, _Z_OPT_INIT_QOS); - } - msg._body._init._version = version; + msg._body._init._version = Z_PROTO_VERSION; msg._body._init._whatami = whatami; - msg._body._init._sn_resolution = sn_resolution; msg._body._init._zid = zid; + msg._body._init._seq_num_res = Z_SN_RESOLUTION; + msg._body._init._key_id_res = Z_KID_RESOLUTION; + msg._body._init._req_id_res = Z_REQ_RESOLUTION; + msg._body._init._batch_size = Z_BATCH_SIZE; msg._body._init._cookie = cookie; - msg._header = _Z_MID_INIT; - _Z_SET_FLAG(msg._header, _Z_FLAG_T_A); - if (sn_resolution != Z_SN_RESOLUTION) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_S); - } - if (msg._body._init._options != 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_O); + if ((msg._body._init._batch_size != _Z_DEFAULT_BATCH_SIZE) || + (msg._body._init._seq_num_res != _Z_DEFAULT_SIZET_SIZE) || + (msg._body._init._key_id_res != _Z_DEFAULT_SIZET_SIZE) || + (msg._body._init._req_id_res != _Z_DEFAULT_SIZET_SIZE)) { + _Z_SET_FLAG(msg._header, _Z_FLAG_INIT_S); } msg._attachment = NULL; @@ -614,10 +606,12 @@ _z_transport_message_t _z_t_msg_make_init_ack(uint8_t version, z_whatami_t whata } void _z_t_msg_copy_init(_z_t_msg_init_t *clone, _z_t_msg_init_t *msg) { - clone->_options = msg->_options; clone->_version = msg->_version; clone->_whatami = msg->_whatami; - clone->_sn_resolution = msg->_sn_resolution; + clone->_seq_num_res = msg->_seq_num_res; + clone->_key_id_res = msg->_key_id_res; + clone->_req_id_res = msg->_req_id_res; + clone->_batch_size = msg->_batch_size; _z_bytes_copy(&clone->_zid, &msg->_zid); _z_bytes_copy(&clone->_cookie, &msg->_cookie); } diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 9b0e4571b..66eda2d9a 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1096,7 +1096,7 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg } if (_Z_HAS_FLAG(header, _Z_FLAG_T_S) == true) { - _Z_EC(_z_zint_encode(wbf, msg->_sn_resolution)) + _Z_EC(_z_zint_encode(wbf, msg->_seq_num_res)) } if (_Z_HAS_FLAG(msg->_options, _Z_OPT_JOIN_QOS) == true) { for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { @@ -1128,9 +1128,9 @@ int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { msg->_lease = msg->_lease * 1000; } if (_Z_HAS_FLAG(header, _Z_FLAG_T_S) == true) { - ret |= _z_zint_decode(&msg->_sn_resolution, zbf); + ret |= _z_zint_decode(&msg->_seq_num_res, zbf); } else { - msg->_sn_resolution = Z_SN_RESOLUTION; + msg->_seq_num_res = Z_SN_RESOLUTION; } if (_Z_HAS_FLAG(msg->_options, _Z_OPT_JOIN_QOS) == true) { msg->_next_sns._is_qos = true; @@ -1156,18 +1156,24 @@ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg _Z_DEBUG("Encoding _Z_MID_INIT\n"); int8_t ret = _Z_RES_OK; - if (_Z_HAS_FLAG(header, _Z_FLAG_T_O) == true) { - _Z_EC(_z_zint_encode(wbf, msg->_options)) - } - if (_Z_HAS_FLAG(header, _Z_FLAG_T_A) == false) { - _Z_EC(_z_wbuf_write(wbf, msg->_version)) - } - _Z_EC(_z_enum_encode(wbf, msg->_whatami)) - _Z_EC(_z_bytes_encode(wbf, &msg->_zid)) - if (_Z_HAS_FLAG(header, _Z_FLAG_T_S) == true) { - _Z_EC(_z_zint_encode(wbf, msg->_sn_resolution)) + _Z_EC(_z_wbuf_write(wbf, msg->_version)) + + uint8_t cbyte = 0; + cbyte |= (msg->_whatami & 0x03); + cbyte |= ((msg->_zid.len - 1) & 0x0F) << 4; // TODO[protocol]: check if ZID > 0 && <= 16 + _Z_EC(_z_uint8_encode(wbf, cbyte)) + _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) + + if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { + cbyte = 0; + cbyte |= ((msg->_seq_num_res - 1) & 0x03); + cbyte |= (((msg->_req_id_res - 1) & 0x03) << 2); + cbyte |= (((msg->_key_id_res - 1) & 0x03) << 4); + _Z_EC(_z_uint8_encode(wbf, cbyte)) + _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) } - if (_Z_HAS_FLAG(header, _Z_FLAG_T_A) == true) { + + if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_A) == true) { _Z_EC(_z_bytes_encode(wbf, &msg->_cookie)) } @@ -1178,24 +1184,29 @@ int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_INIT\n"); int8_t ret = _Z_RES_OK; - if (_Z_HAS_FLAG(header, _Z_FLAG_T_O) == true) { - ret |= _z_zint_decode(&msg->_options, zbf); - } else { - msg->_options = 0; - } - if (_Z_HAS_FLAG(header, _Z_FLAG_T_A) == false) { - ret |= _z_uint8_decode(&msg->_version, zbf); - } else { - msg->_version = Z_PROTO_VERSION; - } - ret |= _z_enum_decode((int *)&msg->_whatami, zbf); - ret |= _z_bytes_decode(&msg->_zid, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_S) == true) { - ret |= _z_zint_decode(&msg->_sn_resolution, zbf); + ret |= _z_uint8_decode(&msg->_version, zbf); + + uint8_t cbyte = 0; + ret |= _z_uint8_decode(&cbyte, zbf); + msg->_whatami = cbyte & 0x03; + msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; + ret |= _z_bytes_val_decode(&msg->_zid, zbf); + + if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { + cbyte = 0; + ret |= _z_uint8_decode(&cbyte, zbf); + msg->_seq_num_res = (cbyte & 0x03) + 1; + msg->_req_id_res = ((cbyte >> 2) & 0x03) + 1; + msg->_key_id_res = ((cbyte >> 4) & 0x03) + 1; + ret |= _z_uint16_decode(&msg->_batch_size, zbf); } else { - msg->_sn_resolution = Z_SN_RESOLUTION; + msg->_seq_num_res = _Z_DEFAULT_SIZET_SIZE; + msg->_req_id_res = _Z_DEFAULT_SIZET_SIZE; + msg->_key_id_res = _Z_DEFAULT_SIZET_SIZE; + msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; } - if (_Z_HAS_FLAG(header, _Z_FLAG_T_A) == true) { + + if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_A) == true) { ret |= _z_bytes_decode(&msg->_cookie, zbf); } else { msg->_cookie = _z_bytes_empty(); diff --git a/src/session/scout.c b/src/session/scout.c index a5cc31cbe..243a0c614 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -48,7 +48,7 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig err = _z_link_send_wbuf(&zl, wbf); if (err == _Z_RES_OK) { // The receiving buffer - _z_zbuf_t zbf = _z_zbuf_make(Z_BATCH_SIZE_RX); + _z_zbuf_t zbf = _z_zbuf_make(Z_BATCH_SIZE); z_time_t start = z_time_now(); while (z_time_elapsed_ms(&start) < period) { @@ -122,12 +122,12 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig return ret; } -_z_hello_list_t *_z_scout_inner(const z_what_t what, const _z_bytes_t zid, const char *locator, - const uint32_t timeout, const _Bool exit_on_first) { +_z_hello_list_t *_z_scout_inner(const z_what_t what, const _z_bytes_t zid, const char *locator, const uint32_t timeout, + const _Bool exit_on_first) { _z_hello_list_t *ret = NULL; // Create the buffer to serialize the scout message on - _z_wbuf_t wbf = _z_wbuf_make(Z_BATCH_SIZE_TX, false); + _z_wbuf_t wbf = _z_wbuf_make(Z_BATCH_SIZE, false); // Create and encode the scout message _z_transport_message_t scout = _z_t_msg_make_scout(what, zid); diff --git a/src/transport/common/rx.c b/src/transport/common/rx.c index 99a8c1ab3..e56a4cfec 100644 --- a/src/transport/common/rx.c +++ b/src/transport/common/rx.c @@ -23,7 +23,7 @@ int8_t _z_link_recv_t_msg(_z_transport_message_t *t_msg, const _z_link_t *zl) { uint8_t ret = _Z_RES_OK; // Create and prepare the buffer - _z_zbuf_t zbf = _z_zbuf_make(Z_BATCH_SIZE_RX); + _z_zbuf_t zbf = _z_zbuf_make(Z_BATCH_SIZE); _z_zbuf_reset(&zbf); if (_Z_LINK_IS_STREAMED(zl->_capabilities) == true) { diff --git a/src/transport/common/tx.c b/src/transport/common/tx.c index 1116fad15..b95034791 100644 --- a/src/transport/common/tx.c +++ b/src/transport/common/tx.c @@ -116,7 +116,7 @@ int8_t _z_link_send_t_msg(const _z_link_t *zl, const _z_transport_message_t *t_m int8_t ret = _Z_RES_OK; // Create and prepare the buffer to serialize the message on - uint16_t mtu = (zl->_mtu < Z_BATCH_SIZE_TX) ? zl->_mtu : Z_BATCH_SIZE_TX; + uint16_t mtu = (zl->_mtu < Z_BATCH_SIZE) ? zl->_mtu : Z_BATCH_SIZE; _z_wbuf_t wbf = _z_wbuf_make(mtu, false); if (_Z_LINK_IS_STREAMED(zl->_capabilities) == true) { for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) { diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index fadaa7021..60ed663db 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -143,14 +143,14 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t entry->_remote_addr = _z_bytes_duplicate(addr); entry->_remote_zid = _z_bytes_duplicate(&t_msg->_body._join._zid); if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_S) == true) { - entry->_sn_resolution = t_msg->_body._join._sn_resolution; + entry->_seq_num_res = t_msg->_body._join._seq_num_res; } else { - entry->_sn_resolution = Z_SN_RESOLUTION; + entry->_seq_num_res = Z_SN_RESOLUTION; } - entry->_sn_resolution_half = entry->_sn_resolution / 2; + entry->_seq_num_res_half = entry->_seq_num_res / 2; _z_conduit_sn_list_copy(&entry->_sn_rx_sns, &t_msg->_body._join._next_sns); - _z_conduit_sn_list_decrement(entry->_sn_resolution, &entry->_sn_rx_sns); + _z_conduit_sn_list_decrement(entry->_seq_num_res, &entry->_sn_rx_sns); #if Z_DYNAMIC_MEMORY_ALLOCATION == 1 entry->_dbuf_reliable = _z_wbuf_make(0, true); @@ -175,14 +175,14 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t // Check if the sn resolution remains the same if ((_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_S) == true) && - (entry->_sn_resolution != t_msg->_body._join._sn_resolution)) { + (entry->_seq_num_res != t_msg->_body._join._seq_num_res)) { _z_transport_peer_entry_list_drop_filter(ztm->_peers, _z_transport_peer_entry_eq, entry); break; } // Update SNs _z_conduit_sn_list_copy(&entry->_sn_rx_sns, &t_msg->_body._join._next_sns); - _z_conduit_sn_list_decrement(entry->_sn_resolution, &entry->_sn_rx_sns); + _z_conduit_sn_list_decrement(entry->_seq_num_res, &entry->_sn_rx_sns); // Update lease time (set as ms during) entry->_lease = t_msg->_body._join._lease; @@ -245,7 +245,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_R) == true) { // @TODO: amend once reliability is in place. For the time being only // monothonic SNs are ensured - if (_z_sn_precedes(entry->_sn_resolution_half, entry->_sn_rx_sns._val._plain._reliable, + if (_z_sn_precedes(entry->_seq_num_res_half, entry->_sn_rx_sns._val._plain._reliable, t_msg->_body._frame._sn) == true) { entry->_sn_rx_sns._val._plain._reliable = t_msg->_body._frame._sn; } else { @@ -254,7 +254,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t break; } } else { - if (_z_sn_precedes(entry->_sn_resolution_half, entry->_sn_rx_sns._val._plain._best_effort, + if (_z_sn_precedes(entry->_seq_num_res_half, entry->_sn_rx_sns._val._plain._best_effort, t_msg->_body._frame._sn) == true) { entry->_sn_rx_sns._val._plain._best_effort = t_msg->_body._frame._sn; } else { diff --git a/src/transport/multicast/link/tx.c b/src/transport/multicast/link/tx.c index a84ab8a10..885bd37c0 100644 --- a/src/transport/multicast/link/tx.c +++ b/src/transport/multicast/link/tx.c @@ -30,10 +30,10 @@ _z_zint_t __unsafe_z_multicast_get_sn(_z_transport_multicast_t *ztm, z_reliabili _z_zint_t sn; if (reliability == Z_RELIABILITY_RELIABLE) { sn = ztm->_sn_tx_reliable; - ztm->_sn_tx_reliable = _z_sn_increment(ztm->_sn_resolution, ztm->_sn_tx_reliable); + ztm->_sn_tx_reliable = _z_sn_increment(ztm->_seq_num_res, ztm->_sn_tx_reliable); } else { sn = ztm->_sn_tx_best_effort; - ztm->_sn_tx_best_effort = _z_sn_increment(ztm->_sn_resolution, ztm->_sn_tx_best_effort); + ztm->_sn_tx_best_effort = _z_sn_increment(ztm->_seq_num_res, ztm->_sn_tx_best_effort); } return sn; } diff --git a/src/transport/peer_entry.c b/src/transport/peer_entry.c index 7d5c2f84a..84884d89a 100644 --- a/src/transport/peer_entry.c +++ b/src/transport/peer_entry.c @@ -27,8 +27,8 @@ void _z_transport_peer_entry_copy(_z_transport_peer_entry_t *dst, const _z_trans _z_wbuf_copy(&dst->_dbuf_reliable, &src->_dbuf_reliable); _z_wbuf_copy(&dst->_dbuf_best_effort, &src->_dbuf_best_effort); - dst->_sn_resolution = src->_sn_resolution; - dst->_sn_resolution_half = src->_sn_resolution_half; + dst->_seq_num_res = src->_seq_num_res; + dst->_seq_num_res_half = src->_seq_num_res_half; _z_conduit_sn_list_copy(&dst->_sn_rx_sns, &src->_sn_rx_sns); dst->_lease = src->_lease; diff --git a/src/transport/transport.c b/src/transport/transport.c index 0abf6e2a5..d5b164b85 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -22,6 +22,7 @@ #include "zenoh-pico/transport/link/tx.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" +#include "zenoh-pico/utils/math.h" #if Z_UNICAST_TRANSPORT == 1 int8_t _z_unicast_send_close(_z_transport_unicast_t *ztu, uint8_t reason, _Bool link_only) { @@ -92,9 +93,9 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic // Initialize the read and write buffers if (ret == _Z_RES_OK) { - uint16_t mtu = (zl->_mtu < Z_BATCH_SIZE_TX) ? zl->_mtu : Z_BATCH_SIZE_TX; + uint16_t mtu = (zl->_mtu < Z_BATCH_SIZE) ? zl->_mtu : Z_BATCH_SIZE; zt->_transport._unicast._wbuf = _z_wbuf_make(mtu, false); - zt->_transport._unicast._zbuf = _z_zbuf_make(Z_BATCH_SIZE_RX); + zt->_transport._unicast._zbuf = _z_zbuf_make(Z_BATCH_SIZE); // Initialize the defragmentation buffers size_t dbuf_size = 0; @@ -108,7 +109,7 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic // Clean up the buffers if one of them failed to be allocated if ((_z_wbuf_capacity(&zt->_transport._unicast._wbuf) != mtu) || - (_z_zbuf_capacity(&zt->_transport._unicast._zbuf) != Z_BATCH_SIZE_RX) || + (_z_zbuf_capacity(&zt->_transport._unicast._zbuf) != Z_BATCH_SIZE) || (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_reliable) != dbuf_size) || (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_best_effort) != dbuf_size)) { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; @@ -127,8 +128,8 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic if (ret == _Z_RES_OK) { // Set default SN resolution - zt->_transport._unicast._sn_resolution = param->_sn_resolution; - zt->_transport._unicast._sn_resolution_half = param->_sn_resolution / 2; + zt->_transport._unicast._seq_num_res = _z_max_value(param->_seq_num_res); + zt->_transport._unicast._seq_num_res_half = zt->_transport._unicast._seq_num_res / 2; // The initial SN at TX side zt->_transport._unicast._sn_tx_reliable = param->_initial_sn_tx; @@ -191,13 +192,13 @@ int8_t _z_transport_multicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_mu // Initialize the read and write buffers if (ret == _Z_RES_OK) { - uint16_t mtu = (zl->_mtu < Z_BATCH_SIZE_TX) ? zl->_mtu : Z_BATCH_SIZE_TX; + uint16_t mtu = (zl->_mtu < Z_BATCH_SIZE) ? zl->_mtu : Z_BATCH_SIZE; zt->_transport._multicast._wbuf = _z_wbuf_make(mtu, false); - zt->_transport._multicast._zbuf = _z_zbuf_make(Z_BATCH_SIZE_RX); + zt->_transport._multicast._zbuf = _z_zbuf_make(Z_BATCH_SIZE); // Clean up the buffers if one of them failed to be allocated if ((_z_wbuf_capacity(&zt->_transport._multicast._wbuf) != mtu) || - (_z_zbuf_capacity(&zt->_transport._multicast._zbuf) != Z_BATCH_SIZE_RX)) { + (_z_zbuf_capacity(&zt->_transport._multicast._zbuf) != Z_BATCH_SIZE)) { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; #if Z_MULTI_THREAD == 1 @@ -213,8 +214,8 @@ int8_t _z_transport_multicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_mu if (ret == _Z_RES_OK) { // Set default SN resolution - zt->_transport._multicast._sn_resolution = param->_sn_resolution; - zt->_transport._multicast._sn_resolution_half = param->_sn_resolution / 2; + zt->_transport._multicast._seq_num_res = param->_seq_num_res; + zt->_transport._multicast._seq_num_res_half = param->_seq_num_res / 2; // The initial SN at TX side zt->_transport._multicast._sn_tx_reliable = param->_initial_sn_tx; @@ -249,15 +250,9 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * const _z_bytes_t *local_zid) { int8_t ret = _Z_RES_OK; - // Build the open message - uint8_t version = Z_PROTO_VERSION; - z_whatami_t whatami = Z_WHATAMI_CLIENT; - _z_zint_t sn_resolution = Z_SN_RESOLUTION; - _Bool is_qos = false; - _z_bytes_t zid = _z_bytes_wrap(local_zid->start, local_zid->len); - _z_transport_message_t ism = _z_t_msg_make_init_syn(version, whatami, sn_resolution, zid, is_qos); - param->_sn_resolution = ism._body._init._sn_resolution; // The announced sn resolution + _z_transport_message_t ism = _z_t_msg_make_init_syn(Z_WHATAMI_CLIENT, zid); + param->_seq_num_res = ism._body._init._seq_num_res; // The announced sn resolution // Encode and send the message _Z_INFO("Sending Z_INIT(Syn)\n"); @@ -267,16 +262,34 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * _z_transport_message_t iam; ret = _z_link_recv_t_msg(&iam, zl); if (ret == _Z_RES_OK) { - if ((_Z_MID(iam._header) == _Z_MID_INIT) && (_Z_HAS_FLAG(iam._header, _Z_FLAG_T_A) == true)) { + if ((_Z_MID(iam._header) == _Z_MID_INIT) && (_Z_HAS_FLAG(iam._header, _Z_FLAG_INIT_A) == true)) { _Z_INFO("Received Z_INIT(Ack)\n"); // Handle SN resolution option if present - if (_Z_HAS_FLAG(iam._header, _Z_FLAG_T_S) == true) { - // The resolution in the InitAck must be less or equal than the resolution in the InitSyn, + if (_Z_HAS_FLAG(iam._header, _Z_FLAG_INIT_S) == true) { + // Any of the size parameters in the InitAck must be less or equal than the one in the InitSyn, // otherwise the InitAck message is considered invalid and it should be treated as a // CLOSE message with L==0 by the Initiating Peer -- the recipient of the InitAck message. - if (iam._body._init._sn_resolution <= param->_sn_resolution) { - param->_sn_resolution = iam._body._init._sn_resolution; + if (iam._body._init._seq_num_res <= param->_seq_num_res) { + param->_seq_num_res = iam._body._init._seq_num_res; + } else { + ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; + } + + if (iam._body._init._key_id_res <= param->_key_id_res) { + param->_seq_num_res = iam._body._init._seq_num_res; + } else { + ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; + } + + if (iam._body._init._req_id_res <= param->_req_id_res) { + param->_seq_num_res = iam._body._init._seq_num_res; + } else { + ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; + } + + if (iam._body._init._batch_size <= param->_batch_size) { + param->_seq_num_res = iam._body._init._seq_num_res; } else { ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; } @@ -285,7 +298,7 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * if (ret == _Z_RES_OK) { // The initial SN at TX side z_random_fill(¶m->_initial_sn_tx, sizeof(param->_initial_sn_tx)); - param->_initial_sn_tx = param->_initial_sn_tx % param->_sn_resolution; + param->_initial_sn_tx = param->_initial_sn_tx % param->_seq_num_res; // Initialize the Local and Remote Peer IDs _z_bytes_copy(¶m->_remote_zid, &iam._body._init._zid); @@ -313,7 +326,7 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * // The initial SN at RX side. Initialize the session as we had already received // a message with a SN equal to initial_sn - 1. param->_initial_sn_rx = - _z_sn_decrement(param->_sn_resolution, oam._body._open._initial_sn); + _z_sn_decrement(param->_seq_num_res, oam._body._open._initial_sn); } else { ret = _Z_ERR_MESSAGE_UNEXPECTED; } @@ -369,7 +382,7 @@ int8_t _z_transport_multicast_open_peer(_z_transport_multicast_establish_param_t param->_is_qos = false; // FIXME: make transport aware of qos configuration param->_initial_sn_tx = 0; - param->_sn_resolution = Z_SN_RESOLUTION; + param->_seq_num_res = Z_SN_RESOLUTION; // Explicitly send a JOIN message upon startup // FIXME: make transport aware of qos configuration @@ -380,7 +393,7 @@ int8_t _z_transport_multicast_open_peer(_z_transport_multicast_establish_param_t _z_bytes_t zid = _z_bytes_wrap(local_zid->start, local_zid->len); _z_transport_message_t jsm = - _z_t_msg_make_join(Z_PROTO_VERSION, Z_WHATAMI_PEER, Z_TRANSPORT_LEASE, param->_sn_resolution, zid, next_sns); + _z_t_msg_make_join(Z_PROTO_VERSION, Z_WHATAMI_PEER, Z_TRANSPORT_LEASE, param->_seq_num_res, zid, next_sns); // Encode and send the message _Z_INFO("Sending Z_JOIN message\n"); diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index 83073b2b7..271d63218 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -136,7 +136,7 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_R) == true) { // @TODO: amend once reliability is in place. For the time being only // monothonic SNs are ensured - if (_z_sn_precedes(ztu->_sn_resolution_half, ztu->_sn_rx_reliable, t_msg->_body._frame._sn) == true) { + if (_z_sn_precedes(ztu->_seq_num_res_half, ztu->_sn_rx_reliable, t_msg->_body._frame._sn) == true) { ztu->_sn_rx_reliable = t_msg->_body._frame._sn; } else { _z_wbuf_clear(&ztu->_dbuf_reliable); @@ -144,8 +144,7 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans break; } } else { - if (_z_sn_precedes(ztu->_sn_resolution_half, ztu->_sn_rx_best_effort, t_msg->_body._frame._sn) == - true) { + if (_z_sn_precedes(ztu->_seq_num_res_half, ztu->_sn_rx_best_effort, t_msg->_body._frame._sn) == true) { ztu->_sn_rx_best_effort = t_msg->_body._frame._sn; } else { _z_wbuf_clear(&ztu->_dbuf_best_effort); diff --git a/src/transport/unicast/link/tx.c b/src/transport/unicast/link/tx.c index 394be48cb..1a940c4ee 100644 --- a/src/transport/unicast/link/tx.c +++ b/src/transport/unicast/link/tx.c @@ -30,10 +30,10 @@ _z_zint_t __unsafe_z_unicast_get_sn(_z_transport_unicast_t *ztu, z_reliability_t _z_zint_t sn; if (reliability == Z_RELIABILITY_RELIABLE) { sn = ztu->_sn_tx_reliable; - ztu->_sn_tx_reliable = _z_sn_increment(ztu->_sn_resolution, ztu->_sn_tx_reliable); + ztu->_sn_tx_reliable = _z_sn_increment(ztu->_seq_num_res, ztu->_sn_tx_reliable); } else { sn = ztu->_sn_tx_best_effort; - ztu->_sn_tx_best_effort = _z_sn_increment(ztu->_sn_resolution, ztu->_sn_tx_best_effort); + ztu->_sn_tx_best_effort = _z_sn_increment(ztu->_seq_num_res, ztu->_sn_tx_best_effort); } return sn; } diff --git a/src/transport/utils.c b/src/transport/utils.c index b5d4b0bcd..dcb259a2e 100644 --- a/src/transport/utils.c +++ b/src/transport/utils.c @@ -14,25 +14,25 @@ #include "zenoh-pico/transport/utils.h" -_Bool _z_sn_precedes(const _z_zint_t sn_resolution_half, const _z_zint_t sn_left, const _z_zint_t sn_right) { +_Bool _z_sn_precedes(const _z_zint_t seq_num_res_half, const _z_zint_t sn_left, const _z_zint_t sn_right) { _Bool ret = false; if (sn_right > sn_left) { - ret = ((sn_right - sn_left) <= sn_resolution_half); + ret = ((sn_right - sn_left) <= seq_num_res_half); } else { - ret = ((sn_left - sn_right) > sn_resolution_half); + ret = ((sn_left - sn_right) > seq_num_res_half); } return ret; } -_z_zint_t _z_sn_increment(const _z_zint_t sn_resolution, const _z_zint_t sn) { return (sn + 1) % sn_resolution; } +_z_zint_t _z_sn_increment(const _z_zint_t seq_num_res, const _z_zint_t sn) { return (sn + 1) % seq_num_res; } -_z_zint_t _z_sn_decrement(const _z_zint_t sn_resolution, const _z_zint_t sn) { +_z_zint_t _z_sn_decrement(const _z_zint_t seq_num_res, const _z_zint_t sn) { _z_zint_t ret = 0; if (sn == 0) { - ret = sn_resolution - 1; + ret = seq_num_res - 1; } else { ret = sn - 1; } @@ -53,14 +53,14 @@ void _z_conduit_sn_list_copy(_z_conduit_sn_list_t *dst, const _z_conduit_sn_list } } -void _z_conduit_sn_list_decrement(const _z_zint_t sn_resolution, _z_conduit_sn_list_t *sns) { +void _z_conduit_sn_list_decrement(const _z_zint_t seq_num_res, _z_conduit_sn_list_t *sns) { if (sns->_is_qos == false) { - sns->_val._plain._best_effort = _z_sn_decrement(sn_resolution, sns->_val._plain._best_effort); - sns->_val._plain._reliable = _z_sn_decrement(sn_resolution, sns->_val._plain._reliable); + sns->_val._plain._best_effort = _z_sn_decrement(seq_num_res, sns->_val._plain._best_effort); + sns->_val._plain._reliable = _z_sn_decrement(seq_num_res, sns->_val._plain._reliable); } else { for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { - sns->_val._qos[i]._best_effort = _z_sn_decrement(sn_resolution, sns->_val._qos[i]._best_effort); - sns->_val._qos[i]._best_effort = _z_sn_decrement(sn_resolution, sns->_val._qos[i]._reliable); + sns->_val._qos[i]._best_effort = _z_sn_decrement(seq_num_res, sns->_val._qos[i]._best_effort); + sns->_val._qos[i]._best_effort = _z_sn_decrement(seq_num_res, sns->_val._qos[i]._reliable); } } } \ No newline at end of file diff --git a/src/utils/math.c b/src/utils/math.c new file mode 100644 index 000000000..633ac9813 --- /dev/null +++ b/src/utils/math.c @@ -0,0 +1,25 @@ +// +// 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, +// + +#include "zenoh-pico/utils/math.h" + +size_t _z_max_value(uint8_t n_bytes) { + size_t max = 0; + + for (uint8_t i = 0; i < (n_bytes * 8); i++) { + max *= 2; + } + + return max; +} diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 9b523e682..e6c27b83e 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -114,7 +114,9 @@ void print_transport_message_type(uint8_t header) { /*=============================*/ _Bool gen_bool(void) { return z_random_u8() % 2; } -uint8_t gen_uint8(void) { return z_random_u8() % 255; } +uint8_t gen_uint8(void) { return z_random_u8(); } + +uint8_t gen_uint16(void) { return z_random_u16(); } _z_zint_t gen_zint(void) { _z_zint_t ret = 0; @@ -1944,7 +1946,7 @@ _z_transport_message_t gen_join_message(void) { z_whatami_t whatami = 0x04 >> (gen_uint8() % 3); _z_bytes_t zid = gen_bytes(16); _z_zint_t lease = gen_bool() ? gen_zint() * 1000 : gen_zint(); - _z_zint_t sn_resolution = gen_bool() ? gen_zint() : Z_SN_RESOLUTION; + _z_zint_t seq_num_res = gen_bool() ? gen_zint() : Z_SN_RESOLUTION; _z_conduit_sn_list_t next_sns; if (gen_bool()) { @@ -1959,7 +1961,7 @@ _z_transport_message_t gen_join_message(void) { next_sns._val._plain._best_effort = gen_zint(); } - return _z_t_msg_make_join(version, whatami, lease, sn_resolution, zid, next_sns); + return _z_t_msg_make_join(version, whatami, lease, seq_num_res, zid, next_sns); } void assert_eq_join_message(_z_t_msg_join_t *left, _z_t_msg_join_t *right, uint8_t header) { @@ -1980,8 +1982,8 @@ void assert_eq_join_message(_z_t_msg_join_t *left, _z_t_msg_join_t *right, uint8 printf("\n"); if (_Z_HAS_FLAG(header, _Z_FLAG_T_S) == true) { - printf(" SN Resolution (%zu:%zu)", left->_sn_resolution, right->_sn_resolution); - assert(left->_sn_resolution == right->_sn_resolution); + printf(" SN Resolution (%zu:%zu)", left->_seq_num_res, right->_seq_num_res); + assert(left->_seq_num_res == right->_seq_num_res); printf("\n"); } @@ -2045,23 +2047,39 @@ void join_message(void) { /*------------------ Init Message ------------------*/ _z_transport_message_t gen_init_message(void) { - uint8_t version = gen_uint8(); - z_whatami_t whatami = 0x04 >> (gen_uint8() % 3); - _z_zint_t sn_resolution = gen_bool() ? gen_zint() : Z_SN_RESOLUTION; + z_whatami_t whatami = (gen_uint8() % 2) + 1; _z_bytes_t zid = gen_bytes(16); - _Bool is_qos = gen_bool(); + _z_transport_message_t t_msg; if (gen_bool()) { - return _z_t_msg_make_init_syn(version, whatami, sn_resolution, zid, is_qos); + t_msg = _z_t_msg_make_init_syn(whatami, zid); } else { _z_bytes_t cookie = gen_bytes(64); - return _z_t_msg_make_init_ack(version, whatami, sn_resolution, zid, cookie, is_qos); + t_msg = _z_t_msg_make_init_ack(whatami, zid, cookie); + } + + if (gen_bool()) { + t_msg._body._init._batch_size = gen_uint16(); + } + + if (gen_bool()) { + t_msg._body._init._seq_num_res = (gen_uint8() % 4) + 1; + } + + if (gen_bool()) { + t_msg._body._init._key_id_res = (gen_uint8() % 4) + 1; + } + + if (gen_bool()) { + t_msg._body._init._req_id_res = (gen_uint8() % 4) + 1; } + + return t_msg; } void assert_eq_init_message(_z_t_msg_init_t *left, _z_t_msg_init_t *right, uint8_t header) { - printf(" Options (%zu:%zu)", left->_options, right->_options); - assert(left->_options == right->_options); + printf(" Version (%u:%u)", left->_version, right->_version); + assert(left->_version == right->_version); printf("\n"); printf(" WhatAmI (%u:%u)", left->_whatami, right->_whatami); @@ -2072,20 +2090,28 @@ void assert_eq_init_message(_z_t_msg_init_t *left, _z_t_msg_init_t *right, uint8 assert_eq_uint8_array(&left->_zid, &right->_zid); printf("\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_S) == true) { - printf(" SN Resolution (%zu:%zu)", left->_sn_resolution, right->_sn_resolution); - assert(left->_sn_resolution == right->_sn_resolution); + if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { + printf(" SN Resolution (%hhu:%hhu)", left->_seq_num_res, right->_seq_num_res); + assert(left->_seq_num_res == right->_seq_num_res); + printf("\n"); + + printf(" Request ID Resolution (%hhu:%hhu)", left->_req_id_res, right->_req_id_res); + assert(left->_req_id_res == right->_req_id_res); + printf("\n"); + + printf(" KeyExpr ID Resolution (%hhu:%hhu)", left->_key_id_res, right->_key_id_res); + assert(left->_key_id_res == right->_key_id_res); + printf("\n"); + + printf(" Batch Size (%hu:%hu)", left->_batch_size, right->_batch_size); + assert(left->_batch_size == right->_batch_size); printf("\n"); } - if (_Z_HAS_FLAG(header, _Z_FLAG_T_A) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_A) == true) { printf(" "); assert_eq_uint8_array(&left->_cookie, &right->_cookie); printf("\n"); - } else { - printf(" Version (%u:%u)", left->_version, right->_version); - assert(left->_version == right->_version); - printf("\n"); } } @@ -2497,10 +2523,12 @@ void frame_message(void) { _z_transport_message_t gen_transport_message(_Bool can_be_fragment) { _z_transport_message_t e_tm; - uint8_t mids[] = { - _Z_MID_SCOUT, _Z_MID_HELLO, _Z_MID_JOIN, _Z_MID_INIT, _Z_MID_OPEN, _Z_MID_CLOSE, - _Z_MID_SYNC, _Z_MID_ACK_NACK, _Z_MID_KEEP_ALIVE, _Z_MID_PING_PONG, _Z_MID_FRAME, - }; + // uint8_t mids[] = { + // _Z_MID_SCOUT, _Z_MID_HELLO, _Z_MID_JOIN, _Z_MID_INIT, _Z_MID_OPEN, _Z_MID_CLOSE, + // _Z_MID_SYNC, _Z_MID_ACK_NACK, _Z_MID_KEEP_ALIVE, _Z_MID_PING_PONG, _Z_MID_FRAME, + // }; + // TODO[protocol]: To be removed + uint8_t mids[] = {_Z_MID_SCOUT}; uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); switch (mids[i]) { @@ -2801,8 +2829,8 @@ void fragmentation(void) { // Fragment the message _Bool is_reliable = gen_bool(); // Fix the sn resoulution to 128 in such a way it requires only 1 byte for encoding - size_t sn_resolution = 128; - size_t sn = sn_resolution - 1; + size_t seq_num_res = 128; + size_t sn = seq_num_res - 1; printf(" - Message serialized\n"); print_wbuf(&fbf); @@ -2813,7 +2841,7 @@ void fragmentation(void) { z_wbuf_prepare(&wbf); // Get the fragment sequence number - sn = (sn + 1) % sn_resolution; + sn = (sn + 1) % seq_num_res; size_t written = _z_wbuf_len(&fbf); int8_t res = z_serialize_zenoh_fragment(&wbf, &fbf, is_reliable, sn); From f978b61e9f7acce92e9c3bc0710e7d92c6bb2952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 16 Feb 2023 15:26:03 +0100 Subject: [PATCH 017/181] Refactor Open message --- include/zenoh-pico/protocol/msg.h | 30 +++++++++++++++++++++++------- src/protocol/msg.c | 10 +++++----- src/protocol/msgcodec.c | 11 +++++++---- src/transport/transport.c | 2 +- tests/z_msgcodec_test.c | 2 +- 5 files changed, 37 insertions(+), 18 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index e7f84caef..1b0d2f448 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -90,6 +90,14 @@ #define _Z_FLAG_INIT_S 0x40 // 1 << 6 #define _Z_FLAG_INIT_Z 0x80 // 1 << 7 +// Open message flags: +// A Ack if A==1 then the message is an acknowledgment (aka OpenAck), otherwise OpenSyn +// T Lease period if T==1 then the lease period is in seconds else in milliseconds +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_OPEN_A 0x20 // 1 << 5 +#define _Z_FLAG_OPEN_T 0x40 // 1 << 6 +#define _Z_FLAG_OPEN_Z 0x80 // 1 << 7 + /* Transport message flags */ #define _Z_FLAG_T_A 0x20 // 1 << 5 | Ack if A==1 then the message is an acknowledgment #define _Z_FLAG_T_C 0x40 // 1 << 6 | Count if C==1 then number of unacknowledged messages is present @@ -840,19 +848,27 @@ void _z_t_msg_clear_init(_z_t_msg_init_t *msg); // // The OPEN message is sent on a link to finally open an initialized session with the peer. // +// Flags: +// - A Ack if A==1 then the message is an acknowledgment (aka OpenAck), otherwise OpenSyn +// - T Lease period if T==1 then the lease period is in seconds else in milliseconds +// - Z Extensions if Z==1 then Zenoh extensions are present +// // 7 6 5 4 3 2 1 0 // +-+-+-+-+-+-+-+-+ -// |X|T|A| OPEN | -// +-+-+-+-+-------+ -// ~ lease_period ~ -- Lease period of the sender of the OPEN message(*) +// |Z|T|A| OPEN | +// +-+-+-+---------+ +// % lease % -- Lease period of the sender of the OPEN message +// +---------------+ +// % initial_sn % -- Initial SN proposed by the sender of the OPEN(*) // +---------------+ -// ~ initial_sn ~ -- Initial SN proposed by the sender of the OPEN(**) +// ~ ~ if Flag(A)==0 (**) -- Cookie // +---------------+ -// ~ cookie ~ if A==0(*) +// ~ [OpenExts] ~ if Flag(Z)==1 // +---------------+ // -// (*) if T==1 then the lease period is expressed in seconds, otherwise in milliseconds -// (**) the cookie MUST be the same received in the INIT message with A==1 from the corresponding peer +// (*) The initial sequence number MUST be compatible with the sequence number resolution agreed in the +// [`super::InitSyn`]-[`super::InitAck`] message exchange +// (**) The cookie MUST be the same received in the [`super::InitAck`]from the corresponding zenoh node // typedef struct { _z_zint_t _lease; diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 389e62c6d..c5c9150da 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -624,14 +624,14 @@ void _z_t_msg_clear_init(_z_t_msg_init_t *msg) { /*------------------ Open Message ------------------*/ _z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie) { _z_transport_message_t msg; + msg._header = _Z_MID_OPEN; msg._body._open._lease = lease; msg._body._open._initial_sn = initial_sn; msg._body._open._cookie = cookie; - msg._header = _Z_MID_OPEN; if ((lease % 1000) == 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_T2); + _Z_SET_FLAG(msg._header, _Z_FLAG_OPEN_T); } msg._attachment = NULL; @@ -642,15 +642,15 @@ _z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial _z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial_sn) { _z_transport_message_t msg; + msg._header = _Z_MID_OPEN; + _Z_SET_FLAG(msg._header, _Z_FLAG_OPEN_A); msg._body._open._lease = lease; msg._body._open._initial_sn = initial_sn; _z_bytes_reset(&msg._body._open._cookie); - msg._header = _Z_MID_OPEN; - _Z_SET_FLAG(msg._header, _Z_FLAG_T_A); if ((lease % 1000) == 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_T2); + _Z_SET_FLAG(msg._header, _Z_FLAG_OPEN_T); } msg._attachment = NULL; diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 66eda2d9a..fc97149b6 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1224,14 +1224,15 @@ int8_t _z_open_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_open_t *msg int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_OPEN\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_T2) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_OPEN_T) == true) { _Z_EC(_z_zint_encode(wbf, msg->_lease / 1000)) } else { _Z_EC(_z_zint_encode(wbf, msg->_lease)) } _Z_EC(_z_zint_encode(wbf, msg->_initial_sn)) - if (_Z_HAS_FLAG(header, _Z_FLAG_T_A) == false) { + + if (_Z_HAS_FLAG(header, _Z_FLAG_OPEN_A) == false) { _Z_EC(_z_bytes_encode(wbf, &msg->_cookie)) } @@ -1243,11 +1244,13 @@ int8_t _z_open_decode_na(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; ret |= _z_zint_decode(&msg->_lease, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_T2) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_OPEN_T) == true) { msg->_lease = msg->_lease * 1000; } + ret |= _z_zint_decode(&msg->_initial_sn, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_A) == false) { + + if (_Z_HAS_FLAG(header, _Z_FLAG_OPEN_A) == false) { ret |= _z_bytes_decode(&msg->_cookie, zbf); } else { msg->_cookie = _z_bytes_empty(); diff --git a/src/transport/transport.c b/src/transport/transport.c index d5b164b85..ecc526fa8 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -319,7 +319,7 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * ret = _z_link_recv_t_msg(&oam, zl); if (ret == _Z_RES_OK) { if ((_Z_MID(oam._header) == _Z_MID_OPEN) && - (_Z_HAS_FLAG(oam._header, _Z_FLAG_T_A) == true)) { + (_Z_HAS_FLAG(oam._header, _Z_FLAG_OPEN_A) == true)) { _Z_INFO("Received Z_OPEN(Ack)\n"); param->_lease = oam._body._open._lease; // The session lease diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index e6c27b83e..ea473f252 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -2167,7 +2167,7 @@ void assert_eq_open_message(_z_t_msg_open_t *left, _z_t_msg_open_t *right, uint8 assert(left->_initial_sn == right->_initial_sn); printf("\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_A) == false) { + if (_Z_HAS_FLAG(header, _Z_FLAG_OPEN_A) == false) { printf(" "); assert_eq_uint8_array(&left->_cookie, &right->_cookie); printf("\n"); From 2ad3a138c7460e2e56c193dac150b20c49a67efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 16 Feb 2023 15:30:23 +0100 Subject: [PATCH 018/181] Refactor Keep Alive --- include/zenoh-pico/protocol/msg.h | 15 ++++++++++----- src/protocol/msg.c | 13 ++++--------- src/protocol/msgcodec.c | 20 +++++++++----------- src/transport/multicast/link/task/lease.c | 5 +---- src/transport/unicast/link/task/lease.c | 6 +----- tests/z_msgcodec_test.c | 14 ++++---------- 6 files changed, 29 insertions(+), 44 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 1b0d2f448..27a5333be 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -971,15 +971,20 @@ void _z_t_msg_clear_ack_nack(_z_t_msg_ack_nack_t *msg); // The KEEP_ALIVE message can be sent periodically to avoid the expiration of the session lease // period in case there are no messages to be sent. // +// Flags: +// - X: Reserved +// - X: Reserved +// - Z: Extensions If Z==1 then Zenoh extensions will follow. +// // 7 6 5 4 3 2 1 0 // +-+-+-+-+-+-+-+-+ -// |X|X|I| K_ALIVE | -// +-+-+-+-+-------+ -// ~ zenoh_id ~ if I==1 -- Peer ID of the KEEP_ALIVE sender. +// |Z|X|X| KALIVE | +// +-+-+-+---------+ +// ~ [KAliveExts] ~ if Flag(Z)==1 // +---------------+ // typedef struct { - _z_bytes_t _zid; + uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior } _z_t_msg_keep_alive_t; void _z_t_msg_clear_keep_alive(_z_t_msg_keep_alive_t *msg); @@ -1081,7 +1086,7 @@ _z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial _z_transport_message_t _z_t_msg_make_close(uint8_t reason, _z_bytes_t zid, _Bool link_only); _z_transport_message_t _z_t_msg_make_sync(_z_zint_t sn, _Bool is_reliable, _z_zint_t count); _z_transport_message_t _z_t_msg_make_ack_nack(_z_zint_t sn, _z_zint_t mask); -_z_transport_message_t _z_t_msg_make_keep_alive(_z_bytes_t zid); +_z_transport_message_t _z_t_msg_make_keep_alive(void); _z_transport_message_t _z_t_msg_make_ping(_z_zint_t hash); _z_transport_message_t _z_t_msg_make_pong(_z_zint_t hash); _z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_frame_payload_t payload, _Bool is_reliable, diff --git a/src/protocol/msg.c b/src/protocol/msg.c index c5c9150da..f6763ff9e 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -755,15 +755,9 @@ void _z_t_msg_clear_ack_nack(_z_t_msg_ack_nack_t *msg) { } /*------------------ Keep Alive Message ------------------*/ -_z_transport_message_t _z_t_msg_make_keep_alive(_z_bytes_t zid) { +_z_transport_message_t _z_t_msg_make_keep_alive(void) { _z_transport_message_t msg; - - msg._body._keep_alive._zid = zid; - msg._header = _Z_MID_KEEP_ALIVE; - if (_z_bytes_is_empty(&zid) == false) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_I); - } msg._attachment = NULL; msg._extensions = _z_msg_ext_vec_make(0); @@ -772,10 +766,11 @@ _z_transport_message_t _z_t_msg_make_keep_alive(_z_bytes_t zid) { } void _z_t_msg_copy_keep_alive(_z_t_msg_keep_alive_t *clone, _z_t_msg_keep_alive_t *msg) { - _z_bytes_copy(&clone->_zid, &msg->_zid); + (void)(clone); + (void)(msg); } -void _z_t_msg_clear_keep_alive(_z_t_msg_keep_alive_t *msg) { _z_bytes_clear(&msg->_zid); } +void _z_t_msg_clear_keep_alive(_z_t_msg_keep_alive_t *msg) { (void)(msg); } /*------------------ PingPong Messages ------------------*/ _z_transport_message_t _z_t_msg_make_ping(_z_zint_t hash) { diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index fc97149b6..a0b1bb891 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1360,25 +1360,23 @@ int8_t _z_ack_nack_decode(_z_t_msg_ack_nack_t *msg, _z_zbuf_t *zbf, uint8_t head /*------------------ Keep Alive Message ------------------*/ int8_t _z_keep_alive_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_keep_alive_t *msg) { + (void)(wbf); + (void)(header); + (void)(msg); + int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_KEEP_ALIVE\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_I) == true) { - _Z_EC(_z_bytes_encode(wbf, &msg->_zid)) - } - return ret; } int8_t _z_keep_alive_decode_na(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_KEEP_ALIVE\n"); - int8_t ret = _Z_RES_OK; + (void)(msg); + (void)(zbf); + (void)(header); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_I) == true) { - ret |= _z_bytes_decode(&msg->_zid, zbf); - } else { - msg->_zid = _z_bytes_empty(); - } + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Decoding _Z_MID_KEEP_ALIVE\n"); return ret; } diff --git a/src/transport/multicast/link/task/lease.c b/src/transport/multicast/link/task/lease.c index 5c3a4fef0..09e8ca2db 100644 --- a/src/transport/multicast/link/task/lease.c +++ b/src/transport/multicast/link/task/lease.c @@ -61,10 +61,7 @@ _z_zint_t _z_get_next_lease(_z_transport_peer_entry_list_t *peers) { int8_t _zp_multicast_send_keep_alive(_z_transport_multicast_t *ztm) { int8_t ret = _Z_RES_OK; - _z_bytes_t zid = _z_bytes_wrap(((_z_session_t *)ztm->_session)->_local_zid.start, - ((_z_session_t *)ztm->_session)->_local_zid.len); - _z_transport_message_t t_msg = _z_t_msg_make_keep_alive(zid); - + _z_transport_message_t t_msg = _z_t_msg_make_keep_alive(); ret = _z_multicast_send_t_msg(ztm, &t_msg); return ret; diff --git a/src/transport/unicast/link/task/lease.c b/src/transport/unicast/link/task/lease.c index d0c07cc3b..e30b91238 100644 --- a/src/transport/unicast/link/task/lease.c +++ b/src/transport/unicast/link/task/lease.c @@ -22,12 +22,8 @@ int8_t _zp_unicast_send_keep_alive(_z_transport_unicast_t *ztu) { int8_t ret = _Z_RES_OK; - _z_bytes_t zid; - _z_bytes_reset(&zid); // Do not send the PID on unicast links - - _z_transport_message_t t_msg = _z_t_msg_make_keep_alive(zid); + _z_transport_message_t t_msg = _z_t_msg_make_keep_alive(); ret = _z_unicast_send_t_msg(ztu, &t_msg); - // FIXME: double check why we dont clear t_msg return ret; } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index ea473f252..04582ae8a 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -2357,18 +2357,12 @@ void ack_nack_message(void) { } /*------------------ KeepAlive Message ------------------*/ -_z_transport_message_t gen_keep_alive_message(void) { - _z_bytes_t zid = gen_bool() ? gen_bytes(16) : gen_bytes(0); - - return _z_t_msg_make_keep_alive(zid); -} +_z_transport_message_t gen_keep_alive_message(void) { return _z_t_msg_make_keep_alive(); } void assert_eq_keep_alive_message(_z_t_msg_keep_alive_t *left, _z_t_msg_keep_alive_t *right, uint8_t header) { - if (_Z_HAS_FLAG(header, _Z_FLAG_T_I) == true) { - printf(" "); - assert_eq_uint8_array(&left->_zid, &right->_zid); - printf("\n"); - } + (void)(left); + (void)(right); + (void)(header); } void keep_alive_message(void) { From d8723c4329c5dfa545a49ad0900628701a1e2f1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 16 Feb 2023 15:36:34 +0100 Subject: [PATCH 019/181] Refactor Close --- include/zenoh-pico/protocol/msg.h | 26 ++++++++++++++++---------- src/protocol/msg.c | 20 ++++++-------------- src/protocol/msgcodec.c | 12 +++--------- src/transport/multicast/link/rx.c | 8 -------- src/transport/transport.c | 8 ++------ tests/z_msgcodec_test.c | 9 ++------- 6 files changed, 29 insertions(+), 54 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 27a5333be..bb509f24c 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -98,6 +98,12 @@ #define _Z_FLAG_OPEN_T 0x40 // 1 << 6 #define _Z_FLAG_OPEN_Z 0x80 // 1 << 7 +// Close message flags: +// S Session Close if S==1 Session close or S==0 Link close +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_CLOSE_S 0x20 // 1 << 5 +#define _Z_FLAG_CLOSE_Z 0x80 // 1 << 7 + /* Transport message flags */ #define _Z_FLAG_T_A 0x20 // 1 << 5 | Ack if A==1 then the message is an acknowledgment #define _Z_FLAG_T_C 0x40 // 1 << 6 | Count if C==1 then number of unacknowledged messages is present @@ -888,21 +894,21 @@ void _z_t_msg_clear_open(_z_t_msg_open_t *msg); // 1) in response to an OPEN message which is not accepted; // 2) at any time to arbitrarly close the session with the corresponding peer. // +// Flags: +// - S: Session Close if S==1 Session close or S==0 Link close +// - X: Reserved +// - Z: Extensions if Z==1 then zenoh extensions will follow. +// // 7 6 5 4 3 2 1 0 // +-+-+-+-+-+-+-+-+ -// |X|K|I| CLOSE | -// +-+-+-+-+-------+ -// ~ zenoh_id ~ if I==1 -- PID of the target peer. +// |Z|X|S| CLOSE | +// +-+-+-+---------+ +// | Reason | // +---------------+ -// | reason | +// ~ [CloseExts] ~ if Flag(Z)==1 // +---------------+ // -// - if K==0 then close the whole zenoh session. -// - if K==1 then close the transport link the CLOSE message was sent on (e.g., TCP socket) but -// keep the whole session open. NOTE: the session will be automatically closed when -// the session's lease period expires. typedef struct { - _z_bytes_t _zid; uint8_t _reason; } _z_t_msg_close_t; void _z_t_msg_clear_close(_z_t_msg_close_t *msg); @@ -1083,7 +1089,7 @@ _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_bytes_t zi _z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_bytes_t zid, _z_bytes_t cookie); _z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie); _z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial_sn); -_z_transport_message_t _z_t_msg_make_close(uint8_t reason, _z_bytes_t zid, _Bool link_only); +_z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only); _z_transport_message_t _z_t_msg_make_sync(_z_zint_t sn, _Bool is_reliable, _z_zint_t count); _z_transport_message_t _z_t_msg_make_ack_nack(_z_zint_t sn, _z_zint_t mask); _z_transport_message_t _z_t_msg_make_keep_alive(void); diff --git a/src/protocol/msg.c b/src/protocol/msg.c index f6763ff9e..4796ea7a8 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -668,18 +668,13 @@ void _z_t_msg_copy_open(_z_t_msg_open_t *clone, _z_t_msg_open_t *msg) { void _z_t_msg_clear_open(_z_t_msg_open_t *msg) { _z_bytes_clear(&msg->_cookie); } /*------------------ Close Message ------------------*/ -_z_transport_message_t _z_t_msg_make_close(uint8_t reason, _z_bytes_t zid, _Bool link_only) { +_z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only) { _z_transport_message_t msg; + msg._header = _Z_MID_CLOSE; msg._body._close._reason = reason; - msg._body._close._zid = zid; - - msg._header = _Z_MID_CLOSE; - if (_z_bytes_is_empty(&zid) == false) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_I); - } - if (link_only == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_K); + if (link_only == false) { + _Z_SET_FLAG(msg._header, _Z_FLAG_CLOSE_S); } msg._attachment = NULL; @@ -688,12 +683,9 @@ _z_transport_message_t _z_t_msg_make_close(uint8_t reason, _z_bytes_t zid, _Bool return msg; } -void _z_t_msg_copy_close(_z_t_msg_close_t *clone, _z_t_msg_close_t *msg) { - _z_bytes_copy(&clone->_zid, &msg->_zid); - clone->_reason = msg->_reason; -} +void _z_t_msg_copy_close(_z_t_msg_close_t *clone, _z_t_msg_close_t *msg) { clone->_reason = msg->_reason; } -void _z_t_msg_clear_close(_z_t_msg_close_t *msg) { _z_bytes_clear(&msg->_zid); } +void _z_t_msg_clear_close(_z_t_msg_close_t *msg) { (void)(msg); } /*------------------ Sync Message ------------------*/ _z_transport_message_t _z_t_msg_make_sync(_z_zint_t sn, _Bool is_reliable, _z_zint_t count) { diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index a0b1bb891..156aee7e0 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1265,26 +1265,20 @@ int8_t _z_open_decode(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header) { /*------------------ Close Message ------------------*/ int8_t _z_close_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_close_t *msg) { + (void)(header); int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_CLOSE\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_I) == true) { - _Z_EC(_z_bytes_encode(wbf, &msg->_zid)) - } ret |= _z_wbuf_write(wbf, msg->_reason); return ret; } int8_t _z_close_decode_na(_z_t_msg_close_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_CLOSE\n"); + (void)(header); int8_t ret = _Z_RES_OK; + _Z_DEBUG("Decoding _Z_MID_CLOSE\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_I) == true) { - ret |= _z_bytes_decode(&msg->_zid, zbf); - } else { - msg->_zid = _z_bytes_empty(); - } ret |= _z_uint8_decode(&msg->_reason, zbf); return ret; diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 60ed663db..f68af4577 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -196,14 +196,6 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t if (entry == NULL) { break; } - - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_I) == true) { - // Check if the Peer ID matches the remote address in the knonw peer list - if ((entry->_remote_zid.len != t_msg->_body._close._zid.len) || - (memcmp(entry->_remote_zid.start, t_msg->_body._close._zid.start, entry->_remote_zid.len) != 0)) { - break; - } - } ztm->_peers = _z_transport_peer_entry_list_drop_filter(ztm->_peers, _z_transport_peer_entry_eq, entry); break; diff --git a/src/transport/transport.c b/src/transport/transport.c index ecc526fa8..2908468fa 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -28,9 +28,7 @@ int8_t _z_unicast_send_close(_z_transport_unicast_t *ztu, uint8_t reason, _Bool link_only) { int8_t ret = _Z_RES_OK; - _z_bytes_t zid = _z_bytes_wrap(((_z_session_t *)ztu->_session)->_local_zid.start, - ((_z_session_t *)ztu->_session)->_local_zid.len); - _z_transport_message_t cm = _z_t_msg_make_close(reason, zid, link_only); + _z_transport_message_t cm = _z_t_msg_make_close(reason, link_only); ret = _z_unicast_send_t_msg(ztu, &cm); _z_t_msg_clear(&cm); @@ -43,9 +41,7 @@ int8_t _z_unicast_send_close(_z_transport_unicast_t *ztu, uint8_t reason, _Bool int8_t _z_multicast_send_close(_z_transport_multicast_t *ztm, uint8_t reason, _Bool link_only) { int8_t ret = _Z_RES_OK; - _z_bytes_t zid = _z_bytes_wrap(((_z_session_t *)ztm->_session)->_local_zid.start, - ((_z_session_t *)ztm->_session)->_local_zid.len); - _z_transport_message_t cm = _z_t_msg_make_close(reason, zid, link_only); + _z_transport_message_t cm = _z_t_msg_make_close(reason, link_only); ret = _z_multicast_send_t_msg(ztm, &cm); _z_t_msg_clear(&cm); diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 04582ae8a..8a8e9c429 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -2207,18 +2207,13 @@ void open_message(void) { /*------------------ Close Message ------------------*/ _z_transport_message_t gen_close_message(void) { uint8_t reason = gen_uint8(); - _z_bytes_t zid = gen_bool() ? gen_bytes(16) : gen_bytes(0); _Bool link_only = gen_bool(); - return _z_t_msg_make_close(reason, zid, link_only); + return _z_t_msg_make_close(reason, link_only); } void assert_eq_close_message(_z_t_msg_close_t *left, _z_t_msg_close_t *right, uint8_t header) { - if (_Z_HAS_FLAG(header, _Z_FLAG_T_I) == true) { - printf(" "); - assert_eq_uint8_array(&left->_zid, &right->_zid); - printf("\n"); - } + (void)(header); printf(" Reason (%u:%u)", left->_reason, right->_reason); assert(left->_reason == right->_reason); From b068a954ab2b26fd8ce267cd5b35144be4dfb132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 16 Feb 2023 16:11:45 +0100 Subject: [PATCH 020/181] Refactor Frame message --- include/zenoh-pico/protocol/msg.h | 50 ++-- include/zenoh-pico/transport/link/tx.h | 1 - src/protocol/msg.c | 55 +--- src/protocol/msgcodec.c | 65 ++--- src/transport/common/tx.c | 43 --- src/transport/multicast/link/rx.c | 60 +--- src/transport/multicast/link/tx.c | 34 +-- src/transport/unicast/link/rx.c | 62 +--- src/transport/unicast/link/tx.c | 34 +-- tests/z_msgcodec_test.c | 374 ++++++++++++------------- 10 files changed, 262 insertions(+), 516 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index bb509f24c..563a51024 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -98,6 +98,12 @@ #define _Z_FLAG_OPEN_T 0x40 // 1 << 6 #define _Z_FLAG_OPEN_Z 0x80 // 1 << 7 +// Frame message flags: +// R Reliable if R==1 it concerns the reliable channel, else the best-effort channel +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_FRAME_R 0x20 // 1 << 5 +#define _Z_FLAG_FRAME_Z 0x80 // 1 << 7 + // Close message flags: // S Session Close if S==1 Session close or S==0 Link close // Z Extensions if Z==1 then Zenoh extensions are present @@ -1022,40 +1028,29 @@ void _z_t_msg_clear_ping_pong(_z_t_msg_ping_pong_t *msg); // the boundary of the serialized messages. The length is encoded as little-endian. // In any case, the length of a message must not exceed 65_535 bytes. // +// Flags: +// - R: Reliable If R==1 it concerns the reliable channel, else the best-effort channel +// - X: Reserved +// - Z: Extensions If Z==1 then zenoh extensions will follow. +// // 7 6 5 4 3 2 1 0 // +-+-+-+-+-+-+-+-+ -// |E|F|R| FRAME | -// +-+-+-+-+-------+ -// ~ SN ~ +// |Z|X|R| FRAME | +// +-+-+-+---------+ +// % seq num % +// +---------------+ +// ~ [FrameExts] ~ if Flag(Z)==1 // +---------------+ -// ~ FramePayload ~ -- if F==1 then the payload is a fragment of a single Zenoh Message, a list of complete Zenoh -// Messages otherwise. +// ~ [NetworkMsg] ~ // +---------------+ // // - if R==1 then the FRAME is sent on the reliable channel, best-effort otherwise. -// - if F==1 then the FRAME is a fragment. -// - if E==1 then the FRAME is the last fragment. E==1 is valid iff F==1. -// -// NOTE: Only one bit would be sufficient to signal fragmentation in a IP-like fashion as follows: -// - if F==1 then this FRAME is a fragment and more fragment will follow; -// - if F==0 then the message is the last fragment if SN-1 had F==1, -// otherwise it's a non-fragmented message. -// However, this would require to always perform a two-steps de-serialization: first -// de-serialize the FRAME and then the Payload. This is due to the fact the F==0 is ambigous -// w.r.t. detecting if the FRAME is a fragment or not before SN re-ordering has occured. -// By using the F bit to only signal whether the FRAME is fragmented or not, it allows to -// de-serialize the payload in one single pass when F==0 since no re-ordering needs to take -// place at this stage. Then, the F bit is used to detect the last fragment during re-ordering. // -typedef union { - _z_payload_t _fragment; - _z_zenoh_message_vec_t _messages; -} _z_frame_payload_t; typedef struct { - _z_frame_payload_t _payload; + _z_zenoh_message_vec_t _messages; _z_zint_t _sn; } _z_t_msg_frame_t; -void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg, uint8_t header); +void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg); /*------------------ Transport Message ------------------*/ typedef union { @@ -1095,9 +1090,8 @@ _z_transport_message_t _z_t_msg_make_ack_nack(_z_zint_t sn, _z_zint_t mask); _z_transport_message_t _z_t_msg_make_keep_alive(void); _z_transport_message_t _z_t_msg_make_ping(_z_zint_t hash); _z_transport_message_t _z_t_msg_make_pong(_z_zint_t hash); -_z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_frame_payload_t payload, _Bool is_reliable, - _Bool is_fragment, _Bool is_final); -_z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable, _Bool is_fragment, _Bool is_final); +_z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_zenoh_message_vec_t messages, _Bool is_reliable); +_z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable); /*------------------ Copy ------------------*/ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg); @@ -1111,6 +1105,6 @@ void _z_t_msg_copy_sync(_z_t_msg_sync_t *clone, _z_t_msg_sync_t *msg); void _z_t_msg_copy_ack_nack(_z_t_msg_ack_nack_t *clone, _z_t_msg_ack_nack_t *msg); void _z_t_msg_copy_keep_alive(_z_t_msg_keep_alive_t *clone, _z_t_msg_keep_alive_t *msg); void _z_t_msg_copy_ping_pong(_z_t_msg_ping_pong_t *clone, _z_t_msg_ping_pong_t *msg); -void _z_t_msg_copy_frame(_z_t_msg_frame_t *clone, _z_t_msg_frame_t *msg, uint8_t header); +void _z_t_msg_copy_frame(_z_t_msg_frame_t *clone, _z_t_msg_frame_t *msg); #endif /* ZENOH_PICO_PROTOCOL_MSG_H */ diff --git a/include/zenoh-pico/transport/link/tx.h b/include/zenoh-pico/transport/link/tx.h index 8c5c073eb..3cca0d6dc 100644 --- a/include/zenoh-pico/transport/link/tx.h +++ b/include/zenoh-pico/transport/link/tx.h @@ -22,7 +22,6 @@ void __unsafe_z_prepare_wbuf(_z_wbuf_t *buf, _Bool is_streamed); void __unsafe_z_finalize_wbuf(_z_wbuf_t *buf, _Bool is_streamed); int8_t __unsafe_z_serialize_zenoh_fragment(_z_wbuf_t *dst, _z_wbuf_t *src, z_reliability_t reliability, size_t sn); -_z_transport_message_t _z_frame_header(z_reliability_t reliability, _Bool is_fragment, _Bool is_final, _z_zint_t sn); /*------------------ Transmission and Reception helpers ------------------*/ int8_t _z_unicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reliability_t reliability, diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 4796ea7a8..737a09d33 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -800,23 +800,13 @@ void _z_t_msg_clear_ping_pong(_z_t_msg_ping_pong_t *msg) { } /*------------------ Frame Message ------------------*/ -_z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable, _Bool is_fragment, _Bool is_final) { +_z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable) { _z_transport_message_t msg; + msg._header = _Z_MID_FRAME; msg._body._frame._sn = sn; - - // Reset payload content - (void)memset(&msg._body._frame._payload, 0, sizeof(_z_frame_payload_t)); - - msg._header = _Z_MID_FRAME; if (is_reliable == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_R); - } - if (is_fragment == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_F); - if (is_final == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_E); - } + _Z_SET_FLAG(msg._header, _Z_FLAG_FRAME_R); } msg._attachment = NULL; @@ -825,23 +815,10 @@ _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliabl return msg; } -_z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_frame_payload_t payload, _Bool is_reliable, - _Bool is_fragment, _Bool is_final) { - _z_transport_message_t msg; +_z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_zenoh_message_vec_t messages, _Bool is_reliable) { + _z_transport_message_t msg = _z_t_msg_make_frame_header(sn, is_reliable); - msg._body._frame._sn = sn; - msg._body._frame._payload = payload; - - msg._header = _Z_MID_FRAME; - if (is_reliable == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_R); - } - if (is_fragment == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_F); - if (is_final == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_E); - } - } + msg._body._frame._messages = messages; msg._attachment = NULL; msg._extensions = _z_msg_ext_vec_make(0); @@ -849,22 +826,12 @@ _z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_frame_payload_t payl return msg; } -void _z_t_msg_copy_frame(_z_t_msg_frame_t *clone, _z_t_msg_frame_t *msg, uint8_t header) { +void _z_t_msg_copy_frame(_z_t_msg_frame_t *clone, _z_t_msg_frame_t *msg) { clone->_sn = msg->_sn; - if (_Z_HAS_FLAG(header, _Z_FLAG_T_F) == true) { - _z_bytes_copy(&clone->_payload._fragment, &msg->_payload._fragment); - } else { - _z_zenoh_message_vec_copy(&clone->_payload._messages, &msg->_payload._messages); - } + _z_zenoh_message_vec_copy(&clone->_messages, &msg->_messages); } -void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg, uint8_t header) { - if (_Z_HAS_FLAG(header, _Z_FLAG_T_F) == true) { - _z_payload_clear(&msg->_payload._fragment); - } else { - _z_zenoh_message_vec_clear(&msg->_payload._messages); - } -} +void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg) { _z_zenoh_message_vec_clear(&msg->_messages); } /*------------------ Transport Message ------------------*/ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg) { @@ -915,7 +882,7 @@ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg) { } break; case _Z_MID_FRAME: { - _z_t_msg_copy_frame(&clone->_body._frame, &msg->_body._frame, clone->_header); + _z_t_msg_copy_frame(&clone->_body._frame, &msg->_body._frame); } break; default: { @@ -973,7 +940,7 @@ void _z_t_msg_clear(_z_transport_message_t *msg) { } break; case _Z_MID_FRAME: { - _z_t_msg_clear_frame(&msg->_body._frame, msg->_header); + _z_t_msg_clear_frame(&msg->_body._frame); } break; default: { diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 156aee7e0..ae37e2042 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1402,59 +1402,52 @@ int8_t _z_ping_pong_decode(_z_t_msg_ping_pong_t *msg, _z_zbuf_t *zbf) { return _ /*------------------ Frame Message ------------------*/ int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg) { + (void)(header); + int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_FRAME\n"); _Z_EC(_z_zint_encode(wbf, msg->_sn)) - if (_Z_HAS_FLAG(header, _Z_FLAG_T_F) == true) { - // Do not write the fragment as _z_bytes_t since the total frame length - // is eventually prepended to the frame. There is no need to encode the fragment length. - ret |= _z_wbuf_write_bytes(wbf, msg->_payload._fragment.start, 0, msg->_payload._fragment.len); - } else { - size_t len = _z_zenoh_message_vec_len(&msg->_payload._messages); - for (size_t i = 0; i < len; i++) { - _Z_EC(_z_zenoh_message_encode(wbf, _z_zenoh_message_vec_get(&msg->_payload._messages, i))) - } + size_t len = _z_zenoh_message_vec_len(&msg->_messages); + for (size_t i = 0; i < len; i++) { + _Z_EC(_z_zenoh_message_encode(wbf, _z_zenoh_message_vec_get(&msg->_messages, i))) } return ret; } int8_t _z_frame_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_FRAME\n"); + (void)(header); + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Decoding _Z_MID_FRAME\n"); ret |= _z_zint_decode(&msg->_sn, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_F) == true) { - msg->_payload._fragment = _z_bytes_wrap(_z_zbuf_get_rptr(zbf), _z_zbuf_len(zbf)); // All remaining bytes - _z_zbuf_set_rpos(zbf, _z_zbuf_get_wpos(zbf)); // Manually move the r_pos to w_pos, we have read it all - } else { - if (ret == _Z_RES_OK) { - msg->_payload._messages = _z_zenoh_message_vec_make(_ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE); - while (_z_zbuf_len(zbf) > 0) { - // Mark the reading position of the iobfer - size_t r_pos = _z_zbuf_get_rpos(zbf); - _z_zenoh_message_t *zm = (_z_zenoh_message_t *)z_malloc(sizeof(_z_zenoh_message_t)); - ret |= _z_zenoh_message_decode(zm, zbf); - if (ret == _Z_RES_OK) { - _z_zenoh_message_vec_append(&msg->_payload._messages, zm); - } else { - _z_zbuf_set_rpos(zbf, r_pos); // Restore the reading position of the iobfer - - // FIXME: Check for the return error, since not all of them means a decoding error - // in this particular case. As of now, we roll-back the reading position - // and return to the Zenoh transport-level decoder. - // https://github.com/eclipse-zenoh/zenoh-pico/pull/132#discussion_r1045593602 - if ((ret & _Z_ERR_MESSAGE_ZENOH_UNKNOWN) == _Z_ERR_MESSAGE_ZENOH_UNKNOWN) { - ret = _Z_RES_OK; - } - break; + if (ret == _Z_RES_OK) { + msg->_messages = _z_zenoh_message_vec_make(_ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE); + while (_z_zbuf_len(zbf) > 0) { + // Mark the reading position of the iobfer + size_t r_pos = _z_zbuf_get_rpos(zbf); + _z_zenoh_message_t *zm = (_z_zenoh_message_t *)z_malloc(sizeof(_z_zenoh_message_t)); + ret |= _z_zenoh_message_decode(zm, zbf); + if (ret == _Z_RES_OK) { + _z_zenoh_message_vec_append(&msg->_messages, zm); + } else { + _z_zbuf_set_rpos(zbf, r_pos); // Restore the reading position of the iobfer + + // FIXME: Check for the return error, since not all of them means a decoding error + // in this particular case. As of now, we roll-back the reading position + // and return to the Zenoh transport-level decoder. + // https://github.com/eclipse-zenoh/zenoh-pico/pull/132#discussion_r1045593602 + if ((ret & _Z_ERR_MESSAGE_ZENOH_UNKNOWN) == _Z_ERR_MESSAGE_ZENOH_UNKNOWN) { + ret = _Z_RES_OK; } + break; } - } else { - msg->_payload._messages = _z_zenoh_message_vec_make(0); } + } else { + msg->_messages = _z_zenoh_message_vec_make(0); } return ret; diff --git a/src/transport/common/tx.c b/src/transport/common/tx.c index b95034791..e1f8146d3 100644 --- a/src/transport/common/tx.c +++ b/src/transport/common/tx.c @@ -48,49 +48,6 @@ void __unsafe_z_finalize_wbuf(_z_wbuf_t *buf, _Bool is_streamed) { } } -_z_transport_message_t _z_frame_header(z_reliability_t reliability, _Bool is_fragment, _Bool is_final, _z_zint_t sn) { - // Create the frame session message that carries the zenoh message - _Bool is_reliable = reliability == Z_RELIABILITY_RELIABLE; - - _z_transport_message_t t_msg = _z_t_msg_make_frame_header(sn, is_reliable, is_fragment, is_final); - - return t_msg; -} - -/** - * This function is unsafe because it operates in potentially concurrent data. - * Make sure that the following mutexes are locked before calling this function: - * - ztu->mutex_tx - */ -int8_t __unsafe_z_serialize_zenoh_fragment(_z_wbuf_t *dst, _z_wbuf_t *src, z_reliability_t reliability, size_t sn) { - int8_t ret = _Z_RES_OK; - - // Assume first that this is not the final fragment - _Bool is_final = false; - do { - size_t w_pos = _z_wbuf_get_wpos(dst); // Mark the buffer for the writing operation - - _z_transport_message_t f_hdr = _z_frame_header(reliability, true, is_final, sn); - ret = _z_transport_message_encode(dst, &f_hdr); // Encode the frame header - if (ret == _Z_RES_OK) { - size_t space_left = _z_wbuf_space_left(dst); - size_t bytes_left = _z_wbuf_len(src); - - if ((is_final == false) && (bytes_left <= space_left)) { // Check if it is really the final fragment - _z_wbuf_set_wpos(dst, w_pos); // Revert the buffer - is_final = true; // It is really the finally fragment, reserialize the header - continue; - } - - size_t to_copy = (bytes_left <= space_left) ? bytes_left : space_left; // Compute bytes to write - ret = _z_wbuf_siphon(dst, src, to_copy); // Write the fragment - } - break; - } while (1); - - return ret; -} - int8_t _z_send_t_msg(_z_transport_t *zt, const _z_transport_message_t *t_msg) { int8_t ret = _Z_RES_OK; diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index f68af4577..88d833b05 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -234,7 +234,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t entry->_received = true; // Check if the SN is correct - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_R) == true) { + if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAME_R) == true) { // @TODO: amend once reliability is in place. For the time being only // monothonic SNs are ensured if (_z_sn_precedes(entry->_seq_num_res_half, entry->_sn_rx_sns._val._plain._reliable, @@ -256,59 +256,13 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t } } - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_F) == true) { - // Select the right defragmentation buffer - _z_wbuf_t *dbuf = (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_R) == true) ? &entry->_dbuf_reliable - : &entry->_dbuf_best_effort; - - _Bool drop = false; - if ((_z_wbuf_len(dbuf) + t_msg->_body._frame._payload._fragment.len) > Z_FRAG_MAX_SIZE) { - // Filling the wbuf capacity as a way to signling the last fragment to reset the dbuf - // Otherwise, last (smaller) fragments can be understood as a complete message - _z_wbuf_write_bytes(dbuf, t_msg->_body._frame._payload._fragment.start, 0, - _z_wbuf_space_left(dbuf)); - drop = true; - } else { - // Add the fragment to the defragmentation buffer - _z_wbuf_write_bytes(dbuf, t_msg->_body._frame._payload._fragment.start, 0, - t_msg->_body._frame._payload._fragment.len); - } - - // Check if this is the last fragment - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_E) == true) { - // Drop message if it is bigger the max buffer size - if (drop == true) { - _z_wbuf_reset(dbuf); - break; - } - - // Convert the defragmentation buffer into a decoding buffer - _z_zbuf_t zbf = _z_wbuf_to_zbuf(dbuf); - - // Decode the zenoh message - _z_zenoh_message_t zm; - ret = _z_zenoh_message_decode(&zm, &zbf); - if (ret == _Z_RES_OK) { - _z_handle_zenoh_message(ztm->_session, &zm); - - // Clear must be explicitly called for fragmented zenoh messages. - // Non-fragmented zenoh messages are released when their transport message is released. - _z_msg_clear(&zm); - } - - // Free the decoding buffer - _z_zbuf_clear(&zbf); - // Reset the defragmentation buffer - _z_wbuf_reset(dbuf); - } - } else { - // Handle all the zenoh message, one by one - size_t len = _z_vec_len(&t_msg->_body._frame._payload._messages); - for (size_t i = 0; i < len; i++) { - _z_handle_zenoh_message( - ztm->_session, (_z_zenoh_message_t *)_z_vec_get(&t_msg->_body._frame._payload._messages, i)); - } + // Handle all the zenoh message, one by one + size_t len = _z_vec_len(&t_msg->_body._frame._messages); + for (size_t i = 0; i < len; i++) { + _z_handle_zenoh_message(ztm->_session, + (_z_zenoh_message_t *)_z_vec_get(&t_msg->_body._frame._messages, i)); } + break; } diff --git a/src/transport/multicast/link/tx.c b/src/transport/multicast/link/tx.c index 885bd37c0..d5e306035 100644 --- a/src/transport/multicast/link/tx.c +++ b/src/transport/multicast/link/tx.c @@ -99,7 +99,7 @@ int8_t _z_multicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_re _z_zint_t sn = __unsafe_z_multicast_get_sn(ztm, reliability); // Get the next sequence number - _z_transport_message_t t_msg = _z_frame_header(reliability, 0, 0, sn); + _z_transport_message_t t_msg = _z_t_msg_make_frame_header(reliability, sn); ret = _z_transport_message_encode(&ztm->_wbuf, &t_msg); // Encode the frame header if (ret == _Z_RES_OK) { ret = _z_zenoh_message_encode(&ztm->_wbuf, z_msg); // Encode the zenoh message @@ -112,37 +112,7 @@ int8_t _z_multicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_re ztm->_transmitted = true; // Mark the session that we have transmitted data } } else { - // The message does not fit in the current batch, let's fragment it - // Create an expandable wbuf for fragmentation - _z_wbuf_t fbf = _z_wbuf_make(Z_IOSLICE_SIZE, true); - - ret = _z_zenoh_message_encode(&fbf, z_msg); // Encode the message on the expandable wbuf - if (ret == _Z_RES_OK) { - _Bool is_first = true; // Fragment and send the message - while (_z_wbuf_len(&fbf) > 0) { - if (is_first == false) { // Get the fragment sequence number - sn = __unsafe_z_multicast_get_sn(ztm, reliability); - } - is_first = false; - - // Clear the buffer for serialization - __unsafe_z_prepare_wbuf(&ztm->_wbuf, _Z_LINK_IS_STREAMED(ztm->_link._capabilities)); - - // Serialize one fragment - ret = __unsafe_z_serialize_zenoh_fragment(&ztm->_wbuf, &fbf, reliability, sn); - if (ret == _Z_RES_OK) { - // Write the message length in the reserved space if needed - __unsafe_z_finalize_wbuf(&ztm->_wbuf, _Z_LINK_IS_STREAMED(ztm->_link._capabilities)); - - ret = _z_link_send_wbuf(&ztm->_link, &ztm->_wbuf); // Send the wbuf on the socket - if (ret == _Z_RES_OK) { - ztm->_transmitted = true; // Mark the session that we have transmitted data - } - } - } - } - - _z_wbuf_clear(&fbf); // Free the fragmentation buffer memory + // TODO[protocol]: Fragmentation goes here } } diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index 271d63218..d376e90e2 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -133,7 +133,7 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans case _Z_MID_FRAME: { _Z_INFO("Received Z_FRAME message\n"); // Check if the SN is correct - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_R) == true) { + if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAME_R) == true) { // @TODO: amend once reliability is in place. For the time being only // monothonic SNs are ensured if (_z_sn_precedes(ztu->_seq_num_res_half, ztu->_sn_rx_reliable, t_msg->_body._frame._sn) == true) { @@ -153,61 +153,13 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans } } - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_F) == true) { - // Select the right defragmentation buffer - _z_wbuf_t *dbuf = - _Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_R) ? &ztu->_dbuf_reliable : &ztu->_dbuf_best_effort; - - _Bool drop = false; - if ((_z_wbuf_len(dbuf) + t_msg->_body._frame._payload._fragment.len) > Z_FRAG_MAX_SIZE) { - // Filling the wbuf capacity as a way to signling the last fragment to reset the dbuf - // Otherwise, last (smaller) fragments can be understood as a complete message - _z_wbuf_write_bytes(dbuf, t_msg->_body._frame._payload._fragment.start, 0, - _z_wbuf_space_left(dbuf)); - drop = true; - } else { - // Add the fragment to the defragmentation buffer - _z_wbuf_write_bytes(dbuf, t_msg->_body._frame._payload._fragment.start, 0, - t_msg->_body._frame._payload._fragment.len); - } - - // Check if this is the last fragment - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_E) == true) { - // Drop message if it is bigger the max buffer size - if (drop == true) { - _z_wbuf_reset(dbuf); - break; - } - - // Convert the defragmentation buffer into a decoding buffer - _z_zbuf_t zbf = _z_wbuf_to_zbuf(dbuf); - - // Decode the zenoh message - _z_zenoh_message_t zm; - int8_t ret = _z_zenoh_message_decode(&zm, &zbf); - if (ret == _Z_RES_OK) { - _z_handle_zenoh_message(ztu->_session, &zm); - - // Clear must be explicitly called for fragmented zenoh messages. - // Non-fragmented zenoh messages are released when their transport message is released. - _z_msg_clear(&zm); - } - - // Free the decoding buffer - _z_zbuf_clear(&zbf); - // Reset the defragmentation buffer - _z_wbuf_reset(dbuf); - } - - break; - } else { - // Handle all the zenoh message, one by one - size_t len = _z_vec_len(&t_msg->_body._frame._payload._messages); - for (size_t i = 0; i < len; i++) { - _z_handle_zenoh_message( - ztu->_session, (_z_zenoh_message_t *)_z_vec_get(&t_msg->_body._frame._payload._messages, i)); - } + // Handle all the zenoh message, one by one + size_t len = _z_vec_len(&t_msg->_body._frame._messages); + for (size_t i = 0; i < len; i++) { + _z_handle_zenoh_message(ztu->_session, + (_z_zenoh_message_t *)_z_vec_get(&t_msg->_body._frame._messages, i)); } + break; } diff --git a/src/transport/unicast/link/tx.c b/src/transport/unicast/link/tx.c index 1a940c4ee..00133d776 100644 --- a/src/transport/unicast/link/tx.c +++ b/src/transport/unicast/link/tx.c @@ -99,7 +99,7 @@ int8_t _z_unicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reli _z_zint_t sn = __unsafe_z_unicast_get_sn(ztu, reliability); // Get the next sequence number - _z_transport_message_t t_msg = _z_frame_header(reliability, 0, 0, sn); + _z_transport_message_t t_msg = _z_t_msg_make_frame_header(reliability, sn); ret = _z_transport_message_encode(&ztu->_wbuf, &t_msg); // Encode the frame header if (ret == _Z_RES_OK) { ret = _z_zenoh_message_encode(&ztu->_wbuf, z_msg); // Encode the zenoh message @@ -112,37 +112,7 @@ int8_t _z_unicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reli ztu->_transmitted = true; // Mark the session that we have transmitted data } } else { - // The message does not fit in the current batch, let's fragment it - // Create an expandable wbuf for fragmentation - _z_wbuf_t fbf = _z_wbuf_make(Z_IOSLICE_SIZE, true); - - ret = _z_zenoh_message_encode(&fbf, z_msg); // Encode the message on the expandable wbuf - if (ret == _Z_RES_OK) { - _Bool is_first = true; // Fragment and send the message - while (_z_wbuf_len(&fbf) > 0) { - if (is_first == false) { // Get the fragment sequence number - sn = __unsafe_z_unicast_get_sn(ztu, reliability); - } - is_first = false; - - // Clear the buffer for serialization - __unsafe_z_prepare_wbuf(&ztu->_wbuf, _Z_LINK_IS_STREAMED(ztu->_link._capabilities)); - - // Serialize one fragment - ret = __unsafe_z_serialize_zenoh_fragment(&ztu->_wbuf, &fbf, reliability, sn); - if (ret == _Z_RES_OK) { - // Write the message length in the reserved space if needed - __unsafe_z_finalize_wbuf(&ztu->_wbuf, _Z_LINK_IS_STREAMED(ztu->_link._capabilities)); - - ret = _z_link_send_wbuf(&ztu->_link, &ztu->_wbuf); // Send the wbuf on the socket - if (ret == _Z_RES_OK) { - ztu->_transmitted = true; // Mark the session that we have transmitted data - } - } - } - } - - _z_wbuf_clear(&fbf); // Free the fragmentation buffer memory + // TODO[protocol]: Fragmentation goes here } } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 8a8e9c429..cb360dab0 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -2435,47 +2435,37 @@ void ping_pong_message(void) { } /*------------------ Frame Message ------------------*/ -_z_transport_message_t gen_frame_message(_Bool can_be_fragment) { +_z_transport_message_t gen_frame_message(void) { _z_zint_t sn = gen_zint(); _Bool is_reliable = gen_bool(); - _Bool is_fragment = can_be_fragment ? gen_bool() : false; - _Bool is_final = is_fragment ? gen_bool() : false; - _z_frame_payload_t payload; - if (is_fragment == true) { - payload._fragment = gen_payload(64); - } else { - _z_zint_t num = (gen_zint() % 4) + 1; - payload._messages = _z_vec_make(num); - for (_z_zint_t i = 0; i < num; i++) { - _z_zenoh_message_t *p_zm = (_z_zenoh_message_t *)z_malloc(sizeof(_z_zenoh_message_t)); - *p_zm = gen_zenoh_message(); - _z_vec_append(&payload._messages, p_zm); - } + _z_zenoh_message_vec_t messages; + _z_zint_t num = (gen_zint() % 4) + 1; + messages = _z_vec_make(num); + for (_z_zint_t i = 0; i < num; i++) { + _z_zenoh_message_t *p_zm = (_z_zenoh_message_t *)z_malloc(sizeof(_z_zenoh_message_t)); + *p_zm = gen_zenoh_message(); + _z_vec_append(&messages, p_zm); } - return _z_t_msg_make_frame(sn, payload, is_reliable, is_fragment, is_final); + return _z_t_msg_make_frame(sn, messages, is_reliable); } void assert_eq_frame_message(_z_t_msg_frame_t *left, _z_t_msg_frame_t *right, uint8_t header) { + (void)(header); + printf(" SN (%zu:%zu)", left->_sn, right->_sn); assert(left->_sn == right->_sn); printf("\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_F) == true) { - printf(" "); - assert_eq_payload(&left->_payload._fragment, &right->_payload._fragment); - printf("\n"); - } else { - size_t l_len = _z_vec_len(&left->_payload._messages); - size_t r_len = _z_vec_len(&right->_payload._messages); - printf(" Lenght (%zu:%zu)", l_len, r_len); - assert(l_len == r_len); + size_t l_len = _z_vec_len(&left->_messages); + size_t r_len = _z_vec_len(&right->_messages); + printf(" Lenght (%zu:%zu)", l_len, r_len); + assert(l_len == r_len); - for (size_t i = 0; i < l_len; i++) - assert_eq_zenoh_message((_z_zenoh_message_t *)_z_vec_get(&left->_payload._messages, i), - (_z_zenoh_message_t *)_z_vec_get(&right->_payload._messages, i)); - } + for (size_t i = 0; i < l_len; i++) + assert_eq_zenoh_message((_z_zenoh_message_t *)_z_vec_get(&left->_messages, i), + (_z_zenoh_message_t *)_z_vec_get(&right->_messages, i)); } void frame_message(void) { @@ -2483,7 +2473,7 @@ void frame_message(void) { _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - _z_transport_message_t t_msg = gen_frame_message(1); + _z_transport_message_t t_msg = gen_frame_message(); assert(_Z_MID(t_msg._header) == _Z_MID_FRAME); _z_t_msg_frame_t e_fr = t_msg._body._frame; @@ -2502,14 +2492,14 @@ void frame_message(void) { assert_eq_frame_message(&e_fr, &d_fr, t_msg._header); // Frame - _z_t_msg_clear_frame(&d_fr, t_msg._header); + _z_t_msg_clear_frame(&d_fr); _z_t_msg_clear(&t_msg); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } /*------------------ Transport Message ------------------*/ -_z_transport_message_t gen_transport_message(_Bool can_be_fragment) { +_z_transport_message_t gen_transport_message(void) { _z_transport_message_t e_tm; // uint8_t mids[] = { @@ -2552,7 +2542,7 @@ _z_transport_message_t gen_transport_message(_Bool can_be_fragment) { e_tm = gen_ping_pong_message(); break; case _Z_MID_FRAME: - e_tm = gen_frame_message(can_be_fragment); + e_tm = gen_frame_message(); break; default: assert(0); @@ -2642,7 +2632,7 @@ void transport_message(void) { _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - _z_transport_message_t e_tm = gen_transport_message(1); + _z_transport_message_t e_tm = gen_transport_message(); printf(" - "); print_transport_message_type(e_tm._header); printf("\n"); @@ -2680,7 +2670,7 @@ void batch(void) { _z_transport_message_t *e_tm = (_z_transport_message_t *)z_malloc(tot_num * sizeof(_z_transport_message_t)); for (uint8_t i = 0; i < bef_num; i++) { // Initialize random transport message - e_tm[i] = gen_transport_message(0); + e_tm[i] = gen_transport_message(); // Encode int8_t res = _z_transport_message_encode(&wbf, &e_tm[i]); assert(res == _Z_RES_OK); @@ -2688,7 +2678,7 @@ void batch(void) { } for (uint8_t i = bef_num; i < bef_num + frm_num; i++) { // Initialize a frame message - e_tm[i] = gen_frame_message(0); + e_tm[i] = gen_frame_message(); // Encode int8_t res = _z_transport_message_encode(&wbf, &e_tm[i]); assert(res == _Z_RES_OK); @@ -2696,7 +2686,7 @@ void batch(void) { } for (uint8_t i = bef_num + frm_num; i < bef_num + frm_num + aft_num; i++) { // Initialize random transport message - e_tm[i] = gen_transport_message(0); + e_tm[i] = gen_transport_message(); // Encode int8_t res = _z_transport_message_encode(&wbf, &e_tm[i]); assert(res == _Z_RES_OK); @@ -2726,161 +2716,161 @@ void batch(void) { } /*------------------ Fragmentation ------------------*/ -_z_transport_message_t z_frame_header(_Bool is_reliable, _Bool is_fragment, _Bool is_final, _z_zint_t sn) { - // Create the frame session message that carries the zenoh message - _z_transport_message_t t_msg; - t_msg._attachment = NULL; - t_msg._header = _Z_MID_FRAME; - t_msg._body._frame._sn = sn; - - if (is_reliable == true) { - _Z_SET_FLAG(t_msg._header, _Z_FLAG_T_R); - }; - - if (is_fragment == true) { - _Z_SET_FLAG(t_msg._header, _Z_FLAG_T_F); - - if (is_final == true) { - _Z_SET_FLAG(t_msg._header, _Z_FLAG_T_E); - } - - // Do not add the payload - t_msg._body._frame._payload._fragment.len = 0; - t_msg._body._frame._payload._fragment.start = NULL; - } else { - // Do not allocate the vector containing the messages - t_msg._body._frame._payload._messages = _z_zenoh_message_vec_make(0); - } - - return t_msg; -} - -void z_wbuf_prepare(_z_wbuf_t *wbf) { - // Clear the buffer for serialization - _z_wbuf_reset(wbf); - - for (size_t i = 0; i < _z_wbuf_space_left(wbf); i++) _z_wbuf_put(wbf, 0xff, i); -} - -int8_t z_serialize_zenoh_fragment(_z_wbuf_t *dst, _z_wbuf_t *src, _Bool is_reliable, size_t sn) { - // Assume first that this is not the final fragment - _Bool is_final = false; - do { - // Mark the buffer for the writing operation - size_t w_pos = _z_wbuf_get_wpos(dst); - // Get the frame header - _z_transport_message_t f_hdr = z_frame_header(is_reliable, true, is_final, sn); - // Encode the frame header - int8_t res = _z_transport_message_encode(dst, &f_hdr); - if (res == _Z_RES_OK) { - size_t space_left = _z_wbuf_space_left(dst); - size_t bytes_left = _z_wbuf_len(src); - // Check if it is really the final fragment - if ((is_final == false) && (bytes_left <= space_left)) { - // Revert the buffer - _z_wbuf_set_wpos(dst, w_pos); - // It is really the finally fragment, reserialize the header - is_final = true; - continue; - } - // Write the fragment - size_t to_copy = bytes_left <= space_left ? bytes_left : space_left; - printf(" -Bytes left: %zu, Space left: %zu, Fragment size: %zu, Is final: %d\n", bytes_left, space_left, - to_copy, is_final); - return _z_wbuf_siphon(dst, src, to_copy); - } else { - return 0; - } - } while (1); -} - -void fragmentation(void) { - printf("\n>> Fragmentation\n"); - size_t len = 16; - _z_wbuf_t wbf = _z_wbuf_make(len, false); - _z_wbuf_t fbf = _z_wbuf_make(len, true); - _z_wbuf_t dbf = _z_wbuf_make(0, true); - - _z_zenoh_message_t e_zm; - - do { - _z_wbuf_reset(&fbf); - // Generate and serialize the message - e_zm = gen_zenoh_message(); - _z_zenoh_message_encode(&fbf, &e_zm); - // Check that the message actually requires fragmentation - if (_z_wbuf_len(&fbf) <= len) - _z_msg_clear(&e_zm); - else - break; - } while (1); - - // Fragment the message - _Bool is_reliable = gen_bool(); - // Fix the sn resoulution to 128 in such a way it requires only 1 byte for encoding - size_t seq_num_res = 128; - size_t sn = seq_num_res - 1; - - printf(" - Message serialized\n"); - print_wbuf(&fbf); - - printf(" - Start fragmenting\n"); - while (_z_wbuf_len(&fbf) > 0) { - // Clear the buffer for serialization - z_wbuf_prepare(&wbf); - - // Get the fragment sequence number - sn = (sn + 1) % seq_num_res; - - size_t written = _z_wbuf_len(&fbf); - int8_t res = z_serialize_zenoh_fragment(&wbf, &fbf, is_reliable, sn); - assert(res == _Z_RES_OK); - (void)(res); - written = written - _z_wbuf_len(&fbf); - - printf(" -Encoded Fragment: "); - print_wbuf(&wbf); - - // Decode the message - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - - _z_transport_message_t r_sm; - res = _z_transport_message_decode(&r_sm, &zbf); - assert(res == _Z_RES_OK); - - _z_bytes_t fragment = r_sm._body._frame._payload._fragment; - printf(" -Decoded Fragment length: %zu\n", fragment.len); - assert(fragment.len == written); - - // Create an iosli for decoding - _z_wbuf_write_bytes(&dbf, fragment.start, 0, fragment.len); - - print_wbuf(&dbf); - - // Free the read buffer - _z_zbuf_clear(&zbf); - } - - printf(" - Start defragmenting\n"); - print_wbuf(&dbf); - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&dbf); - printf(" Defragmented: "); - print_iosli(&zbf._ios); - printf("\n"); - - _z_zenoh_message_t d_zm; - int8_t res = _z_zenoh_message_decode(&d_zm, &zbf); - assert(res == _Z_RES_OK); - - assert_eq_zenoh_message(&e_zm, &d_zm); - _z_msg_clear(&e_zm); - _z_msg_clear(&d_zm); - - _z_wbuf_clear(&dbf); - _z_wbuf_clear(&wbf); - _z_wbuf_clear(&fbf); - _z_zbuf_clear(&zbf); -} +// _z_transport_message_t z_fragment_header(_Bool is_reliable, _Bool is_fragment, _Bool is_final, _z_zint_t sn) { +// // Create the frame session message that carries the zenoh message +// _z_transport_message_t t_msg; +// t_msg._attachment = NULL; +// t_msg._header = _Z_MID_FRAME; +// t_msg._body._frame._sn = sn; + +// if (is_reliable == true) { +// _Z_SET_FLAG(t_msg._header, _Z_FLAG_T_R); +// }; + +// if (is_fragment == true) { +// _Z_SET_FLAG(t_msg._header, _Z_FLAG_T_F); + +// if (is_final == true) { +// _Z_SET_FLAG(t_msg._header, _Z_FLAG_T_E); +// } + +// // Do not add the payload +// t_msg._body._frame._payload._fragment.len = 0; +// t_msg._body._frame._payload._fragment.start = NULL; +// } else { +// // Do not allocate the vector containing the messages +// t_msg._body._frame._payload._messages = _z_zenoh_message_vec_make(0); +// } + +// return t_msg; +// } + +// void z_wbuf_prepare(_z_wbuf_t *wbf) { +// // Clear the buffer for serialization +// _z_wbuf_reset(wbf); + +// for (size_t i = 0; i < _z_wbuf_space_left(wbf); i++) _z_wbuf_put(wbf, 0xff, i); +// } + +// int8_t z_serialize_zenoh_fragment(_z_wbuf_t *dst, _z_wbuf_t *src, _Bool is_reliable, size_t sn) { +// // Assume first that this is not the final fragment +// _Bool is_final = false; +// do { +// // Mark the buffer for the writing operation +// size_t w_pos = _z_wbuf_get_wpos(dst); +// // Get the frame header +// _z_transport_message_t f_hdr = z_fragment_header(is_reliable, true, is_final, sn); +// // Encode the frame header +// int8_t res = _z_transport_message_encode(dst, &f_hdr); +// if (res == _Z_RES_OK) { +// size_t space_left = _z_wbuf_space_left(dst); +// size_t bytes_left = _z_wbuf_len(src); +// // Check if it is really the final fragment +// if ((is_final == false) && (bytes_left <= space_left)) { +// // Revert the buffer +// _z_wbuf_set_wpos(dst, w_pos); +// // It is really the finally fragment, reserialize the header +// is_final = true; +// continue; +// } +// // Write the fragment +// size_t to_copy = bytes_left <= space_left ? bytes_left : space_left; +// printf(" -Bytes left: %zu, Space left: %zu, Fragment size: %zu, Is final: %d\n", bytes_left, space_left, +// to_copy, is_final); +// return _z_wbuf_siphon(dst, src, to_copy); +// } else { +// return 0; +// } +// } while (1); +// } + +// void fragmentation(void) { +// printf("\n>> Fragmentation\n"); +// size_t len = 16; +// _z_wbuf_t wbf = _z_wbuf_make(len, false); +// _z_wbuf_t fbf = _z_wbuf_make(len, true); +// _z_wbuf_t dbf = _z_wbuf_make(0, true); + +// _z_zenoh_message_t e_zm; + +// do { +// _z_wbuf_reset(&fbf); +// // Generate and serialize the message +// e_zm = gen_zenoh_message(); +// _z_zenoh_message_encode(&fbf, &e_zm); +// // Check that the message actually requires fragmentation +// if (_z_wbuf_len(&fbf) <= len) +// _z_msg_clear(&e_zm); +// else +// break; +// } while (1); + +// // Fragment the message +// _Bool is_reliable = gen_bool(); +// // Fix the sn resoulution to 128 in such a way it requires only 1 byte for encoding +// size_t seq_num_res = 128; +// size_t sn = seq_num_res - 1; + +// printf(" - Message serialized\n"); +// print_wbuf(&fbf); + +// printf(" - Start fragmenting\n"); +// while (_z_wbuf_len(&fbf) > 0) { +// // Clear the buffer for serialization +// z_wbuf_prepare(&wbf); + +// // Get the fragment sequence number +// sn = (sn + 1) % seq_num_res; + +// size_t written = _z_wbuf_len(&fbf); +// int8_t res = z_serialize_zenoh_fragment(&wbf, &fbf, is_reliable, sn); +// assert(res == _Z_RES_OK); +// (void)(res); +// written = written - _z_wbuf_len(&fbf); + +// printf(" -Encoded Fragment: "); +// print_wbuf(&wbf); + +// // Decode the message +// _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + +// _z_transport_message_t r_sm; +// res = _z_transport_message_decode(&r_sm, &zbf); +// assert(res == _Z_RES_OK); + +// _z_bytes_t fragment = r_sm._body._frame._payload._fragment; +// printf(" -Decoded Fragment length: %zu\n", fragment.len); +// assert(fragment.len == written); + +// // Create an iosli for decoding +// _z_wbuf_write_bytes(&dbf, fragment.start, 0, fragment.len); + +// print_wbuf(&dbf); + +// // Free the read buffer +// _z_zbuf_clear(&zbf); +// } + +// printf(" - Start defragmenting\n"); +// print_wbuf(&dbf); +// _z_zbuf_t zbf = _z_wbuf_to_zbuf(&dbf); +// printf(" Defragmented: "); +// print_iosli(&zbf._ios); +// printf("\n"); + +// _z_zenoh_message_t d_zm; +// int8_t res = _z_zenoh_message_decode(&d_zm, &zbf); +// assert(res == _Z_RES_OK); + +// assert_eq_zenoh_message(&e_zm, &d_zm); +// _z_msg_clear(&e_zm); +// _z_msg_clear(&d_zm); + +// _z_wbuf_clear(&dbf); +// _z_wbuf_clear(&wbf); +// _z_wbuf_clear(&fbf); +// _z_zbuf_clear(&zbf); +// } /*=============================*/ /* Main */ @@ -2939,7 +2929,7 @@ int main(void) { frame_message(); transport_message(); batch(); - fragmentation(); + // fragmentation(); } return 0; From 1c936505b2e3104d42f0826604dd7470d9585bf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 16 Feb 2023 18:03:34 +0100 Subject: [PATCH 021/181] Remove sync, acknack, pingpong messages --- include/zenoh-pico/protocol/msg.h | 85 ------------ include/zenoh-pico/protocol/msgcodec.h | 12 -- src/protocol/msg.c | 118 ----------------- src/protocol/msgcodec.c | 112 ---------------- tests/z_msgcodec_test.c | 177 +------------------------ 5 files changed, 4 insertions(+), 500 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 563a51024..567190d1b 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -919,60 +919,6 @@ typedef struct { } _z_t_msg_close_t; void _z_t_msg_clear_close(_z_t_msg_close_t *msg); -/*------------------ Sync Message ------------------*/ -// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length -// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. -// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve -// the boundary of the serialized messages. The length is encoded as little-endian. -// In any case, the length of a message must not exceed 65_535 bytes. -// -// The SYNC message allows to signal the corresponding peer the sequence number of the next message -// to be transmitted on the reliable or best-effort channel. In the case of reliable channel, the -// peer can optionally include the number of unacknowledged messages. A SYNC sent on the reliable -// channel triggers the transmission of an ACKNACK message. -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |X|C|R| SYNC | -// +-+-+-+-+-------+ -// ~ sn ~ -- Sequence number of the next message to be transmitted on this channel. -// +---------------+ -// ~ count ~ if R==1 && C==1 -- Number of unacknowledged messages. -// +---------------+ -// -// - if R==1 then the SYNC concerns the reliable channel, otherwise the best-effort channel. -// -typedef struct { - _z_zint_t _sn; - _z_zint_t _count; -} _z_t_msg_sync_t; -void _z_t_msg_clear_sync(_z_t_msg_sync_t *msg); - -/*------------------ AckNack Message ------------------*/ -// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length -// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. -// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve -// the boundary of the serialized messages. The length is encoded as little-endian. -// In any case, the length of a message must not exceed 65_535 bytes. -// -// The ACKNACK messages is used on the reliable channel to signal the corresponding peer the last -// sequence number received and optionally a bitmask of the non-received messages. -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |X|X|M| ACKNACK | -// +-+-+-+-+-------+ -// ~ sn ~ -// +---------------+ -// ~ mask ~ if M==1 -// +---------------+ -// -typedef struct { - _z_zint_t _sn; - _z_zint_t _mask; -} _z_t_msg_ack_nack_t; -void _z_t_msg_clear_ack_nack(_z_t_msg_ack_nack_t *msg); - /*------------------ Keep Alive Message ------------------*/ // NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length // in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. @@ -1000,27 +946,6 @@ typedef struct { } _z_t_msg_keep_alive_t; void _z_t_msg_clear_keep_alive(_z_t_msg_keep_alive_t *msg); -/*------------------ PingPong Messages ------------------*/ -// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length -// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. -// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve -// the boundary of the serialized messages. The length is encoded as little-endian. -// In any case, the length of a message must not exceed 65_535 bytes. -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |X|X|P| P_PONG | -// +-+-+-+-+-------+ -// ~ hash ~ -// +---------------+ -// -// - if P==1 then the message is Ping, otherwise is Pong. -// -typedef struct { - _z_zint_t _hash; -} _z_t_msg_ping_pong_t; -void _z_t_msg_clear_ping_pong(_z_t_msg_ping_pong_t *msg); - /*------------------ Frame Message ------------------*/ // NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length // in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. @@ -1060,10 +985,7 @@ typedef union { _z_t_msg_init_t _init; _z_t_msg_open_t _open; _z_t_msg_close_t _close; - _z_t_msg_sync_t _sync; - _z_t_msg_ack_nack_t _ack_nack; _z_t_msg_keep_alive_t _keep_alive; - _z_t_msg_ping_pong_t _ping_pong; _z_t_msg_frame_t _frame; } _z_transport_body_t; @@ -1085,11 +1007,7 @@ _z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_bytes_t zi _z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie); _z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial_sn); _z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only); -_z_transport_message_t _z_t_msg_make_sync(_z_zint_t sn, _Bool is_reliable, _z_zint_t count); -_z_transport_message_t _z_t_msg_make_ack_nack(_z_zint_t sn, _z_zint_t mask); _z_transport_message_t _z_t_msg_make_keep_alive(void); -_z_transport_message_t _z_t_msg_make_ping(_z_zint_t hash); -_z_transport_message_t _z_t_msg_make_pong(_z_zint_t hash); _z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_zenoh_message_vec_t messages, _Bool is_reliable); _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable); @@ -1101,10 +1019,7 @@ void _z_t_msg_copy_join(_z_t_msg_join_t *clone, _z_t_msg_join_t *msg); void _z_t_msg_copy_init(_z_t_msg_init_t *clone, _z_t_msg_init_t *msg); void _z_t_msg_copy_open(_z_t_msg_open_t *clone, _z_t_msg_open_t *msg); void _z_t_msg_copy_close(_z_t_msg_close_t *clone, _z_t_msg_close_t *msg); -void _z_t_msg_copy_sync(_z_t_msg_sync_t *clone, _z_t_msg_sync_t *msg); -void _z_t_msg_copy_ack_nack(_z_t_msg_ack_nack_t *clone, _z_t_msg_ack_nack_t *msg); void _z_t_msg_copy_keep_alive(_z_t_msg_keep_alive_t *clone, _z_t_msg_keep_alive_t *msg); -void _z_t_msg_copy_ping_pong(_z_t_msg_ping_pong_t *clone, _z_t_msg_ping_pong_t *msg); void _z_t_msg_copy_frame(_z_t_msg_frame_t *clone, _z_t_msg_frame_t *msg); #endif /* ZENOH_PICO_PROTOCOL_MSG_H */ diff --git a/include/zenoh-pico/protocol/msgcodec.h b/include/zenoh-pico/protocol/msgcodec.h index ff896989a..392fde16e 100644 --- a/include/zenoh-pico/protocol/msgcodec.h +++ b/include/zenoh-pico/protocol/msgcodec.h @@ -151,22 +151,10 @@ int8_t _z_close_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_close_t *m int8_t _z_close_decode_na(_z_t_msg_close_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_close_decode(_z_t_msg_close_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_sync_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_sync_t *msg); -int8_t _z_sync_decode_na(_z_t_msg_sync_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_sync_decode(_z_t_msg_sync_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_ack_nack_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_ack_nack_t *msg); -int8_t _z_ack_nack_decode_na(_z_t_msg_ack_nack_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_ack_nack_decode(_z_t_msg_ack_nack_t *msg, _z_zbuf_t *zbf, uint8_t header); - int8_t _z_keep_alive_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_keep_alive_t *msg); int8_t _z_keep_alive_decode_na(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_ping_pong_encode(_z_wbuf_t *wbf, const _z_t_msg_ping_pong_t *msg); -int8_t _z_ping_pong_decode_na(_z_t_msg_ping_pong_t *msg, _z_zbuf_t *zbf); -int8_t _z_ping_pong_decode(_z_t_msg_ping_pong_t *msg, _z_zbuf_t *zbf); - int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg); int8_t _z_frame_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 737a09d33..33e1a229b 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -687,65 +687,6 @@ void _z_t_msg_copy_close(_z_t_msg_close_t *clone, _z_t_msg_close_t *msg) { clone void _z_t_msg_clear_close(_z_t_msg_close_t *msg) { (void)(msg); } -/*------------------ Sync Message ------------------*/ -_z_transport_message_t _z_t_msg_make_sync(_z_zint_t sn, _Bool is_reliable, _z_zint_t count) { - _z_transport_message_t msg; - - msg._body._sync._sn = sn; - msg._body._sync._count = count; - - msg._header = _Z_MID_SYNC; - if (is_reliable == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_R); - if (count != 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_C); - } - } - - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - - return msg; -} - -void _z_t_msg_copy_sync(_z_t_msg_sync_t *clone, _z_t_msg_sync_t *msg) { - clone->_sn = msg->_sn; - clone->_count = msg->_count; -} - -void _z_t_msg_clear_sync(_z_t_msg_sync_t *msg) { - // NOTE: sync does not involve any heap allocation - (void)(msg); -} - -/*------------------ AckNack Message ------------------*/ -_z_transport_message_t _z_t_msg_make_ack_nack(_z_zint_t sn, _z_zint_t mask) { - _z_transport_message_t msg; - - msg._body._ack_nack._sn = sn; - msg._body._ack_nack._mask = mask; - - msg._header = _Z_MID_ACK_NACK; - if (mask != 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_M); - } - - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - - return msg; -} - -void _z_t_msg_copy_ack_nack(_z_t_msg_ack_nack_t *clone, _z_t_msg_ack_nack_t *msg) { - clone->_sn = msg->_sn; - clone->_mask = msg->_mask; -} - -void _z_t_msg_clear_ack_nack(_z_t_msg_ack_nack_t *msg) { - // NOTE: ack_nack does not involve any heap allocation - (void)(msg); -} - /*------------------ Keep Alive Message ------------------*/ _z_transport_message_t _z_t_msg_make_keep_alive(void) { _z_transport_message_t msg; @@ -764,41 +705,6 @@ void _z_t_msg_copy_keep_alive(_z_t_msg_keep_alive_t *clone, _z_t_msg_keep_alive_ void _z_t_msg_clear_keep_alive(_z_t_msg_keep_alive_t *msg) { (void)(msg); } -/*------------------ PingPong Messages ------------------*/ -_z_transport_message_t _z_t_msg_make_ping(_z_zint_t hash) { - _z_transport_message_t msg; - - msg._body._ping_pong._hash = hash; - - msg._header = _Z_MID_PING_PONG; - - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - - return msg; -} - -_z_transport_message_t _z_t_msg_make_pong(_z_zint_t hash) { - _z_transport_message_t msg; - - msg._body._ping_pong._hash = hash; - - msg._header = _Z_MID_PING_PONG; - _Z_SET_FLAG(msg._header, _Z_FLAG_T_P); - - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - - return msg; -} - -void _z_t_msg_copy_ping_pong(_z_t_msg_ping_pong_t *clone, _z_t_msg_ping_pong_t *msg) { clone->_hash = msg->_hash; } - -void _z_t_msg_clear_ping_pong(_z_t_msg_ping_pong_t *msg) { - // NOTE: ping_pong does not involve any heap allocation - (void)(msg); -} - /*------------------ Frame Message ------------------*/ _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable) { _z_transport_message_t msg; @@ -865,22 +771,10 @@ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg) { _z_t_msg_copy_close(&clone->_body._close, &msg->_body._close); } break; - case _Z_MID_SYNC: { - _z_t_msg_copy_sync(&clone->_body._sync, &msg->_body._sync); - } break; - - case _Z_MID_ACK_NACK: { - _z_t_msg_copy_ack_nack(&clone->_body._ack_nack, &msg->_body._ack_nack); - } break; - case _Z_MID_KEEP_ALIVE: { _z_t_msg_copy_keep_alive(&clone->_body._keep_alive, &msg->_body._keep_alive); } break; - case _Z_MID_PING_PONG: { - _z_t_msg_copy_ping_pong(&clone->_body._ping_pong, &msg->_body._ping_pong); - } break; - case _Z_MID_FRAME: { _z_t_msg_copy_frame(&clone->_body._frame, &msg->_body._frame); } break; @@ -923,22 +817,10 @@ void _z_t_msg_clear(_z_transport_message_t *msg) { _z_t_msg_clear_close(&msg->_body._close); } break; - case _Z_MID_SYNC: { - _z_t_msg_clear_sync(&msg->_body._sync); - } break; - - case _Z_MID_ACK_NACK: { - _z_t_msg_clear_ack_nack(&msg->_body._ack_nack); - } break; - case _Z_MID_KEEP_ALIVE: { _z_t_msg_clear_keep_alive(&msg->_body._keep_alive); } break; - case _Z_MID_PING_PONG: { - _z_t_msg_clear_ping_pong(&msg->_body._ping_pong); - } break; - case _Z_MID_FRAME: { _z_t_msg_clear_frame(&msg->_body._frame); } break; diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index ae37e2042..adfc7ba3e 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1288,70 +1288,6 @@ int8_t _z_close_decode(_z_t_msg_close_t *msg, _z_zbuf_t *zbf, uint8_t header) { return _z_close_decode_na(msg, zbf, header); } -/*------------------ Sync Message ------------------*/ -int8_t _z_sync_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_sync_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_SYNC\n"); - - _Z_EC(_z_zint_encode(wbf, msg->_sn)) - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_C) == true) { - _Z_EC(_z_zint_encode(wbf, msg->_count)) - } - - return ret; -} - -int8_t _z_sync_decode_na(_z_t_msg_sync_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_SYNC\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&msg->_sn, zbf); - if ((_Z_HAS_FLAG(header, _Z_FLAG_T_R) == true) && (_Z_HAS_FLAG(header, _Z_FLAG_T_C) == true)) { - ret |= _z_zint_decode(&msg->_count, zbf); - } else { - msg->_count = 0; - } - - return ret; -} - -int8_t _z_sync_decode(_z_t_msg_sync_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_sync_decode_na(msg, zbf, header); -} - -/*------------------ AckNack Message ------------------*/ -int8_t _z_ack_nack_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_ack_nack_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_ACK_NACK\n"); - - _Z_EC(_z_zint_encode(wbf, msg->_sn)) - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_M) == true) { - _Z_EC(_z_zint_encode(wbf, msg->_mask)) - } - - return ret; -} - -int8_t _z_ack_nack_decode_na(_z_t_msg_ack_nack_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_ACK_NACK\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&msg->_sn, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_M) == true) { - ret |= _z_zint_decode(&msg->_mask, zbf); - } else { - msg->_mask = 0; - } - - return ret; -} - -int8_t _z_ack_nack_decode(_z_t_msg_ack_nack_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_ack_nack_decode_na(msg, zbf, header); -} - /*------------------ Keep Alive Message ------------------*/ int8_t _z_keep_alive_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_keep_alive_t *msg) { (void)(wbf); @@ -1379,27 +1315,6 @@ int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t return _z_keep_alive_decode_na(msg, zbf, header); } -/*------------------ PingPong Messages ------------------*/ -int8_t _z_ping_pong_encode(_z_wbuf_t *wbf, const _z_t_msg_ping_pong_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_PING_PONG\n"); - - ret |= _z_zint_encode(wbf, msg->_hash); - - return ret; -} - -int8_t _z_ping_pong_decode_na(_z_t_msg_ping_pong_t *msg, _z_zbuf_t *zbf) { - _Z_DEBUG("Decoding _Z_MID_PING_PONG\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&msg->_hash, zbf); - - return ret; -} - -int8_t _z_ping_pong_decode(_z_t_msg_ping_pong_t *msg, _z_zbuf_t *zbf) { return _z_ping_pong_decode_na(msg, zbf); } - /*------------------ Frame Message ------------------*/ int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg) { (void)(header); @@ -1506,18 +1421,6 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t ret |= _z_close_encode(wbf, msg->_header, &msg->_body._close); } break; - case _Z_MID_SYNC: { - ret |= _z_sync_encode(wbf, msg->_header, &msg->_body._sync); - } break; - - case _Z_MID_ACK_NACK: { - ret |= _z_ack_nack_encode(wbf, msg->_header, &msg->_body._ack_nack); - } break; - - case _Z_MID_PING_PONG: { - ret |= _z_ping_pong_encode(wbf, &msg->_body._ping_pong); - } break; - default: { _Z_DEBUG("WARNING: Trying to encode session message with unknown ID(%d)\n", _Z_MID(msg->_header)); ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; @@ -1592,21 +1495,6 @@ int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *zb is_last = true; } break; - case _Z_MID_SYNC: { - ret |= _z_sync_decode(&msg->_body._sync, zbf, msg->_header); - is_last = true; - } break; - - case _Z_MID_ACK_NACK: { - ret |= _z_ack_nack_decode(&msg->_body._ack_nack, zbf, msg->_header); - is_last = true; - } break; - - case _Z_MID_PING_PONG: { - ret |= _z_ping_pong_decode(&msg->_body._ping_pong, zbf); - is_last = true; - } break; - case _Z_MID_PRIORITY: { // Ignore the priority decorator for the time being since zenoh-pico does not // perform any routing. Hence, priority information does not need to be propagated. diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index cb360dab0..62202471f 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -2250,107 +2250,6 @@ void close_message(void) { _z_wbuf_clear(&wbf); } -/*------------------ Sync Message ------------------*/ -_z_transport_message_t gen_sync_message(void) { - _z_zint_t sn = gen_zint(); - _Bool is_reliable = gen_bool(); - _z_zint_t count = gen_zint(); - - return _z_t_msg_make_sync(sn, is_reliable, count); -} - -void assert_eq_sync_message(_z_t_msg_sync_t *left, _z_t_msg_sync_t *right, uint8_t header) { - printf(" SN (%zu:%zu)", left->_sn, right->_sn); - assert(left->_sn == right->_sn); - printf("\n"); - - if ((_Z_HAS_FLAG(header, _Z_FLAG_T_R) == true) && (_Z_HAS_FLAG(header, _Z_FLAG_T_C) == true)) { - printf(" Count (%zu:%zu)", left->_count, right->_count); - assert(left->_count == right->_count); - printf("\n"); - } -} - -void sync_message(void) { - printf("\n>> Sync message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_transport_message_t t_msg = gen_sync_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_SYNC); - - _z_t_msg_sync_t e_sy = t_msg._body._sync; - - // Encode - int8_t res = _z_sync_encode(&wbf, t_msg._header, &e_sy); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_t_msg_sync_t d_sy; - res = _z_sync_decode(&d_sy, &zbf, t_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_sync_message(&e_sy, &d_sy, t_msg._header); - - // Free - _z_t_msg_clear_sync(&d_sy); - _z_t_msg_clear(&t_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ AckNack Message ------------------*/ -_z_transport_message_t gen_ack_nack_message(void) { - _z_zint_t sn = gen_zint(); - _z_zint_t mask = gen_bool() ? gen_zint() : 0; - - return _z_t_msg_make_ack_nack(sn, mask); -} - -void assert_eq_ack_nack_message(_z_t_msg_ack_nack_t *left, _z_t_msg_ack_nack_t *right, uint8_t header) { - printf(" SN (%zu:%zu)", left->_sn, right->_sn); - assert(left->_sn == right->_sn); - printf("\n"); - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_M) == true) { - printf(" Mask (%zu:%zu)", left->_mask, right->_mask); - assert(left->_mask == right->_mask); - printf("\n"); - } -} - -void ack_nack_message(void) { - printf("\n>> AckNack message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_transport_message_t t_msg = gen_ack_nack_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_ACK_NACK); - - _z_t_msg_ack_nack_t e_an = t_msg._body._ack_nack; - - // Encode - int8_t res = _z_ack_nack_encode(&wbf, t_msg._header, &e_an); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_t_msg_ack_nack_t d_an; - res = _z_ack_nack_decode(&d_an, &zbf, t_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_ack_nack_message(&e_an, &d_an, t_msg._header); - - // Free - _z_t_msg_clear_ack_nack(&d_an); - _z_t_msg_clear(&t_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - /*------------------ KeepAlive Message ------------------*/ _z_transport_message_t gen_keep_alive_message(void) { return _z_t_msg_make_keep_alive(); } @@ -2389,51 +2288,6 @@ void keep_alive_message(void) { _z_wbuf_clear(&wbf); } -/*------------------ PingPong Message ------------------*/ -_z_transport_message_t gen_ping_pong_message(void) { - _z_zint_t hash = gen_zint(); - if (gen_bool()) - return _z_t_msg_make_ping(hash); - else - return _z_t_msg_make_pong(hash); -} - -void assert_eq_ping_pong_message(_z_t_msg_ping_pong_t *left, _z_t_msg_ping_pong_t *right) { - printf(" Hash (%zu:%zu)", left->_hash, right->_hash); - assert(left->_hash == right->_hash); - printf("\n"); -} - -void ping_pong_message(void) { - printf("\n>> PingPong message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_transport_message_t t_msg = gen_ping_pong_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_PING_PONG); - - _z_t_msg_ping_pong_t e_pp = t_msg._body._ping_pong; - - // Encode - int8_t res = _z_ping_pong_encode(&wbf, &e_pp); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_t_msg_ping_pong_t d_pp; - res = _z_ping_pong_decode(&d_pp, &zbf); - assert(res == _Z_RES_OK); - - assert_eq_ping_pong_message(&e_pp, &d_pp); - - // Free - _z_t_msg_clear_ping_pong(&d_pp); - _z_t_msg_clear(&t_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - /*------------------ Frame Message ------------------*/ _z_transport_message_t gen_frame_message(void) { _z_zint_t sn = gen_zint(); @@ -2502,12 +2356,10 @@ void frame_message(void) { _z_transport_message_t gen_transport_message(void) { _z_transport_message_t e_tm; - // uint8_t mids[] = { - // _Z_MID_SCOUT, _Z_MID_HELLO, _Z_MID_JOIN, _Z_MID_INIT, _Z_MID_OPEN, _Z_MID_CLOSE, - // _Z_MID_SYNC, _Z_MID_ACK_NACK, _Z_MID_KEEP_ALIVE, _Z_MID_PING_PONG, _Z_MID_FRAME, - // }; - // TODO[protocol]: To be removed - uint8_t mids[] = {_Z_MID_SCOUT}; + uint8_t mids[] = { + _Z_MID_SCOUT, _Z_MID_HELLO, _Z_MID_JOIN, _Z_MID_INIT, + _Z_MID_OPEN, _Z_MID_CLOSE, _Z_MID_KEEP_ALIVE, _Z_MID_FRAME, + }; uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); switch (mids[i]) { @@ -2529,18 +2381,9 @@ _z_transport_message_t gen_transport_message(void) { case _Z_MID_CLOSE: e_tm = gen_close_message(); break; - case _Z_MID_SYNC: - e_tm = gen_sync_message(); - break; - case _Z_MID_ACK_NACK: - e_tm = gen_ack_nack_message(); - break; case _Z_MID_KEEP_ALIVE: e_tm = gen_keep_alive_message(); break; - case _Z_MID_PING_PONG: - e_tm = gen_ping_pong_message(); - break; case _Z_MID_FRAME: e_tm = gen_frame_message(); break; @@ -2597,18 +2440,9 @@ void assert_eq_transport_message(_z_transport_message_t *left, _z_transport_mess case _Z_MID_CLOSE: assert_eq_close_message(&left->_body._close, &right->_body._close, left->_header); break; - case _Z_MID_SYNC: - assert_eq_sync_message(&left->_body._sync, &right->_body._sync, left->_header); - break; - case _Z_MID_ACK_NACK: - assert_eq_ack_nack_message(&left->_body._ack_nack, &right->_body._ack_nack, left->_header); - break; case _Z_MID_KEEP_ALIVE: assert_eq_keep_alive_message(&left->_body._keep_alive, &right->_body._keep_alive, left->_header); break; - case _Z_MID_PING_PONG: - assert_eq_ping_pong_message(&left->_body._ping_pong, &right->_body._ping_pong); - break; case _Z_MID_FRAME: assert_eq_frame_message(&left->_body._frame, &right->_body._frame, left->_header); break; @@ -2922,10 +2756,7 @@ int main(void) { init_message(); open_message(); close_message(); - sync_message(); - ack_nack_message(); keep_alive_message(); - ping_pong_message(); frame_message(); transport_message(); batch(); From 61a89facdfe4685b793ed35bff27aed250d6cfcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 16 Feb 2023 18:42:06 +0100 Subject: [PATCH 022/181] Fix flags for hello message --- src/protocol/msg.c | 8 +------- src/session/scout.c | 7 ++++--- tests/z_msgcodec_test.c | 2 +- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 33e1a229b..706ec89f2 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -479,14 +479,8 @@ _z_transport_message_t _z_t_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, msg._body._hello._zid = zid; msg._body._hello._locators = locators; - if (whatami != Z_WHATAMI_ROUTER) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_W); - } - if (_z_bytes_is_empty(&zid) == false) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_I); - } if (_z_locator_array_is_empty(&locators) == false) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_L); + _Z_SET_FLAG(msg._header, _Z_FLAG_HELLO_L); } msg._attachment = NULL; diff --git a/src/session/scout.c b/src/session/scout.c index 243a0c614..db98843af 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -77,9 +77,10 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig hello->whatami = t_msg._body._hello._whatami; _z_bytes_copy(&hello->zid, &t_msg._body._hello._zid); - if (_Z_HAS_FLAG(t_msg._header, _Z_FLAG_T_L) == true) { - hello->locators = _z_str_array_make(t_msg._body._hello._locators._len); - for (size_t i = 0; i < hello->locators._len; i++) { + size_t n_loc = _z_locator_array_len(&t_msg._body._hello._locators); + if (n_loc > 0) { + hello->locators = _z_str_array_make(n_loc); + for (size_t i = 0; i < n_loc; i++) { hello->locators._val[i] = _z_locator_to_str(&t_msg._body._hello._locators._val[i]); } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 62202471f..4235456ec 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -1903,7 +1903,7 @@ void assert_eq_hello_message(_z_t_msg_hello_t *left, _z_t_msg_hello_t *right, ui assert_eq_uint8_array(&left->_zid, &right->_zid); printf("\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_L) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_HELLO_L) == true) { printf(" "); assert_eq_locator_array(&left->_locators, &right->_locators); printf("\n"); From 14a3b29be430d81bc073bcd72f1554bbb8bb05db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 16 Feb 2023 18:42:36 +0100 Subject: [PATCH 023/181] Remove unused flags --- include/zenoh-pico/protocol/msg.h | 16 +--------------- src/transport/multicast/link/rx.c | 6 ++---- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 567190d1b..98dbc2f06 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -111,25 +111,11 @@ #define _Z_FLAG_CLOSE_Z 0x80 // 1 << 7 /* Transport message flags */ -#define _Z_FLAG_T_A 0x20 // 1 << 5 | Ack if A==1 then the message is an acknowledgment -#define _Z_FLAG_T_C 0x40 // 1 << 6 | Count if C==1 then number of unacknowledged messages is present -#define _Z_FLAG_T_E 0x80 // 1 << 7 | End if E==1 then it is the last FRAME fragment -#define _Z_FLAG_T_F 0x40 // 1 << 6 | Fragment if F==1 then the FRAME is a fragment -#define _Z_FLAG_T_I 0x20 // 1 << 5 | PeerID if I==1 then the PeerID is present or requested -#define _Z_FLAG_T_K 0x40 // 1 << 6 | CloseLink if K==1 then close the transport link only -#define _Z_FLAG_T_L 0x80 // 1 << 7 | Locators if L==1 then Locators are present -#define _Z_FLAG_T_M 0x20 // 1 << 5 | Mask if M==1 then a Mask is present -#define _Z_FLAG_T_O 0x80 // 1 << 7 | Options if O==1 then Options are present -#define _Z_FLAG_T_P 0x20 // 1 << 5 | PingOrPong if P==1 then the message is Ping, otherwise is Pong -#define _Z_FLAG_T_R \ - 0x20 // 1 << 5 | Reliable if R==1 then it concerns the reliable channel, best-effort otherwise +#define _Z_FLAG_T_O 0x80 // 1 << 7 | Options if O==1 then Options are present #define _Z_FLAG_T_S 0x40 // 1 << 6 | SN Resolution if S==1 then the SN Resolution is present #define _Z_FLAG_T_T1 0x20 // 1 << 5 | TimeRes if T==1 then the time resolution is in seconds -#define _Z_FLAG_T_T2 0x40 // 1 << 6 | TimeRes if T==1 then the time resolution is in seconds -#define _Z_FLAG_T_W 0x40 // 1 << 6 | WhatAmI if W==1 then WhatAmI is indicated #define _Z_FLAG_T_Z \ 0x20 // 1 << 5 | MixedSlices if Z==1 then the payload contains a mix of raw and shm_info payload -#define _Z_FLAG_T_X 0x00 // Unused flags are set to zero /* Zenoh message flags */ #define _Z_FLAG_Z_B 0x40 // 1 << 6 | QueryPayload if B==1 then QueryPayload is present diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 88d833b05..3dd16f2cc 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -130,10 +130,8 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t case _Z_MID_JOIN: { _Z_INFO("Received _Z_JOIN message\n"); - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_A) == true) { - if (t_msg->_body._join._version != Z_PROTO_VERSION) { - break; - } + if (t_msg->_body._join._version != Z_PROTO_VERSION) { + break; } if (entry == NULL) // New peer From 99e5119157d9a0978fd08b2d7bad75ff2e8eb86c Mon Sep 17 00:00:00 2001 From: Carlos Guimaraes Date: Fri, 17 Feb 2023 06:54:41 +0100 Subject: [PATCH 024/181] Fix memory leak on query body --- include/zenoh-pico/protocol/core.h | 2 ++ src/net/memory.c | 16 ++++++++++++++++ src/protocol/msg.c | 2 ++ tests/z_msgcodec_test.c | 2 +- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 6f04eeddc..639e17397 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -95,6 +95,8 @@ typedef struct { _z_bytes_t payload; _z_encoding_t encoding; } _z_value_t; +void _z_value_clear(_z_value_t *src); +void _z_value_free(_z_value_t **hello); /** * A hello message returned by a zenoh entity to a scout message sent with :c:func:`_z_scout`. diff --git a/src/net/memory.c b/src/net/memory.c index 494c8aa0d..bd5f12e51 100644 --- a/src/net/memory.c +++ b/src/net/memory.c @@ -82,3 +82,19 @@ void _z_reply_data_free(_z_reply_data_t **reply_data) { *reply_data = NULL; } } + +void _z_value_clear(_z_value_t *value) { + _z_bytes_clear(&value->encoding.suffix); + _z_bytes_clear(&value->payload); +} + +void _z_value_free(_z_value_t **value) { + _z_value_t *ptr = *value; + + if (ptr != NULL) { + _z_value_clear(ptr); + + z_free(ptr); + *value = NULL; + } +} diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 706ec89f2..e1a89909d 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -399,6 +399,8 @@ _z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, char *parameters, _z_zint void _z_msg_clear_query(_z_msg_query_t *msg) { _z_keyexpr_clear(&msg->_key); _z_str_clear(msg->_parameters); + _z_bytes_clear(&msg->_info._encoding.suffix); + _z_bytes_clear(&msg->_payload); } /*------------------ Reply Message ------------------*/ diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 4235456ec..f440d6049 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -202,7 +202,7 @@ _z_value_t gen_value(void) { if (gen_bool()) { val.payload = _z_bytes_empty(); } else { - val.payload = _z_bytes_make(16); + val.payload = gen_bytes(16); } return val; From aaa938e1c0df349fbe0db8809da792e821da4136 Mon Sep 17 00:00:00 2001 From: Carlos Guimaraes Date: Fri, 17 Feb 2023 06:57:46 +0100 Subject: [PATCH 025/181] Fixes on extensions encoding and clearing --- src/protocol/ext.c | 10 +++++----- src/protocol/msg.c | 5 ++--- tests/z_msgcodec_test.c | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/protocol/ext.c b/src/protocol/ext.c index 4652e7772..e79932592 100644 --- a/src/protocol/ext.c +++ b/src/protocol/ext.c @@ -24,7 +24,7 @@ _z_msg_ext_t _z_msg_ext_make_unit(uint8_t id) { ext._header = 0; ext._header |= (id & 0x1F); - ext._header |= (_Z_MSG_EXT_ENC_UNIT & 0x03) << 5; + ext._header |= _Z_MSG_EXT_ENC_UNIT; return ext; } @@ -41,7 +41,7 @@ _z_msg_ext_t _z_msg_ext_make_zint(uint8_t id, _z_zint_t zid) { ext._header = 0; ext._header |= (id & 0x1F); - ext._header |= (_Z_MSG_EXT_ENC_ZINT & 0x03) << 5; + ext._header |= _Z_MSG_EXT_ENC_ZINT; ext._body._zint._val = zid; @@ -52,14 +52,14 @@ void _z_msg_ext_clear_zint(_z_msg_ext_zint_t *ext) { (void)(ext); } void _z_msg_ext_copy_zint(_z_msg_ext_zint_t *clone, const _z_msg_ext_zint_t *ext) { clone->_val = ext->_val; } -_z_msg_ext_t _z_msg_ext_make_zbuf(uint8_t id, const _z_bytes_t zbuf) { +_z_msg_ext_t _z_msg_ext_make_zbuf(uint8_t id, _z_bytes_t zbuf) { _z_msg_ext_t ext; ext._header = 0; ext._header |= (id & 0x1F); - ext._header |= (_Z_MSG_EXT_ENC_ZBUF & 0x03) << 5; + ext._header |= _Z_MSG_EXT_ENC_ZBUF; - ext._body._zbuf._val = _z_bytes_wrap(zbuf.start, zbuf.len); + _z_bytes_move(&ext._body._zbuf._val, &zbuf); return ext; } diff --git a/src/protocol/msg.c b/src/protocol/msg.c index e1a89909d..e34dc9340 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -711,9 +711,6 @@ _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliabl _Z_SET_FLAG(msg._header, _Z_FLAG_FRAME_R); } - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - return msg; } @@ -825,4 +822,6 @@ void _z_t_msg_clear(_z_transport_message_t *msg) { _Z_DEBUG("WARNING: Trying to free session message with unknown ID(%d)\n", mid); } break; } + + _z_msg_ext_vec_clear(&msg->_extensions); } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index f440d6049..3fa00824e 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -348,7 +348,7 @@ void zint_extension(void) { // Initialize _z_msg_ext_t ext = gen_zint_extension(); - assert(_Z_EXT_ENC(ext._header) == _Z_MSG_EXT_ENC_UNIT); + assert(_Z_EXT_ENC(ext._header) == _Z_MSG_EXT_ENC_ZINT); _z_msg_ext_zint_t e_u = ext._body._zint; @@ -390,7 +390,7 @@ void zbuf_extension(void) { // Initialize _z_msg_ext_t ext = gen_zbuf_extension(); - assert(_Z_EXT_ENC(ext._header) == _Z_MSG_EXT_ENC_UNIT); + assert(_Z_EXT_ENC(ext._header) == _Z_MSG_EXT_ENC_ZBUF); _z_msg_ext_zbuf_t e_u = ext._body._zbuf; From 43dae0c188707aa91e2fbc7602783eebfd7d4c61 Mon Sep 17 00:00:00 2001 From: Carlos Guimaraes Date: Fri, 17 Feb 2023 06:58:27 +0100 Subject: [PATCH 026/181] Non initialized version on hello message --- src/protocol/msg.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/protocol/msg.c b/src/protocol/msg.c index e34dc9340..556277a81 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -477,6 +477,7 @@ _z_transport_message_t _z_t_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _z_transport_message_t msg; msg._header = _Z_MID_HELLO; + msg._body._hello._version = Z_PROTO_VERSION; msg._body._hello._whatami = whatami; msg._body._hello._zid = zid; msg._body._hello._locators = locators; From 019791e80a482a2a3685c7938faf56f2331b57a1 Mon Sep 17 00:00:00 2001 From: Carlos Guimaraes Date: Fri, 17 Feb 2023 07:00:04 +0100 Subject: [PATCH 027/181] Robustness on transport message deserialization --- src/protocol/msgcodec.c | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index adfc7ba3e..80ca454da 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1018,9 +1018,12 @@ int8_t _z_scout_decode_na(_z_t_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) uint8_t cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); msg->_what = cbyte & 0x07; - if (_Z_HAS_FLAG(cbyte, _Z_FLAG_SCOUT_I) == true) { + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(cbyte, _Z_FLAG_SCOUT_I) == true)) { msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; ret |= _z_bytes_val_decode(&msg->_zid, zbf); + if (ret != _Z_RES_OK) { + msg->_zid = _z_bytes_empty(); + } } else { msg->_zid = _z_bytes_empty(); } @@ -1062,10 +1065,21 @@ int8_t _z_hello_decode_na(_z_t_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) ret |= _z_uint8_decode(&cbyte, zbf); msg->_whatami = cbyte & 0x03; msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; - ret |= _z_bytes_val_decode(&msg->_zid, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_HELLO_L) == true) { + if (ret == _Z_RES_OK) { + ret |= _z_bytes_val_decode(&msg->_zid, zbf); + if (ret != _Z_RES_OK) { + msg->_zid = _z_bytes_empty(); + } + } else { + msg->_zid = _z_bytes_empty(); + } + + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_HELLO_L) == true)) { ret |= _z_locators_decode(&msg->_locators, zbf); + if (ret != _Z_RES_OK) { + msg->_locators = _z_locator_array_make(0); + } } else { msg->_locators = _z_locator_array_make(0); } @@ -1190,7 +1204,15 @@ int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { ret |= _z_uint8_decode(&cbyte, zbf); msg->_whatami = cbyte & 0x03; msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; - ret |= _z_bytes_val_decode(&msg->_zid, zbf); + + if (ret == _Z_RES_OK) { + ret |= _z_bytes_val_decode(&msg->_zid, zbf); + if (ret != _Z_RES_OK) { + msg->_zid = _z_bytes_empty(); + } + } else { + msg->_zid = _z_bytes_empty(); + } if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { cbyte = 0; @@ -1206,8 +1228,11 @@ int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; } - if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_A) == true) { + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_INIT_A) == true)) { ret |= _z_bytes_decode(&msg->_cookie, zbf); + if (ret != _Z_RES_OK) { + msg->_cookie = _z_bytes_empty(); + } } else { msg->_cookie = _z_bytes_empty(); } @@ -1250,8 +1275,11 @@ int8_t _z_open_decode_na(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header) { ret |= _z_zint_decode(&msg->_initial_sn, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_OPEN_A) == false) { + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_OPEN_A) == false)) { ret |= _z_bytes_decode(&msg->_cookie, zbf); + if (ret != _Z_RES_OK) { + msg->_cookie = _z_bytes_empty(); + } } else { msg->_cookie = _z_bytes_empty(); } @@ -1349,6 +1377,7 @@ int8_t _z_frame_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) if (ret == _Z_RES_OK) { _z_zenoh_message_vec_append(&msg->_messages, zm); } else { + z_free(zm); _z_zbuf_set_rpos(zbf, r_pos); // Restore the reading position of the iobfer // FIXME: Check for the return error, since not all of them means a decoding error From c43dde3575748ab000c2a309dcccf668af261954 Mon Sep 17 00:00:00 2001 From: Carlos Guimaraes Date: Fri, 17 Feb 2023 07:05:02 +0100 Subject: [PATCH 028/181] Remove include --- src/protocol/msgcodec.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 80ca454da..cf03e8dc9 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -14,7 +14,6 @@ #include "zenoh-pico/protocol/msgcodec.h" -#include <_types/_uint8_t.h> #include #include "zenoh-pico/collections/bytes.h" From 637c9424684ac9c0b1671b8ac95b539d50565eba Mon Sep 17 00:00:00 2001 From: Carlos Guimaraes Date: Fri, 17 Feb 2023 07:25:46 +0100 Subject: [PATCH 029/181] Improve message clear / free upon decoding error --- include/zenoh-pico/protocol/msg.h | 1 + src/protocol/msg.c | 11 +++++++++++ src/protocol/msgcodec.c | 3 ++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 98dbc2f06..4f90a5848 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -588,6 +588,7 @@ typedef struct { uint8_t _header; } _z_zenoh_message_t; void _z_msg_clear(_z_zenoh_message_t *m); +void _z_msg_free(_z_zenoh_message_t **m); _Z_ELEM_DEFINE(_z_zenoh_message, _z_zenoh_message_t, _z_noop_size, _z_msg_clear, _z_noop_copy) _Z_VEC_DEFINE(_z_zenoh_message, _z_zenoh_message_t) diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 556277a81..b8e37e4eb 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -446,6 +446,17 @@ void _z_msg_clear(_z_zenoh_message_t *msg) { } } +void _z_msg_free(_z_zenoh_message_t **msg) { + _z_zenoh_message_t *ptr = *msg; + + if (ptr != NULL) { + _z_msg_clear(ptr); + + z_free(ptr); + *msg = NULL; + } +} + /*=============================*/ /* Transport Messages */ /*=============================*/ diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index cf03e8dc9..05c22f8a9 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1376,7 +1376,8 @@ int8_t _z_frame_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) if (ret == _Z_RES_OK) { _z_zenoh_message_vec_append(&msg->_messages, zm); } else { - z_free(zm); + _z_msg_free(&zm); + _z_zbuf_set_rpos(zbf, r_pos); // Restore the reading position of the iobfer // FIXME: Check for the return error, since not all of them means a decoding error From d4ead7a5eafa4903b165624003b8e6d861e3d2e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 17 Feb 2023 08:32:02 +0100 Subject: [PATCH 030/181] Fix frame and frame header dependency --- src/protocol/msg.c | 12 ++++++++++-- src/transport/multicast/link/tx.c | 2 +- src/transport/unicast/link/tx.c | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/protocol/msg.c b/src/protocol/msg.c index b8e37e4eb..d2a3ac67f 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -722,16 +722,24 @@ _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliabl if (is_reliable == true) { _Z_SET_FLAG(msg._header, _Z_FLAG_FRAME_R); } + msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); + msg._body._frame._messages = _z_zenoh_message_vec_make(0); return msg; } _z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_zenoh_message_vec_t messages, _Bool is_reliable) { - _z_transport_message_t msg = _z_t_msg_make_frame_header(sn, is_reliable); + _z_transport_message_t msg; + msg._header = _Z_MID_FRAME; - msg._body._frame._messages = messages; + msg._body._frame._sn = sn; + if (is_reliable == true) { + _Z_SET_FLAG(msg._header, _Z_FLAG_FRAME_R); + } msg._attachment = NULL; + msg._body._frame._messages = messages; msg._extensions = _z_msg_ext_vec_make(0); return msg; diff --git a/src/transport/multicast/link/tx.c b/src/transport/multicast/link/tx.c index d5e306035..7849d00a3 100644 --- a/src/transport/multicast/link/tx.c +++ b/src/transport/multicast/link/tx.c @@ -99,7 +99,7 @@ int8_t _z_multicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_re _z_zint_t sn = __unsafe_z_multicast_get_sn(ztm, reliability); // Get the next sequence number - _z_transport_message_t t_msg = _z_t_msg_make_frame_header(reliability, sn); + _z_transport_message_t t_msg = _z_t_msg_make_frame_header(sn, reliability); ret = _z_transport_message_encode(&ztm->_wbuf, &t_msg); // Encode the frame header if (ret == _Z_RES_OK) { ret = _z_zenoh_message_encode(&ztm->_wbuf, z_msg); // Encode the zenoh message diff --git a/src/transport/unicast/link/tx.c b/src/transport/unicast/link/tx.c index 00133d776..6e8db641d 100644 --- a/src/transport/unicast/link/tx.c +++ b/src/transport/unicast/link/tx.c @@ -99,7 +99,7 @@ int8_t _z_unicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reli _z_zint_t sn = __unsafe_z_unicast_get_sn(ztu, reliability); // Get the next sequence number - _z_transport_message_t t_msg = _z_t_msg_make_frame_header(reliability, sn); + _z_transport_message_t t_msg = _z_t_msg_make_frame_header(sn, reliability); ret = _z_transport_message_encode(&ztu->_wbuf, &t_msg); // Encode the frame header if (ret == _Z_RES_OK) { ret = _z_zenoh_message_encode(&ztu->_wbuf, z_msg); // Encode the zenoh message From 4b449e649e73de3510dbcdcb22d6bf8d8d4aad0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 17 Feb 2023 08:40:51 +0100 Subject: [PATCH 031/181] Fix multicast transport init with new messages --- src/transport/transport.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transport/transport.c b/src/transport/transport.c index 2908468fa..a582c3793 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -210,8 +210,8 @@ int8_t _z_transport_multicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_mu if (ret == _Z_RES_OK) { // Set default SN resolution - zt->_transport._multicast._seq_num_res = param->_seq_num_res; - zt->_transport._multicast._seq_num_res_half = param->_seq_num_res / 2; + zt->_transport._multicast._seq_num_res = _z_max_value(param->_seq_num_res); + zt->_transport._multicast._seq_num_res_half = zt->_transport._multicast._seq_num_res / 2; // The initial SN at TX side zt->_transport._multicast._sn_tx_reliable = param->_initial_sn_tx; From 3276e97d3945db110f29a01d80a08a8c4a4cf838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 17 Feb 2023 10:13:43 +0100 Subject: [PATCH 032/181] Experimental refactor for join message --- include/zenoh-pico/protocol/codec.h | 4 +- include/zenoh-pico/protocol/msg.h | 25 ++++-- include/zenoh-pico/transport/transport.h | 5 +- src/protocol/msg.c | 38 ++++---- src/protocol/msgcodec.c | 105 ++++++++++++++--------- src/transport/multicast/link/rx.c | 59 +++++++------ src/transport/multicast/link/task/join.c | 11 ++- src/transport/transport.c | 27 +++--- tests/z_msgcodec_test.c | 101 +++++++++++++--------- 9 files changed, 220 insertions(+), 155 deletions(-) diff --git a/include/zenoh-pico/protocol/codec.h b/include/zenoh-pico/protocol/codec.h index df5c53b10..d49f4d05b 100644 --- a/include/zenoh-pico/protocol/codec.h +++ b/include/zenoh-pico/protocol/codec.h @@ -38,10 +38,10 @@ int8_t _z_uint8_encode(_z_wbuf_t *buf, uint8_t v); int8_t _z_uint8_decode(uint8_t *u8, _z_zbuf_t *buf); int8_t _z_uint16_encode(_z_wbuf_t *buf, uint16_t v); -int8_t _z_uint16_decode(uint16_t *u8, _z_zbuf_t *buf); +int8_t _z_uint16_decode(uint16_t *u16, _z_zbuf_t *buf); int8_t _z_uint64_encode(_z_wbuf_t *buf, uint64_t v); -int8_t _z_uint64_decode(uint64_t *u8, _z_zbuf_t *buf); +int8_t _z_uint64_decode(uint64_t *u64, _z_zbuf_t *buf); int8_t _z_zint_encode(_z_wbuf_t *buf, _z_zint_t v); int8_t _z_zint_decode(_z_zint_t *zint, _z_zbuf_t *buf); diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 4f90a5848..5cb94516e 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -82,6 +82,14 @@ #define _Z_FLAG_HELLO_L 0x20 // 1 << 5 #define _Z_FLAG_HELLO_Z 0x80 // 1 << 7 +// Join message flags: +// T Lease period if T==1 then the lease period is in seconds else in milliseconds +// S Size params if S==1 then size parameters are exchanged +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_JOIN_T 0x20 // 1 << 5 +#define _Z_FLAG_JOIN_S 0x40 // 1 << 6 +#define _Z_FLAG_JOIN_Z 0x80 // 1 << 7 + // Init message flags: // A Ack if A==1 then the message is an acknowledgment (aka InitAck), otherwise InitSyn // S Size params if S==1 then size parameters are exchanged @@ -111,9 +119,6 @@ #define _Z_FLAG_CLOSE_Z 0x80 // 1 << 7 /* Transport message flags */ -#define _Z_FLAG_T_O 0x80 // 1 << 7 | Options if O==1 then Options are present -#define _Z_FLAG_T_S 0x40 // 1 << 6 | SN Resolution if S==1 then the SN Resolution is present -#define _Z_FLAG_T_T1 0x20 // 1 << 5 | TimeRes if T==1 then the time resolution is in seconds #define _Z_FLAG_T_Z \ 0x20 // 1 << 5 | MixedSlices if Z==1 then the payload contains a mix of raw and shm_info payload @@ -748,11 +753,13 @@ typedef struct { } _z_conduit_sn_list_t; typedef struct { _z_bytes_t _zid; - _z_zint_t _options; _z_zint_t _lease; - _z_zint_t _seq_num_res; - _z_conduit_sn_list_t _next_sns; + uint16_t _batch_size; + _z_conduit_sn_list_t _next_sn; z_whatami_t _whatami; + uint8_t _key_id_res; + uint8_t _req_id_res; + uint8_t _seq_num_res; uint8_t _version; } _z_t_msg_join_t; void _z_t_msg_clear_join(_z_t_msg_join_t *msg); @@ -829,8 +836,8 @@ void _z_t_msg_clear_join(_z_t_msg_join_t *msg); typedef struct { _z_bytes_t _zid; _z_bytes_t _cookie; - z_whatami_t _whatami; uint16_t _batch_size; + z_whatami_t _whatami; uint8_t _key_id_res; uint8_t _req_id_res; uint8_t _seq_num_res; @@ -987,8 +994,8 @@ void _z_t_msg_clear(_z_transport_message_t *msg); /*------------------ Builders ------------------*/ _z_transport_message_t _z_t_msg_make_scout(z_what_t what, _z_bytes_t zid); _z_transport_message_t _z_t_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _z_locator_array_t locators); -_z_transport_message_t _z_t_msg_make_join(uint8_t version, z_whatami_t whatami, _z_zint_t lease, _z_zint_t seq_num_res, - _z_bytes_t zid, _z_conduit_sn_list_t next_sns); +_z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_bytes_t zid, + _z_conduit_sn_list_t next_sn); _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_bytes_t zid); _z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_bytes_t zid, _z_bytes_t cookie); _z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie); diff --git a/include/zenoh-pico/transport/transport.h b/include/zenoh-pico/transport/transport.h index 8d1439122..77fd18acf 100644 --- a/include/zenoh-pico/transport/transport.h +++ b/include/zenoh-pico/transport/transport.h @@ -158,9 +158,8 @@ typedef struct { } _z_transport_unicast_establish_param_t; typedef struct { - _z_zint_t _seq_num_res; - _z_zint_t _initial_sn_tx; - _Bool _is_qos; + uint8_t _seq_num_res; + _z_conduit_sn_list_t _initial_sn_tx; } _z_transport_multicast_establish_param_t; int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unicast_establish_param_t *param); diff --git a/src/protocol/msg.c b/src/protocol/msg.c index d2a3ac67f..196c62e83 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -515,30 +515,30 @@ void _z_t_msg_clear_hello(_z_t_msg_hello_t *msg) { } /*------------------ Join Message ------------------*/ -_z_transport_message_t _z_t_msg_make_join(uint8_t version, z_whatami_t whatami, _z_zint_t lease, _z_zint_t seq_num_res, - _z_bytes_t zid, _z_conduit_sn_list_t next_sns) { +_z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_bytes_t zid, + _z_conduit_sn_list_t next_sn) { _z_transport_message_t msg; + msg._header = _Z_MID_JOIN; - msg._body._join._options = 0; - if (next_sns._is_qos == true) { - _Z_SET_FLAG(msg._body._join._options, _Z_OPT_JOIN_QOS); - } - msg._body._join._version = version; + msg._body._join._version = Z_PROTO_VERSION; msg._body._join._whatami = whatami; msg._body._join._lease = lease; - msg._body._join._seq_num_res = seq_num_res; - msg._body._join._next_sns = next_sns; + msg._body._join._seq_num_res = Z_SN_RESOLUTION; + msg._body._join._key_id_res = Z_KID_RESOLUTION; + msg._body._join._req_id_res = Z_REQ_RESOLUTION; + msg._body._join._batch_size = Z_BATCH_SIZE; + msg._body._join._next_sn = next_sn; msg._body._join._zid = zid; - msg._header = _Z_MID_JOIN; if ((lease % 1000) == 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_T1); - } - if (seq_num_res != Z_SN_RESOLUTION) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_S); + _Z_SET_FLAG(msg._header, _Z_FLAG_JOIN_T); } - if (msg._body._join._options != 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_O); + + if ((msg._body._join._batch_size != _Z_DEFAULT_BATCH_SIZE) || + (msg._body._join._seq_num_res != _Z_DEFAULT_SIZET_SIZE) || + (msg._body._join._key_id_res != _Z_DEFAULT_SIZET_SIZE) || + (msg._body._join._req_id_res != _Z_DEFAULT_SIZET_SIZE)) { + _Z_SET_FLAG(msg._header, _Z_FLAG_JOIN_S); } msg._attachment = NULL; @@ -548,12 +548,14 @@ _z_transport_message_t _z_t_msg_make_join(uint8_t version, z_whatami_t whatami, } void _z_t_msg_copy_join(_z_t_msg_join_t *clone, _z_t_msg_join_t *msg) { - clone->_options = msg->_options; clone->_version = msg->_version; clone->_whatami = msg->_whatami; clone->_lease = msg->_lease; clone->_seq_num_res = msg->_seq_num_res; - clone->_next_sns = msg->_next_sns; + clone->_key_id_res = msg->_key_id_res; + clone->_req_id_res = msg->_req_id_res; + clone->_batch_size = msg->_batch_size; + clone->_next_sn = msg->_next_sn; _z_bytes_copy(&clone->_zid, &msg->_zid); } diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 05c22f8a9..ad4f2a2b6 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1095,31 +1095,38 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_JOIN\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_O) == true) { - _Z_EC(_z_zint_encode(wbf, msg->_options)) + _Z_EC(_z_wbuf_write(wbf, msg->_version)) + + uint8_t cbyte = 0; + cbyte |= (msg->_whatami & 0x03); + cbyte |= ((msg->_zid.len - 1) & 0x0F) << 4; // TODO[protocol]: check if ZID > 0 && <= 16 + _Z_EC(_z_uint8_encode(wbf, cbyte)) + _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) + + if (_Z_HAS_FLAG(header, _Z_FLAG_JOIN_S) == true) { + cbyte = 0; + cbyte |= ((msg->_seq_num_res - 1) & 0x03); + cbyte |= (((msg->_req_id_res - 1) & 0x03) << 2); + cbyte |= (((msg->_key_id_res - 1) & 0x03) << 4); + _Z_EC(_z_uint8_encode(wbf, cbyte)) + _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) } - _Z_EC(_z_uint8_encode(wbf, msg->_version)) - _Z_EC(_z_enum_encode(wbf, msg->_whatami)) - _Z_EC(_z_bytes_encode(wbf, &msg->_zid)) - if (_Z_HAS_FLAG(header, _Z_FLAG_T_T1) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_JOIN_T) == true) { _Z_EC(_z_zint_encode(wbf, msg->_lease / 1000)) } else { _Z_EC(_z_zint_encode(wbf, msg->_lease)) } - if (_Z_HAS_FLAG(header, _Z_FLAG_T_S) == true) { - _Z_EC(_z_zint_encode(wbf, msg->_seq_num_res)) - } - if (_Z_HAS_FLAG(msg->_options, _Z_OPT_JOIN_QOS) == true) { - for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { - _Z_EC(_z_zint_encode(wbf, msg->_next_sns._val._qos[i]._reliable)) - _Z_EC(_z_zint_encode(wbf, msg->_next_sns._val._qos[i]._best_effort)) - } - } else { - _Z_EC(_z_zint_encode(wbf, msg->_next_sns._val._plain._reliable)) - _Z_EC(_z_zint_encode(wbf, msg->_next_sns._val._plain._best_effort)) - } + // if (_Z_HAS_FLAG(msg->_options, _Z_OPT_JOIN_QOS) == true) { + // for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { + // _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._qos[i]._reliable)) + // _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._qos[i]._best_effort)) + // } + // } else { + _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._plain._reliable)) + _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._plain._best_effort)) + // } return ret; } @@ -1128,35 +1135,51 @@ int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_JOIN\n"); int8_t ret = _Z_RES_OK; - if (_Z_HAS_FLAG(header, _Z_FLAG_T_O) == true) { - ret |= _z_zint_decode(&msg->_options, zbf); - } else { - msg->_options = 0; - } ret |= _z_uint8_decode(&msg->_version, zbf); - ret |= _z_enum_decode((int *)&msg->_whatami, zbf); - ret |= _z_bytes_decode(&msg->_zid, zbf); - ret |= _z_zint_decode(&msg->_lease, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_T1) == true) { - msg->_lease = msg->_lease * 1000; - } - if (_Z_HAS_FLAG(header, _Z_FLAG_T_S) == true) { - ret |= _z_zint_decode(&msg->_seq_num_res, zbf); + + uint8_t cbyte = 0; + ret |= _z_uint8_decode(&cbyte, zbf); + msg->_whatami = cbyte & 0x03; + msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; + + if (ret == _Z_RES_OK) { + ret |= _z_bytes_val_decode(&msg->_zid, zbf); + if (ret != _Z_RES_OK) { + msg->_zid = _z_bytes_empty(); + } } else { - msg->_seq_num_res = Z_SN_RESOLUTION; + msg->_zid = _z_bytes_empty(); } - if (_Z_HAS_FLAG(msg->_options, _Z_OPT_JOIN_QOS) == true) { - msg->_next_sns._is_qos = true; - for (uint8_t i = 0; (ret == _Z_RES_OK) && (i < Z_PRIORITIES_NUM); i++) { - ret |= _z_zint_decode(&msg->_next_sns._val._qos[i]._reliable, zbf); - ret |= _z_zint_decode(&msg->_next_sns._val._qos[i]._best_effort, zbf); - } + + if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { + cbyte = 0; + ret |= _z_uint8_decode(&cbyte, zbf); + msg->_seq_num_res = (cbyte & 0x03) + 1; + msg->_req_id_res = ((cbyte >> 2) & 0x03) + 1; + msg->_key_id_res = ((cbyte >> 4) & 0x03) + 1; + ret |= _z_uint16_decode(&msg->_batch_size, zbf); } else { - msg->_next_sns._is_qos = false; - ret |= _z_zint_decode(&msg->_next_sns._val._plain._reliable, zbf); - ret |= _z_zint_decode(&msg->_next_sns._val._plain._best_effort, zbf); + msg->_seq_num_res = _Z_DEFAULT_SIZET_SIZE; + msg->_req_id_res = _Z_DEFAULT_SIZET_SIZE; + msg->_key_id_res = _Z_DEFAULT_SIZET_SIZE; + msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; + } + + ret |= _z_zint_decode(&msg->_lease, zbf); + if (_Z_HAS_FLAG(header, _Z_FLAG_JOIN_T) == true) { + msg->_lease = msg->_lease * 1000; } + // if (_Z_HAS_FLAG(msg->_options, _Z_OPT_JOIN_QOS) == true) { + // for (uint8_t i = 0; (ret == _Z_RES_OK) && (i < Z_PRIORITIES_NUM); i++) { + // ret |= _z_zint_decode(&msg->_next_sn._val._qos[i]._reliable, zbf); + // ret |= _z_zint_decode(&msg->_next_sn._val._qos[i]._best_effort, zbf); + // } + // } else { + ret |= _z_zint_decode(&msg->_next_sn._val._plain._reliable, zbf); + ret |= _z_zint_decode(&msg->_next_sn._val._plain._best_effort, zbf); + // } + return ret; } diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 3dd16f2cc..b7d512be1 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -20,6 +20,7 @@ #include "zenoh-pico/session/utils.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" +#include "zenoh-pico/utils/math.h" #if Z_MULTICAST_TRANSPORT == 1 @@ -138,48 +139,58 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t { entry = (_z_transport_peer_entry_t *)z_malloc(sizeof(_z_transport_peer_entry_t)); if (entry != NULL) { - entry->_remote_addr = _z_bytes_duplicate(addr); - entry->_remote_zid = _z_bytes_duplicate(&t_msg->_body._join._zid); - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_S) == true) { - entry->_seq_num_res = t_msg->_body._join._seq_num_res; - } else { - entry->_seq_num_res = Z_SN_RESOLUTION; - } + entry->_seq_num_res = _z_max_value(t_msg->_body._join._seq_num_res); entry->_seq_num_res_half = entry->_seq_num_res / 2; - _z_conduit_sn_list_copy(&entry->_sn_rx_sns, &t_msg->_body._join._next_sns); - _z_conduit_sn_list_decrement(entry->_seq_num_res, &entry->_sn_rx_sns); + // If the new node has less representing capabilities then it is incompatible to communication + if ((t_msg->_body._join._seq_num_res < Z_SN_RESOLUTION) || + (t_msg->_body._join._key_id_res < Z_KID_RESOLUTION) || + (t_msg->_body._join._req_id_res < Z_REQ_RESOLUTION) || + (t_msg->_body._join._batch_size < Z_BATCH_SIZE)) { + ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; + } + + if (ret == _Z_RES_OK) { + entry->_remote_addr = _z_bytes_duplicate(addr); + entry->_remote_zid = _z_bytes_duplicate(&t_msg->_body._join._zid); + + _z_conduit_sn_list_copy(&entry->_sn_rx_sns, &t_msg->_body._join._next_sn); + _z_conduit_sn_list_decrement(entry->_seq_num_res, &entry->_sn_rx_sns); #if Z_DYNAMIC_MEMORY_ALLOCATION == 1 - entry->_dbuf_reliable = _z_wbuf_make(0, true); - entry->_dbuf_best_effort = _z_wbuf_make(0, true); + entry->_dbuf_reliable = _z_wbuf_make(0, true); + entry->_dbuf_best_effort = _z_wbuf_make(0, true); #else - entry->_dbuf_reliable = _z_wbuf_make(Z_FRAG_MAX_SIZE, false); - entry->_dbuf_best_effort = _z_wbuf_make(Z_FRAG_MAX_SIZE, false); + entry->_dbuf_reliable = _z_wbuf_make(Z_FRAG_MAX_SIZE, false); + entry->_dbuf_best_effort = _z_wbuf_make(Z_FRAG_MAX_SIZE, false); #endif - // Update lease time (set as ms during) - entry->_lease = t_msg->_body._join._lease; - entry->_next_lease = entry->_lease; - entry->_received = true; + // Update lease time (set as ms during) + entry->_lease = t_msg->_body._join._lease; + entry->_next_lease = entry->_lease; + entry->_received = true; - ztm->_peers = _z_transport_peer_entry_list_push(ztm->_peers, entry); + ztm->_peers = _z_transport_peer_entry_list_push(ztm->_peers, entry); + } else { + z_free(entry); + } } else { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - } else // Existing peer - { + } else { // Existing peer entry->_received = true; - // Check if the sn resolution remains the same - if ((_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_S) == true) && - (entry->_seq_num_res != t_msg->_body._join._seq_num_res)) { + // Check if the representing capapbilities are still the same + if ((t_msg->_body._join._seq_num_res < Z_SN_RESOLUTION) || + (t_msg->_body._join._key_id_res < Z_KID_RESOLUTION) || + (t_msg->_body._join._req_id_res < Z_REQ_RESOLUTION) || + (t_msg->_body._join._batch_size < Z_BATCH_SIZE)) { _z_transport_peer_entry_list_drop_filter(ztm->_peers, _z_transport_peer_entry_eq, entry); break; } // Update SNs - _z_conduit_sn_list_copy(&entry->_sn_rx_sns, &t_msg->_body._join._next_sns); + _z_conduit_sn_list_copy(&entry->_sn_rx_sns, &t_msg->_body._join._next_sn); _z_conduit_sn_list_decrement(entry->_seq_num_res, &entry->_sn_rx_sns); // Update lease time (set as ms during) diff --git a/src/transport/multicast/link/task/join.c b/src/transport/multicast/link/task/join.c index 0dbc9b337..a14ab49b9 100644 --- a/src/transport/multicast/link/task/join.c +++ b/src/transport/multicast/link/task/join.c @@ -20,15 +20,14 @@ #if Z_MULTICAST_TRANSPORT == 1 int8_t _zp_multicast_send_join(_z_transport_multicast_t *ztm) { - _z_conduit_sn_list_t next_sns; - next_sns._is_qos = false; // FIXME: make transport aware of qos configuration - next_sns._val._plain._best_effort = ztm->_sn_tx_best_effort; - next_sns._val._plain._reliable = ztm->_sn_tx_reliable; + _z_conduit_sn_list_t next_sn; + next_sn._is_qos = false; // FIXME: make transport aware of qos configuration + next_sn._val._plain._best_effort = ztm->_sn_tx_best_effort; + next_sn._val._plain._reliable = ztm->_sn_tx_reliable; _z_bytes_t zid = _z_bytes_wrap(((_z_session_t *)ztm->_session)->_local_zid.start, ((_z_session_t *)ztm->_session)->_local_zid.len); - _z_transport_message_t jsm = - _z_t_msg_make_join(Z_PROTO_VERSION, Z_WHATAMI_PEER, Z_TRANSPORT_LEASE, Z_SN_RESOLUTION, zid, next_sns); + _z_transport_message_t jsm = _z_t_msg_make_join(Z_WHATAMI_PEER, Z_TRANSPORT_LEASE, zid, next_sn); return _z_multicast_send_t_msg(ztm, &jsm); } diff --git a/src/transport/transport.c b/src/transport/transport.c index a582c3793..848b08799 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -214,8 +214,8 @@ int8_t _z_transport_multicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_mu zt->_transport._multicast._seq_num_res_half = zt->_transport._multicast._seq_num_res / 2; // The initial SN at TX side - zt->_transport._multicast._sn_tx_reliable = param->_initial_sn_tx; - zt->_transport._multicast._sn_tx_best_effort = param->_initial_sn_tx; + zt->_transport._multicast._sn_tx_reliable = param->_initial_sn_tx._val._plain._reliable; + zt->_transport._multicast._sn_tx_best_effort = param->_initial_sn_tx._val._plain._best_effort; // Initialize peer list zt->_transport._multicast._peers = _z_transport_peer_entry_list_new(); @@ -376,26 +376,27 @@ int8_t _z_transport_multicast_open_peer(_z_transport_multicast_establish_param_t const _z_bytes_t *local_zid) { int8_t ret = _Z_RES_OK; - param->_is_qos = false; // FIXME: make transport aware of qos configuration - param->_initial_sn_tx = 0; - param->_seq_num_res = Z_SN_RESOLUTION; + _z_zint_t initial_sn_tx = 0; + z_random_fill(&initial_sn_tx, sizeof(initial_sn_tx)); + initial_sn_tx = initial_sn_tx % Z_SN_RESOLUTION; - // Explicitly send a JOIN message upon startup - // FIXME: make transport aware of qos configuration - _z_conduit_sn_list_t next_sns; - next_sns._is_qos = false; - next_sns._val._plain._best_effort = param->_initial_sn_tx; - next_sns._val._plain._reliable = param->_initial_sn_tx; + _z_conduit_sn_list_t next_sn; + next_sn._val._plain._best_effort = initial_sn_tx; + next_sn._val._plain._reliable = initial_sn_tx; _z_bytes_t zid = _z_bytes_wrap(local_zid->start, local_zid->len); - _z_transport_message_t jsm = - _z_t_msg_make_join(Z_PROTO_VERSION, Z_WHATAMI_PEER, Z_TRANSPORT_LEASE, param->_seq_num_res, zid, next_sns); + _z_transport_message_t jsm = _z_t_msg_make_join(Z_WHATAMI_PEER, Z_TRANSPORT_LEASE, zid, next_sn); // Encode and send the message _Z_INFO("Sending Z_JOIN message\n"); ret = _z_link_send_t_msg(zl, &jsm); _z_t_msg_clear(&jsm); + if (ret == _Z_RES_OK) { + param->_seq_num_res = jsm._body._join._seq_num_res; + param->_initial_sn_tx = next_sn; + } + return ret; } #endif // Z_MULTICAST_TRANSPORT == 1 diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 3fa00824e..26fe754f4 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -1942,33 +1942,43 @@ void hello_message(void) { /*------------------ Join Message ------------------*/ _z_transport_message_t gen_join_message(void) { - uint8_t version = gen_uint8(); - z_whatami_t whatami = 0x04 >> (gen_uint8() % 3); + z_whatami_t whatami = (gen_uint8() % 2) + 1; _z_bytes_t zid = gen_bytes(16); _z_zint_t lease = gen_bool() ? gen_zint() * 1000 : gen_zint(); - _z_zint_t seq_num_res = gen_bool() ? gen_zint() : Z_SN_RESOLUTION; - _z_conduit_sn_list_t next_sns; + _z_conduit_sn_list_t next_sn; if (gen_bool()) { - next_sns._is_qos = true; for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { - next_sns._val._qos[i]._reliable = gen_zint(); - next_sns._val._qos[i]._best_effort = gen_zint(); + next_sn._val._qos[i]._reliable = gen_zint(); + next_sn._val._qos[i]._best_effort = gen_zint(); } } else { - next_sns._is_qos = false; - next_sns._val._plain._reliable = gen_zint(); - next_sns._val._plain._best_effort = gen_zint(); + next_sn._val._plain._reliable = gen_zint(); + next_sn._val._plain._best_effort = gen_zint(); + } + + _z_transport_message_t t_msg = _z_t_msg_make_join(whatami, lease, zid, next_sn); + + if (gen_bool()) { + t_msg._body._join._batch_size = gen_uint16(); + } + + if (gen_bool()) { + t_msg._body._join._seq_num_res = (gen_uint8() % 4) + 1; + } + + if (gen_bool()) { + t_msg._body._join._key_id_res = (gen_uint8() % 4) + 1; } - return _z_t_msg_make_join(version, whatami, lease, seq_num_res, zid, next_sns); + if (gen_bool()) { + t_msg._body._join._req_id_res = (gen_uint8() % 4) + 1; + } + + return t_msg; } void assert_eq_join_message(_z_t_msg_join_t *left, _z_t_msg_join_t *right, uint8_t header) { - printf(" Options (%zu:%zu)", left->_options, right->_options); - assert(left->_options == right->_options); - printf("\n"); - printf(" Version (%u:%u)", left->_version, right->_version); assert(left->_version == right->_version); printf("\n"); @@ -1981,10 +1991,22 @@ void assert_eq_join_message(_z_t_msg_join_t *left, _z_t_msg_join_t *right, uint8 assert_eq_uint8_array(&left->_zid, &right->_zid); printf("\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_S) == true) { - printf(" SN Resolution (%zu:%zu)", left->_seq_num_res, right->_seq_num_res); + if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { + printf(" SN Resolution (%hhu:%hhu)", left->_seq_num_res, right->_seq_num_res); assert(left->_seq_num_res == right->_seq_num_res); printf("\n"); + + printf(" Request ID Resolution (%hhu:%hhu)", left->_req_id_res, right->_req_id_res); + assert(left->_req_id_res == right->_req_id_res); + printf("\n"); + + printf(" KeyExpr ID Resolution (%hhu:%hhu)", left->_key_id_res, right->_key_id_res); + assert(left->_key_id_res == right->_key_id_res); + printf("\n"); + + printf(" Batch Size (%hu:%hu)", left->_batch_size, right->_batch_size); + assert(left->_batch_size == right->_batch_size); + printf("\n"); } printf(" Lease (%zu:%zu)", left->_lease, right->_lease); @@ -1992,27 +2014,28 @@ void assert_eq_join_message(_z_t_msg_join_t *left, _z_t_msg_join_t *right, uint8 printf("\n"); printf(" Next SNs: "); - if (_Z_HAS_FLAG(left->_options, _Z_OPT_JOIN_QOS) == true) { - assert(left->_next_sns._is_qos == true); - assert(right->_next_sns._is_qos == true); - - for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { - printf("R:%zu:%zu ", left->_next_sns._val._qos[i]._reliable, right->_next_sns._val._qos[i]._reliable); - assert(left->_next_sns._val._qos[i]._reliable == right->_next_sns._val._qos[i]._reliable); - printf("B:%zu:%zu ", left->_next_sns._val._qos[i]._best_effort, right->_next_sns._val._qos[i]._best_effort); - assert(left->_next_sns._val._qos[i]._best_effort == right->_next_sns._val._qos[i]._best_effort); - } - printf("\n"); - } else { - assert(left->_next_sns._is_qos == false); - assert(right->_next_sns._is_qos == false); - - printf("R: %zu:%zu", left->_next_sns._val._plain._reliable, right->_next_sns._val._plain._reliable); - assert(left->_next_sns._val._plain._reliable == right->_next_sns._val._plain._reliable); - printf("B: %zu:%zu", left->_next_sns._val._plain._best_effort, right->_next_sns._val._plain._best_effort); - assert(left->_next_sns._val._plain._best_effort == right->_next_sns._val._plain._best_effort); - printf("\n"); - } + // if (_Z_HAS_FLAG(left->_options, _Z_OPT_JOIN_QOS) == true) { + // assert(left->_next_sn._is_qos == true); + // assert(right->_next_sn._is_qos == true); + + // for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { + // printf("R:%zu:%zu ", left->_next_sn._val._qos[i]._reliable, right->_next_sn._val._qos[i]._reliable); + // assert(left->_next_sn._val._qos[i]._reliable == right->_next_sn._val._qos[i]._reliable); + // printf("B:%zu:%zu ", left->_next_sn._val._qos[i]._best_effort, + // right->_next_sn._val._qos[i]._best_effort); assert(left->_next_sn._val._qos[i]._best_effort == + // right->_next_sn._val._qos[i]._best_effort); + // } + // printf("\n"); + // } else { + // assert(left->_next_sn._is_qos == false); + // assert(right->_next_sn._is_qos == false); + + printf("R: %zu:%zu", left->_next_sn._val._plain._reliable, right->_next_sn._val._plain._reliable); + assert(left->_next_sn._val._plain._reliable == right->_next_sn._val._plain._reliable); + printf("B: %zu:%zu", left->_next_sn._val._plain._best_effort, right->_next_sn._val._plain._best_effort); + assert(left->_next_sn._val._plain._best_effort == right->_next_sn._val._plain._best_effort); + printf("\n"); + // } } void join_message(void) { @@ -2402,7 +2425,7 @@ _z_transport_message_t gen_transport_message(void) { } void assert_eq_transport_message(_z_transport_message_t *left, _z_transport_message_t *right) { - // FIXME: This is here to set the extensions flags that is only known at encoding time + // FIXME[protocol]: This is here to set the extensions flags that is only known at encoding time if (_z_msg_ext_vec_len(&left->_extensions) > (size_t)0) { left->_header |= 0x80; } From 25235910b650a2a3104e3932543cb98fb2c5e757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 17 Feb 2023 10:37:18 +0100 Subject: [PATCH 033/181] Change reliability enum for convenience with is_reliable input --- include/zenoh-pico/api/constants.h | 2 +- src/transport/multicast/link/rx.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/zenoh-pico/api/constants.h b/include/zenoh-pico/api/constants.h index 7887a36e9..edd3621ce 100644 --- a/include/zenoh-pico/api/constants.h +++ b/include/zenoh-pico/api/constants.h @@ -168,7 +168,7 @@ typedef enum { * Z_RELIABILITY_BEST_EFFORT: Defines reliability as ``BEST_EFFORT`` * Z_RELIABILITY_RELIABLE: Defines reliability as ``RELIABLE`` */ -typedef enum { Z_RELIABILITY_BEST_EFFORT = 0, Z_RELIABILITY_RELIABLE = 1 } z_reliability_t; +typedef enum { Z_RELIABILITY_BEST_EFFORT = 1, Z_RELIABILITY_RELIABLE = 0 } z_reliability_t; #define Z_RELIABILITY_DEFAULT Z_RELIABILITY_RELIABLE /** diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index b7d512be1..2eb89a1af 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -251,7 +251,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t entry->_sn_rx_sns._val._plain._reliable = t_msg->_body._frame._sn; } else { _z_wbuf_clear(&entry->_dbuf_reliable); - _Z_INFO("Reliable message dropped because it is out of order"); + _Z_INFO("Reliable message dropped because it is out of order\n"); break; } } else { @@ -260,7 +260,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t entry->_sn_rx_sns._val._plain._best_effort = t_msg->_body._frame._sn; } else { _z_wbuf_clear(&entry->_dbuf_best_effort); - _Z_INFO("Best effort message dropped because it is out of order"); + _Z_INFO("Best effort message dropped because it is out of order\n"); break; } } From 5f09070bd515a5ffcbfb968200ddc7e6062d9285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 17 Feb 2023 10:38:48 +0100 Subject: [PATCH 034/181] Anything multiplied by 0 is always 0..._z_max_value --- src/utils/math.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/utils/math.c b/src/utils/math.c index 633ac9813..81e3e6988 100644 --- a/src/utils/math.c +++ b/src/utils/math.c @@ -15,11 +15,10 @@ #include "zenoh-pico/utils/math.h" size_t _z_max_value(uint8_t n_bytes) { - size_t max = 0; - + size_t max = 1; for (uint8_t i = 0; i < (n_bytes * 8); i++) { max *= 2; } - return max; + return (max - 1); } From b586baab687d8ea4b9ceb4553d8ee11b7582ee23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 17 Feb 2023 11:57:34 +0100 Subject: [PATCH 035/181] Fix intres types conversion to nbytes --- include/zenoh-pico/protocol/core.h | 3 ++ include/zenoh-pico/protocol/msg.h | 12 +++---- include/zenoh-pico/transport/transport.h | 8 ++--- src/protocol/core.c | 43 ++++++++++++++++++++++++ src/protocol/msgcodec.c | 24 ++++++------- src/transport/multicast/link/rx.c | 2 +- src/transport/transport.c | 7 ++-- 7 files changed, 73 insertions(+), 26 deletions(-) create mode 100644 src/protocol/core.c diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 639e17397..e549fb76b 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -150,4 +150,7 @@ typedef struct { z_submode_t mode; } _z_subinfo_t; +typedef uint8_t _z_int_res_t; +uint8_t _z_intres_to_nbytes(_z_int_res_t intres); + #endif /* ZENOH_PICO_PROTOCOL_CORE_H */ diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 5cb94516e..a916f4991 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -757,9 +757,9 @@ typedef struct { uint16_t _batch_size; _z_conduit_sn_list_t _next_sn; z_whatami_t _whatami; - uint8_t _key_id_res; - uint8_t _req_id_res; - uint8_t _seq_num_res; + _z_int_res_t _key_id_res; + _z_int_res_t _req_id_res; + _z_int_res_t _seq_num_res; uint8_t _version; } _z_t_msg_join_t; void _z_t_msg_clear_join(_z_t_msg_join_t *msg); @@ -838,9 +838,9 @@ typedef struct { _z_bytes_t _cookie; uint16_t _batch_size; z_whatami_t _whatami; - uint8_t _key_id_res; - uint8_t _req_id_res; - uint8_t _seq_num_res; + _z_int_res_t _key_id_res; + _z_int_res_t _req_id_res; + _z_int_res_t _seq_num_res; uint8_t _version; } _z_t_msg_init_t; void _z_t_msg_clear_init(_z_t_msg_init_t *msg); diff --git a/include/zenoh-pico/transport/transport.h b/include/zenoh-pico/transport/transport.h index 77fd18acf..380d8acc4 100644 --- a/include/zenoh-pico/transport/transport.h +++ b/include/zenoh-pico/transport/transport.h @@ -146,19 +146,19 @@ _Z_LIST_DEFINE(_z_transport, _z_transport_t) typedef struct { _z_bytes_t _remote_zid; - uint8_t _key_id_res; - uint8_t _req_id_res; - uint8_t _seq_num_res; uint16_t _batch_size; _z_zint_t _initial_sn_rx; _z_zint_t _initial_sn_tx; _z_zint_t _lease; z_whatami_t _whatami; + _z_int_res_t _key_id_res; + _z_int_res_t _req_id_res; + _z_int_res_t _seq_num_res; _Bool _is_qos; } _z_transport_unicast_establish_param_t; typedef struct { - uint8_t _seq_num_res; + _z_int_res_t _seq_num_res; _z_conduit_sn_list_t _initial_sn_tx; } _z_transport_multicast_establish_param_t; diff --git a/src/protocol/core.c b/src/protocol/core.c new file mode 100644 index 000000000..325b6a9e7 --- /dev/null +++ b/src/protocol/core.c @@ -0,0 +1,43 @@ +// +// 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, +// + +#include "zenoh-pico/protocol/core.h" + +uint8_t _z_intres_to_nbytes(_z_int_res_t intres) { + uint8_t n_bytes = 0; + + switch (intres) { + case 0: { + n_bytes = 1; + } break; + + case 1: { + n_bytes = 2; + } break; + + case 2: { + n_bytes = 4; + } break; + + case 3: { + n_bytes = 8; + } break; + + default: { + n_bytes = 0; + } + } + + return n_bytes; +} \ No newline at end of file diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index ad4f2a2b6..fbe993723 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1105,9 +1105,9 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg if (_Z_HAS_FLAG(header, _Z_FLAG_JOIN_S) == true) { cbyte = 0; - cbyte |= ((msg->_seq_num_res - 1) & 0x03); - cbyte |= (((msg->_req_id_res - 1) & 0x03) << 2); - cbyte |= (((msg->_key_id_res - 1) & 0x03) << 4); + cbyte |= ((msg->_seq_num_res) & 0x03); + cbyte |= (((msg->_req_id_res) & 0x03) << 2); + cbyte |= (((msg->_key_id_res) & 0x03) << 4); _Z_EC(_z_uint8_encode(wbf, cbyte)) _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) } @@ -1154,9 +1154,9 @@ int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); - msg->_seq_num_res = (cbyte & 0x03) + 1; - msg->_req_id_res = ((cbyte >> 2) & 0x03) + 1; - msg->_key_id_res = ((cbyte >> 4) & 0x03) + 1; + msg->_seq_num_res = (cbyte & 0x03); + msg->_req_id_res = ((cbyte >> 2) & 0x03); + msg->_key_id_res = ((cbyte >> 4) & 0x03); ret |= _z_uint16_decode(&msg->_batch_size, zbf); } else { msg->_seq_num_res = _Z_DEFAULT_SIZET_SIZE; @@ -1202,9 +1202,9 @@ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { cbyte = 0; - cbyte |= ((msg->_seq_num_res - 1) & 0x03); - cbyte |= (((msg->_req_id_res - 1) & 0x03) << 2); - cbyte |= (((msg->_key_id_res - 1) & 0x03) << 4); + cbyte |= ((msg->_seq_num_res) & 0x03); + cbyte |= (((msg->_req_id_res) & 0x03) << 2); + cbyte |= (((msg->_key_id_res) & 0x03) << 4); _Z_EC(_z_uint8_encode(wbf, cbyte)) _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) } @@ -1239,9 +1239,9 @@ int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); - msg->_seq_num_res = (cbyte & 0x03) + 1; - msg->_req_id_res = ((cbyte >> 2) & 0x03) + 1; - msg->_key_id_res = ((cbyte >> 4) & 0x03) + 1; + msg->_seq_num_res = (cbyte & 0x03); + msg->_req_id_res = ((cbyte >> 2) & 0x03); + msg->_key_id_res = ((cbyte >> 4) & 0x03); ret |= _z_uint16_decode(&msg->_batch_size, zbf); } else { msg->_seq_num_res = _Z_DEFAULT_SIZET_SIZE; diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 2eb89a1af..51453bbc9 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -139,7 +139,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t { entry = (_z_transport_peer_entry_t *)z_malloc(sizeof(_z_transport_peer_entry_t)); if (entry != NULL) { - entry->_seq_num_res = _z_max_value(t_msg->_body._join._seq_num_res); + entry->_seq_num_res = _z_max_value(_z_intres_to_nbytes(t_msg->_body._join._seq_num_res)); entry->_seq_num_res_half = entry->_seq_num_res / 2; // If the new node has less representing capabilities then it is incompatible to communication diff --git a/src/transport/transport.c b/src/transport/transport.c index 848b08799..e7fa3b41a 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -15,6 +15,7 @@ #include "zenoh-pico/transport/transport.h" #include +#include #include #include "zenoh-pico/config.h" @@ -124,7 +125,7 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic if (ret == _Z_RES_OK) { // Set default SN resolution - zt->_transport._unicast._seq_num_res = _z_max_value(param->_seq_num_res); + zt->_transport._unicast._seq_num_res = _z_max_value(_z_intres_to_nbytes(param->_seq_num_res)); zt->_transport._unicast._seq_num_res_half = zt->_transport._unicast._seq_num_res / 2; // The initial SN at TX side @@ -210,7 +211,7 @@ int8_t _z_transport_multicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_mu if (ret == _Z_RES_OK) { // Set default SN resolution - zt->_transport._multicast._seq_num_res = _z_max_value(param->_seq_num_res); + zt->_transport._multicast._seq_num_res = _z_max_value(_z_intres_to_nbytes(param->_seq_num_res)); zt->_transport._multicast._seq_num_res_half = zt->_transport._multicast._seq_num_res / 2; // The initial SN at TX side @@ -378,7 +379,7 @@ int8_t _z_transport_multicast_open_peer(_z_transport_multicast_establish_param_t _z_zint_t initial_sn_tx = 0; z_random_fill(&initial_sn_tx, sizeof(initial_sn_tx)); - initial_sn_tx = initial_sn_tx % Z_SN_RESOLUTION; + initial_sn_tx = initial_sn_tx % _z_max_value(_z_intres_to_nbytes(Z_SN_RESOLUTION)); _z_conduit_sn_list_t next_sn; next_sn._val._plain._best_effort = initial_sn_tx; From 5fbc7bc7c05b946b25c8078bdbd136b281d39586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Mon, 20 Feb 2023 11:25:37 +0100 Subject: [PATCH 036/181] Remove artifacts from removed messages --- include/zenoh-pico/protocol/msg.h | 3 --- src/transport/multicast/link/rx.c | 15 --------------- src/transport/unicast/link/rx.c | 15 --------------- tests/z_msgcodec_test.c | 9 --------- 4 files changed, 42 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index a916f4991..f635e6aca 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -49,10 +49,7 @@ #define _Z_MID_INIT 0x03 #define _Z_MID_OPEN 0x04 #define _Z_MID_CLOSE 0x05 -#define _Z_MID_SYNC 0x06 -#define _Z_MID_ACK_NACK 0x07 #define _Z_MID_KEEP_ALIVE 0x08 -#define _Z_MID_PING_PONG 0x09 #define _Z_MID_FRAME 0x0a /* Zenoh Messages */ #define _Z_MID_DECLARE 0x0b diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 51453bbc9..70f045a5c 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -210,16 +210,6 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t break; } - case _Z_MID_SYNC: { - _Z_INFO("Handling of Sync messages not implemented\n"); - break; - } - - case _Z_MID_ACK_NACK: { - _Z_INFO("Handling of AckNack messages not implemented\n"); - break; - } - case _Z_MID_KEEP_ALIVE: { _Z_INFO("Received _Z_KEEP_ALIVE message\n"); if (entry == NULL) { @@ -230,11 +220,6 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t break; } - case _Z_MID_PING_PONG: { - _Z_DEBUG("Handling of PingPong messages not implemented"); - break; - } - case _Z_MID_FRAME: { _Z_INFO("Received _Z_FRAME message\n"); if (entry == NULL) { diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index d376e90e2..5e17c0f17 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -110,26 +110,11 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans break; } - case _Z_MID_SYNC: { - _Z_INFO("Handling of Sync messages not implemented\n"); - break; - } - - case _Z_MID_ACK_NACK: { - _Z_INFO("Handling of AckNack messages not implemented\n"); - break; - } - case _Z_MID_KEEP_ALIVE: { _Z_INFO("Received Z_KEEP_ALIVE message\n"); break; } - case _Z_MID_PING_PONG: { - _Z_INFO("Handling of PingPong messages not implemented\n"); - break; - } - case _Z_MID_FRAME: { _Z_INFO("Received Z_FRAME message\n"); // Check if the SN is correct diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 26fe754f4..ffbe8655e 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -88,18 +88,9 @@ void print_transport_message_type(uint8_t header) { case _Z_MID_CLOSE: printf("Close message"); break; - case _Z_MID_SYNC: - printf("Sync message"); - break; - case _Z_MID_ACK_NACK: - printf("AckNack message"); - break; case _Z_MID_KEEP_ALIVE: printf("KeepAlive message"); break; - case _Z_MID_PING_PONG: - printf("PingPong message"); - break; case _Z_MID_FRAME: printf("Frame message"); break; From eace0157c0433610a24820183a984ad3b2a2eec0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Mon, 20 Feb 2023 11:30:28 +0100 Subject: [PATCH 037/181] Add fragment message --- include/zenoh-pico/protocol/msg.h | 39 +++++++++++++++ include/zenoh-pico/protocol/msgcodec.h | 4 ++ src/protocol/msg.c | 33 ++++++++++++- src/protocol/msgcodec.c | 46 ++++++++++++++++++ tests/z_msgcodec_test.c | 66 ++++++++++++++++++++++++-- 5 files changed, 182 insertions(+), 6 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index f635e6aca..e5c14d1da 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -51,6 +51,7 @@ #define _Z_MID_CLOSE 0x05 #define _Z_MID_KEEP_ALIVE 0x08 #define _Z_MID_FRAME 0x0a +#define _Z_MID_FRAGMENT 0x09 /* Zenoh Messages */ #define _Z_MID_DECLARE 0x0b #define _Z_MID_DATA 0x0c @@ -109,6 +110,14 @@ #define _Z_FLAG_FRAME_R 0x20 // 1 << 5 #define _Z_FLAG_FRAME_Z 0x80 // 1 << 7 +// Frame message flags: +// R Reliable if R==1 it concerns the reliable channel, else the best-effort channel +// M More if M==1 then other fragments will follow +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_FRAGMENT_R 0x20 // 1 << 5 +#define _Z_FLAG_FRAGMENT_M 0x40 // 1 << 6 +#define _Z_FLAG_FRAGMENT_Z 0x80 // 1 << 7 + // Close message flags: // S Session Close if S==1 Session close or S==0 Link close // Z Extensions if Z==1 then Zenoh extensions are present @@ -968,6 +977,34 @@ typedef struct { } _z_t_msg_frame_t; void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg); +/*------------------ Fragment Message ------------------*/ +// The Fragment message is used to transmit on the wire large Zenoh Message that require fragmentation +// because they are larger thatn the maximum batch size (i.e. 2^16-1) and/or the link MTU. +// +// The [`Fragment`] message flow is the following: +// +// Flags: +// - R: Reliable if R==1 it concerns the reliable channel, else the best-effort channel +// - M: More if M==1 then other fragments will follow +// - Z: Extensions if Z==1 then zenoh extensions will follow. +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|M|R| FRAGMENT| +// +-+-+-+---------+ +// % seq num % +// +---------------+ +// ~ [FragExts] ~ if Flag(Z)==1 +// +---------------+ +// ~ [u8] ~ +// +---------------+ +// +typedef struct { + _z_payload_t _payload; + _z_zint_t _sn; +} _z_t_msg_fragment_t; +void _z_t_msg_clear_fragment(_z_t_msg_fragment_t *msg); + /*------------------ Transport Message ------------------*/ typedef union { _z_t_msg_scout_t _scout; @@ -978,6 +1015,7 @@ typedef union { _z_t_msg_close_t _close; _z_t_msg_keep_alive_t _keep_alive; _z_t_msg_frame_t _frame; + _z_t_msg_fragment_t _fragment; } _z_transport_body_t; typedef struct { @@ -1001,6 +1039,7 @@ _z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only); _z_transport_message_t _z_t_msg_make_keep_alive(void); _z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_zenoh_message_vec_t messages, _Bool is_reliable); _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable); +_z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_payload_t messages, _Bool is_reliable, _Bool is_last); /*------------------ Copy ------------------*/ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg); diff --git a/include/zenoh-pico/protocol/msgcodec.h b/include/zenoh-pico/protocol/msgcodec.h index 392fde16e..795b6506b 100644 --- a/include/zenoh-pico/protocol/msgcodec.h +++ b/include/zenoh-pico/protocol/msgcodec.h @@ -159,6 +159,10 @@ int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *m int8_t _z_frame_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg); +int8_t _z_fragment_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); + // ------------------ Discovery Message ------------------ int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_scout_t *msg); int8_t _z_scout_decode_na(_z_t_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header); diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 196c62e83..c6c8faf25 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -724,9 +724,11 @@ _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliabl if (is_reliable == true) { _Z_SET_FLAG(msg._header, _Z_FLAG_FRAME_R); } + + msg._body._frame._messages = _z_zenoh_message_vec_make(0); + msg._attachment = NULL; msg._extensions = _z_msg_ext_vec_make(0); - msg._body._frame._messages = _z_zenoh_message_vec_make(0); return msg; } @@ -740,8 +742,9 @@ _z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_zenoh_message_vec_t _Z_SET_FLAG(msg._header, _Z_FLAG_FRAME_R); } - msg._attachment = NULL; msg._body._frame._messages = messages; + + msg._attachment = NULL; msg._extensions = _z_msg_ext_vec_make(0); return msg; @@ -754,6 +757,32 @@ void _z_t_msg_copy_frame(_z_t_msg_frame_t *clone, _z_t_msg_frame_t *msg) { void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg) { _z_zenoh_message_vec_clear(&msg->_messages); } +/*------------------ Fragment Message ------------------*/ +_z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_payload_t payload, _Bool is_reliable, _Bool is_last) { + _z_transport_message_t msg; + msg._header = _Z_MID_FRAGMENT; + if (is_last == true) { + _Z_SET_FLAG(msg._header, _Z_FLAG_FRAGMENT_M); + } + if (is_reliable == true) { + _Z_SET_FLAG(msg._header, _Z_FLAG_FRAGMENT_R); + } + + msg._body._fragment._sn = sn; + msg._body._fragment._payload = payload; + + msg._attachment = NULL; + msg._extensions = _z_msg_ext_vec_make(0); + + return msg; +} + +void _z_t_msg_copy_fragment(_z_t_msg_fragment_t *clone, _z_t_msg_fragment_t *msg) { + _z_bytes_copy(&clone->_payload, &msg->_payload); +} + +void _z_t_msg_clear_fragment(_z_t_msg_fragment_t *msg) { _z_bytes_clear(&msg->_payload); } + /*------------------ Transport Message ------------------*/ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg) { clone->_header = msg->_header; diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index fbe993723..b8805ba60 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1424,6 +1424,43 @@ int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { return _z_frame_decode_na(msg, zbf, header); } +/*------------------ Fragment Message ------------------*/ +int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg) { + (void)(header); + + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_FRAGMENT\n"); + + _Z_EC(_z_zint_encode(wbf, msg->_sn)) + + _Z_EC(_z_bytes_encode(wbf, &msg->_payload)) + + return ret; +} + +int8_t _z_fragment_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { + (void)(header); + + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Decoding _Z_MID_FRAME\n"); + + ret |= _z_zint_decode(&msg->_sn, zbf); + if (ret == _Z_RES_OK) { + ret |= _z_bytes_decode(&msg->_payload, zbf); + if (ret != _Z_RES_OK) { + msg->_payload = _z_bytes_empty(); + } + } else { + msg->_payload = _z_bytes_empty(); + } + + return ret; +} + +int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_fragment_decode_na(msg, zbf, header); +} + /*------------------ Transport Message ------------------*/ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t *msg) { int8_t ret = _Z_RES_OK; @@ -1445,6 +1482,10 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t ret |= _z_frame_encode(wbf, msg->_header, &msg->_body._frame); } break; + case _Z_MID_FRAGMENT: { + ret |= _z_fragment_encode(wbf, msg->_header, &msg->_body._fragment); + } break; + case _Z_MID_KEEP_ALIVE: { ret |= _z_keep_alive_encode(wbf, msg->_header, &msg->_body._keep_alive); } break; @@ -1508,6 +1549,11 @@ int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *zb is_last = true; } break; + case _Z_MID_FRAGMENT: { + ret |= _z_fragment_decode(&msg->_body._fragment, zbf, msg->_header); + is_last = true; + } break; + case _Z_MID_ATTACHMENT: { ret |= _z_attachment_decode(&msg->_attachment, zbf, msg->_header); } break; diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index ffbe8655e..f48bf5fdb 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -94,6 +94,9 @@ void print_transport_message_type(uint8_t header) { case _Z_MID_FRAME: printf("Frame message"); break; + case _Z_MID_FRAGMENT: + printf("Frame message"); + break; default: assert(0); break; @@ -2366,14 +2369,62 @@ void frame_message(void) { _z_wbuf_clear(&wbf); } +/*------------------ Fragment Message ------------------*/ +_z_transport_message_t gen_fragment_message(void) { + _z_zint_t sn = gen_zint(); + _Bool is_reliable = gen_bool(); + _Bool is_last = gen_bool(); + _z_payload_t payload = gen_bytes(gen_uint16() % 60000); // 60000 is just to make sure that we are not generating a + // payload bigger than what the fragment can hold + return _z_t_msg_make_fragment(sn, payload, is_reliable, is_last); +} + +void assert_eq_fragment_message(_z_t_msg_fragment_t *left, _z_t_msg_fragment_t *right, uint8_t header) { + (void)(header); + + printf(" SN (%zu:%zu)", left->_sn, right->_sn); + assert(left->_sn == right->_sn); + printf("\n"); + + assert_eq_payload(&left->_payload, &right->_payload); +} + +void fragment_message(void) { + printf("\n>> Fragment message\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_transport_message_t t_msg = gen_fragment_message(); + assert(_Z_MID(t_msg._header) == _Z_MID_FRAGMENT); + + _z_t_msg_fragment_t e_fr = t_msg._body._fragment; + + // Encode + int8_t res = _z_fragment_encode(&wbf, t_msg._header, &e_fr); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_t_msg_fragment_t d_fr; + res = _z_fragment_decode(&d_fr, &zbf, t_msg._header); + assert(res == _Z_RES_OK); + + assert_eq_fragment_message(&e_fr, &d_fr, t_msg._header); + + // Frame + _z_t_msg_clear_fragment(&d_fr); + _z_t_msg_clear(&t_msg); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + /*------------------ Transport Message ------------------*/ _z_transport_message_t gen_transport_message(void) { _z_transport_message_t e_tm; - uint8_t mids[] = { - _Z_MID_SCOUT, _Z_MID_HELLO, _Z_MID_JOIN, _Z_MID_INIT, - _Z_MID_OPEN, _Z_MID_CLOSE, _Z_MID_KEEP_ALIVE, _Z_MID_FRAME, - }; + uint8_t mids[] = {_Z_MID_SCOUT, _Z_MID_HELLO, _Z_MID_JOIN, _Z_MID_INIT, _Z_MID_OPEN, + _Z_MID_CLOSE, _Z_MID_KEEP_ALIVE, _Z_MID_FRAME, _Z_MID_FRAGMENT}; uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); switch (mids[i]) { @@ -2401,6 +2452,9 @@ _z_transport_message_t gen_transport_message(void) { case _Z_MID_FRAME: e_tm = gen_frame_message(); break; + case _Z_MID_FRAGMENT: + e_tm = gen_fragment_message(); + break; default: assert(0); break; @@ -2460,6 +2514,9 @@ void assert_eq_transport_message(_z_transport_message_t *left, _z_transport_mess case _Z_MID_FRAME: assert_eq_frame_message(&left->_body._frame, &right->_body._frame, left->_header); break; + case _Z_MID_FRAGMENT: + assert_eq_fragment_message(&left->_body._fragment, &right->_body._fragment, left->_header); + break; default: assert(0); break; @@ -2772,6 +2829,7 @@ int main(void) { close_message(); keep_alive_message(); frame_message(); + fragment_message(); transport_message(); batch(); // fragmentation(); From 2e4d65eb28002984253f4403ccc218968c12ad37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Tue, 21 Feb 2023 16:01:51 +0100 Subject: [PATCH 038/181] Z flag for extensions should be common --- include/zenoh-pico/protocol/msg.h | 14 ++++---------- src/protocol/msgcodec.c | 6 +++--- tests/z_msgcodec_test.c | 4 ++-- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index e5c14d1da..550172e32 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -68,17 +68,17 @@ /*=============================*/ /* Message flags */ /*=============================*/ +#define _Z_FLAG_T_Z 0x80 // 1 << 7 + // Scout message flags: // I ZenohID if I==1 then the ZenohID is present // Z Extensions if Z==1 then Zenoh extensions are present #define _Z_FLAG_SCOUT_I 0x08 // 1 << 3 -#define _Z_FLAG_SCOUT_Z 0x80 // 1 << 7 // Hello message flags: // L Locators if L==1 then Locators are present // Z Extensions if Z==1 then Zenoh extensions are present #define _Z_FLAG_HELLO_L 0x20 // 1 << 5 -#define _Z_FLAG_HELLO_Z 0x80 // 1 << 7 // Join message flags: // T Lease period if T==1 then the lease period is in seconds else in milliseconds @@ -86,7 +86,6 @@ // Z Extensions if Z==1 then Zenoh extensions are present #define _Z_FLAG_JOIN_T 0x20 // 1 << 5 #define _Z_FLAG_JOIN_S 0x40 // 1 << 6 -#define _Z_FLAG_JOIN_Z 0x80 // 1 << 7 // Init message flags: // A Ack if A==1 then the message is an acknowledgment (aka InitAck), otherwise InitSyn @@ -94,7 +93,6 @@ // Z Extensions if Z==1 then Zenoh extensions are present #define _Z_FLAG_INIT_A 0x20 // 1 << 5 #define _Z_FLAG_INIT_S 0x40 // 1 << 6 -#define _Z_FLAG_INIT_Z 0x80 // 1 << 7 // Open message flags: // A Ack if A==1 then the message is an acknowledgment (aka OpenAck), otherwise OpenSyn @@ -102,13 +100,11 @@ // Z Extensions if Z==1 then Zenoh extensions are present #define _Z_FLAG_OPEN_A 0x20 // 1 << 5 #define _Z_FLAG_OPEN_T 0x40 // 1 << 6 -#define _Z_FLAG_OPEN_Z 0x80 // 1 << 7 // Frame message flags: // R Reliable if R==1 it concerns the reliable channel, else the best-effort channel // Z Extensions if Z==1 then Zenoh extensions are present #define _Z_FLAG_FRAME_R 0x20 // 1 << 5 -#define _Z_FLAG_FRAME_Z 0x80 // 1 << 7 // Frame message flags: // R Reliable if R==1 it concerns the reliable channel, else the best-effort channel @@ -116,16 +112,14 @@ // Z Extensions if Z==1 then Zenoh extensions are present #define _Z_FLAG_FRAGMENT_R 0x20 // 1 << 5 #define _Z_FLAG_FRAGMENT_M 0x40 // 1 << 6 -#define _Z_FLAG_FRAGMENT_Z 0x80 // 1 << 7 // Close message flags: // S Session Close if S==1 Session close or S==0 Link close // Z Extensions if Z==1 then Zenoh extensions are present #define _Z_FLAG_CLOSE_S 0x20 // 1 << 5 -#define _Z_FLAG_CLOSE_Z 0x80 // 1 << 7 -/* Transport message flags */ -#define _Z_FLAG_T_Z \ +/* Attachment message flags */ +#define _Z_FLAG_A_Z \ 0x20 // 1 << 5 | MixedSlices if Z==1 then the payload contains a mix of raw and shm_info payload /* Zenoh message flags */ diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index b8805ba60..3c57cce80 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -198,7 +198,7 @@ int8_t _z_attachment_encode(_z_wbuf_t *wbf, const _z_attachment_t *msg) { // WARNING: we do not support sliced content in zenoh-pico. // Disable the SLICED flag to be on the safe side. - _Z_EC(_z_wbuf_write(wbf, msg->_header & ~_Z_FLAG_T_Z)) + _Z_EC(_z_wbuf_write(wbf, msg->_header & ~_Z_FLAG_A_Z)) ret |= _z_payload_encode(wbf, &msg->_payload); @@ -213,7 +213,7 @@ int8_t _z_attachment_decode_na(_z_attachment_t *atch, _z_zbuf_t *zbf, uint8_t he // WARNING: we do not support sliced content in zenoh-pico. // Return error in case the payload is sliced. - if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == false) { + if (_Z_HAS_FLAG(header, _Z_FLAG_A_Z) == false) { ret |= _z_payload_decode(&atch->_payload, zbf); } @@ -1473,7 +1473,7 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t uint8_t header = msg->_header; size_t n_ext = _z_msg_ext_vec_len(&msg->_extensions); if (n_ext > 0) { - header |= 0x80; + header |= _Z_FLAG_T_Z; } _Z_EC(_z_wbuf_write(wbf, header)) diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index f48bf5fdb..1df5c2a7a 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -2472,7 +2472,7 @@ _z_transport_message_t gen_transport_message(void) { void assert_eq_transport_message(_z_transport_message_t *left, _z_transport_message_t *right) { // FIXME[protocol]: This is here to set the extensions flags that is only known at encoding time if (_z_msg_ext_vec_len(&left->_extensions) > (size_t)0) { - left->_header |= 0x80; + left->_header |= _Z_FLAG_T_Z; } // Test message decorators @@ -2629,7 +2629,7 @@ void batch(void) { // t_msg._body._frame._sn = sn; // if (is_reliable == true) { -// _Z_SET_FLAG(t_msg._header, _Z_FLAG_T_R); +// _Z_SET_FLAG(t_msg._header, _Z_FLAG_FRAGMENT_R); // }; // if (is_fragment == true) { From 9e096b3488b4d720793ce210795fe0d143e63ef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 22 Feb 2023 10:46:19 +0100 Subject: [PATCH 039/181] Fragment logic to reset --- src/protocol/msg.c | 8 ++ src/transport/multicast/link/rx.c | 146 ++++++++++++++++++++---------- src/transport/transport.c | 4 +- src/transport/unicast/link/rx.c | 102 +++++++++++++++------ src/transport/unicast/link/tx.c | 9 +- 5 files changed, 186 insertions(+), 83 deletions(-) diff --git a/src/protocol/msg.c b/src/protocol/msg.c index c6c8faf25..d9a76175e 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -823,6 +823,10 @@ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg) { _z_t_msg_copy_frame(&clone->_body._frame, &msg->_body._frame); } break; + case _Z_MID_FRAGMENT: { + _z_t_msg_copy_fragment(&clone->_body._fragment, &msg->_body._fragment); + } break; + default: { _Z_DEBUG("WARNING: Trying to free session message with unknown ID(%d)\n", mid); } break; @@ -869,6 +873,10 @@ void _z_t_msg_clear(_z_transport_message_t *msg) { _z_t_msg_clear_frame(&msg->_body._frame); } break; + case _Z_MID_FRAGMENT: { + _z_t_msg_clear_fragment(&msg->_body._fragment); + } break; + default: { _Z_DEBUG("WARNING: Trying to free session message with unknown ID(%d)\n", mid); } break; diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 70f045a5c..732e1a242 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -109,6 +109,102 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t // Mark the session that we have received data from this peer _z_transport_peer_entry_t *entry = _z_find_peer_entry(ztm->_peers, addr); switch (_Z_MID(t_msg->_header)) { + case _Z_MID_FRAME: { + _Z_INFO("Received _Z_FRAME message\n"); + if (entry == NULL) { + break; + } + entry->_received = true; + + // Check if the SN is correct + if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAME_R) == true) { + // @TODO: amend once reliability is in place. For the time being only + // monothonic SNs are ensured + if (_z_sn_precedes(entry->_seq_num_res_half, entry->_sn_rx_sns._val._plain._reliable, + t_msg->_body._frame._sn) == true) { + entry->_sn_rx_sns._val._plain._reliable = t_msg->_body._frame._sn; + } else { + _z_wbuf_clear(&entry->_dbuf_reliable); + _Z_INFO("Reliable message dropped because it is out of order\n"); + break; + } + } else { + if (_z_sn_precedes(entry->_seq_num_res_half, entry->_sn_rx_sns._val._plain._best_effort, + t_msg->_body._frame._sn) == true) { + entry->_sn_rx_sns._val._plain._best_effort = t_msg->_body._frame._sn; + } else { + _z_wbuf_clear(&entry->_dbuf_best_effort); + _Z_INFO("Best effort message dropped because it is out of order\n"); + break; + } + } + + // Handle all the zenoh message, one by one + size_t len = _z_vec_len(&t_msg->_body._frame._messages); + for (size_t i = 0; i < len; i++) { + _z_handle_zenoh_message(ztm->_session, + (_z_zenoh_message_t *)_z_vec_get(&t_msg->_body._frame._messages, i)); + } + + break; + } + + case _Z_MID_FRAGMENT: { + _Z_INFO("Received Z_FRAGMENT message\n"); + if (entry == NULL) { + break; + } + entry->_received = true; + + _z_wbuf_t *dbuf = _Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAGMENT_R) + ? &entry->_dbuf_reliable + : &entry->_dbuf_best_effort; // Select the right defragmentation buffer + + _Bool drop = false; + if ((_z_wbuf_len(dbuf) + t_msg->_body._fragment._payload.len) > Z_FRAG_MAX_SIZE) { + // Filling the wbuf capacity as a way to signling the last fragment to reset the dbuf + // Otherwise, last (smaller) fragments can be understood as a complete message + _z_wbuf_write_bytes(dbuf, t_msg->_body._fragment._payload.start, 0, _z_wbuf_space_left(dbuf)); + drop = true; + } else { + _z_wbuf_write_bytes(dbuf, t_msg->_body._fragment._payload.start, 0, + t_msg->_body._fragment._payload.len); + } + + if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAGMENT_M) == false) { + if (drop == true) { // Drop message if it exceeds the fragmentation size + _z_wbuf_reset(dbuf); + break; + } + + _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_zenoh_message_decode(&zm, &zbf); + if (ret == _Z_RES_OK) { + _z_handle_zenoh_message(ztm->_session, &zm); + _z_msg_clear(&zm); // Clear must be explicitly called for fragmented zenoh messages. Non-fragmented + // zenoh messages are released when their transport message is released. + } + + // Free the decoding buffer + _z_zbuf_clear(&zbf); + // Reset the defragmentation buffer + _z_wbuf_reset(dbuf); + } + break; + } + + case _Z_MID_KEEP_ALIVE: { + _Z_INFO("Received _Z_KEEP_ALIVE message\n"); + if (entry == NULL) { + break; + } + entry->_received = true; + + break; + } + case _Z_MID_SCOUT: { // Do nothing, multicast transports are not expected to handle SCOUT messages on established sessions break; @@ -210,56 +306,6 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t break; } - case _Z_MID_KEEP_ALIVE: { - _Z_INFO("Received _Z_KEEP_ALIVE message\n"); - if (entry == NULL) { - break; - } - entry->_received = true; - - break; - } - - case _Z_MID_FRAME: { - _Z_INFO("Received _Z_FRAME message\n"); - if (entry == NULL) { - break; - } - entry->_received = true; - - // Check if the SN is correct - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAME_R) == true) { - // @TODO: amend once reliability is in place. For the time being only - // monothonic SNs are ensured - if (_z_sn_precedes(entry->_seq_num_res_half, entry->_sn_rx_sns._val._plain._reliable, - t_msg->_body._frame._sn) == true) { - entry->_sn_rx_sns._val._plain._reliable = t_msg->_body._frame._sn; - } else { - _z_wbuf_clear(&entry->_dbuf_reliable); - _Z_INFO("Reliable message dropped because it is out of order\n"); - break; - } - } else { - if (_z_sn_precedes(entry->_seq_num_res_half, entry->_sn_rx_sns._val._plain._best_effort, - t_msg->_body._frame._sn) == true) { - entry->_sn_rx_sns._val._plain._best_effort = t_msg->_body._frame._sn; - } else { - _z_wbuf_clear(&entry->_dbuf_best_effort); - _Z_INFO("Best effort message dropped because it is out of order\n"); - break; - } - } - - // Handle all the zenoh message, one by one - size_t len = _z_vec_len(&t_msg->_body._frame._messages); - for (size_t i = 0; i < len; i++) { - _z_handle_zenoh_message(ztm->_session, - (_z_zenoh_message_t *)_z_vec_get(&t_msg->_body._frame._messages, i)); - } - - break; - } - default: { _Z_ERROR("Unknown session message ID\n"); break; diff --git a/src/transport/transport.c b/src/transport/transport.c index e7fa3b41a..640815a0c 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -91,7 +91,7 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic // Initialize the read and write buffers if (ret == _Z_RES_OK) { uint16_t mtu = (zl->_mtu < Z_BATCH_SIZE) ? zl->_mtu : Z_BATCH_SIZE; - zt->_transport._unicast._wbuf = _z_wbuf_make(mtu, false); + zt->_transport._unicast._wbuf = _z_wbuf_make(mtu, true); zt->_transport._unicast._zbuf = _z_zbuf_make(Z_BATCH_SIZE); // Initialize the defragmentation buffers @@ -190,7 +190,7 @@ int8_t _z_transport_multicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_mu // Initialize the read and write buffers if (ret == _Z_RES_OK) { uint16_t mtu = (zl->_mtu < Z_BATCH_SIZE) ? zl->_mtu : Z_BATCH_SIZE; - zt->_transport._multicast._wbuf = _z_wbuf_make(mtu, false); + zt->_transport._multicast._wbuf = _z_wbuf_make(mtu, true); zt->_transport._multicast._zbuf = _z_zbuf_make(Z_BATCH_SIZE); // Clean up the buffers if one of them failed to be allocated diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index 5e17c0f17..f069accf0 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -85,36 +85,6 @@ int8_t _z_unicast_recv_t_msg(_z_transport_unicast_t *ztu, _z_transport_message_t int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_transport_message_t *t_msg) { switch (_Z_MID(t_msg->_header)) { - case _Z_MID_SCOUT: { - _Z_INFO("Handling of Scout messages not implemented\n"); - break; - } - - case _Z_MID_HELLO: { - // Do nothing, zenoh-pico clients are not expected to handle hello messages - break; - } - - case _Z_MID_INIT: { - // Do nothing, zenoh clients are not expected to handle accept messages on established sessions - break; - } - - case _Z_MID_OPEN: { - // Do nothing, zenoh clients are not expected to handle accept messages on established sessions - break; - } - - case _Z_MID_CLOSE: { - _Z_INFO("Closing session as requested by the remote peer\n"); - break; - } - - case _Z_MID_KEEP_ALIVE: { - _Z_INFO("Received Z_KEEP_ALIVE message\n"); - break; - } - case _Z_MID_FRAME: { _Z_INFO("Received Z_FRAME message\n"); // Check if the SN is correct @@ -148,6 +118,78 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans break; } + case _Z_MID_FRAGMENT: { + _Z_INFO("Received Z_FRAGMENT message\n"); + + _z_wbuf_t *dbuf = _Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAGMENT_R) + ? &ztu->_dbuf_reliable + : &ztu->_dbuf_best_effort; // Select the right defragmentation buffer + + _Bool drop = false; + if ((_z_wbuf_len(dbuf) + t_msg->_body._fragment._payload.len) > Z_FRAG_MAX_SIZE) { + // Filling the wbuf capacity as a way to signling the last fragment to reset the dbuf + // Otherwise, last (smaller) fragments can be understood as a complete message + _z_wbuf_write_bytes(dbuf, t_msg->_body._fragment._payload.start, 0, _z_wbuf_space_left(dbuf)); + drop = true; + } else { + _z_wbuf_write_bytes(dbuf, t_msg->_body._fragment._payload.start, 0, + t_msg->_body._fragment._payload.len); + } + + if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAGMENT_M) == false) { + if (drop == true) { // Drop message if it exceeds the fragmentation size + _z_wbuf_reset(dbuf); + break; + } + + _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_zenoh_message_decode(&zm, &zbf); + if (ret == _Z_RES_OK) { + _z_handle_zenoh_message(ztu->_session, &zm); + _z_msg_clear(&zm); // Clear must be explicitly called for fragmented zenoh messages. Non-fragmented + // zenoh messages are released when their transport message is released. + } + + // Free the decoding buffer + _z_zbuf_clear(&zbf); + // Reset the defragmentation buffer + _z_wbuf_reset(dbuf); + } + break; + } + + case _Z_MID_KEEP_ALIVE: { + _Z_INFO("Received Z_KEEP_ALIVE message\n"); + break; + } + + case _Z_MID_SCOUT: { + _Z_INFO("Handling of Scout messages not implemented\n"); + break; + } + + case _Z_MID_HELLO: { + // Do nothing, zenoh-pico clients are not expected to handle hello messages + break; + } + + case _Z_MID_INIT: { + // Do nothing, zenoh clients are not expected to handle accept messages on established sessions + break; + } + + case _Z_MID_OPEN: { + // Do nothing, zenoh clients are not expected to handle accept messages on established sessions + break; + } + + case _Z_MID_CLOSE: { + _Z_INFO("Closing session as requested by the remote peer\n"); + break; + } + default: { _Z_ERROR("Unknown session message ID\n"); break; diff --git a/src/transport/unicast/link/tx.c b/src/transport/unicast/link/tx.c index 6e8db641d..54c2df646 100644 --- a/src/transport/unicast/link/tx.c +++ b/src/transport/unicast/link/tx.c @@ -15,6 +15,7 @@ #include "zenoh-pico/transport/link/tx.h" #include "zenoh-pico/config.h" +#include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" @@ -107,7 +108,13 @@ int8_t _z_unicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reli // Write the message legnth in the reserved space if needed __unsafe_z_finalize_wbuf(&ztu->_wbuf, _Z_LINK_IS_STREAMED(ztu->_link._capabilities)); - ret = _z_link_send_wbuf(&ztu->_link, &ztu->_wbuf); // Send the wbuf on the socket + if (ztu->_wbuf._ioss._len == 1) { + ret = _z_link_send_wbuf(&ztu->_link, &ztu->_wbuf); // Send the wbuf on the socket + } else { + // Change the MID + + // for () + } if (ret == _Z_RES_OK) { ztu->_transmitted = true; // Mark the session that we have transmitted data } From 062b50a9894fb44c6c73c53b1cf8cb0a9ca1e7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 1 Mar 2023 21:39:05 +0100 Subject: [PATCH 040/181] Mitigate issues related to non-initialized data --- src/api/api.c | 2 ++ src/link/multicast/udp.c | 1 + src/session/scout.c | 2 ++ src/transport/manager.c | 4 ++++ 4 files changed, 9 insertions(+) diff --git a/src/api/api.c b/src/api/api.c index 09ff661a9..bb04a2b13 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -415,6 +415,8 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb z_owned_session_t z_open(z_owned_config_t *config) { z_owned_session_t zs = {._value = (_z_session_t *)z_malloc(sizeof(_z_session_t))}; + memset(zs._value, 0, sizeof(_z_session_t)); + if (zs._value != NULL) { if (_z_open(zs._value, config->_value) != _Z_RES_OK) { z_free(zs._value); diff --git a/src/link/multicast/udp.c b/src/link/multicast/udp.c index 5c3f510fc..e74a6dce5 100644 --- a/src/link/multicast/udp.c +++ b/src/link/multicast/udp.c @@ -172,6 +172,7 @@ int8_t _z_new_link_udp_multicast(_z_link_t *zl, _z_endpoint_t endpoint) { char *s_address = __z_parse_address_segment_udp_multicast(endpoint._locator._address); char *s_port = __z_parse_port_segment_udp_multicast(endpoint._locator._address); ret = _z_create_endpoint_udp(&zl->_socket._udp._rep, s_address, s_port); + memset(&zl->_socket._udp._lep, 0, sizeof(zl->_socket._udp._lep)); z_free(s_address); z_free(s_port); diff --git a/src/session/scout.c b/src/session/scout.c index 6d39c8128..b1959d6c8 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -42,6 +42,8 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig if (err == _Z_RES_OK) { _z_link_t zl; + memset(&zl, 0, sizeof(_z_link_t)); + err = _z_open_link(&zl, locator); if (err == _Z_RES_OK) { // Send the scout message diff --git a/src/transport/manager.c b/src/transport/manager.c index 831fab955..49c0cbab2 100644 --- a/src/transport/manager.c +++ b/src/transport/manager.c @@ -21,6 +21,8 @@ int8_t _z_new_transport_client(_z_transport_t *zt, char *locator, _z_bytes_t *lo int8_t ret = _Z_RES_OK; _z_link_t zl; + memset(&zl, 0, sizeof(_z_link_t)); + ret = _z_open_link(&zl, locator); if (ret == _Z_RES_OK) { #if Z_UNICAST_TRANSPORT == 1 @@ -58,6 +60,8 @@ int8_t _z_new_transport_peer(_z_transport_t *zt, char *locator, _z_bytes_t *loca int8_t ret = _Z_RES_OK; _z_link_t zl; + memset(&zl, 0, sizeof(_z_link_t)); + ret = _z_listen_link(&zl, locator); if (ret == _Z_RES_OK) { #if Z_UNICAST_TRANSPORT == 1 From 5b4734114fad6f7fc484322b53f0f02872171ac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Mon, 6 Mar 2023 20:39:08 +0100 Subject: [PATCH 041/181] Avoid casting on encode and decode enums (#167) (#169) * Avoid casting on encode and decode enums * Propogate enum decoding errors --- include/zenoh-pico/protocol/codec.h | 10 ++++-- src/protocol/codec.c | 50 +++++++++++++++++++++++------ src/protocol/msgcodec.c | 47 ++++++++------------------- 3 files changed, 63 insertions(+), 44 deletions(-) diff --git a/include/zenoh-pico/protocol/codec.h b/include/zenoh-pico/protocol/codec.h index 6d17c25b2..6ed578635 100644 --- a/include/zenoh-pico/protocol/codec.h +++ b/include/zenoh-pico/protocol/codec.h @@ -28,8 +28,14 @@ } /*------------------ Internal Zenoh-net Macros ------------------*/ -int8_t _z_enum_encode(_z_wbuf_t *wbf, int en); -int8_t _z_enum_decode(int *en, _z_zbuf_t *zbf); +int8_t _z_encoding_prefix_encode(_z_wbuf_t *wbf, z_encoding_prefix_t en); +int8_t _z_encoding_prefix_decode(z_encoding_prefix_t *en, _z_zbuf_t *zbf); +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); +int8_t _z_query_target_encode(_z_wbuf_t *wbf, z_query_target_t en); +int8_t _z_query_target_decode(z_query_target_t *en, _z_zbuf_t *zbf); +int8_t _z_whatami_encode(_z_wbuf_t *wbf, z_whatami_t en); +int8_t _z_whatami_decode(z_whatami_t *en, _z_zbuf_t *zbf); int8_t _z_uint_encode(_z_wbuf_t *buf, unsigned int v); int8_t _z_uint_decode(unsigned int *u, _z_zbuf_t *buf); diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 9e71bf1fc..d1c5c9ec1 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -49,18 +49,50 @@ int8_t _z_period_decode_na(_z_period_t *p, _z_zbuf_t *buf) { int8_t _z_period_decode(_z_period_t *p, _z_zbuf_t *buf) { return _z_period_decode_na(p, buf); } /*------------------ uint8 -------------------*/ -int8_t _z_enum_encode(_z_wbuf_t *wbf, int en) { return _z_wbuf_write(wbf, (uint8_t)en); } +int8_t _z_encoding_prefix_encode(_z_wbuf_t *wbf, z_encoding_prefix_t en) { return _z_zint_encode(wbf, en); } -int8_t _z_enum_decode(int *en, _z_zbuf_t *zbf) { +int8_t _z_encoding_prefix_decode(z_encoding_prefix_t *en, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; - *en = 0; - if (_z_zbuf_can_read(zbf) == true) { - *en = _z_zbuf_read(zbf); - } else { - _Z_DEBUG("WARNING: Not enough bytes to read\n"); - ret |= _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; - } + _z_zint_t tmp; + ret |= _z_zint_decode(&tmp, zbf); + *en = tmp; + + return ret; +} + +int8_t _z_consolidation_mode_encode(_z_wbuf_t *wbf, z_consolidation_mode_t en) { return _z_zint_encode(wbf, en); } + +int8_t _z_consolidation_mode_decode(z_consolidation_mode_t *en, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + + _z_zint_t tmp; + ret |= _z_zint_decode(&tmp, zbf); + *en = tmp; + + return ret; +} + +int8_t _z_query_target_encode(_z_wbuf_t *wbf, z_query_target_t en) { return _z_zint_encode(wbf, en); } + +int8_t _z_query_target_decode(z_query_target_t *en, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + + _z_zint_t tmp; + ret |= _z_zint_decode(&tmp, zbf); + *en = tmp; + + return ret; +} + +int8_t _z_whatami_encode(_z_wbuf_t *wbf, z_whatami_t en) { return _z_zint_encode(wbf, en); } + +int8_t _z_whatami_decode(z_whatami_t *en, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + + _z_zint_t tmp; + ret |= _z_zint_decode(&tmp, zbf); + *en = tmp; return ret; } diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index f58373be5..ee28ce060 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -652,7 +652,7 @@ int8_t _z_data_info_encode(_z_wbuf_t *wbf, const _z_data_info_t *fld) { _Z_EC(_z_uint8_encode(wbf, fld->_kind)) } if (_Z_HAS_FLAG(fld->_flags, _Z_DATA_INFO_ENC) == true) { - _Z_EC(_z_enum_encode(wbf, fld->_encoding.prefix)) + _Z_EC(_z_encoding_prefix_encode(wbf, fld->_encoding.prefix)) _Z_EC(_z_bytes_encode(wbf, &fld->_encoding.suffix)) } if (_Z_HAS_FLAG(fld->_flags, _Z_DATA_INFO_TSTAMP) == true) { @@ -683,7 +683,7 @@ int8_t _z_data_info_decode_na(_z_data_info_t *di, _z_zbuf_t *zbf) { } if (_Z_HAS_FLAG(di->_flags, _Z_DATA_INFO_ENC) == true) { - ret |= _z_enum_decode((int *)&di->_encoding.prefix, zbf); + ret |= _z_encoding_prefix_decode(&di->_encoding.prefix, zbf); ret |= _z_bytes_decode(&di->_encoding.suffix, zbf); } else { di->_encoding.prefix = Z_ENCODING_PREFIX_EMPTY; @@ -790,16 +790,6 @@ int8_t _z_pull_decode(_z_msg_pull_t *msg, _z_zbuf_t *zbf, uint8_t header) { } /*------------------ Query Message ------------------*/ -int8_t _z_query_consolidation_encode(_z_wbuf_t *wbf, const z_consolidation_mode_t *qc) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _QUERY_CONSOLIDATION\n"); - _z_zint_t mode = *qc; - - ret |= _z_zint_encode(wbf, mode); - - return ret; -} - int8_t _z_query_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_query_t *msg) { int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_QUERY\n"); @@ -811,9 +801,9 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_query_t *msg _Z_EC(_z_zint_encode(wbf, msg->_qid)) if (_Z_HAS_FLAG(header, _Z_FLAG_Z_T) == true) { - _Z_EC(_z_enum_encode(wbf, msg->_target)) + _Z_EC(_z_query_target_encode(wbf, msg->_target)) } - ret |= _z_query_consolidation_encode(wbf, &msg->_consolidation); + ret |= _z_consolidation_mode_encode(wbf, msg->_consolidation); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_B) == true) { _Z_EC(_z_data_info_encode(wbf, &msg->_info)) @@ -823,15 +813,6 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_query_t *msg return ret; } -int8_t _z_query_consolidation_decode(z_consolidation_mode_t *qcm, _z_zbuf_t *zbf) { - _Z_DEBUG("Decoding _QUERY_CONSOLIDATION\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_enum_decode((int *)qcm, zbf); - - return ret; -} - int8_t _z_query_decode_na(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_QUERY\n"); int8_t ret = _Z_RES_OK; @@ -840,11 +821,11 @@ int8_t _z_query_decode_na(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header) { ret |= _z_str_decode(&msg->_parameters, zbf); ret |= _z_zint_decode(&msg->_qid, zbf); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_T) == true) { - ret |= _z_enum_decode((int *)&msg->_target, zbf); + ret |= _z_query_target_decode(&msg->_target, zbf); } else { msg->_target = Z_QUERY_TARGET_BEST_MATCHING; } - ret |= _z_query_consolidation_decode(&msg->_consolidation, zbf); + ret |= _z_consolidation_mode_decode(&msg->_consolidation, zbf); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_I) == true) { ret |= _z_data_info_decode(&msg->_info, zbf); ret |= _z_payload_decode(&msg->_payload, zbf); @@ -989,7 +970,7 @@ int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_scout_t *m _Z_DEBUG("Encoding _Z_MID_SCOUT\n"); if (_Z_HAS_FLAG(header, _Z_FLAG_T_W) == true) { - _Z_EC(_z_enum_encode(wbf, msg->_what)) + _Z_EC(_z_whatami_encode(wbf, msg->_what)) } return ret; @@ -1000,7 +981,7 @@ int8_t _z_scout_decode_na(_z_t_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) int8_t ret = _Z_RES_OK; if (_Z_HAS_FLAG(header, _Z_FLAG_T_W) == true) { - ret |= _z_enum_decode((int *)&msg->_what, zbf); + ret |= _z_whatami_decode(&msg->_what, zbf); } else { msg->_what = Z_WHATAMI_ROUTER; } @@ -1021,7 +1002,7 @@ int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_hello_t *m _Z_EC(_z_bytes_encode(wbf, &msg->_zid)) } if (_Z_HAS_FLAG(header, _Z_FLAG_T_W) == true) { - _Z_EC(_z_enum_encode(wbf, msg->_whatami)) + _Z_EC(_z_whatami_encode(wbf, msg->_whatami)) } if (_Z_HAS_FLAG(header, _Z_FLAG_T_L) == true) { _Z_EC(_z_locators_encode(wbf, &msg->_locators)) @@ -1041,7 +1022,7 @@ int8_t _z_hello_decode_na(_z_t_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) } if (_Z_HAS_FLAG(header, _Z_FLAG_T_W) == true) { - ret |= _z_enum_decode((int *)&msg->_whatami, zbf); + ret |= _z_whatami_decode(&msg->_whatami, zbf); } else { msg->_whatami = Z_WHATAMI_ROUTER; } @@ -1068,7 +1049,7 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg _Z_EC(_z_zint_encode(wbf, msg->_options)) } _Z_EC(_z_uint8_encode(wbf, msg->_version)) - _Z_EC(_z_enum_encode(wbf, msg->_whatami)) + _Z_EC(_z_whatami_encode(wbf, msg->_whatami)) _Z_EC(_z_bytes_encode(wbf, &msg->_zid)) if (_Z_HAS_FLAG(header, _Z_FLAG_T_T1) == true) { @@ -1103,7 +1084,7 @@ int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { msg->_options = 0; } ret |= _z_uint8_decode(&msg->_version, zbf); - ret |= _z_enum_decode((int *)&msg->_whatami, zbf); + ret |= _z_whatami_decode(&msg->_whatami, zbf); ret |= _z_bytes_decode(&msg->_zid, zbf); ret |= _z_zint_decode(&msg->_lease, zbf); if (_Z_HAS_FLAG(header, _Z_FLAG_T_T1) == true) { @@ -1144,7 +1125,7 @@ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg if (_Z_HAS_FLAG(header, _Z_FLAG_T_A) == false) { _Z_EC(_z_wbuf_write(wbf, msg->_version)) } - _Z_EC(_z_enum_encode(wbf, msg->_whatami)) + _Z_EC(_z_whatami_encode(wbf, msg->_whatami)) _Z_EC(_z_bytes_encode(wbf, &msg->_zid)) if (_Z_HAS_FLAG(header, _Z_FLAG_T_S) == true) { _Z_EC(_z_zint_encode(wbf, msg->_sn_resolution)) @@ -1170,7 +1151,7 @@ int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { } else { msg->_version = Z_PROTO_VERSION; } - ret |= _z_enum_decode((int *)&msg->_whatami, zbf); + ret |= _z_whatami_decode(&msg->_whatami, zbf); ret |= _z_bytes_decode(&msg->_zid, zbf); if (_Z_HAS_FLAG(header, _Z_FLAG_T_S) == true) { ret |= _z_zint_decode(&msg->_sn_resolution, zbf); From 829d654551319069082a5712b98539e1abf5f51c Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Thu, 16 Mar 2023 15:17:59 +0100 Subject: [PATCH 042/181] canonizer fix for one-character chunks (#172) * canonizer fix for one-character chunks * undef ndebug right before tests --- src/protocol/keyexpr.c | 6 ++++-- tests/z_keyexpr_test.c | 45 ++++++++++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/protocol/keyexpr.c b/src/protocol/keyexpr.c index 3dfe3092d..e94b484dc 100644 --- a/src/protocol/keyexpr.c +++ b/src/protocol/keyexpr.c @@ -662,8 +662,10 @@ zp_keyexpr_canon_status_t _z_keyexpr_canonize(char *start, size_t *len) { } break; case 1: { - __zp_ke_write_chunk(&writer, "*", 1, write_start); - continue; + if (reader[0] == '*') { + __zp_ke_write_chunk(&writer, "*", 1, write_start); + continue; + } } break; case 2: { diff --git a/tests/z_keyexpr_test.c b/tests/z_keyexpr_test.c index 22ac1cfd9..3cfede7b5 100644 --- a/tests/z_keyexpr_test.c +++ b/tests/z_keyexpr_test.c @@ -11,14 +11,17 @@ // Contributors: // ZettaScale Zenoh Team, -#include #include #include #include "zenoh-pico/api/primitives.h" #include "zenoh-pico/protocol/keyexpr.h" -int main(void) { +#undef NDEBUG +#include + +int main(void) +{ assert(_z_keyexpr_intersects("a", strlen("a"), "a", strlen("a"))); assert(_z_keyexpr_intersects("a/b", strlen("a/b"), "a/b", strlen("a/b"))); assert(_z_keyexpr_intersects("*", strlen("*"), "abc", strlen("abc"))); @@ -237,15 +240,22 @@ int main(void) { assert(!zp_keyexpr_includes_null_terminated("a/**/b$*", "a/ebc")); assert(!zp_keyexpr_includes_null_terminated("a/**/$*b", "a/cbc")); -#define N 26 + // clang-format off + +#define N 31 const char *input[N] = {"greetings/hello/there", "greetings/good/*/morning", "greetings/*", "greetings/*/**", "greetings/$*", "greetings/**/*/morning", + "greetings/**/*/g/morning", + "greetings/**/*/m", "greetings/**/*", "greetings/**/**", + "greetings/**/**/morning", + "greetings/**/**/g/morning", + "greetings/**/**/m", "greetings/**/*/**", "$*", "$*$*", @@ -281,6 +291,11 @@ int main(void) { Z_KEYEXPR_CANON_SUCCESS, Z_KEYEXPR_CANON_SUCCESS, Z_KEYEXPR_CANON_SUCCESS, + Z_KEYEXPR_CANON_SUCCESS, + Z_KEYEXPR_CANON_SUCCESS, + Z_KEYEXPR_CANON_SUCCESS, + Z_KEYEXPR_CANON_SUCCESS, + Z_KEYEXPR_CANON_SUCCESS, Z_KEYEXPR_CANON_STARS_IN_CHUNK, Z_KEYEXPR_CANON_EMPTY_CHUNK, Z_KEYEXPR_CANON_EMPTY_CHUNK, @@ -296,8 +311,13 @@ int main(void) { "greetings/*/**", "greetings/*", "greetings/*/**/morning", + "greetings/*/**/g/morning", + "greetings/*/**/m", "greetings/*/**", "greetings/**", + "greetings/**/morning", + "greetings/**/g/morning", + "greetings/**/m", "greetings/*/**", "*", "*", @@ -317,7 +337,10 @@ int main(void) { "greetings/**/*/e$", "greetings/**/*/$e"}; - for (int i = 0; i < N; i++) { + // clang-format on + + for (int i = 0; i < N; i++) + { const char *ke = input[i]; char *canon = (char *)malloc(128); memset(canon, 0, 128); @@ -325,16 +348,17 @@ int main(void) { size_t canon_len = strlen(canon); zp_keyexpr_canon_status_t status = z_keyexpr_canonize(canon, &canon_len); printf("%s ", ke); - printf(" Status: %d : %d", status, expected[i]); + printf(" Status: %d : %d\n", status, expected[i]); assert(status == expected[i]); - if (status == Z_KEYEXPR_CANON_SUCCESS) { - printf(" Match: %.*s : %s", (int)canon_len, canon, canonized[i]); + if (status == Z_KEYEXPR_CANON_SUCCESS) + { + printf(" Match: %.*s : %s\n", (int)canon_len, canon, canonized[i]); assert(strncmp(canonized[i], canon, canon_len) == 0); } - printf("\n"); } - for (int i = 0; i < N; i++) { + for (int i = 0; i < N; i++) + { const char *ke = input[i]; char *canon = (char *)malloc(128); memset(canon, 0, 128); @@ -344,7 +368,8 @@ int main(void) { printf("%s ", ke); printf(" Status: %d : %d", status, expected[i]); assert(status == expected[i]); - if (status == Z_KEYEXPR_CANON_SUCCESS) { + if (status == Z_KEYEXPR_CANON_SUCCESS) + { printf(" Match: %.*s : %s", (int)canon_len, canon, canonized[i]); assert(strcmp(canonized[i], canon) == 0); } From 1ca97fa3318cc11365b23b9600c87ef581dbd0aa Mon Sep 17 00:00:00 2001 From: Nathan Dumont Date: Tue, 21 Mar 2023 10:14:47 +0000 Subject: [PATCH 043/181] Fixed wrong type in serial CRC function (#177) (#176) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed wrong type in checksum calculation * Didn't mean to change the loop parameter type * Avoid unary minus operator warning when applied to unsigned types * Set crc polynomial as a define --------- Co-authored-by: Carlos Guimarães --- src/utils/checksum.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/utils/checksum.c b/src/utils/checksum.c index c1054045c..ef466f778 100644 --- a/src/utils/checksum.c +++ b/src/utils/checksum.c @@ -14,12 +14,14 @@ #include "zenoh-pico/utils/checksum.h" +#define __CRC_POLYNOMIAL 0x04C11DB7 + uint32_t _z_crc32(const uint8_t *message, size_t len) { - int32_t crc = 0xFFFFFFFF; + uint32_t crc = 0xFFFFFFFF; for (size_t i = 0; i < len; i++) { - crc = crc ^ (int32_t)message[i]; - for (int j = 0; j < 8; j++) { - crc = (crc >> 1) ^ ((int32_t)0x04C11DB7 & (-(crc & (int32_t)1))); + crc = crc ^ (uint32_t)message[i]; + for (uint8_t j = 0; j < (uint8_t)8; j++) { + crc = (crc >> 1) ^ ((uint32_t)__CRC_POLYNOMIAL & (uint32_t)(-(int32_t)(crc & (uint32_t)1))); } } return ~crc; From 6783736567774bc444270faa44ce7e089652d676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Tue, 21 Mar 2023 14:33:33 +0100 Subject: [PATCH 044/181] Fixes dynamic memory allocation wrt buffer allocation checks (#178) (#179) --- src/transport/transport.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/transport/transport.c b/src/transport/transport.c index 0abf6e2a5..b9605abb9 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -109,8 +109,13 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic // Clean up the buffers if one of them failed to be allocated if ((_z_wbuf_capacity(&zt->_transport._unicast._wbuf) != mtu) || (_z_zbuf_capacity(&zt->_transport._unicast._zbuf) != Z_BATCH_SIZE_RX) || +#if Z_DYNAMIC_MEMORY_ALLOCATION == 0 (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_reliable) != dbuf_size) || (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_best_effort) != dbuf_size)) { +#else + (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_reliable) != Z_IOSLICE_SIZE) || + (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_best_effort) != Z_IOSLICE_SIZE)) { +#endif ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; #if Z_MULTI_THREAD == 1 From 55913c309ec238f8300082a33e8585dfbfcae109 Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Wed, 22 Mar 2023 11:39:27 +0100 Subject: [PATCH 045/181] Fix and align return values for keyexpr handlers (#173) (#174) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * return -1 only if keyexpr invalid * test corrected * Update keyexpr functions --------- Co-authored-by: Carlos Guimarães --- include/zenoh-pico/api/primitives.h | 22 +-- src/api/api.c | 70 +++++---- tests/z_api_alignment_test.c | 18 +-- tests/z_keyexpr_test.c | 217 ++++++++++++++-------------- 4 files changed, 170 insertions(+), 157 deletions(-) diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index b34a6be83..3815fa306 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -154,7 +154,7 @@ int8_t zp_keyexpr_canonize_null_terminated(char *start); * * Returns: * Returns ``0`` if ``l`` includes ``r``, i.e. the set defined by ``l`` contains every key belonging to the set - * defined by ``r``. Otherwise, it returns a ``negative value``. + * defined by ``r``. Otherwise, it returns a ``-1``, or other ``negative value`` for errors. */ int8_t z_keyexpr_includes(z_keyexpr_t l, z_keyexpr_t r); @@ -168,10 +168,10 @@ int8_t z_keyexpr_includes(z_keyexpr_t l, z_keyexpr_t r); * rlen: Number of characters in ``r``. * * Returns: - * Returns true if ``l`` includes ``r``, i.e. the set defined by ``l`` contains every key belonging to the set defined - * by ``r``. + * Returns ``0`` if ``l`` includes ``r``, i.e. the set defined by ``l`` contains every key belonging to the set + * defined by ``r``. Otherwise, it returns a ``-1``, or other ``negative value`` for errors. */ -_Bool zp_keyexpr_includes_null_terminated(const char *l, const char *r); +int8_t zp_keyexpr_includes_null_terminated(const char *l, const char *r); /** * Check if a given keyexpr intersects with another keyexpr. @@ -182,7 +182,7 @@ _Bool zp_keyexpr_includes_null_terminated(const char *l, const char *r); * * Returns: * Returns ``0`` if the keyexprs intersect, i.e. there exists at least one key which is contained in both of the - * sets defined by ``l`` and ``r``. Otherwise, it returns ``negative value``. + * sets defined by ``l`` and ``r``. Otherwise, it returns a ``-1``, or other ``negative value`` for errors. */ int8_t z_keyexpr_intersects(z_keyexpr_t l, z_keyexpr_t r); @@ -196,10 +196,10 @@ int8_t z_keyexpr_intersects(z_keyexpr_t l, z_keyexpr_t r); * rlen: Number of characters in ``r``. * * Returns: - * Returns ``true`` if the keyexprs intersect, i.e. there exists at least one key which is contained in both of the - * sets defined by ``l`` and ``r``. Otherwise, it returns ``false``. + * Returns ``0`` if the keyexprs intersect, i.e. there exists at least one key which is contained in both of the + * sets defined by ``l`` and ``r``. Otherwise, it returns a ``-1``, or other ``negative value`` for errors. */ -_Bool zp_keyexpr_intersect_null_terminated(const char *l, const char *r); +int8_t zp_keyexpr_intersect_null_terminated(const char *l, const char *r); /** * Check if a two keyexprs are equal. @@ -209,7 +209,7 @@ _Bool zp_keyexpr_intersect_null_terminated(const char *l, const char *r); * r: The second keyexpr. * * Returns: - * Returns ``0`` if both ``l`` and ``r`` are equal, or ``negative value`` otherwise. + * Returns ``0`` if both ``l`` and ``r`` are equal. Otherwise, it returns a ``-1``, or other ``negative value`` for errors. */ int8_t z_keyexpr_equals(z_keyexpr_t l, z_keyexpr_t r); @@ -223,9 +223,9 @@ int8_t z_keyexpr_equals(z_keyexpr_t l, z_keyexpr_t r); * rlen: Number of characters in ``r``. * * Returns: - * Returns ``true`` if both ``l`` and ``r`` are equal, or ``false`` otherwise. + * Returns ``0`` if both ``l`` and ``r`` are equal. Otherwise, it returns a ``-1``, or other ``negative value`` for errors. */ -_Bool zp_keyexpr_equals_null_terminated(const char *l, const char *r); +int8_t zp_keyexpr_equals_null_terminated(const char *l, const char *r); /** * Return a new, zenoh-allocated, empty configuration. diff --git a/src/api/api.c b/src/api/api.c index bb04a2b13..58bf66fba 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -96,59 +96,75 @@ int8_t zp_keyexpr_canonize_null_terminated(char *start) { int8_t z_keyexpr_includes(z_keyexpr_t l, z_keyexpr_t r) { int8_t ret = 0; - if ((l._id != Z_RESOURCE_ID_NONE) || (r._id != Z_RESOURCE_ID_NONE)) { - ret = -1; - } - - if (_z_keyexpr_includes(l._suffix, strlen(l._suffix), r._suffix, strlen(r._suffix)) == false) { - ret = -1; + if ((l._id == Z_RESOURCE_ID_NONE) && (r._id == Z_RESOURCE_ID_NONE)) { + ret = zp_keyexpr_includes_null_terminated(l._suffix, r._suffix); + } else { + ret = _Z_ERR_GENERIC; } return ret; } -_Bool zp_keyexpr_includes_null_terminated(const char *l, const char *r) { - return _z_keyexpr_includes(l, strlen(l), r, strlen(r)); +int8_t zp_keyexpr_includes_null_terminated(const char *l, const char *r) { + int8_t ret = 0; + + if (l != NULL && r != NULL) { + ret = _z_keyexpr_includes(l, strlen(l), r, strlen(r)) == true ? 0 : -1; + } else { + ret = _Z_ERR_GENERIC; + } + + return ret; } int8_t z_keyexpr_intersects(z_keyexpr_t l, z_keyexpr_t r) { int8_t ret = 0; - if ((l._id != Z_RESOURCE_ID_NONE) || (r._id != Z_RESOURCE_ID_NONE)) { - ret = -1; - } - - if (_z_keyexpr_intersects(l._suffix, strlen(l._suffix), r._suffix, strlen(r._suffix)) == false) { - ret = -1; + if ((l._id == Z_RESOURCE_ID_NONE) && (r._id == Z_RESOURCE_ID_NONE)) { + ret = zp_keyexpr_intersect_null_terminated(l._suffix, r._suffix); + } else { + ret = _Z_ERR_GENERIC; } return ret; } -_Bool zp_keyexpr_intersect_null_terminated(const char *l, const char *r) { - return _z_keyexpr_intersects(l, strlen(l), r, strlen(r)); +int8_t zp_keyexpr_intersect_null_terminated(const char *l, const char *r) { + int8_t ret = -1; + + if (l != NULL && r != NULL) { + ret = (_z_keyexpr_intersects(l, strlen(l), r, strlen(r)) == true) ? 0 : -1; + } else { + ret = _Z_ERR_GENERIC; + } + + return ret; } int8_t z_keyexpr_equals(z_keyexpr_t l, z_keyexpr_t r) { - int8_t ret = -1; + int8_t ret = 0; if ((l._id == Z_RESOURCE_ID_NONE) && (r._id == Z_RESOURCE_ID_NONE)) { - size_t llen = strlen(l._suffix); - if (llen == strlen(r._suffix)) { - if (strncmp(l._suffix, r._suffix, llen) == 0) { - ret = 0; - } - } + ret = zp_keyexpr_equals_null_terminated(l._suffix, r._suffix); + } else { + ret = _Z_ERR_GENERIC; } return ret; } -_Bool zp_keyexpr_equals_null_terminated(const char *l, const char *r) { - _Bool ret = true; +int8_t zp_keyexpr_equals_null_terminated(const char *l, const char *r) { + int8_t ret = -1; - if (strcmp(l, r) != 0) { - ret = false; + if (l != NULL && r != NULL) { + size_t llen = strlen(l); + if (llen == strlen(r)) { + if (strncmp(l, r, llen) == 0) { + ret = 0; + } + } + } else { + ret = _Z_ERR_GENERIC; } return ret; diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 4157d7971..7f44326be 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -118,20 +118,20 @@ int main(int argc, char **argv) { int8_t _ret_int8 = z_keyexpr_includes(z_keyexpr("demo/example/**"), z_keyexpr("demo/example/a")); assert(_ret_int8 == 0); #ifdef ZENOH_PICO - _ret_bool = zp_keyexpr_includes_null_terminated("demo/example/**", "demo/example/a"); - assert(_ret_bool == true); + _ret_int8 = zp_keyexpr_includes_null_terminated("demo/example/**", "demo/example/a"); + assert(_ret_int8 == 0); #endif - _ret_bool = z_keyexpr_intersects(z_keyexpr("demo/example/**"), z_keyexpr("demo/example/a")); + _ret_int8 = z_keyexpr_intersects(z_keyexpr("demo/example/**"), z_keyexpr("demo/example/a")); assert(_ret_int8 == 0); #ifdef ZENOH_PICO - _ret_bool = zp_keyexpr_intersect_null_terminated("demo/example/**", "demo/example/a"); - assert(_ret_bool == true); -#endif - _ret_bool = z_keyexpr_equals(z_keyexpr("demo/example/**"), z_keyexpr("demo/example")); + _ret_int8 = zp_keyexpr_intersect_null_terminated("demo/example/**", "demo/example/a"); assert(_ret_int8 == 0); +#endif + _ret_int8 = z_keyexpr_equals(z_keyexpr("demo/example/**"), z_keyexpr("demo/example")); + assert(_ret_int8 == -1); #ifdef ZENOH_PICO - _ret_bool = zp_keyexpr_equals_null_terminated("demo/example/**", "demo/example"); - assert(_ret_bool == false); + _ret_int8 = zp_keyexpr_equals_null_terminated("demo/example/**", "demo/example"); + assert(_ret_int8 == -1); #endif sleep(SLEEP); diff --git a/tests/z_keyexpr_test.c b/tests/z_keyexpr_test.c index 3cfede7b5..9b9567f7a 100644 --- a/tests/z_keyexpr_test.c +++ b/tests/z_keyexpr_test.c @@ -100,77 +100,77 @@ int main(void) assert(!_z_keyexpr_intersects("a/**/b$*", strlen("a/**/b$*"), "a/ebc", strlen("a/ebc"))); assert(!_z_keyexpr_intersects("a/**/$*b", strlen("a/**/$*b"), "a/cbc", strlen("a/cbc"))); - assert(zp_keyexpr_intersect_null_terminated("a", "a")); - assert(zp_keyexpr_intersect_null_terminated("a/b", "a/b")); - assert(zp_keyexpr_intersect_null_terminated("*", "abc")); - assert(zp_keyexpr_intersect_null_terminated("*", "abc")); - assert(zp_keyexpr_intersect_null_terminated("*", "abc")); - assert(zp_keyexpr_intersect_null_terminated("*", "xxx")); - assert(zp_keyexpr_intersect_null_terminated("ab$*", "abcd")); - assert(zp_keyexpr_intersect_null_terminated("ab$*d", "abcd")); - assert(zp_keyexpr_intersect_null_terminated("ab$*", "ab")); - assert(!zp_keyexpr_intersect_null_terminated("ab/*", "ab")); - assert(zp_keyexpr_intersect_null_terminated("a/*/c/*/e", "a/b/c/d/e")); - assert(zp_keyexpr_intersect_null_terminated("a/**/d/**/l", "a/b/c/d/e/f/g/h/i/l")); - assert(zp_keyexpr_intersect_null_terminated("a/**/d/**/l", "a/d/foo/l")); - assert(zp_keyexpr_intersect_null_terminated("a/$*b/c/$*d/e", "a/xb/c/xd/e")); - assert(!zp_keyexpr_intersect_null_terminated("a/*/c/*/e", "a/c/e")); - assert(!zp_keyexpr_intersect_null_terminated("a/*/c/*/e", "a/b/c/d/x/e")); - assert(!zp_keyexpr_intersect_null_terminated("ab$*cd", "abxxcxxd")); - assert(zp_keyexpr_intersect_null_terminated("ab$*cd", "abxxcxxcd")); - assert(!zp_keyexpr_intersect_null_terminated("ab$*cd", "abxxcxxcdx")); - assert(zp_keyexpr_intersect_null_terminated("**", "abc")); - assert(zp_keyexpr_intersect_null_terminated("**", "a/b/c")); - assert(zp_keyexpr_intersect_null_terminated("ab/**", "ab")); - assert(zp_keyexpr_intersect_null_terminated("**/xyz", "a/b/xyz/d/e/f/xyz")); - assert(!zp_keyexpr_intersect_null_terminated("**/xyz$*xyz", "a/b/xyz/d/e/f/xyz")); - assert(zp_keyexpr_intersect_null_terminated("a/**/c/**/e", "a/b/b/b/c/d/d/d/e")); - assert(zp_keyexpr_intersect_null_terminated("a/**/c/**/e", "a/c/e")); - assert(zp_keyexpr_intersect_null_terminated("a/**/c/*/e/*", "a/b/b/b/c/d/d/c/d/e/f")); - assert(!zp_keyexpr_intersect_null_terminated("a/**/c/*/e/*", "a/b/b/b/c/d/d/c/d/d/e/f")); - assert(!zp_keyexpr_intersect_null_terminated("ab$*cd", "abxxcxxcdx")); - assert(zp_keyexpr_intersect_null_terminated("x/abc", "x/abc")); - assert(!zp_keyexpr_intersect_null_terminated("x/abc", "abc")); - assert(zp_keyexpr_intersect_null_terminated("x/*", "x/abc")); - assert(!zp_keyexpr_intersect_null_terminated("x/*", "abc")); - assert(!zp_keyexpr_intersect_null_terminated("*", "x/abc")); - assert(zp_keyexpr_intersect_null_terminated("x/*", "x/abc$*")); - assert(zp_keyexpr_intersect_null_terminated("x/$*abc", "x/abc$*")); - assert(zp_keyexpr_intersect_null_terminated("x/a$*", "x/abc$*")); - assert(zp_keyexpr_intersect_null_terminated("x/a$*de", "x/abc$*de")); - assert(zp_keyexpr_intersect_null_terminated("x/a$*d$*e", "x/a$*e")); - assert(zp_keyexpr_intersect_null_terminated("x/a$*d$*e", "x/a$*c$*e")); - assert(zp_keyexpr_intersect_null_terminated("x/a$*d$*e", "x/ade")); - assert(!zp_keyexpr_intersect_null_terminated("x/c$*", "x/abc$*")); - assert(!zp_keyexpr_intersect_null_terminated("x/$*d", "x/$*e")); - assert(zp_keyexpr_intersect_null_terminated("a", "a")); - assert(zp_keyexpr_intersect_null_terminated("a/b", "a/b")); - assert(zp_keyexpr_intersect_null_terminated("*", "a")); - assert(zp_keyexpr_intersect_null_terminated("a", "*")); - assert(zp_keyexpr_intersect_null_terminated("*", "aaaaa")); - assert(zp_keyexpr_intersect_null_terminated("**", "a")); - assert(zp_keyexpr_intersect_null_terminated("a", "**")); - assert(zp_keyexpr_intersect_null_terminated("**", "a")); - assert(zp_keyexpr_intersect_null_terminated("a/a/a/a", "**")); - assert(zp_keyexpr_intersect_null_terminated("a/*", "a/b")); - assert(!zp_keyexpr_intersect_null_terminated("a/*/b", "a/b")); - assert(zp_keyexpr_intersect_null_terminated("a/**/b", "a/b")); - assert(zp_keyexpr_intersect_null_terminated("a/b$*", "a/b")); - assert(zp_keyexpr_intersect_null_terminated("a/$*b$*", "a/b")); - assert(zp_keyexpr_intersect_null_terminated("a/$*b", "a/b")); - assert(zp_keyexpr_intersect_null_terminated("a/b$*", "a/bc")); - assert(zp_keyexpr_intersect_null_terminated("a/$*b$*", "a/ebc")); - assert(zp_keyexpr_intersect_null_terminated("a/$*b", "a/cb")); - assert(!zp_keyexpr_intersect_null_terminated("a/b$*", "a/ebc")); - assert(!zp_keyexpr_intersect_null_terminated("a/$*b", "a/cbc")); - assert(zp_keyexpr_intersect_null_terminated("a/**/b$*", "a/b")); - assert(zp_keyexpr_intersect_null_terminated("a/**/$*b$*", "a/b")); - assert(zp_keyexpr_intersect_null_terminated("a/**/$*b", "a/b")); - assert(zp_keyexpr_intersect_null_terminated("a/**/b$*", "a/bc")); - assert(zp_keyexpr_intersect_null_terminated("a/**/$*b$*", "a/ebc")); - assert(zp_keyexpr_intersect_null_terminated("a/**/$*b", "a/cb")); - assert(!zp_keyexpr_intersect_null_terminated("a/**/b$*", "a/ebc")); - assert(!zp_keyexpr_intersect_null_terminated("a/**/$*b", "a/cbc")); + assert(zp_keyexpr_intersect_null_terminated("a", "a") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/b", "a/b") == 0); + assert(zp_keyexpr_intersect_null_terminated("*", "abc") == 0); + assert(zp_keyexpr_intersect_null_terminated("*", "abc") == 0); + assert(zp_keyexpr_intersect_null_terminated("*", "abc") == 0); + assert(zp_keyexpr_intersect_null_terminated("*", "xxx") == 0); + assert(zp_keyexpr_intersect_null_terminated("ab$*", "abcd") == 0); + assert(zp_keyexpr_intersect_null_terminated("ab$*d", "abcd") == 0); + assert(zp_keyexpr_intersect_null_terminated("ab$*", "ab") == 0); + assert(zp_keyexpr_intersect_null_terminated("ab/*", "ab") == -1); + assert(zp_keyexpr_intersect_null_terminated("a/*/c/*/e", "a/b/c/d/e") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/**/d/**/l", "a/b/c/d/e/f/g/h/i/l") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/**/d/**/l", "a/d/foo/l") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/$*b/c/$*d/e", "a/xb/c/xd/e") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/*/c/*/e", "a/c/e") == -1); + assert(zp_keyexpr_intersect_null_terminated("a/*/c/*/e", "a/b/c/d/x/e") == -1); + assert(zp_keyexpr_intersect_null_terminated("ab$*cd", "abxxcxxd") == -1); + assert(zp_keyexpr_intersect_null_terminated("ab$*cd", "abxxcxxcd") == 0); + assert(zp_keyexpr_intersect_null_terminated("ab$*cd", "abxxcxxcdx") == -1); + assert(zp_keyexpr_intersect_null_terminated("**", "abc") == 0); + assert(zp_keyexpr_intersect_null_terminated("**", "a/b/c") == 0); + assert(zp_keyexpr_intersect_null_terminated("ab/**", "ab") == 0); + assert(zp_keyexpr_intersect_null_terminated("**/xyz", "a/b/xyz/d/e/f/xyz") == 0); + assert(zp_keyexpr_intersect_null_terminated("**/xyz$*xyz", "a/b/xyz/d/e/f/xyz") == -1); + assert(zp_keyexpr_intersect_null_terminated("a/**/c/**/e", "a/b/b/b/c/d/d/d/e") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/**/c/**/e", "a/c/e") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/**/c/*/e/*", "a/b/b/b/c/d/d/c/d/e/f") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/**/c/*/e/*", "a/b/b/b/c/d/d/c/d/d/e/f") == -1); + assert(zp_keyexpr_intersect_null_terminated("ab$*cd", "abxxcxxcdx") == -1); + assert(zp_keyexpr_intersect_null_terminated("x/abc", "x/abc") == 0); + assert(zp_keyexpr_intersect_null_terminated("x/abc", "abc") == -1); + assert(zp_keyexpr_intersect_null_terminated("x/*", "x/abc") == 0); + assert(zp_keyexpr_intersect_null_terminated("x/*", "abc") == -1); + assert(zp_keyexpr_intersect_null_terminated("*", "x/abc") == -1); + assert(zp_keyexpr_intersect_null_terminated("x/*", "x/abc$*") == 0); + assert(zp_keyexpr_intersect_null_terminated("x/$*abc", "x/abc$*") == 0); + assert(zp_keyexpr_intersect_null_terminated("x/a$*", "x/abc$*") == 0); + assert(zp_keyexpr_intersect_null_terminated("x/a$*de", "x/abc$*de") == 0); + assert(zp_keyexpr_intersect_null_terminated("x/a$*d$*e", "x/a$*e") == 0); + assert(zp_keyexpr_intersect_null_terminated("x/a$*d$*e", "x/a$*c$*e") == 0); + assert(zp_keyexpr_intersect_null_terminated("x/a$*d$*e", "x/ade") == 0); + assert(zp_keyexpr_intersect_null_terminated("x/c$*", "x/abc$*") == -1); + assert(zp_keyexpr_intersect_null_terminated("x/$*d", "x/$*e") == -1); + assert(zp_keyexpr_intersect_null_terminated("a", "a") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/b", "a/b") == 0); + assert(zp_keyexpr_intersect_null_terminated("*", "a") == 0); + assert(zp_keyexpr_intersect_null_terminated("a", "*") == 0); + assert(zp_keyexpr_intersect_null_terminated("*", "aaaaa") == 0); + assert(zp_keyexpr_intersect_null_terminated("**", "a") == 0); + assert(zp_keyexpr_intersect_null_terminated("a", "**") == 0); + assert(zp_keyexpr_intersect_null_terminated("**", "a") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/a/a/a", "**") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/*", "a/b") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/*/b", "a/b") == -1); + assert(zp_keyexpr_intersect_null_terminated("a/**/b", "a/b") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/b$*", "a/b") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/$*b$*", "a/b") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/$*b", "a/b") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/b$*", "a/bc") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/$*b$*", "a/ebc") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/$*b", "a/cb") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/b$*", "a/ebc") == -1); + assert(zp_keyexpr_intersect_null_terminated("a/$*b", "a/cbc") == -1); + assert(zp_keyexpr_intersect_null_terminated("a/**/b$*", "a/b") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/**/$*b$*", "a/b") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/**/$*b", "a/b") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/**/b$*", "a/bc") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/**/$*b$*", "a/ebc") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/**/$*b", "a/cb") == 0); + assert(zp_keyexpr_intersect_null_terminated("a/**/b$*", "a/ebc") == -1); + assert(zp_keyexpr_intersect_null_terminated("a/**/$*b", "a/cbc") == -1); assert(_z_keyexpr_includes("a", strlen("a"), "a", strlen("a"))); assert(_z_keyexpr_includes("a/b", strlen("a/b"), "a/b", strlen("a/b"))); @@ -206,39 +206,39 @@ int main(void) assert(!_z_keyexpr_includes("a/**/b$*", strlen("a/**/b$*"), "a/ebc", strlen("a/ebc"))); assert(!_z_keyexpr_includes("a/**/$*b", strlen("a/**/$*b"), "a/cbc", strlen("a/cbc"))); - assert(zp_keyexpr_includes_null_terminated("a", "a")); - assert(zp_keyexpr_includes_null_terminated("a/b", "a/b")); - assert(zp_keyexpr_includes_null_terminated("*", "a")); - assert(!zp_keyexpr_includes_null_terminated("a", "*")); - assert(zp_keyexpr_includes_null_terminated("*", "aaaaa")); - assert(zp_keyexpr_includes_null_terminated("**", "a")); - assert(!zp_keyexpr_includes_null_terminated("a", "**")); - assert(zp_keyexpr_includes_null_terminated("**", "a")); - assert(zp_keyexpr_includes_null_terminated("**", "a/a/a/a")); - assert(zp_keyexpr_includes_null_terminated("**", "*/**")); - assert(zp_keyexpr_includes_null_terminated("*/**", "*/**")); - assert(!zp_keyexpr_includes_null_terminated("*/**", "**")); - assert(!zp_keyexpr_includes_null_terminated("a/a/a/a", "**")); - assert(zp_keyexpr_includes_null_terminated("a/*", "a/b")); - assert(!zp_keyexpr_includes_null_terminated("a/*/b", "a/b")); - assert(zp_keyexpr_includes_null_terminated("a/**/b", "a/b")); - assert(zp_keyexpr_includes_null_terminated("a/b$*", "a/b")); - assert(!zp_keyexpr_includes_null_terminated("a/b", "a/b$*")); - assert(zp_keyexpr_includes_null_terminated("a/$*b$*", "a/b")); - assert(zp_keyexpr_includes_null_terminated("a/$*b", "a/b")); - assert(zp_keyexpr_includes_null_terminated("a/b$*", "a/bc")); - assert(zp_keyexpr_includes_null_terminated("a/$*b$*", "a/ebc")); - assert(zp_keyexpr_includes_null_terminated("a/$*b", "a/cb")); - assert(!zp_keyexpr_includes_null_terminated("a/b$*", "a/ebc")); - assert(!zp_keyexpr_includes_null_terminated("a/$*b", "a/cbc")); - assert(zp_keyexpr_includes_null_terminated("a/**/b$*", "a/b")); - assert(zp_keyexpr_includes_null_terminated("a/**/$*b$*", "a/b")); - assert(zp_keyexpr_includes_null_terminated("a/**/$*b", "a/b")); - assert(zp_keyexpr_includes_null_terminated("a/**/b$*", "a/bc")); - assert(zp_keyexpr_includes_null_terminated("a/**/$*b$*", "a/ebc")); - assert(zp_keyexpr_includes_null_terminated("a/**/$*b", "a/cb")); - assert(!zp_keyexpr_includes_null_terminated("a/**/b$*", "a/ebc")); - assert(!zp_keyexpr_includes_null_terminated("a/**/$*b", "a/cbc")); + assert(zp_keyexpr_includes_null_terminated("a", "a") == 0); + assert(zp_keyexpr_includes_null_terminated("a/b", "a/b") == 0); + assert(zp_keyexpr_includes_null_terminated("*", "a") == 0); + assert(zp_keyexpr_includes_null_terminated("a", "*") == -1); + assert(zp_keyexpr_includes_null_terminated("*", "aaaaa") == 0); + assert(zp_keyexpr_includes_null_terminated("**", "a") == 0); + assert(zp_keyexpr_includes_null_terminated("a", "**") == -1); + assert(zp_keyexpr_includes_null_terminated("**", "a") == 0); + assert(zp_keyexpr_includes_null_terminated("**", "a/a/a/a") == 0); + assert(zp_keyexpr_includes_null_terminated("**", "*/**") == 0); + assert(zp_keyexpr_includes_null_terminated("*/**", "*/**") == 0); + assert(zp_keyexpr_includes_null_terminated("*/**", "**") == -1); + assert(zp_keyexpr_includes_null_terminated("a/a/a/a", "**") == -1); + assert(zp_keyexpr_includes_null_terminated("a/*", "a/b") == 0); + assert(zp_keyexpr_includes_null_terminated("a/*/b", "a/b") == -1); + assert(zp_keyexpr_includes_null_terminated("a/**/b", "a/b") == 0); + assert(zp_keyexpr_includes_null_terminated("a/b$*", "a/b") == 0); + assert(zp_keyexpr_includes_null_terminated("a/b", "a/b$*") == -1); + assert(zp_keyexpr_includes_null_terminated("a/$*b$*", "a/b") == 0); + assert(zp_keyexpr_includes_null_terminated("a/$*b", "a/b") == 0); + assert(zp_keyexpr_includes_null_terminated("a/b$*", "a/bc") == 0); + assert(zp_keyexpr_includes_null_terminated("a/$*b$*", "a/ebc") == 0); + assert(zp_keyexpr_includes_null_terminated("a/$*b", "a/cb") == 0); + assert(zp_keyexpr_includes_null_terminated("a/b$*", "a/ebc") == -1); + assert(zp_keyexpr_includes_null_terminated("a/$*b", "a/cbc") == -1); + assert(zp_keyexpr_includes_null_terminated("a/**/b$*", "a/b") == 0); + assert(zp_keyexpr_includes_null_terminated("a/**/$*b$*", "a/b") == 0); + assert(zp_keyexpr_includes_null_terminated("a/**/$*b", "a/b") == 0); + assert(zp_keyexpr_includes_null_terminated("a/**/b$*", "a/bc") == 0); + assert(zp_keyexpr_includes_null_terminated("a/**/$*b$*", "a/ebc") == 0); + assert(zp_keyexpr_includes_null_terminated("a/**/$*b", "a/cb") == 0); + assert(zp_keyexpr_includes_null_terminated("a/**/b$*", "a/ebc") == -1); + assert(zp_keyexpr_includes_null_terminated("a/**/$*b", "a/cbc") == -1); // clang-format off @@ -376,12 +376,9 @@ int main(void) printf("\n"); } - assert(z_keyexpr_equals(z_keyexpr("a/**/$*b"), z_keyexpr("a/cb")) < 0); - assert(z_keyexpr_equals(z_keyexpr("a/bc"), z_keyexpr("a/cb")) < 0); - assert(z_keyexpr_equals(z_keyexpr("greetings/hello/there"), z_keyexpr("greetings/hello/there")) == 0); - assert(zp_keyexpr_equals_null_terminated("a/**/$*b", "a/cb") == false); - assert(zp_keyexpr_equals_null_terminated("a/bc", "a/cb") == false); - assert(zp_keyexpr_equals_null_terminated("greetings/hello/there", "greetings/hello/there") == true); + assert(zp_keyexpr_equals_null_terminated("a/**/$*b", "a/cb") == -1); + assert(zp_keyexpr_equals_null_terminated("a/bc", "a/cb") == -1); + assert(zp_keyexpr_equals_null_terminated("greetings/hello/there", "greetings/hello/there") == 0); return 0; } From 9e2ec08f84005f224ff05219c937276f425daed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 23 Mar 2023 11:34:34 +0100 Subject: [PATCH 046/181] Fix whatami values --- include/zenoh-pico/api/constants.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/include/zenoh-pico/api/constants.h b/include/zenoh-pico/api/constants.h index edd3621ce..9e9e095ca 100644 --- a/include/zenoh-pico/api/constants.h +++ b/include/zenoh-pico/api/constants.h @@ -43,11 +43,7 @@ typedef enum { * Z_WHATAMI_PEER: Bitmask to filter for Zenoh peers. * Z_WHATAMI_CLIENT: Bitmask to filter for Zenoh clients. */ -typedef enum { - Z_WHATAMI_ROUTER = 0x01, // 1 << 0 - Z_WHATAMI_PEER = 0x02, // 1 << 1 - Z_WHATAMI_CLIENT = 0x04 // 1 << 2 -} z_whatami_t; +typedef enum { Z_WHATAMI_ROUTER = 0x00, Z_WHATAMI_PEER = 0x01, Z_WHATAMI_CLIENT = 0x02 } z_whatami_t; #define Z_WHATAMI_DEFAULT Z_WHATAMI_ROUTER /** From 97a347a6f4cd4d97b6baea0535ab7e0cb3789de2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Tue, 21 Mar 2023 14:33:33 +0100 Subject: [PATCH 047/181] Fixes dynamic memory allocation wrt buffer allocation checks (#178) (#179) --- src/transport/transport.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/transport/transport.c b/src/transport/transport.c index 640815a0c..235364dc4 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -91,24 +91,32 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic // Initialize the read and write buffers if (ret == _Z_RES_OK) { uint16_t mtu = (zl->_mtu < Z_BATCH_SIZE) ? zl->_mtu : Z_BATCH_SIZE; - zt->_transport._unicast._wbuf = _z_wbuf_make(mtu, true); - zt->_transport._unicast._zbuf = _z_zbuf_make(Z_BATCH_SIZE); - - // Initialize the defragmentation buffers + _Bool expandable = true; size_t dbuf_size = 0; - _Bool dbuf_expandable = true; + #if Z_DYNAMIC_MEMORY_ALLOCATION == 0 + expandable = false; dbuf_size = Z_FRAG_MAX_SIZE; - dbuf_expandable = false; #endif - zt->_transport._unicast._dbuf_reliable = _z_wbuf_make(dbuf_size, dbuf_expandable); - zt->_transport._unicast._dbuf_best_effort = _z_wbuf_make(dbuf_size, dbuf_expandable); + + zt->_transport._unicast._wbuf = _z_wbuf_make(mtu, expandable); + zt->_transport._unicast._zbuf = _z_zbuf_make(Z_BATCH_SIZE); + + // Initialize the defragmentation buffers + zt->_transport._unicast._dbuf_reliable = _z_wbuf_make(dbuf_size, expandable); + zt->_transport._unicast._dbuf_best_effort = _z_wbuf_make(dbuf_size, expandable); // Clean up the buffers if one of them failed to be allocated - if ((_z_wbuf_capacity(&zt->_transport._unicast._wbuf) != mtu) || - (_z_zbuf_capacity(&zt->_transport._unicast._zbuf) != Z_BATCH_SIZE) || + if ((_z_zbuf_capacity(&zt->_transport._unicast._zbuf) != Z_BATCH_SIZE) || +#if Z_DYNAMIC_MEMORY_ALLOCATION == 0 + (_z_wbuf_capacity(&zt->_transport._unicast._wbuf) != mtu) || (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_reliable) != dbuf_size) || (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_best_effort) != dbuf_size)) { +#else + (_z_wbuf_capacity(&zt->_transport._unicast._wbuf) != Z_IOSLICE_SIZE) || + (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_reliable) != Z_IOSLICE_SIZE) || + (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_best_effort) != Z_IOSLICE_SIZE)) { +#endif ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; #if Z_MULTI_THREAD == 1 From af17959c321405a9b661ebbd4746c22842ed4557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 23 Mar 2023 16:50:59 +0100 Subject: [PATCH 048/181] Fix INIT/JOIN number resolutions --- include/zenoh-pico/protocol/msg.h | 2 +- src/protocol/msgcodec.c | 15 ++++++++------- tests/z_msgcodec_test.c | 14 +++++++------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 550172e32..48fa254b9 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -26,7 +26,7 @@ #include "zenoh-pico/protocol/ext.h" #define _Z_DEFAULT_BATCH_SIZE 65535 -#define _Z_DEFAULT_SIZET_SIZE 8 // In bytes +#define _Z_DEFAULT_SIZET_SIZE 3 // 0x80 << _Z_DEFAULT_SIZET_SIZE. 3 means size_t = 64 bits #define _Z_DECLARE_CLEAR(layer, name) void _z_##layer##_msg_clear_##name(_z_##name##_t *m, uint8_t header) diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 3c57cce80..88ef5ed8e 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1105,9 +1105,9 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg if (_Z_HAS_FLAG(header, _Z_FLAG_JOIN_S) == true) { cbyte = 0; - cbyte |= ((msg->_seq_num_res) & 0x03); - cbyte |= (((msg->_req_id_res) & 0x03) << 2); - cbyte |= (((msg->_key_id_res) & 0x03) << 4); + cbyte |= (msg->_seq_num_res & 0x03); + cbyte |= ((msg->_req_id_res & 0x03) << 2); + cbyte |= ((msg->_key_id_res & 0x03) << 4); _Z_EC(_z_uint8_encode(wbf, cbyte)) _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) } @@ -1151,7 +1151,7 @@ int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { msg->_zid = _z_bytes_empty(); } - if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_JOIN_S) == true) { cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); msg->_seq_num_res = (cbyte & 0x03); @@ -1201,10 +1201,11 @@ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { + printf("[Encode] I have a S flag\n"); cbyte = 0; - cbyte |= ((msg->_seq_num_res) & 0x03); - cbyte |= (((msg->_req_id_res) & 0x03) << 2); - cbyte |= (((msg->_key_id_res) & 0x03) << 4); + cbyte |= (msg->_seq_num_res & 0x03); + cbyte |= ((msg->_req_id_res & 0x03) << 2); + cbyte |= ((msg->_key_id_res & 0x03) << 4); _Z_EC(_z_uint8_encode(wbf, cbyte)) _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 1df5c2a7a..f7655b1d7 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -1958,15 +1958,15 @@ _z_transport_message_t gen_join_message(void) { } if (gen_bool()) { - t_msg._body._join._seq_num_res = (gen_uint8() % 4) + 1; + t_msg._body._join._seq_num_res = gen_uint8() % 4; } if (gen_bool()) { - t_msg._body._join._key_id_res = (gen_uint8() % 4) + 1; + t_msg._body._join._key_id_res = gen_uint8() % 4; } if (gen_bool()) { - t_msg._body._join._req_id_res = (gen_uint8() % 4) + 1; + t_msg._body._join._req_id_res = gen_uint8() % 4; } return t_msg; @@ -1985,7 +1985,7 @@ void assert_eq_join_message(_z_t_msg_join_t *left, _z_t_msg_join_t *right, uint8 assert_eq_uint8_array(&left->_zid, &right->_zid); printf("\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_JOIN_S) == true) { printf(" SN Resolution (%hhu:%hhu)", left->_seq_num_res, right->_seq_num_res); assert(left->_seq_num_res == right->_seq_num_res); printf("\n"); @@ -2080,15 +2080,15 @@ _z_transport_message_t gen_init_message(void) { } if (gen_bool()) { - t_msg._body._init._seq_num_res = (gen_uint8() % 4) + 1; + t_msg._body._init._seq_num_res = gen_uint8() % 4; } if (gen_bool()) { - t_msg._body._init._key_id_res = (gen_uint8() % 4) + 1; + t_msg._body._init._key_id_res = gen_uint8() % 4; } if (gen_bool()) { - t_msg._body._init._req_id_res = (gen_uint8() % 4) + 1; + t_msg._body._init._req_id_res = gen_uint8() % 4; } return t_msg; From 71aabd03891a0c089f0a70520a39c4004f4c7bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 23 Mar 2023 16:52:36 +0100 Subject: [PATCH 049/181] Split scouting from transport messages --- include/zenoh-pico/protocol/msg.h | 34 +++-- include/zenoh-pico/protocol/msgcodec.h | 16 ++- src/protocol/msg.c | 78 +++++++---- src/protocol/msgcodec.c | 127 +++++++++++++---- src/session/scout.c | 22 +-- src/transport/multicast/link/rx.c | 10 -- src/transport/transport.c | 1 - src/transport/unicast/link/rx.c | 10 -- tests/z_msgcodec_test.c | 184 +++++++++++++++++++------ 9 files changed, 342 insertions(+), 140 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 48fa254b9..f36dd2b4a 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -653,8 +653,8 @@ typedef struct { _z_bytes_t _zid; z_what_t _what; uint8_t _version; -} _z_t_msg_scout_t; -void _z_t_msg_clear_scout(_z_t_msg_scout_t *msg); +} _z_s_msg_scout_t; +void _z_s_msg_clear_scout(_z_s_msg_scout_t *msg); /*------------------ Hello Message ------------------*/ // NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length @@ -700,8 +700,8 @@ typedef struct { _z_locator_array_t _locators; z_whatami_t _whatami; uint8_t _version; -} _z_t_msg_hello_t; -void _z_t_msg_clear_hello(_z_t_msg_hello_t *msg); +} _z_s_msg_hello_t; +void _z_s_msg_clear_hello(_z_s_msg_hello_t *msg); /*------------------ Join Message ------------------*/ // # Join message @@ -1001,8 +1001,6 @@ void _z_t_msg_clear_fragment(_z_t_msg_fragment_t *msg); /*------------------ Transport Message ------------------*/ typedef union { - _z_t_msg_scout_t _scout; - _z_t_msg_hello_t _hello; _z_t_msg_join_t _join; _z_t_msg_init_t _init; _z_t_msg_open_t _open; @@ -1021,8 +1019,6 @@ typedef struct { void _z_t_msg_clear(_z_transport_message_t *msg); /*------------------ Builders ------------------*/ -_z_transport_message_t _z_t_msg_make_scout(z_what_t what, _z_bytes_t zid); -_z_transport_message_t _z_t_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _z_locator_array_t locators); _z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_bytes_t zid, _z_conduit_sn_list_t next_sn); _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_bytes_t zid); @@ -1037,8 +1033,6 @@ _z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_payload_t message /*------------------ Copy ------------------*/ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg); -void _z_t_msg_copy_scout(_z_t_msg_scout_t *clone, _z_t_msg_scout_t *msg); -void _z_t_msg_copy_hello(_z_t_msg_hello_t *clone, _z_t_msg_hello_t *msg); void _z_t_msg_copy_join(_z_t_msg_join_t *clone, _z_t_msg_join_t *msg); void _z_t_msg_copy_init(_z_t_msg_init_t *clone, _z_t_msg_init_t *msg); void _z_t_msg_copy_open(_z_t_msg_open_t *clone, _z_t_msg_open_t *msg); @@ -1046,4 +1040,24 @@ void _z_t_msg_copy_close(_z_t_msg_close_t *clone, _z_t_msg_close_t *msg); void _z_t_msg_copy_keep_alive(_z_t_msg_keep_alive_t *clone, _z_t_msg_keep_alive_t *msg); void _z_t_msg_copy_frame(_z_t_msg_frame_t *clone, _z_t_msg_frame_t *msg); +typedef union { + _z_s_msg_scout_t _scout; + _z_s_msg_hello_t _hello; +} _z_scouting_body_t; + +typedef struct { + _z_scouting_body_t _body; + _z_attachment_t *_attachment; + _z_msg_ext_vec_t _extensions; + uint8_t _header; +} _z_scouting_message_t; +void _z_s_msg_clear(_z_scouting_message_t *msg); + +_z_scouting_message_t _z_s_msg_make_scout(z_what_t what, _z_bytes_t zid); +_z_scouting_message_t _z_s_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _z_locator_array_t locators); + +void _z_s_msg_copy(_z_scouting_message_t *clone, _z_scouting_message_t *msg); +void _z_s_msg_copy_scout(_z_s_msg_scout_t *clone, _z_s_msg_scout_t *msg); +void _z_s_msg_copy_hello(_z_s_msg_hello_t *clone, _z_s_msg_hello_t *msg); + #endif /* ZENOH_PICO_PROTOCOL_MSG_H */ diff --git a/include/zenoh-pico/protocol/msgcodec.h b/include/zenoh-pico/protocol/msgcodec.h index 795b6506b..dc5aaf381 100644 --- a/include/zenoh-pico/protocol/msgcodec.h +++ b/include/zenoh-pico/protocol/msgcodec.h @@ -44,6 +44,10 @@ void _z_##name##_decode_na(_z_zbuf_t *zbf, _z_##name##_result_t *r); /*------------------ Zenoh Message ------------------*/ +int8_t _z_scouting_message_encode(_z_wbuf_t *buf, const _z_scouting_message_t *msg); +int8_t _z_scouting_message_decode(_z_scouting_message_t *msg, _z_zbuf_t *buf); +int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *buf); + int8_t _z_transport_message_encode(_z_wbuf_t *buf, const _z_transport_message_t *msg); int8_t _z_transport_message_decode(_z_transport_message_t *msg, _z_zbuf_t *buf); int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *buf); @@ -164,12 +168,12 @@ int8_t _z_fragment_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t h int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); // ------------------ Discovery Message ------------------ -int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_scout_t *msg); -int8_t _z_scout_decode_na(_z_t_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_scout_decode(_z_t_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_scout_t *msg); +int8_t _z_scout_decode_na(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_scout_decode(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_hello_t *msg); -int8_t _z_hello_decode_na(_z_t_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_hello_decode(_z_t_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *msg); +int8_t _z_hello_decode_na(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_hello_decode(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header); #endif /* ZENOH_PICO_TEST_H */ diff --git a/src/protocol/msg.c b/src/protocol/msg.c index d9a76175e..395970ff9 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -461,8 +461,8 @@ void _z_msg_free(_z_zenoh_message_t **msg) { /* Transport Messages */ /*=============================*/ /*------------------ Scout Message ------------------*/ -_z_transport_message_t _z_t_msg_make_scout(z_what_t what, _z_bytes_t zid) { - _z_transport_message_t msg; +_z_scouting_message_t _z_s_msg_make_scout(z_what_t what, _z_bytes_t zid) { + _z_scouting_message_t msg; msg._header = _Z_MID_SCOUT; msg._body._scout._version = Z_PROTO_VERSION; @@ -475,17 +475,17 @@ _z_transport_message_t _z_t_msg_make_scout(z_what_t what, _z_bytes_t zid) { return msg; } -void _z_t_msg_copy_scout(_z_t_msg_scout_t *clone, _z_t_msg_scout_t *msg) { +void _z_s_msg_copy_scout(_z_s_msg_scout_t *clone, _z_s_msg_scout_t *msg) { clone->_what = msg->_what; clone->_version = msg->_version; _z_bytes_copy(&clone->_zid, &msg->_zid); } -void _z_t_msg_clear_scout(_z_t_msg_scout_t *msg) { _z_bytes_clear(&msg->_zid); } +void _z_s_msg_clear_scout(_z_s_msg_scout_t *msg) { _z_bytes_clear(&msg->_zid); } /*------------------ Hello Message ------------------*/ -_z_transport_message_t _z_t_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _z_locator_array_t locators) { - _z_transport_message_t msg; +_z_scouting_message_t _z_s_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _z_locator_array_t locators) { + _z_scouting_message_t msg; msg._header = _Z_MID_HELLO; msg._body._hello._version = Z_PROTO_VERSION; @@ -503,13 +503,13 @@ _z_transport_message_t _z_t_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, return msg; } -void _z_t_msg_copy_hello(_z_t_msg_hello_t *clone, _z_t_msg_hello_t *msg) { +void _z_s_msg_copy_hello(_z_s_msg_hello_t *clone, _z_s_msg_hello_t *msg) { _z_locator_array_copy(&clone->_locators, &msg->_locators); _z_bytes_copy(&clone->_zid, &msg->_zid); clone->_whatami = msg->_whatami; } -void _z_t_msg_clear_hello(_z_t_msg_hello_t *msg) { +void _z_s_msg_clear_hello(_z_s_msg_hello_t *msg) { _z_bytes_clear(&msg->_zid); _z_locators_clear(&msg->_locators); } @@ -791,14 +791,6 @@ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg) { uint8_t mid = _Z_MID(msg->_header); switch (mid) { - case _Z_MID_SCOUT: { - _z_t_msg_copy_scout(&clone->_body._scout, &msg->_body._scout); - } break; - - case _Z_MID_HELLO: { - _z_t_msg_copy_hello(&clone->_body._hello, &msg->_body._hello); - } break; - case _Z_MID_JOIN: { _z_t_msg_copy_join(&clone->_body._join, &msg->_body._join); } break; @@ -841,14 +833,6 @@ void _z_t_msg_clear(_z_transport_message_t *msg) { uint8_t mid = _Z_MID(msg->_header); switch (mid) { - case _Z_MID_SCOUT: { - _z_t_msg_clear_scout(&msg->_body._scout); - } break; - - case _Z_MID_HELLO: { - _z_t_msg_clear_hello(&msg->_body._hello); - } break; - case _Z_MID_JOIN: { _z_t_msg_clear_join(&msg->_body._join); } break; @@ -884,3 +868,49 @@ void _z_t_msg_clear(_z_transport_message_t *msg) { _z_msg_ext_vec_clear(&msg->_extensions); } + +/*------------------ Scouting Message ------------------*/ +void _z_s_msg_copy(_z_scouting_message_t *clone, _z_scouting_message_t *msg) { + clone->_header = msg->_header; + clone->_attachment = msg->_attachment; + _z_msg_ext_vec_copy(&clone->_extensions, &msg->_extensions); + + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_SCOUT: { + _z_s_msg_copy_scout(&clone->_body._scout, &msg->_body._scout); + } break; + + case _Z_MID_HELLO: { + _z_s_msg_copy_hello(&clone->_body._hello, &msg->_body._hello); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to free session message with unknown ID(%d)\n", mid); + } break; + } +} + +void _z_s_msg_clear(_z_scouting_message_t *msg) { + if (msg->_attachment != NULL) { + _z_t_msg_clear_attachment(msg->_attachment); + z_free(msg->_attachment); + } + + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_SCOUT: { + _z_s_msg_clear_scout(&msg->_body._scout); + } break; + + case _Z_MID_HELLO: { + _z_s_msg_clear_hello(&msg->_body._hello); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to free session message with unknown ID(%d)\n", mid); + } break; + } + + _z_msg_ext_vec_clear(&msg->_extensions); +} diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 88ef5ed8e..6049d2bbc 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -986,7 +986,7 @@ int8_t _z_zenoh_message_decode(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { return /* Transport Messages */ /*=============================*/ /*------------------ Scout Message ------------------*/ -int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_scout_t *msg) { +int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_scout_t *msg) { int8_t ret = _Z_RES_OK; (void)(header); _Z_DEBUG("Encoding _Z_MID_SCOUT\n"); @@ -1007,7 +1007,7 @@ int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_scout_t *m return ret; } -int8_t _z_scout_decode_na(_z_t_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_scout_decode_na(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; (void)(header); _Z_DEBUG("Decoding _Z_MID_SCOUT\n"); @@ -1030,12 +1030,12 @@ int8_t _z_scout_decode_na(_z_t_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) return ret; } -int8_t _z_scout_decode(_z_t_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_scout_decode(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) { return _z_scout_decode_na(msg, zbf, header); } /*------------------ Hello Message ------------------*/ -int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_hello_t *msg) { +int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *msg) { int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_HELLO\n"); @@ -1054,7 +1054,7 @@ int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_hello_t *m return ret; } -int8_t _z_hello_decode_na(_z_t_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_hello_decode_na(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_HELLO\n"); int8_t ret = _Z_RES_OK; @@ -1086,7 +1086,7 @@ int8_t _z_hello_decode_na(_z_t_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) return ret; } -int8_t _z_hello_decode(_z_t_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_hello_decode(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) { return _z_hello_decode_na(msg, zbf, header); } @@ -1495,14 +1495,6 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t ret |= _z_join_encode(wbf, msg->_header, &msg->_body._join); } break; - case _Z_MID_SCOUT: { - ret |= _z_scout_encode(wbf, msg->_header, &msg->_body._scout); - } break; - - case _Z_MID_HELLO: { - ret |= _z_hello_encode(wbf, msg->_header, &msg->_body._hello); - } break; - case _Z_MID_INIT: { ret |= _z_init_encode(wbf, msg->_header, &msg->_body._init); } break; @@ -1569,16 +1561,6 @@ int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *zb is_last = true; } break; - case _Z_MID_SCOUT: { - ret |= _z_scout_decode(&msg->_body._scout, zbf, msg->_header); - is_last = true; - } break; - - case _Z_MID_HELLO: { - ret |= _z_hello_decode(&msg->_body._hello, zbf, msg->_header); - is_last = true; - } break; - case _Z_MID_INIT: { ret |= _z_init_decode(&msg->_body._init, zbf, msg->_header); is_last = true; @@ -1630,3 +1612,100 @@ int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *zb int8_t _z_transport_message_decode(_z_transport_message_t *t_msg, _z_zbuf_t *zbf) { return _z_transport_message_decode_na(t_msg, zbf); } + +/*------------------ Scouting Message ------------------*/ +int8_t _z_scouting_message_encode(_z_wbuf_t *wbf, const _z_scouting_message_t *msg) { + int8_t ret = _Z_RES_OK; + + // Encode the decorators if present + if (msg->_attachment != NULL) { + _Z_EC(_z_attachment_encode(wbf, msg->_attachment)) + } + + uint8_t header = msg->_header; + size_t n_ext = _z_msg_ext_vec_len(&msg->_extensions); + if (n_ext > 0) { + header |= _Z_FLAG_T_Z; + } + + _Z_EC(_z_wbuf_write(wbf, header)) + switch (_Z_MID(msg->_header)) { + case _Z_MID_SCOUT: { + ret |= _z_scout_encode(wbf, msg->_header, &msg->_body._scout); + } break; + + case _Z_MID_HELLO: { + ret |= _z_hello_encode(wbf, msg->_header, &msg->_body._hello); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to encode session message with unknown ID(%d)\n", _Z_MID(msg->_header)); + ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; + } break; + } + + if (n_ext > 0) { + for (size_t i = 0; (i + 1) < n_ext; i++) { + _z_msg_ext_t *ext = _z_msg_ext_vec_get(&msg->_extensions, i); + _Z_EXT_SET_FLAG(ext->_header, _Z_MSG_EXT_FLAG_Z); + ret |= _z_msg_ext_encode(wbf, ext); + } + ret |= + _z_msg_ext_encode(wbf, _z_msg_ext_vec_get(&msg->_extensions, n_ext - 1)); // Last extension wo\ next flag + } + + return ret; +} + +int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + + msg->_attachment = NULL; + _Bool is_last = false; + + do { + ret |= _z_uint8_decode(&msg->_header, zbf); // Decode the header + if (ret == _Z_RES_OK) { + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_SCOUT: { + ret |= _z_scout_decode(&msg->_body._scout, zbf, msg->_header); + is_last = true; + } break; + + case _Z_MID_HELLO: { + ret |= _z_hello_decode(&msg->_body._hello, zbf, msg->_header); + is_last = true; + } break; + + default: { + _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(%d)\n", mid); + ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; + is_last = true; + } break; + } + } else { + msg->_header = 0xFF; + } + } while ((ret == _Z_RES_OK) && (is_last == false)); + + if (ret == _Z_RES_OK) { + if ((msg->_header & 0x80) == 0x80) { + msg->_extensions = _z_msg_ext_vec_make(10); + _z_msg_ext_t *ext = NULL; + do { + ext = (_z_msg_ext_t *)z_malloc(sizeof(_z_msg_ext_t)); + ret |= _z_msg_ext_decode(ext, zbf); + _z_msg_ext_vec_append(&msg->_extensions, ext); + } while ((ext->_header & 0x80) == 0x80); + } else { + msg->_extensions = _z_msg_ext_vec_make(0); + } + } + + return ret; +} + +int8_t _z_scouting_message_decode(_z_scouting_message_t *s_msg, _z_zbuf_t *zbf) { + return _z_scouting_message_decode_na(s_msg, zbf); +} \ No newline at end of file diff --git a/src/session/scout.c b/src/session/scout.c index db98843af..ac6a086c3 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -61,28 +61,28 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig continue; } - _z_transport_message_t t_msg; - err = _z_transport_message_decode(&t_msg, &zbf); + _z_scouting_message_t s_msg; + err = _z_scouting_message_decode(&s_msg, &zbf); if (err != _Z_RES_OK) { _Z_ERROR("Scouting loop received malformed message\n"); continue; } - switch (_Z_MID(t_msg._header)) { + switch (_Z_MID(s_msg._header)) { case _Z_MID_HELLO: { _Z_INFO("Received _Z_HELLO message\n"); _z_hello_t *hello = (_z_hello_t *)z_malloc(sizeof(_z_hello_t)); if (hello != NULL) { - hello->version = t_msg._body._hello._version; - hello->whatami = t_msg._body._hello._whatami; - _z_bytes_copy(&hello->zid, &t_msg._body._hello._zid); + hello->version = s_msg._body._hello._version; + hello->whatami = s_msg._body._hello._whatami; + _z_bytes_copy(&hello->zid, &s_msg._body._hello._zid); - size_t n_loc = _z_locator_array_len(&t_msg._body._hello._locators); + size_t n_loc = _z_locator_array_len(&s_msg._body._hello._locators); if (n_loc > 0) { hello->locators = _z_str_array_make(n_loc); for (size_t i = 0; i < n_loc; i++) { hello->locators._val[i] = - _z_locator_to_str(&t_msg._body._hello._locators._val[i]); + _z_locator_to_str(&s_msg._body._hello._locators._val[i]); } } else { // @TODO: construct the locator departing from the sock address @@ -101,7 +101,7 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig break; } } - _z_t_msg_clear(&t_msg); + _z_s_msg_clear(&s_msg); if ((_z_hello_list_len(ret) > 0) && (exit_on_first == true)) { break; @@ -131,9 +131,9 @@ _z_hello_list_t *_z_scout_inner(const z_what_t what, const _z_bytes_t zid, const _z_wbuf_t wbf = _z_wbuf_make(Z_BATCH_SIZE, false); // Create and encode the scout message - _z_transport_message_t scout = _z_t_msg_make_scout(what, zid); + _z_scouting_message_t scout = _z_s_msg_make_scout(what, zid); - _z_transport_message_encode(&wbf, &scout); + _z_scouting_message_encode(&wbf, &scout); // Scout on multicast #if Z_MULTICAST_TRANSPORT == 1 diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 732e1a242..afd6e5a0c 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -205,16 +205,6 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t break; } - case _Z_MID_SCOUT: { - // Do nothing, multicast transports are not expected to handle SCOUT messages on established sessions - break; - } - - case _Z_MID_HELLO: { - // Do nothing, multicast transports are not expected to handle HELLO messages on established sessions - break; - } - case _Z_MID_INIT: { // Do nothing, multicas transports are not expected to handle INIT messages break; diff --git a/src/transport/transport.c b/src/transport/transport.c index 235364dc4..34e0ea25f 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -98,7 +98,6 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic expandable = false; dbuf_size = Z_FRAG_MAX_SIZE; #endif - zt->_transport._unicast._wbuf = _z_wbuf_make(mtu, expandable); zt->_transport._unicast._zbuf = _z_zbuf_make(Z_BATCH_SIZE); diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index f069accf0..c97711e55 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -165,16 +165,6 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans break; } - case _Z_MID_SCOUT: { - _Z_INFO("Handling of Scout messages not implemented\n"); - break; - } - - case _Z_MID_HELLO: { - // Do nothing, zenoh-pico clients are not expected to handle hello messages - break; - } - case _Z_MID_INIT: { // Do nothing, zenoh clients are not expected to handle accept messages on established sessions break; diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index f7655b1d7..d0e3d3aa6 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -70,12 +70,6 @@ void print_uint8_array(_z_bytes_t *arr) { void print_transport_message_type(uint8_t header) { switch (_Z_MID(header)) { - case _Z_MID_SCOUT: - printf("Scout message"); - break; - case _Z_MID_HELLO: - printf("Hello message"); - break; case _Z_MID_JOIN: printf("Join message"); break; @@ -103,6 +97,20 @@ void print_transport_message_type(uint8_t header) { } } +void print_scouting_message_type(uint8_t header) { + switch (_Z_MID(header)) { + case _Z_MID_SCOUT: + printf("Scout message"); + break; + case _Z_MID_HELLO: + printf("Hello message"); + break; + default: + assert(0); + break; + } +} + /*=============================*/ /* Generating functions */ /*=============================*/ @@ -1820,7 +1828,7 @@ void zenoh_message(void) { /* Transport Messages */ /*=============================*/ /*------------------ Scout Message ------------------*/ -_z_transport_message_t gen_scout_message(void) { +_z_scouting_message_t gen_scout_message(void) { z_what_t what = gen_uint8() % 7; _z_bytes_t zid; if (gen_bool()) { @@ -1828,10 +1836,10 @@ _z_transport_message_t gen_scout_message(void) { } else { zid = _z_bytes_empty(); } - return _z_t_msg_make_scout(what, zid); + return _z_s_msg_make_scout(what, zid); } -void assert_eq_scout_message(_z_t_msg_scout_t *left, _z_t_msg_scout_t *right, uint8_t header) { +void assert_eq_scout_message(_z_s_msg_scout_t *left, _z_s_msg_scout_t *right, uint8_t header) { (void)(header); printf(" Version (%u:%u)\n", left->_version, right->_version); assert(left->_version == right->_version); @@ -1847,31 +1855,31 @@ void scout_message(void) { _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - _z_transport_message_t t_msg = gen_scout_message(); - _z_t_msg_scout_t e_sc = t_msg._body._scout; + _z_scouting_message_t s_msg = gen_scout_message(); + _z_s_msg_scout_t e_sc = s_msg._body._scout; // Encode - int8_t res = _z_scout_encode(&wbf, t_msg._header, &e_sc); + int8_t res = _z_scout_encode(&wbf, s_msg._header, &e_sc); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_t_msg_scout_t d_sc; - res = _z_scout_decode(&d_sc, &zbf, t_msg._header); + _z_s_msg_scout_t d_sc; + res = _z_scout_decode(&d_sc, &zbf, s_msg._header); assert(res == _Z_RES_OK); - assert_eq_scout_message(&e_sc, &d_sc, t_msg._header); + assert_eq_scout_message(&e_sc, &d_sc, s_msg._header); // Free - _z_t_msg_clear_scout(&d_sc); - _z_t_msg_clear(&t_msg); + _z_s_msg_clear_scout(&d_sc); + _z_s_msg_clear(&s_msg); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } /*------------------ Hello Message ------------------*/ -_z_transport_message_t gen_hello_message(void) { +_z_scouting_message_t gen_hello_message(void) { z_whatami_t whatami = (gen_uint8() % 2) + 1; _z_bytes_t zid = gen_bytes((gen_uint8() % 16) + 1); @@ -1881,10 +1889,10 @@ _z_transport_message_t gen_hello_message(void) { else locators = gen_locator_array(0); - return _z_t_msg_make_hello(whatami, zid, locators); + return _z_s_msg_make_hello(whatami, zid, locators); } -void assert_eq_hello_message(_z_t_msg_hello_t *left, _z_t_msg_hello_t *right, uint8_t header) { +void assert_eq_hello_message(_z_s_msg_hello_t *left, _z_s_msg_hello_t *right, uint8_t header) { printf(" Version (%u:%u)", left->_version, right->_version); assert(left->_version == right->_version); printf("\n"); @@ -1909,27 +1917,27 @@ void hello_message(void) { _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - _z_transport_message_t t_msg = gen_hello_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_HELLO); + _z_scouting_message_t s_msg = gen_hello_message(); + assert(_Z_MID(s_msg._header) == _Z_MID_HELLO); - _z_t_msg_hello_t e_he = t_msg._body._hello; + _z_s_msg_hello_t e_he = s_msg._body._hello; // Encode - int8_t res = _z_hello_encode(&wbf, t_msg._header, &e_he); + int8_t res = _z_hello_encode(&wbf, s_msg._header, &e_he); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_t_msg_hello_t d_he; - res = _z_hello_decode(&d_he, &zbf, t_msg._header); + _z_s_msg_hello_t d_he; + res = _z_hello_decode(&d_he, &zbf, s_msg._header); assert(res == _Z_RES_OK); - assert_eq_hello_message(&e_he, &d_he, t_msg._header); + assert_eq_hello_message(&e_he, &d_he, s_msg._header); // Free - _z_t_msg_clear_hello(&d_he); - _z_t_msg_clear(&t_msg); + _z_s_msg_clear_hello(&d_he); + _z_s_msg_clear(&s_msg); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } @@ -2423,17 +2431,11 @@ void fragment_message(void) { _z_transport_message_t gen_transport_message(void) { _z_transport_message_t e_tm; - uint8_t mids[] = {_Z_MID_SCOUT, _Z_MID_HELLO, _Z_MID_JOIN, _Z_MID_INIT, _Z_MID_OPEN, - _Z_MID_CLOSE, _Z_MID_KEEP_ALIVE, _Z_MID_FRAME, _Z_MID_FRAGMENT}; + uint8_t mids[] = {_Z_MID_JOIN, _Z_MID_INIT, _Z_MID_OPEN, _Z_MID_CLOSE, + _Z_MID_KEEP_ALIVE, _Z_MID_FRAME, _Z_MID_FRAGMENT}; uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); switch (mids[i]) { - case _Z_MID_SCOUT: - e_tm = gen_scout_message(); - break; - case _Z_MID_HELLO: - e_tm = gen_hello_message(); - break; case _Z_MID_JOIN: e_tm = gen_join_message(); break; @@ -2490,12 +2492,6 @@ void assert_eq_transport_message(_z_transport_message_t *left, _z_transport_mess printf("\n"); switch (_Z_MID(left->_header)) { - case _Z_MID_SCOUT: - assert_eq_scout_message(&left->_body._scout, &right->_body._scout, left->_header); - break; - case _Z_MID_HELLO: - assert_eq_hello_message(&left->_body._hello, &right->_body._hello, left->_header); - break; case _Z_MID_JOIN: assert_eq_join_message(&left->_body._join, &right->_body._join, left->_header); break; @@ -2533,7 +2529,7 @@ void assert_eq_transport_message(_z_transport_message_t *left, _z_transport_mess } void transport_message(void) { - printf("\n>> Session message\n"); + printf("\n>> Transport message\n"); _z_wbuf_t wbf = gen_wbuf(65535); // Initialize @@ -2562,6 +2558,106 @@ void transport_message(void) { _z_wbuf_clear(&wbf); } +/*------------------ Scouting Message ------------------*/ +_z_scouting_message_t gen_scouting_message(void) { + _z_scouting_message_t e_sm; + + uint8_t mids[] = {_Z_MID_SCOUT, _Z_MID_HELLO}; + + uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); + switch (mids[i]) { + case _Z_MID_SCOUT: + e_sm = gen_scout_message(); + break; + case _Z_MID_HELLO: + e_sm = gen_hello_message(); + break; + default: + assert(0); + break; + } + + if (gen_bool()) { + e_sm._attachment = gen_attachment(); + } else { + e_sm._attachment = NULL; + } + + return e_sm; +} + +void assert_eq_scouting_message(_z_scouting_message_t *left, _z_scouting_message_t *right) { + // FIXME[protocol]: This is here to set the extensions flags that is only known at encoding time + if (_z_msg_ext_vec_len(&left->_extensions) > (size_t)0) { + left->_header |= _Z_FLAG_T_Z; + } + + // Test message decorators + if (left->_attachment && right->_attachment) { + printf(" "); + assert_eq_attachment(left->_attachment, right->_attachment); + printf("\n"); + } else { + assert(left->_attachment == right->_attachment); + } + + // Test message + printf(" Header (%x:%x)", left->_header, right->_header); + assert(left->_header == right->_header); + printf("\n"); + + switch (_Z_MID(left->_header)) { + case _Z_MID_SCOUT: + assert_eq_scout_message(&left->_body._scout, &right->_body._scout, left->_header); + break; + case _Z_MID_HELLO: + assert_eq_hello_message(&left->_body._hello, &right->_body._hello, left->_header); + break; + default: + assert(0); + break; + } + + size_t left_n_ext = _z_msg_ext_vec_len(&left->_extensions); + size_t right_n_ext = _z_msg_ext_vec_len(&right->_extensions); + printf(" # of extensions (%zu:%zu)", left_n_ext, right_n_ext); + assert(left_n_ext == right_n_ext); + for (size_t i = 0; i < left_n_ext; i++) { + assert_eq_message_extension(_z_msg_ext_vec_get(&left->_extensions, i), + _z_msg_ext_vec_get(&right->_extensions, i)); + } +} + +void scouting_message(void) { + printf("\n>> Scouting message\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_scouting_message_t e_tm = gen_scouting_message(); + printf(" - "); + print_scouting_message_type(e_tm._header); + printf("\n"); + + // Encode + int8_t res = _z_scouting_message_encode(&wbf, &e_tm); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_scouting_message_t d_sm; + res = _z_scouting_message_decode(&d_sm, &zbf); + assert(res == _Z_RES_OK); + + assert_eq_scouting_message(&e_tm, &d_sm); + + // Free + _z_s_msg_clear(&e_tm); + _z_s_msg_clear(&d_sm); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + /*------------------ Batch ------------------*/ void batch(void) { printf("\n>> Batch\n"); From 7e04a5da911a964e3c0da249223b870637d82058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 23 Mar 2023 16:53:06 +0100 Subject: [PATCH 050/181] Update protocol version and msg ids --- include/zenoh-pico/config.h | 2 +- include/zenoh-pico/protocol/msg.h | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index de63a64c5..4305fc0a3 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -120,7 +120,7 @@ * Do not change this value. */ #ifndef Z_PROTO_VERSION -#define Z_PROTO_VERSION 0x07 +#define Z_PROTO_VERSION 0x08 #endif /** diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index f36dd2b4a..fbe30bade 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -42,16 +42,20 @@ /*=============================*/ /* Message IDs */ /*=============================*/ -/* Transport Messages */ -#define _Z_MID_JOIN 0x00 +/* Scouting Messages */ #define _Z_MID_SCOUT 0x01 #define _Z_MID_HELLO 0x02 -#define _Z_MID_INIT 0x03 -#define _Z_MID_OPEN 0x04 -#define _Z_MID_CLOSE 0x05 -#define _Z_MID_KEEP_ALIVE 0x08 -#define _Z_MID_FRAME 0x0a -#define _Z_MID_FRAGMENT 0x09 + +/* Transport Messages */ +#define _Z_MID_OAM 0x00 +#define _Z_MID_INIT 0x01 +#define _Z_MID_OPEN 0x02 +#define _Z_MID_CLOSE 0x03 +#define _Z_MID_KEEP_ALIVE 0x04 +#define _Z_MID_FRAME 0x05 +#define _Z_MID_FRAGMENT 0x06 +#define _Z_MID_JOIN 0x07 + /* Zenoh Messages */ #define _Z_MID_DECLARE 0x0b #define _Z_MID_DATA 0x0c @@ -59,6 +63,7 @@ #define _Z_MID_PULL 0x0e #define _Z_MID_UNIT 0x0f #define _Z_MID_LINK_STATE_LIST 0x10 + /* Message decorators */ #define _Z_MID_PRIORITY 0x1c #define _Z_MID_ROUTING_CONTEXT 0x1d From f74d86b50d5f1de2cddc541d082a65f9e3108530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 24 Mar 2023 12:01:47 +0100 Subject: [PATCH 051/181] Optimization on SN helpers --- include/zenoh-pico/config.h | 4 +- include/zenoh-pico/protocol/core.h | 3 - include/zenoh-pico/protocol/msg.h | 12 ++-- include/zenoh-pico/transport/transport.h | 17 ++--- include/zenoh-pico/transport/utils.h | 12 ++-- include/zenoh-pico/utils/math.h | 27 -------- src/protocol/core.c | 43 ------------ src/protocol/msgcodec.c | 1 - src/transport/multicast/link/rx.c | 14 ++-- src/transport/multicast/link/tx.c | 4 +- src/transport/peer_entry.c | 3 +- src/transport/transport.c | 28 ++++---- src/transport/unicast/link/rx.c | 4 +- src/transport/unicast/link/tx.c | 4 +- src/transport/utils.c | 84 +++++++++++++++++++----- src/utils/math.c | 24 ------- 16 files changed, 117 insertions(+), 167 deletions(-) delete mode 100644 include/zenoh-pico/utils/math.h delete mode 100644 src/protocol/core.c delete mode 100644 src/utils/math.c diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index 4305fc0a3..6a3c6f7fe 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -161,11 +161,11 @@ #endif #ifndef Z_KID_RESOLUTION -#define Z_KID_RESOLUTION 0x03 +#define Z_KID_RESOLUTION 0x02 #endif #ifndef Z_REQ_RESOLUTION -#define Z_REQ_RESOLUTION 0x03 +#define Z_REQ_RESOLUTION 0x02 #endif /** diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index e549fb76b..639e17397 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -150,7 +150,4 @@ typedef struct { z_submode_t mode; } _z_subinfo_t; -typedef uint8_t _z_int_res_t; -uint8_t _z_intres_to_nbytes(_z_int_res_t intres); - #endif /* ZENOH_PICO_PROTOCOL_CORE_H */ diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index fbe30bade..7288b957f 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -762,9 +762,9 @@ typedef struct { uint16_t _batch_size; _z_conduit_sn_list_t _next_sn; z_whatami_t _whatami; - _z_int_res_t _key_id_res; - _z_int_res_t _req_id_res; - _z_int_res_t _seq_num_res; + uint8_t _key_id_res; + uint8_t _req_id_res; + uint8_t _seq_num_res; uint8_t _version; } _z_t_msg_join_t; void _z_t_msg_clear_join(_z_t_msg_join_t *msg); @@ -843,9 +843,9 @@ typedef struct { _z_bytes_t _cookie; uint16_t _batch_size; z_whatami_t _whatami; - _z_int_res_t _key_id_res; - _z_int_res_t _req_id_res; - _z_int_res_t _seq_num_res; + uint8_t _key_id_res; + uint8_t _req_id_res; + uint8_t _seq_num_res; uint8_t _version; } _z_t_msg_init_t; void _z_t_msg_clear_init(_z_t_msg_init_t *msg); diff --git a/include/zenoh-pico/transport/transport.h b/include/zenoh-pico/transport/transport.h index 380d8acc4..570bd1f4d 100644 --- a/include/zenoh-pico/transport/transport.h +++ b/include/zenoh-pico/transport/transport.h @@ -31,8 +31,7 @@ typedef struct { _z_bytes_t _remote_addr; // SN numbers - _z_zint_t _seq_num_res; - _z_zint_t _seq_num_res_half; + _z_zint_t _sn_res; volatile _z_zint_t _lease; volatile _z_zint_t _next_lease; @@ -69,8 +68,7 @@ typedef struct { _z_bytes_t _remote_zid; // SN numbers - _z_zint_t _seq_num_res; - _z_zint_t _seq_num_res_half; + _z_zint_t _sn_res; _z_zint_t _sn_tx_reliable; _z_zint_t _sn_tx_best_effort; _z_zint_t _sn_rx_reliable; @@ -110,8 +108,7 @@ typedef struct { _z_zbuf_t _zbuf; // SN initial numbers - _z_zint_t _seq_num_res; - _z_zint_t _seq_num_res_half; + _z_zint_t _sn_res; _z_zint_t _sn_tx_reliable; _z_zint_t _sn_tx_best_effort; volatile _z_zint_t _lease; @@ -151,15 +148,15 @@ typedef struct { _z_zint_t _initial_sn_tx; _z_zint_t _lease; z_whatami_t _whatami; - _z_int_res_t _key_id_res; - _z_int_res_t _req_id_res; - _z_int_res_t _seq_num_res; + uint8_t _key_id_res; + uint8_t _req_id_res; + uint8_t _seq_num_res; _Bool _is_qos; } _z_transport_unicast_establish_param_t; typedef struct { - _z_int_res_t _seq_num_res; _z_conduit_sn_list_t _initial_sn_tx; + uint8_t _seq_num_res; } _z_transport_multicast_establish_param_t; int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unicast_establish_param_t *param); diff --git a/include/zenoh-pico/transport/utils.h b/include/zenoh-pico/transport/utils.h index 99aaee7bc..b2de33c5c 100644 --- a/include/zenoh-pico/transport/utils.h +++ b/include/zenoh-pico/transport/utils.h @@ -21,10 +21,14 @@ #include "zenoh-pico/protocol/msg.h" /*------------------ SN helpers ------------------*/ -_Bool _z_sn_precedes(const _z_zint_t seq_num_res_half, const _z_zint_t sn_left, const _z_zint_t sn_right); -_z_zint_t _z_sn_increment(const _z_zint_t seq_num_res, const _z_zint_t sn); -_z_zint_t _z_sn_decrement(const _z_zint_t seq_num_res, const _z_zint_t sn); +_z_zint_t _z_sn_max(uint8_t bits); +_z_zint_t _z_sn_half(_z_zint_t sn); +_z_zint_t _z_sn_modulo_mask(uint8_t bits); +_Bool _z_sn_precedes(const _z_zint_t sn_resolution, const _z_zint_t sn_left, const _z_zint_t sn_right); +_z_zint_t _z_sn_increment(const _z_zint_t sn_resolution, const _z_zint_t sn); +_z_zint_t _z_sn_decrement(const _z_zint_t sn_resolution, const _z_zint_t sn); + void _z_conduit_sn_list_copy(_z_conduit_sn_list_t *dst, const _z_conduit_sn_list_t *src); -void _z_conduit_sn_list_decrement(const _z_zint_t seq_num_res, _z_conduit_sn_list_t *sns); +void _z_conduit_sn_list_decrement(const _z_zint_t sn_resolution, _z_conduit_sn_list_t *sns); #endif /* ZENOH_PICO_TRANSPORT_UTILS_H */ diff --git a/include/zenoh-pico/utils/math.h b/include/zenoh-pico/utils/math.h deleted file mode 100644 index 2db655770..000000000 --- a/include/zenoh-pico/utils/math.h +++ /dev/null @@ -1,27 +0,0 @@ -// -// 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, -// - -#include -#include - -/** - * Compute the maximum representable value. - * - * Parameters: - * n_bytes: Number of bytes used to represent the value. - * - * Returns: - * The maximum representable value. - */ -size_t _z_max_value(uint8_t n_bytes); diff --git a/src/protocol/core.c b/src/protocol/core.c deleted file mode 100644 index 325b6a9e7..000000000 --- a/src/protocol/core.c +++ /dev/null @@ -1,43 +0,0 @@ -// -// 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, -// - -#include "zenoh-pico/protocol/core.h" - -uint8_t _z_intres_to_nbytes(_z_int_res_t intres) { - uint8_t n_bytes = 0; - - switch (intres) { - case 0: { - n_bytes = 1; - } break; - - case 1: { - n_bytes = 2; - } break; - - case 2: { - n_bytes = 4; - } break; - - case 3: { - n_bytes = 8; - } break; - - default: { - n_bytes = 0; - } - } - - return n_bytes; -} \ No newline at end of file diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 6049d2bbc..86e644db0 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1201,7 +1201,6 @@ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { - printf("[Encode] I have a S flag\n"); cbyte = 0; cbyte |= (msg->_seq_num_res & 0x03); cbyte |= ((msg->_req_id_res & 0x03) << 2); diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index afd6e5a0c..4de8481c5 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -20,7 +20,6 @@ #include "zenoh-pico/session/utils.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" -#include "zenoh-pico/utils/math.h" #if Z_MULTICAST_TRANSPORT == 1 @@ -120,8 +119,8 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAME_R) == true) { // @TODO: amend once reliability is in place. For the time being only // monothonic SNs are ensured - if (_z_sn_precedes(entry->_seq_num_res_half, entry->_sn_rx_sns._val._plain._reliable, - t_msg->_body._frame._sn) == true) { + if (_z_sn_precedes(entry->_sn_res, entry->_sn_rx_sns._val._plain._reliable, t_msg->_body._frame._sn) == + true) { entry->_sn_rx_sns._val._plain._reliable = t_msg->_body._frame._sn; } else { _z_wbuf_clear(&entry->_dbuf_reliable); @@ -129,7 +128,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t break; } } else { - if (_z_sn_precedes(entry->_seq_num_res_half, entry->_sn_rx_sns._val._plain._best_effort, + if (_z_sn_precedes(entry->_sn_res, entry->_sn_rx_sns._val._plain._best_effort, t_msg->_body._frame._sn) == true) { entry->_sn_rx_sns._val._plain._best_effort = t_msg->_body._frame._sn; } else { @@ -225,8 +224,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t { entry = (_z_transport_peer_entry_t *)z_malloc(sizeof(_z_transport_peer_entry_t)); if (entry != NULL) { - entry->_seq_num_res = _z_max_value(_z_intres_to_nbytes(t_msg->_body._join._seq_num_res)); - entry->_seq_num_res_half = entry->_seq_num_res / 2; + entry->_sn_res = _z_sn_max(t_msg->_body._join._seq_num_res); // If the new node has less representing capabilities then it is incompatible to communication if ((t_msg->_body._join._seq_num_res < Z_SN_RESOLUTION) || @@ -241,7 +239,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t entry->_remote_zid = _z_bytes_duplicate(&t_msg->_body._join._zid); _z_conduit_sn_list_copy(&entry->_sn_rx_sns, &t_msg->_body._join._next_sn); - _z_conduit_sn_list_decrement(entry->_seq_num_res, &entry->_sn_rx_sns); + _z_conduit_sn_list_decrement(entry->_sn_res, &entry->_sn_rx_sns); #if Z_DYNAMIC_MEMORY_ALLOCATION == 1 entry->_dbuf_reliable = _z_wbuf_make(0, true); @@ -277,7 +275,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t // Update SNs _z_conduit_sn_list_copy(&entry->_sn_rx_sns, &t_msg->_body._join._next_sn); - _z_conduit_sn_list_decrement(entry->_seq_num_res, &entry->_sn_rx_sns); + _z_conduit_sn_list_decrement(entry->_sn_res, &entry->_sn_rx_sns); // Update lease time (set as ms during) entry->_lease = t_msg->_body._join._lease; diff --git a/src/transport/multicast/link/tx.c b/src/transport/multicast/link/tx.c index 7849d00a3..7e6fba9e1 100644 --- a/src/transport/multicast/link/tx.c +++ b/src/transport/multicast/link/tx.c @@ -30,10 +30,10 @@ _z_zint_t __unsafe_z_multicast_get_sn(_z_transport_multicast_t *ztm, z_reliabili _z_zint_t sn; if (reliability == Z_RELIABILITY_RELIABLE) { sn = ztm->_sn_tx_reliable; - ztm->_sn_tx_reliable = _z_sn_increment(ztm->_seq_num_res, ztm->_sn_tx_reliable); + ztm->_sn_tx_reliable = _z_sn_increment(ztm->_sn_res, ztm->_sn_tx_reliable); } else { sn = ztm->_sn_tx_best_effort; - ztm->_sn_tx_best_effort = _z_sn_increment(ztm->_seq_num_res, ztm->_sn_tx_best_effort); + ztm->_sn_tx_best_effort = _z_sn_increment(ztm->_sn_res, ztm->_sn_tx_best_effort); } return sn; } diff --git a/src/transport/peer_entry.c b/src/transport/peer_entry.c index 84884d89a..638e087a1 100644 --- a/src/transport/peer_entry.c +++ b/src/transport/peer_entry.c @@ -27,8 +27,7 @@ void _z_transport_peer_entry_copy(_z_transport_peer_entry_t *dst, const _z_trans _z_wbuf_copy(&dst->_dbuf_reliable, &src->_dbuf_reliable); _z_wbuf_copy(&dst->_dbuf_best_effort, &src->_dbuf_best_effort); - dst->_seq_num_res = src->_seq_num_res; - dst->_seq_num_res_half = src->_seq_num_res_half; + dst->_sn_res = src->_sn_res; _z_conduit_sn_list_copy(&dst->_sn_rx_sns, &src->_sn_rx_sns); dst->_lease = src->_lease; diff --git a/src/transport/transport.c b/src/transport/transport.c index 34e0ea25f..64196ef30 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -23,7 +23,6 @@ #include "zenoh-pico/transport/link/tx.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" -#include "zenoh-pico/utils/math.h" #if Z_UNICAST_TRANSPORT == 1 int8_t _z_unicast_send_close(_z_transport_unicast_t *ztu, uint8_t reason, _Bool link_only) { @@ -132,16 +131,16 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic if (ret == _Z_RES_OK) { // Set default SN resolution - zt->_transport._unicast._seq_num_res = _z_max_value(_z_intres_to_nbytes(param->_seq_num_res)); - zt->_transport._unicast._seq_num_res_half = zt->_transport._unicast._seq_num_res / 2; + zt->_transport._unicast._sn_res = _z_sn_max(param->_seq_num_res); // The initial SN at TX side zt->_transport._unicast._sn_tx_reliable = param->_initial_sn_tx; zt->_transport._unicast._sn_tx_best_effort = param->_initial_sn_tx; // The initial SN at RX side - zt->_transport._unicast._sn_rx_reliable = param->_initial_sn_rx; - zt->_transport._unicast._sn_rx_best_effort = param->_initial_sn_rx; + _z_zint_t initial_sn_rx = _z_sn_decrement(zt->_transport._unicast._sn_res, param->_initial_sn_rx); + zt->_transport._unicast._sn_rx_reliable = initial_sn_rx; + zt->_transport._unicast._sn_rx_best_effort = initial_sn_rx; #if Z_MULTI_THREAD == 1 // Tasks @@ -218,8 +217,7 @@ int8_t _z_transport_multicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_mu if (ret == _Z_RES_OK) { // Set default SN resolution - zt->_transport._multicast._seq_num_res = _z_max_value(_z_intres_to_nbytes(param->_seq_num_res)); - zt->_transport._multicast._seq_num_res_half = zt->_transport._multicast._seq_num_res / 2; + zt->_transport._multicast._sn_res = _z_sn_max(param->_seq_num_res); // The initial SN at TX side zt->_transport._multicast._sn_tx_reliable = param->_initial_sn_tx._val._plain._reliable; @@ -281,28 +279,31 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * } if (iam._body._init._key_id_res <= param->_key_id_res) { - param->_seq_num_res = iam._body._init._seq_num_res; + param->_key_id_res = iam._body._init._key_id_res; } else { ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; } if (iam._body._init._req_id_res <= param->_req_id_res) { - param->_seq_num_res = iam._body._init._seq_num_res; + param->_req_id_res = iam._body._init._req_id_res; } else { ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; } if (iam._body._init._batch_size <= param->_batch_size) { - param->_seq_num_res = iam._body._init._seq_num_res; + param->_batch_size = iam._body._init._batch_size; } else { ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; } } if (ret == _Z_RES_OK) { + param->_key_id_res = 0x08 << param->_key_id_res; + param->_req_id_res = 0x08 << param->_req_id_res; + // The initial SN at TX side z_random_fill(¶m->_initial_sn_tx, sizeof(param->_initial_sn_tx)); - param->_initial_sn_tx = param->_initial_sn_tx % param->_seq_num_res; + param->_initial_sn_tx = param->_initial_sn_tx & !_z_sn_modulo_mask(param->_seq_num_res); // Initialize the Local and Remote Peer IDs _z_bytes_copy(¶m->_remote_zid, &iam._body._init._zid); @@ -329,8 +330,7 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * // The initial SN at RX side. Initialize the session as we had already received // a message with a SN equal to initial_sn - 1. - param->_initial_sn_rx = - _z_sn_decrement(param->_seq_num_res, oam._body._open._initial_sn); + param->_initial_sn_rx = oam._body._open._initial_sn; } else { ret = _Z_ERR_MESSAGE_UNEXPECTED; } @@ -386,7 +386,7 @@ int8_t _z_transport_multicast_open_peer(_z_transport_multicast_establish_param_t _z_zint_t initial_sn_tx = 0; z_random_fill(&initial_sn_tx, sizeof(initial_sn_tx)); - initial_sn_tx = initial_sn_tx % _z_max_value(_z_intres_to_nbytes(Z_SN_RESOLUTION)); + initial_sn_tx = initial_sn_tx & !_z_sn_modulo_mask(Z_SN_RESOLUTION); _z_conduit_sn_list_t next_sn; next_sn._val._plain._best_effort = initial_sn_tx; diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index c97711e55..aeee15096 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -91,7 +91,7 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAME_R) == true) { // @TODO: amend once reliability is in place. For the time being only // monothonic SNs are ensured - if (_z_sn_precedes(ztu->_seq_num_res_half, ztu->_sn_rx_reliable, t_msg->_body._frame._sn) == true) { + if (_z_sn_precedes(ztu->_sn_res, ztu->_sn_rx_reliable, t_msg->_body._frame._sn) == true) { ztu->_sn_rx_reliable = t_msg->_body._frame._sn; } else { _z_wbuf_clear(&ztu->_dbuf_reliable); @@ -99,7 +99,7 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans break; } } else { - if (_z_sn_precedes(ztu->_seq_num_res_half, ztu->_sn_rx_best_effort, t_msg->_body._frame._sn) == true) { + if (_z_sn_precedes(ztu->_sn_res, ztu->_sn_rx_best_effort, t_msg->_body._frame._sn) == true) { ztu->_sn_rx_best_effort = t_msg->_body._frame._sn; } else { _z_wbuf_clear(&ztu->_dbuf_best_effort); diff --git a/src/transport/unicast/link/tx.c b/src/transport/unicast/link/tx.c index 54c2df646..d27c61b3f 100644 --- a/src/transport/unicast/link/tx.c +++ b/src/transport/unicast/link/tx.c @@ -31,10 +31,10 @@ _z_zint_t __unsafe_z_unicast_get_sn(_z_transport_unicast_t *ztu, z_reliability_t _z_zint_t sn; if (reliability == Z_RELIABILITY_RELIABLE) { sn = ztu->_sn_tx_reliable; - ztu->_sn_tx_reliable = _z_sn_increment(ztu->_seq_num_res, ztu->_sn_tx_reliable); + ztu->_sn_tx_reliable = _z_sn_increment(ztu->_sn_res, ztu->_sn_tx_reliable); } else { sn = ztu->_sn_tx_best_effort; - ztu->_sn_tx_best_effort = _z_sn_increment(ztu->_seq_num_res, ztu->_sn_tx_best_effort); + ztu->_sn_tx_best_effort = _z_sn_increment(ztu->_sn_res, ztu->_sn_tx_best_effort); } return sn; } diff --git a/src/transport/utils.c b/src/transport/utils.c index dcb259a2e..87d589139 100644 --- a/src/transport/utils.c +++ b/src/transport/utils.c @@ -14,32 +14,82 @@ #include "zenoh-pico/transport/utils.h" -_Bool _z_sn_precedes(const _z_zint_t seq_num_res_half, const _z_zint_t sn_left, const _z_zint_t sn_right) { - _Bool ret = false; +#define U8_MAX 0xFF +#define U16_MAX 0xFFFF +#define U32_MAX 0xFFFFFFFF +#define U64_MAX 0xFFFFFFFFFFFFFFFF - if (sn_right > sn_left) { - ret = ((sn_right - sn_left) <= seq_num_res_half); - } else { - ret = ((sn_left - sn_right) > seq_num_res_half); +_z_zint_t _z_sn_max(uint8_t bits) { + _z_zint_t ret = 0; + switch (bits) { + case 0x00: { + ret = U8_MAX >> 1; + } break; + + case 0x01: { + ret = U16_MAX >> 2; + } break; + + case 0x02: { + ret = U32_MAX >> 4; + } break; + + case 0x03: { + ret = U64_MAX >> 1; + } break; + + default: { + // Do nothing + } break; } return ret; } -_z_zint_t _z_sn_increment(const _z_zint_t seq_num_res, const _z_zint_t sn) { return (sn + 1) % seq_num_res; } +_z_zint_t _z_sn_half(_z_zint_t sn) { return sn >> 1; } -_z_zint_t _z_sn_decrement(const _z_zint_t seq_num_res, const _z_zint_t sn) { +_z_zint_t _z_sn_modulo_mask(uint8_t bits) { _z_zint_t ret = 0; + switch (bits) { + case 0x00: { + ret = U8_MAX >> 1; + } break; - if (sn == 0) { - ret = seq_num_res - 1; - } else { - ret = sn - 1; + case 0x01: { + ret = U16_MAX >> 2; + } break; + + case 0x02: { + ret = U32_MAX >> 4; + } break; + + case 0x03: { + ret = U64_MAX >> 1; + } break; + + default: { + // Do nothing + } break; } return ret; } +_Bool _z_sn_precedes(const _z_zint_t sn_resolution, const _z_zint_t sn_left, const _z_zint_t sn_right) { + _z_zint_t distance = (sn_right - sn_left) & sn_resolution; + return ((distance <= _z_sn_half(sn_resolution)) && (distance != 0)); +} + +_z_zint_t _z_sn_increment(const _z_zint_t sn_resolution, const _z_zint_t sn) { + _z_zint_t ret = sn + 1; + return (ret &= sn_resolution); +} + +_z_zint_t _z_sn_decrement(const _z_zint_t sn_resolution, const _z_zint_t sn) { + _z_zint_t ret = sn - 1; + return (ret &= sn_resolution); +} + void _z_conduit_sn_list_copy(_z_conduit_sn_list_t *dst, const _z_conduit_sn_list_t *src) { dst->_is_qos = src->_is_qos; if (dst->_is_qos == false) { @@ -53,14 +103,14 @@ void _z_conduit_sn_list_copy(_z_conduit_sn_list_t *dst, const _z_conduit_sn_list } } -void _z_conduit_sn_list_decrement(const _z_zint_t seq_num_res, _z_conduit_sn_list_t *sns) { +void _z_conduit_sn_list_decrement(const _z_zint_t sn_resolution, _z_conduit_sn_list_t *sns) { if (sns->_is_qos == false) { - sns->_val._plain._best_effort = _z_sn_decrement(seq_num_res, sns->_val._plain._best_effort); - sns->_val._plain._reliable = _z_sn_decrement(seq_num_res, sns->_val._plain._reliable); + sns->_val._plain._best_effort = _z_sn_decrement(sn_resolution, sns->_val._plain._best_effort); + sns->_val._plain._reliable = _z_sn_decrement(sn_resolution, sns->_val._plain._reliable); } else { for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { - sns->_val._qos[i]._best_effort = _z_sn_decrement(seq_num_res, sns->_val._qos[i]._best_effort); - sns->_val._qos[i]._best_effort = _z_sn_decrement(seq_num_res, sns->_val._qos[i]._reliable); + sns->_val._qos[i]._best_effort = _z_sn_decrement(sn_resolution, sns->_val._qos[i]._best_effort); + sns->_val._qos[i]._best_effort = _z_sn_decrement(sn_resolution, sns->_val._qos[i]._reliable); } } } \ No newline at end of file diff --git a/src/utils/math.c b/src/utils/math.c deleted file mode 100644 index 81e3e6988..000000000 --- a/src/utils/math.c +++ /dev/null @@ -1,24 +0,0 @@ -// -// 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, -// - -#include "zenoh-pico/utils/math.h" - -size_t _z_max_value(uint8_t n_bytes) { - size_t max = 1; - for (uint8_t i = 0; i < (n_bytes * 8); i++) { - max *= 2; - } - - return (max - 1); -} From 6dc9ad28ea8c9871f747325e01d42c49fe8d49e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 24 Mar 2023 15:37:34 +0100 Subject: [PATCH 052/181] Fix resolution negotiation --- src/transport/transport.c | 50 +++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/transport/transport.c b/src/transport/transport.c index 64196ef30..63f73f106 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -255,6 +255,9 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * _z_bytes_t zid = _z_bytes_wrap(local_zid->start, local_zid->len); _z_transport_message_t ism = _z_t_msg_make_init_syn(Z_WHATAMI_CLIENT, zid); param->_seq_num_res = ism._body._init._seq_num_res; // The announced sn resolution + param->_key_id_res = ism._body._init._key_id_res; // The announced key id resolution + param->_req_id_res = ism._body._init._req_id_res; // The announced req id resolution + param->_batch_size = ism._body._init._batch_size; // The announced batch size // Encode and send the message _Z_INFO("Sending Z_INIT(Syn)\n"); @@ -267,34 +270,31 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * if ((_Z_MID(iam._header) == _Z_MID_INIT) && (_Z_HAS_FLAG(iam._header, _Z_FLAG_INIT_A) == true)) { _Z_INFO("Received Z_INIT(Ack)\n"); - // Handle SN resolution option if present - if (_Z_HAS_FLAG(iam._header, _Z_FLAG_INIT_S) == true) { - // Any of the size parameters in the InitAck must be less or equal than the one in the InitSyn, - // otherwise the InitAck message is considered invalid and it should be treated as a - // CLOSE message with L==0 by the Initiating Peer -- the recipient of the InitAck message. - if (iam._body._init._seq_num_res <= param->_seq_num_res) { - param->_seq_num_res = iam._body._init._seq_num_res; - } else { - ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; - } + // Any of the size parameters in the InitAck must be less or equal than the one in the InitSyn, + // otherwise the InitAck message is considered invalid and it should be treated as a + // CLOSE message with L==0 by the Initiating Peer -- the recipient of the InitAck message. + if (iam._body._init._seq_num_res <= param->_seq_num_res) { + param->_seq_num_res = iam._body._init._seq_num_res; + } else { + ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; + } - if (iam._body._init._key_id_res <= param->_key_id_res) { - param->_key_id_res = iam._body._init._key_id_res; - } else { - ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; - } + if (iam._body._init._key_id_res <= param->_key_id_res) { + param->_key_id_res = iam._body._init._key_id_res; + } else { + ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; + } - if (iam._body._init._req_id_res <= param->_req_id_res) { - param->_req_id_res = iam._body._init._req_id_res; - } else { - ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; - } + if (iam._body._init._req_id_res <= param->_req_id_res) { + param->_req_id_res = iam._body._init._req_id_res; + } else { + ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; + } - if (iam._body._init._batch_size <= param->_batch_size) { - param->_batch_size = iam._body._init._batch_size; - } else { - ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; - } + if (iam._body._init._batch_size <= param->_batch_size) { + param->_batch_size = iam._body._init._batch_size; + } else { + ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; } if (ret == _Z_RES_OK) { From a0d3ec680c92d7b0a37966282529e7f6ebcd04b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Mon, 27 Mar 2023 09:34:48 +0200 Subject: [PATCH 053/181] Ease the integration of Zenoh-Pico in native Zephyr projects (#180) * Zephyr build integrated with Cmake * Allow both Zephyr 2.X via PlatformIO and Zephyr 3.X * Reorganize CMake output messages --- CMakeLists.txt | 181 ++++++++++---------- extra_script.py | 2 +- include/zenoh-pico/system/platform/zephyr.h | 7 +- src/system/zephyr/network.c | 5 + src/system/zephyr/system.c | 6 + 5 files changed, 112 insertions(+), 89 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d22098853..1a29d3ad4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,8 +24,8 @@ if(NOT CMAKE_C_STANDARD) message(STATUS "Setting C99 as the C Standard") endif() endif() - set(CMAKE_C_STANDARD_REQUIRED TRUE) +add_definitions(-DZENOH_C_STANDARD=${CMAKE_C_STANDARD}) # while in development, use timestamp for patch version: string(TIMESTAMP PROJECT_VERSION_PATCH "%Y%m%ddev") @@ -35,32 +35,16 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE RELEASE) endif() - string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE) -message(STATUS "Building in ${CMAKE_BUILD_TYPE} mode") option(BUILD_SHARED_LIBS "Build shared libraries if ON, otherwise build static libraries" ON) -message(STATUS "Build shared library: ${BUILD_SHARED_LIBS}") - option(PACKAGING "Use option on Linux to produce Debian and RPM packages." OFF) -message(STATUS "Produce Debian and RPM packages: ${PACKAGING}") - option(BUILD_EXAMPLES "Use this to also build the examples." ON) -message(STATUS "Build examples: ${BUILD_EXAMPLES}") - option(BUILD_TOOLS "Use this to also build the tools." OFF) -message(STATUS "Build examples: ${BUILD_TOOLS}") - option(BUILD_TESTING "Use this to also build tests." ON) -message(STATUS "Build tests: ${BUILD_TESTING}") - option(BUILD_INTEGRATION "Use this to also build integration tests." OFF) -message(STATUS "Build integration: ${BUILD_INTEGRATION}") - option(ZENOH_DEBUG "Use this to set the ZENOH_DEBUG variable." 0) -message(STATUS "Zenoh Level Log: ${ZENOH_DEBUG}") - -message(STATUS "Configuring for ${CMAKE_SYSTEM_NAME}") +option(WITH_ZEPHYR "Build for Zephyr RTOS" OFF) if(CMAKE_SYSTEM_NAME MATCHES "Linux") add_definitions(-DZENOH_LINUX) @@ -76,13 +60,29 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Windows") add_definitions(-DZENOH_WINDOWS -D_CRT_SECURE_NO_WARNINGS) set(BUILD_SHARED_LIBS "OFF") add_definitions(-DZENOH_NO_STDATOMIC) +elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") + if(WITH_ZEPHYR) + add_definitions(-DZENOH_ZEPHYR) + set(BUILD_SHARED_LIBS "OFF") + endif() else() message(FATAL_ERROR "zenoh-pico is not yet available on ${CMAKE_SYSTEM_NAME} platform") return() endif() add_definitions(-DZENOH_DEBUG=${ZENOH_DEBUG}) -add_definitions(-DZENOH_C_STANDARD=${CMAKE_C_STANDARD}) + +# Print summary of CMAKE configurations +message(STATUS "Building in ${CMAKE_BUILD_TYPE} mode") +message(STATUS "Build shared library: ${BUILD_SHARED_LIBS}") +message(STATUS "Produce Debian and RPM packages: ${PACKAGING}") +message(STATUS "Build examples: ${BUILD_EXAMPLES}") +message(STATUS "Build examples: ${BUILD_TOOLS}") +message(STATUS "Build tests: ${BUILD_TESTING}") +message(STATUS "Build integration: ${BUILD_INTEGRATION}") +message(STATUS "Zenoh Level Log: ${ZENOH_DEBUG}") +message(STATUS "Build for Zephyr RTOS: ${WITH_ZEPHYR}") +message(STATUS "Configuring for ${CMAKE_SYSTEM_NAME}") if(SKBUILD) set(INSTALL_RPATH "zenoh") @@ -96,14 +96,18 @@ find_package(Threads REQUIRED) if(CMAKE_BUILD_TYPE MATCHES "DEBUG") if(UNIX) add_compile_options(-c -Wall -Wextra -Werror -Wunused -Wstrict-prototypes -pipe -g -O0) - elseif (MSVC) + elseif(MSVC) add_compile_options(/W4 /WX /Od) + 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) + elseif(MSVC) add_compile_options(/O2) + elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") + add_compile_options(-pipe -O3) endif() endif() @@ -149,17 +153,16 @@ file(GLOB Sources "src/*.c" "src/utils/*.c" ) -if(CMAKE_SYSTEM_NAME MATCHES "Linux" OR CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "BSD") +if(WITH_ZEPHYR) + file (GLOB Sources_Zephyr "src/system/zephyr/*.c") + list(APPEND Sources ${Sources_Zephyr}) +elseif(CMAKE_SYSTEM_NAME MATCHES "Linux" OR CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "BSD") file (GLOB Sources_Unix "src/system/unix/*.c") list(APPEND Sources ${Sources_Unix}) -endif() - -if(CMAKE_SYSTEM_NAME MATCHES "Emscripten") +elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten") file (GLOB Sources_Emscripten "src/system/emscripten/*.c") list(APPEND Sources ${Sources_Emscripten}) -endif() - -if(CMAKE_SYSTEM_NAME MATCHES "Windows") +elseif(CMAKE_SYSTEM_NAME MATCHES "Windows") file (GLOB Sources_Windows "src/system/windows/*.c") list(APPEND Sources ${Sources_Windows}) endif() @@ -220,7 +223,7 @@ if(BUILD_EXAMPLES) add_executable(z_info ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_info.c) add_executable(z_scout ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_scout.c) endif() - elseif (MSVC) + elseif(MSVC) add_executable(z_put ${PROJECT_SOURCE_DIR}/examples/windows/z_put.c) add_executable(z_pub ${PROJECT_SOURCE_DIR}/examples/windows/z_pub.c) add_executable(z_pub_st ${PROJECT_SOURCE_DIR}/examples/windows/z_pub_st.c) @@ -233,76 +236,80 @@ if(BUILD_EXAMPLES) add_executable(z_scout ${PROJECT_SOURCE_DIR}/examples/windows/z_scout.c) endif() - target_link_libraries(z_put ${Libname}) - target_link_libraries(z_pub ${Libname}) - target_link_libraries(z_pub_st ${Libname}) - target_link_libraries(z_sub ${Libname}) - target_link_libraries(z_sub_st ${Libname}) - target_link_libraries(z_pull ${Libname}) - target_link_libraries(z_get ${Libname}) - target_link_libraries(z_queryable ${Libname}) - target_link_libraries(z_info ${Libname}) - target_link_libraries(z_scout ${Libname}) -endif() - -if(BUILD_TOOLS) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tools) - add_executable(z_keyexpr_canonizer ${PROJECT_SOURCE_DIR}/tools/z_keyexpr_canonizer.c) - target_link_libraries(z_keyexpr_canonizer ${Libname}) + if(UNIX OR MSVC) + target_link_libraries(z_put ${Libname}) + target_link_libraries(z_pub ${Libname}) + target_link_libraries(z_pub_st ${Libname}) + target_link_libraries(z_sub ${Libname}) + target_link_libraries(z_sub_st ${Libname}) + target_link_libraries(z_pull ${Libname}) + target_link_libraries(z_get ${Libname}) + target_link_libraries(z_queryable ${Libname}) + target_link_libraries(z_info ${Libname}) + target_link_libraries(z_scout ${Libname}) + endif() endif() -if(BUILD_TESTING) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") - - add_executable(z_data_struct_test ${PROJECT_SOURCE_DIR}/tests/z_data_struct_test.c) - add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c) - add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c) - add_executable(z_msgcodec_test ${PROJECT_SOURCE_DIR}/tests/z_msgcodec_test.c) - add_executable(z_keyexpr_test ${PROJECT_SOURCE_DIR}/tests/z_keyexpr_test.c) - - target_link_libraries(z_data_struct_test ${Libname}) - target_link_libraries(z_endpoint_test ${Libname}) - target_link_libraries(z_iobuf_test ${Libname}) - target_link_libraries(z_msgcodec_test ${Libname}) - target_link_libraries(z_keyexpr_test ${Libname}) - - enable_testing() - add_test(z_data_struct_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_data_struct_test) - add_test(z_endpoint_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_endpoint_test) - add_test(z_iobuf_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iobuf_test) - add_test(z_msgcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_msgcodec_test) - add_test(z_keyexpr_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_keyexpr_test) -endif() +if(UNIX OR MSVC) + if(BUILD_TOOLS) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tools) + add_executable(z_keyexpr_canonizer ${PROJECT_SOURCE_DIR}/tools/z_keyexpr_canonizer.c) + target_link_libraries(z_keyexpr_canonizer ${Libname}) + endif() -if(BUILD_MULTICAST) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") + if(BUILD_TESTING) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") - if(CMAKE_C_STANDARD MATCHES "11") - add_executable(z_peer_multicast_test ${PROJECT_SOURCE_DIR}/tests/z_peer_multicast_test.c) - target_link_libraries(z_peer_multicast_test ${Libname}) + add_executable(z_data_struct_test ${PROJECT_SOURCE_DIR}/tests/z_data_struct_test.c) + add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c) + add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c) + add_executable(z_msgcodec_test ${PROJECT_SOURCE_DIR}/tests/z_msgcodec_test.c) + add_executable(z_keyexpr_test ${PROJECT_SOURCE_DIR}/tests/z_keyexpr_test.c) - configure_file(${PROJECT_SOURCE_DIR}/tests/multicast.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh COPYONLY) + target_link_libraries(z_data_struct_test ${Libname}) + target_link_libraries(z_endpoint_test ${Libname}) + target_link_libraries(z_iobuf_test ${Libname}) + target_link_libraries(z_msgcodec_test ${Libname}) + target_link_libraries(z_keyexpr_test ${Libname}) enable_testing() - add_test(z_peer_multicast_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh z_peer_multicast_test) + add_test(z_data_struct_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_data_struct_test) + add_test(z_endpoint_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_endpoint_test) + add_test(z_iobuf_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iobuf_test) + add_test(z_msgcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_msgcodec_test) + add_test(z_keyexpr_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_keyexpr_test) endif() -endif() -if(BUILD_INTEGRATION) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") + if(BUILD_MULTICAST) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") - if(CMAKE_C_STANDARD MATCHES "11") - add_executable(z_client_test ${PROJECT_SOURCE_DIR}/tests/z_client_test.c) - add_executable(z_api_alignment_test ${PROJECT_SOURCE_DIR}/tests/z_api_alignment_test.c) - target_link_libraries(z_client_test ${Libname}) - target_link_libraries(z_api_alignment_test ${Libname}) + if(CMAKE_C_STANDARD MATCHES "11") + add_executable(z_peer_multicast_test ${PROJECT_SOURCE_DIR}/tests/z_peer_multicast_test.c) + target_link_libraries(z_peer_multicast_test ${Libname}) - configure_file(${PROJECT_SOURCE_DIR}/tests/routed.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh COPYONLY) - configure_file(${PROJECT_SOURCE_DIR}/tests/api.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh COPYONLY) + configure_file(${PROJECT_SOURCE_DIR}/tests/multicast.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh COPYONLY) - enable_testing() - add_test(z_client_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh z_client_test) - add_test(z_api_alignment_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh z_api_alignment_test) + enable_testing() + add_test(z_peer_multicast_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh z_peer_multicast_test) + endif() + endif() + + if(BUILD_INTEGRATION) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") + + if(CMAKE_C_STANDARD MATCHES "11") + add_executable(z_client_test ${PROJECT_SOURCE_DIR}/tests/z_client_test.c) + add_executable(z_api_alignment_test ${PROJECT_SOURCE_DIR}/tests/z_api_alignment_test.c) + target_link_libraries(z_client_test ${Libname}) + target_link_libraries(z_api_alignment_test ${Libname}) + + configure_file(${PROJECT_SOURCE_DIR}/tests/routed.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh COPYONLY) + configure_file(${PROJECT_SOURCE_DIR}/tests/api.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh COPYONLY) + + enable_testing() + add_test(z_client_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh z_client_test) + add_test(z_api_alignment_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh z_api_alignment_test) + endif() endif() endif() diff --git a/extra_script.py b/extra_script.py index 391eb8047..c454607cc 100644 --- a/extra_script.py +++ b/extra_script.py @@ -28,7 +28,7 @@ "-", "-", "-"] - CPPDEFINES = ["ZENOH_ZEPHYR"] + CPPDEFINES = ["ZENOH_ZEPHYR", "ZENOH_PIO"] elif FRAMEWORK == 'arduino': PLATFORM = env.get("PIOPLATFORM") diff --git a/include/zenoh-pico/system/platform/zephyr.h b/include/zenoh-pico/system/platform/zephyr.h index 19c8795c3..ca3924e11 100644 --- a/include/zenoh-pico/system/platform/zephyr.h +++ b/include/zenoh-pico/system/platform/zephyr.h @@ -15,8 +15,13 @@ #ifndef ZENOH_PICO_SYSTEM_ZEPHYR_TYPES_H #define ZENOH_PICO_SYSTEM_ZEPHYR_TYPES_H -#include +#if defined(ZENOH_PIO) #include +#else +#include +#endif + +#include #include "zenoh-pico/config.h" diff --git a/src/system/zephyr/network.c b/src/system/zephyr/network.c index 4c8f7574e..e89afc107 100644 --- a/src/system/zephyr/network.c +++ b/src/system/zephyr/network.c @@ -12,7 +12,12 @@ // ZettaScale Zenoh Team, // +#if defined(ZENOH_PIO) #include +#else +#include +#endif + #include #include #include diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index d34dc2581..37c2333f1 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -12,8 +12,14 @@ // ZettaScale Zenoh Team, // +#if defined(ZENOH_PIO) #include #include +#else +#include +#include +#endif + #include #include #include From 31d61099e49a8812825e84e39e46b2cac3d308a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Mon, 27 Mar 2023 10:01:32 +0200 Subject: [PATCH 054/181] Align drop macros with Zenoh-C (#181) (#182) --- src/api/api.c | 129 +++++++++++++++++++++++++++++--------------------- 1 file changed, 74 insertions(+), 55 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 58bf66fba..edbc94ada 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -29,6 +29,7 @@ #include "zenoh-pico/session/queryable.h" #include "zenoh-pico/session/resource.h" #include "zenoh-pico/session/utils.h" +#include "zenoh-pico/utils/result.h" /********* Data Types Handlers *********/ z_string_t z_string_make(const char *value) { return _z_string_make(value); } @@ -298,22 +299,26 @@ _Bool z_value_is_initialized(z_value_t *value) { } \ } -#define OWNED_FUNCTIONS_PTR(type, ownedtype, name, f_free, f_copy) \ +#define OWNED_FUNCTIONS_PTR_COMMON(type, ownedtype, name) \ _Bool z_##name##_check(const ownedtype *val) { return val->_value != NULL; } \ type z_##name##_loan(const ownedtype *val) { return (type){._val = val->_value}; } \ - ownedtype *z_##name##_move(ownedtype *val) { return val; } \ - ownedtype z_##name##_clone(ownedtype *val) { \ - ownedtype ret; \ - ret._value = (_##type *)z_malloc(sizeof(_##type)); \ - if (ret._value != NULL) { \ - f_copy(ret._value, val->_value); \ - } \ - return ret; \ - } \ - void z_##name##_drop(ownedtype *val) { \ - if (val->_value != NULL) { \ - f_free(&val->_value); \ - } \ + ownedtype *z_##name##_move(ownedtype *val) { return val; } + +#define OWNED_FUNCTIONS_PTR_CLONE(type, ownedtype, name, f_copy) \ + ownedtype z_##name##_clone(ownedtype *val) { \ + ownedtype ret; \ + ret._value = (_##type *)z_malloc(sizeof(_##type)); \ + if (ret._value != NULL) { \ + f_copy(ret._value, val->_value); \ + } \ + return ret; \ + } + +#define OWNED_FUNCTIONS_PTR_DROP(type, ownedtype, name, f_free) \ + void z_##name##_drop(ownedtype *val) { \ + if (val->_value != NULL) { \ + f_free(&val->_value); \ + } \ } #define OWNED_FUNCTIONS_STR(type, ownedtype, name, f_free, f_copy) \ @@ -342,15 +347,33 @@ static inline void _z_owner_noop_copy(void *dst, const void *src) { OWNED_FUNCTIONS_STR(z_str_t, z_owned_str_t, str, _z_str_free, _z_str_n_copy) -OWNED_FUNCTIONS_PTR(z_config_t, z_owned_config_t, config, _z_config_free, _z_owner_noop_copy) -OWNED_FUNCTIONS_PTR(z_scouting_config_t, z_owned_scouting_config_t, scouting_config, _z_scouting_config_free, - _z_owner_noop_copy) -OWNED_FUNCTIONS_PTR(z_session_t, z_owned_session_t, session, _z_session_free, _z_owner_noop_copy) -OWNED_FUNCTIONS_PTR(z_subscriber_t, z_owned_subscriber_t, subscriber, _z_subscriber_free, _z_owner_noop_copy) -OWNED_FUNCTIONS_PTR(z_pull_subscriber_t, z_owned_pull_subscriber_t, pull_subscriber, _z_subscriber_free, - _z_owner_noop_copy) -OWNED_FUNCTIONS_PTR(z_publisher_t, z_owned_publisher_t, publisher, _z_publisher_free, _z_owner_noop_copy) -OWNED_FUNCTIONS_PTR(z_queryable_t, z_owned_queryable_t, queryable, _z_queryable_free, _z_owner_noop_copy) +OWNED_FUNCTIONS_PTR_COMMON(z_config_t, z_owned_config_t, config) +OWNED_FUNCTIONS_PTR_CLONE(z_config_t, z_owned_config_t, config, _z_owner_noop_copy) +OWNED_FUNCTIONS_PTR_DROP(z_config_t, z_owned_config_t, config, _z_config_free) + +OWNED_FUNCTIONS_PTR_COMMON(z_scouting_config_t, z_owned_scouting_config_t, scouting_config) +OWNED_FUNCTIONS_PTR_CLONE(z_scouting_config_t, z_owned_scouting_config_t, scouting_config, _z_owner_noop_copy) +OWNED_FUNCTIONS_PTR_DROP(z_scouting_config_t, z_owned_scouting_config_t, scouting_config, _z_scouting_config_free) + +OWNED_FUNCTIONS_PTR_COMMON(z_session_t, z_owned_session_t, session) +OWNED_FUNCTIONS_PTR_CLONE(z_session_t, z_owned_session_t, session, _z_owner_noop_copy) +void z_session_drop(z_owned_session_t *val) { z_close(val); } + +OWNED_FUNCTIONS_PTR_COMMON(z_subscriber_t, z_owned_subscriber_t, subscriber) +OWNED_FUNCTIONS_PTR_CLONE(z_subscriber_t, z_owned_subscriber_t, subscriber, _z_owner_noop_copy) +void z_subscriber_drop(z_owned_subscriber_t *val) { z_undeclare_subscriber(val); } + +OWNED_FUNCTIONS_PTR_COMMON(z_pull_subscriber_t, z_owned_pull_subscriber_t, pull_subscriber) +OWNED_FUNCTIONS_PTR_CLONE(z_pull_subscriber_t, z_owned_pull_subscriber_t, pull_subscriber, _z_owner_noop_copy) +void z_pull_subscriber_drop(z_owned_pull_subscriber_t *val) { z_undeclare_pull_subscriber(val); } + +OWNED_FUNCTIONS_PTR_COMMON(z_publisher_t, z_owned_publisher_t, publisher) +OWNED_FUNCTIONS_PTR_CLONE(z_publisher_t, z_owned_publisher_t, publisher, _z_owner_noop_copy) +void z_publisher_drop(z_owned_publisher_t *val) { z_undeclare_publisher(val); } + +OWNED_FUNCTIONS_PTR_COMMON(z_queryable_t, z_owned_queryable_t, queryable) +OWNED_FUNCTIONS_PTR_CLONE(z_queryable_t, z_owned_queryable_t, queryable, _z_owner_noop_copy) +void z_queryable_drop(z_owned_queryable_t *val) { z_undeclare_queryable(val); } OWNED_FUNCTIONS_PTR_INTERNAL(z_keyexpr_t, z_owned_keyexpr_t, keyexpr, _z_keyexpr_free, _z_keyexpr_copy) OWNED_FUNCTIONS_PTR_INTERNAL(z_hello_t, z_owned_hello_t, hello, _z_hello_free, _z_owner_noop_copy) @@ -421,7 +444,6 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb z_free(wrapped_ctx); z_scouting_config_drop(config); - config->_value = NULL; } else { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; } @@ -440,19 +462,18 @@ z_owned_session_t z_open(z_owned_config_t *config) { } z_config_drop(config); - config->_value = NULL; } return zs; } int8_t z_close(z_owned_session_t *zs) { - _z_close(zs->_value); + int8_t ret = _Z_RES_OK; - z_session_drop(zs); - zs->_value = NULL; + _z_close(zs->_value); + _z_session_free(&zs->_value); - return 0; + return ret; } int8_t z_info_peers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) { @@ -635,12 +656,10 @@ z_owned_keyexpr_t z_declare_keyexpr(z_session_t zs, z_keyexpr_t keyexpr) { } int8_t z_undeclare_keyexpr(z_session_t zs, z_owned_keyexpr_t *keyexpr) { - int8_t ret = 0; + int8_t ret = _Z_RES_OK; ret = _z_undeclare_resource(zs._val, keyexpr->_value->_id); - z_keyexpr_drop(keyexpr); - keyexpr->_value = NULL; return ret; } @@ -677,12 +696,10 @@ z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, z_p } int8_t z_undeclare_publisher(z_owned_publisher_t *pub) { - int8_t ret = 0; + int8_t ret = _Z_RES_OK; ret = _z_undeclare_publisher(pub->_value); - - z_publisher_drop(pub); - pub->_value = NULL; + _z_publisher_free(&pub->_value); return ret; } @@ -691,11 +708,13 @@ z_publisher_put_options_t z_publisher_put_options_default(void) { return (z_publisher_put_options_t){.encoding = z_encoding_default()}; } -z_publisher_delete_options_t z_publisher_delete_options_default(void) { return (z_publisher_delete_options_t){.__dummy = 0}; } +z_publisher_delete_options_t z_publisher_delete_options_default(void) { + return (z_publisher_delete_options_t){.__dummy = 0}; +} int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t len, const z_publisher_put_options_t *options) { - int8_t ret = 0; + int8_t ret = _Z_RES_OK; z_publisher_put_options_t opt = z_publisher_put_options_default(); if (options != NULL) { @@ -775,21 +794,21 @@ z_owned_pull_subscriber_t z_declare_pull_subscriber(z_session_t zs, z_keyexpr_t } int8_t z_undeclare_subscriber(z_owned_subscriber_t *sub) { - _z_undeclare_subscriber(sub->_value); + int8_t ret = _Z_RES_OK; - z_subscriber_drop(sub); - sub->_value = NULL; + _z_undeclare_subscriber(sub->_value); + _z_subscriber_free(&sub->_value); - return 0; + return ret; } int8_t z_undeclare_pull_subscriber(z_owned_pull_subscriber_t *sub) { - _z_undeclare_subscriber(sub->_value); + int8_t ret = _Z_RES_OK; - z_pull_subscriber_drop(sub); - sub->_value = NULL; + _z_undeclare_subscriber(sub->_value); + _z_subscriber_free(&sub->_value); - return 0; + return ret; } int8_t z_subscriber_pull(const z_pull_subscriber_t sub) { return _z_subscriber_pull(sub._val); } @@ -820,12 +839,10 @@ z_owned_queryable_t z_declare_queryable(z_session_t zs, z_keyexpr_t keyexpr, z_o } int8_t z_undeclare_queryable(z_owned_queryable_t *queryable) { - int8_t ret = 0; + int8_t ret = _Z_RES_OK; ret = _z_undeclare_queryable(queryable->_value); - - z_queryable_drop(queryable); - queryable->_value = NULL; + _z_queryable_free(&queryable->_value); return ret; } @@ -855,7 +872,7 @@ z_value_t z_reply_err(const z_owned_reply_t *reply) { z_sample_t z_reply_ok(z_owned_reply_t *reply) { return reply->_value->data.sample; } /**************** Tasks ****************/ -zp_task_read_options_t zp_task_read_options_default(void) { return (zp_task_read_options_t){ .__dummy = 0 }; } +zp_task_read_options_t zp_task_read_options_default(void) { return (zp_task_read_options_t){.__dummy = 0}; } int8_t zp_start_read_task(z_session_t zs, const zp_task_read_options_t *options) { (void)(options); @@ -876,7 +893,7 @@ int8_t zp_stop_read_task(z_session_t zs) { #endif } -zp_task_lease_options_t zp_task_lease_options_default(void) { return (zp_task_lease_options_t){ .__dummy = 0 }; } +zp_task_lease_options_t zp_task_lease_options_default(void) { return (zp_task_lease_options_t){.__dummy = 0}; } int8_t zp_start_lease_task(z_session_t zs, const zp_task_lease_options_t *options) { (void)(options); @@ -897,21 +914,23 @@ int8_t zp_stop_lease_task(z_session_t zs) { #endif } -zp_read_options_t zp_read_options_default(void) { return (zp_read_options_t){ .__dummy = 0 }; } +zp_read_options_t zp_read_options_default(void) { return (zp_read_options_t){.__dummy = 0}; } int8_t zp_read(z_session_t zs, const zp_read_options_t *options) { (void)(options); return _zp_read(zs._val); } -zp_send_keep_alive_options_t zp_send_keep_alive_options_default(void) { return (zp_send_keep_alive_options_t){ .__dummy = 0 }; } +zp_send_keep_alive_options_t zp_send_keep_alive_options_default(void) { + return (zp_send_keep_alive_options_t){.__dummy = 0}; +} int8_t zp_send_keep_alive(z_session_t zs, const zp_send_keep_alive_options_t *options) { (void)(options); return _zp_send_keep_alive(zs._val); } -zp_send_join_options_t zp_send_join_options_default(void) { return (zp_send_join_options_t){ .__dummy = 0 }; } +zp_send_join_options_t zp_send_join_options_default(void) { return (zp_send_join_options_t){.__dummy = 0}; } int8_t zp_send_join(z_session_t zs, const zp_send_join_options_t *options) { (void)(options); From 370af849c724285b38affe4d8730f0a915d1f9e2 Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Mon, 27 Mar 2023 11:25:09 +0200 Subject: [PATCH 055/181] z_xxx_null functions added for all owned objects (#183) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Align drop macros with Zenoh-C * null funcs added, crashes on test * fixed crashes in null-drop test * emscripen build fix * added z_keyexpr_new and double_drop_test * Avoid early return on error and rely on single return point (MISRA) --------- Co-authored-by: Carlos Guimarães --- CMakeLists.txt | 7 ++ include/zenoh-pico/api/macros.h | 62 +++++++++--- include/zenoh-pico/api/primitives.h | 45 ++++----- src/api/api.c | 48 ++++++--- src/net/primitives.c | 85 +++++++++------- src/session/utils.c | 6 +- tests/z_api_double_drop_test.c | 57 +++++++++++ tests/z_api_null_drop_test.c | 151 ++++++++++++++++++++++++++++ 8 files changed, 367 insertions(+), 94 deletions(-) create mode 100644 tests/z_api_double_drop_test.c create mode 100644 tests/z_api_null_drop_test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a29d3ad4..015b3da34 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -250,6 +250,7 @@ if(BUILD_EXAMPLES) endif() endif() + if(UNIX OR MSVC) if(BUILD_TOOLS) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tools) @@ -265,12 +266,16 @@ if(UNIX OR MSVC) add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c) add_executable(z_msgcodec_test ${PROJECT_SOURCE_DIR}/tests/z_msgcodec_test.c) add_executable(z_keyexpr_test ${PROJECT_SOURCE_DIR}/tests/z_keyexpr_test.c) + add_executable(z_api_null_drop_test ${PROJECT_SOURCE_DIR}/tests/z_api_null_drop_test.c) + add_executable(z_api_double_drop_test ${PROJECT_SOURCE_DIR}/tests/z_api_double_drop_test.c) target_link_libraries(z_data_struct_test ${Libname}) target_link_libraries(z_endpoint_test ${Libname}) target_link_libraries(z_iobuf_test ${Libname}) target_link_libraries(z_msgcodec_test ${Libname}) target_link_libraries(z_keyexpr_test ${Libname}) + target_link_libraries(z_api_null_drop_test ${Libname}) + target_link_libraries(z_api_double_drop_test ${Libname}) enable_testing() add_test(z_data_struct_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_data_struct_test) @@ -278,6 +283,8 @@ if(UNIX OR MSVC) add_test(z_iobuf_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iobuf_test) add_test(z_msgcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_msgcodec_test) 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) endif() if(BUILD_MULTICAST) diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 61ee5030f..3c4cd409f 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -21,6 +21,8 @@ #ifndef __cplusplus +// clang-format off + /** * Defines a generic function for loaning any of the ``z_owned_X_t`` types. * @@ -49,20 +51,25 @@ * Parameters: * x: The instance to drop. */ -#define z_drop(x) \ - _Generic((*x), z_owned_keyexpr_t \ - : z_keyexpr_drop, z_owned_config_t \ - : z_config_drop, z_owned_scouting_config_t \ - : z_scouting_config_drop, z_owned_session_t \ - : z_session_drop, z_owned_subscriber_t \ - : z_subscriber_drop, z_owned_pull_subscriber_t \ - : z_pull_subscriber_drop, z_owned_publisher_t \ - : z_publisher_drop, z_owned_queryable_t \ - : z_queryable_drop, z_owned_reply_t \ - : z_reply_drop, z_owned_hello_t \ - : z_hello_drop, z_owned_str_t \ - : z_str_drop, z_owned_str_array_t \ - : z_str_array_drop)(x) +#define z_drop(x) _Generic((x), \ + z_owned_keyexpr_t * : z_keyexpr_drop, \ + z_owned_config_t * : z_config_drop, \ + z_owned_scouting_config_t * : z_scouting_config_drop, \ + z_owned_session_t * : z_session_drop, \ + z_owned_subscriber_t * : z_subscriber_drop, \ + z_owned_pull_subscriber_t * : z_pull_subscriber_drop, \ + z_owned_publisher_t * : z_publisher_drop, \ + z_owned_queryable_t * : z_queryable_drop, \ + z_owned_reply_t * : z_reply_drop, \ + z_owned_hello_t * : z_hello_drop, \ + z_owned_str_t * : z_str_drop, \ + z_owned_str_array_t * : z_str_array_drop, \ + z_owned_closure_sample_t * : z_closure_sample_drop, \ + z_owned_closure_query_t * : z_closure_query_drop, \ + 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 \ + )(x) /** * Defines a generic function for checking the validity of any of the ``z_owned_X_t`` types. @@ -142,6 +149,33 @@ : z_str_clone, z_owned_str_array_t \ : z_str_array_clone)(&x) +/** + * Defines a generic function for making null object of any of the ``z_owned_X_t`` types. + * + * Returns: + * Returns the unitialized instance of `x`. + */ +#define z_null(x) (*x = _Generic((x), \ + z_owned_session_t * : z_session_null, \ + z_owned_publisher_t * : z_publisher_null, \ + z_owned_keyexpr_t * : z_keyexpr_null, \ + z_owned_config_t * : z_config_null, \ + z_owned_scouting_config_t * : z_scouting_config_null, \ + z_owned_pull_subscriber_t * : z_pull_subscriber_null, \ + z_owned_subscriber_t * : z_subscriber_null, \ + z_owned_queryable_t * : z_queryable_null, \ + z_owned_reply_t * : z_reply_null, \ + z_owned_hello_t * : z_hello_null, \ + z_owned_str_t * : z_str_null, \ + z_owned_closure_sample_t * : z_closure_sample_null, \ + z_owned_closure_query_t * : z_closure_query_null, \ + z_owned_closure_reply_t * : z_closure_reply_null, \ + z_owned_closure_hello_t * : z_closure_hello_null, \ + z_owned_closure_zid_t * : z_closure_zid_null \ + )()) + +// clang-format on + #define _z_closure_overloader(callback, droper, ctx, ...) \ { .call = callback, .drop = droper, .context = ctx } diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 3815fa306..3a8ed557c 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -370,16 +370,6 @@ const char *zp_scouting_config_get(z_scouting_config_t config, uint8_t key); */ int8_t zp_scouting_config_insert(z_scouting_config_t config, uint8_t key, z_string_t value); -/** - * Constructs a gravestone value for hello, useful to steal one from a callback. - * This is useful when you wish to take ownership of a value from a callback to :c:func:`z_scout`: - * - * - copy the value of the callback's argument's pointee, - * - overwrite the pointee with this function's return value, - * - you are now responsible for dropping your copy of the hello. - */ -z_owned_hello_t z_hello_null(void); - /** * Constructs a :c:type:`z_encoding_t`. * @@ -493,17 +483,6 @@ z_value_t z_query_value(const z_query_t *query); */ z_keyexpr_t z_query_keyexpr(const z_query_t *query); -/** - * Returns an invalidated :c:type:`z_owned_reply_t`. - * - * This is useful when you wish to take ownership of a value from a callback to :c:func:`z_get`: - * - * - copy the value of the callback's argument's pointee, - * - overwrite the pointee with this function's return value, - * - you are now responsible for dropping your copy of the reply. - */ -z_owned_reply_t z_reply_null(void); - /** * Return a new sample closure. * It consists on a structure that contains all the elements for stateful, memory-leak-free callbacks. @@ -645,7 +624,8 @@ z_owned_closure_zid_t z_closure_zid(z_id_handler_t call, _z_dropper_handler_t dr type z_##name##_loan(const ownedtype *name); \ ownedtype *z_##name##_move(ownedtype *name); \ ownedtype z_##name##_clone(ownedtype *name); \ - void z_##name##_drop(ownedtype *name); + void z_##name##_drop(ownedtype *name); \ + ownedtype z_##name##_null(void); _OWNED_FUNCTIONS(z_str_t, z_owned_str_t, str) _OWNED_FUNCTIONS(z_keyexpr_t, z_owned_keyexpr_t, keyexpr) @@ -660,11 +640,17 @@ _OWNED_FUNCTIONS(z_hello_t, z_owned_hello_t, hello) _OWNED_FUNCTIONS(z_reply_t, z_owned_reply_t, reply) _OWNED_FUNCTIONS(z_str_array_t, z_owned_str_array_t, str_array) -z_owned_closure_sample_t *z_closure_sample_move(z_owned_closure_sample_t *closure_sample); -z_owned_closure_query_t *z_closure_query_move(z_owned_closure_query_t *closure_query); -z_owned_closure_reply_t *z_closure_reply_move(z_owned_closure_reply_t *closure_reply); -z_owned_closure_hello_t *z_closure_hello_move(z_owned_closure_hello_t *closure_hello); -z_owned_closure_zid_t *z_closure_zid_move(z_owned_closure_zid_t *closure_zid); +#define _OWNED_FUNCTIONS_CLOSURE(ownedtype, name) \ + _Bool z_##name##_check(const ownedtype *val); \ + ownedtype *z_##name##_move(ownedtype *val); \ + void z_##name##_drop(ownedtype *val); \ + ownedtype z_##name##_null(void); + +_OWNED_FUNCTIONS_CLOSURE(z_owned_closure_sample_t, closure_sample) +_OWNED_FUNCTIONS_CLOSURE(z_owned_closure_query_t, closure_query) +_OWNED_FUNCTIONS_CLOSURE(z_owned_closure_reply_t, closure_reply) +_OWNED_FUNCTIONS_CLOSURE(z_owned_closure_hello_t, closure_hello) +_OWNED_FUNCTIONS_CLOSURE(z_owned_closure_zid_t, closure_zid) /************* Primitives **************/ /** @@ -828,6 +814,11 @@ z_get_options_t z_get_options_default(void); int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owned_closure_reply_t *callback, const z_get_options_t *options); +/** + * Creates keyexpr owning string passed to it + */ +z_owned_keyexpr_t z_keyexpr_new(const char *name); + /** * Declares a keyexpr, so that it is internally mapped into into a numerical id. * diff --git a/src/api/api.c b/src/api/api.c index edbc94ada..be0751f4c 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -225,8 +225,6 @@ int8_t zp_scouting_config_insert(z_scouting_config_t sc, uint8_t key, z_string_t return _zp_config_insert(sc._val, key, value); } -z_owned_hello_t z_hello_null(void) { return (z_owned_hello_t){._value = NULL}; } - z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix) { return (_z_encoding_t){ .prefix = prefix, @@ -268,8 +266,6 @@ z_value_t z_query_value(const z_query_t *query) { return query->_with_value; } z_keyexpr_t z_query_keyexpr(const z_query_t *query) { return query->_key; } -z_owned_reply_t z_reply_null(void) { return (z_owned_reply_t){._value = NULL}; } - _Bool z_value_is_initialized(z_value_t *value) { _Bool ret = false; @@ -284,6 +280,7 @@ _Bool z_value_is_initialized(z_value_t *value) { #define OWNED_FUNCTIONS_PTR_INTERNAL(type, ownedtype, name, f_free, f_copy) \ _Bool z_##name##_check(const ownedtype *val) { return val->_value != NULL; } \ type z_##name##_loan(const ownedtype *val) { return *val->_value; } \ + ownedtype z_##name##_null(void) { return (ownedtype){._value = NULL}; } \ ownedtype *z_##name##_move(ownedtype *val) { return val; } \ ownedtype z_##name##_clone(ownedtype *val) { \ ownedtype ret; \ @@ -302,6 +299,7 @@ _Bool z_value_is_initialized(z_value_t *value) { #define OWNED_FUNCTIONS_PTR_COMMON(type, ownedtype, name) \ _Bool z_##name##_check(const ownedtype *val) { return val->_value != NULL; } \ type z_##name##_loan(const ownedtype *val) { return (type){._val = val->_value}; } \ + ownedtype z_##name##_null(void) { return (ownedtype){._value = NULL}; } \ ownedtype *z_##name##_move(ownedtype *val) { return val; } #define OWNED_FUNCTIONS_PTR_CLONE(type, ownedtype, name, f_copy) \ @@ -324,6 +322,7 @@ _Bool z_value_is_initialized(z_value_t *value) { #define OWNED_FUNCTIONS_STR(type, ownedtype, name, f_free, f_copy) \ _Bool z_##name##_check(const ownedtype *val) { return val->_value != NULL; } \ type z_##name##_loan(const ownedtype *val) { return val->_value; } \ + ownedtype z_##name##_null(void) { return (ownedtype){._value = NULL}; }; \ ownedtype *z_##name##_move(ownedtype *val) { return val; } \ ownedtype z_##name##_clone(ownedtype *val) { \ ownedtype ret; \ @@ -380,6 +379,22 @@ OWNED_FUNCTIONS_PTR_INTERNAL(z_hello_t, z_owned_hello_t, hello, _z_hello_free, _ OWNED_FUNCTIONS_PTR_INTERNAL(z_reply_t, z_owned_reply_t, reply, _z_reply_free, _z_owner_noop_copy) OWNED_FUNCTIONS_PTR_INTERNAL(z_str_array_t, z_owned_str_array_t, str_array, _z_str_array_free, _z_owner_noop_copy) +#define OWNED_FUNCTIONS_CLOSURE(ownedtype, name) \ + _Bool z_##name##_check(const ownedtype *val) { return val->call != NULL; } \ + ownedtype *z_##name##_move(ownedtype *val) { return val; } \ + void z_##name##_drop(ownedtype *val) { \ + if (val->drop != NULL) { \ + (val->drop)(val->context); \ + val->drop = NULL; \ + } \ + val->call = NULL; \ + val->context = NULL; \ + } \ + ownedtype z_##name##_null(void) { \ + ownedtype v = {.call = NULL, .drop = NULL, .context = NULL}; \ + return v; \ + } + z_owned_closure_sample_t z_closure_sample(_z_data_handler_t call, _z_dropper_handler_t drop, void *context) { return (z_owned_closure_sample_t){.call = call, .drop = drop, .context = context}; } @@ -400,15 +415,11 @@ z_owned_closure_zid_t z_closure_zid(z_id_handler_t call, _z_dropper_handler_t dr return (z_owned_closure_zid_t){.call = call, .drop = drop, .context = context}; } -z_owned_closure_sample_t *z_closure_sample_move(z_owned_closure_sample_t *closure_sample) { return closure_sample; } - -z_owned_closure_query_t *z_closure_query_move(z_owned_closure_query_t *closure_query) { return closure_query; } - -z_owned_closure_reply_t *z_closure_reply_move(z_owned_closure_reply_t *closure_reply) { return closure_reply; } - -z_owned_closure_hello_t *z_closure_hello_move(z_owned_closure_hello_t *closure_hello) { return closure_hello; } - -z_owned_closure_zid_t *z_closure_zid_move(z_owned_closure_zid_t *closure_zid) { return closure_zid; } +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_sample_t, closure_sample) +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_query_t, closure_query) +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_reply_t, closure_reply) +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_hello_t, closure_hello) +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_zid_t, closure_zid) /************* Primitives **************/ typedef struct __z_hello_handler_wrapper_t { @@ -643,6 +654,17 @@ int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owne return ret; } +z_owned_keyexpr_t z_keyexpr_new(const char *name) { + z_owned_keyexpr_t key; + + key._value = name ? (z_keyexpr_t *)z_malloc(sizeof(z_keyexpr_t)) : NULL; + if (key._value != NULL) { + *key._value = _z_rid_with_suffix(Z_RESOURCE_ID_NONE, name); + } + + return key; +} + z_owned_keyexpr_t z_declare_keyexpr(z_session_t zs, z_keyexpr_t keyexpr) { z_owned_keyexpr_t key; diff --git a/src/net/primitives.c b/src/net/primitives.c index 9d0deaf52..a6019e9bb 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -120,16 +120,20 @@ _z_publisher_t *_z_declare_publisher(_z_session_t *zn, _z_keyexpr_t keyexpr, z_c } int8_t _z_undeclare_publisher(_z_publisher_t *pub) { - int8_t ret = _Z_RES_OK; + int8_t ret = _Z_ERR_GENERIC; - // Build the declare message to send on the wire - _z_declaration_array_t declarations = _z_declaration_array_make(1); - declarations._val[0] = _z_msg_make_declaration_forget_publisher(_z_keyexpr_duplicate(&pub->_key)); - _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); - if (_z_send_z_msg(pub->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { - ret = _Z_ERR_TRANSPORT_TX_FAILED; + if (pub != NULL) { + ret = _Z_RES_OK; + + // Build the declare message to send on the wire + _z_declaration_array_t declarations = _z_declaration_array_make(1); + declarations._val[0] = _z_msg_make_declaration_forget_publisher(_z_keyexpr_duplicate(&pub->_key)); + _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); + if (_z_send_z_msg(pub->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + ret = _Z_ERR_TRANSPORT_TX_FAILED; + } + _z_msg_clear(&z_msg); } - _z_msg_clear(&z_msg); return ret; } @@ -175,25 +179,28 @@ _z_subscriber_t *_z_declare_subscriber(_z_session_t *zn, _z_keyexpr_t keyexpr, _ } int8_t _z_undeclare_subscriber(_z_subscriber_t *sub) { - int8_t ret = _Z_RES_OK; + int8_t ret = _Z_ERR_GENERIC; - _z_subscription_sptr_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_id); - if (s != NULL) { - // Build the declare message to send on the wire - _z_declaration_array_t declarations = _z_declaration_array_make(1); - declarations._val[0] = _z_msg_make_declaration_forget_subscriber(_z_keyexpr_duplicate(&s->ptr->_key)); - _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); - if (_z_send_z_msg(sub->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { - // Only if message is successfully send, local subscription state can be removed - _z_unregister_subscription(sub->_zn, _Z_RESOURCE_IS_LOCAL, s); + if (sub != NULL) { + ret = _Z_RES_OK; + + _z_subscription_sptr_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_id); + if (s != NULL) { + // Build the declare message to send on the wire + _z_declaration_array_t declarations = _z_declaration_array_make(1); + declarations._val[0] = _z_msg_make_declaration_forget_subscriber(_z_keyexpr_duplicate(&s->ptr->_key)); + _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); + if (_z_send_z_msg(sub->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { + // Only if message is successfully send, local subscription state can be removed + _z_unregister_subscription(sub->_zn, _Z_RESOURCE_IS_LOCAL, s); + } else { + ret = _Z_ERR_TRANSPORT_TX_FAILED; + } + _z_msg_clear(&z_msg); } else { - ret = _Z_ERR_TRANSPORT_TX_FAILED; + ret = _Z_ERR_ENTITY_UNKNOWN; } - _z_msg_clear(&z_msg); - } else { - ret = _Z_ERR_ENTITY_UNKNOWN; } - return ret; } @@ -239,24 +246,26 @@ _z_queryable_t *_z_declare_queryable(_z_session_t *zn, _z_keyexpr_t keyexpr, _Bo } int8_t _z_undeclare_queryable(_z_queryable_t *qle) { - int8_t ret = _Z_RES_OK; + int8_t ret = _Z_ERR_GENERIC; - _z_questionable_sptr_t *q = _z_get_questionable_by_id(qle->_zn, qle->_id); - if (q != NULL) { - // Build the declare message to send on the wire - _z_declaration_array_t declarations = _z_declaration_array_make(1); - declarations._val[0] = _z_msg_make_declaration_forget_queryable(_z_keyexpr_duplicate(&q->ptr->_key)); - _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); - if (_z_send_z_msg(qle->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { - // Only if message is successfully send, local queryable state can be removed - _z_unregister_questionable(qle->_zn, q); + if (qle != NULL) { + _z_questionable_sptr_t *q = _z_get_questionable_by_id(qle->_zn, qle->_id); + if (q != NULL) { + ret = _Z_RES_OK; + // Build the declare message to send on the wire + _z_declaration_array_t declarations = _z_declaration_array_make(1); + declarations._val[0] = _z_msg_make_declaration_forget_queryable(_z_keyexpr_duplicate(&q->ptr->_key)); + _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); + if (_z_send_z_msg(qle->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { + // Only if message is successfully send, local queryable state can be removed + _z_unregister_questionable(qle->_zn, q); + } else { + ret = _Z_ERR_TRANSPORT_TX_FAILED; + } + _z_msg_clear(&z_msg); } else { - ret = _Z_ERR_TRANSPORT_TX_FAILED; + ret = _Z_ERR_ENTITY_UNKNOWN; } - _z_msg_clear(&z_msg); - - } else { - ret = _Z_ERR_ENTITY_UNKNOWN; } return ret; diff --git a/src/session/utils.c b/src/session/utils.c index 341dfbe2c..1dd417233 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -134,9 +134,11 @@ void _z_session_free(_z_session_t **zn) { } int8_t _z_session_close(_z_session_t *zn, uint8_t reason) { - int8_t ret = _Z_RES_OK; + int8_t ret = _Z_ERR_GENERIC; - ret = _z_transport_close(&zn->_tp, reason); + if (zn != NULL) { + ret = _z_transport_close(&zn->_tp, reason); + } return ret; } diff --git a/tests/z_api_double_drop_test.c b/tests/z_api_double_drop_test.c new file mode 100644 index 000000000..776848b2a --- /dev/null +++ b/tests/z_api_double_drop_test.c @@ -0,0 +1,57 @@ +// +// 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, + +#include +#include +#include + +#include "zenoh-pico.h" + +#undef NDEBUG +#include + +#define URL "demo/example" + +void test_keyexpr(void) { + z_owned_keyexpr_t keyexpr = z_keyexpr_new(URL); + assert(z_check(keyexpr)); + z_drop(z_move(keyexpr)); + assert(!z_check(keyexpr)); + z_drop(z_move(keyexpr)); + assert(!z_check(keyexpr)); +} + +void test_config(void) { + z_owned_config_t config = z_config_default(); + assert(z_check(config)); + z_drop(z_move(config)); + assert(!z_check(config)); + z_drop(z_move(config)); + assert(!z_check(config)); +} + +void test_scouting_config(void) { + z_owned_scouting_config_t config = z_scouting_config_default(); + assert(z_check(config)); + z_drop(z_move(config)); + assert(!z_check(config)); + z_drop(z_move(config)); + assert(!z_check(config)); +} + +int main(void) { + test_keyexpr(); + test_config(); + test_scouting_config(); + return 0; +} diff --git a/tests/z_api_null_drop_test.c b/tests/z_api_null_drop_test.c new file mode 100644 index 000000000..f5707167c --- /dev/null +++ b/tests/z_api_null_drop_test.c @@ -0,0 +1,151 @@ +// +// 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, + +#include +#include +#include + +#undef NDEBUG +#include + +#include "zenoh-pico.h" + +int main(void) { + // + // Check that all null functions exists + // + z_owned_session_t session_null_1 = z_session_null(); + z_owned_publisher_t publisher_null_1 = z_publisher_null(); + z_owned_keyexpr_t keyexpr_null_1 = z_keyexpr_null(); + z_owned_config_t config_null_1 = z_config_null(); + z_owned_scouting_config_t scouting_config_null_1 = z_scouting_config_null(); + z_owned_pull_subscriber_t pull_subscriber_null_1 = z_pull_subscriber_null(); + z_owned_subscriber_t subscriber_null_1 = z_subscriber_null(); + z_owned_queryable_t queryable_null_1 = z_queryable_null(); + z_owned_reply_t reply_null_1 = z_reply_null(); + z_owned_hello_t hello_null_1 = z_hello_null(); + z_owned_closure_sample_t closure_sample_null_1 = z_closure_sample_null(); + z_owned_closure_query_t closure_query_null_1 = z_closure_query_null(); + z_owned_closure_reply_t closure_reply_null_1 = z_closure_reply_null(); + z_owned_closure_hello_t closure_hello_null_1 = z_closure_hello_null(); + z_owned_closure_zid_t closure_zid_null_1 = z_closure_zid_null(); + z_owned_str_t str_null_1 = z_str_null(); + + // + // Test that they actually make invalid value (where applicable) + // + assert(!z_check(session_null_1)); + assert(!z_check(publisher_null_1)); + assert(!z_check(keyexpr_null_1)); + assert(!z_check(config_null_1)); + assert(!z_check(scouting_config_null_1)); + assert(!z_check(pull_subscriber_null_1)); + assert(!z_check(subscriber_null_1)); + assert(!z_check(queryable_null_1)); + assert(!z_check(reply_null_1)); + assert(!z_check(hello_null_1)); + assert(!z_check(str_null_1)); + + // + // Test that z_null macro defined for all types + // + z_owned_session_t session_null_2; + z_owned_publisher_t publisher_null_2; + z_owned_keyexpr_t keyexpr_null_2; + z_owned_config_t config_null_2; + z_owned_scouting_config_t scouting_config_null_2; + z_owned_pull_subscriber_t pull_subscriber_null_2; + z_owned_subscriber_t subscriber_null_2; + z_owned_queryable_t queryable_null_2; + z_owned_reply_t reply_null_2; + z_owned_hello_t hello_null_2; + z_owned_closure_sample_t closure_sample_null_2; + z_owned_closure_query_t closure_query_null_2; + z_owned_closure_reply_t closure_reply_null_2; + z_owned_closure_hello_t closure_hello_null_2; + z_owned_closure_zid_t closure_zid_null_2; + z_owned_str_t str_null_2; + + z_null(&session_null_2); + z_null(&publisher_null_2); + z_null(&keyexpr_null_2); + z_null(&config_null_2); + z_null(&scouting_config_null_2); + z_null(&pull_subscriber_null_2); + z_null(&subscriber_null_2); + z_null(&queryable_null_2); + z_null(&reply_null_2); + z_null(&hello_null_2); + z_null(&closure_sample_null_2); + z_null(&closure_query_null_2); + z_null(&closure_reply_null_2); + z_null(&closure_hello_null_2); + z_null(&closure_zid_null_2); + z_null(&str_null_2); + + // + // Test that null macro works the same as direct call + // + assert(!z_check(session_null_2)); + assert(!z_check(publisher_null_2)); + assert(!z_check(keyexpr_null_2)); + assert(!z_check(config_null_2)); + assert(!z_check(scouting_config_null_2)); + assert(!z_check(pull_subscriber_null_2)); + assert(!z_check(subscriber_null_2)); + assert(!z_check(queryable_null_2)); + assert(!z_check(reply_null_2)); + assert(!z_check(hello_null_2)); + assert(!z_check(str_null_2)); + + // + // Test drop null and double drop it + // + for (int i = 0; i < 2; ++i) { + z_drop(z_move(session_null_1)); + z_drop(z_move(publisher_null_1)); + z_drop(z_move(keyexpr_null_1)); + z_drop(z_move(config_null_1)); + z_drop(z_move(scouting_config_null_1)); + z_drop(z_move(pull_subscriber_null_1)); + z_drop(z_move(subscriber_null_1)); + z_drop(z_move(queryable_null_1)); + z_drop(z_move(reply_null_1)); + z_drop(z_move(hello_null_1)); + z_drop(z_move(closure_sample_null_1)); + z_drop(z_move(closure_query_null_1)); + z_drop(z_move(closure_reply_null_1)); + z_drop(z_move(closure_hello_null_1)); + z_drop(z_move(closure_zid_null_1)); + z_drop(z_move(str_null_1)); + + z_drop(z_move(session_null_2)); + z_drop(z_move(publisher_null_2)); + z_drop(z_move(keyexpr_null_2)); + z_drop(z_move(config_null_2)); + z_drop(z_move(scouting_config_null_2)); + z_drop(z_move(pull_subscriber_null_2)); + z_drop(z_move(subscriber_null_2)); + z_drop(z_move(queryable_null_2)); + z_drop(z_move(reply_null_2)); + z_drop(z_move(hello_null_2)); + z_drop(z_move(closure_sample_null_2)); + z_drop(z_move(closure_query_null_2)); + z_drop(z_move(closure_reply_null_2)); + z_drop(z_move(closure_hello_null_2)); + z_drop(z_move(closure_zid_null_2)); + z_drop(z_move(str_null_2)); + } + + return 0; +} From 1a6eace71a85f9e62c4ad100137deab58218734a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Mon, 27 Mar 2023 15:03:45 +0200 Subject: [PATCH 056/181] Default resolution size is 2 --- include/zenoh-pico/config.h | 5 ----- include/zenoh-pico/protocol/msg.h | 3 +-- src/protocol/msg.c | 18 +++++++++--------- src/protocol/msgcodec.c | 12 ++++++------ 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index 6a3c6f7fe..09c916884 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -151,11 +151,6 @@ #define Z_CONFIG_SOCKET_TIMEOUT 100 #endif -/** - * The default sequence number resolution takes 4 bytes on the wire. - * Given the VLE encoding of ZInt, 4 bytes result in 28 useful bits. - * 2^28 = 268_435_456 => Max Seq Num = 268_435_455 - */ #ifndef Z_SN_RESOLUTION #define Z_SN_RESOLUTION 0x02 #endif diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 7288b957f..73655a5aa 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -26,10 +26,9 @@ #include "zenoh-pico/protocol/ext.h" #define _Z_DEFAULT_BATCH_SIZE 65535 -#define _Z_DEFAULT_SIZET_SIZE 3 // 0x80 << _Z_DEFAULT_SIZET_SIZE. 3 means size_t = 64 bits +#define _Z_DEFAULT_RESOLUTION_SIZE 2 #define _Z_DECLARE_CLEAR(layer, name) void _z_##layer##_msg_clear_##name(_z_##name##_t *m, uint8_t header) - #define _Z_DECLARE_CLEAR_NOH(layer, name) void _z_##layer##_msg_clear_##name(_z_##name##_t *m) // NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 395970ff9..aaa79ca88 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -535,9 +535,9 @@ _z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, } if ((msg._body._join._batch_size != _Z_DEFAULT_BATCH_SIZE) || - (msg._body._join._seq_num_res != _Z_DEFAULT_SIZET_SIZE) || - (msg._body._join._key_id_res != _Z_DEFAULT_SIZET_SIZE) || - (msg._body._join._req_id_res != _Z_DEFAULT_SIZET_SIZE)) { + (msg._body._join._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || + (msg._body._join._key_id_res != _Z_DEFAULT_RESOLUTION_SIZE) || + (msg._body._join._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { _Z_SET_FLAG(msg._header, _Z_FLAG_JOIN_S); } @@ -576,9 +576,9 @@ _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_bytes_t zi _z_bytes_reset(&msg._body._init._cookie); if ((msg._body._init._batch_size != _Z_DEFAULT_BATCH_SIZE) || - (msg._body._init._seq_num_res != _Z_DEFAULT_SIZET_SIZE) || - (msg._body._init._key_id_res != _Z_DEFAULT_SIZET_SIZE) || - (msg._body._init._req_id_res != _Z_DEFAULT_SIZET_SIZE)) { + (msg._body._init._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || + (msg._body._init._key_id_res != _Z_DEFAULT_RESOLUTION_SIZE) || + (msg._body._init._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { _Z_SET_FLAG(msg._header, _Z_FLAG_INIT_S); } @@ -603,9 +603,9 @@ _z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_bytes_t zi msg._body._init._cookie = cookie; if ((msg._body._init._batch_size != _Z_DEFAULT_BATCH_SIZE) || - (msg._body._init._seq_num_res != _Z_DEFAULT_SIZET_SIZE) || - (msg._body._init._key_id_res != _Z_DEFAULT_SIZET_SIZE) || - (msg._body._init._req_id_res != _Z_DEFAULT_SIZET_SIZE)) { + (msg._body._init._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || + (msg._body._init._key_id_res != _Z_DEFAULT_RESOLUTION_SIZE) || + (msg._body._init._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { _Z_SET_FLAG(msg._header, _Z_FLAG_INIT_S); } diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 86e644db0..7bbf0aa5c 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1159,9 +1159,9 @@ int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { msg->_key_id_res = ((cbyte >> 4) & 0x03); ret |= _z_uint16_decode(&msg->_batch_size, zbf); } else { - msg->_seq_num_res = _Z_DEFAULT_SIZET_SIZE; - msg->_req_id_res = _Z_DEFAULT_SIZET_SIZE; - msg->_key_id_res = _Z_DEFAULT_SIZET_SIZE; + msg->_seq_num_res = _Z_DEFAULT_RESOLUTION_SIZE; + msg->_req_id_res = _Z_DEFAULT_RESOLUTION_SIZE; + msg->_key_id_res = _Z_DEFAULT_RESOLUTION_SIZE; msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; } @@ -1244,9 +1244,9 @@ int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { msg->_key_id_res = ((cbyte >> 4) & 0x03); ret |= _z_uint16_decode(&msg->_batch_size, zbf); } else { - msg->_seq_num_res = _Z_DEFAULT_SIZET_SIZE; - msg->_req_id_res = _Z_DEFAULT_SIZET_SIZE; - msg->_key_id_res = _Z_DEFAULT_SIZET_SIZE; + msg->_seq_num_res = _Z_DEFAULT_RESOLUTION_SIZE; + msg->_req_id_res = _Z_DEFAULT_RESOLUTION_SIZE; + msg->_key_id_res = _Z_DEFAULT_RESOLUTION_SIZE; msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; } From ea39f9f935db4358ded76823db1dfb8964487e5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Mon, 27 Mar 2023 16:46:33 +0200 Subject: [PATCH 057/181] Support for mandatory and optional extensions --- include/zenoh-pico/protocol/ext.h | 7 +++-- include/zenoh-pico/utils/result.h | 1 + src/protocol/ext.c | 6 ++-- src/protocol/extcodec.c | 50 +++++++++++++++++++++---------- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/include/zenoh-pico/protocol/ext.h b/include/zenoh-pico/protocol/ext.h index 3538267a6..1fe5620d7 100644 --- a/include/zenoh-pico/protocol/ext.h +++ b/include/zenoh-pico/protocol/ext.h @@ -22,12 +22,14 @@ /*=============================*/ /* Message header */ /*=============================*/ -#define _Z_EXT_ID_MASK 0x1F +#define _Z_EXT_FULL_ID_MASK 0x7F +#define _Z_EXT_ID_MASK 0x0F #define _Z_EXT_ENC_MASK 0x60 /*=============================*/ /* Message helpers */ /*=============================*/ +#define _Z_EXT_FULL_ID(h) (_Z_EXT_FULL_ID_MASK & h) #define _Z_EXT_ID(h) (_Z_EXT_ID_MASK & h) #define _Z_EXT_ENC(h) (_Z_EXT_ENC_MASK & h) #define _Z_EXT_HAS_FLAG(h, f) ((h & f) != 0) @@ -36,7 +38,7 @@ /*=============================*/ /* Extension IDs */ /*=============================*/ -#define _Z_MSG_EXT_ID_EXT1 0x01 +// #define _Z_MSG_EXT_ID_FOO 0x00 // Hex(ENC|M|ID) /*=============================*/ /* Extension Encodings */ @@ -48,6 +50,7 @@ /*=============================*/ /* Extension flags */ /*=============================*/ +#define _Z_MSG_EXT_FLAG_M 0x10 #define _Z_MSG_EXT_FLAG_Z 0x80 typedef struct { diff --git a/include/zenoh-pico/utils/result.h b/include/zenoh-pico/utils/result.h index 9118e0a34..761bc6d6a 100644 --- a/include/zenoh-pico/utils/result.h +++ b/include/zenoh-pico/utils/result.h @@ -34,6 +34,7 @@ typedef enum { _Z_ERR_MESSAGE_ZENOH_DECLARATION_UNKNOWN = -115, _Z_ERR_MESSAGE_ZENOH_UNKNOWN = -114, _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN = -113, + _Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN = -112, _Z_ERR_ENTITY_DECLARATION_FAILED = -111, _Z_ERR_ENTITY_UNKNOWN = -110, diff --git a/src/protocol/ext.c b/src/protocol/ext.c index e79932592..6202c11df 100644 --- a/src/protocol/ext.c +++ b/src/protocol/ext.c @@ -23,7 +23,7 @@ _z_msg_ext_t _z_msg_ext_make_unit(uint8_t id) { _z_msg_ext_t ext; ext._header = 0; - ext._header |= (id & 0x1F); + ext._header |= (id & _Z_EXT_ID_MASK); ext._header |= _Z_MSG_EXT_ENC_UNIT; return ext; @@ -40,7 +40,7 @@ _z_msg_ext_t _z_msg_ext_make_zint(uint8_t id, _z_zint_t zid) { _z_msg_ext_t ext; ext._header = 0; - ext._header |= (id & 0x1F); + ext._header |= (id & _Z_EXT_ID_MASK); ext._header |= _Z_MSG_EXT_ENC_ZINT; ext._body._zint._val = zid; @@ -56,7 +56,7 @@ _z_msg_ext_t _z_msg_ext_make_zbuf(uint8_t id, _z_bytes_t zbuf) { _z_msg_ext_t ext; ext._header = 0; - ext._header |= (id & 0x1F); + ext._header |= (id & _Z_EXT_ID_MASK); ext._header |= _Z_MSG_EXT_ENC_ZBUF; _z_bytes_move(&ext._body._zbuf._val, &zbuf); diff --git a/src/protocol/extcodec.c b/src/protocol/extcodec.c index b605aaef1..8a087639c 100644 --- a/src/protocol/extcodec.c +++ b/src/protocol/extcodec.c @@ -91,27 +91,47 @@ int8_t _z_msg_ext_encode(_z_wbuf_t *wbf, const _z_msg_ext_t *ext) { return ret; } -int8_t _z_msg_ext_decode(_z_msg_ext_t *ext, _z_zbuf_t *zbf) { +int8_t _z_msg_ext_unknown_body_decode(_z_msg_ext_body_t *body, uint8_t enc, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; - ret |= _z_uint8_decode(&ext->_header, zbf); // Decode the header - if (ret == _Z_RES_OK) { - uint8_t enc = _Z_EXT_ENC(ext->_header); - switch (enc) { - case _Z_MSG_EXT_ENC_UNIT: { - ret |= _z_msg_ext_decode_unit(&ext->_body._unit, zbf); - } break; + switch (enc) { + case _Z_MSG_EXT_ENC_UNIT: { + ret |= _z_msg_ext_decode_unit(&body->_unit, zbf); + } break; - case _Z_MSG_EXT_ENC_ZINT: { - ret |= _z_msg_ext_decode_zint(&ext->_body._zint, zbf); - } break; + case _Z_MSG_EXT_ENC_ZINT: { + ret |= _z_msg_ext_decode_zint(&body->_zint, zbf); + } break; - case _Z_MSG_EXT_ENC_ZBUF: { - ret |= _z_msg_ext_decode_zbuf(&ext->_body._zbuf, zbf); - } break; + case _Z_MSG_EXT_ENC_ZBUF: { + ret |= _z_msg_ext_decode_zbuf(&body->_zbuf, zbf); + } break; + default: { + _Z_DEBUG("WARNING: Trying to copy message extension with unknown encoding(%d)\n", enc); + } break; + } + + return ret; +} + +int8_t _z_msg_ext_decode(_z_msg_ext_t *ext, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + + ret |= _z_uint8_decode(&ext->_header, zbf); // Decode the header + if (ret == _Z_RES_OK) { + uint8_t id = _Z_EXT_FULL_ID(ext->_header); + switch (id) { + // case _Z_MSG_EXT_ID_FOO: { + // ret |= _z_msg_ext_decode_foo(&ext->_body, zbf); + // } break; default: { - _Z_DEBUG("WARNING: Trying to copy message extension with unknown encoding(%d)\n", enc); + _Z_DEBUG("WARNING: Trying to decode message extension with unknown id(%d)\n", id); + if (_Z_EXT_HAS_FLAG(ext->_header, _Z_MSG_EXT_FLAG_M) == false) { + ret |= _z_msg_ext_unknown_body_decode(&ext->_body, _Z_EXT_ENC(ext->_header), zbf); + } else { + ret |= _Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN; + } } break; } } From 9d14efacac6f7d443a1cced0b81b4c2dba3ca3d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Mon, 27 Mar 2023 17:25:35 +0200 Subject: [PATCH 058/181] Fix extensions placement in frame and fragments --- include/zenoh-pico/protocol/msgcodec.h | 24 ++- src/protocol/msgcodec.c | 217 +++++++++++++++++-------- tests/z_msgcodec_test.c | 16 +- 3 files changed, 175 insertions(+), 82 deletions(-) diff --git a/include/zenoh-pico/protocol/msgcodec.h b/include/zenoh-pico/protocol/msgcodec.h index dc5aaf381..b405dba6b 100644 --- a/include/zenoh-pico/protocol/msgcodec.h +++ b/include/zenoh-pico/protocol/msgcodec.h @@ -159,13 +159,23 @@ int8_t _z_keep_alive_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_keep_ int8_t _z_keep_alive_decode_na(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg); -int8_t _z_frame_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg); -int8_t _z_fragment_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_frame_header_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg); +int8_t _z_frame_payload_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg); +int8_t _z_frame_header_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_frame_payload_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_frame_header_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_frame_payload_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_fragment_header_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg); +int8_t _z_fragment_payload_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg); +int8_t _z_fragment_header_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_fragment_payload_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_fragment_header_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_fragment_payload_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_extensions_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_ext_vec_t *v_ext); +int8_t _z_extensions_decode_na(_z_msg_ext_vec_t *v_ext, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_extensions_decode(_z_msg_ext_vec_t *v_ext, _z_zbuf_t *zbf, uint8_t header); // ------------------ Discovery Message ------------------ int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_scout_t *msg); diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 7bbf0aa5c..2c3359917 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1366,14 +1366,23 @@ int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t } /*------------------ Frame Message ------------------*/ -int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg) { +int8_t _z_frame_header_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg) { (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_FRAME\n"); + _Z_DEBUG("Encoding _Z_MID_FRAME_HEADER\n"); _Z_EC(_z_zint_encode(wbf, msg->_sn)) + return ret; +} + +int8_t _z_frame_payload_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg) { + (void)(header); + + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_FRAME_PAYLOAD\n"); + size_t len = _z_zenoh_message_vec_len(&msg->_messages); for (size_t i = 0; i < len; i++) { _Z_EC(_z_zenoh_message_encode(wbf, _z_zenoh_message_vec_get(&msg->_messages, i))) @@ -1382,83 +1391,155 @@ int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *m return ret; } -int8_t _z_frame_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_frame_header_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_FRAME\n"); + _Z_DEBUG("Decoding _Z_MID_FRAME_HEADER\n"); ret |= _z_zint_decode(&msg->_sn, zbf); - if (ret == _Z_RES_OK) { - msg->_messages = _z_zenoh_message_vec_make(_ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE); - while (_z_zbuf_len(zbf) > 0) { - // Mark the reading position of the iobfer - size_t r_pos = _z_zbuf_get_rpos(zbf); - _z_zenoh_message_t *zm = (_z_zenoh_message_t *)z_malloc(sizeof(_z_zenoh_message_t)); - ret |= _z_zenoh_message_decode(zm, zbf); - if (ret == _Z_RES_OK) { - _z_zenoh_message_vec_append(&msg->_messages, zm); - } else { - _z_msg_free(&zm); - - _z_zbuf_set_rpos(zbf, r_pos); // Restore the reading position of the iobfer - - // FIXME: Check for the return error, since not all of them means a decoding error - // in this particular case. As of now, we roll-back the reading position - // and return to the Zenoh transport-level decoder. - // https://github.com/eclipse-zenoh/zenoh-pico/pull/132#discussion_r1045593602 - if ((ret & _Z_ERR_MESSAGE_ZENOH_UNKNOWN) == _Z_ERR_MESSAGE_ZENOH_UNKNOWN) { - ret = _Z_RES_OK; - } - break; + + return ret; +} + +int8_t _z_frame_payload_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { + (void)(header); + + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Decoding _Z_MID_FRAME_PAYLOAD\n"); + + msg->_messages = _z_zenoh_message_vec_make(_ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE); + while (_z_zbuf_len(zbf) > 0) { + // Mark the reading position of the iobfer + size_t r_pos = _z_zbuf_get_rpos(zbf); + _z_zenoh_message_t *zm = (_z_zenoh_message_t *)z_malloc(sizeof(_z_zenoh_message_t)); + ret |= _z_zenoh_message_decode(zm, zbf); + if (ret == _Z_RES_OK) { + _z_zenoh_message_vec_append(&msg->_messages, zm); + } else { + _z_msg_free(&zm); + + _z_zbuf_set_rpos(zbf, r_pos); // Restore the reading position of the iobfer + + // FIXME: Check for the return error, since not all of them means a decoding error + // in this particular case. As of now, we roll-back the reading position + // and return to the Zenoh transport-level decoder. + // https://github.com/eclipse-zenoh/zenoh-pico/pull/132#discussion_r1045593602 + if ((ret & _Z_ERR_MESSAGE_ZENOH_UNKNOWN) == _Z_ERR_MESSAGE_ZENOH_UNKNOWN) { + ret = _Z_RES_OK; } + break; } - } else { - msg->_messages = _z_zenoh_message_vec_make(0); } return ret; } -int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_frame_decode_na(msg, zbf, header); +int8_t _z_frame_header_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_frame_header_decode_na(msg, zbf, header); +} + +int8_t _z_frame_payload_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_frame_payload_decode_na(msg, zbf, header); } /*------------------ Fragment Message ------------------*/ -int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg) { +int8_t _z_fragment_header_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg) { (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_FRAGMENT\n"); + _Z_DEBUG("Encoding _Z_MID_FRAGMENT_HEADER\n"); _Z_EC(_z_zint_encode(wbf, msg->_sn)) + return ret; +} + +int8_t _z_fragment_payload_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg) { + (void)(header); + + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_FRAGMENT_PAYLOAD\n"); + _Z_EC(_z_bytes_encode(wbf, &msg->_payload)) return ret; } -int8_t _z_fragment_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_fragment_header_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_FRAME\n"); + _Z_DEBUG("Decoding _Z_MID_FRAGMENT_HEADER\n"); ret |= _z_zint_decode(&msg->_sn, zbf); - if (ret == _Z_RES_OK) { - ret |= _z_bytes_decode(&msg->_payload, zbf); - if (ret != _Z_RES_OK) { - msg->_payload = _z_bytes_empty(); + + return ret; +} + +int8_t _z_fragment_payload_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { + (void)(header); + + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Decoding _Z_MID_FRAGMENT_PAYLOAD\n"); + + ret |= _z_bytes_decode(&msg->_payload, zbf); + if (ret != _Z_RES_OK) { + msg->_payload = _z_bytes_empty(); + } + + return ret; +} + +int8_t _z_fragment_header_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_fragment_header_decode_na(msg, zbf, header); +} + +int8_t _z_fragment_payload_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_fragment_payload_decode_na(msg, zbf, header); +} + +/*------------------ Transport Extensions Message ------------------*/ +int8_t _z_extensions_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_ext_vec_t *v_ext) { + (void)(header); + int8_t ret = _Z_RES_OK; + + _Z_DEBUG("Encoding _Z_TRANSPORT_EXTENSIONS\n"); + if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true) { + size_t n_ext = _z_msg_ext_vec_len(v_ext); + for (size_t i = 0; (i + 1) < n_ext; i++) { + _z_msg_ext_t *ext = _z_msg_ext_vec_get(v_ext, i); + _Z_EXT_SET_FLAG(ext->_header, _Z_MSG_EXT_FLAG_Z); + ret |= _z_msg_ext_encode(wbf, ext); } + ret |= _z_msg_ext_encode(wbf, _z_msg_ext_vec_get(v_ext, n_ext - 1)); // Last extension wo\ next flag + } + + return ret; +} + +int8_t _z_extensions_decode_na(_z_msg_ext_vec_t *v_ext, _z_zbuf_t *zbf, uint8_t header) { + (void)(header); + int8_t ret = _Z_RES_OK; + + _Z_DEBUG("Decoding _Z_TRANSPORT_EXTENSIONS\n"); + if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true) { + *v_ext = _z_msg_ext_vec_make(10); // TODO[protocol]: make it a list + _z_msg_ext_t *ext = NULL; + do { + ext = (_z_msg_ext_t *)z_malloc(sizeof(_z_msg_ext_t)); + ret |= _z_msg_ext_decode(ext, zbf); + _z_msg_ext_vec_append(v_ext, ext); + } while (_Z_EXT_HAS_FLAG(header, _Z_MSG_EXT_FLAG_Z) == true); } else { - msg->_payload = _z_bytes_empty(); + *v_ext = _z_msg_ext_vec_make(0); } return ret; } -int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_fragment_decode_na(msg, zbf, header); +int8_t _z_extensions_decode(_z_msg_ext_vec_t *v_ext, _z_zbuf_t *zbf, uint8_t header) { + return _z_extensions_decode_na(v_ext, zbf, header); } /*------------------ Transport Message ------------------*/ @@ -1471,39 +1552,47 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t } uint8_t header = msg->_header; - size_t n_ext = _z_msg_ext_vec_len(&msg->_extensions); - if (n_ext > 0) { + if (_z_msg_ext_vec_is_empty(&msg->_extensions) == false) { header |= _Z_FLAG_T_Z; } _Z_EC(_z_wbuf_write(wbf, header)) switch (_Z_MID(msg->_header)) { case _Z_MID_FRAME: { - ret |= _z_frame_encode(wbf, msg->_header, &msg->_body._frame); + ret |= _z_frame_header_encode(wbf, msg->_header, &msg->_body._frame); + ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); + ret |= _z_frame_payload_encode(wbf, msg->_header, &msg->_body._frame); } break; case _Z_MID_FRAGMENT: { - ret |= _z_fragment_encode(wbf, msg->_header, &msg->_body._fragment); + ret |= _z_fragment_header_encode(wbf, msg->_header, &msg->_body._fragment); + ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); + ret |= _z_fragment_payload_encode(wbf, msg->_header, &msg->_body._fragment); } break; case _Z_MID_KEEP_ALIVE: { ret |= _z_keep_alive_encode(wbf, msg->_header, &msg->_body._keep_alive); + ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; case _Z_MID_JOIN: { ret |= _z_join_encode(wbf, msg->_header, &msg->_body._join); + ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; case _Z_MID_INIT: { ret |= _z_init_encode(wbf, msg->_header, &msg->_body._init); + ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; case _Z_MID_OPEN: { ret |= _z_open_encode(wbf, msg->_header, &msg->_body._open); + ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; case _Z_MID_CLOSE: { ret |= _z_close_encode(wbf, msg->_header, &msg->_body._close); + ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; default: { @@ -1512,16 +1601,6 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t } break; } - if (n_ext > 0) { - for (size_t i = 0; (i + 1) < n_ext; i++) { - _z_msg_ext_t *ext = _z_msg_ext_vec_get(&msg->_extensions, i); - _Z_EXT_SET_FLAG(ext->_header, _Z_MSG_EXT_FLAG_Z); - ret |= _z_msg_ext_encode(wbf, ext); - } - ret |= - _z_msg_ext_encode(wbf, _z_msg_ext_vec_get(&msg->_extensions, n_ext - 1)); // Last extension wo\ next flag - } - return ret; } @@ -1537,41 +1616,51 @@ int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *zb uint8_t mid = _Z_MID(msg->_header); switch (mid) { case _Z_MID_FRAME: { - ret |= _z_frame_decode(&msg->_body._frame, zbf, msg->_header); + ret |= _z_frame_header_decode(&msg->_body._frame, zbf, msg->_header); + ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); + ret |= _z_frame_payload_decode(&msg->_body._frame, zbf, msg->_header); is_last = true; } break; case _Z_MID_FRAGMENT: { - ret |= _z_fragment_decode(&msg->_body._fragment, zbf, msg->_header); + ret |= _z_fragment_header_decode(&msg->_body._fragment, zbf, msg->_header); + ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); + ret |= _z_fragment_payload_decode(&msg->_body._fragment, zbf, msg->_header); is_last = true; } break; case _Z_MID_ATTACHMENT: { ret |= _z_attachment_decode(&msg->_attachment, zbf, msg->_header); + ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); } break; case _Z_MID_KEEP_ALIVE: { ret |= _z_keep_alive_decode(&msg->_body._keep_alive, zbf, msg->_header); + ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; case _Z_MID_JOIN: { ret |= _z_join_decode(&msg->_body._join, zbf, msg->_header); + ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; case _Z_MID_INIT: { ret |= _z_init_decode(&msg->_body._init, zbf, msg->_header); + ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; case _Z_MID_OPEN: { ret |= _z_open_decode(&msg->_body._open, zbf, msg->_header); + ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; case _Z_MID_CLOSE: { ret |= _z_close_decode(&msg->_body._close, zbf, msg->_header); + ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; @@ -1591,20 +1680,6 @@ int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *zb } } while ((ret == _Z_RES_OK) && (is_last == false)); - if (ret == _Z_RES_OK) { - if ((msg->_header & 0x80) == 0x80) { - msg->_extensions = _z_msg_ext_vec_make(10); - _z_msg_ext_t *ext = NULL; - do { - ext = (_z_msg_ext_t *)z_malloc(sizeof(_z_msg_ext_t)); - ret |= _z_msg_ext_decode(ext, zbf); - _z_msg_ext_vec_append(&msg->_extensions, ext); - } while ((ext->_header & 0x80) == 0x80); - } else { - msg->_extensions = _z_msg_ext_vec_make(0); - } - } - return ret; } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index d0e3d3aa6..bae889b5f 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -2358,14 +2358,18 @@ void frame_message(void) { _z_t_msg_frame_t e_fr = t_msg._body._frame; // Encode - int8_t res = _z_frame_encode(&wbf, t_msg._header, &e_fr); + int8_t res = _z_frame_header_encode(&wbf, t_msg._header, &e_fr); + res |= _z_extensions_encode(&wbf, t_msg._header, &t_msg._extensions); + res |= _z_frame_payload_encode(&wbf, t_msg._header, &e_fr); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); _z_t_msg_frame_t d_fr; - res = _z_frame_decode(&d_fr, &zbf, t_msg._header); + res = _z_frame_header_decode(&d_fr, &zbf, t_msg._header); + res |= _z_extensions_decode(&t_msg._extensions, &zbf, t_msg._header); + res |= _z_frame_payload_decode(&d_fr, &zbf, t_msg._header); assert(res == _Z_RES_OK); assert_eq_frame_message(&e_fr, &d_fr, t_msg._header); @@ -2408,14 +2412,18 @@ void fragment_message(void) { _z_t_msg_fragment_t e_fr = t_msg._body._fragment; // Encode - int8_t res = _z_fragment_encode(&wbf, t_msg._header, &e_fr); + int8_t res = _z_fragment_header_encode(&wbf, t_msg._header, &e_fr); + res |= _z_extensions_encode(&wbf, t_msg._header, &t_msg._extensions); + res |= _z_fragment_payload_encode(&wbf, t_msg._header, &e_fr); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); _z_t_msg_fragment_t d_fr; - res = _z_fragment_decode(&d_fr, &zbf, t_msg._header); + res = _z_fragment_header_decode(&d_fr, &zbf, t_msg._header); + res |= _z_extensions_decode(&t_msg._extensions, &zbf, t_msg._header); + res |= _z_fragment_payload_decode(&d_fr, &zbf, t_msg._header); assert(res == _Z_RES_OK); assert_eq_fragment_message(&e_fr, &d_fr, t_msg._header); From 6750a1a17737d08e6eb041bb79daa4b4686c22c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 29 Mar 2023 11:19:56 +0200 Subject: [PATCH 059/181] Rename message names to explicitly identify the layer --- include/zenoh-pico/protocol/msg.h | 68 +++++++------ src/protocol/msg.c | 98 +++++++++---------- src/protocol/msgcodec.c | 152 +++++++++++++++--------------- src/session/rx.c | 12 +-- src/transport/multicast/link/rx.c | 20 ++-- src/transport/transport.c | 6 +- src/transport/unicast/link/rx.c | 18 ++-- tests/z_msgcodec_test.c | 116 +++++++++++------------ 8 files changed, 249 insertions(+), 241 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 73655a5aa..cfdc82660 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -46,28 +46,36 @@ #define _Z_MID_HELLO 0x02 /* Transport Messages */ -#define _Z_MID_OAM 0x00 -#define _Z_MID_INIT 0x01 -#define _Z_MID_OPEN 0x02 -#define _Z_MID_CLOSE 0x03 -#define _Z_MID_KEEP_ALIVE 0x04 -#define _Z_MID_FRAME 0x05 -#define _Z_MID_FRAGMENT 0x06 -#define _Z_MID_JOIN 0x07 +#define _Z_MID_T_OAM 0x00 +#define _Z_MID_T_INIT 0x01 +#define _Z_MID_T_OPEN 0x02 +#define _Z_MID_T_CLOSE 0x03 +#define _Z_MID_T_KEEP_ALIVE 0x04 +#define _Z_MID_T_FRAME 0x05 +#define _Z_MID_T_FRAGMENT 0x06 +#define _Z_MID_T_JOIN 0x07 + +/* Network Messages */ +#define _Z_MID_N_OAM 0x1f +#define _Z_MID_N_DECLARE 0x1e +#define _Z_MID_N_PUSH 0x1d +#define _Z_MID_N_REQUEST 0x1c +#define _Z_MID_N_RESPONSE 0x1b +#define _Z_MID_N_RESPONSE_FINAL 0x1a /* Zenoh Messages */ -#define _Z_MID_DECLARE 0x0b -#define _Z_MID_DATA 0x0c -#define _Z_MID_QUERY 0x0d -#define _Z_MID_PULL 0x0e -#define _Z_MID_UNIT 0x0f -#define _Z_MID_LINK_STATE_LIST 0x10 +#define _Z_MID_Z_DECLARE 0x0b +#define _Z_MID_Z_DATA 0x0c +#define _Z_MID_Z_QUERY 0x0d +#define _Z_MID_Z_PULL 0x0e +#define _Z_MID_Z_UNIT 0x0f +#define _Z_MID_Z_LINK_STATE_LIST 0x10 /* Message decorators */ -#define _Z_MID_PRIORITY 0x1c -#define _Z_MID_ROUTING_CONTEXT 0x1d -#define _Z_MID_REPLY_CONTEXT 0x1e -#define _Z_MID_ATTACHMENT 0x1f +#define _Z_MID_A_PRIORITY 0x1c +#define _Z_MID_A_ROUTING_CONTEXT 0x1d +#define _Z_MID_A_REPLY_CONTEXT 0x1e +#define _Z_MID_A_ATTACHMENT 0x1f /*=============================*/ /* Message flags */ @@ -77,50 +85,50 @@ // Scout message flags: // I ZenohID if I==1 then the ZenohID is present // Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_SCOUT_I 0x08 // 1 << 3 +#define _Z_FLAG_T_SCOUT_I 0x08 // 1 << 3 // Hello message flags: // L Locators if L==1 then Locators are present // Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_HELLO_L 0x20 // 1 << 5 +#define _Z_FLAG_T_HELLO_L 0x20 // 1 << 5 // Join message flags: // T Lease period if T==1 then the lease period is in seconds else in milliseconds // S Size params if S==1 then size parameters are exchanged // Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_JOIN_T 0x20 // 1 << 5 -#define _Z_FLAG_JOIN_S 0x40 // 1 << 6 +#define _Z_FLAG_T_JOIN_T 0x20 // 1 << 5 +#define _Z_FLAG_T_JOIN_S 0x40 // 1 << 6 // Init message flags: // A Ack if A==1 then the message is an acknowledgment (aka InitAck), otherwise InitSyn // S Size params if S==1 then size parameters are exchanged // Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_INIT_A 0x20 // 1 << 5 -#define _Z_FLAG_INIT_S 0x40 // 1 << 6 +#define _Z_FLAG_T_INIT_A 0x20 // 1 << 5 +#define _Z_FLAG_T_INIT_S 0x40 // 1 << 6 // Open message flags: // A Ack if A==1 then the message is an acknowledgment (aka OpenAck), otherwise OpenSyn // T Lease period if T==1 then the lease period is in seconds else in milliseconds // Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_OPEN_A 0x20 // 1 << 5 -#define _Z_FLAG_OPEN_T 0x40 // 1 << 6 +#define _Z_FLAG_T_OPEN_A 0x20 // 1 << 5 +#define _Z_FLAG_T_OPEN_T 0x40 // 1 << 6 // Frame message flags: // R Reliable if R==1 it concerns the reliable channel, else the best-effort channel // Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_FRAME_R 0x20 // 1 << 5 +#define _Z_FLAG_T_FRAME_R 0x20 // 1 << 5 // Frame message flags: // R Reliable if R==1 it concerns the reliable channel, else the best-effort channel // M More if M==1 then other fragments will follow // Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_FRAGMENT_R 0x20 // 1 << 5 -#define _Z_FLAG_FRAGMENT_M 0x40 // 1 << 6 +#define _Z_FLAG_T_FRAGMENT_R 0x20 // 1 << 5 +#define _Z_FLAG_T_FRAGMENT_M 0x40 // 1 << 6 // Close message flags: // S Session Close if S==1 Session close or S==0 Link close // Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_CLOSE_S 0x20 // 1 << 5 +#define _Z_FLAG_T_CLOSE_S 0x20 // 1 << 5 /* Attachment message flags */ #define _Z_FLAG_A_Z \ diff --git a/src/protocol/msg.c b/src/protocol/msg.c index aaa79ca88..f656bcdbc 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -66,7 +66,7 @@ _z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_bytes_t replier_ rctx->_qid = qid; rctx->_replier_id = replier_id; - rctx->_header = _Z_MID_REPLY_CONTEXT; + rctx->_header = _Z_MID_A_REPLY_CONTEXT; if (is_final == true) { _Z_SET_FLAG(rctx->_header, _Z_FLAG_Z_F); } @@ -238,7 +238,7 @@ _z_zenoh_message_t _z_msg_make_declare(_z_declaration_array_t declarations) { msg._body._declare._declarations = declarations; - msg._header = _Z_MID_DECLARE; + msg._header = _Z_MID_Z_DECLARE; msg._attachment = NULL; msg._reply_context = NULL; @@ -298,7 +298,7 @@ _z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_pa msg._body._data._info = info; msg._body._data._payload = payload; - msg._header = _Z_MID_DATA; + msg._header = _Z_MID_Z_DATA; if (msg._body._data._info._flags != 0) { _Z_SET_FLAG(msg._header, _Z_FLAG_Z_I); } @@ -325,7 +325,7 @@ void _z_msg_clear_data(_z_msg_data_t *msg) { _z_zenoh_message_t _z_msg_make_unit(_Bool can_be_dropped) { _z_zenoh_message_t msg; - msg._header = _Z_MID_UNIT; + msg._header = _Z_MID_Z_UNIT; if (can_be_dropped == true) { _Z_SET_FLAG(msg._header, _Z_FLAG_Z_D); } @@ -346,7 +346,7 @@ _z_zenoh_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id, _z_zint msg._body._pull._pull_id = pull_id; msg._body._pull._max_samples = max_samples; - msg._header = _Z_MID_PULL; + msg._header = _Z_MID_Z_PULL; if (is_final == true) { _Z_SET_FLAG(msg._header, _Z_FLAG_Z_F); } @@ -379,7 +379,7 @@ _z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, char *parameters, _z_zint msg._body._query._info._encoding = with_value.encoding; msg._body._query._payload = with_value.payload; - msg._header = _Z_MID_QUERY; + msg._header = _Z_MID_Z_QUERY; if (msg._body._query._target != Z_QUERY_TARGET_BEST_MATCHING) { _Z_SET_FLAG(msg._header, _Z_FLAG_Z_T); } @@ -425,19 +425,19 @@ void _z_msg_clear(_z_zenoh_message_t *msg) { uint8_t mid = _Z_MID(msg->_header); switch (mid) { - case _Z_MID_DECLARE: + case _Z_MID_Z_DECLARE: _z_msg_clear_declare(&msg->_body._declare); break; - case _Z_MID_DATA: + case _Z_MID_Z_DATA: _z_msg_clear_data(&msg->_body._data); break; - case _Z_MID_PULL: + case _Z_MID_Z_PULL: _z_msg_clear_pull(&msg->_body._pull); break; - case _Z_MID_QUERY: + case _Z_MID_Z_QUERY: _z_msg_clear_query(&msg->_body._query); break; - case _Z_MID_UNIT: + case _Z_MID_Z_UNIT: _z_msg_clear_unit(&msg->_body._unit); break; default: @@ -494,7 +494,7 @@ _z_scouting_message_t _z_s_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _ msg._body._hello._locators = locators; if (_z_locator_array_is_empty(&locators) == false) { - _Z_SET_FLAG(msg._header, _Z_FLAG_HELLO_L); + _Z_SET_FLAG(msg._header, _Z_FLAG_T_HELLO_L); } msg._attachment = NULL; @@ -518,7 +518,7 @@ void _z_s_msg_clear_hello(_z_s_msg_hello_t *msg) { _z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_bytes_t zid, _z_conduit_sn_list_t next_sn) { _z_transport_message_t msg; - msg._header = _Z_MID_JOIN; + msg._header = _Z_MID_T_JOIN; msg._body._join._version = Z_PROTO_VERSION; msg._body._join._whatami = whatami; @@ -531,14 +531,14 @@ _z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, msg._body._join._zid = zid; if ((lease % 1000) == 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_JOIN_T); + _Z_SET_FLAG(msg._header, _Z_FLAG_T_JOIN_T); } if ((msg._body._join._batch_size != _Z_DEFAULT_BATCH_SIZE) || (msg._body._join._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || (msg._body._join._key_id_res != _Z_DEFAULT_RESOLUTION_SIZE) || (msg._body._join._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { - _Z_SET_FLAG(msg._header, _Z_FLAG_JOIN_S); + _Z_SET_FLAG(msg._header, _Z_FLAG_T_JOIN_S); } msg._attachment = NULL; @@ -564,7 +564,7 @@ void _z_t_msg_clear_join(_z_t_msg_join_t *msg) { _z_bytes_clear(&msg->_zid); } /*------------------ Init Message ------------------*/ _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_bytes_t zid) { _z_transport_message_t msg; - msg._header = _Z_MID_INIT; + msg._header = _Z_MID_T_INIT; msg._body._init._version = Z_PROTO_VERSION; msg._body._init._whatami = whatami; @@ -579,7 +579,7 @@ _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_bytes_t zi (msg._body._init._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || (msg._body._init._key_id_res != _Z_DEFAULT_RESOLUTION_SIZE) || (msg._body._init._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { - _Z_SET_FLAG(msg._header, _Z_FLAG_INIT_S); + _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_S); } msg._attachment = NULL; @@ -590,8 +590,8 @@ _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_bytes_t zi _z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_bytes_t zid, _z_bytes_t cookie) { _z_transport_message_t msg; - msg._header = _Z_MID_INIT; - _Z_SET_FLAG(msg._header, _Z_FLAG_INIT_A); + msg._header = _Z_MID_T_INIT; + _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_A); msg._body._init._version = Z_PROTO_VERSION; msg._body._init._whatami = whatami; @@ -606,7 +606,7 @@ _z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_bytes_t zi (msg._body._init._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || (msg._body._init._key_id_res != _Z_DEFAULT_RESOLUTION_SIZE) || (msg._body._init._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { - _Z_SET_FLAG(msg._header, _Z_FLAG_INIT_S); + _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_S); } msg._attachment = NULL; @@ -634,14 +634,14 @@ void _z_t_msg_clear_init(_z_t_msg_init_t *msg) { /*------------------ Open Message ------------------*/ _z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie) { _z_transport_message_t msg; - msg._header = _Z_MID_OPEN; + msg._header = _Z_MID_T_OPEN; msg._body._open._lease = lease; msg._body._open._initial_sn = initial_sn; msg._body._open._cookie = cookie; if ((lease % 1000) == 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_OPEN_T); + _Z_SET_FLAG(msg._header, _Z_FLAG_T_OPEN_T); } msg._attachment = NULL; @@ -652,15 +652,15 @@ _z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial _z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial_sn) { _z_transport_message_t msg; - msg._header = _Z_MID_OPEN; - _Z_SET_FLAG(msg._header, _Z_FLAG_OPEN_A); + msg._header = _Z_MID_T_OPEN; + _Z_SET_FLAG(msg._header, _Z_FLAG_T_OPEN_A); msg._body._open._lease = lease; msg._body._open._initial_sn = initial_sn; _z_bytes_reset(&msg._body._open._cookie); if ((lease % 1000) == 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_OPEN_T); + _Z_SET_FLAG(msg._header, _Z_FLAG_T_OPEN_T); } msg._attachment = NULL; @@ -680,11 +680,11 @@ void _z_t_msg_clear_open(_z_t_msg_open_t *msg) { _z_bytes_clear(&msg->_cookie); /*------------------ Close Message ------------------*/ _z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only) { _z_transport_message_t msg; - msg._header = _Z_MID_CLOSE; + msg._header = _Z_MID_T_CLOSE; msg._body._close._reason = reason; if (link_only == false) { - _Z_SET_FLAG(msg._header, _Z_FLAG_CLOSE_S); + _Z_SET_FLAG(msg._header, _Z_FLAG_T_CLOSE_S); } msg._attachment = NULL; @@ -700,7 +700,7 @@ void _z_t_msg_clear_close(_z_t_msg_close_t *msg) { (void)(msg); } /*------------------ Keep Alive Message ------------------*/ _z_transport_message_t _z_t_msg_make_keep_alive(void) { _z_transport_message_t msg; - msg._header = _Z_MID_KEEP_ALIVE; + msg._header = _Z_MID_T_KEEP_ALIVE; msg._attachment = NULL; msg._extensions = _z_msg_ext_vec_make(0); @@ -718,11 +718,11 @@ void _z_t_msg_clear_keep_alive(_z_t_msg_keep_alive_t *msg) { (void)(msg); } /*------------------ Frame Message ------------------*/ _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable) { _z_transport_message_t msg; - msg._header = _Z_MID_FRAME; + msg._header = _Z_MID_T_FRAME; msg._body._frame._sn = sn; if (is_reliable == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_FRAME_R); + _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAME_R); } msg._body._frame._messages = _z_zenoh_message_vec_make(0); @@ -735,11 +735,11 @@ _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliabl _z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_zenoh_message_vec_t messages, _Bool is_reliable) { _z_transport_message_t msg; - msg._header = _Z_MID_FRAME; + msg._header = _Z_MID_T_FRAME; msg._body._frame._sn = sn; if (is_reliable == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_FRAME_R); + _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAME_R); } msg._body._frame._messages = messages; @@ -760,12 +760,12 @@ void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg) { _z_zenoh_message_vec_clear(&m /*------------------ Fragment Message ------------------*/ _z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_payload_t payload, _Bool is_reliable, _Bool is_last) { _z_transport_message_t msg; - msg._header = _Z_MID_FRAGMENT; + msg._header = _Z_MID_T_FRAGMENT; if (is_last == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_FRAGMENT_M); + _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAGMENT_M); } if (is_reliable == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_FRAGMENT_R); + _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAGMENT_R); } msg._body._fragment._sn = sn; @@ -791,31 +791,31 @@ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg) { uint8_t mid = _Z_MID(msg->_header); switch (mid) { - case _Z_MID_JOIN: { + case _Z_MID_T_JOIN: { _z_t_msg_copy_join(&clone->_body._join, &msg->_body._join); } break; - case _Z_MID_INIT: { + case _Z_MID_T_INIT: { _z_t_msg_copy_init(&clone->_body._init, &msg->_body._init); } break; - case _Z_MID_OPEN: { + case _Z_MID_T_OPEN: { _z_t_msg_copy_open(&clone->_body._open, &msg->_body._open); } break; - case _Z_MID_CLOSE: { + case _Z_MID_T_CLOSE: { _z_t_msg_copy_close(&clone->_body._close, &msg->_body._close); } break; - case _Z_MID_KEEP_ALIVE: { + case _Z_MID_T_KEEP_ALIVE: { _z_t_msg_copy_keep_alive(&clone->_body._keep_alive, &msg->_body._keep_alive); } break; - case _Z_MID_FRAME: { + case _Z_MID_T_FRAME: { _z_t_msg_copy_frame(&clone->_body._frame, &msg->_body._frame); } break; - case _Z_MID_FRAGMENT: { + case _Z_MID_T_FRAGMENT: { _z_t_msg_copy_fragment(&clone->_body._fragment, &msg->_body._fragment); } break; @@ -833,31 +833,31 @@ void _z_t_msg_clear(_z_transport_message_t *msg) { uint8_t mid = _Z_MID(msg->_header); switch (mid) { - case _Z_MID_JOIN: { + case _Z_MID_T_JOIN: { _z_t_msg_clear_join(&msg->_body._join); } break; - case _Z_MID_INIT: { + case _Z_MID_T_INIT: { _z_t_msg_clear_init(&msg->_body._init); } break; - case _Z_MID_OPEN: { + case _Z_MID_T_OPEN: { _z_t_msg_clear_open(&msg->_body._open); } break; - case _Z_MID_CLOSE: { + case _Z_MID_T_CLOSE: { _z_t_msg_clear_close(&msg->_body._close); } break; - case _Z_MID_KEEP_ALIVE: { + case _Z_MID_T_KEEP_ALIVE: { _z_t_msg_clear_keep_alive(&msg->_body._keep_alive); } break; - case _Z_MID_FRAME: { + case _Z_MID_T_FRAME: { _z_t_msg_clear_frame(&msg->_body._frame); } break; - case _Z_MID_FRAGMENT: { + case _Z_MID_T_FRAGMENT: { _z_t_msg_clear_fragment(&msg->_body._fragment); } break; diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 2c3359917..50f0ab11b 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -194,7 +194,7 @@ int8_t _z_locators_decode(_z_locator_array_t *a_loc, _z_zbuf_t *zbf) { return _z /*------------------ Attachment Decorator ------------------*/ int8_t _z_attachment_encode(_z_wbuf_t *wbf, const _z_attachment_t *msg) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_ATTACHMENT\n"); + _Z_DEBUG("Encoding _Z_MID_A_ATTACHMENT\n"); // WARNING: we do not support sliced content in zenoh-pico. // Disable the SLICED flag to be on the safe side. @@ -206,7 +206,7 @@ int8_t _z_attachment_encode(_z_wbuf_t *wbf, const _z_attachment_t *msg) { } int8_t _z_attachment_decode_na(_z_attachment_t *atch, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_ATTACHMENT\n"); + _Z_DEBUG("Decoding _Z_MID_A_ATTACHMENT\n"); int8_t ret = _Z_RES_OK; atch->_header = header; @@ -241,7 +241,7 @@ int8_t _z_attachment_decode(_z_attachment_t **atch, _z_zbuf_t *zbf, uint8_t head /*------------------ ReplyContext Decorator ------------------*/ int8_t _z_reply_context_encode(_z_wbuf_t *wbf, const _z_reply_context_t *msg) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_REPLY_CONTEXT\n"); + _Z_DEBUG("Encoding _Z_MID_A_REPLY_CONTEXT\n"); _Z_EC(_z_wbuf_write(wbf, msg->_header)) @@ -254,7 +254,7 @@ int8_t _z_reply_context_encode(_z_wbuf_t *wbf, const _z_reply_context_t *msg) { } int8_t _z_reply_context_decode_na(_z_reply_context_t *rc, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_REPLY_CONTEXT\n"); + _Z_DEBUG("Decoding _Z_MID_A_REPLY_CONTEXT\n"); int8_t ret = _Z_RES_OK; rc->_header = header; @@ -607,7 +607,7 @@ int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf) { return _z /*------------------ Declaration Message ------------------*/ int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_msg_declare_t *msg) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_DECLARE\n"); + _Z_DEBUG("Encoding _Z_MID_Z_DECLARE\n"); _Z_EC(_z_zint_encode(wbf, msg->_declarations._len)) for (_z_zint_t i = 0; i < msg->_declarations._len; i++) { @@ -618,7 +618,7 @@ int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_msg_declare_t *msg) { } int8_t _z_declare_decode_na(_z_msg_declare_t *msg, _z_zbuf_t *zbf) { - _Z_DEBUG("Decoding _Z_MID_DECLARE\n"); + _Z_DEBUG("Decoding _Z_MID_Z_DECLARE\n"); int8_t ret = _Z_RES_OK; _z_zint_t len = 0; @@ -719,7 +719,7 @@ int8_t _z_data_info_decode(_z_data_info_t *di, _z_zbuf_t *zbf) { return _z_data_ /*------------------ Data Message ------------------*/ int8_t _z_data_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_data_t *msg) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_DATA\n"); + _Z_DEBUG("Encoding _Z_MID_Z_DATA\n"); _Z_EC(_z_keyexpr_encode(wbf, header, &msg->_key)) @@ -732,7 +732,7 @@ int8_t _z_data_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_data_t *msg) } int8_t _z_data_decode_na(_z_msg_data_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_DATA\n"); + _Z_DEBUG("Decoding _Z_MID_Z_DATA\n"); int8_t ret = _Z_RES_OK; ret |= _z_keyexpr_decode(&msg->_key, zbf, header); @@ -759,7 +759,7 @@ int8_t _z_data_decode(_z_msg_data_t *msg, _z_zbuf_t *zbf, uint8_t header) { /*------------------ Pull Message ------------------*/ int8_t _z_pull_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_pull_t *msg) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_PULL\n"); + _Z_DEBUG("Encoding _Z_MID_Z_PULL\n"); _Z_EC(_z_keyexpr_encode(wbf, header, &msg->_key)) @@ -773,7 +773,7 @@ int8_t _z_pull_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_pull_t *msg) } int8_t _z_pull_decode_na(_z_msg_pull_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_PULL\n"); + _Z_DEBUG("Decoding _Z_MID_Z_PULL\n"); int8_t ret = _Z_RES_OK; ret |= _z_keyexpr_decode(&msg->_key, zbf, header); @@ -804,7 +804,7 @@ int8_t _z_query_consolidation_encode(_z_wbuf_t *wbf, const z_consolidation_mode_ int8_t _z_query_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_query_t *msg) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_QUERY\n"); + _Z_DEBUG("Encoding _Z_MID_Z_QUERY\n"); _Z_EC(_z_keyexpr_encode(wbf, header, &msg->_key)) @@ -835,7 +835,7 @@ int8_t _z_query_consolidation_decode(z_consolidation_mode_t *qcm, _z_zbuf_t *zbf } int8_t _z_query_decode_na(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_QUERY\n"); + _Z_DEBUG("Decoding _Z_MID_Z_QUERY\n"); int8_t ret = _Z_RES_OK; ret |= _z_keyexpr_decode(&msg->_key, zbf, header); @@ -884,23 +884,23 @@ int8_t _z_zenoh_message_encode(_z_wbuf_t *wbf, const _z_zenoh_message_t *msg) { uint8_t mid = _Z_MID(msg->_header); switch (mid) { - case _Z_MID_DATA: { + case _Z_MID_Z_DATA: { ret |= _z_data_encode(wbf, msg->_header, &msg->_body._data); } break; - case _Z_MID_QUERY: { + case _Z_MID_Z_QUERY: { ret |= _z_query_encode(wbf, msg->_header, &msg->_body._query); } break; - case _Z_MID_DECLARE: { + case _Z_MID_Z_DECLARE: { ret |= _z_declare_encode(wbf, &msg->_body._declare); } break; - case _Z_MID_PULL: { + case _Z_MID_Z_PULL: { ret |= _z_pull_encode(wbf, msg->_header, &msg->_body._pull); } break; - case _Z_MID_UNIT: { + case _Z_MID_Z_UNIT: { // Do nothing. Unit messages have no body } break; @@ -924,45 +924,45 @@ int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { if (ret == _Z_RES_OK) { uint8_t mid = _Z_MID(msg->_header); switch (mid) { - case _Z_MID_DATA: { + case _Z_MID_Z_DATA: { ret |= _z_data_decode(&msg->_body._data, zbf, msg->_header); is_last = true; } break; - case _Z_MID_ATTACHMENT: { + case _Z_MID_A_ATTACHMENT: { ret |= _z_attachment_decode(&msg->_attachment, zbf, msg->_header); } break; - case _Z_MID_REPLY_CONTEXT: { + case _Z_MID_A_REPLY_CONTEXT: { ret |= _z_reply_context_decode(&msg->_reply_context, zbf, msg->_header); } break; - case _Z_MID_QUERY: { + case _Z_MID_Z_QUERY: { ret |= _z_query_decode(&msg->_body._query, zbf, msg->_header); is_last = true; } break; - case _Z_MID_DECLARE: { + case _Z_MID_Z_DECLARE: { ret |= _z_declare_decode(&msg->_body._declare, zbf); is_last = true; } break; - case _Z_MID_PULL: { + case _Z_MID_Z_PULL: { ret |= _z_pull_decode(&msg->_body._pull, zbf, msg->_header); is_last = true; } break; - case _Z_MID_UNIT: { + case _Z_MID_Z_UNIT: { // Do nothing. Unit messages have no body. is_last = true; } break; - case _Z_MID_PRIORITY: { + case _Z_MID_A_PRIORITY: { // Ignore the priority decorator for the time being since zenoh-pico does not // perform any routing. Hence, priority information does not need to be propagated. } break; - case _Z_MID_LINK_STATE_LIST: { + case _Z_MID_Z_LINK_STATE_LIST: { _Z_DEBUG("WARNING: Link state not supported in zenoh-pico\n"); is_last = true; } break; @@ -997,7 +997,7 @@ int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_scout_t *m cbyte |= (msg->_what & 0x07); uint8_t zid_len = msg->_zid.len; if (zid_len > 0) { // TODO[protocol]: check if ZID > 0 && <= 16 - _Z_SET_FLAG(cbyte, _Z_FLAG_SCOUT_I); + _Z_SET_FLAG(cbyte, _Z_FLAG_T_SCOUT_I); cbyte |= ((zid_len - 1) & 0x0F) << 4; } _Z_EC(_z_uint8_encode(wbf, cbyte)) @@ -1017,7 +1017,7 @@ int8_t _z_scout_decode_na(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) uint8_t cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); msg->_what = cbyte & 0x07; - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(cbyte, _Z_FLAG_SCOUT_I) == true)) { + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(cbyte, _Z_FLAG_T_SCOUT_I) == true)) { msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; ret |= _z_bytes_val_decode(&msg->_zid, zbf); if (ret != _Z_RES_OK) { @@ -1047,7 +1047,7 @@ int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *m _Z_EC(_z_uint8_encode(wbf, cbyte)) _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) - if (_Z_HAS_FLAG(header, _Z_FLAG_HELLO_L) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true) { _Z_EC(_z_locators_encode(wbf, &msg->_locators)) } @@ -1074,7 +1074,7 @@ int8_t _z_hello_decode_na(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) msg->_zid = _z_bytes_empty(); } - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_HELLO_L) == true)) { + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true)) { ret |= _z_locators_decode(&msg->_locators, zbf); if (ret != _Z_RES_OK) { msg->_locators = _z_locator_array_make(0); @@ -1093,7 +1093,7 @@ int8_t _z_hello_decode(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) { /*------------------ Join Message ------------------*/ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_JOIN\n"); + _Z_DEBUG("Encoding _Z_MID_T_JOIN\n"); _Z_EC(_z_wbuf_write(wbf, msg->_version)) @@ -1103,7 +1103,7 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg _Z_EC(_z_uint8_encode(wbf, cbyte)) _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) - if (_Z_HAS_FLAG(header, _Z_FLAG_JOIN_S) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S) == true) { cbyte = 0; cbyte |= (msg->_seq_num_res & 0x03); cbyte |= ((msg->_req_id_res & 0x03) << 2); @@ -1112,7 +1112,7 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) } - if (_Z_HAS_FLAG(header, _Z_FLAG_JOIN_T) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_T) == true) { _Z_EC(_z_zint_encode(wbf, msg->_lease / 1000)) } else { _Z_EC(_z_zint_encode(wbf, msg->_lease)) @@ -1132,7 +1132,7 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg } int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_JOIN\n"); + _Z_DEBUG("Decoding _Z_MID_T_JOIN\n"); int8_t ret = _Z_RES_OK; ret |= _z_uint8_decode(&msg->_version, zbf); @@ -1151,7 +1151,7 @@ int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { msg->_zid = _z_bytes_empty(); } - if (_Z_HAS_FLAG(header, _Z_FLAG_JOIN_S) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S) == true) { cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); msg->_seq_num_res = (cbyte & 0x03); @@ -1166,7 +1166,7 @@ int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { } ret |= _z_zint_decode(&msg->_lease, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_JOIN_T) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_T) == true) { msg->_lease = msg->_lease * 1000; } @@ -1189,7 +1189,7 @@ int8_t _z_join_decode(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { /*------------------ Init Message ------------------*/ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg) { - _Z_DEBUG("Encoding _Z_MID_INIT\n"); + _Z_DEBUG("Encoding _Z_MID_T_INIT\n"); int8_t ret = _Z_RES_OK; _Z_EC(_z_wbuf_write(wbf, msg->_version)) @@ -1200,7 +1200,7 @@ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg _Z_EC(_z_uint8_encode(wbf, cbyte)) _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) - if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_S) == true) { cbyte = 0; cbyte |= (msg->_seq_num_res & 0x03); cbyte |= ((msg->_req_id_res & 0x03) << 2); @@ -1209,7 +1209,7 @@ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) } - if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_A) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_A) == true) { _Z_EC(_z_bytes_encode(wbf, &msg->_cookie)) } @@ -1217,7 +1217,7 @@ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg } int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_INIT\n"); + _Z_DEBUG("Decoding _Z_MID_T_INIT\n"); int8_t ret = _Z_RES_OK; ret |= _z_uint8_decode(&msg->_version, zbf); @@ -1236,7 +1236,7 @@ int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { msg->_zid = _z_bytes_empty(); } - if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_S) == true) { cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); msg->_seq_num_res = (cbyte & 0x03); @@ -1250,7 +1250,7 @@ int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; } - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_INIT_A) == true)) { + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_A) == true)) { ret |= _z_bytes_decode(&msg->_cookie, zbf); if (ret != _Z_RES_OK) { msg->_cookie = _z_bytes_empty(); @@ -1269,9 +1269,9 @@ int8_t _z_init_decode(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { /*------------------ Open Message ------------------*/ int8_t _z_open_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_open_t *msg) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_OPEN\n"); + _Z_DEBUG("Encoding _Z_MID_T_OPEN\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_OPEN_T) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_T) == true) { _Z_EC(_z_zint_encode(wbf, msg->_lease / 1000)) } else { _Z_EC(_z_zint_encode(wbf, msg->_lease)) @@ -1279,7 +1279,7 @@ int8_t _z_open_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_open_t *msg _Z_EC(_z_zint_encode(wbf, msg->_initial_sn)) - if (_Z_HAS_FLAG(header, _Z_FLAG_OPEN_A) == false) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_A) == false) { _Z_EC(_z_bytes_encode(wbf, &msg->_cookie)) } @@ -1287,17 +1287,17 @@ int8_t _z_open_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_open_t *msg } int8_t _z_open_decode_na(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_OPEN\n"); + _Z_DEBUG("Decoding _Z_MID_T_OPEN\n"); int8_t ret = _Z_RES_OK; ret |= _z_zint_decode(&msg->_lease, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_OPEN_T) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_T) == true) { msg->_lease = msg->_lease * 1000; } ret |= _z_zint_decode(&msg->_initial_sn, zbf); - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_OPEN_A) == false)) { + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_A) == false)) { ret |= _z_bytes_decode(&msg->_cookie, zbf); if (ret != _Z_RES_OK) { msg->_cookie = _z_bytes_empty(); @@ -1317,7 +1317,7 @@ int8_t _z_open_decode(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t _z_close_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_close_t *msg) { (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_CLOSE\n"); + _Z_DEBUG("Encoding _Z_MID_T_CLOSE\n"); ret |= _z_wbuf_write(wbf, msg->_reason); @@ -1327,7 +1327,7 @@ int8_t _z_close_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_close_t *m int8_t _z_close_decode_na(_z_t_msg_close_t *msg, _z_zbuf_t *zbf, uint8_t header) { (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_CLOSE\n"); + _Z_DEBUG("Decoding _Z_MID_T_CLOSE\n"); ret |= _z_uint8_decode(&msg->_reason, zbf); @@ -1345,7 +1345,7 @@ int8_t _z_keep_alive_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_keep_ (void)(msg); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_KEEP_ALIVE\n"); + _Z_DEBUG("Encoding _Z_MID_T_KEEP_ALIVE\n"); return ret; } @@ -1356,7 +1356,7 @@ int8_t _z_keep_alive_decode_na(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8 (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_KEEP_ALIVE\n"); + _Z_DEBUG("Decoding _Z_MID_T_KEEP_ALIVE\n"); return ret; } @@ -1370,7 +1370,7 @@ int8_t _z_frame_header_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fra (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_FRAME_HEADER\n"); + _Z_DEBUG("Encoding _Z_MID_T_FRAME_HEADER\n"); _Z_EC(_z_zint_encode(wbf, msg->_sn)) @@ -1381,7 +1381,7 @@ int8_t _z_frame_payload_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fr (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_FRAME_PAYLOAD\n"); + _Z_DEBUG("Encoding _Z_MID_T_FRAME_PAYLOAD\n"); size_t len = _z_zenoh_message_vec_len(&msg->_messages); for (size_t i = 0; i < len; i++) { @@ -1395,7 +1395,7 @@ int8_t _z_frame_header_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_FRAME_HEADER\n"); + _Z_DEBUG("Decoding _Z_MID_T_FRAME_HEADER\n"); ret |= _z_zint_decode(&msg->_sn, zbf); @@ -1406,7 +1406,7 @@ int8_t _z_frame_payload_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_FRAME_PAYLOAD\n"); + _Z_DEBUG("Decoding _Z_MID_T_FRAME_PAYLOAD\n"); msg->_messages = _z_zenoh_message_vec_make(_ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE); while (_z_zbuf_len(zbf) > 0) { @@ -1448,7 +1448,7 @@ int8_t _z_fragment_header_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_ (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_FRAGMENT_HEADER\n"); + _Z_DEBUG("Encoding _Z_MID_T_FRAGMENT_HEADER\n"); _Z_EC(_z_zint_encode(wbf, msg->_sn)) @@ -1459,7 +1459,7 @@ int8_t _z_fragment_payload_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_FRAGMENT_PAYLOAD\n"); + _Z_DEBUG("Encoding _Z_MID_T_FRAGMENT_PAYLOAD\n"); _Z_EC(_z_bytes_encode(wbf, &msg->_payload)) @@ -1470,7 +1470,7 @@ int8_t _z_fragment_header_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, ui (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_FRAGMENT_HEADER\n"); + _Z_DEBUG("Decoding _Z_MID_T_FRAGMENT_HEADER\n"); ret |= _z_zint_decode(&msg->_sn, zbf); @@ -1481,7 +1481,7 @@ int8_t _z_fragment_payload_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, u (void)(header); int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_FRAGMENT_PAYLOAD\n"); + _Z_DEBUG("Decoding _Z_MID_T_FRAGMENT_PAYLOAD\n"); ret |= _z_bytes_decode(&msg->_payload, zbf); if (ret != _Z_RES_OK) { @@ -1558,39 +1558,39 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t _Z_EC(_z_wbuf_write(wbf, header)) switch (_Z_MID(msg->_header)) { - case _Z_MID_FRAME: { + case _Z_MID_T_FRAME: { ret |= _z_frame_header_encode(wbf, msg->_header, &msg->_body._frame); ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); ret |= _z_frame_payload_encode(wbf, msg->_header, &msg->_body._frame); } break; - case _Z_MID_FRAGMENT: { + case _Z_MID_T_FRAGMENT: { ret |= _z_fragment_header_encode(wbf, msg->_header, &msg->_body._fragment); ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); ret |= _z_fragment_payload_encode(wbf, msg->_header, &msg->_body._fragment); } break; - case _Z_MID_KEEP_ALIVE: { + case _Z_MID_T_KEEP_ALIVE: { ret |= _z_keep_alive_encode(wbf, msg->_header, &msg->_body._keep_alive); ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; - case _Z_MID_JOIN: { + case _Z_MID_T_JOIN: { ret |= _z_join_encode(wbf, msg->_header, &msg->_body._join); ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; - case _Z_MID_INIT: { + case _Z_MID_T_INIT: { ret |= _z_init_encode(wbf, msg->_header, &msg->_body._init); ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; - case _Z_MID_OPEN: { + case _Z_MID_T_OPEN: { ret |= _z_open_encode(wbf, msg->_header, &msg->_body._open); ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; - case _Z_MID_CLOSE: { + case _Z_MID_T_CLOSE: { ret |= _z_close_encode(wbf, msg->_header, &msg->_body._close); ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; @@ -1615,56 +1615,56 @@ int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *zb if (ret == _Z_RES_OK) { uint8_t mid = _Z_MID(msg->_header); switch (mid) { - case _Z_MID_FRAME: { + case _Z_MID_T_FRAME: { ret |= _z_frame_header_decode(&msg->_body._frame, zbf, msg->_header); ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); ret |= _z_frame_payload_decode(&msg->_body._frame, zbf, msg->_header); is_last = true; } break; - case _Z_MID_FRAGMENT: { + case _Z_MID_T_FRAGMENT: { ret |= _z_fragment_header_decode(&msg->_body._fragment, zbf, msg->_header); ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); ret |= _z_fragment_payload_decode(&msg->_body._fragment, zbf, msg->_header); is_last = true; } break; - case _Z_MID_ATTACHMENT: { + case _Z_MID_A_ATTACHMENT: { ret |= _z_attachment_decode(&msg->_attachment, zbf, msg->_header); ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); } break; - case _Z_MID_KEEP_ALIVE: { + case _Z_MID_T_KEEP_ALIVE: { ret |= _z_keep_alive_decode(&msg->_body._keep_alive, zbf, msg->_header); ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; - case _Z_MID_JOIN: { + case _Z_MID_T_JOIN: { ret |= _z_join_decode(&msg->_body._join, zbf, msg->_header); ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; - case _Z_MID_INIT: { + case _Z_MID_T_INIT: { ret |= _z_init_decode(&msg->_body._init, zbf, msg->_header); ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; - case _Z_MID_OPEN: { + case _Z_MID_T_OPEN: { ret |= _z_open_decode(&msg->_body._open, zbf, msg->_header); ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; - case _Z_MID_CLOSE: { + case _Z_MID_T_CLOSE: { ret |= _z_close_decode(&msg->_body._close, zbf, msg->_header); ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; - case _Z_MID_PRIORITY: { + case _Z_MID_A_PRIORITY: { // Ignore the priority decorator for the time being since zenoh-pico does not // perform any routing. Hence, priority information does not need to be propagated. } break; diff --git a/src/session/rx.c b/src/session/rx.c index 3b9f15e80..f2f28da0b 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -27,8 +27,8 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg) { int8_t ret = _Z_RES_OK; switch (_Z_MID(msg->_header)) { - case _Z_MID_DATA: { - _Z_INFO("Received _Z_MID_DATA message %d\n", msg->_header); + case _Z_MID_Z_DATA: { + _Z_INFO("Received _Z_MID_Z_DATA message %d\n", msg->_header); if (msg->_reply_context != NULL) { // This is some data from a query ret = _z_trigger_query_reply_partial(zn, msg->_reply_context, msg->_body._data._key, msg->_body._data._payload, msg->_body._data._info._encoding, @@ -40,7 +40,7 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg) { } } break; - case _Z_MID_DECLARE: { + case _Z_MID_Z_DECLARE: { _Z_INFO("Received _Z_DECLARE message\n"); for (size_t i = 0; i < msg->_body._declare._declarations._len; i++) { _z_declaration_t decl = msg->_body._declare._declarations._val[i]; @@ -150,17 +150,17 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg) { } } break; - case _Z_MID_PULL: { + case _Z_MID_Z_PULL: { _Z_INFO("Received _Z_PULL message\n"); // TODO: not supported yet } break; - case _Z_MID_QUERY: { + case _Z_MID_Z_QUERY: { _Z_INFO("Received _Z_QUERY message\n"); _z_trigger_queryables(zn, &msg->_body._query); } break; - case _Z_MID_UNIT: { + case _Z_MID_Z_UNIT: { _Z_INFO("Received _Z_UNIT message\n"); // This might be the final reply if (msg->_reply_context != NULL) { diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 4de8481c5..db4eb8a3e 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -108,7 +108,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t // Mark the session that we have received data from this peer _z_transport_peer_entry_t *entry = _z_find_peer_entry(ztm->_peers, addr); switch (_Z_MID(t_msg->_header)) { - case _Z_MID_FRAME: { + case _Z_MID_T_FRAME: { _Z_INFO("Received _Z_FRAME message\n"); if (entry == NULL) { break; @@ -116,7 +116,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t entry->_received = true; // Check if the SN is correct - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAME_R) == true) { + if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_FRAME_R) == true) { // @TODO: amend once reliability is in place. For the time being only // monothonic SNs are ensured if (_z_sn_precedes(entry->_sn_res, entry->_sn_rx_sns._val._plain._reliable, t_msg->_body._frame._sn) == @@ -148,14 +148,14 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t break; } - case _Z_MID_FRAGMENT: { + case _Z_MID_T_FRAGMENT: { _Z_INFO("Received Z_FRAGMENT message\n"); if (entry == NULL) { break; } entry->_received = true; - _z_wbuf_t *dbuf = _Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAGMENT_R) + _z_wbuf_t *dbuf = _Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_FRAGMENT_R) ? &entry->_dbuf_reliable : &entry->_dbuf_best_effort; // Select the right defragmentation buffer @@ -170,7 +170,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t t_msg->_body._fragment._payload.len); } - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAGMENT_M) == false) { + if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_FRAGMENT_M) == false) { if (drop == true) { // Drop message if it exceeds the fragmentation size _z_wbuf_reset(dbuf); break; @@ -194,7 +194,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t break; } - case _Z_MID_KEEP_ALIVE: { + case _Z_MID_T_KEEP_ALIVE: { _Z_INFO("Received _Z_KEEP_ALIVE message\n"); if (entry == NULL) { break; @@ -204,17 +204,17 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t break; } - case _Z_MID_INIT: { + case _Z_MID_T_INIT: { // Do nothing, multicas transports are not expected to handle INIT messages break; } - case _Z_MID_OPEN: { + case _Z_MID_T_OPEN: { // Do nothing, multicas transports are not expected to handle OPEN messages break; } - case _Z_MID_JOIN: { + case _Z_MID_T_JOIN: { _Z_INFO("Received _Z_JOIN message\n"); if (t_msg->_body._join._version != Z_PROTO_VERSION) { break; @@ -283,7 +283,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t break; } - case _Z_MID_CLOSE: { + case _Z_MID_T_CLOSE: { _Z_INFO("Closing session as requested by the remote peer\n"); if (entry == NULL) { diff --git a/src/transport/transport.c b/src/transport/transport.c index 63f73f106..db3e9ba7e 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -267,7 +267,7 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * _z_transport_message_t iam; ret = _z_link_recv_t_msg(&iam, zl); if (ret == _Z_RES_OK) { - if ((_Z_MID(iam._header) == _Z_MID_INIT) && (_Z_HAS_FLAG(iam._header, _Z_FLAG_INIT_A) == true)) { + if ((_Z_MID(iam._header) == _Z_MID_T_INIT) && (_Z_HAS_FLAG(iam._header, _Z_FLAG_T_INIT_A) == true)) { _Z_INFO("Received Z_INIT(Ack)\n"); // Any of the size parameters in the InitAck must be less or equal than the one in the InitSyn, @@ -323,8 +323,8 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * _z_transport_message_t oam; ret = _z_link_recv_t_msg(&oam, zl); if (ret == _Z_RES_OK) { - if ((_Z_MID(oam._header) == _Z_MID_OPEN) && - (_Z_HAS_FLAG(oam._header, _Z_FLAG_OPEN_A) == true)) { + if ((_Z_MID(oam._header) == _Z_MID_T_OPEN) && + (_Z_HAS_FLAG(oam._header, _Z_FLAG_T_OPEN_A) == true)) { _Z_INFO("Received Z_OPEN(Ack)\n"); param->_lease = oam._body._open._lease; // The session lease diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index aeee15096..de0f829ae 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -85,10 +85,10 @@ int8_t _z_unicast_recv_t_msg(_z_transport_unicast_t *ztu, _z_transport_message_t int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_transport_message_t *t_msg) { switch (_Z_MID(t_msg->_header)) { - case _Z_MID_FRAME: { + case _Z_MID_T_FRAME: { _Z_INFO("Received Z_FRAME message\n"); // Check if the SN is correct - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAME_R) == true) { + if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_FRAME_R) == true) { // @TODO: amend once reliability is in place. For the time being only // monothonic SNs are ensured if (_z_sn_precedes(ztu->_sn_res, ztu->_sn_rx_reliable, t_msg->_body._frame._sn) == true) { @@ -118,10 +118,10 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans break; } - case _Z_MID_FRAGMENT: { + case _Z_MID_T_FRAGMENT: { _Z_INFO("Received Z_FRAGMENT message\n"); - _z_wbuf_t *dbuf = _Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAGMENT_R) + _z_wbuf_t *dbuf = _Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_FRAGMENT_R) ? &ztu->_dbuf_reliable : &ztu->_dbuf_best_effort; // Select the right defragmentation buffer @@ -136,7 +136,7 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans t_msg->_body._fragment._payload.len); } - if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_FRAGMENT_M) == false) { + if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_FRAGMENT_M) == false) { if (drop == true) { // Drop message if it exceeds the fragmentation size _z_wbuf_reset(dbuf); break; @@ -160,22 +160,22 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans break; } - case _Z_MID_KEEP_ALIVE: { + case _Z_MID_T_KEEP_ALIVE: { _Z_INFO("Received Z_KEEP_ALIVE message\n"); break; } - case _Z_MID_INIT: { + case _Z_MID_T_INIT: { // Do nothing, zenoh clients are not expected to handle accept messages on established sessions break; } - case _Z_MID_OPEN: { + case _Z_MID_T_OPEN: { // Do nothing, zenoh clients are not expected to handle accept messages on established sessions break; } - case _Z_MID_CLOSE: { + case _Z_MID_T_CLOSE: { _Z_INFO("Closing session as requested by the remote peer\n"); break; } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index bae889b5f..eb3bdad6b 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -70,25 +70,25 @@ void print_uint8_array(_z_bytes_t *arr) { void print_transport_message_type(uint8_t header) { switch (_Z_MID(header)) { - case _Z_MID_JOIN: + case _Z_MID_T_JOIN: printf("Join message"); break; - case _Z_MID_INIT: + case _Z_MID_T_INIT: printf("Init message"); break; - case _Z_MID_OPEN: + case _Z_MID_T_OPEN: printf("Open message"); break; - case _Z_MID_CLOSE: + case _Z_MID_T_CLOSE: printf("Close message"); break; - case _Z_MID_KEEP_ALIVE: + case _Z_MID_T_KEEP_ALIVE: printf("KeepAlive message"); break; - case _Z_MID_FRAME: + case _Z_MID_T_FRAME: printf("Frame message"); break; - case _Z_MID_FRAGMENT: + case _Z_MID_T_FRAGMENT: printf("Frame message"); break; default: @@ -839,7 +839,7 @@ void print_attachment(_z_attachment_t *att) { _z_attachment_t *gen_attachment(void) { _z_attachment_t *p_at = (_z_attachment_t *)z_malloc(sizeof(_z_attachment_t)); - p_at->_header = _Z_MID_ATTACHMENT; + p_at->_header = _Z_MID_A_ATTACHMENT; // _Z_SET_FLAG(p_at->_header, _Z_FLAGS(gen_uint8())); p_at->_payload = gen_payload(64); @@ -1429,7 +1429,7 @@ void declare_message(void) { // Initialize _z_zenoh_message_t z_msg = gen_declare_message(); - assert(_Z_MID(z_msg._header) == _Z_MID_DECLARE); + assert(_Z_MID(z_msg._header) == _Z_MID_Z_DECLARE); _z_msg_declare_t e_dcl = z_msg._body._declare; @@ -1496,7 +1496,7 @@ void data_message(void) { // Initialize _z_zenoh_message_t z_msg = gen_data_message(); - assert(_Z_MID(z_msg._header) == _Z_MID_DATA); + assert(_Z_MID(z_msg._header) == _Z_MID_Z_DATA); _z_msg_data_t e_da = z_msg._body._data; @@ -1552,7 +1552,7 @@ void pull_message(void) { // Initialize _z_zenoh_message_t z_msg = gen_pull_message(); - assert(_Z_MID(z_msg._header) == _Z_MID_PULL); + assert(_Z_MID(z_msg._header) == _Z_MID_Z_PULL); _z_msg_pull_t e_pu = z_msg._body._pull; @@ -1646,7 +1646,7 @@ void query_message(void) { // Initialize uint8_t e_hdr = 0; _z_zenoh_message_t z_msg = gen_query_message(); - assert(_Z_MID(z_msg._header) == _Z_MID_QUERY); + assert(_Z_MID(z_msg._header) == _Z_MID_Z_QUERY); _z_msg_query_t e_qy = z_msg._body._query; @@ -1680,23 +1680,23 @@ _z_zenoh_message_t gen_unit_message(void) { _z_zenoh_message_t gen_zenoh_message(void) { _z_zenoh_message_t p_zm; - uint8_t mids[] = {_Z_MID_DECLARE, _Z_MID_DATA, _Z_MID_PULL, _Z_MID_QUERY, _Z_MID_UNIT}; + uint8_t mids[] = {_Z_MID_Z_DECLARE, _Z_MID_Z_DATA, _Z_MID_Z_PULL, _Z_MID_Z_QUERY, _Z_MID_Z_UNIT}; uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); switch (mids[i]) { - case _Z_MID_DECLARE: + case _Z_MID_Z_DECLARE: p_zm = gen_declare_message(); break; - case _Z_MID_DATA: + case _Z_MID_Z_DATA: p_zm = gen_data_message(); break; - case _Z_MID_PULL: + case _Z_MID_Z_PULL: p_zm = gen_pull_message(); break; - case _Z_MID_QUERY: + case _Z_MID_Z_QUERY: p_zm = gen_query_message(); break; - case _Z_MID_UNIT: + case _Z_MID_Z_UNIT: p_zm = gen_unit_message(); break; default: @@ -1743,19 +1743,19 @@ void assert_eq_zenoh_message(_z_zenoh_message_t *left, _z_zenoh_message_t *right printf("\n"); switch (_Z_MID(left->_header)) { - case _Z_MID_DECLARE: + case _Z_MID_Z_DECLARE: assert_eq_declare_message(&left->_body._declare, &right->_body._declare); break; - case _Z_MID_DATA: + case _Z_MID_Z_DATA: assert_eq_data_message(&left->_body._data, &right->_body._data, left->_header); break; - case _Z_MID_PULL: + case _Z_MID_Z_PULL: assert_eq_pull_message(&left->_body._pull, &right->_body._pull, left->_header); break; - case _Z_MID_QUERY: + case _Z_MID_Z_QUERY: assert_eq_query_message(&left->_body._query, &right->_body._query, left->_header); break; - case _Z_MID_UNIT: + case _Z_MID_Z_UNIT: // Do nothing. Unit messages have no body break; default: @@ -1773,19 +1773,19 @@ void zenoh_message(void) { printf(" - "); switch (_Z_MID(e_zm._header)) { - case _Z_MID_DECLARE: + case _Z_MID_Z_DECLARE: printf("Declare message"); break; - case _Z_MID_DATA: + case _Z_MID_Z_DATA: printf("Data message"); break; - case _Z_MID_PULL: + case _Z_MID_Z_PULL: printf("Pull message"); break; - case _Z_MID_QUERY: + case _Z_MID_Z_QUERY: printf("Query message"); break; - case _Z_MID_UNIT: + case _Z_MID_Z_UNIT: printf("Unit message"); break; default: @@ -1905,7 +1905,7 @@ void assert_eq_hello_message(_z_s_msg_hello_t *left, _z_s_msg_hello_t *right, ui assert_eq_uint8_array(&left->_zid, &right->_zid); printf("\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_HELLO_L) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true) { printf(" "); assert_eq_locator_array(&left->_locators, &right->_locators); printf("\n"); @@ -1993,7 +1993,7 @@ void assert_eq_join_message(_z_t_msg_join_t *left, _z_t_msg_join_t *right, uint8 assert_eq_uint8_array(&left->_zid, &right->_zid); printf("\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_JOIN_S) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S) == true) { printf(" SN Resolution (%hhu:%hhu)", left->_seq_num_res, right->_seq_num_res); assert(left->_seq_num_res == right->_seq_num_res); printf("\n"); @@ -2046,7 +2046,7 @@ void join_message(void) { // Initialize _z_transport_message_t t_msg = gen_join_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_JOIN); + assert(_Z_MID(t_msg._header) == _Z_MID_T_JOIN); _z_t_msg_join_t e_it = t_msg._body._join; @@ -2115,7 +2115,7 @@ void assert_eq_init_message(_z_t_msg_init_t *left, _z_t_msg_init_t *right, uint8 assert_eq_uint8_array(&left->_zid, &right->_zid); printf("\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_S) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_S) == true) { printf(" SN Resolution (%hhu:%hhu)", left->_seq_num_res, right->_seq_num_res); assert(left->_seq_num_res == right->_seq_num_res); printf("\n"); @@ -2133,7 +2133,7 @@ void assert_eq_init_message(_z_t_msg_init_t *left, _z_t_msg_init_t *right, uint8 printf("\n"); } - if (_Z_HAS_FLAG(header, _Z_FLAG_INIT_A) == true) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_A) == true) { printf(" "); assert_eq_uint8_array(&left->_cookie, &right->_cookie); printf("\n"); @@ -2146,7 +2146,7 @@ void init_message(void) { // Initialize _z_transport_message_t t_msg = gen_init_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_INIT); + assert(_Z_MID(t_msg._header) == _Z_MID_T_INIT); _z_t_msg_init_t e_it = t_msg._body._init; @@ -2192,7 +2192,7 @@ void assert_eq_open_message(_z_t_msg_open_t *left, _z_t_msg_open_t *right, uint8 assert(left->_initial_sn == right->_initial_sn); printf("\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_OPEN_A) == false) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_A) == false) { printf(" "); assert_eq_uint8_array(&left->_cookie, &right->_cookie); printf("\n"); @@ -2205,7 +2205,7 @@ void open_message(void) { // Initialize _z_transport_message_t t_msg = gen_open_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_OPEN); + assert(_Z_MID(t_msg._header) == _Z_MID_T_OPEN); _z_t_msg_open_t e_op = t_msg._body._open; @@ -2251,7 +2251,7 @@ void close_message(void) { // Initialize _z_transport_message_t t_msg = gen_close_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_CLOSE); + assert(_Z_MID(t_msg._header) == _Z_MID_T_CLOSE); _z_t_msg_close_t e_cl = t_msg._body._close; @@ -2291,7 +2291,7 @@ void keep_alive_message(void) { // Initialize _z_transport_message_t t_msg = gen_keep_alive_message(); _z_t_msg_keep_alive_t e_ka = t_msg._body._keep_alive; - assert(_Z_MID(t_msg._header) == _Z_MID_KEEP_ALIVE); + assert(_Z_MID(t_msg._header) == _Z_MID_T_KEEP_ALIVE); // Encode int8_t res = _z_keep_alive_encode(&wbf, t_msg._header, &e_ka); @@ -2353,7 +2353,7 @@ void frame_message(void) { // Initialize _z_transport_message_t t_msg = gen_frame_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_FRAME); + assert(_Z_MID(t_msg._header) == _Z_MID_T_FRAME); _z_t_msg_frame_t e_fr = t_msg._body._frame; @@ -2407,7 +2407,7 @@ void fragment_message(void) { // Initialize _z_transport_message_t t_msg = gen_fragment_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_FRAGMENT); + assert(_Z_MID(t_msg._header) == _Z_MID_T_FRAGMENT); _z_t_msg_fragment_t e_fr = t_msg._body._fragment; @@ -2439,30 +2439,30 @@ void fragment_message(void) { _z_transport_message_t gen_transport_message(void) { _z_transport_message_t e_tm; - uint8_t mids[] = {_Z_MID_JOIN, _Z_MID_INIT, _Z_MID_OPEN, _Z_MID_CLOSE, - _Z_MID_KEEP_ALIVE, _Z_MID_FRAME, _Z_MID_FRAGMENT}; + uint8_t mids[] = {_Z_MID_T_JOIN, _Z_MID_T_INIT, _Z_MID_T_OPEN, _Z_MID_T_CLOSE, + _Z_MID_T_KEEP_ALIVE, _Z_MID_T_FRAME, _Z_MID_T_FRAGMENT}; uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); switch (mids[i]) { - case _Z_MID_JOIN: + case _Z_MID_T_JOIN: e_tm = gen_join_message(); break; - case _Z_MID_INIT: + case _Z_MID_T_INIT: e_tm = gen_init_message(); break; - case _Z_MID_OPEN: + case _Z_MID_T_OPEN: e_tm = gen_open_message(); break; - case _Z_MID_CLOSE: + case _Z_MID_T_CLOSE: e_tm = gen_close_message(); break; - case _Z_MID_KEEP_ALIVE: + case _Z_MID_T_KEEP_ALIVE: e_tm = gen_keep_alive_message(); break; - case _Z_MID_FRAME: + case _Z_MID_T_FRAME: e_tm = gen_frame_message(); break; - case _Z_MID_FRAGMENT: + case _Z_MID_T_FRAGMENT: e_tm = gen_fragment_message(); break; default: @@ -2500,25 +2500,25 @@ void assert_eq_transport_message(_z_transport_message_t *left, _z_transport_mess printf("\n"); switch (_Z_MID(left->_header)) { - case _Z_MID_JOIN: + case _Z_MID_T_JOIN: assert_eq_join_message(&left->_body._join, &right->_body._join, left->_header); break; - case _Z_MID_INIT: + case _Z_MID_T_INIT: assert_eq_init_message(&left->_body._init, &right->_body._init, left->_header); break; - case _Z_MID_OPEN: + case _Z_MID_T_OPEN: assert_eq_open_message(&left->_body._open, &right->_body._open, left->_header); break; - case _Z_MID_CLOSE: + case _Z_MID_T_CLOSE: assert_eq_close_message(&left->_body._close, &right->_body._close, left->_header); break; - case _Z_MID_KEEP_ALIVE: + case _Z_MID_T_KEEP_ALIVE: assert_eq_keep_alive_message(&left->_body._keep_alive, &right->_body._keep_alive, left->_header); break; - case _Z_MID_FRAME: + case _Z_MID_T_FRAME: assert_eq_frame_message(&left->_body._frame, &right->_body._frame, left->_header); break; - case _Z_MID_FRAGMENT: + case _Z_MID_T_FRAGMENT: assert_eq_fragment_message(&left->_body._fragment, &right->_body._fragment, left->_header); break; default: @@ -2729,11 +2729,11 @@ void batch(void) { // // Create the frame session message that carries the zenoh message // _z_transport_message_t t_msg; // t_msg._attachment = NULL; -// t_msg._header = _Z_MID_FRAME; +// t_msg._header = _Z_MID_T_FRAME; // t_msg._body._frame._sn = sn; // if (is_reliable == true) { -// _Z_SET_FLAG(t_msg._header, _Z_FLAG_FRAGMENT_R); +// _Z_SET_FLAG(t_msg._header, _Z_FLAG_T_FRAGMENT_R); // }; // if (is_fragment == true) { From f00a79a0dbd33f875eca5c90fbcc411955c3df7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 29 Mar 2023 11:32:33 +0200 Subject: [PATCH 060/181] Remove unused flags --- include/zenoh-pico/protocol/msg.h | 4 ---- src/protocol/msgcodec.c | 2 ++ tests/z_msgcodec_test.c | 1 + 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index cfdc82660..404ade8ef 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -150,10 +150,6 @@ #define _Z_FLAG_Z_T 0x20 // 1 << 5 | QueryTarget if T==1 then the query target is present #define _Z_FLAG_Z_X 0x00 // Unused flags are set to zero -/* Init option flags */ -#define _Z_OPT_INIT_QOS 0x01 // 1 << 0 | QoS if QOS==1 then the session supports QoS -#define _Z_OPT_JOIN_QOS 0x01 // 1 << 0 | QoS if QOS==1 then the session supports QoS - /*=============================*/ /* Message header */ /*=============================*/ diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 50f0ab11b..07d6463fd 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1118,6 +1118,7 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg _Z_EC(_z_zint_encode(wbf, msg->_lease)) } + // TODO[protocol] // if (_Z_HAS_FLAG(msg->_options, _Z_OPT_JOIN_QOS) == true) { // for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { // _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._qos[i]._reliable)) @@ -1170,6 +1171,7 @@ int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { msg->_lease = msg->_lease * 1000; } + // TODO[protocol] // if (_Z_HAS_FLAG(msg->_options, _Z_OPT_JOIN_QOS) == true) { // for (uint8_t i = 0; (ret == _Z_RES_OK) && (i < Z_PRIORITIES_NUM); i++) { // ret |= _z_zint_decode(&msg->_next_sn._val._qos[i]._reliable, zbf); diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index eb3bdad6b..44b25cf75 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -2016,6 +2016,7 @@ void assert_eq_join_message(_z_t_msg_join_t *left, _z_t_msg_join_t *right, uint8 printf("\n"); printf(" Next SNs: "); + // TODO[protocol] // if (_Z_HAS_FLAG(left->_options, _Z_OPT_JOIN_QOS) == true) { // assert(left->_next_sn._is_qos == true); // assert(right->_next_sn._is_qos == true); From 784c042d4d8019891aa4d27aa17bdeb50b930ca8 Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Thu, 30 Mar 2023 10:16:20 +0200 Subject: [PATCH 061/181] User callback can take ownership on reply or hello (#184) * user callback can take ownership on reply or hello * compilation fix * compilation fix * single _pop function * part of remarks fixed * correct return on out of memory * restored reply_t as field, cleanup in new * restored incorrectly moved lines * rename to alloc_and_move --- include/zenoh-pico/collections/list.h | 6 ++++-- src/api/api.c | 1 + src/collections/list.c | 10 +++++++--- src/net/primitives.c | 6 +++--- src/session/query.c | 13 +++++++++++-- tests/z_api_alignment_test.c | 3 ++- tests/z_client_test.c | 6 +++--- tests/z_data_struct_test.c | 2 +- tests/z_peer_multicast_test.c | 2 +- 9 files changed, 33 insertions(+), 16 deletions(-) diff --git a/include/zenoh-pico/collections/list.h b/include/zenoh-pico/collections/list.h index eb0c05f16..b2b913175 100644 --- a/include/zenoh-pico/collections/list.h +++ b/include/zenoh-pico/collections/list.h @@ -42,7 +42,7 @@ void *_z_list_head(const _z_list_t *xs); _z_list_t *_z_list_tail(const _z_list_t *xs); _z_list_t *_z_list_push(_z_list_t *xs, void *x); -_z_list_t *_z_list_pop(_z_list_t *xs, z_element_free_f f_f); +_z_list_t *_z_list_pop(_z_list_t *xs, z_element_free_f f_f, void **x); _z_list_t *_z_list_find(const _z_list_t *xs, z_element_eq_f f_f, void *e); @@ -59,7 +59,9 @@ void _z_list_free(_z_list_t **xs, z_element_free_f f_f); static inline type *name##_list_head(const name##_list_t *l) { return (type *)_z_list_head(l); } \ static inline name##_list_t *name##_list_tail(const name##_list_t *l) { return _z_list_tail(l); } \ static inline name##_list_t *name##_list_push(name##_list_t *l, type *e) { return _z_list_push(l, e); } \ - static inline name##_list_t *name##_list_pop(name##_list_t *l) { return _z_list_pop(l, name##_elem_free); } \ + static inline name##_list_t *name##_list_pop(name##_list_t *l, type** x) { \ + return _z_list_pop(l, name##_elem_free, (void**)x); \ + } \ static inline name##_list_t *name##_list_find(const name##_list_t *l, name##_eq_f c_f, type *e) { \ return _z_list_find(l, (z_element_eq_f)c_f, e); \ } \ diff --git a/src/api/api.c b/src/api/api.c index be0751f4c..c48113b87 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -615,6 +615,7 @@ void __z_reply_handler(_z_reply_t *reply, __z_reply_handler_wrapper_t *wrapped_c z_owned_reply_t oreply = {._value = reply}; wrapped_ctx->user_call(&oreply, wrapped_ctx->ctx); + z_reply_drop(&oreply); // user_call is allowed to take ownership of the reply by setting oreply._value to NULL } int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owned_closure_reply_t *callback, diff --git a/src/collections/list.c b/src/collections/list.c index f6941bd89..c3d4f8deb 100644 --- a/src/collections/list.c +++ b/src/collections/list.c @@ -48,11 +48,15 @@ size_t _z_list_len(const _z_list_t *xs) { _Bool _z_list_is_empty(const _z_list_t *xs) { return _z_list_len(xs) == (size_t)0; } -_z_list_t *_z_list_pop(_z_list_t *xs, z_element_free_f f_f) { +_z_list_t *_z_list_pop(_z_list_t *xs, z_element_free_f f_f, void **x) { _z_list_t *l = (_z_list_t *)xs; _z_list_t *head = xs; l = head->_tail; - f_f(&head->_val); + if (x != NULL) { + *x = head->_val; + } else { + f_f(&head->_val); + } z_free(head); return l; @@ -126,7 +130,7 @@ _z_list_t *_z_list_clone(const _z_list_t *xs, z_element_clone_f d_f) { void _z_list_free(_z_list_t **xs, z_element_free_f f) { _z_list_t *ptr = *xs; while (ptr != NULL) { - ptr = _z_list_pop(ptr, f); + ptr = _z_list_pop(ptr, f, NULL); } *xs = NULL; diff --git a/src/net/primitives.c b/src/net/primitives.c index a6019e9bb..c029512ed 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -31,9 +31,9 @@ void _z_scout(const z_whatami_t what, const char *locator, const uint32_t timeou _z_hello_list_t *hellos = _z_scout_inner(what, locator, timeout, false); while (hellos != NULL) { - _z_hello_t *hello = _z_hello_list_head(hellos); - (*callback)(hello, arg_call); - hellos = _z_hello_list_pop(hellos); + _z_hello_t *hello = NULL; + hellos = _z_hello_list_pop(hellos, &hello); + (*callback)(hello, arg_call); // callback takes ownership of hello } if (dropper != NULL) { diff --git a/src/session/query.c b/src/session/query.c index aae6d3cee..e5845c579 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -22,6 +22,15 @@ #include "zenoh-pico/session/resource.h" #include "zenoh-pico/utils/logging.h" +_z_reply_t *_z_reply_alloc_and_move(_z_reply_t *_reply) { + _z_reply_t *reply = (_z_reply_t *)z_malloc(sizeof(_z_reply_t)); + if (reply != NULL) { + *reply = *_reply; + (void)memset(_reply, 0, sizeof(_z_reply_t)); + } + return reply; +} + void _z_reply_clear(_z_reply_t *reply) { _z_reply_data_clear(&reply->data); } void _z_reply_free(_z_reply_t **reply) { @@ -252,8 +261,8 @@ int8_t _z_trigger_query_reply_final(_z_session_t *zn, const _z_reply_context_t * _z_pending_reply_t *pen_rep = _z_pending_reply_list_head(pen_qry->_pending_replies); // Trigger the query handler - pen_qry->_callback(&pen_rep->_reply, pen_qry->_call_arg); - pen_qry->_pending_replies = _z_pending_reply_list_pop(pen_qry->_pending_replies); + pen_qry->_callback(_z_reply_alloc_and_move(&pen_rep->_reply), pen_qry->_call_arg); + pen_qry->_pending_replies = _z_pending_reply_list_pop(pen_qry->_pending_replies, NULL); } } diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 7f44326be..0f77a436c 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -41,9 +41,9 @@ void zid_handler(const z_id_t *id, void *arg) { volatile unsigned int hellos = 0; void hello_handler(z_owned_hello_t *hello, void *arg) { (void)(arg); - (void)(hello); hellos++; z_hello_null(); + z_drop(hello); // validate double-drop safety: caller drops hello if it's not dropped by the handler } volatile unsigned int queries = 0; @@ -87,6 +87,7 @@ void reply_handler(z_owned_reply_t *reply, void *arg) { } z_reply_null(); // Does nothing. Just to test compilation + z_drop(reply); // validate double-drop safety: caller drops reply if it's not dropped by the handler } volatile unsigned int datas = 0; diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 64fcbeca6..f226c1962 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -267,7 +267,7 @@ int main(int argc, char **argv) { z_owned_publisher_t *pub = _z_list_head(pubs1); printf("Undeclared publisher on session 2: %zu\n", z_loan(*pub)._val->_id); z_undeclare_publisher(z_move(*pub)); - pubs1 = _z_list_pop(pubs1, _z_noop_elem_free); + pubs1 = _z_list_pop(pubs1, _z_noop_elem_free, NULL); } z_sleep_s(SLEEP); @@ -277,7 +277,7 @@ int main(int argc, char **argv) { z_owned_subscriber_t *sub = _z_list_head(subs2); printf("Undeclared subscriber on session 2: %zu\n", sub->_value->_id); z_undeclare_subscriber(z_move(*sub)); - subs2 = _z_list_pop(subs2, _z_noop_elem_free); + subs2 = _z_list_pop(subs2, _z_noop_elem_free, NULL); } z_sleep_s(SLEEP); @@ -286,7 +286,7 @@ int main(int argc, char **argv) { z_owned_queryable_t *qle = _z_list_head(qles2); printf("Undeclared queryable on session 2: %zu\n", qle->_value->_id); z_undeclare_queryable(z_move(*qle)); - qles2 = _z_list_pop(qles2, _z_noop_elem_free); + qles2 = _z_list_pop(qles2, _z_noop_elem_free, NULL); } z_sleep_s(SLEEP); diff --git a/tests/z_data_struct_test.c b/tests/z_data_struct_test.c index 2936dd24e..cc88e4a39 100644 --- a/tests/z_data_struct_test.c +++ b/tests/z_data_struct_test.c @@ -69,7 +69,7 @@ int main(void) { for (size_t i = 0; i < len; i++) { snprintf(s, 64, "%zu", i); - list = _z_str_list_pop(list); + list = _z_str_list_pop(list, NULL); assert(_z_str_list_len(list) == len - (i + 1)); } assert(_z_str_list_is_empty(list) == true); diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 5658cff13..3c5d29a0a 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -147,7 +147,7 @@ int main(int argc, char **argv) { z_owned_subscriber_t *sub = _z_list_head(subs2); printf("Undeclared subscriber on session 2: %zu\n", z_subscriber_loan(sub)._val->_id); z_undeclare_subscriber(z_move(*sub)); - subs2 = _z_list_pop(subs2, _z_noop_elem_free); + subs2 = _z_list_pop(subs2, _z_noop_elem_free, NULL); } z_sleep_s(SLEEP); From 7efc08cfb31a902cc4643e385a4dee161be7e671 Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Thu, 30 Mar 2023 16:35:00 +0200 Subject: [PATCH 062/181] Fix task stop immediately after start condition (#188) --- CMakeLists.txt | 4 ++ src/net/session.c | 8 ++++ src/transport/multicast/link/task/lease.c | 1 - src/transport/multicast/link/task/read.c | 2 - src/transport/unicast/link/task/lease.c | 1 - src/transport/unicast/link/task/read.c | 1 - tests/z_session_test.c | 53 +++++++++++++++++++++++ 7 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 tests/z_session_test.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 015b3da34..74a12f7f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -307,8 +307,11 @@ if(UNIX OR MSVC) if(CMAKE_C_STANDARD MATCHES "11") add_executable(z_client_test ${PROJECT_SOURCE_DIR}/tests/z_client_test.c) add_executable(z_api_alignment_test ${PROJECT_SOURCE_DIR}/tests/z_api_alignment_test.c) + add_executable(z_session_test ${PROJECT_SOURCE_DIR}/tests/z_session_test.c) + target_link_libraries(z_client_test ${Libname}) target_link_libraries(z_api_alignment_test ${Libname}) + target_link_libraries(z_session_test ${Libname}) configure_file(${PROJECT_SOURCE_DIR}/tests/routed.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh COPYONLY) configure_file(${PROJECT_SOURCE_DIR}/tests/api.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh COPYONLY) @@ -316,6 +319,7 @@ if(UNIX OR MSVC) enable_testing() add_test(z_client_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh z_client_test) add_test(z_api_alignment_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh z_api_alignment_test) + add_test(z_session_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh z_session_test) endif() endif() endif() diff --git a/src/net/session.c b/src/net/session.c index 23ebf5078..e394302eb 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -173,7 +173,9 @@ int8_t _zp_start_read_task(_z_session_t *zn) { #if Z_UNICAST_TRANSPORT == 1 if (zn->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { zn->_tp._transport._unicast._read_task = task; + zn->_tp._transport._unicast._read_task_running = true; if (_z_task_init(task, NULL, _zp_unicast_read_task, &zn->_tp._transport._unicast) != _Z_RES_OK) { + zn->_tp._transport._unicast._read_task_running = false; ret = _Z_ERR_SYSTEM_TASK_FAILED; z_free(task); } @@ -182,7 +184,9 @@ int8_t _zp_start_read_task(_z_session_t *zn) { #if Z_MULTICAST_TRANSPORT == 1 if (zn->_tp._type == _Z_TRANSPORT_MULTICAST_TYPE) { zn->_tp._transport._multicast._read_task = task; + zn->_tp._transport._multicast._read_task_running = true; if (_z_task_init(task, NULL, _zp_multicast_read_task, &zn->_tp._transport._multicast) != _Z_RES_OK) { + zn->_tp._transport._multicast._read_task_running = false; ret = _Z_ERR_SYSTEM_TASK_FAILED; z_free(task); } @@ -227,7 +231,9 @@ int8_t _zp_start_lease_task(_z_session_t *zn) { #if Z_UNICAST_TRANSPORT == 1 if (zn->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { zn->_tp._transport._unicast._lease_task = task; + zn->_tp._transport._unicast._lease_task_running = true; if (_z_task_init(task, NULL, _zp_unicast_lease_task, &zn->_tp._transport._unicast) != _Z_RES_OK) { + zn->_tp._transport._unicast._lease_task_running = false; ret = _Z_ERR_SYSTEM_TASK_FAILED; z_free(task); } @@ -236,7 +242,9 @@ int8_t _zp_start_lease_task(_z_session_t *zn) { #if Z_MULTICAST_TRANSPORT == 1 if (zn->_tp._type == _Z_TRANSPORT_MULTICAST_TYPE) { zn->_tp._transport._multicast._lease_task = task; + zn->_tp._transport._multicast._lease_task_running = true; if (_z_task_init(task, NULL, _zp_multicast_lease_task, &zn->_tp._transport._multicast) != _Z_RES_OK) { + zn->_tp._transport._multicast._lease_task_running = false; ret = _Z_ERR_SYSTEM_TASK_FAILED; z_free(task); } diff --git a/src/transport/multicast/link/task/lease.c b/src/transport/multicast/link/task/lease.c index 2bec9330f..6edac4c22 100644 --- a/src/transport/multicast/link/task/lease.c +++ b/src/transport/multicast/link/task/lease.c @@ -73,7 +73,6 @@ int8_t _zp_multicast_send_keep_alive(_z_transport_multicast_t *ztm) { void *_zp_multicast_lease_task(void *ztm_arg) { #if Z_MULTI_THREAD == 1 _z_transport_multicast_t *ztm = (_z_transport_multicast_t *)ztm_arg; - ztm->_lease_task_running = true; ztm->_transmitted = false; // From all peers, get the next lease time (minimum) diff --git a/src/transport/multicast/link/task/read.c b/src/transport/multicast/link/task/read.c index 36b8b7a0f..3100b5f86 100644 --- a/src/transport/multicast/link/task/read.c +++ b/src/transport/multicast/link/task/read.c @@ -40,8 +40,6 @@ void *_zp_multicast_read_task(void *ztm_arg) { #if Z_MULTI_THREAD == 1 _z_transport_multicast_t *ztm = (_z_transport_multicast_t *)ztm_arg; - ztm->_read_task_running = true; - // Acquire and keep the lock _z_mutex_lock(&ztm->_mutex_rx); diff --git a/src/transport/unicast/link/task/lease.c b/src/transport/unicast/link/task/lease.c index 933d9aa52..57aabadfa 100644 --- a/src/transport/unicast/link/task/lease.c +++ b/src/transport/unicast/link/task/lease.c @@ -36,7 +36,6 @@ void *_zp_unicast_lease_task(void *ztu_arg) { #if Z_MULTI_THREAD == 1 _z_transport_unicast_t *ztu = (_z_transport_unicast_t *)ztu_arg; - ztu->_lease_task_running = true; ztu->_received = false; ztu->_transmitted = false; diff --git a/src/transport/unicast/link/task/read.c b/src/transport/unicast/link/task/read.c index 1046ba5e0..7f0cc14c7 100644 --- a/src/transport/unicast/link/task/read.c +++ b/src/transport/unicast/link/task/read.c @@ -38,7 +38,6 @@ int8_t _zp_unicast_read(_z_transport_unicast_t *ztu) { void *_zp_unicast_read_task(void *ztu_arg) { #if Z_MULTI_THREAD == 1 _z_transport_unicast_t *ztu = (_z_transport_unicast_t *)ztu_arg; - ztu->_read_task_running = true; uint8_t ret; diff --git a/tests/z_session_test.c b/tests/z_session_test.c new file mode 100644 index 000000000..29d0ba6e7 --- /dev/null +++ b/tests/z_session_test.c @@ -0,0 +1,53 @@ +// +// 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, +// + +#include +#include +#include + +#include "zenoh-pico.h" + +#undef NDEBUG +#include + +int main(void) +{ + z_owned_config_t config = z_config_default(); + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make("client")); + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) + { + printf("Unable to open session!\n"); + return -1; + } + + // Start read and lease tasks for zenoh-pico + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) + { + printf("Unable to start read and lease tasks"); + return -1; + } + + // Commented out wait for 1 second. Stopping should work without it. + // z_sleep_ms(1000); + + // Stop read and lease tasks for zenoh-pico + zp_stop_read_task(z_loan(s)); + zp_stop_lease_task(z_loan(s)); + + // Immediately close the session + z_close(&s); +} From 7d11b591aa0b7208c1e232d817e6580f825f707a Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Thu, 30 Mar 2023 17:06:18 +0200 Subject: [PATCH 063/181] Add .clang-format (#187) --- .clang-format | 3 +++ .gitignore | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..a777cf13a --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +BasedOnStyle: Google +IndentWidth: 4 +ColumnLimit: 120 diff --git a/.gitignore b/.gitignore index 19b508559..11d4de96f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ build # vscode .vscode -.clang-format # IntelliJ / CLion .idea From 114b5781a037c0dd1ef8561eee177a4df3ab3da4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 30 Mar 2023 18:20:19 +0200 Subject: [PATCH 064/181] Fix queries in multicast (#186) * Fix queries in multicast * Fix C99 config loan usage --- examples/unix/c11/z_get.c | 9 +++++++-- examples/unix/c11/z_queryable.c | 15 ++++++++++----- examples/unix/c99/z_get.c | 9 +++++++-- examples/unix/c99/z_queryable.c | 9 +++++++-- src/api/api.c | 20 ++++++++++++++++---- 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index 2262867a4..1bff89070 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -37,11 +37,12 @@ void reply_handler(z_owned_reply_t *reply, void *ctx) { int main(int argc, char **argv) { const char *keyexpr = "demo/example/**"; + const char *mode = "client"; const char *locator = NULL; const char *value = NULL; int opt; - while ((opt = getopt(argc, argv, "k:e:v:")) != -1) { + while ((opt = getopt(argc, argv, "k:e:m:v:")) != -1) { switch (opt) { case 'k': keyexpr = optarg; @@ -49,11 +50,14 @@ int main(int argc, char **argv) { case 'e': locator = optarg; break; + case 'm': + mode = optarg; + break; case 'v': value = optarg; break; case '?': - if (optopt == 'k' || optopt == 'e' || optopt == 'v') { + if (optopt == 'k' || optopt == 'e' || optopt == 'm' || optopt == 'v') { fprintf(stderr, "Option -%c requires an argument.\n", optopt); } else { fprintf(stderr, "Unknown option `-%c'.\n", optopt); @@ -65,6 +69,7 @@ int main(int argc, char **argv) { } z_owned_config_t config = z_config_default(); + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); } diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index 37076f3a0..d8e067b46 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -37,22 +37,26 @@ void query_handler(const z_query_t *query, void *ctx) { } int main(int argc, char **argv) { + const char *mode = "client"; char *locator = NULL; int opt; - while ((opt = getopt(argc, argv, "k:v:e:")) != -1) { + while ((opt = getopt(argc, argv, "k:e:m:v:")) != -1) { switch (opt) { case 'k': keyexpr = optarg; break; - case 'v': - value = optarg; - break; case 'e': locator = optarg; break; + case 'm': + mode = optarg; + break; + case 'v': + value = optarg; + break; case '?': - if (optopt == 'k' || optopt == 'v' || optopt == 'e') { + if (optopt == 'k' || optopt == 'e' || optopt == 'm' || optopt == 'v') { fprintf(stderr, "Option -%c requires an argument.\n", optopt); } else { fprintf(stderr, "Unknown option `-%c'.\n", optopt); @@ -64,6 +68,7 @@ int main(int argc, char **argv) { } z_owned_config_t config = z_config_default(); + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); } diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 1fa6e2aa5..0915b783b 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -37,11 +37,12 @@ void reply_handler(z_owned_reply_t *reply, void *ctx) { int main(int argc, char **argv) { const char *keyexpr = "demo/example/**"; + const char *mode = "client"; const char *locator = NULL; const char *value = NULL; int opt; - while ((opt = getopt(argc, argv, "k:e:v:")) != -1) { + while ((opt = getopt(argc, argv, "k:e:m:v:")) != -1) { switch (opt) { case 'k': keyexpr = optarg; @@ -49,11 +50,14 @@ int main(int argc, char **argv) { case 'e': locator = optarg; break; + case 'm': + mode = optarg; + break; case 'v': value = optarg; break; case '?': - if (optopt == 'k' || optopt == 'e') { + if (optopt == 'k' || optopt == 'e' || optopt == 'm' || optopt == 'v') { fprintf(stderr, "Option -%c requires an argument.\n", optopt); } else { fprintf(stderr, "Unknown option `-%c'.\n", optopt); @@ -65,6 +69,7 @@ int main(int argc, char **argv) { } z_owned_config_t config = z_config_default(); + zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(locator)); } diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index 1a40967c4..58ddb2b3c 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -33,10 +33,11 @@ void query_handler(const z_query_t *query, void *ctx) { } int main(int argc, char **argv) { + const char *mode = "client"; char *locator = NULL; int opt; - while ((opt = getopt(argc, argv, "k:v:e:")) != -1) { + while ((opt = getopt(argc, argv, "k:e:m:v:")) != -1) { switch (opt) { case 'k': keyexpr = optarg; @@ -44,11 +45,14 @@ int main(int argc, char **argv) { case 'v': value = optarg; break; + case 'm': + mode = optarg; + break; case 'e': locator = optarg; break; case '?': - if (optopt == 'k' || optopt == 'v' || optopt == 'e') { + if (optopt == 'k' || optopt == 'e' || optopt == 'm' || optopt == 'v') { fprintf(stderr, "Option -%c requires an argument.\n", optopt); } else { fprintf(stderr, "Unknown option `-%c'.\n", optopt); @@ -60,6 +64,7 @@ int main(int argc, char **argv) { } z_owned_config_t config = z_config_default(); + zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(locator)); } diff --git a/src/api/api.c b/src/api/api.c index c48113b87..d98faeda8 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -775,11 +775,13 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z // resource declarations are only performed on unicast transports. #if Z_MULTICAST_TRANSPORT == 1 if (zs._val->_tp._type != _Z_TRANSPORT_MULTICAST_TYPE) { +#endif // Z_MULTICAST_TRANSPORT == 1 _z_resource_t *r = _z_get_resource_by_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); if (r == NULL) { _z_zint_t id = _z_declare_resource(zs._val, keyexpr); key = _z_rid_with_suffix(id, NULL); } +#if Z_MULTICAST_TRANSPORT == 1 } #endif // Z_MULTICAST_TRANSPORT == 1 @@ -846,11 +848,21 @@ z_owned_queryable_t z_declare_queryable(z_session_t zs, z_keyexpr_t keyexpr, z_o callback->context = NULL; z_keyexpr_t key = keyexpr; - _z_resource_t *r = _z_get_resource_by_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); - if (r == NULL) { - _z_zint_t id = _z_declare_resource(zs._val, keyexpr); - key = _z_rid_with_suffix(id, NULL); + + // TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition + // lacks a way to convey them to later-joining nodes. Thus, in the current version automatic + // resource declarations are only performed on unicast transports. +#if Z_MULTICAST_TRANSPORT == 1 + if (zs._val->_tp._type != _Z_TRANSPORT_MULTICAST_TYPE) { +#endif // Z_MULTICAST_TRANSPORT == 1 + _z_resource_t *r = _z_get_resource_by_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); + if (r == NULL) { + _z_zint_t id = _z_declare_resource(zs._val, keyexpr); + key = _z_rid_with_suffix(id, NULL); + } +#if Z_MULTICAST_TRANSPORT == 1 } +#endif // Z_MULTICAST_TRANSPORT == 1 z_queryable_options_t opt = z_queryable_options_default(); if (options != NULL) { From b352671c91073fee1842d4ceb59b315448fdcab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 29 Mar 2023 11:58:24 +0200 Subject: [PATCH 065/181] Update DECLARE message --- include/zenoh-pico/protocol/msg.h | 94 +++++--- include/zenoh-pico/protocol/msgcodec.h | 13 +- include/zenoh-pico/session/utils.h | 6 +- include/zenoh-pico/transport/link/tx.h | 5 + src/net/primitives.c | 74 +++---- src/protocol/msg.c | 110 ++++++---- src/protocol/msgcodec.c | 284 ++++++++++++++----------- src/session/rx.c | 110 ---------- src/session/tx.c | 22 ++ src/transport/multicast/link/tx.c | 55 +++++ src/transport/unicast/link/tx.c | 61 ++++++ tests/z_msgcodec_test.c | 225 +++++++++++++------- 12 files changed, 617 insertions(+), 442 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 404ade8ef..84d01e712 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -64,7 +64,6 @@ #define _Z_MID_N_RESPONSE_FINAL 0x1a /* Zenoh Messages */ -#define _Z_MID_Z_DECLARE 0x0b #define _Z_MID_Z_DATA 0x0c #define _Z_MID_Z_QUERY 0x0d #define _Z_MID_Z_PULL 0x0e @@ -130,6 +129,17 @@ // Z Extensions if Z==1 then Zenoh extensions are present #define _Z_FLAG_T_CLOSE_S 0x20 // 1 << 5 +/*=============================*/ +/* Network flags */ +/*=============================*/ +#define _Z_FLAG_N_Z 0x80 // 1 << 7 + +// PUSH message flags: +// I ZenohID if I==1 then the ZenohID is present +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_N_PUSH_N 0x20 // 1 << 5 +#define _Z_FLAG_N_PUSH_M 0x40 // 1 << 6 + /* Attachment message flags */ #define _Z_FLAG_A_Z \ 0x20 // 1 << 5 | MixedSlices if Z==1 then the payload contains a mix of raw and shm_info payload @@ -312,7 +322,7 @@ typedef struct { _z_keyexpr_t _key; _z_zint_t _id; } _z_res_decl_t; -void _z_msg_clear_declaration_resource(_z_res_decl_t *dcl); +void _z_declaration_clear_resource(_z_res_decl_t *dcl); /*------------------ Forget Resource Declaration ------------------*/ // 7 6 5 4 3 2 1 0 @@ -325,7 +335,7 @@ void _z_msg_clear_declaration_resource(_z_res_decl_t *dcl); typedef struct { _z_zint_t _rid; } _z_forget_res_decl_t; -void _z_msg_clear_declaration_forget_resource(_z_forget_res_decl_t *dcl); +void _z_declaration_clear_forget_resource(_z_forget_res_decl_t *dcl); /*------------------ Publisher Declaration ------------------*/ // 7 6 5 4 3 2 1 0 @@ -338,7 +348,7 @@ void _z_msg_clear_declaration_forget_resource(_z_forget_res_decl_t *dcl); typedef struct { _z_keyexpr_t _key; } _z_pub_decl_t; -void _z_msg_clear_declaration_publisher(_z_pub_decl_t *dcl); +void _z_declaration_clear_publisher(_z_pub_decl_t *dcl); /*------------------ Forget Publisher Declaration ------------------*/ // 7 6 5 4 3 2 1 0 @@ -351,7 +361,7 @@ void _z_msg_clear_declaration_publisher(_z_pub_decl_t *dcl); typedef struct { _z_keyexpr_t _key; } _z_forget_pub_decl_t; -void _z_msg_clear_declaration_forget_publisher(_z_forget_pub_decl_t *dcl); +void _z_declaration_clear_forget_publisher(_z_forget_pub_decl_t *dcl); /*------------------ SubInfo Field ------------------*/ // 7 6 5 4 3 2 1 0 @@ -379,7 +389,7 @@ typedef struct { _z_keyexpr_t _key; _z_subinfo_t _subinfo; } _z_sub_decl_t; -void _z_msg_clear_declaration_subscriber(_z_sub_decl_t *dcl); +void _z_declaration_clear_subscriber(_z_sub_decl_t *dcl); /*------------------ Forget Subscriber Message ------------------*/ // 7 6 5 4 3 2 1 0 @@ -392,7 +402,7 @@ void _z_msg_clear_declaration_subscriber(_z_sub_decl_t *dcl); typedef struct { _z_keyexpr_t _key; } _z_forget_sub_decl_t; -void _z_msg_clear_declaration_forget_subscriber(_z_forget_sub_decl_t *dcl); +void _z_declaration_clear_forget_subscriber(_z_forget_sub_decl_t *dcl); /*------------------ Queryable Declaration ------------------*/ // 7 6 5 4 3 2 1 0 @@ -409,7 +419,7 @@ typedef struct { _z_zint_t _complete; _z_zint_t _distance; } _z_qle_decl_t; -void _z_msg_clear_declaration_queryable(_z_qle_decl_t *dcl); +void _z_declaration_clear_queryable(_z_qle_decl_t *dcl); /*------------------ Forget Queryable Declaration ------------------*/ // 7 6 5 4 3 2 1 0 @@ -422,18 +432,8 @@ void _z_msg_clear_declaration_queryable(_z_qle_decl_t *dcl); typedef struct { _z_keyexpr_t _key; } _z_forget_qle_decl_t; -void _z_msg_clear_declaration_forget_queryable(_z_forget_qle_decl_t *dcl); +void _z_declaration_clear_forget_queryable(_z_forget_qle_decl_t *dcl); -/*------------------ Declaration Message ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |X|X|X| DECLARE | -// +-+-+-+---------+ -// ~ Num of Decl ~ -// +---------------+ -// ~ [Declaration] ~ -// +---------------+ -// typedef struct { union { _z_res_decl_t _res; @@ -447,15 +447,7 @@ typedef struct { } _body; uint8_t _header; } _z_declaration_t; - -void _z_msg_clear_declaration(_z_declaration_t *dcl); -_Z_ELEM_DEFINE(_z_declaration, _z_declaration_t, _z_noop_size, _z_msg_clear_declaration, _z_noop_copy) -_Z_ARRAY_DEFINE(_z_declaration, _z_declaration_t) - -typedef struct { - _z_declaration_array_t _declarations; -} _z_msg_declare_t; -void _z_msg_clear_declare(_z_msg_declare_t *dcl); +void _z_declaration_clear(_z_declaration_t *dcl); /*------------------ Timestamp Field ------------------*/ // 7 6 5 4 3 2 1 0 @@ -588,7 +580,6 @@ void _z_msg_clear_query(_z_msg_query_t *msg); /*------------------ Zenoh Message ------------------*/ typedef union { - _z_msg_declare_t _declare; _z_msg_data_t _data; _z_msg_query_t _query; _z_msg_pull_t _pull; @@ -615,7 +606,6 @@ _z_declaration_t _z_msg_make_declaration_subscriber(_z_keyexpr_t key, _z_subinfo _z_declaration_t _z_msg_make_declaration_forget_subscriber(_z_keyexpr_t key); _z_declaration_t _z_msg_make_declaration_queryable(_z_keyexpr_t key, _z_zint_t complete, _z_zint_t distance); _z_declaration_t _z_msg_make_declaration_forget_queryable(_z_keyexpr_t key); -_z_zenoh_message_t _z_msg_make_declare(_z_declaration_array_t declarations); _z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_payload_t payload, _Bool can_be_dropped); _z_zenoh_message_t _z_msg_make_unit(_Bool can_be_dropped); _z_zenoh_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id, _z_zint_t max_samples, _Bool is_final); @@ -624,6 +614,46 @@ _z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, char *parameters, _z_zint _z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_payload_t payload, _Bool can_be_dropped, _z_reply_context_t *rctx); +/*=============================*/ +/* Network Messages */ +/*=============================*/ +/*------------------ Declaration Message ------------------*/ +// Flags: +// - X: Reserved +// - X: Reserved +// - Z: Extension if Z==1 then at least one extension is present +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|X|X| DECLARE | +// +-+-+-+---------+ +// ~ [decl_exts] ~ -- if Flag(Z)==1 +// +---------------+ +// ~ declaration ~ +// +---------------+ +// +typedef struct { + _z_declaration_t _declaration; +} _z_n_msg_declare_t; +void _z_n_msg_clear_declare(_z_n_msg_declare_t *dcl); + +/*------------------ Zenoh Message ------------------*/ +typedef union { + _z_n_msg_declare_t _declare; +} _z_network_body_t; +typedef struct { + _z_network_body_t _body; + _z_msg_ext_vec_t _extensions; + uint8_t _header; +} _z_network_message_t; +void _z_n_msg_clear(_z_network_message_t *m); +void _z_n_msg_free(_z_network_message_t **m); +_Z_ELEM_DEFINE(_z_network_message, _z_network_message_t, _z_noop_size, _z_n_msg_clear, _z_noop_copy) +_Z_VEC_DEFINE(_z_network_message, _z_network_message_t) + +/*------------------ Builders ------------------*/ +_z_network_message_t _z_n_msg_make_declare(_z_declaration_t declarations); + /*=============================*/ /* Transport Messages */ /*=============================*/ @@ -974,7 +1004,7 @@ void _z_t_msg_clear_keep_alive(_z_t_msg_keep_alive_t *msg); // - if R==1 then the FRAME is sent on the reliable channel, best-effort otherwise. // typedef struct { - _z_zenoh_message_vec_t _messages; + _z_network_message_vec_t _messages; _z_zint_t _sn; } _z_t_msg_frame_t; void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg); @@ -1035,7 +1065,7 @@ _z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial _z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial_sn); _z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only); _z_transport_message_t _z_t_msg_make_keep_alive(void); -_z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_zenoh_message_vec_t messages, _Bool is_reliable); +_z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_network_message_vec_t messages, _Bool is_reliable); _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable); _z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_payload_t messages, _Bool is_reliable, _Bool is_last); diff --git a/include/zenoh-pico/protocol/msgcodec.h b/include/zenoh-pico/protocol/msgcodec.h index b405dba6b..b1b3b42f9 100644 --- a/include/zenoh-pico/protocol/msgcodec.h +++ b/include/zenoh-pico/protocol/msgcodec.h @@ -52,6 +52,10 @@ int8_t _z_transport_message_encode(_z_wbuf_t *buf, const _z_transport_message_t int8_t _z_transport_message_decode(_z_transport_message_t *msg, _z_zbuf_t *buf); int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *buf); +int8_t _z_network_message_encode(_z_wbuf_t *buf, const _z_network_message_t *msg); +int8_t _z_network_message_decode(_z_network_message_t *msg, _z_zbuf_t *buf); +int8_t _z_network_message_decode_na(_z_network_message_t *msg, _z_zbuf_t *buf); + int8_t _z_zenoh_message_encode(_z_wbuf_t *buf, const _z_zenoh_message_t *msg); int8_t _z_zenoh_message_decode(_z_zenoh_message_t *msg, _z_zbuf_t *buf); int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *buf); @@ -122,10 +126,6 @@ int8_t _z_reply_context_encode(_z_wbuf_t *buf, const _z_reply_context_t *rc); int8_t _z_reply_context_decode(_z_reply_context_t **rc, _z_zbuf_t *buf, uint8_t header); int8_t _z_reply_context_decode_na(_z_reply_context_t *rc, _z_zbuf_t *buf, uint8_t header); -int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_msg_declare_t *msg); -int8_t _z_declare_decode(_z_msg_declare_t *msg, _z_zbuf_t *zbf); -int8_t _z_declare_decode_na(_z_msg_declare_t *msg, _z_zbuf_t *zbf); - int8_t _z_data_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_data_t *msg); int8_t _z_data_decode_na(_z_msg_data_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_data_decode(_z_msg_data_t *msg, _z_zbuf_t *zbf, uint8_t header); @@ -138,6 +138,11 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_query_t *msg int8_t _z_query_decode_na(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_query_decode(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header); +// ------------------ Network Message ------------------ +int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_n_msg_declare_t *msg); +int8_t _z_declare_decode(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf); +int8_t _z_declare_decode_na(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf); + // ------------------ Transport Message ------------------ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg); int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header); diff --git a/include/zenoh-pico/session/utils.h b/include/zenoh-pico/session/utils.h index 562a71559..b21645eb8 100644 --- a/include/zenoh-pico/session/utils.h +++ b/include/zenoh-pico/session/utils.h @@ -21,8 +21,8 @@ #include "zenoh-pico/net/session.h" /*------------------ Session ------------------*/ -_z_hello_list_t *_z_scout_inner(const z_what_t what, const _z_bytes_t zid, const char *locator, - const uint32_t timeout, const _Bool exit_on_first); +_z_hello_list_t *_z_scout_inner(const z_what_t what, const _z_bytes_t zid, const char *locator, const uint32_t timeout, + const _Bool exit_on_first); int8_t _z_session_init(_z_session_t *zn, _z_bytes_t *zid); int8_t _z_session_close(_z_session_t *zn, uint8_t reason); @@ -32,5 +32,7 @@ void _z_session_free(_z_session_t **zn); int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *z_msg); int8_t _z_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reliability_t reliability, z_congestion_control_t cong_ctrl); +int8_t _z_send_n_msg(_z_session_t *zn, _z_network_message_t *n_msg, z_reliability_t reliability, + z_congestion_control_t cong_ctrl); #endif /* ZENOH_PICO_SESSION_UTILS_H */ diff --git a/include/zenoh-pico/transport/link/tx.h b/include/zenoh-pico/transport/link/tx.h index 3cca0d6dc..833b496ab 100644 --- a/include/zenoh-pico/transport/link/tx.h +++ b/include/zenoh-pico/transport/link/tx.h @@ -29,6 +29,11 @@ int8_t _z_unicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reli int8_t _z_multicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reliability_t reliability, z_congestion_control_t cong_ctrl); +int8_t _z_unicast_send_n_msg(_z_session_t *zn, _z_network_message_t *z_msg, z_reliability_t reliability, + z_congestion_control_t cong_ctrl); +int8_t _z_multicast_send_n_msg(_z_session_t *zn, _z_network_message_t *z_msg, z_reliability_t reliability, + z_congestion_control_t cong_ctrl); + int8_t _z_send_t_msg(_z_transport_t *zt, const _z_transport_message_t *t_msg); int8_t _z_unicast_send_t_msg(_z_transport_unicast_t *ztu, const _z_transport_message_t *t_msg); int8_t _z_multicast_send_t_msg(_z_transport_multicast_t *ztm, const _z_transport_message_t *t_msg); diff --git a/src/net/primitives.c b/src/net/primitives.c index 7c0ab3a9a..b3b012d4d 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -54,15 +54,14 @@ _z_zint_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr) { r->_key = _z_keyexpr_duplicate(&keyexpr); if (_z_register_resource(zn, _Z_RESOURCE_IS_LOCAL, r) == _Z_RES_OK) { // Build the declare message to send on the wire - _z_declaration_array_t declarations = _z_declaration_array_make(1); - declarations._val[0] = _z_msg_make_declaration_resource(r->_id, _z_keyexpr_duplicate(&keyexpr)); - _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); - if (_z_send_z_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { + _z_declaration_t declaration = _z_msg_make_declaration_resource(r->_id, _z_keyexpr_duplicate(&keyexpr)); + _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); + if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { ret = r->_id; } else { _z_unregister_resource(zn, _Z_RESOURCE_IS_LOCAL, r); } - _z_msg_clear(&z_msg); + _z_n_msg_clear(&n_msg); } else { _z_resource_free(&r); } @@ -78,15 +77,14 @@ int8_t _z_undeclare_resource(_z_session_t *zn, const _z_zint_t rid) { _z_resource_t *r = _z_get_resource_by_id(zn, _Z_RESOURCE_IS_LOCAL, rid); if (r != NULL) { // Build the declare message to send on the wire - _z_declaration_array_t declarations = _z_declaration_array_make(1); - declarations._val[0] = _z_msg_make_declaration_forget_resource(rid); - _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); - if (_z_send_z_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { + _z_declaration_t declaration = _z_msg_make_declaration_forget_resource(rid); + _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); + if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { _z_unregister_resource(zn, _Z_RESOURCE_IS_LOCAL, r); // Only if message is send, local resource is removed } else { ret = _Z_ERR_TRANSPORT_TX_FAILED; } - _z_msg_clear(&z_msg); + _z_n_msg_clear(&n_msg); } else { ret = _Z_ERR_KEYEXPR_UNKNOWN; } @@ -106,14 +104,13 @@ _z_publisher_t *_z_declare_publisher(_z_session_t *zn, _z_keyexpr_t keyexpr, z_c ret->_priority = priority; // Build the declare message to send on the wire - _z_declaration_array_t declarations = _z_declaration_array_make(1); - declarations._val[0] = _z_msg_make_declaration_publisher(_z_keyexpr_duplicate(&keyexpr)); - _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); - if (_z_send_z_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + _z_declaration_t declaration = _z_msg_make_declaration_publisher(_z_keyexpr_duplicate(&keyexpr)); + _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); + if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { // ret = _Z_ERR_TRANSPORT_TX_FAILED; _z_publisher_free(&ret); } - _z_msg_clear(&z_msg); + _z_n_msg_clear(&n_msg); } return ret; @@ -123,13 +120,12 @@ int8_t _z_undeclare_publisher(_z_publisher_t *pub) { int8_t ret = _Z_RES_OK; // Build the declare message to send on the wire - _z_declaration_array_t declarations = _z_declaration_array_make(1); - declarations._val[0] = _z_msg_make_declaration_forget_publisher(_z_keyexpr_duplicate(&pub->_key)); - _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); - if (_z_send_z_msg(pub->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + _z_declaration_t declaration = _z_msg_make_declaration_forget_publisher(_z_keyexpr_duplicate(&pub->_key)); + _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); + if (_z_send_n_msg(pub->_zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { ret = _Z_ERR_TRANSPORT_TX_FAILED; } - _z_msg_clear(&z_msg); + _z_n_msg_clear(&n_msg); return ret; } @@ -155,15 +151,14 @@ _z_subscriber_t *_z_declare_subscriber(_z_session_t *zn, _z_keyexpr_t keyexpr, _ // Do not drop it by the end of this function. if (sp_s != NULL) { // Build the declare message to send on the wire - _z_declaration_array_t declarations = _z_declaration_array_make(1); - declarations._val[0] = _z_msg_make_declaration_subscriber(_z_keyexpr_duplicate(&keyexpr), sub_info); - _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); - if (_z_send_z_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + _z_declaration_t declaration = _z_msg_make_declaration_subscriber(_z_keyexpr_duplicate(&keyexpr), sub_info); + _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); + if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { // ret = _Z_ERR_TRANSPORT_TX_FAILED; _z_unregister_subscription(zn, _Z_RESOURCE_IS_LOCAL, sp_s); _z_subscriber_free(&ret); } - _z_msg_clear(&z_msg); + _z_n_msg_clear(&n_msg); } else { _z_subscriber_free(&ret); } @@ -180,16 +175,15 @@ int8_t _z_undeclare_subscriber(_z_subscriber_t *sub) { _z_subscription_sptr_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_id); if (s != NULL) { // Build the declare message to send on the wire - _z_declaration_array_t declarations = _z_declaration_array_make(1); - declarations._val[0] = _z_msg_make_declaration_forget_subscriber(_z_keyexpr_duplicate(&s->ptr->_key)); - _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); - if (_z_send_z_msg(sub->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { + _z_declaration_t declaration = _z_msg_make_declaration_forget_subscriber(_z_keyexpr_duplicate(&s->ptr->_key)); + _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); + if (_z_send_n_msg(sub->_zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { // Only if message is successfully send, local subscription state can be removed _z_unregister_subscription(sub->_zn, _Z_RESOURCE_IS_LOCAL, s); } else { ret = _Z_ERR_TRANSPORT_TX_FAILED; } - _z_msg_clear(&z_msg); + _z_n_msg_clear(&n_msg); } else { ret = _Z_ERR_ENTITY_UNKNOWN; } @@ -218,16 +212,15 @@ _z_queryable_t *_z_declare_queryable(_z_session_t *zn, _z_keyexpr_t keyexpr, _Bo // Do not drop it by the end of this function. if (sp_q != NULL) { // Build the declare message to send on the wire - _z_declaration_array_t declarations = _z_declaration_array_make(1); - declarations._val[0] = _z_msg_make_declaration_queryable(_z_keyexpr_duplicate(&keyexpr), q._complete, - _Z_QUERYABLE_DISTANCE_DEFAULT); - _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); - if (_z_send_z_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + _z_declaration_t declaration = _z_msg_make_declaration_queryable( + _z_keyexpr_duplicate(&keyexpr), q._complete, _Z_QUERYABLE_DISTANCE_DEFAULT); + _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); + if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { // ret = _Z_ERR_TRANSPORT_TX_FAILED; _z_unregister_questionable(zn, sp_q); _z_queryable_free(&ret); } - _z_msg_clear(&z_msg); + _z_n_msg_clear(&n_msg); } else { _z_queryable_free(&ret); } @@ -244,16 +237,15 @@ int8_t _z_undeclare_queryable(_z_queryable_t *qle) { _z_questionable_sptr_t *q = _z_get_questionable_by_id(qle->_zn, qle->_id); if (q != NULL) { // Build the declare message to send on the wire - _z_declaration_array_t declarations = _z_declaration_array_make(1); - declarations._val[0] = _z_msg_make_declaration_forget_queryable(_z_keyexpr_duplicate(&q->ptr->_key)); - _z_zenoh_message_t z_msg = _z_msg_make_declare(declarations); - if (_z_send_z_msg(qle->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { + _z_declaration_t declaration = _z_msg_make_declaration_forget_queryable(_z_keyexpr_duplicate(&q->ptr->_key)); + _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); + if (_z_send_n_msg(qle->_zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { // Only if message is successfully send, local queryable state can be removed _z_unregister_questionable(qle->_zn, q); } else { ret = _Z_ERR_TRANSPORT_TX_FAILED; } - _z_msg_clear(&z_msg); + _z_n_msg_clear(&n_msg); } else { ret = _Z_ERR_ENTITY_UNKNOWN; diff --git a/src/protocol/msg.c b/src/protocol/msg.c index f656bcdbc..59cd0caff 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -99,7 +99,7 @@ _z_declaration_t _z_msg_make_declaration_resource(_z_zint_t id, _z_keyexpr_t key return decl; } -void _z_msg_clear_declaration_resource(_z_res_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } +void _z_declaration_clear_resource(_z_res_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } /*------------------ Forget Resource Declaration ------------------*/ _z_declaration_t _z_msg_make_declaration_forget_resource(_z_zint_t rid) { @@ -112,7 +112,7 @@ _z_declaration_t _z_msg_make_declaration_forget_resource(_z_zint_t rid) { return decl; } -void _z_msg_clear_declaration_forget_resource(_z_forget_res_decl_t *dcl) { (void)(dcl); } +void _z_declaration_clear_forget_resource(_z_forget_res_decl_t *dcl) { (void)(dcl); } /*------------------ Publisher Declaration ------------------*/ _z_declaration_t _z_msg_make_declaration_publisher(_z_keyexpr_t key) { @@ -128,7 +128,7 @@ _z_declaration_t _z_msg_make_declaration_publisher(_z_keyexpr_t key) { return decl; } -void _z_msg_clear_declaration_publisher(_z_pub_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } +void _z_declaration_clear_publisher(_z_pub_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } /*------------------ Forget Publisher Declaration ------------------*/ _z_declaration_t _z_msg_make_declaration_forget_publisher(_z_keyexpr_t key) { @@ -144,7 +144,7 @@ _z_declaration_t _z_msg_make_declaration_forget_publisher(_z_keyexpr_t key) { return decl; } -void _z_msg_clear_declaration_forget_publisher(_z_forget_pub_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } +void _z_declaration_clear_forget_publisher(_z_forget_pub_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } /*------------------ Subscriber Declaration ------------------*/ _z_declaration_t _z_msg_make_declaration_subscriber(_z_keyexpr_t key, _z_subinfo_t subinfo) { @@ -172,7 +172,7 @@ void _z_subinfo_clear(_z_subinfo_t *si) { // Nothing to clear } -void _z_msg_clear_declaration_subscriber(_z_sub_decl_t *dcl) { +void _z_declaration_clear_subscriber(_z_sub_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); _z_subinfo_clear(&dcl->_subinfo); } @@ -191,7 +191,7 @@ _z_declaration_t _z_msg_make_declaration_forget_subscriber(_z_keyexpr_t key) { return decl; } -void _z_msg_clear_declaration_forget_subscriber(_z_forget_sub_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } +void _z_declaration_clear_forget_subscriber(_z_forget_sub_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } /*------------------ Queryable Declaration ------------------*/ _z_declaration_t _z_msg_make_declaration_queryable(_z_keyexpr_t key, _z_zint_t complete, _z_zint_t distance) { @@ -214,7 +214,7 @@ _z_declaration_t _z_msg_make_declaration_queryable(_z_keyexpr_t key, _z_zint_t c return decl; } -void _z_msg_clear_declaration_queryable(_z_qle_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } +void _z_declaration_clear_queryable(_z_qle_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } /*------------------ Forget Queryable Declaration ------------------*/ _z_declaration_t _z_msg_make_declaration_forget_queryable(_z_keyexpr_t key) { @@ -230,57 +230,42 @@ _z_declaration_t _z_msg_make_declaration_forget_queryable(_z_keyexpr_t key) { return decl; } -void _z_msg_clear_declaration_forget_queryable(_z_forget_qle_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } +void _z_declaration_clear_forget_queryable(_z_forget_qle_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } /*------------------ Declare ------------------*/ -_z_zenoh_message_t _z_msg_make_declare(_z_declaration_array_t declarations) { - _z_zenoh_message_t msg; - - msg._body._declare._declarations = declarations; - - msg._header = _Z_MID_Z_DECLARE; - - msg._attachment = NULL; - msg._reply_context = NULL; - - return msg; -} - -void _z_msg_clear_declaration(_z_declaration_t *dcl) { +void _z_declaration_clear(_z_declaration_t *dcl) { uint8_t did = _Z_MID(dcl->_header); switch (did) { case _Z_DECL_RESOURCE: - _z_msg_clear_declaration_resource(&dcl->_body._res); + _z_declaration_clear_resource(&dcl->_body._res); break; case _Z_DECL_PUBLISHER: - _z_msg_clear_declaration_publisher(&dcl->_body._pub); + _z_declaration_clear_publisher(&dcl->_body._pub); break; case _Z_DECL_SUBSCRIBER: - _z_msg_clear_declaration_subscriber(&dcl->_body._sub); + _z_declaration_clear_subscriber(&dcl->_body._sub); break; case _Z_DECL_QUERYABLE: - _z_msg_clear_declaration_queryable(&dcl->_body._qle); + _z_declaration_clear_queryable(&dcl->_body._qle); break; case _Z_DECL_FORGET_RESOURCE: - _z_msg_clear_declaration_forget_resource(&dcl->_body._forget_res); + _z_declaration_clear_forget_resource(&dcl->_body._forget_res); break; case _Z_DECL_FORGET_PUBLISHER: - _z_msg_clear_declaration_forget_publisher(&dcl->_body._forget_pub); + _z_declaration_clear_forget_publisher(&dcl->_body._forget_pub); break; case _Z_DECL_FORGET_SUBSCRIBER: - _z_msg_clear_declaration_forget_subscriber(&dcl->_body._forget_sub); + _z_declaration_clear_forget_subscriber(&dcl->_body._forget_sub); break; case _Z_DECL_FORGET_QUERYABLE: - _z_msg_clear_declaration_forget_queryable(&dcl->_body._forget_qle); + _z_declaration_clear_forget_queryable(&dcl->_body._forget_qle); break; default: - _Z_DEBUG("WARNING: Trying to free declaration with unknown ID(%d)\n", did); + _Z_DEBUG("WARNING: Trying to clear declaration with unknown ID(%d)\n", did); break; } } -void _z_msg_clear_declare(_z_msg_declare_t *msg) { _z_declaration_array_clear(&msg->_declarations); } - /*------------------ Data Info Field ------------------*/ // @TODO: implement builder for _z_data_info_t @@ -425,9 +410,6 @@ void _z_msg_clear(_z_zenoh_message_t *msg) { uint8_t mid = _Z_MID(msg->_header); switch (mid) { - case _Z_MID_Z_DECLARE: - _z_msg_clear_declare(&msg->_body._declare); - break; case _Z_MID_Z_DATA: _z_msg_clear_data(&msg->_body._data); break; @@ -457,6 +439,46 @@ void _z_msg_free(_z_zenoh_message_t **msg) { } } +/*=============================*/ +/* Network Messages */ +/*=============================*/ +_z_network_message_t _z_n_msg_make_declare(_z_declaration_t declaration) { + _z_network_message_t msg; + msg._header = _Z_MID_N_DECLARE; + + msg._body._declare._declaration = declaration; + msg._extensions = _z_msg_ext_vec_make(0); + + return msg; +} + +void _z_n_msg_clear_declare(_z_n_msg_declare_t *msg) { _z_declaration_clear(&msg->_declaration); } + +void _z_n_msg_clear(_z_network_message_t *msg) { + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_N_DECLARE: + _z_n_msg_clear_declare(&msg->_body._declare); + break; + default: + _Z_DEBUG("WARNING: Trying to clear network message with unknown ID(%d)\n", mid); + break; + } + + _z_msg_ext_vec_clear(&msg->_extensions); +} + +void _z_n_msg_free(_z_network_message_t **msg) { + _z_network_message_t *ptr = *msg; + + if (ptr != NULL) { + _z_n_msg_clear(ptr); + + z_free(ptr); + *msg = NULL; + } +} + /*=============================*/ /* Transport Messages */ /*=============================*/ @@ -725,7 +747,7 @@ _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliabl _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAME_R); } - msg._body._frame._messages = _z_zenoh_message_vec_make(0); + msg._body._frame._messages = _z_network_message_vec_make(0); msg._attachment = NULL; msg._extensions = _z_msg_ext_vec_make(0); @@ -733,7 +755,7 @@ _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliabl return msg; } -_z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_zenoh_message_vec_t messages, _Bool is_reliable) { +_z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_network_message_vec_t messages, _Bool is_reliable) { _z_transport_message_t msg; msg._header = _Z_MID_T_FRAME; @@ -752,10 +774,10 @@ _z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_zenoh_message_vec_t void _z_t_msg_copy_frame(_z_t_msg_frame_t *clone, _z_t_msg_frame_t *msg) { clone->_sn = msg->_sn; - _z_zenoh_message_vec_copy(&clone->_messages, &msg->_messages); + _z_network_message_vec_copy(&clone->_messages, &msg->_messages); } -void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg) { _z_zenoh_message_vec_clear(&msg->_messages); } +void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg) { _z_network_message_vec_clear(&msg->_messages); } /*------------------ Fragment Message ------------------*/ _z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_payload_t payload, _Bool is_reliable, _Bool is_last) { @@ -820,7 +842,7 @@ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg) { } break; default: { - _Z_DEBUG("WARNING: Trying to free session message with unknown ID(%d)\n", mid); + _Z_DEBUG("WARNING: Trying to copy transport message with unknown ID(%d)\n", mid); } break; } } @@ -862,7 +884,7 @@ void _z_t_msg_clear(_z_transport_message_t *msg) { } break; default: { - _Z_DEBUG("WARNING: Trying to free session message with unknown ID(%d)\n", mid); + _Z_DEBUG("WARNING: Trying to clear transport message with unknown ID(%d)\n", mid); } break; } @@ -886,7 +908,7 @@ void _z_s_msg_copy(_z_scouting_message_t *clone, _z_scouting_message_t *msg) { } break; default: { - _Z_DEBUG("WARNING: Trying to free session message with unknown ID(%d)\n", mid); + _Z_DEBUG("WARNING: Trying to copy session message with unknown ID(%d)\n", mid); } break; } } @@ -908,7 +930,7 @@ void _z_s_msg_clear(_z_scouting_message_t *msg) { } break; default: { - _Z_DEBUG("WARNING: Trying to free session message with unknown ID(%d)\n", mid); + _Z_DEBUG("WARNING: Trying to clear session message with unknown ID(%d)\n", mid); } break; } diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 07d6463fd..0807074d1 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -505,7 +505,7 @@ int8_t _z_forget_qle_decl_decode(_z_forget_qle_decl_t *dcl, _z_zbuf_t *zbf, uint } /*------------------ Declaration Field ------------------*/ -int8_t _z_declaration_encode(_z_wbuf_t *wbf, _z_declaration_t *dcl) { +int8_t _z_declaration_encode(_z_wbuf_t *wbf, const _z_declaration_t *dcl) { int8_t ret = _Z_RES_OK; _Z_EC(_z_wbuf_write(wbf, dcl->_header)) @@ -604,42 +604,6 @@ int8_t _z_declaration_decode_na(_z_declaration_t *decl, _z_zbuf_t *zbf) { int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf) { return _z_declaration_decode_na(decl, zbf); } -/*------------------ Declaration Message ------------------*/ -int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_msg_declare_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_Z_DECLARE\n"); - - _Z_EC(_z_zint_encode(wbf, msg->_declarations._len)) - for (_z_zint_t i = 0; i < msg->_declarations._len; i++) { - _Z_EC(_z_declaration_encode(wbf, &msg->_declarations._val[i])); - } - - return ret; -} - -int8_t _z_declare_decode_na(_z_msg_declare_t *msg, _z_zbuf_t *zbf) { - _Z_DEBUG("Decoding _Z_MID_Z_DECLARE\n"); - int8_t ret = _Z_RES_OK; - - _z_zint_t len = 0; - ret |= _z_zint_decode(&len, zbf); - if (ret == _Z_RES_OK) { - msg->_declarations = _z_declaration_array_make(len); - for (size_t i = 0; (ret == _Z_RES_OK) && (i < len); i++) { - ret |= _z_declaration_decode_na(&msg->_declarations._val[i], zbf); - if (ret != _Z_RES_OK) { - msg->_declarations._len = i; - } - } - } else { - msg->_declarations = _z_declaration_array_make(0); - } - - return ret; -} - -int8_t _z_declare_decode(_z_msg_declare_t *msg, _z_zbuf_t *zbf) { return _z_declare_decode_na(msg, zbf); } - /*------------------ Data Info Field ------------------*/ int8_t _z_data_info_encode(_z_wbuf_t *wbf, const _z_data_info_t *fld) { int8_t ret = _Z_RES_OK; @@ -892,10 +856,6 @@ int8_t _z_zenoh_message_encode(_z_wbuf_t *wbf, const _z_zenoh_message_t *msg) { ret |= _z_query_encode(wbf, msg->_header, &msg->_body._query); } break; - case _Z_MID_Z_DECLARE: { - ret |= _z_declare_encode(wbf, &msg->_body._declare); - } break; - case _Z_MID_Z_PULL: { ret |= _z_pull_encode(wbf, msg->_header, &msg->_body._pull); } break; @@ -942,11 +902,6 @@ int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { is_last = true; } break; - case _Z_MID_Z_DECLARE: { - ret |= _z_declare_decode(&msg->_body._declare, zbf); - is_last = true; - } break; - case _Z_MID_Z_PULL: { ret |= _z_pull_decode(&msg->_body._pull, zbf, msg->_header); is_last = true; @@ -982,114 +937,82 @@ int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { int8_t _z_zenoh_message_decode(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { return _z_zenoh_message_decode_na(msg, zbf); } -/*=============================*/ -/* Transport Messages */ -/*=============================*/ -/*------------------ Scout Message ------------------*/ -int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_scout_t *msg) { +/*------------------ Declaration Message ------------------*/ +int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_n_msg_declare_t *msg) { int8_t ret = _Z_RES_OK; - (void)(header); - _Z_DEBUG("Encoding _Z_MID_SCOUT\n"); - - _Z_EC(_z_uint8_encode(wbf, msg->_version)) - - uint8_t cbyte = 0; - cbyte |= (msg->_what & 0x07); - uint8_t zid_len = msg->_zid.len; - if (zid_len > 0) { // TODO[protocol]: check if ZID > 0 && <= 16 - _Z_SET_FLAG(cbyte, _Z_FLAG_T_SCOUT_I); - cbyte |= ((zid_len - 1) & 0x0F) << 4; - } - _Z_EC(_z_uint8_encode(wbf, cbyte)) + _Z_DEBUG("Encoding _Z_MID_N_DECLARE\n"); - _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) + _Z_EC(_z_declaration_encode(wbf, &msg->_declaration)); return ret; } -int8_t _z_scout_decode_na(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_declare_decode_na(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf) { + _Z_DEBUG("Decoding _Z_MID_N_DECLARE\n"); int8_t ret = _Z_RES_OK; - (void)(header); - _Z_DEBUG("Decoding _Z_MID_SCOUT\n"); - ret |= _z_uint8_decode(&msg->_version, zbf); - - uint8_t cbyte = 0; - ret |= _z_uint8_decode(&cbyte, zbf); - msg->_what = cbyte & 0x07; - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(cbyte, _Z_FLAG_T_SCOUT_I) == true)) { - msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; - ret |= _z_bytes_val_decode(&msg->_zid, zbf); - if (ret != _Z_RES_OK) { - msg->_zid = _z_bytes_empty(); - } - } else { - msg->_zid = _z_bytes_empty(); + ret |= _z_declaration_decode_na(&msg->_declaration, zbf); + if (ret != _Z_RES_OK) { + // TODO[protocol]: Add empty declaration } return ret; } -int8_t _z_scout_decode(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_scout_decode_na(msg, zbf, header); -} +int8_t _z_declare_decode(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf) { return _z_declare_decode_na(msg, zbf); } -/*------------------ Hello Message ------------------*/ -int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *msg) { +/*------------------ Network Message ------------------*/ +int8_t _z_network_message_encode(_z_wbuf_t *wbf, const _z_network_message_t *msg) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_HELLO\n"); - _Z_EC(_z_uint8_encode(wbf, msg->_version)) + uint8_t header = msg->_header; + if (_z_msg_ext_vec_is_empty(&msg->_extensions) == false) { + header |= _Z_FLAG_N_Z; + } - uint8_t cbyte = 0; - cbyte |= (msg->_whatami & 0x03); - cbyte |= ((msg->_zid.len - 1) & 0x0F) << 4; // TODO[protocol]: check if ZID > 0 && <= 16 - _Z_EC(_z_uint8_encode(wbf, cbyte)) - _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) + _Z_EC(_z_wbuf_write(wbf, header)) + switch (_Z_MID(msg->_header)) { + case _Z_MID_N_DECLARE: { + ret |= _z_declare_encode(wbf, &msg->_body._declare); + } break; - if (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true) { - _Z_EC(_z_locators_encode(wbf, &msg->_locators)) + default: { + _Z_DEBUG("WARNING: Trying to encode network message with unknown ID(%d)\n", _Z_MID(msg->_header)); + ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; + } break; } return ret; } -int8_t _z_hello_decode_na(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_HELLO\n"); +int8_t _z_network_message_decode_na(_z_network_message_t *msg, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; - ret |= _z_uint8_decode(&msg->_version, zbf); - - uint8_t cbyte = 0; - ret |= _z_uint8_decode(&cbyte, zbf); - msg->_whatami = cbyte & 0x03; - msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; - + ret |= _z_uint8_decode(&msg->_header, zbf); // Decode the header if (ret == _Z_RES_OK) { - ret |= _z_bytes_val_decode(&msg->_zid, zbf); - if (ret != _Z_RES_OK) { - msg->_zid = _z_bytes_empty(); - } - } else { - msg->_zid = _z_bytes_empty(); - } + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_N_DECLARE: { + ret |= _z_declare_decode(&msg->_body._declare, zbf); + } break; - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true)) { - ret |= _z_locators_decode(&msg->_locators, zbf); - if (ret != _Z_RES_OK) { - msg->_locators = _z_locator_array_make(0); + default: { + _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(%d)\n", mid); + ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; + } break; } - } else { - msg->_locators = _z_locator_array_make(0); } return ret; } -int8_t _z_hello_decode(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_hello_decode_na(msg, zbf, header); +int8_t _z_network_message_decode(_z_network_message_t *n_msg, _z_zbuf_t *zbf) { + return _z_network_message_decode_na(n_msg, zbf); } +/*=============================*/ +/* Transport Messages */ +/*=============================*/ /*------------------ Join Message ------------------*/ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg) { int8_t ret = _Z_RES_OK; @@ -1385,9 +1308,9 @@ int8_t _z_frame_payload_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fr int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_T_FRAME_PAYLOAD\n"); - size_t len = _z_zenoh_message_vec_len(&msg->_messages); + size_t len = _z_network_message_vec_len(&msg->_messages); for (size_t i = 0; i < len; i++) { - _Z_EC(_z_zenoh_message_encode(wbf, _z_zenoh_message_vec_get(&msg->_messages, i))) + _Z_EC(_z_network_message_encode(wbf, _z_network_message_vec_get(&msg->_messages, i))) } return ret; @@ -1410,16 +1333,16 @@ int8_t _z_frame_payload_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t int8_t ret = _Z_RES_OK; _Z_DEBUG("Decoding _Z_MID_T_FRAME_PAYLOAD\n"); - msg->_messages = _z_zenoh_message_vec_make(_ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE); + msg->_messages = _z_network_message_vec_make(_ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE); while (_z_zbuf_len(zbf) > 0) { // Mark the reading position of the iobfer size_t r_pos = _z_zbuf_get_rpos(zbf); - _z_zenoh_message_t *zm = (_z_zenoh_message_t *)z_malloc(sizeof(_z_zenoh_message_t)); - ret |= _z_zenoh_message_decode(zm, zbf); + _z_network_message_t *nm = (_z_network_message_t *)z_malloc(sizeof(_z_network_message_t)); + ret |= _z_network_message_decode(nm, zbf); if (ret == _Z_RES_OK) { - _z_zenoh_message_vec_append(&msg->_messages, zm); + _z_network_message_vec_append(&msg->_messages, nm); } else { - _z_msg_free(&zm); + _z_n_msg_free(&nm); _z_zbuf_set_rpos(zbf, r_pos); // Restore the reading position of the iobfer @@ -1689,7 +1612,114 @@ int8_t _z_transport_message_decode(_z_transport_message_t *t_msg, _z_zbuf_t *zbf return _z_transport_message_decode_na(t_msg, zbf); } -/*------------------ Scouting Message ------------------*/ +/*=============================*/ +/* Scouting Messages */ +/*=============================*/ +/*------------------ Scout Message ------------------*/ +int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_scout_t *msg) { + int8_t ret = _Z_RES_OK; + (void)(header); + _Z_DEBUG("Encoding _Z_MID_SCOUT\n"); + + _Z_EC(_z_uint8_encode(wbf, msg->_version)) + + uint8_t cbyte = 0; + cbyte |= (msg->_what & 0x07); + uint8_t zid_len = msg->_zid.len; + if (zid_len > 0) { // TODO[protocol]: check if ZID > 0 && <= 16 + _Z_SET_FLAG(cbyte, _Z_FLAG_T_SCOUT_I); + cbyte |= ((zid_len - 1) & 0x0F) << 4; + } + _Z_EC(_z_uint8_encode(wbf, cbyte)) + + _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) + + return ret; +} + +int8_t _z_scout_decode_na(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) { + int8_t ret = _Z_RES_OK; + (void)(header); + _Z_DEBUG("Decoding _Z_MID_SCOUT\n"); + + ret |= _z_uint8_decode(&msg->_version, zbf); + + uint8_t cbyte = 0; + ret |= _z_uint8_decode(&cbyte, zbf); + msg->_what = cbyte & 0x07; + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(cbyte, _Z_FLAG_T_SCOUT_I) == true)) { + msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; + ret |= _z_bytes_val_decode(&msg->_zid, zbf); + if (ret != _Z_RES_OK) { + msg->_zid = _z_bytes_empty(); + } + } else { + msg->_zid = _z_bytes_empty(); + } + + return ret; +} + +int8_t _z_scout_decode(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_scout_decode_na(msg, zbf, header); +} + +/*------------------ Hello Message ------------------*/ +int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *msg) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_HELLO\n"); + + _Z_EC(_z_uint8_encode(wbf, msg->_version)) + + uint8_t cbyte = 0; + cbyte |= (msg->_whatami & 0x03); + cbyte |= ((msg->_zid.len - 1) & 0x0F) << 4; // TODO[protocol]: check if ZID > 0 && <= 16 + _Z_EC(_z_uint8_encode(wbf, cbyte)) + _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) + + if (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true) { + _Z_EC(_z_locators_encode(wbf, &msg->_locators)) + } + + return ret; +} + +int8_t _z_hello_decode_na(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_HELLO\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_uint8_decode(&msg->_version, zbf); + + uint8_t cbyte = 0; + ret |= _z_uint8_decode(&cbyte, zbf); + msg->_whatami = cbyte & 0x03; + msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; + + if (ret == _Z_RES_OK) { + ret |= _z_bytes_val_decode(&msg->_zid, zbf); + if (ret != _Z_RES_OK) { + msg->_zid = _z_bytes_empty(); + } + } else { + msg->_zid = _z_bytes_empty(); + } + + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true)) { + ret |= _z_locators_decode(&msg->_locators, zbf); + if (ret != _Z_RES_OK) { + msg->_locators = _z_locator_array_make(0); + } + } else { + msg->_locators = _z_locator_array_make(0); + } + + return ret; +} + +int8_t _z_hello_decode(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_hello_decode_na(msg, zbf, header); +} + int8_t _z_scouting_message_encode(_z_wbuf_t *wbf, const _z_scouting_message_t *msg) { int8_t ret = _Z_RES_OK; diff --git a/src/session/rx.c b/src/session/rx.c index f2f28da0b..83bd206e8 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -40,116 +40,6 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg) { } } break; - case _Z_MID_Z_DECLARE: { - _Z_INFO("Received _Z_DECLARE message\n"); - for (size_t i = 0; i < msg->_body._declare._declarations._len; i++) { - _z_declaration_t decl = msg->_body._declare._declarations._val[i]; - switch (_Z_MID(decl._header)) { - case _Z_DECL_RESOURCE: { - _Z_INFO("Received declare-resource message\n"); - - // Register remote resource declaration - _z_resource_t *r = (_z_resource_t *)z_malloc(sizeof(_z_resource_t)); - if (r != NULL) { - r->_id = decl._body._res._id; - r->_key = _z_keyexpr_duplicate(&decl._body._res._key); - - ret = _z_register_resource(zn, _Z_RESOURCE_IS_REMOTE, r); - if (ret != _Z_RES_OK) { - _z_resource_free(&r); - } - } else { - ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - } break; - - case _Z_DECL_PUBLISHER: { - _Z_INFO("Received declare-publisher message\n"); - // TODO: not supported yet - } break; - - case _Z_DECL_SUBSCRIBER: { - _Z_INFO("Received declare-subscriber message\n"); - _z_keyexpr_t key = - _z_get_expanded_key_from_key(zn, _Z_RESOURCE_IS_REMOTE, &decl._body._sub._key); - _z_subscription_sptr_list_t *subs = - _z_get_subscriptions_by_key(zn, _Z_RESOURCE_IS_REMOTE, &key); - if (subs != NULL) { - _z_keyexpr_clear(&key); - _z_subscription_sptr_list_free(&subs); - break; - } - - _z_subscription_t s; - s._id = _z_get_entity_id(zn); - s._key = key; - s._info = decl._body._sub._subinfo; - s._callback = NULL; - s._dropper = NULL; - s._arg = NULL; - - _z_subscription_sptr_t *sp_s = _z_register_subscription( - zn, _Z_RESOURCE_IS_REMOTE, &s); // This a pointer to the entry stored at session-level. - // Do not drop it by the end of this function. - if (sp_s == NULL) { - _z_unregister_subscription(zn, _Z_RESOURCE_IS_REMOTE, sp_s); - break; - } - - _z_subscription_sptr_list_free(&subs); - } break; - - case _Z_DECL_QUERYABLE: { - _Z_INFO("Received declare-queryable message\n"); - // TODO: not supported yet - } break; - - case _Z_DECL_FORGET_RESOURCE: { - _Z_INFO("Received forget-resource message\n"); - _z_resource_t *rd = - _z_get_resource_by_id(zn, _Z_RESOURCE_IS_REMOTE, decl._body._forget_res._rid); - if (rd != NULL) { - _z_unregister_resource(zn, _Z_RESOURCE_IS_REMOTE, rd); - } - } break; - - case _Z_DECL_FORGET_PUBLISHER: { - _Z_INFO("Received forget-publisher message\n"); - // TODO: not supported yet - - } break; - - case _Z_DECL_FORGET_SUBSCRIBER: { - _Z_INFO("Received forget-subscriber message\n"); - _z_keyexpr_t key = - _z_get_expanded_key_from_key(zn, _Z_RESOURCE_IS_REMOTE, &decl._body._forget_sub._key); - _z_subscription_sptr_list_t *subs = - _z_get_subscriptions_by_key(zn, _Z_RESOURCE_IS_REMOTE, &key); - - _z_subscription_sptr_list_t *xs = subs; - while (xs != NULL) { - _z_subscription_sptr_t *sub = _z_subscription_sptr_list_head(xs); - _z_unregister_subscription(zn, _Z_RESOURCE_IS_REMOTE, sub); - xs = _z_subscription_sptr_list_tail(xs); - } - - _z_subscription_sptr_list_free(&subs); - _z_keyexpr_clear(&key); - } break; - - case _Z_DECL_FORGET_QUERYABLE: { - _Z_INFO("Received forget-queryable message\n"); - // TODO: not supported yet - } break; - - default: { - _Z_INFO("Unknown declaration message ID"); - ret = _Z_ERR_MESSAGE_ZENOH_DECLARATION_UNKNOWN; - } break; - } - } - } break; - case _Z_MID_Z_PULL: { _Z_INFO("Received _Z_PULL message\n"); // TODO: not supported yet diff --git a/src/session/tx.c b/src/session/tx.c index 092bb29fb..ef33760ad 100644 --- a/src/session/tx.c +++ b/src/session/tx.c @@ -37,3 +37,25 @@ int8_t _z_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reliability_ return ret; } + +int8_t _z_send_n_msg(_z_session_t *zn, _z_network_message_t *z_msg, z_reliability_t reliability, + z_congestion_control_t cong_ctrl) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG(">> send network message\n"); + +#if Z_UNICAST_TRANSPORT == 1 + if (zn->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { + ret = _z_unicast_send_n_msg(zn, z_msg, reliability, cong_ctrl); + } else +#endif // Z_UNICAST_TRANSPORT == 1 +#if Z_MULTICAST_TRANSPORT == 1 + if (zn->_tp._type == _Z_TRANSPORT_MULTICAST_TYPE) { + ret = _z_multicast_send_n_msg(zn, z_msg, reliability, cong_ctrl); + } else +#endif // Z_MULTICAST_TRANSPORT == 1 + { + ret = _Z_ERR_TRANSPORT_NOT_AVAILABLE; + } + + return ret; +} diff --git a/src/transport/multicast/link/tx.c b/src/transport/multicast/link/tx.c index 7e6fba9e1..c310273dc 100644 --- a/src/transport/multicast/link/tx.c +++ b/src/transport/multicast/link/tx.c @@ -124,4 +124,59 @@ int8_t _z_multicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_re return ret; } +int8_t _z_multicast_send_n_msg(_z_session_t *zn, _z_network_message_t *n_msg, z_reliability_t reliability, + z_congestion_control_t cong_ctrl) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG(">> send network message\n"); + + _z_transport_multicast_t *ztm = &zn->_tp._transport._multicast; + + // Acquire the lock and drop the message if needed + _Bool drop = false; + if (cong_ctrl == Z_CONGESTION_CONTROL_BLOCK) { +#if Z_MULTI_THREAD == 1 + _z_mutex_lock(&ztm->_mutex_tx); +#endif // Z_MULTI_THREAD == 1 + } else { +#if Z_MULTI_THREAD == 1 + int8_t locked = _z_mutex_trylock(&ztm->_mutex_tx); + if (locked != (int8_t)0) { + _Z_INFO("Dropping zenoh message because of congestion control\n"); + // We failed to acquire the lock, drop the message + drop = true; + } +#endif // Z_MULTI_THREAD == 1 + } + + if (drop == false) { + // Prepare the buffer eventually reserving space for the message length + __unsafe_z_prepare_wbuf(&ztm->_wbuf, _Z_LINK_IS_STREAMED(ztm->_link._capabilities)); + + _z_zint_t sn = __unsafe_z_multicast_get_sn(ztm, reliability); // Get the next sequence number + + _z_transport_message_t t_msg = _z_t_msg_make_frame_header(sn, reliability); + ret = _z_transport_message_encode(&ztm->_wbuf, &t_msg); // Encode the frame header + if (ret == _Z_RES_OK) { + ret = _z_network_message_encode(&ztm->_wbuf, n_msg); // Encode the network message + if (ret == _Z_RES_OK) { + // Write the message legnth in the reserved space if needed + __unsafe_z_finalize_wbuf(&ztm->_wbuf, _Z_LINK_IS_STREAMED(ztm->_link._capabilities)); + + ret = _z_link_send_wbuf(&ztm->_link, &ztm->_wbuf); // Send the wbuf on the socket + if (ret == _Z_RES_OK) { + ztm->_transmitted = true; // Mark the session that we have transmitted data + } + } else { + // TODO[protocol]: Fragmentation goes here + } + } + +#if Z_MULTI_THREAD == 1 + _z_mutex_unlock(&ztm->_mutex_tx); +#endif // Z_MULTI_THREAD == 1 + } + + return ret; +} + #endif // Z_MULTICAST_TRANSPORT == 1 diff --git a/src/transport/unicast/link/tx.c b/src/transport/unicast/link/tx.c index d27c61b3f..37017a176 100644 --- a/src/transport/unicast/link/tx.c +++ b/src/transport/unicast/link/tx.c @@ -131,4 +131,65 @@ int8_t _z_unicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reli return ret; } +int8_t _z_unicast_send_n_msg(_z_session_t *zn, _z_network_message_t *n_msg, z_reliability_t reliability, + z_congestion_control_t cong_ctrl) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG(">> send network message\n"); + + _z_transport_unicast_t *ztu = &zn->_tp._transport._unicast; + + // Acquire the lock and drop the message if needed + _Bool drop = false; + if (cong_ctrl == Z_CONGESTION_CONTROL_BLOCK) { +#if Z_MULTI_THREAD == 1 + _z_mutex_lock(&ztu->_mutex_tx); +#endif // Z_MULTI_THREAD == 1 + } else { +#if Z_MULTI_THREAD == 1 + int8_t locked = _z_mutex_trylock(&ztu->_mutex_tx); + if (locked != (int8_t)0) { + _Z_INFO("Dropping zenoh message because of congestion control\n"); + // We failed to acquire the lock, drop the message + drop = true; + } +#endif // Z_MULTI_THREAD == 1 + } + + if (drop == false) { + // Prepare the buffer eventually reserving space for the message length + __unsafe_z_prepare_wbuf(&ztu->_wbuf, _Z_LINK_IS_STREAMED(ztu->_link._capabilities)); + + _z_zint_t sn = __unsafe_z_unicast_get_sn(ztu, reliability); // Get the next sequence number + + _z_transport_message_t t_msg = _z_t_msg_make_frame_header(sn, reliability); + ret = _z_transport_message_encode(&ztu->_wbuf, &t_msg); // Encode the frame header + if (ret == _Z_RES_OK) { + ret = _z_network_message_encode(&ztu->_wbuf, n_msg); // Encode the network message + if (ret == _Z_RES_OK) { + // Write the message legnth in the reserved space if needed + __unsafe_z_finalize_wbuf(&ztu->_wbuf, _Z_LINK_IS_STREAMED(ztu->_link._capabilities)); + + if (ztu->_wbuf._ioss._len == 1) { + ret = _z_link_send_wbuf(&ztu->_link, &ztu->_wbuf); // Send the wbuf on the socket + } else { + // Change the MID + + // for () + } + if (ret == _Z_RES_OK) { + ztu->_transmitted = true; // Mark the session that we have transmitted data + } + } else { + // TODO[protocol]: Fragmentation goes here + } + } + +#if Z_MULTI_THREAD == 1 + _z_mutex_unlock(&ztu->_mutex_tx); +#endif // Z_MULTI_THREAD == 1 + } + + return ret; +} + #endif // Z_UNICAST_TRANSPORT == 1 diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 44b25cf75..e9fc6fac9 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -996,8 +996,8 @@ void resource_declaration(void) { printf("\n"); // Free - _z_msg_clear_declaration_resource(&e_rd); - _z_msg_clear_declaration_resource(&d_rd); + _z_declaration_clear_resource(&e_rd); + _z_declaration_clear_resource(&d_rd); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } @@ -1040,8 +1040,8 @@ void publisher_declaration(void) { printf("\n"); // Free - _z_msg_clear_declaration_publisher(&e_pd); - _z_msg_clear_declaration_publisher(&d_pd); + _z_declaration_clear_publisher(&e_pd); + _z_declaration_clear_publisher(&d_pd); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } @@ -1092,8 +1092,8 @@ void subscriber_declaration(void) { printf("\n"); // Free - _z_msg_clear_declaration_subscriber(&e_sd); - _z_msg_clear_declaration_subscriber(&d_sd); + _z_declaration_clear_subscriber(&e_sd); + _z_declaration_clear_subscriber(&d_sd); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } @@ -1149,8 +1149,8 @@ void queryable_declaration(void) { printf("\n"); // Free - _z_msg_clear_declaration_queryable(&e_qd); - _z_msg_clear_declaration_queryable(&d_qd); + _z_declaration_clear_queryable(&e_qd); + _z_declaration_clear_queryable(&d_qd); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } @@ -1235,8 +1235,8 @@ void forget_publisher_declaration(void) { printf("\n"); // Free - _z_msg_clear_declaration_forget_publisher(&e_fpd); - _z_msg_clear_declaration_forget_publisher(&d_fpd); + _z_declaration_clear_forget_publisher(&e_fpd); + _z_declaration_clear_forget_publisher(&d_fpd); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } @@ -1279,8 +1279,8 @@ void forget_subscriber_declaration(void) { printf("\n"); // Free - _z_msg_clear_declaration_forget_subscriber(&e_fsd); - _z_msg_clear_declaration_forget_subscriber(&d_fsd); + _z_declaration_clear_forget_subscriber(&e_fsd); + _z_declaration_clear_forget_subscriber(&d_fsd); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } @@ -1323,8 +1323,8 @@ void forget_queryable_declaration(void) { printf("\n"); // Free - _z_msg_clear_declaration_forget_queryable(&e_fqd); - _z_msg_clear_declaration_forget_queryable(&d_fqd); + _z_declaration_clear_forget_queryable(&e_fqd); + _z_declaration_clear_forget_queryable(&d_fqd); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } @@ -1404,55 +1404,6 @@ void assert_eq_declaration(_z_declaration_t *left, _z_declaration_t *right) { /*=============================*/ /* Zenoh Messages */ /*=============================*/ -/*------------------ Declare message ------------------*/ -_z_zenoh_message_t gen_declare_message(void) { - _z_declaration_array_t declarations = _z_declaration_array_make(gen_zint() % 16); - - for (_z_zint_t i = 0; i < declarations._len; i++) declarations._val[i] = gen_declaration(); - - return _z_msg_make_declare(declarations); -} - -void assert_eq_declare_message(_z_msg_declare_t *left, _z_msg_declare_t *right) { - assert(left->_declarations._len == right->_declarations._len); - - for (_z_zint_t i = 0; i < left->_declarations._len; i++) { - printf(" "); - assert_eq_declaration(&left->_declarations._val[i], &right->_declarations._val[i]); - printf("\n"); - } -} - -void declare_message(void) { - printf("\n>> Declare message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_zenoh_message_t z_msg = gen_declare_message(); - assert(_Z_MID(z_msg._header) == _Z_MID_Z_DECLARE); - - _z_msg_declare_t e_dcl = z_msg._body._declare; - - // Encode - int8_t res = _z_declare_encode(&wbf, &e_dcl); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_msg_declare_t d_dcl; - res = _z_declare_decode(&d_dcl, &zbf); - assert(res == _Z_RES_OK); - - assert_eq_declare_message(&e_dcl, &d_dcl); - - // Free - _z_msg_clear_declare(&d_dcl); - _z_msg_clear(&z_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - /*------------------ Data message ------------------*/ _z_zenoh_message_t gen_data_message(void) { _z_keyexpr_t key = gen_res_key(); @@ -1680,13 +1631,10 @@ _z_zenoh_message_t gen_unit_message(void) { _z_zenoh_message_t gen_zenoh_message(void) { _z_zenoh_message_t p_zm; - uint8_t mids[] = {_Z_MID_Z_DECLARE, _Z_MID_Z_DATA, _Z_MID_Z_PULL, _Z_MID_Z_QUERY, _Z_MID_Z_UNIT}; + uint8_t mids[] = {_Z_MID_Z_DATA, _Z_MID_Z_PULL, _Z_MID_Z_QUERY, _Z_MID_Z_UNIT}; uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); switch (mids[i]) { - case _Z_MID_Z_DECLARE: - p_zm = gen_declare_message(); - break; case _Z_MID_Z_DATA: p_zm = gen_data_message(); break; @@ -1743,9 +1691,6 @@ void assert_eq_zenoh_message(_z_zenoh_message_t *left, _z_zenoh_message_t *right printf("\n"); switch (_Z_MID(left->_header)) { - case _Z_MID_Z_DECLARE: - assert_eq_declare_message(&left->_body._declare, &right->_body._declare); - break; case _Z_MID_Z_DATA: assert_eq_data_message(&left->_body._data, &right->_body._data, left->_header); break; @@ -1773,9 +1718,6 @@ void zenoh_message(void) { printf(" - "); switch (_Z_MID(e_zm._header)) { - case _Z_MID_Z_DECLARE: - printf("Declare message"); - break; case _Z_MID_Z_DATA: printf("Data message"); break; @@ -1825,7 +1767,126 @@ void zenoh_message(void) { } /*=============================*/ -/* Transport Messages */ +/* Network Messages */ +/*=============================*/ +/*------------------ Declare message ------------------*/ +_z_network_message_t gen_declare_message(void) { + _z_declaration_t declaration = gen_declaration(); + + return _z_n_msg_make_declare(declaration); +} + +void assert_eq_declare_message(_z_n_msg_declare_t *left, _z_n_msg_declare_t *right) { + printf(" "); + assert_eq_declaration(&left->_declaration, &right->_declaration); + printf("\n"); +} + +void declare_message(void) { + printf("\n>> Declare message\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_network_message_t n_msg = gen_declare_message(); + assert(_Z_MID(n_msg._header) == _Z_MID_N_DECLARE); + + _z_n_msg_declare_t e_dcl = n_msg._body._declare; + + // Encode + int8_t res = _z_declare_encode(&wbf, &e_dcl); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_n_msg_declare_t d_dcl; + res = _z_declare_decode(&d_dcl, &zbf); + assert(res == _Z_RES_OK); + + assert_eq_declare_message(&e_dcl, &d_dcl); + + // Free + _z_n_msg_clear_declare(&d_dcl); + _z_n_msg_clear(&n_msg); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +/*------------------ Network message ------------------*/ +_z_network_message_t gen_network_message(void) { + _z_network_message_t p_nm; + + uint8_t mids[] = {_Z_MID_N_DECLARE}; + uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); + + switch (mids[i]) { + case _Z_MID_N_DECLARE: + p_nm = gen_declare_message(); + break; + default: + assert(0); + break; + } + + return p_nm; +} + +void assert_eq_network_message(_z_network_message_t *left, _z_network_message_t *right) { + // Test message + printf(" Header (%x:%x)", left->_header, right->_header); + assert(left->_header == right->_header); + printf("\n"); + + switch (_Z_MID(left->_header)) { + case _Z_MID_N_DECLARE: + assert_eq_declare_message(&left->_body._declare, &right->_body._declare); + break; + default: + assert(0); + break; + } +} + +void network_message(void) { + printf("\n>> Network message\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_network_message_t e_nm = gen_network_message(); + + printf(" - "); + switch (_Z_MID(e_nm._header)) { + case _Z_MID_N_DECLARE: + printf("Declare message"); + break; + default: + assert(0); + break; + } + printf("\n"); + + // Encode + int8_t res = _z_network_message_encode(&wbf, &e_nm); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_network_message_t d_nm; + res = _z_network_message_decode(&d_nm, &zbf); + assert(res == _Z_RES_OK); + + assert_eq_network_message(&e_nm, &d_nm); + + // Free + _z_n_msg_clear(&e_nm); + _z_n_msg_clear(&d_nm); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +/*=============================*/ +/* Transport Messages */ /*=============================*/ /*------------------ Scout Message ------------------*/ _z_scouting_message_t gen_scout_message(void) { @@ -2319,13 +2380,13 @@ _z_transport_message_t gen_frame_message(void) { _z_zint_t sn = gen_zint(); _Bool is_reliable = gen_bool(); - _z_zenoh_message_vec_t messages; + _z_network_message_vec_t messages; _z_zint_t num = (gen_zint() % 4) + 1; - messages = _z_vec_make(num); + messages = _z_network_message_vec_make(num); for (_z_zint_t i = 0; i < num; i++) { - _z_zenoh_message_t *p_zm = (_z_zenoh_message_t *)z_malloc(sizeof(_z_zenoh_message_t)); - *p_zm = gen_zenoh_message(); - _z_vec_append(&messages, p_zm); + _z_network_message_t *p_zm = (_z_network_message_t *)z_malloc(sizeof(_z_network_message_t)); + *p_zm = gen_network_message(); + _z_network_message_vec_append(&messages, p_zm); } return _z_t_msg_make_frame(sn, messages, is_reliable); @@ -2344,8 +2405,8 @@ void assert_eq_frame_message(_z_t_msg_frame_t *left, _z_t_msg_frame_t *right, ui assert(l_len == r_len); for (size_t i = 0; i < l_len; i++) - assert_eq_zenoh_message((_z_zenoh_message_t *)_z_vec_get(&left->_messages, i), - (_z_zenoh_message_t *)_z_vec_get(&right->_messages, i)); + assert_eq_network_message((_z_network_message_t *)_z_vec_get(&left->_messages, i), + (_z_network_message_t *)_z_vec_get(&right->_messages, i)); } void frame_message(void) { @@ -2749,7 +2810,7 @@ void batch(void) { // t_msg._body._frame._payload._fragment.start = NULL; // } else { // // Do not allocate the vector containing the messages -// t_msg._body._frame._payload._messages = _z_zenoh_message_vec_make(0); +// t_msg._body._frame._payload._messages = _z_network_message_vec_make(0); // } // return t_msg; From 93c498f9a61f54d3c00955076633c1a8e7b3e756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 31 Mar 2023 14:08:19 +0200 Subject: [PATCH 066/181] Add PUSH, REQUEST, RESPONSE and RESPONSE_FINAL messages --- include/zenoh-pico/protocol/msg.h | 145 ++++++++++++- include/zenoh-pico/protocol/msgcodec.h | 16 ++ src/protocol/msg.c | 103 +++++++++ src/protocol/msgcodec.c | 211 +++++++++++++++--- tests/z_msgcodec_test.c | 283 ++++++++++++++++++++++--- 5 files changed, 700 insertions(+), 58 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 84d01e712..99859af76 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -125,7 +125,7 @@ #define _Z_FLAG_T_FRAGMENT_M 0x40 // 1 << 6 // Close message flags: -// S Session Close if S==1 Session close or S==0 Link close +// S Session Close if S==1 Session close or S==0 Link close // Z Extensions if Z==1 then Zenoh extensions are present #define _Z_FLAG_T_CLOSE_S 0x20 // 1 << 5 @@ -135,11 +135,31 @@ #define _Z_FLAG_N_Z 0x80 // 1 << 7 // PUSH message flags: -// I ZenohID if I==1 then the ZenohID is present +// N Named if N==1 then the key expr has name/suffix +// M Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver // Z Extensions if Z==1 then Zenoh extensions are present #define _Z_FLAG_N_PUSH_N 0x20 // 1 << 5 #define _Z_FLAG_N_PUSH_M 0x40 // 1 << 6 +// REQUEST message flags: +// N Named if N==1 then the key expr has name/suffix +// M Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_N_REQUEST_N 0x20 // 1 << 5 +#define _Z_FLAG_N_REQUEST_M 0x40 // 1 << 6 + +// RESPONSE message flags: +// N Named if N==1 then the key expr has name/suffix +// M Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_N_RESPONSE_N 0x20 // 1 << 5 +#define _Z_FLAG_N_RESPONSE_M 0x40 // 1 << 6 + +// RESPONSE FINAL message flags: +// Z Extensions if Z==1 then Zenoh extensions are present +// #define _Z_FLAG_N_RESPONSE_X 0x20 // 1 << 5 +// #define _Z_FLAG_N_RESPONSE_X 0x40 // 1 << 6 + /* Attachment message flags */ #define _Z_FLAG_A_Z \ 0x20 // 1 << 5 | MixedSlices if Z==1 then the payload contains a mix of raw and shm_info payload @@ -637,9 +657,124 @@ typedef struct { } _z_n_msg_declare_t; void _z_n_msg_clear_declare(_z_n_msg_declare_t *dcl); +/*------------------ Push Message ------------------*/ +typedef union { +} _z_push_body_t; +void _z_push_body_clear(_z_push_body_t *msg); + +// Flags: +// - N: Named if N==1 then the keyexpr has name/suffix +// - M: Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver +// - Z: Extension if Z==1 then at least one extension is present +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|M|N| PUSH | +// +-+-+-+---------+ +// ~ key_scope:z16 ~ +// +---------------+ +// ~ key_suffix ~ if N==1 -- +// +---------------+ +// ~ [push_exts] ~ if Z==1 +// +---------------+ +// ~ PushBody ~ +// +---------------+ +// +typedef struct { + _z_keyexpr_t _key; + _z_push_body_t _body; +} _z_n_msg_push_t; +void _z_n_msg_clear_push(_z_n_msg_push_t *msg); + +/*------------------ Request Message ------------------*/ +typedef union { +} _z_request_body_t; +void _z_request_body_clear(_z_request_body_t *msg); + +// Flags: +// - N: Named if N==1 then the keyexpr has name/suffix +// - M: Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver +// - Z: Extension if Z==1 then at least one extension is present +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|M|N| REQUEST | +// +-+-+-+---------+ +// ~ request_id:z32~ +// +---------------+ +// ~ key_scope:z16 ~ +// +---------------+ +// ~ key_suffix ~ if N==1 -- +// +---------------+ +// ~ [req_exts] ~ if Z==1 +// +---------------+ +// ~ RequestBody ~ +// +---------------+ +// +typedef struct { + _z_zint_t _rid; + _z_keyexpr_t _key; + _z_request_body_t _body; +} _z_n_msg_request_t; +void _z_n_msg_clear_request(_z_n_msg_request_t *msg); + +/*------------------ Response Message ------------------*/ +typedef union { +} _z_response_body_t; +void _z_response_body_clear(_z_response_body_t *msg); + +// Flags: +// - N: Named if N==1 then the keyexpr has name/suffix +// - M: Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver +// - Z: Extension if Z==1 then at least one extension is present +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|M|N| RESPONSE| +// +-+-+-+---------+ +// ~ request_id:z32~ +// +---------------+ +// ~ key_scope:z16 ~ +// +---------------+ +// ~ key_suffix ~ if N==1 -- +// +---------------+ +// ~ [reply_exts] ~ if Z==1 +// +---------------+ +// ~ ReplyBody ~ +// +---------------+ +// +typedef struct { + _z_zint_t _rid; + _z_keyexpr_t _key; + _z_response_body_t _body; +} _z_n_msg_response_t; +void _z_n_msg_clear_response(_z_n_msg_response_t *msg); + +/*------------------ Response Final Message ------------------*/ +// Flags: +// - Z: Extension if Z==1 then at least one extension is present +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|M|N| ResFinal| +// +-+-+-+---------+ +// ~ request_id:z32~ +// +---------------+ +// ~ [reply_exts] ~ if Z==1 +// +---------------+ +// +typedef struct { + _z_zint_t _rid; +} _z_n_msg_response_final_t; +void _z_n_msg_clear_response_final(_z_n_msg_response_final_t *msg); + /*------------------ Zenoh Message ------------------*/ typedef union { _z_n_msg_declare_t _declare; + _z_n_msg_push_t _push; + _z_n_msg_request_t _request; + _z_n_msg_response_t _response; + _z_n_msg_response_final_t _response_f; } _z_network_body_t; typedef struct { _z_network_body_t _body; @@ -653,6 +788,12 @@ _Z_VEC_DEFINE(_z_network_message, _z_network_message_t) /*------------------ Builders ------------------*/ _z_network_message_t _z_n_msg_make_declare(_z_declaration_t declarations); +_z_network_message_t _z_n_msg_make_push(_z_keyexpr_t key, _z_push_body_t body, _Bool is_remote_mapping); +_z_network_message_t _z_n_msg_make_request(_z_zint_t rid, _z_keyexpr_t key, _z_request_body_t body, + _Bool is_remote_mapping); +_z_network_message_t _z_n_msg_make_response(_z_zint_t rid, _z_keyexpr_t key, _z_response_body_t body, + _Bool is_remote_mapping); +_z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid); /*=============================*/ /* Transport Messages */ diff --git a/include/zenoh-pico/protocol/msgcodec.h b/include/zenoh-pico/protocol/msgcodec.h index b1b3b42f9..0ed763181 100644 --- a/include/zenoh-pico/protocol/msgcodec.h +++ b/include/zenoh-pico/protocol/msgcodec.h @@ -143,6 +143,22 @@ int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_n_msg_declare_t *msg); int8_t _z_declare_decode(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf); int8_t _z_declare_decode_na(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf); +int8_t _z_push_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_push_t *msg); +int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_push_decode_na(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_request_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_request_t *msg); +int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_request_decode_na(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_response_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_t *msg); +int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_response_decode_na(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_response_final_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_final_t *msg); +int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_response_final_decode_na(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header); + // ------------------ Transport Message ------------------ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg); int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header); diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 59cd0caff..e161ae475 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -442,6 +442,12 @@ void _z_msg_free(_z_zenoh_message_t **msg) { /*=============================*/ /* Network Messages */ /*=============================*/ +void _z_push_body_clear(_z_push_body_t *msg) { (void)(msg); } + +void _z_request_body_clear(_z_request_body_t *msg) { (void)(msg); } + +void _z_response_body_clear(_z_response_body_t *msg) { (void)(msg); } + _z_network_message_t _z_n_msg_make_declare(_z_declaration_t declaration) { _z_network_message_t msg; msg._header = _Z_MID_N_DECLARE; @@ -454,9 +460,106 @@ _z_network_message_t _z_n_msg_make_declare(_z_declaration_t declaration) { void _z_n_msg_clear_declare(_z_n_msg_declare_t *msg) { _z_declaration_clear(&msg->_declaration); } +_z_network_message_t _z_n_msg_make_push(_z_keyexpr_t key, _z_push_body_t body, _Bool is_remote_mapping) { + _z_network_message_t msg; + msg._header = _Z_MID_N_PUSH; + + msg._body._push._key = key; + if (key._suffix != NULL) { + _Z_SET_FLAG(msg._header, _Z_FLAG_N_PUSH_N); + } + if (is_remote_mapping == true) { + _Z_SET_FLAG(msg._header, _Z_FLAG_N_PUSH_M); + } + msg._body._push._body = body; + + msg._extensions = _z_msg_ext_vec_make(0); + + return msg; +} + +void _z_n_msg_clear_push(_z_n_msg_push_t *msg) { + _z_keyexpr_clear(&msg->_key); + _z_push_body_clear(&msg->_body); +} + +_z_network_message_t _z_n_msg_make_request(_z_zint_t rid, _z_keyexpr_t key, _z_request_body_t body, + _Bool is_remote_mapping) { + _z_network_message_t msg; + msg._header = _Z_MID_N_REQUEST; + + msg._body._request._rid = rid; + msg._body._request._key = key; + if (key._suffix != NULL) { + _Z_SET_FLAG(msg._header, _Z_FLAG_N_REQUEST_N); + } + if (is_remote_mapping == true) { + _Z_SET_FLAG(msg._header, _Z_FLAG_N_REQUEST_M); + } + msg._body._request._body = body; + + msg._extensions = _z_msg_ext_vec_make(0); + + return msg; +} + +void _z_n_msg_clear_request(_z_n_msg_request_t *msg) { + _z_keyexpr_clear(&msg->_key); + _z_request_body_clear(&msg->_body); +} + +_z_network_message_t _z_n_msg_make_response(_z_zint_t rid, _z_keyexpr_t key, _z_response_body_t body, + _Bool is_remote_mapping) { + _z_network_message_t msg; + msg._header = _Z_MID_N_RESPONSE; + + msg._body._response._rid = rid; + msg._body._response._key = key; + if (key._suffix != NULL) { + _Z_SET_FLAG(msg._header, _Z_FLAG_N_RESPONSE_N); + } + if (is_remote_mapping == true) { + _Z_SET_FLAG(msg._header, _Z_FLAG_N_RESPONSE_M); + } + msg._body._response._body = body; + + msg._extensions = _z_msg_ext_vec_make(0); + + return msg; +} + +void _z_n_msg_clear_response(_z_n_msg_response_t *msg) { + _z_keyexpr_clear(&msg->_key); + _z_response_body_clear(&msg->_body); +} + +_z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid) { + _z_network_message_t msg; + msg._header = _Z_MID_N_RESPONSE_FINAL; + + msg._body._response_f._rid = rid; + msg._extensions = _z_msg_ext_vec_make(0); + + return msg; +} + +void _z_n_msg_clear_response_final(_z_n_msg_response_final_t *msg) { (void)(msg); } + void _z_n_msg_clear(_z_network_message_t *msg) { uint8_t mid = _Z_MID(msg->_header); switch (mid) { + case _Z_MID_N_PUSH: + _z_n_msg_clear_push(&msg->_body._push); + break; + case _Z_MID_N_REQUEST: + _z_n_msg_clear_request(&msg->_body._request); + break; + case _Z_MID_N_RESPONSE: + _z_n_msg_clear_response(&msg->_body._response); + break; + case _Z_MID_N_RESPONSE_FINAL: + _z_n_msg_clear_response_final(&msg->_body._response_f); + break; case _Z_MID_N_DECLARE: _z_n_msg_clear_declare(&msg->_body._declare); break; diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 0807074d1..af9b95df0 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -108,24 +108,24 @@ int8_t _z_subinfo_decode(_z_subinfo_t *si, _z_zbuf_t *zbf, uint8_t header) { } /*------------------ ResKey Field ------------------*/ -int8_t _z_keyexpr_encode(_z_wbuf_t *wbf, uint8_t header, const _z_keyexpr_t *fld) { +int8_t _z_keyexpr_encode(_z_wbuf_t *wbf, _Bool has_suffix, const _z_keyexpr_t *fld) { int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _RESKEY\n"); _Z_EC(_z_zint_encode(wbf, fld->_id)) - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_K) == true) { + if (has_suffix == true) { _Z_EC(_z_str_encode(wbf, fld->_suffix)) } return ret; } -int8_t _z_keyexpr_decode_na(_z_keyexpr_t *ke, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_keyexpr_decode_na(_z_keyexpr_t *ke, _z_zbuf_t *zbf, _Bool has_suffix) { _Z_DEBUG("Decoding _RESKEY\n"); int8_t ret = _Z_RES_OK; ret |= _z_zint_decode(&ke->_id, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_K) == true) { + if (has_suffix == true) { char *str = NULL; ret |= _z_str_decode(&str, zbf); if (ret == _Z_RES_OK) { @@ -298,7 +298,7 @@ int8_t _z_res_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_res_decl_t *d _Z_DEBUG("Encoding _Z_DECL_RESOURCE\n"); _Z_EC(_z_zint_encode(wbf, dcl->_id)) - ret |= _z_keyexpr_encode(wbf, header, &dcl->_key); + ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); return ret; } @@ -308,7 +308,7 @@ int8_t _z_res_decl_decode_na(_z_res_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) int8_t ret = _Z_RES_OK; ret |= _z_zint_decode(&dcl->_id, zbf); - ret |= _z_keyexpr_decode(&dcl->_key, zbf, header); + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); return ret; } @@ -322,7 +322,7 @@ int8_t _z_pub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_pub_decl_t *d int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_DECL_PUBLISHER\n"); - ret |= _z_keyexpr_encode(wbf, header, &dcl->_key); + ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); return ret; } @@ -331,7 +331,7 @@ int8_t _z_pub_decl_decode_na(_z_pub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) _Z_DEBUG("Decoding _Z_DECL_PUBLISHER\n"); int8_t ret = _Z_RES_OK; - ret |= _z_keyexpr_decode(&dcl->_key, zbf, header); + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); return ret; } @@ -345,7 +345,7 @@ int8_t _z_sub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_sub_decl_t *d int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_DECL_SUBSCRIBER\n"); - _Z_EC(_z_keyexpr_encode(wbf, header, &dcl->_key)) + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key)) if (_Z_HAS_FLAG(header, _Z_FLAG_Z_S) == true) { ret |= _z_subinfo_encode(wbf, &dcl->_subinfo); } @@ -357,7 +357,7 @@ int8_t _z_sub_decl_decode_na(_z_sub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) _Z_DEBUG("Decoding _Z_DECL_SUBSCRIBER\n"); int8_t ret = _Z_RES_OK; - ret |= _z_keyexpr_decode(&dcl->_key, zbf, header); + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_S) == true) { ret |= _z_subinfo_decode(&dcl->_subinfo, zbf, header); } else { @@ -382,7 +382,7 @@ int8_t _z_qle_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_qle_decl_t *d int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_DECL_QUERYABLE\n"); - _Z_EC(_z_keyexpr_encode(wbf, header, &dcl->_key)); + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key)); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Q) == true) { _Z_EC(_z_zint_encode(wbf, dcl->_complete)); @@ -396,7 +396,7 @@ int8_t _z_qle_decl_decode_na(_z_qle_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) _Z_DEBUG("Decoding _Z_DECL_QUERYABLE\n"); int8_t ret = _Z_RES_OK; - ret |= _z_keyexpr_decode(&dcl->_key, zbf, header); + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Q) == true) { ret |= _z_zint_decode(&dcl->_complete, zbf); ret |= _z_zint_decode(&dcl->_distance, zbf); @@ -440,7 +440,7 @@ int8_t _z_forget_pub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_forget int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_DECL_FORGET_PUBLISHER\n"); - ret |= _z_keyexpr_encode(wbf, header, &dcl->_key); + ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); return ret; } @@ -449,7 +449,7 @@ int8_t _z_forget_pub_decl_decode_na(_z_forget_pub_decl_t *dcl, _z_zbuf_t *zbf, u _Z_DEBUG("Decoding _Z_DECL_FORGET_PUBLISHER\n"); int8_t ret = _Z_RES_OK; - ret |= _z_keyexpr_decode(&dcl->_key, zbf, header); + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); return ret; } @@ -463,7 +463,7 @@ int8_t _z_forget_sub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_forget _Z_DEBUG("Encoding _Z_DECL_FORGET_PUBLISHER\n"); int8_t ret = _Z_RES_OK; - ret |= _z_keyexpr_encode(wbf, header, &dcl->_key); + ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); return ret; } @@ -472,7 +472,7 @@ int8_t _z_forget_sub_decl_decode_na(_z_forget_sub_decl_t *dcl, _z_zbuf_t *zbf, u _Z_DEBUG("Decoding _Z_DECL_FORGET_PUBLISHER\n"); int8_t ret = _Z_RES_OK; - ret |= _z_keyexpr_decode(&dcl->_key, zbf, header); + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); return ret; } @@ -486,7 +486,7 @@ int8_t _z_forget_qle_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_forget int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_DECL_FORGET_QUERYABLE\n"); - ret |= _z_keyexpr_encode(wbf, header, &dcl->_key); + ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); return ret; } @@ -495,7 +495,7 @@ int8_t _z_forget_qle_decl_decode_na(_z_forget_qle_decl_t *dcl, _z_zbuf_t *zbf, u _Z_DEBUG("Decoding _Z_DECL_FORGET_QUERYABLE\n"); int8_t ret = _Z_RES_OK; - ret |= _z_keyexpr_decode(&dcl->_key, zbf, header); + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); return ret; } @@ -685,7 +685,7 @@ int8_t _z_data_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_data_t *msg) int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_Z_DATA\n"); - _Z_EC(_z_keyexpr_encode(wbf, header, &msg->_key)) + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &msg->_key)) if (_Z_HAS_FLAG(header, _Z_FLAG_Z_I) == true) { _Z_EC(_z_data_info_encode(wbf, &msg->_info)) @@ -699,7 +699,7 @@ int8_t _z_data_decode_na(_z_msg_data_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_Z_DATA\n"); int8_t ret = _Z_RES_OK; - ret |= _z_keyexpr_decode(&msg->_key, zbf, header); + ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_I) == true) { ret |= _z_data_info_decode(&msg->_info, zbf); } else { @@ -725,7 +725,7 @@ int8_t _z_pull_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_pull_t *msg) int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_Z_PULL\n"); - _Z_EC(_z_keyexpr_encode(wbf, header, &msg->_key)) + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &msg->_key)) _Z_EC(_z_zint_encode(wbf, msg->_pull_id)) @@ -740,7 +740,7 @@ int8_t _z_pull_decode_na(_z_msg_pull_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_Z_PULL\n"); int8_t ret = _Z_RES_OK; - ret |= _z_keyexpr_decode(&msg->_key, zbf, header); + ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); ret |= _z_zint_decode(&msg->_pull_id, zbf); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_N) == true) { ret |= _z_zint_decode(&msg->_max_samples, zbf); @@ -770,7 +770,7 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_query_t *msg int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_Z_QUERY\n"); - _Z_EC(_z_keyexpr_encode(wbf, header, &msg->_key)) + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &msg->_key)) _Z_EC(_z_str_encode(wbf, msg->_parameters)) @@ -802,7 +802,7 @@ int8_t _z_query_decode_na(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_Z_QUERY\n"); int8_t ret = _Z_RES_OK; - ret |= _z_keyexpr_decode(&msg->_key, zbf, header); + ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); ret |= _z_str_decode(&msg->_parameters, zbf); ret |= _z_zint_decode(&msg->_qid, zbf); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_T) == true) { @@ -961,6 +961,169 @@ int8_t _z_declare_decode_na(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf) { int8_t _z_declare_decode(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf) { return _z_declare_decode_na(msg, zbf); } +/*------------------ Push Body Field ------------------*/ +int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { + (void)(wbf); + (void)(pshb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_push_body_decode_na(_z_push_body_t *pshb, _z_zbuf_t *zbf) { + (void)(zbf); + (void)(pshb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf) { return _z_push_body_decode_na(pshb, zbf); } + +/*------------------ Push Message ------------------*/ +int8_t _z_push_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_push_t *msg) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_N_PUSH\n"); + + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N), &msg->_key)); + _Z_EC(_z_push_body_encode(wbf, &msg->_body)); + + return ret; +} + +int8_t _z_push_decode_na(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_N_PUSH\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N)); + ret |= _z_push_body_decode(&msg->_body, zbf); + + return ret; +} + +int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_push_decode_na(msg, zbf, header); +} + +/*------------------ Request Body Field ------------------*/ +int8_t _z_request_body_encode(_z_wbuf_t *wbf, const _z_request_body_t *reqb) { + (void)(wbf); + (void)(reqb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_request_body_decode_na(_z_request_body_t *reqb, _z_zbuf_t *zbf) { + (void)(zbf); + (void)(reqb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_request_body_decode(_z_request_body_t *reqb, _z_zbuf_t *zbf) { return _z_request_body_decode_na(reqb, zbf); } + +/*------------------ Request Message ------------------*/ +int8_t _z_request_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_request_t *msg) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_N_REQUEST\n"); + + _Z_EC(_z_zint_encode(wbf, msg->_rid)); + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N), &msg->_key)); + _Z_EC(_z_request_body_encode(wbf, &msg->_body)); + + return ret; +} + +int8_t _z_request_decode_na(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_N_REQUEST\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_zint_decode(&msg->_rid, zbf); + ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N)); + ret |= _z_request_body_decode(&msg->_body, zbf); + + return ret; +} + +int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_request_decode_na(msg, zbf, header); +} + +/*------------------ Response Body Field ------------------*/ +int8_t _z_response_body_encode(_z_wbuf_t *wbf, const _z_response_body_t *rspb) { + (void)(wbf); + (void)(rspb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_response_body_decode_na(_z_response_body_t *rspb, _z_zbuf_t *zbf) { + (void)(zbf); + (void)(rspb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_response_body_decode(_z_response_body_t *rspb, _z_zbuf_t *zbf) { + return _z_response_body_decode_na(rspb, zbf); +} + +/*------------------ Response Message ------------------*/ +int8_t _z_response_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_t *msg) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); + + _Z_EC(_z_zint_encode(wbf, msg->_rid)); + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N), &msg->_key)); + _Z_EC(_z_response_body_encode(wbf, &msg->_body)); + + return ret; +} + +int8_t _z_response_decode_na(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_zint_decode(&msg->_rid, zbf); + ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N)); + ret |= _z_response_body_decode(&msg->_body, zbf); + + return ret; +} + +int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_response_decode_na(msg, zbf, header); +} + +/*------------------ Response Final Message ------------------*/ +int8_t _z_response_final_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_final_t *msg) { + (void)(header); + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); + + _Z_EC(_z_zint_encode(wbf, msg->_rid)); + + return ret; +} + +int8_t _z_response_final_decode_na(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { + (void)(header); + _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_zint_decode(&msg->_rid, zbf); + + return ret; +} + +int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_response_final_decode_na(msg, zbf, header); +} + /*------------------ Network Message ------------------*/ int8_t _z_network_message_encode(_z_wbuf_t *wbf, const _z_network_message_t *msg) { int8_t ret = _Z_RES_OK; diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index e9fc6fac9..677d1e426 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -655,7 +655,7 @@ void subinfo_field(void) { } /*------------------ ResKey field ------------------*/ -_z_keyexpr_t gen_res_key(void) { +_z_keyexpr_t gen_keyexpr(void) { _z_keyexpr_t key; key._id = gen_zint(); _Bool is_numerical = gen_bool(); @@ -667,7 +667,7 @@ _z_keyexpr_t gen_res_key(void) { return key; } -void assert_eq_res_key(_z_keyexpr_t *left, _z_keyexpr_t *right, uint8_t header) { +void assert_eq_keyexpr(_z_keyexpr_t *left, _z_keyexpr_t *right, uint8_t header) { printf("ResKey -> "); printf("ID (%zu:%zu), ", left->_id, right->_id); assert(left->_id == right->_id); @@ -682,27 +682,27 @@ void assert_eq_res_key(_z_keyexpr_t *left, _z_keyexpr_t *right, uint8_t header) printf(")"); } -void res_key_field(void) { +void keyexpr_field(void) { printf("\n>> ResKey field\n"); _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - _z_keyexpr_t e_rk = gen_res_key(); + _z_keyexpr_t e_rk = gen_keyexpr(); // Encode uint8_t header = (e_rk._suffix) ? _Z_FLAG_Z_K : 0; - int8_t res = _z_keyexpr_encode(&wbf, header, &e_rk); + int8_t res = _z_keyexpr_encode(&wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &e_rk); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); _z_keyexpr_t d_rk; - res = _z_keyexpr_decode(&d_rk, &zbf, header); + res = _z_keyexpr_decode(&d_rk, &zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); assert(res == _Z_RES_OK); printf(" "); - assert_eq_res_key(&e_rk, &d_rk, header); + assert_eq_keyexpr(&e_rk, &d_rk, header); printf("\n"); // Free @@ -960,7 +960,7 @@ _z_res_decl_t gen_resource_declaration(uint8_t *header) { _z_res_decl_t e_rd; e_rd._id = gen_zint(); - e_rd._key = gen_res_key(); + e_rd._key = gen_keyexpr(); _Z_SET_FLAG(*header, (e_rd._key._suffix) ? _Z_FLAG_Z_K : 0); return e_rd; @@ -969,7 +969,7 @@ _z_res_decl_t gen_resource_declaration(uint8_t *header) { void assert_eq_resource_declaration(_z_res_decl_t *left, _z_res_decl_t *right, uint8_t header) { printf("RID (%zu:%zu), ", left->_id, right->_id); assert(left->_id == right->_id); - assert_eq_res_key(&left->_key, &right->_key, header); + assert_eq_keyexpr(&left->_key, &right->_key, header); } void resource_declaration(void) { @@ -1006,14 +1006,14 @@ void resource_declaration(void) { _z_pub_decl_t gen_publisher_declaration(uint8_t *header) { _z_pub_decl_t e_pd; - e_pd._key = gen_res_key(); + e_pd._key = gen_keyexpr(); _Z_SET_FLAG(*header, (e_pd._key._suffix) ? _Z_FLAG_Z_K : 0); return e_pd; } void assert_eq_publisher_declaration(_z_pub_decl_t *left, _z_pub_decl_t *right, uint8_t header) { - assert_eq_res_key(&left->_key, &right->_key, header); + assert_eq_keyexpr(&left->_key, &right->_key, header); } void publisher_declaration(void) { @@ -1054,14 +1054,14 @@ _z_sub_decl_t gen_subscriber_declaration(uint8_t *header) { if (e_sd._subinfo.mode != Z_SUBMODE_PUSH || e_sd._subinfo.period.period != 0) _Z_SET_FLAG(*header, _Z_FLAG_Z_S); if (e_sd._subinfo.reliability == Z_RELIABILITY_RELIABLE) _Z_SET_FLAG(*header, _Z_FLAG_Z_R); - e_sd._key = gen_res_key(); + e_sd._key = gen_keyexpr(); if (e_sd._key._suffix) _Z_SET_FLAG(*header, _Z_FLAG_Z_K); return e_sd; } void assert_eq_subscriber_declaration(_z_sub_decl_t *left, _z_sub_decl_t *right, uint8_t header) { - assert_eq_res_key(&left->_key, &right->_key, header); + assert_eq_keyexpr(&left->_key, &right->_key, header); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_S) == true) { printf(", "); assert_eq_subinfo(&left->_subinfo, &right->_subinfo); @@ -1102,7 +1102,7 @@ void subscriber_declaration(void) { _z_qle_decl_t gen_queryable_declaration(uint8_t *header) { _z_qle_decl_t e_qd; - e_qd._key = gen_res_key(); + e_qd._key = gen_keyexpr(); _Z_SET_FLAG(*header, (e_qd._key._suffix) ? _Z_FLAG_Z_K : 0); if (gen_bool()) { @@ -1115,7 +1115,7 @@ _z_qle_decl_t gen_queryable_declaration(uint8_t *header) { } void assert_eq_queryable_declaration(_z_qle_decl_t *left, _z_qle_decl_t *right, uint8_t header) { - assert_eq_res_key(&left->_key, &right->_key, header); + assert_eq_keyexpr(&left->_key, &right->_key, header); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_I) == true) { printf("Complete (%zu:%zu), ", left->_complete, right->_complete); @@ -1201,14 +1201,14 @@ void forget_resource_declaration(void) { _z_forget_pub_decl_t gen_forget_publisher_declaration(uint8_t *header) { _z_forget_pub_decl_t e_fpd; - e_fpd._key = gen_res_key(); + e_fpd._key = gen_keyexpr(); _Z_SET_FLAG(*header, (e_fpd._key._suffix) ? _Z_FLAG_Z_K : 0); return e_fpd; } void assert_eq_forget_publisher_declaration(_z_forget_pub_decl_t *left, _z_forget_pub_decl_t *right, uint8_t header) { - assert_eq_res_key(&left->_key, &right->_key, header); + assert_eq_keyexpr(&left->_key, &right->_key, header); } void forget_publisher_declaration(void) { @@ -1245,14 +1245,14 @@ void forget_publisher_declaration(void) { _z_forget_sub_decl_t gen_forget_subscriber_declaration(uint8_t *header) { _z_forget_sub_decl_t e_fsd; - e_fsd._key = gen_res_key(); + e_fsd._key = gen_keyexpr(); _Z_SET_FLAG(*header, (e_fsd._key._suffix) ? _Z_FLAG_Z_K : 0); return e_fsd; } void assert_eq_forget_subscriber_declaration(_z_forget_sub_decl_t *left, _z_forget_sub_decl_t *right, uint8_t header) { - assert_eq_res_key(&left->_key, &right->_key, header); + assert_eq_keyexpr(&left->_key, &right->_key, header); } void forget_subscriber_declaration(void) { @@ -1289,14 +1289,14 @@ void forget_subscriber_declaration(void) { _z_forget_qle_decl_t gen_forget_queryable_declaration(uint8_t *header) { _z_forget_qle_decl_t e_fqd; - e_fqd._key = gen_res_key(); + e_fqd._key = gen_keyexpr(); _Z_SET_FLAG(*header, (e_fqd._key._suffix) ? _Z_FLAG_Z_K : 0); return e_fqd; } void assert_eq_forget_queryable_declaration(_z_forget_qle_decl_t *left, _z_forget_qle_decl_t *right, uint8_t header) { - assert_eq_res_key(&left->_key, &right->_key, header); + assert_eq_keyexpr(&left->_key, &right->_key, header); } void forget_queryable_declaration(void) { @@ -1406,7 +1406,7 @@ void assert_eq_declaration(_z_declaration_t *left, _z_declaration_t *right) { /*=============================*/ /*------------------ Data message ------------------*/ _z_zenoh_message_t gen_data_message(void) { - _z_keyexpr_t key = gen_res_key(); + _z_keyexpr_t key = gen_keyexpr(); _z_data_info_t info; if (gen_bool()) { @@ -1429,7 +1429,7 @@ _z_zenoh_message_t gen_data_message(void) { void assert_eq_data_message(_z_msg_data_t *left, _z_msg_data_t *right, uint8_t header) { printf(" "); - assert_eq_res_key(&left->_key, &right->_key, header); + assert_eq_keyexpr(&left->_key, &right->_key, header); printf("\n"); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_I) == true) { printf(" "); @@ -1473,7 +1473,7 @@ void data_message(void) { /*------------------ Pull message ------------------*/ _z_zenoh_message_t gen_pull_message(void) { - _z_keyexpr_t key = gen_res_key(); + _z_keyexpr_t key = gen_keyexpr(); _z_zint_t pull_id = gen_zint(); _z_zint_t max_samples = gen_bool() ? gen_zint() : 0; _Bool is_final = gen_bool(); @@ -1483,7 +1483,7 @@ _z_zenoh_message_t gen_pull_message(void) { void assert_eq_pull_message(_z_msg_pull_t *left, _z_msg_pull_t *right, uint8_t header) { printf(" "); - assert_eq_res_key(&left->_key, &right->_key, header); + assert_eq_keyexpr(&left->_key, &right->_key, header); printf("\n"); printf(" Pull ID (%zu:%zu)", left->_pull_id, right->_pull_id); @@ -1529,7 +1529,7 @@ void pull_message(void) { /*------------------ Query message ------------------*/ _z_zenoh_message_t gen_query_message(void) { - _z_keyexpr_t key = gen_res_key(); + _z_keyexpr_t key = gen_keyexpr(); char *parameters = gen_str(gen_uint8() % 16); _z_zint_t qid = gen_zint(); @@ -1559,7 +1559,7 @@ _z_zenoh_message_t gen_query_message(void) { void assert_eq_query_message(_z_msg_query_t *left, _z_msg_query_t *right, uint8_t header) { printf(" "); - assert_eq_res_key(&left->_key, &right->_key, header); + assert_eq_keyexpr(&left->_key, &right->_key, header); printf("\n"); printf(" Predicate (%s:%s)", left->_parameters, right->_parameters); @@ -1886,7 +1886,215 @@ void network_message(void) { } /*=============================*/ -/* Transport Messages */ +/* Network Messages */ +/*=============================*/ +/*------------------ Push Message ------------------*/ +_z_network_message_t gen_push_message(void) { + _z_network_message_t n_msg; + + _z_keyexpr_t key = gen_keyexpr(); + _Bool is_remote = gen_bool(); + n_msg = _z_n_msg_make_push(key, (_z_push_body_t){}, is_remote); // TODO[protocol]: body + + return n_msg; +} + +void assert_eq_push_message(_z_n_msg_push_t *left, _z_n_msg_push_t *right, uint8_t header) { + printf(" "); + assert_eq_keyexpr(&left->_key, &right->_key, header); + printf("\n"); + + // TODO[protocol]: body +} + +void push_message(void) { + printf("\n>> Push message\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_network_message_t n_msg = gen_push_message(); + assert(_Z_MID(n_msg._header) == _Z_MID_N_PUSH); + + _z_n_msg_push_t e_pu = n_msg._body._push; + + // Encode + int8_t res = _z_push_encode(&wbf, n_msg._header, &e_pu); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_n_msg_push_t d_pu; + res = _z_push_decode(&d_pu, &zbf, n_msg._header); + assert(res == _Z_RES_OK); + + assert_eq_push_message(&e_pu, &d_pu, n_msg._header); + + // Free + _z_n_msg_clear_push(&d_pu); + _z_n_msg_clear(&n_msg); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +/*------------------ Request Message ------------------*/ +_z_network_message_t gen_request_message(void) { + _z_network_message_t n_msg; + + _z_zint_t rid = gen_zint(); + _z_keyexpr_t key = gen_keyexpr(); + _Bool is_remote = gen_bool(); + n_msg = _z_n_msg_make_request(rid, key, (_z_request_body_t){}, is_remote); // TODO[protocol]: body + + return n_msg; +} + +void assert_eq_request_message(_z_n_msg_request_t *left, _z_n_msg_request_t *right, uint8_t header) { + printf(" "); + assert(left->_rid == right->_rid); + printf("\n"); + + printf(" "); + assert_eq_keyexpr(&left->_key, &right->_key, header); + printf("\n"); + + // TODO[protocol]: body +} + +void request_message(void) { + printf("\n>> Request message\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_network_message_t n_msg = gen_request_message(); + assert(_Z_MID(n_msg._header) == _Z_MID_N_REQUEST); + + _z_n_msg_request_t e_re = n_msg._body._request; + + // Encode + int8_t res = _z_request_encode(&wbf, n_msg._header, &e_re); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_n_msg_request_t d_re; + res = _z_request_decode(&d_re, &zbf, n_msg._header); + assert(res == _Z_RES_OK); + + assert_eq_request_message(&e_re, &d_re, n_msg._header); + + // Free + _z_n_msg_clear_request(&d_re); + _z_n_msg_clear(&n_msg); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +/*------------------ Response Message ------------------*/ +_z_network_message_t gen_response_message(void) { + _z_network_message_t n_msg; + + _z_zint_t rid = gen_zint(); + _z_keyexpr_t key = gen_keyexpr(); + _Bool is_remote = gen_bool(); + n_msg = _z_n_msg_make_response(rid, key, (_z_response_body_t){}, is_remote); // TODO[protocol]: body + + return n_msg; +} + +void assert_eq_response_message(_z_n_msg_response_t *left, _z_n_msg_response_t *right, uint8_t header) { + printf(" "); + assert(left->_rid == right->_rid); + printf("\n"); + + printf(" "); + assert_eq_keyexpr(&left->_key, &right->_key, header); + printf("\n"); + + // TODO[protocol]: body +} + +void response_message(void) { + printf("\n>> Response message\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_network_message_t n_msg = gen_response_message(); + assert(_Z_MID(n_msg._header) == _Z_MID_N_RESPONSE); + + _z_n_msg_response_t e_re = n_msg._body._response; + + // Encode + int8_t res = _z_response_encode(&wbf, n_msg._header, &e_re); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_n_msg_response_t d_re; + res = _z_response_decode(&d_re, &zbf, n_msg._header); + assert(res == _Z_RES_OK); + + assert_eq_response_message(&e_re, &d_re, n_msg._header); + + // Free + _z_n_msg_clear_response(&d_re); + _z_n_msg_clear(&n_msg); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +/*------------------ Response Final Message ------------------*/ +_z_network_message_t gen_response_final_message(void) { + _z_network_message_t n_msg; + + _z_zint_t rid = gen_zint(); + n_msg = _z_n_msg_make_response_final(rid); + + return n_msg; +} + +void assert_eq_response_final_message(_z_n_msg_response_final_t *left, _z_n_msg_response_final_t *right) { + printf(" "); + assert(left->_rid == right->_rid); + printf("\n"); + + // TODO[protocol]: body +} + +void response_final_message(void) { + printf("\n>> Response Final message\n"); + _z_wbuf_t wbf = gen_wbuf(65535); + + // Initialize + _z_network_message_t n_msg = gen_response_final_message(); + assert(_Z_MID(n_msg._header) == _Z_MID_N_RESPONSE_FINAL); + + _z_n_msg_response_final_t e_re = n_msg._body._response_f; + + // Encode + int8_t res = _z_response_final_encode(&wbf, n_msg._header, &e_re); + assert(res == _Z_RES_OK); + (void)(res); + + // Decode + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + _z_n_msg_response_final_t d_re; + res = _z_response_final_decode(&d_re, &zbf, n_msg._header); + assert(res == _Z_RES_OK); + + assert_eq_response_final_message(&e_re, &d_re); + + // Free + _z_n_msg_clear_response_final(&d_re); + _z_n_msg_clear(&n_msg); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +/*=============================*/ +/* Scouting Messages */ /*=============================*/ /*------------------ Scout Message ------------------*/ _z_scouting_message_t gen_scout_message(void) { @@ -2003,6 +2211,9 @@ void hello_message(void) { _z_wbuf_clear(&wbf); } +/*=============================*/ +/* Transport Messages */ +/*=============================*/ /*------------------ Join Message ------------------*/ _z_transport_message_t gen_join_message(void) { z_whatami_t whatami = (gen_uint8() % 2) + 1; @@ -2962,7 +3173,7 @@ int main(void) { payload_field(); timestamp_field(); subinfo_field(); - res_key_field(); + keyexpr_field(); data_info_field(); // Message decorators @@ -2986,9 +3197,13 @@ int main(void) { query_message(); zenoh_message(); - // Session messages - scout_message(); - hello_message(); + // Network messages + push_message(); + request_message(); + response_message(); + response_final_message(); + + // Transport messages join_message(); init_message(); open_message(); @@ -2999,6 +3214,10 @@ int main(void) { transport_message(); batch(); // fragmentation(); + + // Scouting messages + scout_message(); + hello_message(); } return 0; From 47af506282b6f7eafac3ca65a5f08afd3d16a529 Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Mon, 3 Apr 2023 16:06:48 +0200 Subject: [PATCH 067/181] Make CMakeLists.txt includable (#189) * examples CMakeLists in separate directory * make cmakelists includable * windows build fix * obsolete JNI_PLATFORM_NAME removed * helpers.cmake formatted * gitignore updated * cmake-format added --- .cmake-format.py | 7 + CMakeLists.txt | 140 ++++++++------------ cmake/helpers.cmake | 286 ++++++++++++++++++++++++++++++++++++++++ examples/CMakeLists.txt | 56 ++++++++ 4 files changed, 405 insertions(+), 84 deletions(-) create mode 100644 .cmake-format.py create mode 100644 cmake/helpers.cmake create mode 100644 examples/CMakeLists.txt diff --git a/.cmake-format.py b/.cmake-format.py new file mode 100644 index 000000000..c1eb2af17 --- /dev/null +++ b/.cmake-format.py @@ -0,0 +1,7 @@ +with section("format"): + line_width = 120 + max_subgroups_hwrap = 4 + max_pargs_hwrap = 10 + +with section("markup"): + enable_markup = False diff --git a/CMakeLists.txt b/CMakeLists.txt index 74a12f7f6..287d55747 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,30 @@ cmake_minimum_required(VERSION 3.8) project(libzenohpico VERSION 0.7.0.1 LANGUAGES C) +option(BUILD_SHARED_LIBS "Build shared libraries if ON, otherwise build static libraries" ON) +option(ZENOH_DEBUG "Use this to set the ZENOH_DEBUG variable." 0) +option(WITH_ZEPHYR "Build for Zephyr RTOS" OFF) + +if(CMAKE_SYSTEM_NAME MATCHES "Windows") + set(BUILD_SHARED_LIBS "OFF") +elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") + if(WITH_ZEPHYR) + set(BUILD_SHARED_LIBS "OFF") + endif() +endif() + +set(Libname "zenohpico") +if(BUILD_SHARED_LIBS) + add_library(${Libname} SHARED) +else() + add_library(${Libname} STATIC) +endif() + +function(add_definition value) + add_definitions(-D${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) @@ -25,7 +49,7 @@ if(NOT CMAKE_C_STANDARD) endif() endif() set(CMAKE_C_STANDARD_REQUIRED TRUE) -add_definitions(-DZENOH_C_STANDARD=${CMAKE_C_STANDARD}) +add_definition(ZENOH_C_STANDARD=${CMAKE_C_STANDARD}) # while in development, use timestamp for patch version: string(TIMESTAMP PROJECT_VERSION_PATCH "%Y%m%ddev") @@ -37,49 +61,33 @@ if(NOT CMAKE_BUILD_TYPE) endif() string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE) -option(BUILD_SHARED_LIBS "Build shared libraries if ON, otherwise build static libraries" ON) -option(PACKAGING "Use option on Linux to produce Debian and RPM packages." OFF) -option(BUILD_EXAMPLES "Use this to also build the examples." ON) -option(BUILD_TOOLS "Use this to also build the tools." OFF) -option(BUILD_TESTING "Use this to also build tests." ON) -option(BUILD_INTEGRATION "Use this to also build integration tests." OFF) -option(ZENOH_DEBUG "Use this to set the ZENOH_DEBUG variable." 0) -option(WITH_ZEPHYR "Build for Zephyr RTOS" OFF) - if(CMAKE_SYSTEM_NAME MATCHES "Linux") - add_definitions(-DZENOH_LINUX) - set(JNI_PLATFORM_NAME "linux") + add_definition(ZENOH_LINUX) elseif(CMAKE_SYSTEM_NAME MATCHES "BSD") - add_definitions(-DZENOH_BSD) + add_definition(ZENOH_BSD) elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin") - add_definitions(-DZENOH_MACOS) + add_definition(ZENOH_MACOS) set(MACOSX_RPATH "ON") elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten") - add_definitions(-DZENOH_EMSCRIPTEN) + add_definition(ZENOH_EMSCRIPTEN) elseif(CMAKE_SYSTEM_NAME MATCHES "Windows") - add_definitions(-DZENOH_WINDOWS -D_CRT_SECURE_NO_WARNINGS) - set(BUILD_SHARED_LIBS "OFF") - add_definitions(-DZENOH_NO_STDATOMIC) + add_definition(ZENOH_WINDOWS) + add_definition(_CRT_SECURE_NO_WARNINGS) + add_definition(ZENOH_NO_STDATOMIC) elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") if(WITH_ZEPHYR) - add_definitions(-DZENOH_ZEPHYR) - set(BUILD_SHARED_LIBS "OFF") + add_definition(ZENOH_ZEPHYR) endif() else() message(FATAL_ERROR "zenoh-pico is not yet available on ${CMAKE_SYSTEM_NAME} platform") return() endif() -add_definitions(-DZENOH_DEBUG=${ZENOH_DEBUG}) +add_definition(ZENOH_DEBUG=${ZENOH_DEBUG}) # Print summary of CMAKE configurations message(STATUS "Building in ${CMAKE_BUILD_TYPE} mode") message(STATUS "Build shared library: ${BUILD_SHARED_LIBS}") -message(STATUS "Produce Debian and RPM packages: ${PACKAGING}") -message(STATUS "Build examples: ${BUILD_EXAMPLES}") -message(STATUS "Build examples: ${BUILD_TOOLS}") -message(STATUS "Build tests: ${BUILD_TESTING}") -message(STATUS "Build integration: ${BUILD_INTEGRATION}") message(STATUS "Zenoh Level Log: ${ZENOH_DEBUG}") message(STATUS "Build for Zephyr RTOS: ${WITH_ZEPHYR}") message(STATUS "Configuring for ${CMAKE_SYSTEM_NAME}") @@ -111,8 +119,6 @@ elseif(CMAKE_BUILD_TYPE MATCHES "RELEASE") endif() endif() -set(Libname "zenohpico") - file(GLOB PublicHeaders "include/*.h" "include/zenoh-pico/*.h" "include/zenoh-pico/api/*.h" @@ -126,9 +132,7 @@ file(GLOB PublicHeaders "include/*.h" "include/zenoh-pico/transport/*.h" "include/zenoh-pico/utils/*.h" ) -include_directories( - ${PROJECT_SOURCE_DIR}/include -) +target_include_directories(${Libname} PUBLIC ${PROJECT_SOURCE_DIR}/include) file(GLOB Sources "src/*.c" "src/api/*.c" @@ -170,7 +174,7 @@ endif() set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib) link_directories(${LIBRARY_OUTPUT_PATH}) -add_library(${Libname} ${Sources}) +target_sources(${Libname} PRIVATE ${Sources}) target_link_libraries(${Libname} Threads::Threads) @@ -182,6 +186,23 @@ if(CMAKE_SYSTEM_NAME MATCHES "Windows") target_link_libraries(${Libname} Ws2_32 Iphlpapi) endif() +# +# Build tests, examples, intallation only when project is root +# +if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) + +option(PACKAGING "Use option on Linux to produce Debian and RPM packages." OFF) +option(BUILD_EXAMPLES "Use this to also build the examples." ON) +option(BUILD_TOOLS "Use this to also build the tools." OFF) +option(BUILD_TESTING "Use this to also build tests." ON) +option(BUILD_INTEGRATION "Use this to also build integration tests." OFF) + +message(STATUS "Produce Debian and RPM packages: ${PACKAGING}") +message(STATUS "Build examples: ${BUILD_EXAMPLES}") +message(STATUS "Build tools: ${BUILD_TOOLS}") +message(STATUS "Build tests: ${BUILD_TESTING}") +message(STATUS "Build integration: ${BUILD_INTEGRATION}") + install(TARGETS ${Libname} LIBRARY DESTINATION lib ARCHIVE DESTINATION lib @@ -197,60 +218,9 @@ install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/zenoh-pico ) if(BUILD_EXAMPLES) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/examples) - - if(UNIX) - if(CMAKE_C_STANDARD MATCHES "99") - add_executable(z_put ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_put.c) - add_executable(z_pub ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_pub.c) - add_executable(z_pub_st ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_pub_st.c) - add_executable(z_sub ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_sub.c) - add_executable(z_sub_st ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_sub_st.c) - add_executable(z_pull ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_pull.c) - add_executable(z_get ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_get.c) - add_executable(z_queryable ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_queryable.c) - add_executable(z_info ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_info.c) - add_executable(z_scout ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_scout.c) - else() - add_executable(z_put ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_put.c) - add_executable(z_pub ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_pub.c) - add_executable(z_pub_st ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_pub_st.c) - add_executable(z_sub ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_sub.c) - add_executable(z_sub_st ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_sub_st.c) - add_executable(z_pull ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_pull.c) - add_executable(z_get ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_get.c) - add_executable(z_queryable ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_queryable.c) - add_executable(z_info ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_info.c) - add_executable(z_scout ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_scout.c) - endif() - elseif(MSVC) - add_executable(z_put ${PROJECT_SOURCE_DIR}/examples/windows/z_put.c) - add_executable(z_pub ${PROJECT_SOURCE_DIR}/examples/windows/z_pub.c) - add_executable(z_pub_st ${PROJECT_SOURCE_DIR}/examples/windows/z_pub_st.c) - add_executable(z_sub ${PROJECT_SOURCE_DIR}/examples/windows/z_sub.c) - add_executable(z_sub_st ${PROJECT_SOURCE_DIR}/examples/windows/z_sub_st.c) - add_executable(z_pull ${PROJECT_SOURCE_DIR}/examples/windows/z_pull.c) - add_executable(z_get ${PROJECT_SOURCE_DIR}/examples/windows/z_get.c) - add_executable(z_queryable ${PROJECT_SOURCE_DIR}/examples/windows/z_queryable.c) - add_executable(z_info ${PROJECT_SOURCE_DIR}/examples/windows/z_info.c) - add_executable(z_scout ${PROJECT_SOURCE_DIR}/examples/windows/z_scout.c) - endif() - - if(UNIX OR MSVC) - target_link_libraries(z_put ${Libname}) - target_link_libraries(z_pub ${Libname}) - target_link_libraries(z_pub_st ${Libname}) - target_link_libraries(z_sub ${Libname}) - target_link_libraries(z_sub_st ${Libname}) - target_link_libraries(z_pull ${Libname}) - target_link_libraries(z_get ${Libname}) - target_link_libraries(z_queryable ${Libname}) - target_link_libraries(z_info ${Libname}) - target_link_libraries(z_scout ${Libname}) - endif() + add_subdirectory(examples) endif() - if(UNIX OR MSVC) if(BUILD_TOOLS) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tools) @@ -392,3 +362,5 @@ if(PACKAGING) include(CPack) endif() + +endif() diff --git a/cmake/helpers.cmake b/cmake/helpers.cmake new file mode 100644 index 000000000..818b82c92 --- /dev/null +++ b/cmake/helpers.cmake @@ -0,0 +1,286 @@ +# +# Copyright (c) 2023 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, +# + +# +# DO NOT add include_guard() to this file +# It have to be reincluded multiple times on each 'include_project' call to restore +# functions and macros definitions +# + +# +# Show VARIABLE = value on configuration stage +# +macro(status_print var) + message(STATUS "${var} = ${${var}}") +endmacro() + +# +# Declare cache variable and print VARIABLE = value on configuration stage +# +function(declare_cache_var var default_value type docstring) + set(${var} ${default_value} CACHE ${type} ${docstring}) + status_print(${var}) +endfunction() + +# +# Declare cache variable which is set to TRUE if project is supposedly +# loaded as root project into vscode +# +function(declare_cache_var_true_if_vscode var docstring) + if(CMAKE_CURRENT_BINARY_DIR STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}/build") + set(in_vscode TRUE) + else() + set(in_vscode FALSE) + endif() + declare_cache_var(${var} ${in_vscode} BOOL ${docstring}) +endfunction() + +# +# Create target named 'debug_print' which prints VARIABLE = value +# when this target is built. Useful to debug generated expressions. +#` +function(debug_print var) + if(NOT TARGET debug_print) + add_custom_target(debug_print GLOBAL) + endif() + add_custom_command(COMMAND ${CMAKE_COMMAND} -E echo ${var} = ${${var}} TARGET debug_print) +endfunction() + +# +# Copy necessary dlls to target runtime directory +# +function(copy_dlls target) + if(WIN32) + add_custom_command( + TARGET ${target} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $ + $ COMMAND_EXPAND_LISTS) + endif() +endfunction() + +# +# Select default build config with support of multi config generators +# +macro(set_default_build_type config_type) + get_property(GENERATOR_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) + if(GENERATOR_IS_MULTI_CONFIG) + if(NOT DEFINED CMAKE_BUILD_TYPE) # if user passed argument '-DCMAKE_BUILD_TYPE=value', use it + set(CMAKE_BUILD_TYPE ${config_type}) + endif() + list(FIND CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} n) + if(n LESS 0) + message(FATAL_ERROR "Configuration ${CMAKE_BUILD_TYPE} is not in CMAKE_CONFIGURATION_TYPES") + else() + if(CMAKE_GENERATOR STREQUAL "Ninja Multi-Config") + set(CMAKE_DEFAULT_BUILD_TYPE ${CMAKE_BUILD_TYPE}) + status_print(CMAKE_DEFAULT_BUILD_TYPE) + else() + message(STATUS "Default build type is not supported for generator '${CMAKE_GENERATOR}'") + message(STATUS "use cmake --build . --config ${config_type}") + endif() + endif() + else() + if(CMAKE_BUILD_TYPE STREQUAL "") + set(CMAKE_BUILD_TYPE ${config_type}) + endif() + status_print(CMAKE_BUILD_TYPE) + endif() +endmacro() + +# Copy necessary dlls to target runtime directory +# +function(copy_dlls target) + if(WIN32) + add_custom_command( + TARGET ${target} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different $ + $ COMMAND_EXPAND_LISTS) + endif() +endfunction() + +# +# get property value avoiding CMake behavior - setting variable to -NOTFOUND for undefined property +# +function(get_target_property_if_set var target property) + get_property(is_set TARGET ${target} PROPERTY ${property} SET) + if(NOT is_set) + unset(${var} PARENT_SCOPE) + return() + endif() + get_property(value TARGET ${target} PROPERTY ${property}) + set(${var} ${value} PARENT_SCOPE) +endfunction() + +# +# Unset variables if they have empty string value +# +macro(unset_if_empty) + foreach(var ${ARGN}) + if("${${var}}" STREQUAL "") + unset(${var}) + endif() + endforeach() +endmacro() + +# +# Usage: +# +# include_project( TARGET +# < PATH ] [QUIET] | +# PACKAGE ] [QUIET] | +# GIT_URL [GIT_TAG ] > +# ) +# +# includes CMake project with one of the following ways: +# add_subdirectory(project_path) or +# find_package(package_name) or +# FetchContent(git_url) +# +# If target is already defined, does nothing. If parameter QUIET is passed, does nothing +# in case of failure to incude project from requested source. This allows to try to load project +# from first available source, like this: +# +# include_project(zenohc TARGET zenohc::lib PATH ..\zenoh_c QUIET) +# include_project(zenohc TARGET zenohc::lib PACKAGE zenohc QUIET) +# include_project(zenohc TARGET zenohc::lib GIT_URL https://github.com/eclipse-zenoh/zenoh-c) +# +# QUIET parameter not supported for GIT_URL due to lack of support of such mode in FetchContent +# +function(include_project) + __include_project(${ARGN}) + # recover functions which may be replaced by included project + include(${CMAKE_CURRENT_FUNCTION_LIST_FILE}) +endfunction() + +function(__include_project project_name) + include(FetchContent) + include(CMakeParseArguments) + cmake_parse_arguments(PARSE_ARGV 1 "ARG" "QUIET" "TARGET;PATH;PACKAGE;GIT_URL;GIT_TAG" "") + unset_if_empty(ARG_PATH ARG_TARGET ARG_PACKAGE ARG_GIT_URL) + if(NOT DEFINED ARG_TARGET) + message(FATAL_ERROR "Non-empty TARGET parameter is required") + endif() + if(TARGET ${ARG_TARGET}) + return() + endif() + + if(DEFINED ARG_PATH) + message(STATUS "trying to include project '${project_name} from directory '${ARG_PATH}'") + file(GLOB cmakelists ${ARG_PATH}/CMakeLists.txt) + if(NOT (cmakelists STREQUAL "")) + message(STATUS "found cmake project in directory, including it") + list(APPEND CMAKE_MESSAGE_INDENT " ") + add_subdirectory(${ARG_PATH} ${project_name}) + list(POP_BACK CMAKE_MESSAGE_INDENT) + if(TARGET ${ARG_TARGET} OR ARG_QUIET) + return() + endif() + message(FATAL_ERROR "Project at '${ARG_PATH}' should define target ${ARG_TARGET}") + elseif(ARG_QUIET) + return() + else() + message(FATAL_ERROR "no CMakeLists.txt file in '${ARG_PATH}'") + endif() + elseif(DEFINED ARG_PACKAGE) + message(STATUS "trying to include project '${project_name}' from package '${ARG_PACKAGE}'") + # Give priority to install directory + # Useful for development when older version of the project version may be installed in system + # + # TODO: "if( NOT TARGET" below should be not necessary + # (see https://cmake.org/cmake/help/latest/command/find_package.html, search for "override the order") + # but in fact cmake fails without it when zenohc is present both in CMAKE_INSTALL_PREFIX and in /usr/local. + # Consider is it still necessary after next bumping up cmake version + find_package(${ARG_PACKAGE} PATHS ${CMAKE_INSTALL_PREFIX} NO_DEFAULT_PATH QUIET) + if(NOT TARGET ${ARG_TARGET}) + find_package(${ARG_PACKAGE} QUIET) + endif() + set(package_path ${${ARG_PACKAGE}_CONFIG}) + if(TARGET ${ARG_TARGET}) + message(STATUS "found the package on path '${package_path}'") + return() + endif() + if(ARG_QUIET) + return() + endif() + if("${package_path}" STREQUAL "") + message(FATAL_ERROR "Package '${ARG_PACKAGE}' not found") + else() + message(FATAL_ERROR "Package '${ARG_PACKAGE}' on path '${package_path}' doesn't define target '${ARG_TARGET}") + endif() + elseif(DEFINED ARG_GIT_URL) + if(DEFINED ARG_GIT_TAG) + set(git_url "${ARG_GIT_URL}#{ARG_GIT_TAG}") + else() + set(git_url ${ARG_GIT_URL}) + endif() + message(STATUS "trying to include project '${project_name}' from git '${git_url}'") + list(APPEND CMAKE_MESSAGE_INDENT " ") + if(DEFINED ARG_GIT_TAG) + FetchContent_Declare(${project_name} GIT_REPOSITORY ${ARG_GIT_URL} GIT_TAG ${ARG_GIT_TAG}) + else() + FetchContent_Declare(${project_name} GIT_REPOSITORY ${ARG_GIT_URL}) + endif() + FetchContent_MakeAvailable(${project_name}) + list(POP_BACK CMAKE_MESSAGE_INDENT) + if(TARGET ${ARG_TARGET}) + return() + endif() + message(FATAL_ERROR "Project at ${git_url} should define target ${ARG_TARGET}") + else() + message(FATAL_ERROR "No source for project '${project_name}' specified") + endif() +endfunction() + +# +# Configure set of cache variables +# Include external project accordingly to these variables +# +# Example: +# +# configure_include_project(ZENOHC zenohc zenohc::lib ".." zenohc "https://github.com/eclipse-zenoh/zenoh-c" "") +# +# This command defines cache variables +# +# ZENOHC_SOURCE = "" +# ZENOHC_PATH = ".." +# ZENOHC_PACKAGE = "zenohc" +# ZENOHC_GIT_URL = "https://github.com/eclipse-zenoh/zenoh-c" +# ZENOHC_GIT_TAG = "" +# +# Then it tries to include the project with name 'zenohc' from first available source in order (PATH,PACKAGE,GIT_URL). +# Project should define target `zenohc::lib`, otherwise cmake configuration step fails with error. +# +# If ZENOHC_SOURCE is set by user to value PATH, PACKAGE or GIT_URL, then the project is included from this source only. +# +# For example: +# +# cmake ../zenoh-c/examples -DZENOHC_SOURCE=GIT_URL -DZENOHC_GIT_URL=https://github.com/username/zenoh-c +# +# makes 'examples' project to compile with zenoh-c from username's zenoh-c git repository +# +function(configure_include_project var_prefix project target path package git_url git_tag) + declare_cache_var( + ${var_prefix}_SOURCE "" STRING + "Explicit ${project} source type. Can be PATH, PACKAGE or GIT_URL. If empty, tries all these variants in order") + declare_cache_var(${var_prefix}_PATH "${path}" STRING "PATH to ${project} source directory") + declare_cache_var(${var_prefix}_PACKAGE "${package}" STRING "name of ${project} PACKAGE") + declare_cache_var(${var_prefix}_GIT_URL "${git_url}" STRING "GIT_URL of ${project} repository") + declare_cache_var(${var_prefix}_GIT_TAG "${git_tag}" STRING "GIT_TAG of ${project} repository") + if(${var_prefix}_SOURCE STREQUAL "") + include_project(${project} TARGET ${target} PATH ${${var_prefix}_PATH} QUIET) + include_project(${project} TARGET ${target} PACKAGE ${${var_prefix}_PACKAGE} QUIET) + include_project(${project} TARGET ${target} GIT_URL ${${var_prefix}_GIT_URL} GIT_TAG ${${var_prefix}_GIT_TAG}) + else() + include_project(${project} TARGET ${target} ${${var_prefix}_SOURCE} ${${var_prefix}_${${var_prefix}_SOURCE}} + GIT_TAG ${${var_prefix}_GIT_TAG}) + endif() +endfunction() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 000000000..a9a55b8d6 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,56 @@ +if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) + # Settings when 'examples' is the root projet + cmake_minimum_required(VERSION 3.20) + project(zenohpico_examples LANGUAGES C) + include(../cmake/helpers.cmake) + set_default_build_type(Release) + configure_include_project(ZENOHPICO zenohpico zenohpico ".." zenohc "https://github.com/eclipse-zenoh/zenoh-pico" "") + add_custom_target(examples ALL) +else() + message(STATUS "zenoh-pico examples") + add_custom_target(examples) +endif() + +function(add_example name) + add_executable(${name} ${ARGN}) + set_property(TARGET ${name} PROPERTY C_STANDARD 11) + target_link_libraries(${name} zenohpico) + add_dependencies(examples ${name}) +endfunction() + +if(UNIX) + if(CMAKE_C_STANDARD MATCHES "99") + add_example(z_put unix/c99/z_put.c) + add_example(z_pub unix/c99/z_pub.c) + add_example(z_pub_st unix/c99/z_pub_st.c) + add_example(z_sub unix/c99/z_sub.c) + add_example(z_sub_st unix/c99/z_sub_st.c) + add_example(z_pull unix/c99/z_pull.c) + add_example(z_get unix/c99/z_get.c) + add_example(z_queryable unix/c99/z_queryable.c) + add_example(z_info unix/c99/z_info.c) + add_example(z_scout unix/c99/z_scout.c) + else() + add_example(z_put unix/c11/z_put.c) + add_example(z_pub unix/c11/z_pub.c) + add_example(z_pub_st unix/c11/z_pub_st.c) + add_example(z_sub unix/c11/z_sub.c) + add_example(z_sub_st unix/c11/z_sub_st.c) + add_example(z_pull unix/c11/z_pull.c) + add_example(z_get unix/c11/z_get.c) + add_example(z_queryable unix/c11/z_queryable.c) + add_example(z_info unix/c11/z_info.c) + add_example(z_scout unix/c11/z_scout.c) + endif() +elseif(MSVC) + add_example(z_put windows/z_put.c) + add_example(z_pub windows/z_pub.c) + add_example(z_pub_st windows/z_pub_st.c) + add_example(z_sub windows/z_sub.c) + add_example(z_sub_st windows/z_sub_st.c) + add_example(z_pull windows/z_pull.c) + add_example(z_get windows/z_get.c) + add_example(z_queryable windows/z_queryable.c) + add_example(z_info windows/z_info.c) + add_example(z_scout windows/z_scout.c) +endif() From 53a691bc674fe2f94a6badb4c744fc3d927cbd0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 5 Apr 2023 14:00:08 +0200 Subject: [PATCH 068/181] Fix and improve transport read for single thread (#192) * Remove task execution artifact on single thread examples * Fix and improve transport read for single thread * Update return value type * Multicast must fwded address information --- examples/unix/c11/z_sub_st.c | 6 --- examples/windows/z_sub_st.c | 6 --- include/zenoh-pico/utils/result.h | 1 + src/transport/multicast/link/rx.c | 50 +++++++++++++----------- src/transport/unicast/link/rx.c | 53 ++++++++++++++------------ src/transport/unicast/link/task/read.c | 4 +- 6 files changed, 57 insertions(+), 63 deletions(-) diff --git a/examples/unix/c11/z_sub_st.c b/examples/unix/c11/z_sub_st.c index d14d68956..a007918a6 100644 --- a/examples/unix/c11/z_sub_st.c +++ b/examples/unix/c11/z_sub_st.c @@ -69,12 +69,6 @@ int main(int argc, char **argv) { return -1; } - // Start read and lease tasks for zenoh-pico - if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { - printf("Unable to start read and lease tasks"); - return -1; - } - z_owned_closure_sample_t callback = z_closure(data_handler); printf("Declaring Subscriber on '%s'...\n", keyexpr); z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL); diff --git a/examples/windows/z_sub_st.c b/examples/windows/z_sub_st.c index d89337c02..0a2f76692 100644 --- a/examples/windows/z_sub_st.c +++ b/examples/windows/z_sub_st.c @@ -46,12 +46,6 @@ int main(int argc, char **argv) { return -1; } - // Start read and lease tasks for zenoh-pico - if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { - printf("Unable to start read and lease tasks"); - return -1; - } - z_owned_closure_sample_t callback = z_closure(data_handler); printf("Declaring Subscriber on '%s'...\n", keyexpr); z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL); diff --git a/include/zenoh-pico/utils/result.h b/include/zenoh-pico/utils/result.h index 9118e0a34..4e6ed2838 100644 --- a/include/zenoh-pico/utils/result.h +++ b/include/zenoh-pico/utils/result.h @@ -47,6 +47,7 @@ typedef enum { _Z_ERR_TRANSPORT_TX_FAILED = -100, _Z_ERR_TRANSPORT_RX_FAILED = -99, _Z_ERR_TRANSPORT_NO_SPACE = -98, + _Z_ERR_TRANSPORT_NOT_ENOUGH_BYTES = -97, _Z_ERR_CONFIG_FAILED_INSERT = -95, _Z_ERR_CONFIG_UNSUPPORTED_CLIENT_MULTICAST = -94, diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 987a3ac2b..93fae8261 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -51,39 +51,43 @@ int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_me _z_mutex_lock(&ztm->_mutex_rx); #endif // Z_MULTI_THREAD == 1 - // Prepare the buffer - _z_zbuf_reset(&ztm->_zbuf); + do { + size_t to_read = 0; + if (_Z_LINK_IS_STREAMED(ztm->_link._capabilities) == true) { + if (_z_zbuf_len(&ztm->_zbuf) < _Z_MSG_LEN_ENC_SIZE) { + _z_link_recv_zbuf(&ztm->_link, &ztm->_zbuf, addr); + if (_z_zbuf_len(&ztm->_zbuf) < _Z_MSG_LEN_ENC_SIZE) { + _z_zbuf_compact(&ztm->_zbuf); + ret = _Z_ERR_TRANSPORT_NOT_ENOUGH_BYTES; + continue; + } + } - if (_Z_LINK_IS_STREAMED(ztm->_link._capabilities) == true) { - // Read the message length - if (_z_link_recv_exact_zbuf(&ztm->_link, &ztm->_zbuf, _Z_MSG_LEN_ENC_SIZE, addr) != _Z_MSG_LEN_ENC_SIZE) { - size_t len = 0; for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) { - len |= _z_zbuf_read(&ztm->_zbuf) << (i * (uint8_t)8); + to_read |= _z_zbuf_read(&ztm->_zbuf) << (i * (uint8_t)8); } - _Z_DEBUG(">> \t msg len = %zu\n", len); - size_t writable = _z_zbuf_capacity(&ztm->_zbuf) - _z_zbuf_len(&ztm->_zbuf); - if (writable < len) { - // Read enough bytes to decode the message - if (_z_link_recv_exact_zbuf(&ztm->_link, &ztm->_zbuf, len, addr) != len) { - ret = _Z_ERR_TRANSPORT_RX_FAILED; + 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) { + _z_zbuf_set_rpos(&ztm->_zbuf, _z_zbuf_get_rpos(&ztm->_zbuf) - _Z_MSG_LEN_ENC_SIZE); + _z_zbuf_compact(&ztm->_zbuf); + ret = _Z_ERR_TRANSPORT_NOT_ENOUGH_BYTES; + continue; } - } else { - ret = _Z_ERR_TRANSPORT_NO_SPACE; } } else { - ret = _Z_ERR_TRANSPORT_RX_FAILED; - } - } else { - if (_z_link_recv_zbuf(&ztm->_link, &ztm->_zbuf, addr) == SIZE_MAX) { - ret = _Z_ERR_TRANSPORT_RX_FAILED; + _z_zbuf_compact(&ztm->_zbuf); + to_read = _z_link_recv_zbuf(&ztm->_link, &ztm->_zbuf, addr); + if (to_read == SIZE_MAX) { + ret = _Z_ERR_TRANSPORT_RX_FAILED; + } } - } + } while (false); // The 1-iteration loop to use continue to break the entire loop on error - _Z_DEBUG(">> \t transport_message_decode\n"); if (ret == _Z_RES_OK) { - _z_transport_message_decode_na(t_msg, &ztm->_zbuf); + _Z_DEBUG(">> \t transport_message_decode\n"); + ret = _z_transport_message_decode_na(t_msg, &ztm->_zbuf); } #if Z_MULTI_THREAD == 1 diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index d8cd12a9e..5962d7100 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -26,50 +26,53 @@ int8_t _z_unicast_recv_t_msg_na(_z_transport_unicast_t *ztu, _z_transport_message_t *t_msg) { _Z_DEBUG(">> recv session msg\n"); int8_t ret = _Z_RES_OK; - #if Z_MULTI_THREAD == 1 // Acquire the lock _z_mutex_lock(&ztu->_mutex_rx); #endif // Z_MULTI_THREAD == 1 - // Prepare the buffer - _z_zbuf_reset(&ztu->_zbuf); + do { + size_t to_read = 0; + if (_Z_LINK_IS_STREAMED(ztu->_link._capabilities) == true) { + if (_z_zbuf_len(&ztu->_zbuf) < _Z_MSG_LEN_ENC_SIZE) { + _z_link_recv_zbuf(&ztu->_link, &ztu->_zbuf, NULL); + if (_z_zbuf_len(&ztu->_zbuf) < _Z_MSG_LEN_ENC_SIZE) { + _z_zbuf_compact(&ztu->_zbuf); + ret = _Z_ERR_TRANSPORT_NOT_ENOUGH_BYTES; + continue; + } + } - if (_Z_LINK_IS_STREAMED(ztu->_link._capabilities) == true) { - // Read the message length - if (_z_link_recv_exact_zbuf(&ztu->_link, &ztu->_zbuf, _Z_MSG_LEN_ENC_SIZE, NULL) != _Z_MSG_LEN_ENC_SIZE) { - size_t len = 0; for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) { - len |= _z_zbuf_read(&ztu->_zbuf) << (i * (uint8_t)8); + to_read |= _z_zbuf_read(&ztu->_zbuf) << (i * (uint8_t)8); } - _Z_DEBUG(">> \t msg len = %zu\n", len); - size_t writable = _z_zbuf_capacity(&ztu->_zbuf) - _z_zbuf_len(&ztu->_zbuf); - if (writable < len) { - // Read enough bytes to decode the message - if (_z_link_recv_exact_zbuf(&ztu->_link, &ztu->_zbuf, len, NULL) != len) { - ret = _Z_ERR_TRANSPORT_RX_FAILED; + 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) { + _z_zbuf_set_rpos(&ztu->_zbuf, _z_zbuf_get_rpos(&ztu->_zbuf) - _Z_MSG_LEN_ENC_SIZE); + _z_zbuf_compact(&ztu->_zbuf); + ret = _Z_ERR_TRANSPORT_NOT_ENOUGH_BYTES; + continue; } - } else { - ret = _Z_ERR_TRANSPORT_NO_SPACE; } } else { - ret = _Z_ERR_TRANSPORT_RX_FAILED; - } - } else { - if (_z_link_recv_zbuf(&ztu->_link, &ztu->_zbuf, NULL) == SIZE_MAX) { - ret = _Z_ERR_TRANSPORT_RX_FAILED; + _z_zbuf_compact(&ztu->_zbuf); + to_read = _z_link_recv_zbuf(&ztu->_link, &ztu->_zbuf, NULL); + if (to_read == SIZE_MAX) { + ret = _Z_ERR_TRANSPORT_RX_FAILED; + } } - } + } while (false); // The 1-iteration loop to use continue to break the entire loop on error - // Mark the session that we have received data if (ret == _Z_RES_OK) { + _Z_DEBUG(">> \t transport_message_decode\n"); ret = _z_transport_message_decode_na(t_msg, &ztu->_zbuf); + + // Mark the session that we have received data if (ret == _Z_RES_OK) { ztu->_received = true; } - - _Z_DEBUG(">> \t transport_message_decode\n"); } #if Z_MULTI_THREAD == 1 diff --git a/src/transport/unicast/link/task/read.c b/src/transport/unicast/link/task/read.c index 7f0cc14c7..af45dc5c9 100644 --- a/src/transport/unicast/link/task/read.c +++ b/src/transport/unicast/link/task/read.c @@ -39,8 +39,6 @@ void *_zp_unicast_read_task(void *ztu_arg) { #if Z_MULTI_THREAD == 1 _z_transport_unicast_t *ztu = (_z_transport_unicast_t *)ztu_arg; - uint8_t ret; - // Acquire and keep the lock _z_mutex_lock(&ztu->_mutex_rx); @@ -87,7 +85,7 @@ void *_zp_unicast_read_task(void *ztu_arg) { // Decode one session message _z_transport_message_t t_msg; - ret = _z_transport_message_decode_na(&t_msg, &zbuf); + int8_t ret = _z_transport_message_decode_na(&t_msg, &zbuf); if (ret == _Z_RES_OK) { ret = _z_unicast_handle_transport_message(ztu, &t_msg); From c79de85666337d7c2e049a3e5ab6d704adc98ca8 Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Thu, 6 Apr 2023 11:03:08 +0200 Subject: [PATCH 069/181] Changes to make zenohpico compatible with zenoh-cpp (#191) (#190) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * target_include_directories to export include path * null methods added * z_XXX_null() added * undoed separate z_str_array_t * StrArrayView val, len no underscores * bytes field order same as in zenoh-c * z_keyexpr_as_bytes added * timestamp updated * bytes_check, closure calls * z_keyexpr_new * const added * build tests, examples, installation only if root * keyexpr_unchecked, z_xxx_null * same fields order as in zenohc * trailing zero not included to z_keyexpr_as_bytes * create null owned keyexpr if null parameter passed * canonizer fix for one-character chunks * undef ndebug right before tests * return -1 only if keyexpr invalid * test corrected * session test * crash on close null session * fix crash on double session close * Update keyexpr functions * compilation in debug fixed * macros updated, tests for null drop added, crashes * build fix * user callback can take ownership on reply or hello * compilation fix * compilation fix * single _pop function * part of remarks fixed * correct return on out of memory * restored reply_t as field, cleanup in new * restored incorrectly moved lines * rename to alloc_and_move * make thread stop work immediately after run * add .clang-format * examples CMakeLists in separate directory * make cmakelists includable * windows build fix * obsolete JNI_PLATFORM_NAME removed * helpers.cmake formatted * gitignore updated * cmake-format added * restored old fields order, comments corrected --------- Co-authored-by: Carlos Guimarães --- include/zenoh-pico/api/macros.h | 426 +++++++++++------------- include/zenoh-pico/api/primitives.h | 49 ++- include/zenoh-pico/api/types.h | 27 +- include/zenoh-pico/collections/string.h | 4 +- include/zenoh-pico/protocol/core.h | 8 +- src/api/api.c | 50 ++- src/collections/string.c | 32 +- src/net/memory.c | 6 +- src/net/session.c | 4 +- src/protocol/msg.c | 2 +- src/protocol/msgcodec.c | 8 +- src/session/query.c | 6 +- src/session/scout.c | 8 +- src/session/utils.c | 14 +- tests/z_msgcodec_test.c | 24 +- tests/z_session_test.c | 9 +- 16 files changed, 372 insertions(+), 305 deletions(-) diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 3c4cd409f..23ed7eb21 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -32,19 +32,19 @@ * Returns: * Returns the loaned type associated with `x`. */ -#define z_loan(x) \ - _Generic((x), z_owned_keyexpr_t \ - : z_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_pull_subscriber_t \ - : z_pull_subscriber_loan, z_owned_publisher_t \ - : z_publisher_loan, z_owned_reply_t \ - : z_reply_loan, z_owned_hello_t \ - : z_hello_loan, z_owned_str_t \ - : z_str_loan, z_owned_str_array_t \ - : z_str_array_loan)(&x) +#define z_loan(x) _Generic((x), \ + z_owned_keyexpr_t : z_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_pull_subscriber_t : z_pull_subscriber_loan, \ + z_owned_publisher_t : z_publisher_loan, \ + z_owned_reply_t : z_reply_loan, \ + z_owned_hello_t : z_hello_loan, \ + z_owned_str_t : z_str_loan, \ + z_owned_str_array_t : z_str_array_loan \ + )(&x) /** * Defines a generic function for droping any of the ``z_owned_X_t`` types. * @@ -71,6 +71,30 @@ z_owned_closure_zid_t * : z_closure_zid_drop \ )(x) +/** + * Defines a generic function for making null object of any of the ``z_owned_X_t`` types. + * + * Returns: + * Returns the unitialized instance of `x`. + */ +#define z_null(x) (*x = _Generic((x), \ + z_owned_session_t * : z_session_null, \ + z_owned_publisher_t * : z_publisher_null, \ + z_owned_keyexpr_t * : z_keyexpr_null, \ + z_owned_config_t * : z_config_null, \ + z_owned_scouting_config_t * : z_scouting_config_null, \ + z_owned_pull_subscriber_t * : z_pull_subscriber_null, \ + z_owned_subscriber_t * : z_subscriber_null, \ + z_owned_queryable_t * : z_queryable_null, \ + z_owned_reply_t * : z_reply_null, \ + z_owned_hello_t * : z_hello_null, \ + z_owned_str_t * : z_str_null, \ + z_owned_closure_sample_t * : z_closure_sample_null, \ + z_owned_closure_query_t * : z_closure_query_null, \ + z_owned_closure_reply_t * : z_closure_reply_null, \ + z_owned_closure_hello_t * : z_closure_hello_null, \ + z_owned_closure_zid_t * : z_closure_zid_null \ + )()) /** * Defines a generic function for checking the validity of any of the ``z_owned_X_t`` types. * @@ -80,22 +104,38 @@ * Returns: * Returns ``true`` if valid, or ``false`` otherwise. */ -#define z_check(x) \ - _Generic((x), z_owned_keyexpr_t \ - : z_keyexpr_check, z_keyexpr_t \ - : z_keyexpr_is_initialized, z_value_t \ - : z_value_is_initialized, z_owned_config_t \ - : z_config_check, z_owned_scouting_config_t \ - : z_scouting_config_check, z_owned_session_t \ - : z_session_check, z_owned_subscriber_t \ - : z_subscriber_check, z_owned_pull_subscriber_t \ - : z_pull_subscriber_check, z_owned_publisher_t \ - : z_publisher_check, z_owned_queryable_t \ - : z_queryable_check, z_owned_reply_t \ - : z_reply_check, z_owned_hello_t \ - : z_hello_check, z_owned_str_t \ - : z_str_check, z_owned_str_array_t \ - : z_str_array_check)(&x) + +#define z_check(x) _Generic((x), \ + z_owned_keyexpr_t : z_keyexpr_check, \ + z_keyexpr_t : z_keyexpr_is_initialized, \ + z_value_t : z_value_is_initialized, \ + z_owned_config_t : z_config_check, \ + z_owned_scouting_config_t : z_scouting_config_check, \ + z_owned_session_t : z_session_check, \ + z_owned_subscriber_t : z_subscriber_check, \ + z_owned_pull_subscriber_t : z_pull_subscriber_check, \ + z_owned_publisher_t : z_publisher_check, \ + z_owned_queryable_t : z_queryable_check, \ + z_owned_reply_t : z_reply_check, \ + z_owned_hello_t : z_hello_check, \ + z_owned_str_t : z_str_check, \ + z_owned_str_array_t : z_str_array_check, \ + z_bytes_t : z_bytes_check \ + )(&x) + +/** + * Defines a generic function for calling closure stored in any of ``z_owned_closure_X_t`` + * + * Parameters: + * x: The closure to call + */ +#define z_call(x, ...) \ + _Generic((x), z_owned_closure_sample_t : z_closure_sample_call, \ + z_owned_closure_query_t : z_closure_query_call, \ + z_owned_closure_reply_t : z_closure_reply_call, \ + z_owned_closure_hello_t : z_closure_hello_call, \ + z_owned_closure_zid_t : z_closure_zid_call \ + ) (&x, __VA_ARGS__) /** * Defines a generic function for moving any of the ``z_owned_X_t`` types. @@ -106,25 +146,25 @@ * Returns: * Returns the instance associated with `x`. */ -#define z_move(x) \ - _Generic((x), z_owned_keyexpr_t \ - : z_keyexpr_move, z_owned_config_t \ - : z_config_move, z_owned_scouting_config_t \ - : z_scouting_config_move, z_owned_session_t \ - : z_session_move, z_owned_subscriber_t \ - : z_subscriber_move, z_owned_pull_subscriber_t \ - : z_pull_subscriber_move, z_owned_publisher_t \ - : z_publisher_move, z_owned_queryable_t \ - : z_queryable_move, z_owned_reply_t \ - : z_reply_move, z_owned_hello_t \ - : z_hello_move, z_owned_str_t \ - : z_str_move, z_owned_str_array_t \ - : z_str_array_move, z_owned_closure_sample_t \ - : z_closure_sample_move, z_owned_closure_query_t \ - : z_closure_query_move, z_owned_closure_reply_t \ - : z_closure_reply_move, z_owned_closure_hello_t \ - : z_closure_hello_move, z_owned_closure_zid_t \ - : z_closure_zid_move)(&x) +#define z_move(x) _Generic((x), \ + z_owned_keyexpr_t : z_keyexpr_move, \ + z_owned_config_t : z_config_move, \ + z_owned_scouting_config_t : z_scouting_config_move, \ + z_owned_session_t : z_session_move, \ + z_owned_subscriber_t : z_subscriber_move, \ + z_owned_pull_subscriber_t : z_pull_subscriber_move, \ + z_owned_publisher_t : z_publisher_move, \ + z_owned_queryable_t : z_queryable_move, \ + z_owned_reply_t : z_reply_move, \ + z_owned_hello_t : z_hello_move, \ + z_owned_str_t : z_str_move, \ + z_owned_str_array_t : z_str_array_move, \ + z_owned_closure_sample_t : z_closure_sample_move, \ + z_owned_closure_query_t : z_closure_query_move, \ + z_owned_closure_reply_t : z_closure_reply_move, \ + z_owned_closure_hello_t : z_closure_hello_move, \ + z_owned_closure_zid_t : z_closure_zid_move \ + )(&x) /** * Defines a generic function for cloning any of the ``z_owned_X_t`` types. @@ -135,19 +175,19 @@ * Returns: * Returns the cloned instance of `x`. */ -#define z_clone(x) \ - _Generic((x), z_owned_keyexpr_t \ - : z_keyexpr_clone, z_owned_config_t \ - : z_config_clone, z_owned_session_t \ - : z_session_clone, z_owned_subscriber_t \ - : z_subscriber_clone, z_owned_pull_subscriber_t \ - : z_pull_subscriber_clone, z_owned_publisher_t \ - : z_publisher_clone, z_owned_queryable_t \ - : z_queryable_clone, z_owned_reply_t \ - : z_reply_clone, z_owned_hello_t \ - : z_hello_clone, z_owned_str_t \ - : z_str_clone, z_owned_str_array_t \ - : z_str_array_clone)(&x) +#define z_clone(x) _Generic((x), \ + z_owned_keyexpr_t : z_keyexpr_clone, \ + z_owned_config_t : z_config_clone, \ + z_owned_session_t : z_session_clone, \ + z_owned_subscriber_t : z_subscriber_clone, \ + z_owned_pull_subscriber_t : z_pull_subscriber_clone, \ + z_owned_publisher_t : z_publisher_clone, \ + z_owned_queryable_t : z_queryable_clone, \ + z_owned_reply_t : z_reply_clone, \ + z_owned_hello_t : z_hello_clone, \ + z_owned_str_t : z_str_clone, \ + z_owned_str_array_t : z_str_array_clone \ + )(&x) /** * Defines a generic function for making null object of any of the ``z_owned_X_t`` types. @@ -194,186 +234,108 @@ #else -template -struct zenoh_loan_type { - typedef T type; -}; -template -inline typename zenoh_loan_type::type z_loan(const T&); +// clang-format off +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 z_session_t type; }; +template<> struct zenoh_loan_type{ typedef z_keyexpr_t type; }; +template<> struct zenoh_loan_type{ typedef z_config_t type; }; +template<> struct zenoh_loan_type{ typedef z_publisher_t type; }; +template<> struct zenoh_loan_type{ typedef z_pull_subscriber_t type; }; +template<> struct zenoh_loan_type{ typedef z_hello_t type; }; +template<> struct zenoh_loan_type{ typedef const char* type; }; -template <> -struct zenoh_loan_type { - typedef z_session_t type; -}; -template <> -struct zenoh_loan_type { - typedef z_keyexpr_t type; -}; -template <> -struct zenoh_loan_type { - typedef z_config_t type; -}; -template <> -struct zenoh_loan_type { - typedef z_publisher_t type; -}; -template <> -struct zenoh_loan_type { - typedef z_pull_subscriber_t type; -}; -template <> -struct zenoh_loan_type { - typedef z_hello_t type; -}; +template<> inline z_session_t z_loan(const z_owned_session_t& x) { return z_session_loan(&x); } +template<> inline z_keyexpr_t z_loan(const z_owned_keyexpr_t& x) { return z_keyexpr_loan(&x); } +template<> inline z_config_t z_loan(const z_owned_config_t& x) { return z_config_loan(&x); } +template<> inline z_publisher_t z_loan(const z_owned_publisher_t& x) { return z_publisher_loan(&x); } +template<> inline z_pull_subscriber_t z_loan(const z_owned_pull_subscriber_t& x) { return z_pull_subscriber_loan(&x); } +template<> inline z_hello_t z_loan(const z_owned_hello_t& x) { return z_hello_loan(&x); } +template<> inline const char* z_loan(const z_owned_str_t& x) { return z_str_loan(&x); } -template <> -inline z_session_t z_loan(const z_owned_session_t& x) { - return z_session_loan(&x); -} -template <> -inline z_keyexpr_t z_loan(const z_owned_keyexpr_t& x) { - return z_keyexpr_loan(&x); -} -template <> -inline z_config_t z_loan(const z_owned_config_t& x) { - return z_config_loan(&x); -} -template <> -inline z_publisher_t z_loan(const z_owned_publisher_t& x) { - return z_publisher_loan(&x); -} -template <> -inline z_pull_subscriber_t z_loan(const z_owned_pull_subscriber_t& x) { - return z_pull_subscriber_loan(&x); -} -template <> -inline z_hello_t z_loan(const z_owned_hello_t& x) { - return z_hello_loan(&x); -} +template struct zenoh_drop_type { typedef T type; }; +template inline typename zenoh_drop_type::type z_drop(T*); -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 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 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 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<> 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_pull_subscriber_t* v) { return z_undeclare_pull_subscriber(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_str_t* v) { z_str_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 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_pull_subscriber_t* v) { - return z_undeclare_pull_subscriber(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); -} +inline void z_null(z_owned_session_t& v) { v = z_session_null(); } +inline void z_null(z_owned_publisher_t& v) { v = z_publisher_null(); } +inline void z_null(z_owned_keyexpr_t& v) { v = z_keyexpr_null(); } +inline void z_null(z_owned_config_t& v) { v = z_config_null(); } +inline void z_null(z_owned_scouting_config_t& v) { v = z_scouting_config_null(); } +inline void z_null(z_owned_pull_subscriber_t& v) { v = z_pull_subscriber_null(); } +inline void z_null(z_owned_subscriber_t& v) { v = z_subscriber_null(); } +inline void z_null(z_owned_queryable_t& v) { v = z_queryable_null(); } +inline void z_null(z_owned_reply_t& v) { v = z_reply_null(); } +inline void z_null(z_owned_hello_t& v) { v = z_hello_null(); } +inline void z_null(z_owned_str_t& v) { v = z_str_null(); } +inline void z_null(z_owned_closure_sample_t& v) { v = z_closure_sample_null(); } +inline void z_null(z_owned_closure_query_t& v) { v = z_closure_query_null(); } +inline void z_null(z_owned_closure_reply_t& v) { v = z_closure_reply_null(); } +inline void z_null(z_owned_closure_hello_t& v) { v = z_closure_hello_null(); } +inline void z_null(z_owned_closure_zid_t& v) { v = z_closure_zid_null(); } -inline _Bool z_check(const z_owned_session_t& v) { return z_session_check(&v); } -inline _Bool z_check(const z_owned_publisher_t& v) { return z_publisher_check(&v); } -inline _Bool z_check(const z_owned_keyexpr_t& v) { return z_keyexpr_check(&v); } -inline _Bool z_check(const z_keyexpr_t& v) { return z_keyexpr_is_initialized(&v); } -inline _Bool z_check(const z_owned_config_t& v) { return z_config_check(&v); } -inline _Bool z_check(const z_owned_scouting_config_t& v) { return z_scouting_config_check(&v); } -inline _Bool z_check(const z_owned_subscriber_t& v) { return z_subscriber_check(&v); } -inline _Bool z_check(const z_owned_pull_subscriber_t& v) { return z_pull_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_hello_t& v) { return z_hello_check(&v); } +inline bool z_check(const z_owned_session_t& v) { return z_session_check(&v); } +inline bool z_check(const z_owned_publisher_t& v) { return z_publisher_check(&v); } +inline bool z_check(const z_owned_keyexpr_t& v) { return z_keyexpr_check(&v); } +inline bool z_check(const z_keyexpr_t& v) { return z_keyexpr_is_initialized(&v); } +inline bool z_check(const z_owned_config_t& v) { return z_config_check(&v); } +inline bool z_check(const z_owned_scouting_config_t& v) { return z_scouting_config_check(&v); } +inline bool z_check(const z_bytes_t& v) { return z_bytes_check(&v); } +inline bool z_check(const z_owned_subscriber_t& v) { return z_subscriber_check(&v); } +inline bool z_check(const z_owned_pull_subscriber_t& v) { return z_pull_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_hello_t& v) { return z_hello_check(&v); } +inline bool z_check(const z_owned_str_t& v) { return z_str_check(&v); } + +inline void z_call(const z_owned_closure_sample_t &closure, const z_sample_t *sample) + { z_closure_sample_call(&closure, sample); } +inline void z_call(const z_owned_closure_query_t &closure, const z_query_t *query) + { z_closure_query_call(&closure, query); } +inline void z_call(const z_owned_closure_reply_t &closure, z_owned_reply_t *sample) + { z_closure_reply_call(&closure, sample); } +inline void z_call(const z_owned_closure_hello_t &closure, z_owned_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(callback, droper, ctx, ...) \ - { .context = const_cast(static_cast(ctx)), .call = callback, .drop = droper } + { .context = const_cast(static_cast(ctx)), .call = callback, .drop = droper } #define z_closure(...) _z_closure_overloader(__VA_ARGS__, NULL, NULL) #define z_move(x) (&x) diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 3a8ed557c..4ac4ebef9 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -43,6 +43,7 @@ z_string_t z_string_make(const char *value); /** * Constructs a :c:type:`z_keyexpr_t` departing from a string. * It is a loaned key expression that aliases ``name``. + * Unlike it's counterpart in zenoh-c, this function does not test passed expression to correctness. * * Parameters: * name: Pointer to string representation of the keyexpr as a null terminated string. @@ -52,21 +53,46 @@ z_string_t z_string_make(const char *value); */ z_keyexpr_t z_keyexpr(const char *name); +/** + * Constructs a :c:type:`z_keyexpr_t` departing from a string. + * It is a loaned key expression that aliases ``name``. + * Input key expression is not checked for correctness. + * + * Parameters: + * name: Pointer to string representation of the keyexpr as a null terminated string. + * + * Returns: + * The :c:type:`z_keyexpr_t` corresponding to the given string. + */ +z_keyexpr_t z_keyexpr_unchecked(const char *name); + /** * Get null-terminated string departing from a :c:type:`z_keyexpr_t`. * - * If given keyexpr contains a declared keyexpr, the resulting value will be ``NULL``. + * If given keyexpr contains a declared keyexpr, the resulting owned string will be unitialized. * In that case, the user must use :c:func:`zp_keyexpr_resolve` to resolve the nesting declarations * and get its full expanded representation. * * Parameters: - * name: Pointer to string representation of the keyexpr as a null terminated string. + * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` * * Returns: - * The :c:type:`z_keyexpr_t` corresponding to the given string. + * The :c:type:`z_owned_str_t` containing key expression string representation if it's possible */ z_owned_str_t z_keyexpr_to_string(z_keyexpr_t keyexpr); +/** + * Returns the key expression's internal string by aliasing it. + * + * Parameters: + * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` + * + * Returns: + * The :c:type:`z_bytes_t` pointing to key expression string representation if it's possible + + */ +z_bytes_t z_keyexpr_as_bytes(z_keyexpr_t keyexpr); + /** * Constructs a null-terminated string departing from a :c:type:`z_keyexpr_t` for a given :c:type:`z_session_t`. * The user is responsible of droping the returned string using ``z_free``. @@ -209,7 +235,8 @@ int8_t zp_keyexpr_intersect_null_terminated(const char *l, const char *r); * r: The second keyexpr. * * Returns: - * Returns ``0`` if both ``l`` and ``r`` are equal. Otherwise, it returns a ``-1``, or other ``negative value`` for errors. + * Returns ``0`` if both ``l`` and ``r`` are equal. Otherwise, it returns a ``-1``, or other ``negative value`` for + * errors. */ int8_t z_keyexpr_equals(z_keyexpr_t l, z_keyexpr_t r); @@ -223,7 +250,8 @@ int8_t z_keyexpr_equals(z_keyexpr_t l, z_keyexpr_t r); * rlen: Number of characters in ``r``. * * Returns: - * Returns ``0`` if both ``l`` and ``r`` are equal. Otherwise, it returns a ``-1``, or other ``negative value`` for errors. + * Returns ``0`` if both ``l`` and ``r`` are equal. Otherwise, it returns a ``-1``, or other ``negative value`` for + * errors. */ int8_t zp_keyexpr_equals_null_terminated(const char *l, const char *r); @@ -390,6 +418,13 @@ z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix); */ z_encoding_t z_encoding_default(void); +/** + * + * Checks validity of the timestamp + * + */ +_Bool z_timestamp_check(z_timestamp_t ts); + /** * Constructs a default query target. * @@ -896,7 +931,7 @@ z_publisher_options_t z_publisher_options_default(void); * A :c:type:`z_owned_publisher_t` with either a valid publisher or a failing publisher. * Should the publisher be invalid, ``z_check(val)`` ing the returned value will return ``false``. */ -z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, z_publisher_options_t *options); +z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, const z_publisher_options_t *options); /** * Undeclares the publisher generated by a call to :c:func:`z_declare_publisher`. @@ -1173,7 +1208,7 @@ _Bool z_reply_is_ok(const z_owned_reply_t *reply); * Returns: * Returns the :c:type:`z_sample_t` wrapped in the query reply. */ -z_sample_t z_reply_ok(z_owned_reply_t *reply); +z_sample_t z_reply_ok(const z_owned_reply_t *reply); /** * Yields the contents of the reply by asserting it indicates a failure. diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 0953764a1..ac1c1e265 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -50,6 +50,7 @@ typedef _z_zint_t z_zint_t; * uint8_t *start: A pointer to the bytes array. */ typedef _z_bytes_t z_bytes_t; +_Bool z_bytes_check(const z_bytes_t *v); /** * Represents a Zenoh ID. @@ -194,6 +195,11 @@ _OWNED_TYPE_PTR(_z_queryable_t, queryable) */ typedef _z_encoding_t z_encoding_t; +/* + * Represents timestamp value in Zenoh + */ +typedef _z_timestamp_t z_timestamp_t; + /** * Represents a Zenoh value. * @@ -427,6 +433,7 @@ _OWNED_TYPE_PTR(z_reply_t, reply) * - ``_Bool z_str_array_array_is_empty(z_str_array_t *a);`` */ typedef _z_str_array_t z_str_array_t; + z_str_t *z_str_array_get(const z_str_array_t *a, size_t k); size_t z_str_array_len(const z_str_array_t *a); _Bool z_str_array_is_empty(const z_str_array_t *a); @@ -445,11 +452,13 @@ typedef void (*_z_dropper_handler_t)(void *arg); * void *context: a pointer to an arbitrary state. */ typedef struct { + void *context; _z_data_handler_t call; _z_dropper_handler_t drop; - void *context; } z_owned_closure_sample_t; +void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_sample_t *sample); + /** * Represents the query callback closure. * @@ -462,11 +471,13 @@ typedef struct { * void *context: a pointer to an arbitrary state. */ typedef struct { + void *context; _z_questionable_handler_t call; _z_dropper_handler_t drop; - void *context; } z_owned_closure_query_t; +void z_closure_query_call(const z_owned_closure_query_t *closure, const z_query_t *query); + typedef void (*z_owned_reply_handler_t)(z_owned_reply_t *reply, void *arg); /** @@ -481,11 +492,13 @@ typedef void (*z_owned_reply_handler_t)(z_owned_reply_t *reply, void *arg); * void *context: a pointer to an arbitrary state. */ typedef struct { + void *context; z_owned_reply_handler_t call; _z_dropper_handler_t drop; - void *context; } z_owned_closure_reply_t; +void z_closure_reply_call(const z_owned_closure_reply_t *closure, z_owned_reply_t *reply); + typedef void (*z_owned_hello_handler_t)(z_owned_hello_t *hello, void *arg); /** @@ -500,11 +513,13 @@ typedef void (*z_owned_hello_handler_t)(z_owned_hello_t *hello, void *arg); * void *context: a pointer to an arbitrary state. */ typedef struct { + void *context; z_owned_hello_handler_t call; _z_dropper_handler_t drop; - void *context; } z_owned_closure_hello_t; +void z_closure_hello_call(const z_owned_closure_hello_t *closure, z_owned_hello_t *hello); + typedef void (*z_id_handler_t)(const z_id_t *id, void *arg); /** @@ -518,11 +533,13 @@ typedef void (*z_id_handler_t)(const z_id_t *id, void *arg); * void *context: a pointer to an arbitrary state. */ typedef struct { + void *context; z_id_handler_t call; _z_dropper_handler_t drop; - void *context; } z_owned_closure_zid_t; +void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id); + #ifdef __cplusplus } #endif diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index 04fcbd3ff..683df1c81 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -90,8 +90,8 @@ _z_string_t _z_string_from_bytes(const _z_bytes_t *bs); * char **_val: A pointer to the array. */ typedef struct { - size_t _len; - char **_val; + size_t len; + char **val; } _z_str_array_t; _z_str_array_t _z_str_array_make(size_t len); diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 55ac8580a..11cb5c6da 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -50,10 +50,14 @@ typedef struct { * A zenoh timestamp. */ typedef struct { - _z_bytes_t _id; - uint64_t _time; + _z_bytes_t id; + uint64_t time; } _z_timestamp_t; +_z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp); +void _z_timestamp_reset(_z_timestamp_t *tstamp); +_Bool _z_timestamp_check(const _z_timestamp_t *stamp); + /** * A zenoh-net resource key. * diff --git a/src/api/api.c b/src/api/api.c index d98faeda8..37f33bad4 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -32,6 +32,8 @@ #include "zenoh-pico/utils/result.h" /********* Data Types Handlers *********/ +_Bool z_bytes_check(const z_bytes_t *v) { return v->start != NULL; } + z_string_t z_string_make(const char *value) { return _z_string_make(value); } z_str_t *z_str_array_get(const z_str_array_t *a, size_t k) { return _z_str_array_get(a, k); } @@ -40,6 +42,8 @@ _Bool z_str_array_is_empty(const z_str_array_t *a) { return _z_str_array_is_empt z_keyexpr_t z_keyexpr(const char *name) { return _z_rname(name); } +z_keyexpr_t z_keyexpr_unchecked(const char *name) { return _z_rname(name); } + z_owned_str_t z_keyexpr_to_string(z_keyexpr_t keyexpr) { z_owned_str_t ret = {._value = NULL}; @@ -55,6 +59,16 @@ z_owned_str_t z_keyexpr_to_string(z_keyexpr_t keyexpr) { return ret; } +z_bytes_t z_keyexpr_as_bytes(z_keyexpr_t keyexpr) { + if (keyexpr._id == Z_RESOURCE_ID_NONE) { + z_bytes_t ret = {.start = (const uint8_t *)keyexpr._suffix, .len = strlen(keyexpr._suffix), ._is_alloc = false}; + return ret; + } else { + z_bytes_t ret = {.start = NULL, .len = 0, ._is_alloc = false}; + return ret; + } +} + z_owned_str_t zp_keyexpr_resolve(z_session_t zs, z_keyexpr_t keyexpr) { z_owned_str_t ret = {._value = NULL}; @@ -233,6 +247,8 @@ z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix) { z_encoding_t z_encoding_default(void) { return z_encoding(Z_ENCODING_PREFIX_DEFAULT, NULL); } +_Bool z_timestamp_check(z_timestamp_t ts) { return _z_timestamp_check(&ts); } + z_value_t z_value(const char *payload, size_t payload_len, z_encoding_t encoding) { return (z_value_t){.payload = {.start = (const uint8_t *)payload, .len = payload_len}, .encoding = encoding}; } @@ -276,6 +292,36 @@ _Bool z_value_is_initialized(z_value_t *value) { return ret; } +void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_sample_t *sample) { + if (closure->call) { + (closure->call)(sample, closure->context); + } +} + +void z_closure_query_call(const z_owned_closure_query_t *closure, const z_query_t *query) { + if (closure->call) { + (closure->call)(query, closure->context); + } +} + +void z_closure_reply_call(const z_owned_closure_reply_t *closure, z_owned_reply_t *reply) { + if (closure->call) { + (closure->call)(reply, closure->context); + } +} + +void z_closure_hello_call(const z_owned_closure_hello_t *closure, z_owned_hello_t *hello) { + if (closure->call) { + (closure->call)(hello, closure->context); + } +} + +void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id) { + if (closure->call) { + (closure->call)(id, closure->context); + } +} + /**************** Loans ****************/ #define OWNED_FUNCTIONS_PTR_INTERNAL(type, ownedtype, name, f_free, f_copy) \ _Bool z_##name##_check(const ownedtype *val) { return val->_value != NULL; } \ @@ -691,7 +737,7 @@ z_publisher_options_t z_publisher_options_default(void) { return (z_publisher_options_t){.congestion_control = Z_CONGESTION_CONTROL_DEFAULT, .priority = Z_PRIORITY_DEFAULT}; } -z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, z_publisher_options_t *options) { +z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, const z_publisher_options_t *options) { z_keyexpr_t key = keyexpr; // TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition @@ -904,7 +950,7 @@ z_value_t z_reply_err(const z_owned_reply_t *reply) { return (z_value_t){.payload = _z_bytes_empty(), .encoding = z_encoding_default()}; } -z_sample_t z_reply_ok(z_owned_reply_t *reply) { return reply->_value->data.sample; } +z_sample_t z_reply_ok(const z_owned_reply_t *reply) { return reply->_value->data.sample; } /**************** Tasks ****************/ zp_task_read_options_t zp_task_read_options_default(void) { return (zp_task_read_options_t){.__dummy = 0}; } diff --git a/src/collections/string.c b/src/collections/string.c index e5d77f69a..0e3a8c5c1 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -128,10 +128,10 @@ _Bool _z_str_eq(const char *left, const char *right) { return strcmp(left, right /*-------- str_array --------*/ void _z_str_array_init(_z_str_array_t *sa, size_t len) { - char **val = (char **)&sa->_val; + char **val = (char **)&sa->val; *val = (char *)z_malloc(len * sizeof(char *)); if (*val != NULL) { - sa->_len = len; + sa->len = len; } } @@ -141,17 +141,17 @@ _z_str_array_t _z_str_array_make(size_t len) { return sa; } -char **_z_str_array_get(const _z_str_array_t *sa, size_t pos) { return &sa->_val[pos]; } +char **_z_str_array_get(const _z_str_array_t *sa, size_t pos) { return &sa->val[pos]; } -size_t _z_str_array_len(const _z_str_array_t *sa) { return sa->_len; } +size_t _z_str_array_len(const _z_str_array_t *sa) { return sa->len; } -_Bool _z_str_array_is_empty(const _z_str_array_t *sa) { return sa->_len == 0; } +_Bool _z_str_array_is_empty(const _z_str_array_t *sa) { return sa->len == 0; } void _z_str_array_clear(_z_str_array_t *sa) { - for (size_t i = 0; i < sa->_len; i++) { - z_free(sa->_val[i]); + for (size_t i = 0; i < sa->len; i++) { + z_free(sa->val[i]); } - z_free(sa->_val); + z_free(sa->val); } void _z_str_array_free(_z_str_array_t **sa) { @@ -165,17 +165,17 @@ void _z_str_array_free(_z_str_array_t **sa) { } void _z_str_array_copy(_z_str_array_t *dst, const _z_str_array_t *src) { - _z_str_array_init(dst, src->_len); - for (size_t i = 0; i < src->_len; i++) { - dst->_val[i] = _z_str_clone(src->_val[i]); + _z_str_array_init(dst, src->len); + for (size_t i = 0; i < src->len; i++) { + dst->val[i] = _z_str_clone(src->val[i]); } - dst->_len = src->_len; + dst->len = src->len; } void _z_str_array_move(_z_str_array_t *dst, _z_str_array_t *src) { - dst->_val = src->_val; - dst->_len = src->_len; + dst->val = src->val; + dst->len = src->len; - src->_val = NULL; - src->_len = 0; + src->val = NULL; + src->len = 0; } diff --git a/src/net/memory.c b/src/net/memory.c index 494c8aa0d..914f58826 100644 --- a/src/net/memory.c +++ b/src/net/memory.c @@ -25,8 +25,8 @@ void _z_sample_move(_z_sample_t *dst, _z_sample_t *src) { dst->encoding.prefix = src->encoding.prefix; // FIXME: call the z_encoding_move _z_bytes_move(&dst->encoding.suffix, &src->encoding.suffix); // FIXME: call the z_encoding_move - dst->timestamp._time = src->timestamp._time; // FIXME: call the z_timestamp_move - _z_bytes_move(&dst->timestamp._id, &src->timestamp._id); // FIXME: call the z_timestamp_move + dst->timestamp.time = src->timestamp.time; // FIXME: call the z_timestamp_move + _z_bytes_move(&dst->timestamp.id, &src->timestamp.id); // FIXME: call the z_timestamp_move } void _z_sample_clear(_z_sample_t *sample) { @@ -51,7 +51,7 @@ void _z_hello_clear(_z_hello_t *hello) { if (hello->zid.len > 0) { _z_bytes_clear(&hello->zid); } - if (hello->locators._len > 0) { + if (hello->locators.len > 0) { _z_str_array_clear(&hello->locators); } } diff --git a/src/net/session.c b/src/net/session.c index e394302eb..d06595cef 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -77,8 +77,8 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { _z_hello_list_t *xs = hellos; while (xs != NULL) { _z_hello_t *hello = _z_hello_list_head(xs); - if (hello->locators._len > 0) { - locator = _z_str_clone(hello->locators._val[0]); + if (hello->locators.len > 0) { + locator = _z_str_clone(hello->locators.val[0]); } xs = _z_hello_list_tail(xs); diff --git a/src/protocol/msg.c b/src/protocol/msg.c index c7ada5f41..4628a089f 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -27,7 +27,7 @@ void _z_payload_clear(_z_payload_t *p) { _z_bytes_clear(p); } /*------------------ Timestamp Field ------------------*/ -void _z_timestamp_clear(_z_timestamp_t *ts) { _z_bytes_clear(&ts->_id); } +void _z_timestamp_clear(_z_timestamp_t *ts) { _z_bytes_clear(&ts->id); } /*------------------ ResKey Field ------------------*/ void _z_keyexpr_clear(_z_keyexpr_t *rk) { diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index ee28ce060..4dbc9d5d5 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -43,8 +43,8 @@ int8_t _z_timestamp_encode(_z_wbuf_t *wbf, const _z_timestamp_t *ts) { int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _TIMESTAMP\n"); - _Z_EC(_z_uint64_encode(wbf, ts->_time)) - ret |= _z_bytes_encode(wbf, &ts->_id); + _Z_EC(_z_uint64_encode(wbf, ts->time)) + ret |= _z_bytes_encode(wbf, &ts->id); return ret; } @@ -53,8 +53,8 @@ int8_t _z_timestamp_decode_na(_z_timestamp_t *ts, _z_zbuf_t *zbf) { _Z_DEBUG("Decoding _TIMESTAMP\n"); int8_t ret = _Z_RES_OK; - ret |= _z_uint64_decode(&ts->_time, zbf); - ret |= _z_bytes_decode(&ts->_id, zbf); + ret |= _z_uint64_decode(&ts->time, zbf); + ret |= _z_bytes_decode(&ts->id, zbf); return ret; } diff --git a/src/session/query.c b/src/session/query.c index e5845c579..8f9627236 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -45,7 +45,7 @@ void _z_reply_free(_z_reply_t **reply) { } _Bool _z_pending_reply_eq(const _z_pending_reply_t *one, const _z_pending_reply_t *two) { - return one->_tstamp._time == two->_tstamp._time; + return one->_tstamp.time == two->_tstamp.time; } void _z_pending_reply_clear(_z_pending_reply_t *pr) { @@ -53,7 +53,7 @@ void _z_pending_reply_clear(_z_pending_reply_t *pr) { _z_reply_clear(&pr->_reply); // Free the timestamp - _z_bytes_clear(&pr->_tstamp._id); + _z_bytes_clear(&pr->_tstamp.id); } void _z_pending_query_clear(_z_pending_query_t *pen_qry) { @@ -185,7 +185,7 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_reply_context_t // Check if this is the same resource key if (_z_str_eq(pen_rep->_reply.data.sample.keyexpr._suffix, reply.data.sample.keyexpr._suffix) == true) { - if (timestamp._time <= pen_rep->_tstamp._time) { + if (timestamp.time <= pen_rep->_tstamp.time) { drop = true; } else { pen_qry->_pending_replies = diff --git a/src/session/scout.c b/src/session/scout.c index b1959d6c8..c2c501297 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -89,14 +89,14 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig if (_Z_HAS_FLAG(t_msg._header, _Z_FLAG_T_L) == true) { hello->locators = _z_str_array_make(t_msg._body._hello._locators._len); - for (size_t i = 0; i < hello->locators._len; i++) { - hello->locators._val[i] = + for (size_t i = 0; i < hello->locators.len; i++) { + hello->locators.val[i] = _z_locator_to_str(&t_msg._body._hello._locators._val[i]); } } else { // @TODO: construct the locator departing from the sock address - hello->locators._len = 0; - hello->locators._val = NULL; + hello->locators.len = 0; + hello->locators.val = NULL; } ret = _z_hello_list_push(ret, hello); diff --git a/src/session/utils.c b/src/session/utils.c index 1dd417233..ce4b8d4af 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -36,14 +36,20 @@ _z_keyexpr_t _z_keyexpr_duplicate(const _z_keyexpr_t *src) { _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp) { _z_timestamp_t ts; - _z_bytes_copy(&ts._id, &tstamp->_id); - ts._time = tstamp->_time; + _z_bytes_copy(&ts.id, &tstamp->id); + ts.time = tstamp->time; return ts; } void _z_timestamp_reset(_z_timestamp_t *tstamp) { - _z_bytes_reset(&tstamp->_id); - tstamp->_time = 0; + _z_bytes_reset(&tstamp->id); + tstamp->time = 0; +} + +_Bool _z_timestamp_check(const _z_timestamp_t *stamp) { + for (size_t i = 0; i < stamp->id.len; ++i) + if (stamp->id.start[i]) return true; + return false; } int8_t _z_session_generate_zid(_z_bytes_t *bs, uint8_t size) { diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 0ec8280c5..2c043aa13 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -175,7 +175,7 @@ char *gen_str(size_t size) { _z_str_array_t gen_str_array(size_t size) { _z_str_array_t sa = _z_str_array_make(size); - for (size_t i = 0; i < size; i++) ((char **)sa._val)[i] = gen_str(16); + for (size_t i = 0; i < size; i++) ((char **)sa.val)[i] = gen_str(16); return sa; } @@ -252,16 +252,16 @@ void assert_eq_uint8_array(_z_bytes_t *left, _z_bytes_t *right) { void assert_eq_str_array(_z_str_array_t *left, _z_str_array_t *right) { printf("Array -> "); - printf("Length (%zu:%zu), ", left->_len, right->_len); + printf("Length (%zu:%zu), ", left->len, right->len); - assert(left->_len == right->_len); + assert(left->len == right->len); printf("Content ("); - for (size_t i = 0; i < left->_len; i++) { - const char *l = left->_val[i]; - const char *r = right->_val[i]; + for (size_t i = 0; i < left->len; i++) { + const char *l = left->val[i]; + const char *r = right->val[i]; printf("%s:%s", l, r); - if (i < left->_len - 1) printf(" "); + if (i < left->len - 1) printf(" "); assert(_z_str_eq(l, r) == true); } @@ -330,19 +330,19 @@ void payload_field(void) { /*------------------ Timestamp field ------------------*/ _z_timestamp_t gen_timestamp(void) { _z_timestamp_t ts; - ts._time = gen_uint64(); - ts._id = gen_bytes(16); + ts.time = gen_uint64(); + ts.id = gen_bytes(16); return ts; } void assert_eq_timestamp(_z_timestamp_t *left, _z_timestamp_t *right) { printf("Timestamp -> "); - printf("Time (%llu:%llu), ", (unsigned long long)left->_time, (unsigned long long)right->_time); - assert(left->_time == right->_time); + printf("Time (%llu:%llu), ", (unsigned long long)left->time, (unsigned long long)right->time); + assert(left->time == right->time); printf("ID ("); - assert_eq_uint8_array(&left->_id, &right->_id); + assert_eq_uint8_array(&left->id, &right->id); printf(")"); } diff --git a/tests/z_session_test.c b/tests/z_session_test.c index 29d0ba6e7..c24cab42c 100644 --- a/tests/z_session_test.c +++ b/tests/z_session_test.c @@ -21,22 +21,19 @@ #undef NDEBUG #include -int main(void) -{ +int main(void) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make("client")); printf("Opening session...\n"); z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) - { + if (!z_check(s)) { printf("Unable to open session!\n"); return -1; } // Start read and lease tasks for zenoh-pico - if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) - { + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { printf("Unable to start read and lease tasks"); return -1; } From f650f6c4d9f56369f625193b23bb467bf233f19d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 7 Apr 2023 10:37:25 +0200 Subject: [PATCH 070/181] Slice buffer before passing it to the deserializer --- src/transport/multicast/link/rx.c | 5 ++++- src/transport/unicast/link/rx.c | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 93fae8261..52e9d788f 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -51,8 +51,8 @@ int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_me _z_mutex_lock(&ztm->_mutex_rx); #endif // Z_MULTI_THREAD == 1 + size_t to_read = 0; do { - size_t to_read = 0; if (_Z_LINK_IS_STREAMED(ztm->_link._capabilities) == true) { if (_z_zbuf_len(&ztm->_zbuf) < _Z_MSG_LEN_ENC_SIZE) { _z_link_recv_zbuf(&ztm->_link, &ztm->_zbuf, addr); @@ -85,6 +85,9 @@ int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_me } } while (false); // The 1-iteration loop to use continue to break the entire loop on error + // Wrap the main buffer for to_read bytes + _z_zbuf_t zbuf = _z_zbuf_view(&ztm->_zbuf, to_read); + if (ret == _Z_RES_OK) { _Z_DEBUG(">> \t transport_message_decode\n"); ret = _z_transport_message_decode_na(t_msg, &ztm->_zbuf); diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index 5962d7100..f5a0025d4 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -31,8 +31,8 @@ int8_t _z_unicast_recv_t_msg_na(_z_transport_unicast_t *ztu, _z_transport_messag _z_mutex_lock(&ztu->_mutex_rx); #endif // Z_MULTI_THREAD == 1 + size_t to_read = 0; do { - size_t to_read = 0; if (_Z_LINK_IS_STREAMED(ztu->_link._capabilities) == true) { if (_z_zbuf_len(&ztu->_zbuf) < _Z_MSG_LEN_ENC_SIZE) { _z_link_recv_zbuf(&ztu->_link, &ztu->_zbuf, NULL); @@ -63,7 +63,10 @@ int8_t _z_unicast_recv_t_msg_na(_z_transport_unicast_t *ztu, _z_transport_messag ret = _Z_ERR_TRANSPORT_RX_FAILED; } } - } while (false); // The 1-iteration loop to use continue to break the entire loop on error + } while (false); // The 1-iteration loop to use continue to break the entire loop on error + + // Wrap the main buffer for to_read bytes + _z_zbuf_t zbuf = _z_zbuf_view(&ztu->_zbuf, to_read); if (ret == _Z_RES_OK) { _Z_DEBUG(">> \t transport_message_decode\n"); From 911e0ffb585f115f155b1c0086f436cc095de39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 7 Apr 2023 10:37:49 +0200 Subject: [PATCH 071/181] Fix iosli wrap to use input argument --- src/protocol/iobuf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/protocol/iobuf.c b/src/protocol/iobuf.c index 9c186d6a0..eef65ad60 100644 --- a/src/protocol/iobuf.c +++ b/src/protocol/iobuf.c @@ -26,8 +26,8 @@ _z_iosli_t _z_iosli_wrap(const uint8_t *buf, size_t length, size_t r_pos, size_t w_pos) { assert((r_pos <= w_pos) && (w_pos <= length)); _z_iosli_t ios; - ios._r_pos = 0; - ios._w_pos = length; + ios._r_pos = r_pos; + ios._w_pos = w_pos; ios._capacity = length; ios._is_alloc = false; ios._buf = (uint8_t *)buf; From c0846442703147237a0062f42da06de3782524a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Tue, 11 Apr 2023 14:44:42 +0200 Subject: [PATCH 072/181] Fix segfault while handling queries with NONE or MONOTONIC modes (#200) --- src/session/query.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/session/query.c b/src/session/query.c index 8f9627236..f921ae8a5 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -225,8 +225,7 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_reply_context_t // Trigger the user callback if ((ret == _Z_RES_OK) && (pen_qry->_consolidation != Z_CONSOLIDATION_MODE_LATEST)) { - pen_qry->_callback(&reply, pen_qry->_call_arg); - _z_reply_clear(&reply); + pen_qry->_callback(_z_reply_alloc_and_move(&reply), pen_qry->_call_arg); } if (ret != _Z_RES_OK) { From 86bcbf9e90f5047707f8d73694ea3c0901fcdf8d Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Tue, 11 Apr 2023 14:47:55 +0200 Subject: [PATCH 073/181] Stabilize query with_value (#195) --- include/zenoh-pico/api/types.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index ac1c1e265..38e045040 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -331,8 +331,7 @@ typedef struct { * Members: * z_query_target_t target: The queryables that should be targeted by this get. * z_query_consolidation_t consolidation: The replies consolidation strategy to apply on replies. - * z_value_t with_value: The payload to include in the query. Note: This parameter has been marked as unstable: it - * works as advertised, but we may change it in a future release. + * z_value_t with_value: The payload to include in the query. */ typedef struct { z_value_t with_value; From ec364ef102311fac1091ccee512a6370b290d43a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 13 Apr 2023 14:03:05 +0200 Subject: [PATCH 074/181] Fix CI for integration tests (#201) * Fix run of integration tests * Fix run of integration tests * Fix run of integration tests * Fix run of integration tests * Temporarily disable O2 on release build for Windows * Add verbosity on tests * Avoid make test to override the previous compilation on Windows * Debugging Windows CI * Add executable extension on CI scripts for Windows * Debug CI * Debug CI * Debug CI * Debug CI * Debug CI * Debug CI * Debug CI * Debug CI * Comment workaround on Windows test step * Extra log messages on tests * Ensure time for zenoh to converge --- .github/workflows/integration.yaml | 6 +++- BSDmakefile | 4 +-- CMakeLists.txt | 2 +- GNUmakefile | 4 +-- tests/api.sh | 12 +++++-- tests/multicast.sh | 10 ++++-- tests/routed.sh | 12 +++++-- tests/z_api_alignment_test.c | 53 +++++++++++++++++++++++++++++- 8 files changed, 88 insertions(+), 15 deletions(-) diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml index 00302ace2..d05463919 100644 --- a/.github/workflows/integration.yaml +++ b/.github/workflows/integration.yaml @@ -50,6 +50,10 @@ jobs: BUILD_INTEGRATION: ON - name: Test debug - run: ctest --verbose + run: make test env: + BUILD_TYPE: Debug # Workaround for Windows as it seems the previous step is being ignored + 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: master diff --git a/BSDmakefile b/BSDmakefile index 505dabb90..9339f3620 100644 --- a/BSDmakefile +++ b/BSDmakefile @@ -72,8 +72,8 @@ make: $(BUILD_DIR)/Makefile install: $(BUILD_DIR)/Makefile cmake --install $(BUILD_DIR) -test: $(BUILD_DIR)/Makefile - make -C$(BUILD_DIR) test +test: make + ctest --verbose --test-dir build crossbuilds: $(CROSSBUILD_TARGETS) diff --git a/CMakeLists.txt b/CMakeLists.txt index 287d55747..8c8151bb9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,7 +113,7 @@ elseif(CMAKE_BUILD_TYPE MATCHES "RELEASE") if(UNIX) add_compile_options(-pipe -O3) elseif(MSVC) - add_compile_options(/O2) + # add_compile_options(/O2) elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") add_compile_options(-pipe -O3) endif() diff --git a/GNUmakefile b/GNUmakefile index 367b18ac3..28f9eb062 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -72,8 +72,8 @@ make: $(BUILD_DIR)/Makefile install: $(BUILD_DIR)/Makefile cmake --install $(BUILD_DIR) -test: $(BUILD_DIR)/Makefile - make -C$(BUILD_DIR) test +test: make + ctest --verbose --test-dir build crossbuilds: $(CROSSBUILD_TARGETS) diff --git a/tests/api.sh b/tests/api.sh index f848404eb..cd84c0860 100644 --- a/tests/api.sh +++ b/tests/api.sh @@ -16,6 +16,12 @@ TESTBIN="$1" TESTDIR=$(dirname "$0") +if [ "$OSTYPE" = "msys" ]; then + TESTBIN="$TESTDIR/Debug/$TESTBIN.exe" +else + TESTBIN="./$TESTBIN" +fi + cd "$TESTDIR"|| exit echo "------------------ Running test $TESTBIN -------------------" @@ -40,13 +46,13 @@ for LOCATOR in $(echo "$LOCATORS" | xargs); do sleep 1 echo "> Running zenohd ... $LOCATOR" - RUST_LOG=debug ./zenohd -l "$LOCATOR" > zenohd."$TESTBIN".log 2>&1 & + RUST_LOG=debug ./zenohd -l "$LOCATOR" > zenohd."$1".log 2>&1 & ZPID=$! sleep 5 echo "> Running $TESTBIN ..." - ./"$TESTBIN" "$LOCATOR" + "$TESTBIN" "$LOCATOR" RETCODE=$? echo "> Stopping zenohd ..." @@ -55,7 +61,7 @@ for LOCATOR in $(echo "$LOCATORS" | xargs); do sleep 1 echo "> Logs of zenohd ..." - cat zenohd."$TESTBIN".log + cat zenohd."$1".log [ "$RETCODE" -lt 0 ] && exit "$RETCODE" done diff --git a/tests/multicast.sh b/tests/multicast.sh index 650f34c68..d03dcb1dc 100644 --- a/tests/multicast.sh +++ b/tests/multicast.sh @@ -16,6 +16,12 @@ TESTBIN="$1" TESTDIR=$(dirname "$0") +if [ "$OSTYPE" = "msys" ]; then + TESTBIN="$TESTDIR/Debug/$TESTBIN.exe" +else + TESTBIN="./$TESTBIN" +fi + cd "$TESTDIR" || exit echo "------------------ Running test $TESTBIN -------------------" @@ -33,7 +39,7 @@ for LOCATOR in $(echo "$LOCATORS" | xargs); do sleep 1 echo "> Running $TESTBIN $LOCATOR..." - ./"$TESTBIN" "$LOCATOR" + "$TESTBIN" "$LOCATOR" RETCODE=$? [ "$RETCODE" -lt 0 ] && exit "$RETCODE" @@ -52,7 +58,7 @@ for LOCATOR in $(echo "$LOCATORS" | xargs); do sleep 1 echo "> Running $TESTBIN $LOCATOR..." - ./"$TESTBIN" "$LOCATOR" + "$TESTBIN" "$LOCATOR" RETCODE=$? [ "$RETCODE" -lt 0 ] && exit "$RETCODE" diff --git a/tests/routed.sh b/tests/routed.sh index fbdea4203..0558efb96 100644 --- a/tests/routed.sh +++ b/tests/routed.sh @@ -16,6 +16,12 @@ TESTBIN="$1" TESTDIR=$(dirname "$0") +if [ "$OSTYPE" = "msys" ]; then + TESTBIN="$TESTDIR/Debug/$TESTBIN.exe" +else + TESTBIN="./$TESTBIN" +fi + cd "$TESTDIR" || exit echo "------------------ Running test $TESTBIN -------------------" @@ -40,13 +46,13 @@ for LOCATOR in $(echo "$LOCATORS" | xargs); do sleep 1 echo "> Running zenohd ... $LOCATOR" - RUST_LOG=debug ./zenohd -l "$LOCATOR" > zenohd."$TESTBIN".log 2>&1 & + RUST_LOG=debug ./zenohd -l "$LOCATOR" > zenohd."$1".log 2>&1 & ZPID=$! sleep 5 echo "> Running $TESTBIN ..." - ./"$TESTBIN" "$LOCATOR" + "$TESTBIN" "$LOCATOR" RETCODE=$? echo "> Stopping zenohd ..." @@ -55,7 +61,7 @@ for LOCATOR in $(echo "$LOCATORS" | xargs); do sleep 1 echo "> Logs of zenohd ..." - cat zenohd."$TESTBIN".log + cat zenohd."$1".log [ "$RETCODE" -lt 0 ] && exit "$RETCODE" done diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 0f77a436c..1e04dbe38 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -26,7 +26,7 @@ #include "zenoh-pico.h" -#define SLEEP 1 +#define SLEEP 2 #define SCOUTING_TIMEOUT "1000" const char *value = "Test value"; @@ -35,12 +35,14 @@ volatile unsigned int zids = 0; void zid_handler(const z_id_t *id, void *arg) { (void)(arg); (void)(id); + printf("%s\n", __func__); zids++; } volatile unsigned int hellos = 0; void hello_handler(z_owned_hello_t *hello, void *arg) { (void)(arg); + printf("%s\n", __func__); hellos++; z_hello_null(); z_drop(hello); // validate double-drop safety: caller drops hello if it's not dropped by the handler @@ -48,6 +50,7 @@ void hello_handler(z_owned_hello_t *hello, void *arg) { volatile unsigned int queries = 0; void query_handler(const z_query_t *query, void *arg) { + printf("%s\n", __func__); queries++; z_owned_str_t k_str = z_keyexpr_to_string(z_query_keyexpr(query)); @@ -69,6 +72,7 @@ void query_handler(const z_query_t *query, void *arg) { volatile unsigned int replies = 0; void reply_handler(z_owned_reply_t *reply, void *arg) { + printf("%s\n", __func__); replies++; if (z_reply_is_ok(reply)) { @@ -92,6 +96,7 @@ void reply_handler(z_owned_reply_t *reply, void *arg) { volatile unsigned int datas = 0; void data_handler(const z_sample_t *sample, void *arg) { + printf("%s\n", __func__); datas++; z_owned_str_t k_str = z_keyexpr_to_string(sample->keyexpr); @@ -112,6 +117,7 @@ int main(int argc, char **argv) { zc_init_logger(); #endif + printf("Testing Keyexpr..."); z_keyexpr_t key = z_keyexpr("demo/example"); _Bool _ret_bool = z_keyexpr_is_initialized(&key); assert(_ret_bool == true); @@ -157,8 +163,10 @@ int main(int argc, char **argv) { assert(strlen("demo/example/**/*") == keyexpr_len); #endif + printf("Ok\n"); sleep(SLEEP); + printf("Testing Configs..."); z_owned_config_t _ret_config = z_config_new(); assert(z_check(_ret_config)); z_drop(z_move(_ret_config)); @@ -184,8 +192,10 @@ int main(int argc, char **argv) { #endif z_drop(z_move(_ret_sconfig)); + printf("Ok\n"); sleep(SLEEP); + printf("Testing Scouting..."); _ret_sconfig = z_scouting_config_from(z_loan(_ret_config)); z_owned_closure_hello_t _ret_closure_hello = z_closure(hello_handler, NULL, NULL); _ret_int8 = z_scout(z_move(_ret_sconfig), z_move(_ret_closure_hello)); @@ -194,6 +204,7 @@ int main(int argc, char **argv) { uint32_t _scouting_timeout = strtoul(SCOUTING_TIMEOUT, NULL, 10); z_sleep_ms(_scouting_timeout); + printf("Ok\n"); z_sleep_s(SLEEP); z_owned_session_t s1 = z_open(z_move(_ret_config)); @@ -252,18 +263,24 @@ int main(int argc, char **argv) { sleep(SLEEP); z_session_t ls1 = z_loan(s1); + printf("Declaring Subscriber..."); z_owned_closure_sample_t _ret_closure_sample = z_closure(data_handler, NULL, &ls1); z_subscriber_options_t _ret_sub_opt = z_subscriber_options_default(); z_owned_subscriber_t _ret_sub = z_declare_subscriber(z_loan(s2), z_keyexpr(keyexpr_str), z_move(_ret_closure_sample), &_ret_sub_opt); assert(z_check(_ret_sub)); + printf("Ok\n"); sleep(SLEEP); + printf("Declaring Keyexpr..."); char *s1_res = (char *)malloc(64); snprintf(s1_res, 64, "%s/chunk/%d", keyexpr_str, 1); z_owned_keyexpr_t _ret_expr = z_declare_keyexpr(z_loan(s1), z_keyexpr(s1_res)); assert(z_check(_ret_expr)); + printf("Ok\n"); + + printf("Session Put..."); z_put_options_t _ret_put_opt = z_put_options_default(); _ret_put_opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; z_encoding_t _ret_encoding = z_encoding_default(); @@ -272,76 +289,102 @@ int main(int argc, char **argv) { _ret_put_opt.encoding = _ret_encoding; _ret_int8 = z_put(z_loan(s1), z_loan(_ret_expr), (const uint8_t *)value, strlen(value), &_ret_put_opt); assert(_ret_int8 == 0); + printf("Ok\n"); sleep(SLEEP); assert(datas == 1); + printf("Session delete..."); z_delete_options_t _ret_delete_opt = z_delete_options_default(); _ret_int8 = z_delete(z_loan(s1), z_loan(_ret_expr), &_ret_delete_opt); assert(_ret_int8 == 0); + printf("Ok\n"); sleep(SLEEP); assert(datas == 2); + printf("Undeclaring Keyexpr..."); _ret_int8 = z_undeclare_keyexpr(z_loan(s1), z_move(_ret_expr)); assert(_ret_int8 == 0); assert(!z_check(_ret_expr)); + printf("Ok\n"); _ret_int8 = z_undeclare_subscriber(z_move(_ret_sub)); assert(_ret_int8 == 0); + printf("Declaring Pull Subscriber..."); z_owned_closure_sample_t _ret_closure_sample2 = z_closure(data_handler, NULL, &ls1); z_pull_subscriber_options_t _ret_psub_opt = z_pull_subscriber_options_default(); z_owned_pull_subscriber_t _ret_psub = z_declare_pull_subscriber(z_loan(s2), z_keyexpr(keyexpr_str), z_move(_ret_closure_sample2), &_ret_psub_opt); assert(z_check(_ret_psub)); + printf("Ok\n"); + printf("Declaring Publisher..."); z_publisher_options_t _ret_pub_opt = z_publisher_options_default(); _ret_pub_opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; z_owned_publisher_t _ret_pub = z_declare_publisher(z_loan(s1), z_keyexpr(keyexpr_str), &_ret_pub_opt); assert(z_check(_ret_pub)); + printf("Ok\n"); + + sleep(SLEEP); + printf("Publisher Put..."); z_publisher_put_options_t _ret_pput_opt = z_publisher_put_options_default(); _ret_int8 = z_publisher_put(z_loan(_ret_pub), (const uint8_t *)value, strlen(value), &_ret_pput_opt); assert(_ret_int8 == 0); + printf("Ok\n"); sleep(SLEEP); + printf("Pull Subscriber Pulling data..."); _ret_int8 = z_subscriber_pull(z_loan(_ret_psub)); assert(_ret_int8 == 0); + printf("Ok\n"); sleep(SLEEP); assert(datas == 3); + printf("Publisher Delete..."); z_publisher_delete_options_t _ret_pdelete_opt = z_publisher_delete_options_default(); _ret_int8 = z_publisher_delete(z_loan(_ret_pub), &_ret_pdelete_opt); assert(_ret_int8 == 0); + printf("Ok\n"); sleep(SLEEP); + printf("Pull Subscriber Pulling data..."); _ret_int8 = z_subscriber_pull(z_loan(_ret_psub)); assert(_ret_int8 == 0); + printf("Ok\n"); sleep(SLEEP); assert(datas == 4); + printf("Undeclaring Publisher..."); _ret_int8 = z_undeclare_publisher(z_move(_ret_pub)); assert(_ret_int8 == 0); assert(!z_check(_ret_pub)); + printf("Ok\n"); + printf("Undeclaring Pull Subscriber..."); _ret_int8 = z_undeclare_pull_subscriber(z_move(_ret_psub)); assert(_ret_int8 == 0); + printf("Ok\n"); sleep(SLEEP); + printf("Declaring Queryable..."); z_owned_closure_query_t _ret_closure_query = z_closure(query_handler, NULL, &ls1); z_queryable_options_t _ret_qle_opt = z_queryable_options_default(); z_owned_queryable_t qle = z_declare_queryable(z_loan(s1), z_keyexpr(s1_res), z_move(_ret_closure_query), &_ret_qle_opt); assert(z_check(qle)); + printf("Ok\n"); sleep(SLEEP); + printf("Testing Consolidations..."); z_session_t ls2 = z_loan(s2); z_owned_closure_reply_t _ret_closure_reply = z_closure(reply_handler, NULL, &ls2); z_get_options_t _ret_get_opt = z_get_options_default(); @@ -356,21 +399,28 @@ int main(int argc, char **argv) { (void)(_ret_get_opt.consolidation); _ret_get_opt.consolidation = z_query_consolidation_none(); (void)(_ret_get_opt.consolidation); + printf("Ok\n"); + + printf("Testing Get..."); _ret_int8 = z_get(z_loan(s2), z_keyexpr(s1_res), "", z_move(_ret_closure_reply), &_ret_get_opt); assert(_ret_int8 == 0); + printf("Ok\n"); sleep(SLEEP); assert(queries == 1); assert(replies == 1); + printf("Undeclaring Queryable..."); _ret_int8 = z_undeclare_queryable(z_move(qle)); assert(_ret_int8 == 0); + printf("Ok\n"); #ifdef ZENOH_PICO zp_stop_read_task(z_loan(s1)); zp_stop_lease_task(z_loan(s1)); #endif + printf("Close sessions..."); _ret_int8 = z_close(z_move(s1)); assert(_ret_int8 == 0); @@ -380,6 +430,7 @@ int main(int argc, char **argv) { #endif _ret_int8 = z_close(z_move(s2)); assert(_ret_int8 == 0); + printf("Ok\n"); sleep(SLEEP * 5); From b77b03749d3156d2187b00bd8212ee3347271f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 13 Apr 2023 14:13:19 +0200 Subject: [PATCH 075/181] Replace deprecated CryptGenRandom with RtlGenRandom (#202) --- src/system/windows/system.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/system/windows/system.c b/src/system/windows/system.c index 519a9ba01..80643d9f3 100644 --- a/src/system/windows/system.c +++ b/src/system/windows/system.c @@ -15,7 +15,7 @@ #include // The following includes must come after winsock2 #include -#include +#include #include "zenoh-pico/config.h" #include "zenoh-pico/system/platform.h" @@ -46,16 +46,7 @@ uint64_t z_random_u64(void) { return ret; } -void z_random_fill(void *buf, size_t len) { - HCRYPTPROV crypt_ctx; - if (CryptAcquireContext(&crypt_ctx, NULL, (LPCSTR)L"Microsoft Base Cryptographic Provider v1.0", PROV_RSA_FULL, - CRYPT_VERIFYCONTEXT)) { - for (size_t i = 0; i < len; i++) { - CryptGenRandom(crypt_ctx, 1, &((uint8_t *)buf)[i]); - } - CryptReleaseContext(crypt_ctx, 0); - } -} +void z_random_fill(void *buf, size_t len) { RtlGenRandom(buf, len); } /*------------------ Memory ------------------*/ // #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) From ea576d3219cf16cd98efdbe9557c33f06a9927f5 Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Wed, 19 Apr 2023 09:31:39 +0200 Subject: [PATCH 076/181] Add automatic changelog generation --- .github/release.yml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .github/release.yml diff --git a/.github/release.yml b/.github/release.yml new file mode 100644 index 000000000..4523efea0 --- /dev/null +++ b/.github/release.yml @@ -0,0 +1,25 @@ +// +// Copyright (c) 2023 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, +// + +changelog: + categories: + - title: New features 🎉 + labels: + - enhancement + - title: Bug fixes 🐞 + labels: + - bug + - title: Other changes + labels: + - "*" \ No newline at end of file From 975a0e449eec082535816d9f830760977614cb57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 20 Apr 2023 11:44:20 +0200 Subject: [PATCH 077/181] Stop further endpoint validation in case of failure (#204) (#205) --- src/link/multicast/bt.c | 6 ++++-- src/link/multicast/udp.c | 28 ++++++++++++++++------------ src/link/unicast/serial.c | 20 +++++++++++--------- src/link/unicast/tcp.c | 28 ++++++++++++++++------------ src/link/unicast/udp.c | 28 ++++++++++++++++------------ src/link/unicast/ws.c | 28 ++++++++++++++++------------ 6 files changed, 79 insertions(+), 59 deletions(-) diff --git a/src/link/multicast/bt.c b/src/link/multicast/bt.c index 581e71d1f..e269d420e 100644 --- a/src/link/multicast/bt.c +++ b/src/link/multicast/bt.c @@ -31,8 +31,10 @@ int8_t _z_endpoint_bt_valid(_z_endpoint_t *ep) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } - if (strlen(ep->_locator._address) == (size_t)0) { - ret = _Z_ERR_CONFIG_LOCATOR_INVALID; + if (ret == _Z_RES_OK) { + if (strlen(ep->_locator._address) == (size_t)0) { + ret = _Z_ERR_CONFIG_LOCATOR_INVALID; + } } return ret; diff --git a/src/link/multicast/udp.c b/src/link/multicast/udp.c index e74a6dce5..bc07494e8 100644 --- a/src/link/multicast/udp.c +++ b/src/link/multicast/udp.c @@ -79,22 +79,26 @@ int8_t _z_endpoint_udp_multicast_valid(_z_endpoint_t *endpoint) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } - char *s_address = __z_parse_address_segment_udp_multicast(endpoint->_locator._address); - if (s_address == NULL) { - ret = _Z_ERR_CONFIG_LOCATOR_INVALID; - } else { - z_free(s_address); + if (ret == _Z_RES_OK) { + char *s_address = __z_parse_address_segment_udp_multicast(endpoint->_locator._address); + if (s_address == NULL) { + ret = _Z_ERR_CONFIG_LOCATOR_INVALID; + } else { + z_free(s_address); + } } - char *s_port = __z_parse_port_segment_udp_multicast(endpoint->_locator._address); - if (s_port == NULL) { - ret = _Z_ERR_CONFIG_LOCATOR_INVALID; - } else { - uint32_t port = strtoul(s_port, NULL, 10); - if ((port < (uint32_t)1) || (port > (uint32_t)65355)) { // Port numbers should range from 1 to 65355 + if (ret == _Z_RES_OK) { + char *s_port = __z_parse_port_segment_udp_multicast(endpoint->_locator._address); + if (s_port == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; + } else { + uint32_t port = 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; + } + z_free(s_port); } - z_free(s_port); } const char *iface = _z_str_intmap_get(&endpoint->_config, UDP_CONFIG_IFACE_KEY); diff --git a/src/link/unicast/serial.c b/src/link/unicast/serial.c index fd544bda0..40cdbf349 100644 --- a/src/link/unicast/serial.c +++ b/src/link/unicast/serial.c @@ -34,15 +34,17 @@ int8_t _z_endpoint_serial_valid(_z_endpoint_t *endpoint) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } - char *p_dot = strchr(endpoint->_locator._address, '.'); - if (p_dot != NULL) { - if ((endpoint->_locator._address == p_dot) || - (strlen(p_dot) == (size_t)1)) { // If dot is the first or last character - ret = _Z_ERR_CONFIG_LOCATOR_INVALID; - } - } else { - if (strlen(endpoint->_locator._address) == (size_t)0) { - ret = _Z_ERR_CONFIG_LOCATOR_INVALID; + if (ret == _Z_RES_OK) { + char *p_dot = strchr(endpoint->_locator._address, '.'); + if (p_dot != NULL) { + if ((endpoint->_locator._address == p_dot) || + (strlen(p_dot) == (size_t)1)) { // If dot is the first or last character + ret = _Z_ERR_CONFIG_LOCATOR_INVALID; + } + } else { + if (strlen(endpoint->_locator._address) == (size_t)0) { + ret = _Z_ERR_CONFIG_LOCATOR_INVALID; + } } } diff --git a/src/link/unicast/tcp.c b/src/link/unicast/tcp.c index 23344ee3e..f05980924 100644 --- a/src/link/unicast/tcp.c +++ b/src/link/unicast/tcp.c @@ -79,22 +79,26 @@ int8_t _z_endpoint_tcp_valid(_z_endpoint_t *endpoint) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } - char *s_address = __z_parse_address_segment_tcp(endpoint->_locator._address); - if (s_address == NULL) { - ret = _Z_ERR_CONFIG_LOCATOR_INVALID; - } else { - z_free(s_address); + if (ret == _Z_RES_OK) { + char *s_address = __z_parse_address_segment_tcp(endpoint->_locator._address); + if (s_address == NULL) { + ret = _Z_ERR_CONFIG_LOCATOR_INVALID; + } else { + z_free(s_address); + } } - char *s_port = __z_parse_port_segment_tcp(endpoint->_locator._address); - if (s_port == NULL) { - ret = _Z_ERR_CONFIG_LOCATOR_INVALID; - } else { - uint32_t port = strtoul(s_port, NULL, 10); - if ((port < (uint32_t)1) || (port > (uint32_t)65355)) { // Port numbers should range from 1 to 65355 + if (ret == _Z_RES_OK) { + char *s_port = __z_parse_port_segment_tcp(endpoint->_locator._address); + if (s_port == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; + } else { + uint32_t port = 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; + } + z_free(s_port); } - z_free(s_port); } return ret; diff --git a/src/link/unicast/udp.c b/src/link/unicast/udp.c index 71ee861d7..63ae5de04 100644 --- a/src/link/unicast/udp.c +++ b/src/link/unicast/udp.c @@ -79,22 +79,26 @@ int8_t _z_endpoint_udp_unicast_valid(_z_endpoint_t *endpoint) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } - char *s_address = __z_parse_address_segment_udp_unicast(endpoint->_locator._address); - if (s_address == NULL) { - ret = _Z_ERR_CONFIG_LOCATOR_INVALID; - } else { - z_free(s_address); + if (ret == _Z_RES_OK) { + char *s_address = __z_parse_address_segment_udp_unicast(endpoint->_locator._address); + if (s_address == NULL) { + ret = _Z_ERR_CONFIG_LOCATOR_INVALID; + } else { + z_free(s_address); + } } - char *s_port = __z_parse_port_segment_udp_unicast(endpoint->_locator._address); - if (s_port == NULL) { - ret = _Z_ERR_CONFIG_LOCATOR_INVALID; - } else { - uint32_t port = strtoul(s_port, NULL, 10); - if ((port < (uint32_t)1) || (port > (uint32_t)65355)) { // Port numbers should range from 1 to 65355 + if (ret == _Z_RES_OK) { + char *s_port = __z_parse_port_segment_udp_unicast(endpoint->_locator._address); + if (s_port == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; + } else { + uint32_t port = 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; + } + z_free(s_port); } - z_free(s_port); } return ret; diff --git a/src/link/unicast/ws.c b/src/link/unicast/ws.c index 64b9fd34f..5d1119d8d 100644 --- a/src/link/unicast/ws.c +++ b/src/link/unicast/ws.c @@ -80,22 +80,26 @@ int8_t _z_endpoint_ws_valid(_z_endpoint_t *endpoint) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } - char *s_addr = __z_parse_address_segment_ws(endpoint->_locator._address); - if (s_addr == NULL) { - ret = _Z_ERR_CONFIG_LOCATOR_INVALID; - } else { - z_free(s_addr); + if (ret == _Z_RES_OK) { + char *s_addr = __z_parse_address_segment_ws(endpoint->_locator._address); + if (s_addr == NULL) { + ret = _Z_ERR_CONFIG_LOCATOR_INVALID; + } else { + z_free(s_addr); + } } - char *s_port = __z_parse_port_segment_ws(endpoint->_locator._address); - if (s_port == NULL) { - ret = _Z_ERR_CONFIG_LOCATOR_INVALID; - } else { - uint32_t port = strtoul(s_port, NULL, 10); - if ((port < (uint32_t)1) || (port > (uint32_t)65355)) { // Port numbers should range from 1 to 65355 + if (ret == _Z_RES_OK) { + char *s_port = __z_parse_port_segment_ws(endpoint->_locator._address); + if (s_port == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; + } else { + uint32_t port = 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; + } + z_free(s_port); } - z_free(s_port); } return ret; From c35bc16312e842833eed2b1b97119acd0530f91c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 20 Apr 2023 19:38:24 +0200 Subject: [PATCH 078/181] Fix check on multicast support in zephyr (#206) --- src/system/zephyr/network.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/system/zephyr/network.c b/src/system/zephyr/network.c index e89afc107..0f34432d8 100644 --- a/src/system/zephyr/network.c +++ b/src/system/zephyr/network.c @@ -300,7 +300,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin } // Create lep endpoint - if (ret != _Z_RES_OK) { + if (ret == _Z_RES_OK) { struct addrinfo *laddr = (struct addrinfo *)z_malloc(sizeof(struct addrinfo)); if (laddr != NULL) { laddr->ai_flags = 0; @@ -362,7 +362,6 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo struct sockaddr_in6 *c_laddr = (struct sockaddr_in6 *)lsockaddr; c_laddr->sin6_family = AF_INET6; c_laddr->sin6_addr = in6addr_any; - c_laddr->sin6_port = htons(INADDR_ANY); c_laddr->sin6_port = ((struct sockaddr_in6 *)rep._iptcp->ai_addr)->sin6_port; // c_laddr->sin6_scope_id; // Not needed to be defined } else { From e9165d1d01113057df008f66ffac575306c563f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Fri, 21 Apr 2023 14:30:04 +0200 Subject: [PATCH 079/181] Temporarily disable Windows CI for integration tests --- .github/workflows/integration.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml index d05463919..5015e4757 100644 --- a/.github/workflows/integration.yaml +++ b/.github/workflows/integration.yaml @@ -29,7 +29,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ ubuntu-latest, macOS-latest, windows-latest ] + os: [ ubuntu-latest, macOS-latest ] steps: - uses: actions/checkout@v2 From 42689e072cae568e28c07d82dd60acf1206d044f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Tue, 25 Apr 2023 20:02:02 +0200 Subject: [PATCH 080/181] Iterate over list of locators upon scouting (#207) (#210) * Try all locators until successful * Add array empty helper * Error if no scouting results * Copy will make, thus leaking the previous make --- include/zenoh-pico/collections/array.h | 6 +++++ include/zenoh-pico/collections/string.h | 1 + src/collections/string.c | 2 ++ src/net/session.c | 31 ++++++++++++------------- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/include/zenoh-pico/collections/array.h b/include/zenoh-pico/collections/array.h index 5b18ad265..fe2b3f306 100644 --- a/include/zenoh-pico/collections/array.h +++ b/include/zenoh-pico/collections/array.h @@ -25,6 +25,12 @@ size_t _len; \ type *_val; \ } name##_array_t; \ + static inline name##_array_t name##_array_empty(void) { \ + name##_array_t a; \ + a._val = NULL; \ + a._len = 0; \ + return a; \ + } \ static inline name##_array_t name##_array_make(size_t capacity) { \ name##_array_t a; \ a._val = (type *)z_malloc(capacity * sizeof(type)); \ diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index 683df1c81..38fe3d334 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -94,6 +94,7 @@ typedef struct { char **val; } _z_str_array_t; +_z_str_array_t _z_str_array_empty(void); _z_str_array_t _z_str_array_make(size_t len); void _z_str_array_init(_z_str_array_t *sa, size_t len); char **_z_str_array_get(const _z_str_array_t *sa, size_t pos); diff --git a/src/collections/string.c b/src/collections/string.c index 0e3a8c5c1..51a19c38b 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -135,6 +135,8 @@ void _z_str_array_init(_z_str_array_t *sa, size_t len) { } } +_z_str_array_t _z_str_array_empty(void) { return (_z_str_array_t){.val = NULL, .len = 0}; } + _z_str_array_t _z_str_array_make(size_t len) { _z_str_array_t sa; _z_str_array_init(&sa, len); diff --git a/src/net/session.c b/src/net/session.c index d06595cef..e13f4ca05 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -52,7 +52,7 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { int8_t ret = _Z_RES_OK; if (config != NULL) { - char *locator = NULL; + _z_str_array_t locators = _z_str_array_empty(); if (_z_config_get(config, Z_CONFIG_PEER_KEY) == NULL) { // Scout if peer is not configured char *opt_as_str = _z_config_get(config, Z_CONFIG_SCOUTING_WHAT_KEY); if (opt_as_str == NULL) { @@ -74,21 +74,21 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { // Scout and return upon the first result _z_hello_list_t *hellos = _z_scout_inner(what, mcast_locator, timeout, true); - _z_hello_list_t *xs = hellos; - while (xs != NULL) { - _z_hello_t *hello = _z_hello_list_head(xs); - if (hello->locators.len > 0) { - locator = _z_str_clone(hello->locators.val[0]); - } - - xs = _z_hello_list_tail(xs); + if (hellos != NULL) { + _z_hello_t *hello = _z_hello_list_head(hellos); + _z_str_array_copy(&locators, &hello->locators); } _z_hello_list_free(&hellos); } else { - locator = _z_str_clone(_z_config_get(config, Z_CONFIG_PEER_KEY)); + locators = _z_str_array_make(1); + locators.val[0] = _z_str_clone(_z_config_get(config, Z_CONFIG_PEER_KEY)); } - if (locator != NULL) { + ret = _Z_ERR_SCOUT_NO_RESULTS; + for (size_t i = 0; i < locators.len; i++) { + ret = _Z_RES_OK; + + char *locator = locators.val[i]; // @TODO: check invalid configurations // For example, client mode in multicast links @@ -107,15 +107,14 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { if (ret == _Z_RES_OK) { ret = __z_open_inner(zn, locator, mode); + if (ret == _Z_RES_OK) { + break; + } } else { _Z_ERROR("Trying to configure an invalid mode.\n"); } - z_free(locator); - } else { - ret = _Z_ERR_SCOUT_NO_RESULTS; - _Z_DEBUG("Unable to scout a zenoh router\n"); - _Z_ERROR("Please make sure at least one router is running on your network!\n"); } + _z_str_array_clear(&locators); } else { _Z_ERROR("A valid config is missing.\n"); } From 467edbd3ce7427d4391ea5387e03e9aff6c42c8c Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Tue, 25 Apr 2023 20:02:35 +0200 Subject: [PATCH 081/181] Align API. Rename with_value option to value. (#211) --- examples/arduino/z_get.ino | 2 +- examples/espidf/z_get.c | 2 +- examples/mbed/z_get.cpp | 2 +- examples/unix/c11/z_get.c | 2 +- examples/unix/c99/z_get.c | 2 +- examples/windows/z_get.c | 2 +- include/zenoh-pico/api/types.h | 4 ++-- include/zenoh-pico/net/primitives.h | 4 ++-- include/zenoh-pico/net/query.h | 2 +- include/zenoh-pico/protocol/msg.h | 2 +- src/api/api.c | 8 ++++---- src/net/primitives.c | 4 ++-- src/protocol/msg.c | 6 +++--- src/session/queryable.c | 4 ++-- tests/z_msgcodec_test.c | 12 ++++++------ 15 files changed, 29 insertions(+), 29 deletions(-) diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index f099fa3c2..3cdba9b88 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -111,7 +111,7 @@ void loop() { Serial.println(" ..."); z_get_options_t opts = z_get_options_default(); if (strcmp(VALUE, "") != 0) { - opts.with_value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); } z_owned_closure_reply_t callback = z_closure_reply(reply_handler, reply_dropper, NULL); if (z_get(z_session_loan(&s), z_keyexpr(KEYEXPR), "", z_closure_reply_move(&callback), &opts) < 0) { diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index c8ccacbec..891588acd 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -155,7 +155,7 @@ void app_main() { printf("Sending Query '%s'...\n", KEYEXPR); z_get_options_t opts = z_get_options_default(); if (strcmp(VALUE, "") != 0) { - opts.with_value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); } z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); if (z_get(z_loan(s), z_keyexpr(KEYEXPR), "", z_move(callback), &opts) < 0) { diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index d77f82aeb..693f6214a 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -75,7 +75,7 @@ int main(int argc, char **argv) { printf("Sending Query '%s'...\n", KEYEXPR); z_get_options_t opts = z_get_options_default(); if (strcmp(VALUE, "") != 0) { - opts.with_value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); } z_owned_closure_reply_t callback = z_closure_reply(reply_handler, reply_dropper, NULL); if (z_get(z_session_loan(&s), z_keyexpr(KEYEXPR), "", z_closure_reply_move(&callback), &opts) < 0) { diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index 1bff89070..632be3719 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -105,7 +105,7 @@ int main(int argc, char **argv) { printf("Sending Query '%s'...\n", keyexpr); z_get_options_t opts = z_get_options_default(); if (value != NULL) { - opts.with_value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); } z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 0915b783b..1d01b4890 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -105,7 +105,7 @@ int main(int argc, char **argv) { printf("Sending Query '%s'...\n", keyexpr); z_get_options_t opts = z_get_options_default(); if (value != NULL) { - opts.with_value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); } z_owned_closure_reply_t callback = z_closure_reply(reply_handler, reply_dropper, NULL); if (z_get(z_session_loan(&s), ke, "", z_closure_reply_move(&callback), &opts) < 0) { diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index a8e73652c..602703050 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -77,7 +77,7 @@ int main(int argc, char **argv) { printf("Sending Query '%s'...\n", keyexpr); z_get_options_t opts = z_get_options_default(); if (value != NULL) { - opts.with_value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); } z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 38e045040..25ebb72c1 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -331,10 +331,10 @@ typedef struct { * Members: * z_query_target_t target: The queryables that should be targeted by this get. * z_query_consolidation_t consolidation: The replies consolidation strategy to apply on replies. - * z_value_t with_value: The payload to include in the query. + * z_value_t value: The payload to include in the query. */ typedef struct { - z_value_t with_value; + z_value_t value; z_query_consolidation_t consolidation; z_query_target_t target; } z_get_options_t; diff --git a/include/zenoh-pico/net/primitives.h b/include/zenoh-pico/net/primitives.h index f83b89adb..e7fc75839 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -195,14 +195,14 @@ int8_t _z_subscriber_pull(const _z_subscriber_t *sub); * parameters: An indication to matching queryables about the queried data. * target: The kind of queryables that should be target of this query. * consolidation: The kind of consolidation that should be applied on replies. - * with_value: The payload of the query. + * value: The payload of the query. * callback: The callback function that will be called on reception of replies for this query. * arg_call: A pointer that will be passed to the **callback** on each call. * dropper: The callback function that will be called on upon completion of the callback. * arg_drop: A pointer that will be passed to the **dropper** on each call. */ 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 with_value, _z_reply_handler_t callback, + const z_consolidation_mode_t consolidation, const _z_value_t value, _z_reply_handler_t callback, void *arg_call, _z_drop_handler_t dropper, void *arg_drop); /** diff --git a/include/zenoh-pico/net/query.h b/include/zenoh-pico/net/query.h index 773ad8e57..104b98432 100644 --- a/include/zenoh-pico/net/query.h +++ b/include/zenoh-pico/net/query.h @@ -21,7 +21,7 @@ * The query to be answered by a queryable. */ typedef struct { - _z_value_t _with_value; + _z_value_t _value; _z_keyexpr_t _key; _z_zint_t _qid; void *_zn; // FIXME: _z_session_t *zn; diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 8fcbf32bd..597acf6eb 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -576,7 +576,7 @@ _z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_pa _z_zenoh_message_t _z_msg_make_unit(_Bool can_be_dropped); _z_zenoh_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id, _z_zint_t max_samples, _Bool is_final); _z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, char *parameters, _z_zint_t qid, z_query_target_t target, - z_consolidation_mode_t consolidation, _z_value_t with_value); + z_consolidation_mode_t consolidation, _z_value_t value); _z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_payload_t payload, _Bool can_be_dropped, _z_reply_context_t *rctx); diff --git a/src/api/api.c b/src/api/api.c index 37f33bad4..f156f60eb 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -278,7 +278,7 @@ z_bytes_t z_query_parameters(const z_query_t *query) { return parameters; } -z_value_t z_query_value(const z_query_t *query) { return query->_with_value; } +z_value_t z_query_value(const z_query_t *query) { return query->_value; } z_keyexpr_t z_query_keyexpr(const z_query_t *query) { return query->_key; } @@ -649,7 +649,7 @@ int8_t z_delete(z_session_t zs, z_keyexpr_t keyexpr, const z_delete_options_t *o z_get_options_t z_get_options_default(void) { return (z_get_options_t){.target = z_query_target_default(), .consolidation = z_query_consolidation_default(), - .with_value = {.encoding = z_encoding_default(), .payload = _z_bytes_empty()}}; + .value = {.encoding = z_encoding_default(), .payload = _z_bytes_empty()}}; } typedef struct __z_reply_handler_wrapper_t { @@ -675,7 +675,7 @@ int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owne if (options != NULL) { opt.consolidation = options->consolidation; opt.target = options->target; - opt.with_value = options->with_value; + opt.value = options->value; } if (opt.consolidation.mode == Z_CONSOLIDATION_MODE_AUTO) { @@ -696,7 +696,7 @@ int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owne wrapped_ctx->ctx = ctx; } - ret = _z_query(zs._val, keyexpr, parameters, opt.target, opt.consolidation.mode, opt.with_value, __z_reply_handler, + ret = _z_query(zs._val, keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, __z_reply_handler, wrapped_ctx, callback->drop, ctx); return ret; } diff --git a/src/net/primitives.c b/src/net/primitives.c index c029512ed..b28f82efa 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -332,7 +332,7 @@ int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *pay /*------------------ 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 with_value, _z_reply_handler_t callback, + const z_consolidation_mode_t consolidation, _z_value_t value, _z_reply_handler_t callback, void *arg_call, _z_drop_handler_t dropper, void *arg_drop) { int8_t ret = _Z_RES_OK; @@ -354,7 +354,7 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, ret = _z_register_pending_query(zn, pq); // Add the pending query to the current session if (ret == _Z_RES_OK) { _z_zenoh_message_t z_msg = - _z_msg_make_query(keyexpr, pq->_parameters, pq->_id, pq->_target, pq->_consolidation, with_value); + _z_msg_make_query(keyexpr, pq->_parameters, pq->_id, pq->_target, pq->_consolidation, value); if (_z_send_z_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { _z_unregister_pending_query(zn, pq); diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 4628a089f..42cd2da34 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -365,7 +365,7 @@ void _z_msg_clear_pull(_z_msg_pull_t *msg) { _z_keyexpr_clear(&msg->_key); } /*------------------ Query Message ------------------*/ _z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, char *parameters, _z_zint_t qid, z_query_target_t target, - z_consolidation_mode_t consolidation, _z_value_t with_value) { + z_consolidation_mode_t consolidation, _z_value_t value) { _z_zenoh_message_t msg; msg._body._query._key = key; @@ -374,8 +374,8 @@ _z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, char *parameters, _z_zint msg._body._query._target = target; msg._body._query._consolidation = consolidation; (void)memset(&msg._body._query._info, 0, sizeof(msg._body._query._info)); - msg._body._query._info._encoding = with_value.encoding; - msg._body._query._payload = with_value.payload; + msg._body._query._info._encoding = value.encoding; + msg._body._query._payload = value.payload; msg._header = _Z_MID_QUERY; if (msg._body._query._target != Z_QUERY_TARGET_BEST_MATCHING) { diff --git a/src/session/queryable.c b/src/session/queryable.c index 928481a23..466e2c54c 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -156,8 +156,8 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query) { q._qid = query->_qid; q._key = key; q._parameters = query->_parameters; - q._with_value.encoding = query->_info._encoding; - q._with_value.payload = query->_payload; + q._value.encoding = query->_info._encoding; + q._value.payload = query->_payload; q._anyke = (strstr(q._parameters, Z_SELECTOR_QUERY_MATCH) == NULL) ? false : true; _z_questionable_sptr_list_t *xs = qles; while (xs != NULL) { diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 2c043aa13..d409da3dc 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -1381,16 +1381,16 @@ _z_zenoh_message_t gen_query_message(void) { z_consolidation_mode_t consolidation; consolidation = con[gen_uint8() % (sizeof(con) / sizeof(uint8_t))]; - _z_value_t with_value; + _z_value_t value; if (gen_bool()) { - with_value = gen_value(); + value = gen_value(); } else { - with_value.encoding.prefix = Z_ENCODING_PREFIX_EMPTY; - with_value.encoding.suffix = _z_bytes_empty(); - with_value.payload = _z_bytes_empty(); + value.encoding.prefix = Z_ENCODING_PREFIX_EMPTY; + value.encoding.suffix = _z_bytes_empty(); + value.payload = _z_bytes_empty(); } - return _z_msg_make_query(key, parameters, qid, target, consolidation, with_value); + return _z_msg_make_query(key, parameters, qid, target, consolidation, value); } void assert_eq_query_message(_z_msg_query_t *left, _z_msg_query_t *right, uint8_t header) { From 2b05590bf86148517c1a161273a7c0d17d50cb56 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 28 Apr 2023 10:49:27 +0200 Subject: [PATCH 082/181] provide ping/pong examples (#212) Co-authored-by: Luca Cominardi --- examples/CMakeLists.txt | 6 ++ examples/unix/c11/z_ping.c | 128 +++++++++++++++++++++++++++++++++++++ examples/unix/c11/z_pong.c | 31 +++++++++ examples/unix/c99/z_ping.c | 127 ++++++++++++++++++++++++++++++++++++ examples/unix/c99/z_pong.c | 32 ++++++++++ examples/windows/z_ping.c | 126 ++++++++++++++++++++++++++++++++++++ examples/windows/z_pong.c | 31 +++++++++ 7 files changed, 481 insertions(+) create mode 100644 examples/unix/c11/z_ping.c create mode 100644 examples/unix/c11/z_pong.c create mode 100644 examples/unix/c99/z_ping.c create mode 100644 examples/unix/c99/z_pong.c create mode 100644 examples/windows/z_ping.c create mode 100644 examples/windows/z_pong.c diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a9a55b8d6..837e41199 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -30,6 +30,8 @@ if(UNIX) add_example(z_queryable unix/c99/z_queryable.c) add_example(z_info unix/c99/z_info.c) add_example(z_scout unix/c99/z_scout.c) + add_example(z_ping unix/c99/z_ping.c) + add_example(z_pong unix/c99/z_pong.c) else() add_example(z_put unix/c11/z_put.c) add_example(z_pub unix/c11/z_pub.c) @@ -41,6 +43,8 @@ if(UNIX) add_example(z_queryable unix/c11/z_queryable.c) add_example(z_info unix/c11/z_info.c) add_example(z_scout unix/c11/z_scout.c) + add_example(z_ping unix/c11/z_ping.c) + add_example(z_pong unix/c11/z_pong.c) endif() elseif(MSVC) add_example(z_put windows/z_put.c) @@ -53,4 +57,6 @@ elseif(MSVC) add_example(z_queryable windows/z_queryable.c) add_example(z_info windows/z_info.c) add_example(z_scout windows/z_scout.c) + add_example(z_ping windows/z_ping.c) + add_example(z_pong windows/z_pong.c) endif() diff --git a/examples/unix/c11/z_ping.c b/examples/unix/c11/z_ping.c new file mode 100644 index 000000000..2472196fe --- /dev/null +++ b/examples/unix/c11/z_ping.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include + +#include "zenoh-pico.h" +#include "zenoh-pico/system/platform.h" + +// WARNING: for the sake of this example we are using "internal" structs and functions (starting with "_"). +// Synchronisation primitives are planned to be added to the API in the future. +_z_condvar_t cond; +_z_mutex_t mutex; + +void callback(const z_sample_t* sample, void* context) { + (void)sample; + (void)context; + _z_condvar_signal(&cond); +} +void drop(void* context) { + (void)context; + _z_condvar_free(&cond); +} + +struct args_t { + unsigned int size; // -s + unsigned int number_of_pings; // -n + unsigned int warmup_ms; // -w + uint8_t help_requested; // -h +}; +struct args_t parse_args(int argc, char** argv); + +int main(int argc, char** argv) { + struct args_t args = parse_args(argc, argv); + if (args.help_requested) { + printf( + "\ + -n (optional, int, default=4): the number of pings to be attempted\n\ + -s (optional, int, default=8): the size of the payload embedded in the ping and repeated by the pong\n\ + -w (optional, int, default=0): the warmup time in ms during which pings will be emitted but not measured\n\ + -c (optional, string): the path to a configuration file for the session. If this option isn't passed, the default configuration will be used.\n\ + "); + return 1; + } + _z_mutex_init(&mutex); + _z_condvar_init(&cond); + z_owned_config_t config = z_config_default(); + z_owned_session_t session = z_open(z_move(config)); + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_publisher_t pub = z_declare_publisher(z_loan(session), ping, NULL); + z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)(&pub)); + z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), pong, z_move(respond), NULL); + uint8_t* data = z_malloc(args.size); + for (unsigned int i = 0; i < args.size; i++) { + data[i] = i % 10; + } + _z_mutex_lock(&mutex); + if (args.warmup_ms) { + printf("Warming up for %dms...\n", args.warmup_ms); + clock_t warmup_end = clock() + CLOCKS_PER_SEC * args.warmup_ms / 1000; + for (clock_t now = clock(); now < warmup_end; now = clock()) { + z_publisher_put(z_loan(pub), data, args.size, NULL); + _z_condvar_wait(&cond, &mutex); + } + } + clock_t* results = z_malloc(sizeof(clock_t) * args.number_of_pings); + for (unsigned int i = 0; i < args.number_of_pings; i++) { + clock_t start = clock(); + z_publisher_put(z_loan(pub), data, args.size, NULL); + _z_condvar_wait(&cond, &mutex); + clock_t end = clock(); + results[i] = end - start; + } + for (unsigned int i = 0; i < args.number_of_pings; i++) { + clock_t rtt = results[i] * 1000000 / CLOCKS_PER_SEC; + printf("%d bytes: seq=%d rtt=%ldµs lat=%ldµs\n", args.size, i, rtt, rtt / 2); + } + _z_mutex_unlock(&mutex); + z_free(results); + z_free(data); + z_drop(z_move(sub)); + z_drop(z_move(pub)); + z_close(z_move(session)); +} + +char* getopt(int argc, char** argv, char option) { + for (int i = 0; i < argc; i++) { + size_t len = strlen(argv[i]); + if (len >= 2 && argv[i][0] == '-' && argv[i][1] == option) { + if (len > 2 && argv[i][2] == '=') { + return argv[i] + 3; + } else if (i + 1 < argc) { + return argv[i + 1]; + } + } + } + return NULL; +} + +struct args_t parse_args(int argc, char** argv) { + for (int i = 0; i < argc; i++) { + if (strcmp(argv[i], "-h") == 0) { + return (struct args_t){.help_requested = 1}; + } + } + char* arg = getopt(argc, argv, 's'); + unsigned int size = 8; + if (arg) { + size = atoi(arg); + } + arg = getopt(argc, argv, 'n'); + unsigned int number_of_pings = 4; + if (arg) { + number_of_pings = atoi(arg); + } + arg = getopt(argc, argv, 'w'); + unsigned int warmup_ms = 0; + if (arg) { + warmup_ms = atoi(arg); + } + return (struct args_t){ + .help_requested = 0, + .size = size, + .number_of_pings = number_of_pings, + .warmup_ms = warmup_ms, + }; +} \ No newline at end of file diff --git a/examples/unix/c11/z_pong.c b/examples/unix/c11/z_pong.c new file mode 100644 index 000000000..7f0731478 --- /dev/null +++ b/examples/unix/c11/z_pong.c @@ -0,0 +1,31 @@ +#include "stdio.h" +#include "zenoh-pico.h" + +void callback(const z_sample_t* sample, void* context) { + z_publisher_t pub = z_loan(*(z_owned_publisher_t*)context); + z_publisher_put(pub, sample->payload.start, sample->payload.len, NULL); +} +void drop(void* context) { + z_owned_publisher_t* pub = (z_owned_publisher_t*)context; + z_drop(pub); + // A note on lifetimes: + // here, `sub` takes ownership of `pub` and will drop it before returning from its own `drop`, + // which makes passing a pointer to the stack safe as long as `sub` is dropped in a scope where `pub` is still + // valid. +} + +int main(int argc, char** argv) { + (void)argc; + (void)argv; + z_owned_config_t config = z_config_default(); + z_owned_session_t session = z_open(z_move(config)); + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_publisher_t pub = z_declare_publisher(z_loan(session), pong, NULL); + z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub)); + z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), ping, z_move(respond), NULL); + while (getchar() != 'q') { + } + z_drop(z_move(sub)); + z_close(z_move(session)); +} \ No newline at end of file diff --git a/examples/unix/c99/z_ping.c b/examples/unix/c99/z_ping.c new file mode 100644 index 000000000..9bbb18695 --- /dev/null +++ b/examples/unix/c99/z_ping.c @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include + +#include "zenoh-pico.h" +#include "zenoh-pico/api/primitives.h" +#include "zenoh-pico/system/platform.h" + +_z_condvar_t cond; +_z_mutex_t mutex; + +void callback(const z_sample_t* sample, void* context) { + (void)sample; + (void)context; + _z_condvar_signal(&cond); +} +void drop(void* context) { + (void)context; + _z_condvar_free(&cond); +} + +struct args_t { + unsigned int size; // -s + unsigned int number_of_pings; // -n + unsigned int warmup_ms; // -w + uint8_t help_requested; // -h +}; +struct args_t parse_args(int argc, char** argv); + +int main(int argc, char** argv) { + struct args_t args = parse_args(argc, argv); + if (args.help_requested) { + printf( + "\ + -n (optional, int, default=4): the number of pings to be attempted\n\ + -s (optional, int, default=8): the size of the payload embedded in the ping and repeated by the pong\n\ + -w (optional, int, default=0): the warmup time in ms during which pings will be emitted but not measured\n\ + -c (optional, string): the path to a configuration file for the session. If this option isn't passed, the default configuration will be used.\n\ + "); + return 1; + } + _z_mutex_init(&mutex); + _z_condvar_init(&cond); + z_owned_config_t config = z_config_default(); + z_owned_session_t session = z_open(z_move(config)); + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_publisher_t pub = z_declare_publisher(z_session_loan(&session), ping, NULL); + z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)(&pub)); + z_owned_subscriber_t sub = z_declare_subscriber(z_session_loan(&session), pong, z_move(respond), NULL); + uint8_t* data = z_malloc(args.size); + for (unsigned int i = 0; i < args.size; i++) { + data[i] = i % 10; + } + _z_mutex_lock(&mutex); + if (args.warmup_ms) { + printf("Warming up for %dms...\n", args.warmup_ms); + clock_t warmup_end = clock() + CLOCKS_PER_SEC * args.warmup_ms / 1000; + for (clock_t now = clock(); now < warmup_end; now = clock()) { + z_publisher_put(z_publisher_loan(&pub), data, args.size, NULL); + _z_condvar_wait(&cond, &mutex); + } + } + clock_t* results = z_malloc(sizeof(clock_t) * args.number_of_pings); + for (unsigned int i = 0; i < args.number_of_pings; i++) { + clock_t start = clock(); + z_publisher_put(z_publisher_loan(&pub), data, args.size, NULL); + _z_condvar_wait(&cond, &mutex); + clock_t end = clock(); + results[i] = end - start; + } + for (unsigned int i = 0; i < args.number_of_pings; i++) { + clock_t rtt = results[i] * 1000000 / CLOCKS_PER_SEC; + printf("%d bytes: seq=%d rtt=%ldµs lat=%ldµs\n", args.size, i, rtt, rtt / 2); + } + _z_mutex_unlock(&mutex); + z_free(results); + z_free(data); + z_undeclare_subscriber(z_move(sub)); + z_undeclare_publisher(z_move(pub)); + z_close(z_move(session)); +} + +char* getopt(int argc, char** argv, char option) { + for (int i = 0; i < argc; i++) { + size_t len = strlen(argv[i]); + if (len >= 2 && argv[i][0] == '-' && argv[i][1] == option) { + if (len > 2 && argv[i][2] == '=') { + return argv[i] + 3; + } else if (i + 1 < argc) { + return argv[i + 1]; + } + } + } + return NULL; +} + +struct args_t parse_args(int argc, char** argv) { + for (int i = 0; i < argc; i++) { + if (strcmp(argv[i], "-h") == 0) { + return (struct args_t){.help_requested = 1}; + } + } + char* arg = getopt(argc, argv, 's'); + unsigned int size = 8; + if (arg) { + size = atoi(arg); + } + arg = getopt(argc, argv, 'n'); + unsigned int number_of_pings = 4; + if (arg) { + number_of_pings = atoi(arg); + } + arg = getopt(argc, argv, 'w'); + unsigned int warmup_ms = 0; + if (arg) { + warmup_ms = atoi(arg); + } + return (struct args_t){ + .help_requested = 0, + .size = size, + .number_of_pings = number_of_pings, + .warmup_ms = warmup_ms, + }; +} \ No newline at end of file diff --git a/examples/unix/c99/z_pong.c b/examples/unix/c99/z_pong.c new file mode 100644 index 000000000..49c99c488 --- /dev/null +++ b/examples/unix/c99/z_pong.c @@ -0,0 +1,32 @@ +#include "stdio.h" +#include "zenoh-pico.h" +#include "zenoh-pico/api/primitives.h" + +void callback(const z_sample_t* sample, void* context) { + z_publisher_t pub = z_publisher_loan((z_owned_publisher_t*)context); + z_publisher_put(pub, sample->payload.start, sample->payload.len, NULL); +} +void drop(void* context) { + z_owned_publisher_t* pub = (z_owned_publisher_t*)context; + z_undeclare_publisher(pub); + // A note on lifetimes: + // here, `sub` takes ownership of `pub` and will drop it before returning from its own `drop`, + // which makes passing a pointer to the stack safe as long as `sub` is dropped in a scope where `pub` is still + // valid. +} + +int main(int argc, char** argv) { + (void)argc; + (void)argv; + z_owned_config_t config = z_config_default(); + z_owned_session_t session = z_open(z_move(config)); + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_publisher_t pub = z_declare_publisher(z_session_loan(&session), pong, NULL); + z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub)); + z_owned_subscriber_t sub = z_declare_subscriber(z_session_loan(&session), ping, z_move(respond), NULL); + while (getchar() != 'q') { + } + z_undeclare_subscriber(z_move(sub)); + z_close(z_move(session)); +} \ No newline at end of file diff --git a/examples/windows/z_ping.c b/examples/windows/z_ping.c new file mode 100644 index 000000000..1ce6759ef --- /dev/null +++ b/examples/windows/z_ping.c @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include + +#include "zenoh-pico.h" +#include "zenoh-pico/system/platform.h" + +_z_condvar_t cond; +_z_mutex_t mutex; + +void callback(const z_sample_t* sample, void* context) { + (void)sample; + (void)context; + _z_condvar_signal(&cond); +} +void drop(void* context) { + (void)context; + _z_condvar_free(&cond); +} + +struct args_t { + unsigned int size; // -s + unsigned int number_of_pings; // -n + unsigned int warmup_ms; // -w + uint8_t help_requested; // -h +}; +struct args_t parse_args(int argc, char** argv); + +int main(int argc, char** argv) { + struct args_t args = parse_args(argc, argv); + if (args.help_requested) { + printf( + "\ + -n (optional, int, default=4): the number of pings to be attempted\n\ + -s (optional, int, default=8): the size of the payload embedded in the ping and repeated by the pong\n\ + -w (optional, int, default=0): the warmup time in ms during which pings will be emitted but not measured\n\ + -c (optional, string): the path to a configuration file for the session. If this option isn't passed, the default configuration will be used.\n\ + "); + return 1; + } + _z_mutex_init(&mutex); + _z_condvar_init(&cond); + z_owned_config_t config = z_config_default(); + z_owned_session_t session = z_open(z_move(config)); + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_publisher_t pub = z_declare_publisher(z_loan(session), ping, NULL); + z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)(&pub)); + z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), pong, z_move(respond), NULL); + uint8_t* data = z_malloc(args.size); + for (unsigned int i = 0; i < args.size; i++) { + data[i] = i % 10; + } + _z_mutex_lock(&mutex); + if (args.warmup_ms) { + printf("Warming up for %dms...\n", args.warmup_ms); + clock_t warmup_end = clock() + CLOCKS_PER_SEC * args.warmup_ms / 1000; + for (clock_t now = clock(); now < warmup_end; now = clock()) { + z_publisher_put(z_loan(pub), data, args.size, NULL); + _z_condvar_wait(&cond, &mutex); + } + } + clock_t* results = z_malloc(sizeof(clock_t) * args.number_of_pings); + for (unsigned int i = 0; i < args.number_of_pings; i++) { + clock_t start = clock(); + z_publisher_put(z_loan(pub), data, args.size, NULL); + _z_condvar_wait(&cond, &mutex); + clock_t end = clock(); + results[i] = end - start; + } + for (unsigned int i = 0; i < args.number_of_pings; i++) { + clock_t rtt = results[i] * 1000000 / CLOCKS_PER_SEC; + printf("%d bytes: seq=%d rtt=%ldµs lat=%ldµs\n", args.size, i, rtt, rtt / 2); + } + _z_mutex_unlock(&mutex); + z_free(results); + z_free(data); + z_drop(z_move(sub)); + z_drop(z_move(pub)); + z_close(z_move(session)); +} + +char* getopt(int argc, char** argv, char option) { + for (int i = 0; i < argc; i++) { + size_t len = strlen(argv[i]); + if (len >= 2 && argv[i][0] == '-' && argv[i][1] == option) { + if (len > 2 && argv[i][2] == '=') { + return argv[i] + 3; + } else if (i + 1 < argc) { + return argv[i + 1]; + } + } + } + return NULL; +} + +struct args_t parse_args(int argc, char** argv) { + for (int i = 0; i < argc; i++) { + if (strcmp(argv[i], "-h") == 0) { + return (struct args_t){.help_requested = 1}; + } + } + char* arg = getopt(argc, argv, 's'); + unsigned int size = 8; + if (arg) { + size = atoi(arg); + } + arg = getopt(argc, argv, 'n'); + unsigned int number_of_pings = 4; + if (arg) { + number_of_pings = atoi(arg); + } + arg = getopt(argc, argv, 'w'); + unsigned int warmup_ms = 0; + if (arg) { + warmup_ms = atoi(arg); + } + return (struct args_t){ + .help_requested = 0, + .size = size, + .number_of_pings = number_of_pings, + .warmup_ms = warmup_ms, + }; +} \ No newline at end of file diff --git a/examples/windows/z_pong.c b/examples/windows/z_pong.c new file mode 100644 index 000000000..7f0731478 --- /dev/null +++ b/examples/windows/z_pong.c @@ -0,0 +1,31 @@ +#include "stdio.h" +#include "zenoh-pico.h" + +void callback(const z_sample_t* sample, void* context) { + z_publisher_t pub = z_loan(*(z_owned_publisher_t*)context); + z_publisher_put(pub, sample->payload.start, sample->payload.len, NULL); +} +void drop(void* context) { + z_owned_publisher_t* pub = (z_owned_publisher_t*)context; + z_drop(pub); + // A note on lifetimes: + // here, `sub` takes ownership of `pub` and will drop it before returning from its own `drop`, + // which makes passing a pointer to the stack safe as long as `sub` is dropped in a scope where `pub` is still + // valid. +} + +int main(int argc, char** argv) { + (void)argc; + (void)argv; + z_owned_config_t config = z_config_default(); + z_owned_session_t session = z_open(z_move(config)); + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_publisher_t pub = z_declare_publisher(z_loan(session), pong, NULL); + z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub)); + z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), ping, z_move(respond), NULL); + while (getchar() != 'q') { + } + z_drop(z_move(sub)); + z_close(z_move(session)); +} \ No newline at end of file From 152bef6dc4f4debdc7882724b32defd722136e9a Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Wed, 3 May 2023 09:40:18 +0200 Subject: [PATCH 083/181] Update release.yml --- .github/release.yml | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/.github/release.yml b/.github/release.yml index 4523efea0..3e4ee51f7 100644 --- a/.github/release.yml +++ b/.github/release.yml @@ -1,16 +1,16 @@ -// -// Copyright (c) 2023 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, -// +# +# Copyright (c) 2023 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, +# changelog: categories: @@ -22,4 +22,4 @@ changelog: - bug - title: Other changes labels: - - "*" \ No newline at end of file + - "*" From ed467af65b698da087d773154f2bea794c00461d Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Wed, 3 May 2023 11:40:18 +0200 Subject: [PATCH 084/181] Update release.yml --- .github/release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/release.yml b/.github/release.yml index 3e4ee51f7..c4022026e 100644 --- a/.github/release.yml +++ b/.github/release.yml @@ -3,8 +3,8 @@ # # 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. +# 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 # From f51284e46d6b09378fb6e5b37e07e2910683a40a Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Wed, 31 May 2023 09:27:49 +0200 Subject: [PATCH 085/181] Align with zid branch (#216) Co-authored-by: Pierre Avital --- include/zenoh-pico/api/types.h | 4 +-- include/zenoh-pico/protocol/core.h | 10 +++++++- src/protocol/msg.c | 2 +- src/protocol/msgcodec.c | 39 ++++++++++++++++++++++++++++-- src/session/utils.c | 8 +++--- tests/z_msgcodec_test.c | 7 +++--- 6 files changed, 56 insertions(+), 14 deletions(-) diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 25ebb72c1..f62d495cd 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -60,9 +60,7 @@ _Bool z_bytes_check(const z_bytes_t *v); * Members: * uint8_t id[16]: The array containing the 16 octets of a Zenoh ID. */ -typedef struct { - uint8_t id[16]; -} z_id_t; +typedef _z_id_t z_id_t; /** * Represents a string without null-terminator. diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 11cb5c6da..4909c4504 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -38,6 +38,14 @@ */ typedef size_t _z_zint_t; +/** + * A zenoh ID. + */ + +typedef struct { + uint8_t id[16]; +} _z_id_t; + /** * A zenoh encoding. */ @@ -50,7 +58,7 @@ typedef struct { * A zenoh timestamp. */ typedef struct { - _z_bytes_t id; + _z_id_t id; uint64_t time; } _z_timestamp_t; diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 42cd2da34..7d0c9ddba 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -27,7 +27,7 @@ void _z_payload_clear(_z_payload_t *p) { _z_bytes_clear(p); } /*------------------ Timestamp Field ------------------*/ -void _z_timestamp_clear(_z_timestamp_t *ts) { _z_bytes_clear(&ts->id); } +void _z_timestamp_clear(_z_timestamp_t *ts) {} /*------------------ ResKey Field ------------------*/ void _z_keyexpr_clear(_z_keyexpr_t *rk) { diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 4dbc9d5d5..e2793d810 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -15,7 +15,11 @@ #include "zenoh-pico/protocol/msgcodec.h" #include +#include +#include +#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/utils/logging.h" @@ -38,13 +42,44 @@ int8_t _z_payload_decode_na(_z_payload_t *pld, _z_zbuf_t *zbf) { int8_t _z_payload_decode(_z_payload_t *pld, _z_zbuf_t *zbf) { return _z_payload_decode_na(pld, zbf); } +int8_t _z_id_encode(_z_wbuf_t *wbf, const _z_id_t *id) { + int len; + int8_t ret = _Z_RES_OK; + for (len = 15; len > 0; len--) { + if (id->id[len]) { + break; + } + } + len++; // `len` is treated as an "end" until this point, and as a length from then on + if (id->id[len] != 0) { + ret |= _z_wbuf_write(wbf, len); + ret |= _z_wbuf_write_bytes(wbf, id->id, 0, len); + } else { + _Z_DEBUG("Attempted to encode invalid ID 0"); + ret = _Z_ERR_MESSAGE_ZENOH_UNKNOWN; + } + return ret; +} + +/// Decodes a `zid` from the zbf, returning a negative value in case of error. +/// +/// Note that while `_z_id_t` has an error state (full 0s), this function doesn't +/// guarantee that this state will be set in case of errors. +int8_t _z_id_decode(_z_id_t *id, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + uint8_t len = _z_zbuf_read(zbf); + _z_zbuf_read_bytes(zbf, id->id, 0, len); + memset(id->id + len, 0, 16 - len); + return ret; +} + /*------------------ Timestamp Field ------------------*/ int8_t _z_timestamp_encode(_z_wbuf_t *wbf, const _z_timestamp_t *ts) { int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _TIMESTAMP\n"); _Z_EC(_z_uint64_encode(wbf, ts->time)) - ret |= _z_bytes_encode(wbf, &ts->id); + ret |= _z_id_encode(wbf, &ts->id); return ret; } @@ -54,7 +89,7 @@ int8_t _z_timestamp_decode_na(_z_timestamp_t *ts, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; ret |= _z_uint64_decode(&ts->time, zbf); - ret |= _z_bytes_decode(&ts->id, zbf); + ret |= _z_id_decode(&ts->id, zbf); return ret; } diff --git a/src/session/utils.c b/src/session/utils.c index ce4b8d4af..bba256942 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -36,19 +36,19 @@ _z_keyexpr_t _z_keyexpr_duplicate(const _z_keyexpr_t *src) { _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp) { _z_timestamp_t ts; - _z_bytes_copy(&ts.id, &tstamp->id); + ts.id = tstamp->id; ts.time = tstamp->time; return ts; } void _z_timestamp_reset(_z_timestamp_t *tstamp) { - _z_bytes_reset(&tstamp->id); + memset(&tstamp->id, 0, sizeof(_z_id_t)); tstamp->time = 0; } _Bool _z_timestamp_check(const _z_timestamp_t *stamp) { - for (size_t i = 0; i < stamp->id.len; ++i) - if (stamp->id.start[i]) return true; + for (uint8_t i = 0; i < sizeof(_z_id_t); ++i) + if (stamp->id.id[i]) return true; return false; } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index d409da3dc..32d1b3af7 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -12,6 +12,7 @@ // ZettaScale Zenoh Team, // +#include #define ZENOH_PICO_TEST_H #include @@ -331,8 +332,8 @@ void payload_field(void) { _z_timestamp_t gen_timestamp(void) { _z_timestamp_t ts; ts.time = gen_uint64(); - ts.id = gen_bytes(16); - + _z_bytes_t id = gen_bytes(16); + memcpy(ts.id.id, id.start, id.len); return ts; } @@ -342,7 +343,7 @@ void assert_eq_timestamp(_z_timestamp_t *left, _z_timestamp_t *right) { assert(left->time == right->time); printf("ID ("); - assert_eq_uint8_array(&left->id, &right->id); + assert(memcmp(left->id.id, right->id.id, 16) == 0); printf(")"); } From 0fb11f913595196fbb6ad91ccfea60c46d2dfb0b Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Wed, 31 May 2023 10:52:13 +0200 Subject: [PATCH 086/181] Fix timestamp clear (#217) --- src/net/memory.c | 4 ++-- src/session/query.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/net/memory.c b/src/net/memory.c index 914f58826..23eb9b0cf 100644 --- a/src/net/memory.c +++ b/src/net/memory.c @@ -25,8 +25,8 @@ void _z_sample_move(_z_sample_t *dst, _z_sample_t *src) { dst->encoding.prefix = src->encoding.prefix; // FIXME: call the z_encoding_move _z_bytes_move(&dst->encoding.suffix, &src->encoding.suffix); // FIXME: call the z_encoding_move - dst->timestamp.time = src->timestamp.time; // FIXME: call the z_timestamp_move - _z_bytes_move(&dst->timestamp.id, &src->timestamp.id); // FIXME: call the z_timestamp_move + dst->timestamp.time = src->timestamp.time; // FIXME: call the z_timestamp_move + dst->timestamp.id = src->timestamp.id; // FIXME: call the z_timestamp_move } void _z_sample_clear(_z_sample_t *sample) { diff --git a/src/session/query.c b/src/session/query.c index f921ae8a5..6951dbede 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -53,7 +53,7 @@ void _z_pending_reply_clear(_z_pending_reply_t *pr) { _z_reply_clear(&pr->_reply); // Free the timestamp - _z_bytes_clear(&pr->_tstamp.id); + _z_timestamp_clear(&pr->_tstamp); } void _z_pending_query_clear(_z_pending_query_t *pen_qry) { From e0f5fd74fb7ec64bcccf4f8e88d849812c3655bf Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Thu, 1 Jun 2023 14:18:16 +0200 Subject: [PATCH 087/181] fix UB in vec_make(0) --- include/zenoh-pico/protocol/extcodec.h | 8 ++-- src/collections/vec.c | 9 ++-- src/protocol/extcodec.c | 58 ++++++++++++++++++-------- src/protocol/msgcodec.c | 41 ++++-------------- tests/z_msgcodec_test.c | 5 ++- 5 files changed, 60 insertions(+), 61 deletions(-) diff --git a/include/zenoh-pico/protocol/extcodec.h b/include/zenoh-pico/protocol/extcodec.h index 05277523b..0ec1d49b3 100644 --- a/include/zenoh-pico/protocol/extcodec.h +++ b/include/zenoh-pico/protocol/extcodec.h @@ -20,9 +20,11 @@ #include "zenoh-pico/protocol/iobuf.h" /*------------------ Message Extension ------------------*/ -int8_t _z_msg_ext_encode(_z_wbuf_t *wbf, const _z_msg_ext_t *ext); -int8_t _z_msg_ext_decode(_z_msg_ext_t *ext, _z_zbuf_t *zbf); -int8_t _z_msg_ext_decode_na(_z_msg_ext_t *ext, _z_zbuf_t *zbf); +int8_t _z_msg_ext_encode(_z_wbuf_t *wbf, const _z_msg_ext_t *ext, _Bool has_next); +int8_t _z_msg_ext_decode(_z_msg_ext_t *ext, _z_zbuf_t *zbf, _Bool *has_next); +int8_t _z_msg_ext_decode_na(_z_msg_ext_t *ext, _z_zbuf_t *zbf, _Bool *has_next); +int8_t _z_msg_ext_vec_encode(_z_wbuf_t *wbf, const _z_msg_ext_vec_t *extensions); +int8_t _z_msg_ext_vec_decode(_z_msg_ext_vec_t *extensions, _z_zbuf_t *zbf); #endif /* ZENOH_PICO_MSGEXTCODEC_H */ diff --git a/src/collections/vec.c b/src/collections/vec.c index d4e73b4bb..20afff6ca 100644 --- a/src/collections/vec.c +++ b/src/collections/vec.c @@ -20,14 +20,13 @@ /*-------- vec --------*/ _z_vec_t _z_vec_make(size_t capacity) { - _z_vec_t v; - v._val = (void **)z_malloc(sizeof(void *) * capacity); + _z_vec_t v = {._capacity = capacity, ._len = 0, ._val = NULL}; + if (capacity != 0) { + v._val = (void **)z_malloc(sizeof(void *) * capacity); + } if (v._val != NULL) { v._capacity = capacity; - } else { - v._capacity = 0; } - v._len = 0; return v; } diff --git a/src/protocol/extcodec.c b/src/protocol/extcodec.c index 8a087639c..ceaa34521 100644 --- a/src/protocol/extcodec.c +++ b/src/protocol/extcodec.c @@ -14,8 +14,12 @@ #include "zenoh-pico/protocol/extcodec.h" +#include +#include + #include "zenoh-pico/protocol/codec.h" #include "zenoh-pico/protocol/ext.h" +#include "zenoh-pico/system/platform.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" @@ -64,10 +68,10 @@ int8_t _z_msg_ext_decode_zbuf(_z_msg_ext_zbuf_t *ext, _z_zbuf_t *zbf) { int8_t _z_msg_ext_decode_zbuf_na(_z_msg_ext_zbuf_t *ext, _z_zbuf_t *zbf) { return _z_msg_ext_decode_zbuf(ext, zbf); } /*------------------ Message Extension ------------------*/ -int8_t _z_msg_ext_encode(_z_wbuf_t *wbf, const _z_msg_ext_t *ext) { +int8_t _z_msg_ext_encode(_z_wbuf_t *wbf, const _z_msg_ext_t *ext, _Bool has_next) { int8_t ret = _Z_RES_OK; - _Z_EC(_z_wbuf_write(wbf, ext->_header)) + _Z_EC(_z_wbuf_write(wbf, _Z_EXT_FULL_ID(ext->_header) | (has_next << 7))) uint8_t enc = _Z_EXT_ENC(ext->_header); switch (enc) { @@ -115,28 +119,46 @@ int8_t _z_msg_ext_unknown_body_decode(_z_msg_ext_body_t *body, uint8_t enc, _z_z return ret; } -int8_t _z_msg_ext_decode(_z_msg_ext_t *ext, _z_zbuf_t *zbf) { +int8_t _z_msg_ext_decode(_z_msg_ext_t *ext, _z_zbuf_t *zbf, _Bool *has_next) { int8_t ret = _Z_RES_OK; ret |= _z_uint8_decode(&ext->_header, zbf); // Decode the header if (ret == _Z_RES_OK) { - uint8_t id = _Z_EXT_FULL_ID(ext->_header); - switch (id) { - // case _Z_MSG_EXT_ID_FOO: { - // ret |= _z_msg_ext_decode_foo(&ext->_body, zbf); - // } break; - default: { - _Z_DEBUG("WARNING: Trying to decode message extension with unknown id(%d)\n", id); - if (_Z_EXT_HAS_FLAG(ext->_header, _Z_MSG_EXT_FLAG_M) == false) { - ret |= _z_msg_ext_unknown_body_decode(&ext->_body, _Z_EXT_ENC(ext->_header), zbf); - } else { - ret |= _Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN; - } - } break; - } + // TODO: define behaviour on decode failure, regarding zbuf allocation + ret |= _z_msg_ext_unknown_body_decode(&ext->_body, _Z_EXT_ENC(ext->_header), zbf); } + *has_next = (ext->_header & _Z_MSG_EXT_FLAG_Z) != 0; + ext->_header |= _Z_EXT_FULL_ID_MASK; return ret; } -int8_t _z_msg_ext_decode_na(_z_msg_ext_t *ext, _z_zbuf_t *zbf) { return _z_msg_ext_decode(ext, zbf); } +int8_t _z_msg_ext_decode_na(_z_msg_ext_t *ext, _z_zbuf_t *zbf, _Bool *has_next) { + return _z_msg_ext_decode(ext, zbf, has_next); +} + +int8_t _z_msg_ext_vec_encode(_z_wbuf_t *wbf, const _z_msg_ext_vec_t *extensions) { + int8_t ret = _Z_RES_OK; + size_t len = _z_msg_ext_vec_len(extensions); + if (len > 0) { + int i; + for (i = 0; ret == _Z_RES_OK && i < len - 1; i++) { + ret |= _z_msg_ext_encode(wbf, _z_msg_ext_vec_get(extensions, i), true); + } + if (ret == _Z_RES_OK) { + ret |= _z_msg_ext_encode(wbf, _z_msg_ext_vec_get(extensions, i), true); + } + } + return ret; +} +int8_t _z_msg_ext_vec_decode(_z_msg_ext_vec_t *extensions, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + _z_msg_ext_vec_reset(extensions); + _Bool has_next = true; + while (has_next && ret == _Z_RES_OK) { + _z_msg_ext_t *ext = (_z_msg_ext_t *)z_malloc(sizeof(_z_msg_ext_t)); + ret |= _z_msg_ext_decode(ext, zbf, &has_next); + _z_msg_ext_vec_append(extensions, ext); + } + return ret; +} \ No newline at end of file diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 0e3d0a5c8..59348ff66 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -20,6 +20,7 @@ #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/extcodec.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/keyexpr.h" @@ -1610,13 +1611,7 @@ int8_t _z_extensions_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_ext_vec _Z_DEBUG("Encoding _Z_TRANSPORT_EXTENSIONS\n"); if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true) { - size_t n_ext = _z_msg_ext_vec_len(v_ext); - for (size_t i = 0; (i + 1) < n_ext; i++) { - _z_msg_ext_t *ext = _z_msg_ext_vec_get(v_ext, i); - _Z_EXT_SET_FLAG(ext->_header, _Z_MSG_EXT_FLAG_Z); - ret |= _z_msg_ext_encode(wbf, ext); - } - ret |= _z_msg_ext_encode(wbf, _z_msg_ext_vec_get(v_ext, n_ext - 1)); // Last extension wo\ next flag + ret |= _z_msg_ext_vec_encode(wbf, v_ext); } return ret; @@ -1628,15 +1623,9 @@ int8_t _z_extensions_decode_na(_z_msg_ext_vec_t *v_ext, _z_zbuf_t *zbf, uint8_t _Z_DEBUG("Decoding _Z_TRANSPORT_EXTENSIONS\n"); if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true) { - *v_ext = _z_msg_ext_vec_make(10); // TODO[protocol]: make it a list - _z_msg_ext_t *ext = NULL; - do { - ext = (_z_msg_ext_t *)z_malloc(sizeof(_z_msg_ext_t)); - ret |= _z_msg_ext_decode(ext, zbf); - _z_msg_ext_vec_append(v_ext, ext); - } while (_Z_EXT_HAS_FLAG(header, _Z_MSG_EXT_FLAG_Z) == true); + ret |= _z_msg_ext_vec_decode(v_ext, zbf); } else { - *v_ext = _z_msg_ext_vec_make(0); + _z_msg_ext_vec_reset(v_ext); } return ret; @@ -1929,15 +1918,7 @@ int8_t _z_scouting_message_encode(_z_wbuf_t *wbf, const _z_scouting_message_t *m } break; } - if (n_ext > 0) { - for (size_t i = 0; (i + 1) < n_ext; i++) { - _z_msg_ext_t *ext = _z_msg_ext_vec_get(&msg->_extensions, i); - _Z_EXT_SET_FLAG(ext->_header, _Z_MSG_EXT_FLAG_Z); - ret |= _z_msg_ext_encode(wbf, ext); - } - ret |= - _z_msg_ext_encode(wbf, _z_msg_ext_vec_get(&msg->_extensions, n_ext - 1)); // Last extension wo\ next flag - } + ret |= _z_msg_ext_vec_encode(wbf, &msg->_extensions); return ret; } @@ -1975,16 +1956,10 @@ int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *zbf) } while ((ret == _Z_RES_OK) && (is_last == false)); if (ret == _Z_RES_OK) { - if ((msg->_header & 0x80) == 0x80) { - msg->_extensions = _z_msg_ext_vec_make(10); - _z_msg_ext_t *ext = NULL; - do { - ext = (_z_msg_ext_t *)z_malloc(sizeof(_z_msg_ext_t)); - ret |= _z_msg_ext_decode(ext, zbf); - _z_msg_ext_vec_append(&msg->_extensions, ext); - } while ((ext->_header & 0x80) == 0x80); + if ((msg->_header & _Z_MSG_EXT_FLAG_Z) != 0) { + ret |= _z_msg_ext_vec_decode(&msg->_extensions, zbf); } else { - msg->_extensions = _z_msg_ext_vec_make(0); + _z_msg_ext_vec_reset(&msg->_extensions); } } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 4e7549f28..23d8f876b 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -497,14 +497,15 @@ void message_extension(void) { printf("\n"); // Encode - int8_t res = _z_msg_ext_encode(&wbf, &e_me); + int8_t res = _z_msg_ext_encode(&wbf, &e_me, false); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); _z_msg_ext_t d_me; - res = _z_msg_ext_decode(&d_me, &zbf); + _Bool has_next; + res = _z_msg_ext_decode(&d_me, &zbf, &has_next); assert(res == _Z_RES_OK); assert_eq_message_extension(&e_me, &d_me); From 372984510d902faa41dc2f20508922167061e0de Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 2 Jun 2023 17:54:52 +0200 Subject: [PATCH 088/181] finish zid update --- examples/unix/c11/z_scout.c | 9 ++-- include/zenoh-pico/net/primitives.h | 2 +- include/zenoh-pico/protocol/core.h | 6 ++- include/zenoh-pico/protocol/extcodec.h | 8 +++ include/zenoh-pico/protocol/msg.h | 9 ++-- include/zenoh-pico/session/utils.h | 3 +- src/api/api.c | 12 ++--- src/net/memory.c | 3 -- src/net/primitives.c | 4 +- src/net/session.c | 10 ++-- src/protocol/core.c | 39 +++++++++++++++ src/protocol/extcodec.c | 21 ++++++-- src/protocol/msg.c | 22 +++------ src/protocol/msgcodec.c | 67 +++++++++++--------------- src/session/scout.c | 6 ++- tests/z_msgcodec_test.c | 44 ++++++++++------- 16 files changed, 159 insertions(+), 106 deletions(-) create mode 100644 src/protocol/core.c diff --git a/examples/unix/c11/z_scout.c b/examples/unix/c11/z_scout.c index 84dbfe39e..185211c00 100644 --- a/examples/unix/c11/z_scout.c +++ b/examples/unix/c11/z_scout.c @@ -19,13 +19,14 @@ #include #include -void fprintzid(FILE *stream, z_bytes_t zid) { - if (zid.start == NULL) { +void fprintzid(FILE *stream, z_id_t zid) { + unsigned int zidlen = _z_id_len(zid); + if (zidlen == 0) { fprintf(stream, "None"); } else { fprintf(stream, "Some("); - for (unsigned int i = 0; i < zid.len; i++) { - fprintf(stream, "%02X", (int)zid.start[i]); + for (unsigned int i = 0; i < zidlen; i++) { + fprintf(stream, "%02X", (int)zid.id[i]); } fprintf(stream, ")"); } diff --git a/include/zenoh-pico/net/primitives.h b/include/zenoh-pico/net/primitives.h index 08a514cc8..c9fc92834 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -33,7 +33,7 @@ * locator: The locator where to scout. * timeout: The time that should be spent scouting before returnng the results. */ -void _z_scout(const z_what_t what, const _z_bytes_t zid, const char *locator, const uint32_t timeout, +void _z_scout(const z_what_t what, const _z_id_t zid, const char *locator, const uint32_t timeout, _z_hello_handler_t callback, void *arg_call, _z_drop_handler_t dropper, void *arg_drop); /*------------------ Declarations ------------------*/ diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 0e94909fe..ba4d0b315 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -15,6 +15,7 @@ #ifndef ZENOH_PICO_PROTOCOL_CORE_H #define ZENOH_PICO_PROTOCOL_CORE_H +#include #include #include "zenoh-pico/api/constants.h" @@ -45,6 +46,9 @@ typedef size_t _z_zint_t; typedef struct { uint8_t id[16]; } _z_id_t; +uint8_t _z_id_len(_z_id_t id); +_Bool _z_id_check(_z_id_t id); +_z_id_t _z_id_empty(); /** * A zenoh encoding. @@ -119,7 +123,7 @@ void _z_value_free(_z_value_t **hello); * z_whatami_t whatami: The kind of zenoh entity. */ typedef struct { - _z_bytes_t zid; + _z_id_t zid; _z_str_array_t locators; z_whatami_t whatami; uint8_t version; diff --git a/include/zenoh-pico/protocol/extcodec.h b/include/zenoh-pico/protocol/extcodec.h index 0ec1d49b3..1e5277954 100644 --- a/include/zenoh-pico/protocol/extcodec.h +++ b/include/zenoh-pico/protocol/extcodec.h @@ -25,6 +25,14 @@ int8_t _z_msg_ext_decode(_z_msg_ext_t *ext, _z_zbuf_t *zbf, _Bool *has_next); int8_t _z_msg_ext_decode_na(_z_msg_ext_t *ext, _z_zbuf_t *zbf, _Bool *has_next); int8_t _z_msg_ext_vec_encode(_z_wbuf_t *wbf, const _z_msg_ext_vec_t *extensions); int8_t _z_msg_ext_vec_decode(_z_msg_ext_vec_t *extensions, _z_zbuf_t *zbf); +/** + * Iterates through the extensions in `zbf`, assuming at least one is present at its begining + * (calling this function otherwise is UB). Short-circuits if `callback` returns a non-zero value. + * + * `callback` will receive `context` as its second argument, and may "steal" its first argument by + * copying its value and setting it to `_z_msg_ext_make_unit(0)`. + */ +int8_t _z_msg_ext_decode_iter(_z_zbuf_t *zbf, int8_t (*callback)(_z_msg_ext_t *, void *), void *context); #endif /* ZENOH_PICO_MSGEXTCODEC_H */ diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 94f08ba8f..9a294258b 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -829,7 +829,7 @@ _z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid); // - 0b100: Client // typedef struct { - _z_bytes_t _zid; + _z_id_t _zid; z_what_t _what; uint8_t _version; } _z_s_msg_scout_t; @@ -875,7 +875,7 @@ void _z_s_msg_clear_scout(_z_s_msg_scout_t *msg); // - 0b11: Reserved // typedef struct { - _z_bytes_t _zid; + _z_id_t _zid; _z_locator_array_t _locators; z_whatami_t _whatami; uint8_t _version; @@ -1227,13 +1227,12 @@ typedef union { typedef struct { _z_scouting_body_t _body; _z_attachment_t *_attachment; - _z_msg_ext_vec_t _extensions; uint8_t _header; } _z_scouting_message_t; void _z_s_msg_clear(_z_scouting_message_t *msg); -_z_scouting_message_t _z_s_msg_make_scout(z_what_t what, _z_bytes_t zid); -_z_scouting_message_t _z_s_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _z_locator_array_t locators); +_z_scouting_message_t _z_s_msg_make_scout(z_what_t what, _z_id_t zid); +_z_scouting_message_t _z_s_msg_make_hello(z_whatami_t whatami, _z_id_t zid, _z_locator_array_t locators); void _z_s_msg_copy(_z_scouting_message_t *clone, _z_scouting_message_t *msg); void _z_s_msg_copy_scout(_z_s_msg_scout_t *clone, _z_s_msg_scout_t *msg); diff --git a/include/zenoh-pico/session/utils.h b/include/zenoh-pico/session/utils.h index 615ac1e22..02a695605 100644 --- a/include/zenoh-pico/session/utils.h +++ b/include/zenoh-pico/session/utils.h @@ -19,9 +19,10 @@ #include "zenoh-pico/api/constants.h" #include "zenoh-pico/net/session.h" +#include "zenoh-pico/protocol/core.h" /*------------------ Session ------------------*/ -_z_hello_list_t *_z_scout_inner(const z_what_t what, const char *locator, const uint32_t timeout, +_z_hello_list_t *_z_scout_inner(const z_what_t what, _z_id_t id, const char *locator, const uint32_t timeout, const _Bool exit_on_first); int8_t _z_session_init(_z_session_t *zn, _z_bytes_t *zid); diff --git a/src/api/api.c b/src/api/api.c index 61597d4c1..2a8229adf 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -26,12 +26,13 @@ #include "zenoh-pico/net/primitives.h" #include "zenoh-pico/net/resource.h" #include "zenoh-pico/net/session.h" +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/session/queryable.h" #include "zenoh-pico/session/resource.h" #include "zenoh-pico/session/utils.h" -#include "zenoh-pico/utils/uuid.h" #include "zenoh-pico/utils/result.h" +#include "zenoh-pico/utils/uuid.h" /********* Data Types Handlers *********/ _Bool z_bytes_check(const z_bytes_t *v) { return v->start != NULL; } @@ -512,13 +513,10 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb } uint32_t timeout = strtoul(opt_as_str, NULL, 10); - _z_bytes_t zid; + _z_id_t zid = _z_id_empty(); char *zid_str = _z_config_get(config->_value, Z_CONFIG_SESSION_ZID_KEY); if (zid_str != NULL) { - zid = _z_bytes_make(16); - _z_uuid_to_bytes((uint8_t *)zid.start, zid_str); - } else { - zid = _z_bytes_empty(); + _z_uuid_to_bytes(zid.id, zid_str); } _z_scout(what, zid, mcast_locator, timeout, __z_hello_handler, wrapped_ctx, callback->drop, ctx); @@ -685,7 +683,7 @@ void __z_reply_handler(_z_reply_t *reply, __z_reply_handler_wrapper_t *wrapped_c z_owned_reply_t oreply = {._value = reply}; wrapped_ctx->user_call(&oreply, wrapped_ctx->ctx); - z_reply_drop(&oreply); // user_call is allowed to take ownership of the reply by setting oreply._value to NULL + z_reply_drop(&oreply); // user_call is allowed to take ownership of the reply by setting oreply._value to NULL } int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owned_closure_reply_t *callback, diff --git a/src/net/memory.c b/src/net/memory.c index b94083ff9..67fedfc2e 100644 --- a/src/net/memory.c +++ b/src/net/memory.c @@ -48,9 +48,6 @@ void _z_sample_free(_z_sample_t **sample) { } void _z_hello_clear(_z_hello_t *hello) { - if (hello->zid.len > 0) { - _z_bytes_clear(&hello->zid); - } if (hello->locators.len > 0) { _z_str_array_clear(&hello->locators); } diff --git a/src/net/primitives.c b/src/net/primitives.c index 83c04ad53..66004d819 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -26,9 +26,9 @@ #include "zenoh-pico/session/utils.h" /*------------------ Scouting ------------------*/ -void _z_scout(const z_what_t what, const _z_bytes_t zid, const char *locator, const uint32_t timeout, +void _z_scout(const z_what_t what, const _z_id_t zid, const char *locator, const uint32_t timeout, _z_hello_handler_t callback, void *arg_call, _z_drop_handler_t dropper, void *arg_drop) { - _z_hello_list_t *hellos = _z_scout_inner(what, locator, timeout, false); + _z_hello_list_t *hellos = _z_scout_inner(what, zid, locator, timeout, false); while (hellos != NULL) { _z_hello_t *hello = NULL; diff --git a/src/net/session.c b/src/net/session.c index 7247fffb7..4f7abd212 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -18,6 +18,7 @@ #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/net/memory.h" +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/session/utils.h" #include "zenoh-pico/transport/link/task/join.h" #include "zenoh-pico/transport/link/task/lease.h" @@ -53,13 +54,10 @@ int8_t __z_open_inner(_z_session_t *zn, char *locator, z_whatami_t mode) { int8_t _z_open(_z_session_t *zn, _z_config_t *config) { int8_t ret = _Z_RES_OK; - _z_bytes_t zid; + _z_id_t zid = _z_id_empty(); char *opt_as_str = _z_config_get(config, Z_CONFIG_SESSION_ZID_KEY); if (opt_as_str != NULL) { - zid = _z_bytes_make(16); - _z_uuid_to_bytes((uint8_t *)zid.start, opt_as_str); - } else { - zid = _z_bytes_empty(); + _z_uuid_to_bytes(zid.id, opt_as_str); } if (config != NULL) { @@ -84,7 +82,7 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { uint32_t timeout = strtoul(opt_as_str, NULL, 10); // Scout and return upon the first result - _z_hello_list_t *hellos = _z_scout_inner(what, mcast_locator, timeout, true); + _z_hello_list_t *hellos = _z_scout_inner(what, zid, mcast_locator, timeout, true); if (hellos != NULL) { _z_hello_t *hello = _z_hello_list_head(hellos); _z_str_array_copy(&locators, &hello->locators); diff --git a/src/protocol/core.c b/src/protocol/core.c new file mode 100644 index 000000000..d014d7473 --- /dev/null +++ b/src/protocol/core.c @@ -0,0 +1,39 @@ +#include "zenoh-pico/protocol/core.h" + +uint8_t _z_id_len(_z_id_t id) { + uint8_t len = 16; + while (len > 0) { + --len; + if (id.id[len] != 0) { + break; + } + } + return len; +} +_Bool _z_id_check(_z_id_t id) { + _Bool ret = false; + for (int i = 0; !ret && i < 16; i++) { + ret |= id.id[i] != 0; + } + return ret; +} +_z_id_t _z_id_empty() { + return (_z_id_t){.id = { + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + }}; +} \ No newline at end of file diff --git a/src/protocol/extcodec.c b/src/protocol/extcodec.c index ceaa34521..9bffebe87 100644 --- a/src/protocol/extcodec.c +++ b/src/protocol/extcodec.c @@ -151,14 +151,29 @@ int8_t _z_msg_ext_vec_encode(_z_wbuf_t *wbf, const _z_msg_ext_vec_t *extensions) } return ret; } +int8_t _z_msg_ext_vec_push_callback(_z_msg_ext_t *extension, _z_msg_ext_vec_t *extensions) { + _z_msg_ext_t *ext = (_z_msg_ext_t *)z_malloc(sizeof(_z_msg_ext_t)); + *ext = *extension; + *extension = _z_msg_ext_make_unit(0); + _z_msg_ext_vec_append(extensions, extension); + return 0; +} int8_t _z_msg_ext_vec_decode(_z_msg_ext_vec_t *extensions, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; _z_msg_ext_vec_reset(extensions); + return _z_msg_ext_decode_iter(zbf, (int8_t(*)(_z_msg_ext_t *, void *))_z_msg_ext_vec_push_callback, + (void *)extensions); +} +int8_t _z_msg_ext_decode_iter(_z_zbuf_t *zbf, int8_t (*callback)(_z_msg_ext_t *, void *), void *context) { + int8_t ret = _Z_RES_OK; _Bool has_next = true; while (has_next && ret == _Z_RES_OK) { - _z_msg_ext_t *ext = (_z_msg_ext_t *)z_malloc(sizeof(_z_msg_ext_t)); - ret |= _z_msg_ext_decode(ext, zbf, &has_next); - _z_msg_ext_vec_append(extensions, ext); + _z_msg_ext_t ext = _z_msg_ext_make_unit(0); + ret |= _z_msg_ext_decode(&ext, zbf, &has_next); + if (ret == _Z_RES_OK) { + ret |= callback(&ext, context); + _z_msg_ext_clear(&ext); + } } return ret; } \ No newline at end of file diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 15a48ef8f..f0b2e679f 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -15,6 +15,7 @@ #include "zenoh-pico/protocol/msg.h" #include +#include #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/protocol/core.h" @@ -586,7 +587,7 @@ void _z_n_msg_free(_z_network_message_t **msg) { /* Transport Messages */ /*=============================*/ /*------------------ Scout Message ------------------*/ -_z_scouting_message_t _z_s_msg_make_scout(z_what_t what, _z_bytes_t zid) { +_z_scouting_message_t _z_s_msg_make_scout(z_what_t what, _z_id_t zid) { _z_scouting_message_t msg; msg._header = _Z_MID_SCOUT; @@ -595,7 +596,6 @@ _z_scouting_message_t _z_s_msg_make_scout(z_what_t what, _z_bytes_t zid) { msg._body._scout._zid = zid; msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); return msg; } @@ -603,13 +603,13 @@ _z_scouting_message_t _z_s_msg_make_scout(z_what_t what, _z_bytes_t zid) { void _z_s_msg_copy_scout(_z_s_msg_scout_t *clone, _z_s_msg_scout_t *msg) { clone->_what = msg->_what; clone->_version = msg->_version; - _z_bytes_copy(&clone->_zid, &msg->_zid); + memcpy(clone->_zid.id, msg->_zid.id, 16); } -void _z_s_msg_clear_scout(_z_s_msg_scout_t *msg) { _z_bytes_clear(&msg->_zid); } +void _z_s_msg_clear_scout(_z_s_msg_scout_t *msg) {} /*------------------ Hello Message ------------------*/ -_z_scouting_message_t _z_s_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _z_locator_array_t locators) { +_z_scouting_message_t _z_s_msg_make_hello(z_whatami_t whatami, _z_id_t zid, _z_locator_array_t locators) { _z_scouting_message_t msg; msg._header = _Z_MID_HELLO; @@ -623,21 +623,16 @@ _z_scouting_message_t _z_s_msg_make_hello(z_whatami_t whatami, _z_bytes_t zid, _ } msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - return msg; } void _z_s_msg_copy_hello(_z_s_msg_hello_t *clone, _z_s_msg_hello_t *msg) { _z_locator_array_copy(&clone->_locators, &msg->_locators); - _z_bytes_copy(&clone->_zid, &msg->_zid); + memcpy(clone->_zid.id, msg->_zid.id, 16); clone->_whatami = msg->_whatami; } -void _z_s_msg_clear_hello(_z_s_msg_hello_t *msg) { - _z_bytes_clear(&msg->_zid); - _z_locators_clear(&msg->_locators); -} +void _z_s_msg_clear_hello(_z_s_msg_hello_t *msg) { _z_locators_clear(&msg->_locators); } /*------------------ Join Message ------------------*/ _z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_bytes_t zid, @@ -998,7 +993,6 @@ void _z_t_msg_clear(_z_transport_message_t *msg) { void _z_s_msg_copy(_z_scouting_message_t *clone, _z_scouting_message_t *msg) { clone->_header = msg->_header; clone->_attachment = msg->_attachment; - _z_msg_ext_vec_copy(&clone->_extensions, &msg->_extensions); uint8_t mid = _Z_MID(msg->_header); switch (mid) { @@ -1036,6 +1030,4 @@ void _z_s_msg_clear(_z_scouting_message_t *msg) { _Z_DEBUG("WARNING: Trying to clear session message with unknown ID(%d)\n", mid); } break; } - - _z_msg_ext_vec_clear(&msg->_extensions); } diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 59348ff66..f38396443 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -19,6 +19,7 @@ #include #include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/link/endpoint.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/extcodec.h" @@ -45,15 +46,10 @@ int8_t _z_payload_decode_na(_z_payload_t *pld, _z_zbuf_t *zbf) { int8_t _z_payload_decode(_z_payload_t *pld, _z_zbuf_t *zbf) { return _z_payload_decode_na(pld, zbf); } -int8_t _z_id_encode(_z_wbuf_t *wbf, const _z_id_t *id) { - int len; +int8_t _z_id_encode_as_zbytes(_z_wbuf_t *wbf, const _z_id_t *id) { int8_t ret = _Z_RES_OK; - for (len = 15; len > 0; len--) { - if (id->id[len]) { - break; - } - } - len++; // `len` is treated as an "end" until this point, and as a length from then on + uint8_t len = _z_id_len(*id); + if (id->id[len] != 0) { ret |= _z_wbuf_write(wbf, len); ret |= _z_wbuf_write_bytes(wbf, id->id, 0, len); @@ -68,7 +64,7 @@ int8_t _z_id_encode(_z_wbuf_t *wbf, const _z_id_t *id) { /// /// Note that while `_z_id_t` has an error state (full 0s), this function doesn't /// guarantee that this state will be set in case of errors. -int8_t _z_id_decode(_z_id_t *id, _z_zbuf_t *zbf) { +int8_t _z_id_decode_as_zbytes(_z_id_t *id, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; uint8_t len = _z_zbuf_read(zbf); _z_zbuf_read_bytes(zbf, id->id, 0, len); @@ -82,7 +78,7 @@ int8_t _z_timestamp_encode(_z_wbuf_t *wbf, const _z_timestamp_t *ts) { _Z_DEBUG("Encoding _TIMESTAMP\n"); _Z_EC(_z_uint64_encode(wbf, ts->time)) - ret |= _z_id_encode(wbf, &ts->id); + ret |= _z_id_encode_as_zbytes(wbf, &ts->id); return ret; } @@ -92,7 +88,7 @@ int8_t _z_timestamp_decode_na(_z_timestamp_t *ts, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; ret |= _z_uint64_decode(&ts->time, zbf); - ret |= _z_id_decode(&ts->id, zbf); + ret |= _z_id_decode_as_zbytes(&ts->id, zbf); return ret; } @@ -1793,14 +1789,14 @@ int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_scout_t *m uint8_t cbyte = 0; cbyte |= (msg->_what & 0x07); - uint8_t zid_len = msg->_zid.len; + uint8_t zid_len = _z_id_len(msg->_zid); if (zid_len > 0) { // TODO[protocol]: check if ZID > 0 && <= 16 _Z_SET_FLAG(cbyte, _Z_FLAG_T_SCOUT_I); cbyte |= ((zid_len - 1) & 0x0F) << 4; } _Z_EC(_z_uint8_encode(wbf, cbyte)) - _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) + ret |= _z_wbuf_write_bytes(wbf, msg->_zid.id, 0, zid_len); return ret; } @@ -1816,13 +1812,11 @@ int8_t _z_scout_decode_na(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) ret |= _z_uint8_decode(&cbyte, zbf); msg->_what = cbyte & 0x07; if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(cbyte, _Z_FLAG_T_SCOUT_I) == true)) { - msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; - ret |= _z_bytes_val_decode(&msg->_zid, zbf); - if (ret != _Z_RES_OK) { - msg->_zid = _z_bytes_empty(); - } + uint8_t len = ((cbyte & 0xF0) >> 4) + 1; + msg->_zid = _z_id_empty(); + _z_zbuf_read_bytes(zbf, msg->_zid.id, 0, len); } else { - msg->_zid = _z_bytes_empty(); + msg->_zid = _z_id_empty(); } return ret; @@ -1838,12 +1832,12 @@ int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *m _Z_DEBUG("Encoding _Z_MID_HELLO\n"); _Z_EC(_z_uint8_encode(wbf, msg->_version)) - + uint8_t zidlen = _z_id_len(msg->_zid); uint8_t cbyte = 0; cbyte |= (msg->_whatami & 0x03); - cbyte |= ((msg->_zid.len - 1) & 0x0F) << 4; // TODO[protocol]: check if ZID > 0 && <= 16 + cbyte |= ((zidlen - 1) & 0x0F) << 4; // TODO[protocol]: check if ZID > 0 && <= 16 _Z_EC(_z_uint8_encode(wbf, cbyte)) - _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) + _Z_EC(_z_bytes_val_encode(wbf, &(_z_bytes_t){.start = msg->_zid.id, .len = zidlen, ._is_alloc = false})); if (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true) { _Z_EC(_z_locators_encode(wbf, &msg->_locators)) @@ -1861,24 +1855,22 @@ int8_t _z_hello_decode_na(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) uint8_t cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); msg->_whatami = cbyte & 0x03; - msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; + uint8_t zidlen = ((cbyte & 0xF0) >> 4) + 1; if (ret == _Z_RES_OK) { - ret |= _z_bytes_val_decode(&msg->_zid, zbf); - if (ret != _Z_RES_OK) { - msg->_zid = _z_bytes_empty(); - } + msg->_zid = _z_id_empty(); + _z_zbuf_read_bytes(zbf, msg->_zid.id, 0, zidlen); } else { - msg->_zid = _z_bytes_empty(); + msg->_zid = _z_id_empty(); } if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true)) { ret |= _z_locators_decode(&msg->_locators, zbf); if (ret != _Z_RES_OK) { - msg->_locators = _z_locator_array_make(0); + msg->_locators = _z_locator_array_empty(); } } else { - msg->_locators = _z_locator_array_make(0); + msg->_locators = _z_locator_array_empty(); } return ret; @@ -1897,10 +1889,10 @@ int8_t _z_scouting_message_encode(_z_wbuf_t *wbf, const _z_scouting_message_t *m } uint8_t header = msg->_header; - size_t n_ext = _z_msg_ext_vec_len(&msg->_extensions); - if (n_ext > 0) { - header |= _Z_FLAG_T_Z; - } + // size_t n_ext = _z_msg_ext_vec_len(&msg->_extensions); + // if (n_ext > 0) { + // header |= _Z_FLAG_T_Z; + // } _Z_EC(_z_wbuf_write(wbf, header)) switch (_Z_MID(msg->_header)) { @@ -1918,11 +1910,12 @@ int8_t _z_scouting_message_encode(_z_wbuf_t *wbf, const _z_scouting_message_t *m } break; } - ret |= _z_msg_ext_vec_encode(wbf, &msg->_extensions); + // ret |= _z_msg_ext_vec_encode(wbf, &msg->_extensions); return ret; } +int8_t _z_scouting_decode_handle_ext(_z_msg_ext_t *extension, void *extensions) { return 0; } int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; @@ -1957,9 +1950,7 @@ int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *zbf) if (ret == _Z_RES_OK) { if ((msg->_header & _Z_MSG_EXT_FLAG_Z) != 0) { - ret |= _z_msg_ext_vec_decode(&msg->_extensions, zbf); - } else { - _z_msg_ext_vec_reset(&msg->_extensions); + ret |= _z_msg_ext_decode_iter(zbf, _z_scouting_decode_handle_ext, NULL); } } diff --git a/src/session/scout.c b/src/session/scout.c index 2a872eef0..33d3afae4 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -13,8 +13,10 @@ // #include +#include #include "zenoh-pico/link/manager.h" +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/utils/logging.h" @@ -77,7 +79,7 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig if (hello != NULL) { hello->version = s_msg._body._hello._version; hello->whatami = s_msg._body._hello._whatami; - _z_bytes_copy(&hello->zid, &s_msg._body._hello._zid); + memcpy(hello->zid.id, s_msg._body._hello._zid.id, 16); size_t n_loc = _z_locator_array_len(&s_msg._body._hello._locators); if (n_loc > 0) { @@ -125,7 +127,7 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig return ret; } -_z_hello_list_t *_z_scout_inner(const z_what_t what, const _z_bytes_t zid, const char *locator, const uint32_t timeout, +_z_hello_list_t *_z_scout_inner(const z_what_t what, _z_id_t zid, const char *locator, const uint32_t timeout, const _Bool exit_on_first) { _z_hello_list_t *ret = NULL; diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 23d8f876b..b9cbf5446 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -12,7 +12,10 @@ // ZettaScale Zenoh Team, // +#include #include + +#include "zenoh-pico/protocol/core.h" #define ZENOH_PICO_TEST_H #include @@ -497,7 +500,7 @@ void message_extension(void) { printf("\n"); // Encode - int8_t res = _z_msg_ext_encode(&wbf, &e_me, false); + int8_t res = _z_msg_ext_encode(&wbf, &e_me, true); assert(res == _Z_RES_OK); (void)(res); @@ -2111,12 +2114,14 @@ void response_final_message(void) { /*------------------ Scout Message ------------------*/ _z_scouting_message_t gen_scout_message(void) { z_what_t what = gen_uint8() % 7; - _z_bytes_t zid; + _z_bytes_t zidbytes; if (gen_bool()) { - zid = gen_bytes((gen_uint8() % 16) + 1); + zidbytes = gen_bytes((gen_uint8() % 16) + 1); } else { - zid = _z_bytes_empty(); + zidbytes = _z_bytes_empty(); } + _z_id_t zid = _z_id_empty(); + memcpy(zid.id, zidbytes.start, zidbytes.len); return _z_s_msg_make_scout(what, zid); } @@ -2127,7 +2132,7 @@ void assert_eq_scout_message(_z_s_msg_scout_t *left, _z_s_msg_scout_t *right, ui printf(" What (%u:%u)\n", left->_what, right->_what); assert(left->_what == right->_what); printf(" "); - assert_eq_uint8_array(&left->_zid, &right->_zid); + assert(memcmp(left->_zid.id, right->_zid.id, 16) == 0); printf("\n"); } @@ -2162,7 +2167,10 @@ void scout_message(void) { /*------------------ Hello Message ------------------*/ _z_scouting_message_t gen_hello_message(void) { z_whatami_t whatami = (gen_uint8() % 2) + 1; - _z_bytes_t zid = gen_bytes((gen_uint8() % 16) + 1); + _z_bytes_t zidbytes = gen_bytes((gen_uint8() % 16) + 1); + _z_id_t zid = _z_id_empty(); + memcpy(zid.id, zidbytes.start, zidbytes.len); + _z_bytes_clear(&zidbytes); _z_locator_array_t locators; if (gen_bool() == true) @@ -2183,7 +2191,7 @@ void assert_eq_hello_message(_z_s_msg_hello_t *left, _z_s_msg_hello_t *right, ui printf("\n"); printf(" "); - assert_eq_uint8_array(&left->_zid, &right->_zid); + assert(memcmp(left->_zid.id, right->_zid.id, 16) == 0); printf("\n"); if (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true) { @@ -2881,9 +2889,9 @@ _z_scouting_message_t gen_scouting_message(void) { void assert_eq_scouting_message(_z_scouting_message_t *left, _z_scouting_message_t *right) { // FIXME[protocol]: This is here to set the extensions flags that is only known at encoding time - if (_z_msg_ext_vec_len(&left->_extensions) > (size_t)0) { - left->_header |= _Z_FLAG_T_Z; - } + // if (_z_msg_ext_vec_len(&left->_extensions) > (size_t)0) { + // left->_header |= _Z_FLAG_T_Z; + // } // Test message decorators if (left->_attachment && right->_attachment) { @@ -2911,14 +2919,14 @@ void assert_eq_scouting_message(_z_scouting_message_t *left, _z_scouting_message break; } - size_t left_n_ext = _z_msg_ext_vec_len(&left->_extensions); - size_t right_n_ext = _z_msg_ext_vec_len(&right->_extensions); - printf(" # of extensions (%zu:%zu)", left_n_ext, right_n_ext); - assert(left_n_ext == right_n_ext); - for (size_t i = 0; i < left_n_ext; i++) { - assert_eq_message_extension(_z_msg_ext_vec_get(&left->_extensions, i), - _z_msg_ext_vec_get(&right->_extensions, i)); - } + // size_t left_n_ext = _z_msg_ext_vec_len(&left->_extensions); + // size_t right_n_ext = _z_msg_ext_vec_len(&right->_extensions); + // printf(" # of extensions (%zu:%zu)", left_n_ext, right_n_ext); + // assert(left_n_ext == right_n_ext); + // for (size_t i = 0; i < left_n_ext; i++) { + // assert_eq_message_extension(_z_msg_ext_vec_get(&left->_extensions, i), + // _z_msg_ext_vec_get(&right->_extensions, i)); + // } } void scouting_message(void) { From 830c9ac4c43b7e44ec1d33557b14613672ad47cd Mon Sep 17 00:00:00 2001 From: Steven Palma Date: Tue, 6 Jun 2023 11:57:51 +0200 Subject: [PATCH 089/181] Bump version to 0.7.2.0 (#219) * Bump version to 0.7.2.1 * Changed version number --- CMakeLists.txt | 2 +- docs/conf.py | 2 +- include/zenoh-pico.h | 5 +++-- library.json | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c8151bb9..8e917defd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ # cmake_minimum_required(VERSION 3.8) -project(libzenohpico VERSION 0.7.0.1 LANGUAGES C) +project(libzenohpico VERSION 0.7.2.0 LANGUAGES C) option(BUILD_SHARED_LIBS "Build shared libraries if ON, otherwise build static libraries" ON) option(ZENOH_DEBUG "Use this to set the ZENOH_DEBUG variable." 0) diff --git a/docs/conf.py b/docs/conf.py index 3891a7f28..0ccb067bf 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -21,7 +21,7 @@ project = 'zenoh-pico' copyright = '2017, 2022 ZettaScale Technology Inc' author = 'ZettaScale Zenoh team' -release = '0.7.0-rc' +release = '0.7.2.0' # -- General configuration --------------------------------------------------- master_doc = 'index' diff --git a/include/zenoh-pico.h b/include/zenoh-pico.h index 8056e3008..67bf4c9e7 100644 --- a/include/zenoh-pico.h +++ b/include/zenoh-pico.h @@ -15,10 +15,11 @@ #ifndef ZENOH_PICO_H #define ZENOH_PICO_H -#define ZENOH_PICO "0.7.0" +#define ZENOH_PICO "0.7.2.0" #define ZENOH_PICO_MAJOR 0 #define ZENOH_PICO_MINOR 7 -#define ZENOH_PICO_PATCH 0 +#define ZENOH_PICO_PATCH 2 +#define ZENOH_PICO_TWEAK 0 #include "zenoh-pico/api/constants.h" #include "zenoh-pico/api/macros.h" diff --git a/library.json b/library.json index 6d5944a5f..81c1314db 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "zenoh-pico", - "version": "0.7.0-rc", + "version": "0.7.2.0", "description": "The Eclipse Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. It unifies data in motion, data in-use, data at rest and computations. It carefully blends traditional pub/sub with geo-distributed storages, queries and computations, while retaining a level of time and space efficiency that is well beyond any of the mainstream stacks. Zenoh-Pico is the implementation able to scale down to extremely constrainded devices and networks.", "keywords": [ "pubsub", From a54dfcfc894cdc8b492cb7146507945d7b0cb102 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Tue, 6 Jun 2023 17:00:20 +0200 Subject: [PATCH 090/181] more zids that aren't zbytes anymore --- include/zenoh-pico/net/session.h | 2 +- include/zenoh-pico/protocol/codec.h | 6 +-- include/zenoh-pico/protocol/core.h | 2 +- include/zenoh-pico/protocol/extcodec.h | 6 +++ include/zenoh-pico/protocol/msg.h | 20 +++---- include/zenoh-pico/session/session.h | 4 +- include/zenoh-pico/session/utils.h | 2 +- include/zenoh-pico/transport/manager.h | 2 +- include/zenoh-pico/transport/transport.h | 14 ++--- src/api/api.c | 39 ++------------ src/net/memory.c | 4 +- src/net/primitives.c | 3 +- src/net/session.c | 17 +++--- src/protocol/extcodec.c | 9 ++++ src/protocol/msg.c | 25 ++++----- src/protocol/msgcodec.c | 66 ++++++++++-------------- src/session/query.c | 2 +- src/session/queryable.c | 6 +-- src/session/utils.c | 18 ++----- src/transport/manager.c | 6 +-- src/transport/multicast/link/rx.c | 2 +- src/transport/multicast/link/task/join.c | 3 +- src/transport/peer_entry.c | 11 ++-- src/transport/transport.c | 21 ++++---- tests/z_msgcodec_test.c | 46 +++++++++-------- 25 files changed, 149 insertions(+), 187 deletions(-) diff --git a/include/zenoh-pico/net/session.h b/include/zenoh-pico/net/session.h index 76e628692..c92f7a674 100644 --- a/include/zenoh-pico/net/session.h +++ b/include/zenoh-pico/net/session.h @@ -31,7 +31,7 @@ typedef struct { _z_transport_t _tp; // Zenoh PID - _z_bytes_t _local_zid; + _z_id_t _local_zid; // Session counters _z_zint_t _resource_id; diff --git a/include/zenoh-pico/protocol/codec.h b/include/zenoh-pico/protocol/codec.h index 1947b5c1a..91cf4f825 100644 --- a/include/zenoh-pico/protocol/codec.h +++ b/include/zenoh-pico/protocol/codec.h @@ -22,9 +22,9 @@ #include "zenoh-pico/utils/config.h" #include "zenoh-pico/utils/result.h" -#define _Z_EC(fn) \ - if (fn != _Z_RES_OK) { \ - return -1; \ +#define _Z_EC(fn) \ + if ((fn) != _Z_RES_OK) { \ + return -1; \ } /*------------------ Internal Zenoh-net Macros ------------------*/ diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index ba4d0b315..e6e781ef6 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -48,7 +48,7 @@ typedef struct { } _z_id_t; uint8_t _z_id_len(_z_id_t id); _Bool _z_id_check(_z_id_t id); -_z_id_t _z_id_empty(); +_z_id_t _z_id_empty(void); /** * A zenoh encoding. diff --git a/include/zenoh-pico/protocol/extcodec.h b/include/zenoh-pico/protocol/extcodec.h index 1e5277954..f140b82f7 100644 --- a/include/zenoh-pico/protocol/extcodec.h +++ b/include/zenoh-pico/protocol/extcodec.h @@ -33,6 +33,12 @@ int8_t _z_msg_ext_vec_decode(_z_msg_ext_vec_t *extensions, _z_zbuf_t *zbf); * copying its value and setting it to `_z_msg_ext_make_unit(0)`. */ int8_t _z_msg_ext_decode_iter(_z_zbuf_t *zbf, int8_t (*callback)(_z_msg_ext_t *, void *), void *context); +/** + * Iterates through the extensions in `zbf`, assuming at least one is present at its begining. + * Returns `_Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN` if a mandatory extension is found, + * `_Z_RES_OK` otherwise. + */ +int8_t _z_msg_ext_skip_non_mandatories(_z_zbuf_t *zbf); #endif /* ZENOH_PICO_MSGEXTCODEC_H */ diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 9a294258b..51c0c09d8 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -189,9 +189,9 @@ /*=============================*/ /* Message helpers */ /*=============================*/ -#define _Z_MID(h) (_Z_MID_MASK & h) -#define _Z_FLAGS(h) (_Z_FLAGS_MASK & h) -#define _Z_HAS_FLAG(h, f) ((h & f) != 0) +#define _Z_MID(h) (_Z_MID_MASK & (h)) +#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) /*=============================*/ @@ -296,7 +296,7 @@ void _z_t_msg_clear_attachment(_z_attachment_t *a); // - if F==1 then the message is a REPLY_FINAL // typedef struct { - _z_bytes_t _replier_id; + _z_id_t _replier_id; _z_zint_t _qid; uint8_t _header; } _z_reply_context_t; @@ -617,7 +617,7 @@ _Z_ELEM_DEFINE(_z_zenoh_message, _z_zenoh_message_t, _z_noop_size, _z_msg_clear, _Z_VEC_DEFINE(_z_zenoh_message, _z_zenoh_message_t) /*------------------ Builders ------------------*/ -_z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_bytes_t replier_id, _Bool is_final); +_z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, _Bool is_final); _z_declaration_t _z_msg_make_declaration_resource(_z_zint_t id, _z_keyexpr_t key); _z_declaration_t _z_msg_make_declaration_forget_resource(_z_zint_t rid); _z_declaration_t _z_msg_make_declaration_publisher(_z_keyexpr_t key); @@ -931,7 +931,7 @@ typedef struct { _Bool _is_qos; } _z_conduit_sn_list_t; typedef struct { - _z_bytes_t _zid; + _z_id_t _zid; _z_zint_t _lease; uint16_t _batch_size; _z_conduit_sn_list_t _next_sn; @@ -1013,7 +1013,7 @@ void _z_t_msg_clear_join(_z_t_msg_join_t *msg); // ($) Batch Size. It indicates the maximum size of a batch the sender of the // typedef struct { - _z_bytes_t _zid; + _z_id_t _zid; _z_bytes_t _cookie; uint16_t _batch_size; z_whatami_t _whatami; @@ -1198,10 +1198,10 @@ typedef struct { void _z_t_msg_clear(_z_transport_message_t *msg); /*------------------ Builders ------------------*/ -_z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_bytes_t zid, +_z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_id_t zid, _z_conduit_sn_list_t next_sn); -_z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_bytes_t zid); -_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_bytes_t zid, _z_bytes_t cookie); +_z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_id_t zid); +_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, _z_bytes_t cookie); _z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie); _z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial_sn); _z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only); diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index 88ab2e631..963475b1d 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -43,7 +43,7 @@ typedef void (*_z_drop_handler_t)(void *arg); */ typedef struct { _z_sample_t sample; - _z_bytes_t replier_id; + _z_id_t replier_id; } _z_reply_data_t; void _z_reply_data_clear(_z_reply_data_t *rd); @@ -180,6 +180,6 @@ struct __z_hello_handler_wrapper_t; // Forward declaration to be used in _z_hel */ typedef void (*_z_hello_handler_t)(_z_hello_t *hello, struct __z_hello_handler_wrapper_t *arg); -int8_t _z_session_generate_zid(_z_bytes_t *bs, uint8_t size); +int8_t _z_session_generate_zid(_z_id_t *bs, uint8_t size); #endif /* ZENOH_PICO_SESSION_TYPES_H */ diff --git a/include/zenoh-pico/session/utils.h b/include/zenoh-pico/session/utils.h index 02a695605..0c625d501 100644 --- a/include/zenoh-pico/session/utils.h +++ b/include/zenoh-pico/session/utils.h @@ -25,7 +25,7 @@ _z_hello_list_t *_z_scout_inner(const z_what_t what, _z_id_t id, const char *locator, const uint32_t timeout, const _Bool exit_on_first); -int8_t _z_session_init(_z_session_t *zn, _z_bytes_t *zid); +int8_t _z_session_init(_z_session_t *zn, _z_id_t *zid); int8_t _z_session_close(_z_session_t *zn, uint8_t reason); void _z_session_clear(_z_session_t *zn); void _z_session_free(_z_session_t **zn); diff --git a/include/zenoh-pico/transport/manager.h b/include/zenoh-pico/transport/manager.h index 2d378f782..d20315876 100644 --- a/include/zenoh-pico/transport/manager.h +++ b/include/zenoh-pico/transport/manager.h @@ -19,7 +19,7 @@ #include "zenoh-pico/link/manager.h" #include "zenoh-pico/transport/transport.h" -int8_t _z_new_transport(_z_transport_t *zt, _z_bytes_t *bs, char *locator, z_whatami_t mode); +int8_t _z_new_transport(_z_transport_t *zt, _z_id_t *bs, char *locator, z_whatami_t mode); void _z_free_transport(_z_transport_t **zt); #endif /* ZENOH_PICO_TRANSPORT_MANAGER_H */ diff --git a/include/zenoh-pico/transport/transport.h b/include/zenoh-pico/transport/transport.h index 570bd1f4d..1ee42173f 100644 --- a/include/zenoh-pico/transport/transport.h +++ b/include/zenoh-pico/transport/transport.h @@ -27,7 +27,7 @@ typedef struct { _z_wbuf_t _dbuf_reliable; _z_wbuf_t _dbuf_best_effort; - _z_bytes_t _remote_zid; + _z_id_t _remote_zid; _z_bytes_t _remote_addr; // SN numbers @@ -65,7 +65,7 @@ typedef struct { _z_wbuf_t _wbuf; _z_zbuf_t _zbuf; - _z_bytes_t _remote_zid; + _z_id_t _remote_zid; // SN numbers _z_zint_t _sn_res; @@ -142,7 +142,7 @@ _Z_ELEM_DEFINE(_z_transport, _z_transport_t, _z_noop_size, _z_noop_clear, _z_noo _Z_LIST_DEFINE(_z_transport, _z_transport_t) typedef struct { - _z_bytes_t _remote_zid; + _z_id_t _remote_zid; uint16_t _batch_size; _z_zint_t _initial_sn_rx; _z_zint_t _initial_sn_tx; @@ -163,13 +163,13 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic int8_t _z_transport_multicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_multicast_establish_param_t *param); int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t *param, const _z_link_t *zl, - const _z_bytes_t *local_zid); + const _z_id_t *local_zid); int8_t _z_transport_multicast_open_client(_z_transport_multicast_establish_param_t *param, const _z_link_t *zl, - const _z_bytes_t *local_zid); + const _z_id_t *local_zid); int8_t _z_transport_unicast_open_peer(_z_transport_unicast_establish_param_t *param, const _z_link_t *zl, - const _z_bytes_t *local_zid); + const _z_id_t *local_zid); int8_t _z_transport_multicast_open_peer(_z_transport_multicast_establish_param_t *param, const _z_link_t *zl, - const _z_bytes_t *local_zid); + const _z_id_t *local_zid); int8_t _z_transport_close(_z_transport_t *zt, uint8_t reason); int8_t _z_transport_unicast_close(_z_transport_unicast_t *ztu, uint8_t reason); diff --git a/src/api/api.c b/src/api/api.c index 2a8229adf..e818d0c31 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -563,18 +563,10 @@ int8_t z_info_peers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) { if (zs._val->_tp._type == _Z_TRANSPORT_MULTICAST_TYPE) { _z_transport_peer_entry_list_t *l = zs._val->_tp._transport._multicast._peers; for (; l != NULL; l = _z_transport_peer_entry_list_tail(l)) { - z_id_t id; _z_transport_peer_entry_t *val = _z_transport_peer_entry_list_head(l); + z_id_t id = val->_remote_zid; - if (val->_remote_zid.len <= sizeof(id.id)) { - _z_bytes_t bs = val->_remote_zid; - for (size_t i = 0; i < bs.len; i++) { - id.id[i] = bs.start[bs.len - i - 1]; - } - (void)memset(&id.id[bs.len], 0, sizeof(id.id) - bs.len); - - callback->call(&id, ctx); - } + callback->call(&id, ctx); } } #else @@ -594,16 +586,8 @@ int8_t z_info_routers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) #if Z_UNICAST_TRANSPORT == 1 if (zs._val->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { - z_id_t id; - if (zs._val->_tp._transport._unicast._remote_zid.len <= sizeof(id.id)) { - _z_bytes_t bs = zs._val->_tp._transport._unicast._remote_zid; - for (size_t i = 0; i < bs.len; i++) { - id.id[i] = bs.start[bs.len - i - 1]; - } - (void)memset(&id.id[bs.len], 0, sizeof(id.id) - bs.len); - - callback->call(&id, ctx); - } + z_id_t id = zs._val->_tp._transport._unicast._remote_zid; + callback->call(&id, ctx); } #endif // Z_UNICAST_TRANSPORT == 1 @@ -614,20 +598,7 @@ int8_t z_info_routers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) return 0; } -z_id_t z_info_zid(const z_session_t zs) { - z_id_t id; - if (zs._val->_local_zid.len <= sizeof(id.id)) { - _z_bytes_t bs = zs._val->_local_zid; - for (size_t i = 0; i < bs.len; i++) { - id.id[i] = bs.start[bs.len - i - 1]; - } - (void)memset(&id.id[bs.len], 0, sizeof(id.id) - bs.len); - } else { - (void)memset(&id.id[0], 0, sizeof(id.id)); - } - - return id; -} +z_id_t z_info_zid(const z_session_t zs) { return zs._val->_local_zid; } z_put_options_t z_put_options_default(void) { return (z_put_options_t){.encoding = z_encoding_default(), diff --git a/src/net/memory.c b/src/net/memory.c index 67fedfc2e..33f7582ec 100644 --- a/src/net/memory.c +++ b/src/net/memory.c @@ -15,6 +15,8 @@ #include +#include "zenoh-pico/protocol/core.h" + void _z_sample_move(_z_sample_t *dst, _z_sample_t *src) { dst->keyexpr._id = src->keyexpr._id; // FIXME: call the z_keyexpr_move dst->keyexpr._suffix = src->keyexpr._suffix; // FIXME: call the z_keyexpr_move @@ -66,7 +68,7 @@ void _z_hello_free(_z_hello_t **hello) { void _z_reply_data_clear(_z_reply_data_t *reply_data) { _z_sample_clear(&reply_data->sample); - _z_bytes_clear(&reply_data->replier_id); + reply_data->replier_id = _z_id_empty(); } void _z_reply_data_free(_z_reply_data_t **reply_data) { diff --git a/src/net/primitives.c b/src/net/primitives.c index 66004d819..8f751bde2 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -285,8 +285,7 @@ int8_t _z_send_reply(const z_query_t *query, _z_keyexpr_t keyexpr, const uint8_t if (ret == _Z_RES_OK) { // Build the reply context decorator. This is NOT the final reply. - _z_bytes_t zid = - _z_bytes_wrap(((_z_session_t *)query->_zn)->_local_zid.start, ((_z_session_t *)query->_zn)->_local_zid.len); + _z_id_t zid = ((_z_session_t *)query->_zn)->_local_zid; _z_reply_context_t *rctx = _z_msg_make_reply_context(query->_qid, zid, false); _z_data_info_t di = {._flags = 0}; // Empty data info diff --git a/src/net/session.c b/src/net/session.c index 4f7abd212..c1b830915 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -30,15 +30,15 @@ int8_t __z_open_inner(_z_session_t *zn, char *locator, z_whatami_t mode) { int8_t ret = _Z_RES_OK; #if Z_UNICAST_TRANSPORT == 1 || Z_MULTICAST_TRANSPORT == 1 - _z_bytes_t local_zid = _z_bytes_empty(); + _z_id_t local_zid = _z_id_empty(); ret = _z_session_generate_zid(&local_zid, Z_ZID_LENGTH); if (ret == _Z_RES_OK) { ret = _z_new_transport(&zn->_tp, &local_zid, locator, mode); if (ret != _Z_RES_OK) { - _z_bytes_clear(&local_zid); + local_zid = _z_id_empty(); } } else { - _z_bytes_clear(&local_zid); + local_zid = _z_id_empty(); } #else ret = _Z_ERR_TRANSPORT_NOT_AVAILABLE; @@ -137,12 +137,14 @@ _z_config_t *_z_info(const _z_session_t *zn) { _z_config_t *ps = (_z_config_t *)z_malloc(sizeof(_z_config_t)); if (ps != NULL) { _z_config_init(ps); - _zp_config_insert(ps, Z_INFO_PID_KEY, _z_string_from_bytes(&zn->_local_zid)); + _z_bytes_t local_zid = _z_bytes_wrap(zn->_local_zid.id, _z_id_len(zn->_local_zid)); + _zp_config_insert(ps, Z_INFO_PID_KEY, _z_string_from_bytes(&local_zid)); #if Z_UNICAST_TRANSPORT == 1 if (zn->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { - _zp_config_insert(ps, Z_INFO_ROUTER_PID_KEY, - _z_string_from_bytes(&zn->_tp._transport._unicast._remote_zid)); + _z_id_t remote_zid = zn->_tp._transport._unicast._remote_zid; + _z_bytes_t remote_zidbytes = _z_bytes_wrap(remote_zid.id, _z_id_len(remote_zid)); + _zp_config_insert(ps, Z_INFO_ROUTER_PID_KEY, _z_string_from_bytes(&remote_zidbytes)); } else #endif // Z_UNICAST_TRANSPORT == 1 #if Z_MULTICAST_TRANSPORT == 1 @@ -150,7 +152,8 @@ _z_config_t *_z_info(const _z_session_t *zn) { _z_transport_peer_entry_list_t *xs = zn->_tp._transport._multicast._peers; while (xs != NULL) { _z_transport_peer_entry_t *peer = _z_transport_peer_entry_list_head(xs); - _zp_config_insert(ps, Z_INFO_PEER_PID_KEY, _z_string_from_bytes(&peer->_remote_zid)); + _z_bytes_t remote_zid = _z_bytes_wrap(peer->_remote_zid.id, _z_id_len(peer->_remote_zid)); + _zp_config_insert(ps, Z_INFO_PEER_PID_KEY, _z_string_from_bytes(&remote_zid)); xs = _z_transport_peer_entry_list_tail(xs); } diff --git a/src/protocol/extcodec.c b/src/protocol/extcodec.c index 9bffebe87..0a2c92247 100644 --- a/src/protocol/extcodec.c +++ b/src/protocol/extcodec.c @@ -164,6 +164,11 @@ int8_t _z_msg_ext_vec_decode(_z_msg_ext_vec_t *extensions, _z_zbuf_t *zbf) { return _z_msg_ext_decode_iter(zbf, (int8_t(*)(_z_msg_ext_t *, void *))_z_msg_ext_vec_push_callback, (void *)extensions); } + +int8_t _z_msg_ext_skip_non_mandatory(_z_msg_ext_t *extension, void *ctx) { + (void)ctx; + return (extension->_header & _Z_MSG_EXT_FLAG_M) ? _Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN : _Z_RES_OK; +} int8_t _z_msg_ext_decode_iter(_z_zbuf_t *zbf, int8_t (*callback)(_z_msg_ext_t *, void *), void *context) { int8_t ret = _Z_RES_OK; _Bool has_next = true; @@ -176,4 +181,8 @@ int8_t _z_msg_ext_decode_iter(_z_zbuf_t *zbf, int8_t (*callback)(_z_msg_ext_t *, } } return ret; +} + +int8_t _z_msg_ext_skip_non_mandatories(_z_zbuf_t *zbf) { + return _z_msg_ext_decode_iter(zbf, _z_msg_ext_skip_non_mandatory, NULL); } \ No newline at end of file diff --git a/src/protocol/msg.c b/src/protocol/msg.c index f0b2e679f..850c98a71 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -61,7 +61,7 @@ void _z_locators_clear(_z_locator_array_t *ls) { _z_locator_array_clear(ls); } void _z_t_msg_clear_attachment(_z_attachment_t *a) { _z_payload_clear(&a->_payload); } /*------------------ ReplyContext Decorator ------------------*/ -_z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_bytes_t replier_id, _Bool is_final) { +_z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, _Bool is_final) { _z_reply_context_t *rctx = (_z_reply_context_t *)z_malloc(sizeof(_z_reply_context_t)); if (rctx != NULL) { rctx->_qid = qid; @@ -76,11 +76,7 @@ _z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_bytes_t replier_ return rctx; } -void _z_msg_clear_reply_context(_z_reply_context_t *rc) { - if (_Z_HAS_FLAG(rc->_header, _Z_FLAG_Z_F) == false) { - _z_bytes_clear(&rc->_replier_id); - } -} +void _z_msg_clear_reply_context(_z_reply_context_t *rc) {} /*=============================*/ /* Zenoh Messages */ @@ -635,7 +631,7 @@ void _z_s_msg_copy_hello(_z_s_msg_hello_t *clone, _z_s_msg_hello_t *msg) { void _z_s_msg_clear_hello(_z_s_msg_hello_t *msg) { _z_locators_clear(&msg->_locators); } /*------------------ Join Message ------------------*/ -_z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_bytes_t zid, +_z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_id_t zid, _z_conduit_sn_list_t next_sn) { _z_transport_message_t msg; msg._header = _Z_MID_T_JOIN; @@ -676,13 +672,13 @@ void _z_t_msg_copy_join(_z_t_msg_join_t *clone, _z_t_msg_join_t *msg) { clone->_req_id_res = msg->_req_id_res; clone->_batch_size = msg->_batch_size; clone->_next_sn = msg->_next_sn; - _z_bytes_copy(&clone->_zid, &msg->_zid); + memcpy(clone->_zid.id, msg->_zid.id, 16); } -void _z_t_msg_clear_join(_z_t_msg_join_t *msg) { _z_bytes_clear(&msg->_zid); } +void _z_t_msg_clear_join(_z_t_msg_join_t *msg) {} /*------------------ Init Message ------------------*/ -_z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_bytes_t zid) { +_z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_id_t zid) { _z_transport_message_t msg; msg._header = _Z_MID_T_INIT; @@ -708,7 +704,7 @@ _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_bytes_t zi return msg; } -_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_bytes_t zid, _z_bytes_t cookie) { +_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, _z_bytes_t cookie) { _z_transport_message_t msg; msg._header = _Z_MID_T_INIT; _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_A); @@ -742,14 +738,11 @@ void _z_t_msg_copy_init(_z_t_msg_init_t *clone, _z_t_msg_init_t *msg) { clone->_key_id_res = msg->_key_id_res; clone->_req_id_res = msg->_req_id_res; clone->_batch_size = msg->_batch_size; - _z_bytes_copy(&clone->_zid, &msg->_zid); + memcpy(clone->_zid.id, msg->_zid.id, 16); _z_bytes_copy(&clone->_cookie, &msg->_cookie); } -void _z_t_msg_clear_init(_z_t_msg_init_t *msg) { - _z_bytes_clear(&msg->_zid); - _z_bytes_clear(&msg->_cookie); -} +void _z_t_msg_clear_init(_z_t_msg_init_t *msg) { _z_bytes_clear(&msg->_cookie); } /*------------------ Open Message ------------------*/ _z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie) { diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index f38396443..bd804e197 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -25,7 +25,9 @@ #include "zenoh-pico/protocol/extcodec.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/keyexpr.h" +#include "zenoh-pico/protocol/msg.h" #include "zenoh-pico/utils/logging.h" +#include "zenoh-pico/utils/result.h" /*=============================*/ /* Fields */ @@ -279,7 +281,7 @@ int8_t _z_reply_context_encode(_z_wbuf_t *wbf, const _z_reply_context_t *msg) { _Z_EC(_z_zint_encode(wbf, msg->_qid)) if (_Z_HAS_FLAG(msg->_header, _Z_FLAG_Z_F) == false) { - _Z_EC(_z_bytes_encode(wbf, &msg->_replier_id)) + _Z_EC(_z_id_encode_as_zbytes(wbf, &msg->_replier_id)) } return ret; @@ -293,11 +295,9 @@ int8_t _z_reply_context_decode_na(_z_reply_context_t *rc, _z_zbuf_t *zbf, uint8_ ret |= _z_zint_decode(&rc->_qid, zbf); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_F) == false) { - _z_bytes_t rid; - ret |= _z_bytes_decode(&rid, zbf); - _z_bytes_copy(&rc->_replier_id, &rid); + ret |= _z_id_decode_as_zbytes(&rc->_replier_id, zbf); } else { - rc->_replier_id = _z_bytes_empty(); + rc->_replier_id = _z_id_empty(); } return ret; @@ -1194,13 +1194,16 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_T_JOIN\n"); + // TODO[protocol] add options byte if `O==1` + _Z_EC(_z_wbuf_write(wbf, msg->_version)) uint8_t cbyte = 0; cbyte |= (msg->_whatami & 0x03); - cbyte |= ((msg->_zid.len - 1) & 0x0F) << 4; // TODO[protocol]: check if ZID > 0 && <= 16 + uint8_t zidlen = _z_id_len(msg->_zid); + cbyte |= ((zidlen - 1) & 0x0F) << 4; // TODO[protocol]: check if ZID > 0 && <= 16 _Z_EC(_z_uint8_encode(wbf, cbyte)) - _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) + _Z_EC(_z_wbuf_write_bytes(wbf, msg->_zid.id, 0, zidlen)) if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S) == true) { cbyte = 0; @@ -1240,15 +1243,11 @@ int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { uint8_t cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); msg->_whatami = cbyte & 0x03; - msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; + uint8_t zidlen = ((cbyte & 0xF0) >> 4) + 1; + msg->_zid = _z_id_empty(); if (ret == _Z_RES_OK) { - ret |= _z_bytes_val_decode(&msg->_zid, zbf); - if (ret != _Z_RES_OK) { - msg->_zid = _z_bytes_empty(); - } - } else { - msg->_zid = _z_bytes_empty(); + _z_zbuf_read_bytes(zbf, msg->_zid.id, 0, zidlen); } if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S) == true) { @@ -1297,9 +1296,10 @@ 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); - cbyte |= ((msg->_zid.len - 1) & 0x0F) << 4; // TODO[protocol]: check if ZID > 0 && <= 16 + uint8_t zidlen = _z_id_len(msg->_zid); + cbyte |= ((zidlen - 1) & 0x0F) << 4; // TODO[protocol]: check if ZID > 0 && <= 16 _Z_EC(_z_uint8_encode(wbf, cbyte)) - _Z_EC(_z_bytes_val_encode(wbf, &msg->_zid)) + _Z_EC(_z_wbuf_write_bytes(wbf, msg->_zid.id, 0, zidlen)) if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_S) == true) { cbyte = 0; @@ -1325,16 +1325,12 @@ int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { uint8_t cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); - msg->_whatami = cbyte & 0x03; - msg->_zid.len = ((cbyte & 0xF0) >> 4) + 1; + msg->_zid = _z_id_empty(); if (ret == _Z_RES_OK) { - ret |= _z_bytes_val_decode(&msg->_zid, zbf); - if (ret != _Z_RES_OK) { - msg->_zid = _z_bytes_empty(); - } - } else { - msg->_zid = _z_bytes_empty(); + msg->_whatami = cbyte & 0x03; + uint8_t zidlen = ((cbyte & 0xF0) >> 4) + 1; + _z_zbuf_read_bytes(zbf, msg->_zid.id, 0, zidlen); } if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_S) == true) { @@ -1666,7 +1662,7 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t case _Z_MID_T_JOIN: { ret |= _z_join_encode(wbf, msg->_header, &msg->_body._join); - ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); + // ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; case _Z_MID_T_INIT: { @@ -1790,7 +1786,7 @@ int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_scout_t *m uint8_t cbyte = 0; cbyte |= (msg->_what & 0x07); uint8_t zid_len = _z_id_len(msg->_zid); - if (zid_len > 0) { // TODO[protocol]: check if ZID > 0 && <= 16 + if (zid_len > 0) { _Z_SET_FLAG(cbyte, _Z_FLAG_T_SCOUT_I); cbyte |= ((zid_len - 1) & 0x0F) << 4; } @@ -1811,12 +1807,10 @@ int8_t _z_scout_decode_na(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) uint8_t cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); msg->_what = cbyte & 0x07; + msg->_zid = _z_id_empty(); if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(cbyte, _Z_FLAG_T_SCOUT_I) == true)) { - uint8_t len = ((cbyte & 0xF0) >> 4) + 1; - msg->_zid = _z_id_empty(); - _z_zbuf_read_bytes(zbf, msg->_zid.id, 0, len); - } else { - msg->_zid = _z_id_empty(); + uint8_t zidlen = ((cbyte & 0xF0) >> 4) + 1; + _z_zbuf_read_bytes(zbf, msg->_zid.id, 0, zidlen); } return ret; @@ -1835,7 +1829,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; // TODO[protocol]: check if ZID > 0 && <= 16 + cbyte |= ((zidlen - 1) & 0x0F) << 4; _Z_EC(_z_uint8_encode(wbf, cbyte)) _Z_EC(_z_bytes_val_encode(wbf, &(_z_bytes_t){.start = msg->_zid.id, .len = zidlen, ._is_alloc = false})); @@ -1914,8 +1908,6 @@ int8_t _z_scouting_message_encode(_z_wbuf_t *wbf, const _z_scouting_message_t *m return ret; } - -int8_t _z_scouting_decode_handle_ext(_z_msg_ext_t *extension, void *extensions) { return 0; } int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; @@ -1948,10 +1940,8 @@ int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *zbf) } } while ((ret == _Z_RES_OK) && (is_last == false)); - if (ret == _Z_RES_OK) { - if ((msg->_header & _Z_MSG_EXT_FLAG_Z) != 0) { - ret |= _z_msg_ext_decode_iter(zbf, _z_scouting_decode_handle_ext, NULL); - } + if ((ret == _Z_RES_OK) && (msg->_header & _Z_MSG_EXT_FLAG_Z) != 0) { + ret |= _z_msg_ext_skip_non_mandatories(zbf); } return ret; diff --git a/src/session/query.c b/src/session/query.c index 6951dbede..8c3042671 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -166,7 +166,7 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_reply_context_t // Build the reply _z_reply_t reply; reply._tag = Z_REPLY_TAG_DATA; - _z_bytes_copy(&reply.data.replier_id, &reply_context->_replier_id); + reply.data.replier_id = reply_context->_replier_id; reply.data.sample.keyexpr = expanded_ke; _z_bytes_copy(&reply.data.sample.payload, &payload); reply.data.sample.encoding.prefix = encoding.prefix; diff --git a/src/session/queryable.c b/src/session/queryable.c index 466e2c54c..2d2e28510 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -16,6 +16,7 @@ #include "zenoh-pico/config.h" #include "zenoh-pico/net/resource.h" +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/session/resource.h" #include "zenoh-pico/session/utils.h" @@ -171,10 +172,7 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query) { // Send the final reply // Final flagged reply context does not encode the ZID - _z_bytes_t zid; - _z_bytes_reset(&zid); - _Bool is_final = true; - _z_reply_context_t *rctx = _z_msg_make_reply_context(query->_qid, zid, is_final); + _z_reply_context_t *rctx = _z_msg_make_reply_context(query->_qid, _z_id_empty(), true); // Congestion control _Bool can_be_dropped = false; diff --git a/src/session/utils.c b/src/session/utils.c index bba256942..70e5a320f 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -52,21 +52,14 @@ _Bool _z_timestamp_check(const _z_timestamp_t *stamp) { return false; } -int8_t _z_session_generate_zid(_z_bytes_t *bs, uint8_t size) { +int8_t _z_session_generate_zid(_z_id_t *bs, uint8_t size) { int8_t ret = _Z_RES_OK; - - *bs = _z_bytes_make(size); - if (bs->_is_alloc == true) { - z_random_fill((uint8_t *)bs->start, bs->len); - } else { - ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - + z_random_fill((uint8_t *)bs->id, size); return ret; } /*------------------ Init/Free/Close session ------------------*/ -int8_t _z_session_init(_z_session_t *zn, _z_bytes_t *zid) { +int8_t _z_session_init(_z_session_t *zn, _z_id_t *zid) { int8_t ret = _Z_RES_OK; // Initialize the counters to 1 @@ -87,8 +80,7 @@ int8_t _z_session_init(_z_session_t *zn, _z_bytes_t *zid) { ret = _z_mutex_init(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 if (ret == _Z_RES_OK) { - zn->_local_zid = _z_bytes_empty(); - _z_bytes_move(&zn->_local_zid, zid); + zn->_local_zid = *zid; #if Z_UNICAST_TRANSPORT == 1 if (zn->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { zn->_tp._transport._unicast._session = zn; @@ -104,7 +96,6 @@ int8_t _z_session_init(_z_session_t *zn, _z_bytes_t *zid) { } } else { _z_transport_clear(&zn->_tp); - _z_bytes_clear(&zn->_local_zid); } return ret; @@ -112,7 +103,6 @@ int8_t _z_session_init(_z_session_t *zn, _z_bytes_t *zid) { void _z_session_clear(_z_session_t *zn) { // Clear Zenoh PID - _z_bytes_clear(&zn->_local_zid); // Clean up transports _z_transport_clear(&zn->_tp); diff --git a/src/transport/manager.c b/src/transport/manager.c index 49c0cbab2..f365a399e 100644 --- a/src/transport/manager.c +++ b/src/transport/manager.c @@ -17,7 +17,7 @@ #include #include -int8_t _z_new_transport_client(_z_transport_t *zt, char *locator, _z_bytes_t *local_zid) { +int8_t _z_new_transport_client(_z_transport_t *zt, char *locator, _z_id_t *local_zid) { int8_t ret = _Z_RES_OK; _z_link_t zl; @@ -56,7 +56,7 @@ int8_t _z_new_transport_client(_z_transport_t *zt, char *locator, _z_bytes_t *lo return ret; } -int8_t _z_new_transport_peer(_z_transport_t *zt, char *locator, _z_bytes_t *local_zid) { +int8_t _z_new_transport_peer(_z_transport_t *zt, char *locator, _z_id_t *local_zid) { int8_t ret = _Z_RES_OK; _z_link_t zl; @@ -95,7 +95,7 @@ int8_t _z_new_transport_peer(_z_transport_t *zt, char *locator, _z_bytes_t *loca return ret; } -int8_t _z_new_transport(_z_transport_t *zt, _z_bytes_t *bs, char *locator, z_whatami_t mode) { +int8_t _z_new_transport(_z_transport_t *zt, _z_id_t *bs, char *locator, z_whatami_t mode) { int8_t ret; if (mode == Z_WHATAMI_CLIENT) { diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 99b190a7a..c2b57e388 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -243,7 +243,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t if (ret == _Z_RES_OK) { entry->_remote_addr = _z_bytes_duplicate(addr); - entry->_remote_zid = _z_bytes_duplicate(&t_msg->_body._join._zid); + entry->_remote_zid = t_msg->_body._join._zid; _z_conduit_sn_list_copy(&entry->_sn_rx_sns, &t_msg->_body._join._next_sn); _z_conduit_sn_list_decrement(entry->_sn_res, &entry->_sn_rx_sns); diff --git a/src/transport/multicast/link/task/join.c b/src/transport/multicast/link/task/join.c index a14ab49b9..dedc7f49c 100644 --- a/src/transport/multicast/link/task/join.c +++ b/src/transport/multicast/link/task/join.c @@ -25,8 +25,7 @@ int8_t _zp_multicast_send_join(_z_transport_multicast_t *ztm) { next_sn._val._plain._best_effort = ztm->_sn_tx_best_effort; next_sn._val._plain._reliable = ztm->_sn_tx_reliable; - _z_bytes_t zid = _z_bytes_wrap(((_z_session_t *)ztm->_session)->_local_zid.start, - ((_z_session_t *)ztm->_session)->_local_zid.len); + _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 _z_multicast_send_t_msg(ztm, &jsm); diff --git a/src/transport/peer_entry.c b/src/transport/peer_entry.c index 638e087a1..eebb7508f 100644 --- a/src/transport/peer_entry.c +++ b/src/transport/peer_entry.c @@ -12,6 +12,7 @@ // ZettaScale Zenoh Team, // +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/transport/transport.h" #include "zenoh-pico/transport/utils.h" @@ -19,7 +20,7 @@ void _z_transport_peer_entry_clear(_z_transport_peer_entry_t *src) { _z_wbuf_clear(&src->_dbuf_reliable); _z_wbuf_clear(&src->_dbuf_best_effort); - _z_bytes_clear(&src->_remote_zid); + src->_remote_zid = _z_id_empty(); _z_bytes_clear(&src->_remote_addr); } @@ -34,7 +35,7 @@ void _z_transport_peer_entry_copy(_z_transport_peer_entry_t *dst, const _z_trans dst->_next_lease = src->_next_lease; dst->_received = src->_received; - _z_bytes_copy(&dst->_remote_zid, &src->_remote_zid); + dst->_remote_zid = src->_remote_zid; _z_bytes_copy(&dst->_remote_addr, &src->_remote_addr); } @@ -45,11 +46,7 @@ size_t _z_transport_peer_entry_size(const _z_transport_peer_entry_t *src) { _Bool _z_transport_peer_entry_eq(const _z_transport_peer_entry_t *left, const _z_transport_peer_entry_t *right) { _Bool ret = true; - if (left->_remote_zid.len == right->_remote_zid.len) { - if (memcmp(left->_remote_zid.start, right->_remote_zid.start, left->_remote_zid.len) != 0) { - ret = false; - } - } else { + if (memcmp(left->_remote_zid.id, right->_remote_zid.id, 16) != 0) { ret = false; } diff --git a/src/transport/transport.c b/src/transport/transport.c index a06bcbc09..aa0993e0c 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -19,6 +19,7 @@ #include #include "zenoh-pico/config.h" +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/transport/link/rx.h" #include "zenoh-pico/transport/link/tx.h" #include "zenoh-pico/transport/utils.h" @@ -160,9 +161,9 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic zt->_transport._unicast._link = *zl; // Remote peer PID - _z_bytes_move(&zt->_transport._unicast._remote_zid, ¶m->_remote_zid); + zt->_transport._unicast._remote_zid = param->_remote_zid; } else { - _z_bytes_clear(¶m->_remote_zid); + param->_remote_zid = _z_id_empty(); } return ret; @@ -248,10 +249,10 @@ int8_t _z_transport_multicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_mu #if Z_UNICAST_TRANSPORT == 1 int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t *param, const _z_link_t *zl, - const _z_bytes_t *local_zid) { + const _z_id_t *local_zid) { int8_t ret = _Z_RES_OK; - _z_bytes_t zid = _z_bytes_wrap(local_zid->start, local_zid->len); + _z_id_t zid = *local_zid; _z_transport_message_t ism = _z_t_msg_make_init_syn(Z_WHATAMI_CLIENT, zid); param->_seq_num_res = ism._body._init._seq_num_res; // The announced sn resolution param->_key_id_res = ism._body._init._key_id_res; // The announced key id resolution @@ -305,7 +306,7 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * param->_initial_sn_tx = param->_initial_sn_tx & !_z_sn_modulo_mask(param->_seq_num_res); // Initialize the Local and Remote Peer IDs - _z_bytes_copy(¶m->_remote_zid, &iam._body._init._zid); + param->_remote_zid = iam._body._init._zid; // Create the OpenSyn message _z_zint_t lease = Z_TRANSPORT_LEASE; @@ -352,7 +353,7 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * #if Z_MULTICAST_TRANSPORT == 1 int8_t _z_transport_multicast_open_client(_z_transport_multicast_establish_param_t *param, const _z_link_t *zl, - const _z_bytes_t *local_zid) { + const _z_id_t *local_zid) { (void)(param); (void)(zl); (void)(local_zid); @@ -366,7 +367,7 @@ int8_t _z_transport_multicast_open_client(_z_transport_multicast_establish_param #if Z_UNICAST_TRANSPORT == 1 int8_t _z_transport_unicast_open_peer(_z_transport_unicast_establish_param_t *param, const _z_link_t *zl, - const _z_bytes_t *local_zid) { + const _z_id_t *local_zid) { (void)(param); (void)(zl); (void)(local_zid); @@ -380,7 +381,7 @@ int8_t _z_transport_unicast_open_peer(_z_transport_unicast_establish_param_t *pa #if Z_MULTICAST_TRANSPORT == 1 int8_t _z_transport_multicast_open_peer(_z_transport_multicast_establish_param_t *param, const _z_link_t *zl, - const _z_bytes_t *local_zid) { + const _z_id_t *local_zid) { int8_t ret = _Z_RES_OK; _z_zint_t initial_sn_tx = 0; @@ -391,7 +392,7 @@ int8_t _z_transport_multicast_open_peer(_z_transport_multicast_establish_param_t next_sn._val._plain._best_effort = initial_sn_tx; next_sn._val._plain._reliable = initial_sn_tx; - _z_bytes_t zid = _z_bytes_wrap(local_zid->start, local_zid->len); + _z_id_t zid = *local_zid; _z_transport_message_t jsm = _z_t_msg_make_join(Z_WHATAMI_PEER, Z_TRANSPORT_LEASE, zid, next_sn); // Encode and send the message @@ -447,7 +448,7 @@ void _z_transport_unicast_clear(_z_transport_unicast_t *ztu) { _z_wbuf_clear(&ztu->_dbuf_best_effort); // Clean up PIDs - _z_bytes_clear(&ztu->_remote_zid); + ztu->_remote_zid = _z_id_empty(); _z_link_clear(&ztu->_link); } #endif // Z_UNICAST_TRANSPORT == 1 diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index b9cbf5446..1c7574cfc 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -176,6 +176,19 @@ _z_bytes_t gen_bytes(size_t len) { return arr; } +_z_id_t gen_zid(void) { + _z_id_t id = _z_id_empty(); + uint8_t hash = 55; + uint8_t len = gen_uint8() % 16; + for (uint8_t i = 0; i < len; i++) { + uint8_t byte = gen_uint8(); + id.id[i] = byte; + hash ^= byte; + } + id.id[0] = hash; + return id; +} + char *gen_str(size_t size) { char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; char *str = (char *)z_malloc(size + 1); @@ -912,10 +925,10 @@ void print_reply_context(_z_reply_context_t *rc) { _z_reply_context_t *gen_reply_context(void) { _z_zint_t qid = gen_zint(); - _z_bytes_t replier_id = gen_bytes(16); + _z_id_t replier_id = gen_zid(); _Bool is_final = gen_bool(); if (is_final == true) { - _z_bytes_clear(&replier_id); + replier_id = _z_id_empty(); } return _z_msg_make_reply_context(qid, replier_id, is_final); @@ -929,10 +942,11 @@ void assert_eq_reply_context(_z_reply_context_t *left, _z_reply_context_t *right assert(left->_qid == right->_qid); printf("Replier ID ("); - if (_Z_HAS_FLAG(left->_header, _Z_FLAG_Z_F) == false) - assert_eq_uint8_array(&left->_replier_id, &right->_replier_id); - else + if (_Z_HAS_FLAG(left->_header, _Z_FLAG_Z_F) == false) { + assert(memcmp(left->_replier_id.id, right->_replier_id.id, 16) == 0); + } else { printf("NULL:NULL"); + } printf(")"); } @@ -2114,14 +2128,7 @@ void response_final_message(void) { /*------------------ Scout Message ------------------*/ _z_scouting_message_t gen_scout_message(void) { z_what_t what = gen_uint8() % 7; - _z_bytes_t zidbytes; - if (gen_bool()) { - zidbytes = gen_bytes((gen_uint8() % 16) + 1); - } else { - zidbytes = _z_bytes_empty(); - } - _z_id_t zid = _z_id_empty(); - memcpy(zid.id, zidbytes.start, zidbytes.len); + _z_id_t zid = gen_zid(); return _z_s_msg_make_scout(what, zid); } @@ -2167,10 +2174,7 @@ void scout_message(void) { /*------------------ Hello Message ------------------*/ _z_scouting_message_t gen_hello_message(void) { z_whatami_t whatami = (gen_uint8() % 2) + 1; - _z_bytes_t zidbytes = gen_bytes((gen_uint8() % 16) + 1); - _z_id_t zid = _z_id_empty(); - memcpy(zid.id, zidbytes.start, zidbytes.len); - _z_bytes_clear(&zidbytes); + _z_id_t zid = gen_zid(); _z_locator_array_t locators; if (gen_bool() == true) @@ -2237,7 +2241,7 @@ void hello_message(void) { /*------------------ Join Message ------------------*/ _z_transport_message_t gen_join_message(void) { z_whatami_t whatami = (gen_uint8() % 2) + 1; - _z_bytes_t zid = gen_bytes(16); + _z_id_t zid = gen_zid(); _z_zint_t lease = gen_bool() ? gen_zint() * 1000 : gen_zint(); _z_conduit_sn_list_t next_sn; @@ -2282,7 +2286,7 @@ void assert_eq_join_message(_z_t_msg_join_t *left, _z_t_msg_join_t *right, uint8 printf("\n"); printf(" "); - assert_eq_uint8_array(&left->_zid, &right->_zid); + assert(memcmp(left->_zid.id, right->_zid.id, 16) == 0); printf("\n"); if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S) == true) { @@ -2366,7 +2370,7 @@ void join_message(void) { /*------------------ Init Message ------------------*/ _z_transport_message_t gen_init_message(void) { z_whatami_t whatami = (gen_uint8() % 2) + 1; - _z_bytes_t zid = gen_bytes(16); + _z_id_t zid = gen_zid(); _z_transport_message_t t_msg; if (gen_bool()) { @@ -2405,7 +2409,7 @@ void assert_eq_init_message(_z_t_msg_init_t *left, _z_t_msg_init_t *right, uint8 printf("\n"); printf(" "); - assert_eq_uint8_array(&left->_zid, &right->_zid); + assert(memcmp(left->_zid.id, right->_zid.id, 16) == 0); printf("\n"); if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_S) == true) { From 8315b0be6f3fbf4f58c9462dcae815e6f3a18f5c Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 7 Jun 2023 14:21:41 +0200 Subject: [PATCH 091/181] new scout message --- .clang-format | 1 + include/zenoh-pico/collections/list.h | 4 +-- include/zenoh-pico/protocol/ext.h | 1 + include/zenoh-pico/protocol/iobuf.h | 2 ++ include/zenoh-pico/protocol/msg.h | 7 ++-- include/zenoh-pico/session/query.h | 5 +-- .../system/platform/arduino/opencr.h | 2 +- src/collections/bytes.c | 3 +- src/protocol/iobuf.c | 8 +++++ src/protocol/msg.c | 3 -- src/protocol/msgcodec.c | 34 ++++++++++++------- src/system/unix/network.c | 4 +-- src/system/windows/network.c | 4 +-- src/system/windows/system.c | 2 +- src/transport/multicast/link/rx.c | 2 -- tests/z_msgcodec_test.c | 5 --- 16 files changed, 50 insertions(+), 37 deletions(-) diff --git a/.clang-format b/.clang-format index a777cf13a..f36aab3ab 100644 --- a/.clang-format +++ b/.clang-format @@ -1,3 +1,4 @@ BasedOnStyle: Google IndentWidth: 4 ColumnLimit: 120 +PointerAlignment: Left \ No newline at end of file diff --git a/include/zenoh-pico/collections/list.h b/include/zenoh-pico/collections/list.h index b2b913175..339e7c25f 100644 --- a/include/zenoh-pico/collections/list.h +++ b/include/zenoh-pico/collections/list.h @@ -59,8 +59,8 @@ void _z_list_free(_z_list_t **xs, z_element_free_f f_f); static inline type *name##_list_head(const name##_list_t *l) { return (type *)_z_list_head(l); } \ static inline name##_list_t *name##_list_tail(const name##_list_t *l) { return _z_list_tail(l); } \ static inline name##_list_t *name##_list_push(name##_list_t *l, type *e) { return _z_list_push(l, e); } \ - static inline name##_list_t *name##_list_pop(name##_list_t *l, type** x) { \ - return _z_list_pop(l, name##_elem_free, (void**)x); \ + static inline name##_list_t *name##_list_pop(name##_list_t *l, type **x) { \ + return _z_list_pop(l, name##_elem_free, (void **)x); \ } \ static inline name##_list_t *name##_list_find(const name##_list_t *l, name##_eq_f c_f, type *e) { \ return _z_list_find(l, (z_element_eq_f)c_f, e); \ diff --git a/include/zenoh-pico/protocol/ext.h b/include/zenoh-pico/protocol/ext.h index 1fe5620d7..3a81075d5 100644 --- a/include/zenoh-pico/protocol/ext.h +++ b/include/zenoh-pico/protocol/ext.h @@ -51,6 +51,7 @@ /* Extension flags */ /*=============================*/ #define _Z_MSG_EXT_FLAG_M 0x10 +#define _Z_MSG_EXT_IS_MANDATORY(h) ((h & _Z_MSG_EXT_FLAG_M) != 0) #define _Z_MSG_EXT_FLAG_Z 0x80 typedef struct { diff --git a/include/zenoh-pico/protocol/iobuf.h b/include/zenoh-pico/protocol/iobuf.h index 0e0b98a97..1ffc0b47f 100644 --- a/include/zenoh-pico/protocol/iobuf.h +++ b/include/zenoh-pico/protocol/iobuf.h @@ -64,6 +64,8 @@ typedef struct { _z_zbuf_t _z_zbuf_make(size_t capacity); _z_zbuf_t _z_zbuf_view(_z_zbuf_t *zbf, size_t length); +/// Constructs a _borrowing_ reader on `slice` +_z_zbuf_t _z_zbytes_as_zbuf(_z_bytes_t slice); size_t _z_zbuf_capacity(const _z_zbuf_t *zbf); size_t _z_zbuf_len(const _z_zbuf_t *zbf); diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 51c0c09d8..b0a671006 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -95,8 +95,8 @@ // T Lease period if T==1 then the lease period is in seconds else in milliseconds // S Size params if S==1 then size parameters are exchanged // Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_T_JOIN_T 0x20 // 1 << 5 -#define _Z_FLAG_T_JOIN_S 0x40 // 1 << 6 +#define _Z_FLAG_T_JOIN_T 0x40 // 1 << 6 +#define _Z_FLAG_T_JOIN_S 0x20 // 1 << 5 // Init message flags: // A Ack if A==1 then the message is an acknowledgment (aka InitAck), otherwise InitSyn @@ -933,10 +933,9 @@ typedef struct { typedef struct { _z_id_t _zid; _z_zint_t _lease; - uint16_t _batch_size; _z_conduit_sn_list_t _next_sn; + uint16_t _batch_size; z_whatami_t _whatami; - uint8_t _key_id_res; uint8_t _req_id_res; uint8_t _seq_num_res; uint8_t _version; diff --git a/include/zenoh-pico/session/query.h b/include/zenoh-pico/session/query.h index ab8fb42b2..e30f23af9 100644 --- a/include/zenoh-pico/session/query.h +++ b/include/zenoh-pico/session/query.h @@ -24,8 +24,9 @@ _z_pending_query_t *_z_get_pending_query_by_id(_z_session_t *zn, const _z_zint_t int8_t _z_register_pending_query(_z_session_t *zn, _z_pending_query_t *pq); int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_reply_context_t *reply_context, - 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_keyexpr_t keyexpr, const _z_bytes_t payload, + const _z_encoding_t encoding, const _z_zint_t kind, + const _z_timestamp_t timestamp); int8_t _z_trigger_query_reply_final(_z_session_t *zn, const _z_reply_context_t *reply_context); void _z_unregister_pending_query(_z_session_t *zn, _z_pending_query_t *pq); void _z_flush_pending_queries(_z_session_t *zn); diff --git a/include/zenoh-pico/system/platform/arduino/opencr.h b/include/zenoh-pico/system/platform/arduino/opencr.h index 85f4b5752..23c43f222 100644 --- a/include/zenoh-pico/system/platform/arduino/opencr.h +++ b/include/zenoh-pico/system/platform/arduino/opencr.h @@ -42,7 +42,7 @@ typedef struct { #if Z_LINK_UDP_MULTICAST == 1 || Z_LINK_UDP_UNICAST == 1 WiFiUDP *_udp; // As pointer to cross the boundary between C and C++ #endif - _Bool _err; + _Bool _err; }; } _z_sys_net_socket_t; diff --git a/src/collections/bytes.c b/src/collections/bytes.c index 49643b827..0a228fd38 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -78,7 +78,8 @@ void _z_bytes_free(_z_bytes_t **bs) { } void _z_bytes_copy(_z_bytes_t *dst, const _z_bytes_t *src) { - int8_t ret = _z_bytes_init(dst, src->len); // FIXME: it should check if dst is already initialized. Otherwise it will leak + int8_t ret = + _z_bytes_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); } diff --git a/src/protocol/iobuf.c b/src/protocol/iobuf.c index eef65ad60..9652d79a8 100644 --- a/src/protocol/iobuf.c +++ b/src/protocol/iobuf.c @@ -16,6 +16,7 @@ #include #include +#include #include #include "zenoh-pico/config.h" @@ -166,6 +167,13 @@ _z_zbuf_t _z_zbuf_view(_z_zbuf_t *zbf, size_t length) { v._ios = _z_iosli_wrap(_z_zbuf_get_rptr(zbf), length, 0, length); return v; } +_z_zbuf_t _z_zbytes_as_zbuf(_z_bytes_t slice) { + return (_z_zbuf_t){._ios = {._buf = (uint8_t *)slice.start, // Safety: `_z_zbuf_t` is an immutable buffer + ._is_alloc = false, + ._capacity = slice.len, + ._r_pos = 0, + ._w_pos = slice.len}}; +} size_t _z_zbuf_capacity(const _z_zbuf_t *zbf) { return zbf->_ios._capacity; } diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 850c98a71..8f8a53606 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -640,7 +640,6 @@ _z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, msg._body._join._whatami = whatami; msg._body._join._lease = lease; msg._body._join._seq_num_res = Z_SN_RESOLUTION; - msg._body._join._key_id_res = Z_KID_RESOLUTION; msg._body._join._req_id_res = Z_REQ_RESOLUTION; msg._body._join._batch_size = Z_BATCH_SIZE; msg._body._join._next_sn = next_sn; @@ -652,7 +651,6 @@ _z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, if ((msg._body._join._batch_size != _Z_DEFAULT_BATCH_SIZE) || (msg._body._join._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || - (msg._body._join._key_id_res != _Z_DEFAULT_RESOLUTION_SIZE) || (msg._body._join._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_JOIN_S); } @@ -668,7 +666,6 @@ void _z_t_msg_copy_join(_z_t_msg_join_t *clone, _z_t_msg_join_t *msg) { clone->_whatami = msg->_whatami; clone->_lease = msg->_lease; clone->_seq_num_res = msg->_seq_num_res; - clone->_key_id_res = msg->_key_id_res; clone->_req_id_res = msg->_req_id_res; clone->_batch_size = msg->_batch_size; clone->_next_sn = msg->_next_sn; diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index bd804e197..954045df2 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1209,7 +1209,6 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg cbyte = 0; cbyte |= (msg->_seq_num_res & 0x03); cbyte |= ((msg->_req_id_res & 0x03) << 2); - cbyte |= ((msg->_key_id_res & 0x03) << 4); _Z_EC(_z_uint8_encode(wbf, cbyte)) _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) } @@ -1234,6 +1233,22 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg return ret; } +int8_t _z_join_decode_ext(_z_msg_ext_t *extension, void *ctx) { + int8_t ret = _Z_RES_OK; + _z_t_msg_join_t *msg = (_z_t_msg_join_t *)ctx; + if (_Z_EXT_FULL_ID(extension->_header) == 0x51) { // QOS: (enc=zbuf)(mandatory=true)(id=1) + msg->_next_sn._is_qos = true; + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + for (int i = 0; (ret == _Z_RES_OK) && (i < Z_PRIORITIES_NUM); ++i) { + ret |= _z_zint_decode(&msg->_next_sn._val._plain._reliable, &zbf); + ret |= _z_zint_decode(&msg->_next_sn._val._plain._best_effort, &zbf); + } + } else if (_Z_MSG_EXT_IS_MANDATORY(extension->_header)) { + ret = _Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN; + } + return ret; +} + int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_T_JOIN\n"); int8_t ret = _Z_RES_OK; @@ -1243,8 +1258,8 @@ int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { uint8_t cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); msg->_whatami = cbyte & 0x03; - uint8_t zidlen = ((cbyte & 0xF0) >> 4) + 1; + uint8_t zidlen = ((cbyte & 0xF0) >> 4) + 1; msg->_zid = _z_id_empty(); if (ret == _Z_RES_OK) { _z_zbuf_read_bytes(zbf, msg->_zid.id, 0, zidlen); @@ -1255,12 +1270,10 @@ int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { ret |= _z_uint8_decode(&cbyte, zbf); msg->_seq_num_res = (cbyte & 0x03); msg->_req_id_res = ((cbyte >> 2) & 0x03); - msg->_key_id_res = ((cbyte >> 4) & 0x03); ret |= _z_uint16_decode(&msg->_batch_size, zbf); } else { msg->_seq_num_res = _Z_DEFAULT_RESOLUTION_SIZE; msg->_req_id_res = _Z_DEFAULT_RESOLUTION_SIZE; - msg->_key_id_res = _Z_DEFAULT_RESOLUTION_SIZE; msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; } @@ -1269,16 +1282,13 @@ int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { msg->_lease = msg->_lease * 1000; } - // TODO[protocol] - // if (_Z_HAS_FLAG(msg->_options, _Z_OPT_JOIN_QOS) == true) { - // for (uint8_t i = 0; (ret == _Z_RES_OK) && (i < Z_PRIORITIES_NUM); i++) { - // ret |= _z_zint_decode(&msg->_next_sn._val._qos[i]._reliable, zbf); - // ret |= _z_zint_decode(&msg->_next_sn._val._qos[i]._best_effort, zbf); - // } - // } else { + msg->_next_sn._is_qos = false; ret |= _z_zint_decode(&msg->_next_sn._val._plain._reliable, zbf); ret |= _z_zint_decode(&msg->_next_sn._val._plain._best_effort, zbf); - // } + + if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { + ret |= _z_msg_ext_decode_iter(zbf, _z_join_decode_ext, msg); + } return ret; } diff --git a/src/system/unix/network.c b/src/system/unix/network.c index 8a71d1d82..2d5baa7fb 100644 --- a/src/system/unix/network.c +++ b/src/system/unix/network.c @@ -350,9 +350,9 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin // implementations: // https://lists.debian.org/debian-glibc/2016/03/msg00241.html // To avoid a fix to break zenoh-pico, we are let it leak for the moment. - //#if defined(ZENOH_LINUX) + // #if defined(ZENOH_LINUX) // z_free(lsockaddr); - //#endif + // #endif } else { ret = _Z_ERR_GENERIC; } diff --git a/src/system/windows/network.c b/src/system/windows/network.c index 8895b68ea..3268a0890 100644 --- a/src/system/windows/network.c +++ b/src/system/windows/network.c @@ -390,9 +390,9 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin // implementations: // https://lists.debian.org/debian-glibc/2016/03/msg00241.html // To avoid a fix to break zenoh-pico, we are let it leak for the moment. - //#if defined(ZENOH_LINUX) + // #if defined(ZENOH_LINUX) // z_free(lsockaddr); - //#endif + // #endif } else { ret = _Z_ERR_GENERIC; } diff --git a/src/system/windows/system.c b/src/system/windows/system.c index 80643d9f3..6441a3684 100644 --- a/src/system/windows/system.c +++ b/src/system/windows/system.c @@ -14,8 +14,8 @@ #include // The following includes must come after winsock2 -#include #include +#include #include "zenoh-pico/config.h" #include "zenoh-pico/system/platform.h" diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index c2b57e388..95a1c723e 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -235,7 +235,6 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t // If the new node has less representing capabilities then it is incompatible to communication if ((t_msg->_body._join._seq_num_res < Z_SN_RESOLUTION) || - (t_msg->_body._join._key_id_res < Z_KID_RESOLUTION) || (t_msg->_body._join._req_id_res < Z_REQ_RESOLUTION) || (t_msg->_body._join._batch_size < Z_BATCH_SIZE)) { ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; @@ -273,7 +272,6 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t // Check if the representing capapbilities are still the same if ((t_msg->_body._join._seq_num_res < Z_SN_RESOLUTION) || - (t_msg->_body._join._key_id_res < Z_KID_RESOLUTION) || (t_msg->_body._join._req_id_res < Z_REQ_RESOLUTION) || (t_msg->_body._join._batch_size < Z_BATCH_SIZE)) { _z_transport_peer_entry_list_drop_filter(ztm->_peers, _z_transport_peer_entry_eq, entry); diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 1c7574cfc..1e8e0997e 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -2265,10 +2265,6 @@ _z_transport_message_t gen_join_message(void) { t_msg._body._join._seq_num_res = gen_uint8() % 4; } - if (gen_bool()) { - t_msg._body._join._key_id_res = gen_uint8() % 4; - } - if (gen_bool()) { t_msg._body._join._req_id_res = gen_uint8() % 4; } @@ -2298,7 +2294,6 @@ void assert_eq_join_message(_z_t_msg_join_t *left, _z_t_msg_join_t *right, uint8 assert(left->_req_id_res == right->_req_id_res); printf("\n"); - printf(" KeyExpr ID Resolution (%hhu:%hhu)", left->_key_id_res, right->_key_id_res); assert(left->_key_id_res == right->_key_id_res); printf("\n"); From 0f0555cd9ec50713390200036a4237c7e5f79c7a Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 7 Jun 2023 15:32:42 +0200 Subject: [PATCH 092/181] ensure Z flag is set for scout messages with QoS --- src/protocol/msg.c | 9 ++++++--- src/protocol/msgcodec.c | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 8f8a53606..1a82fca2c 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -649,12 +649,15 @@ _z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _Z_SET_FLAG(msg._header, _Z_FLAG_T_JOIN_T); } - if ((msg._body._join._batch_size != _Z_DEFAULT_BATCH_SIZE) || - (msg._body._join._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || - (msg._body._join._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { + if ((Z_BATCH_SIZE != _Z_DEFAULT_BATCH_SIZE) || (Z_SN_RESOLUTION != _Z_DEFAULT_RESOLUTION_SIZE) || + (Z_REQ_RESOLUTION != _Z_DEFAULT_RESOLUTION_SIZE)) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_JOIN_S); } + if (next_sn._is_qos) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_Z); + } + msg._attachment = NULL; msg._extensions = _z_msg_ext_vec_make(0); diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 954045df2..2237a63c5 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1672,7 +1672,7 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t case _Z_MID_T_JOIN: { ret |= _z_join_encode(wbf, msg->_header, &msg->_body._join); - // ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); + ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; case _Z_MID_T_INIT: { From 4f9a9ac14a3330430a54cb14caae876be16b1b14 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 7 Jun 2023 15:53:34 +0200 Subject: [PATCH 093/181] delete attachements and transport-message-extension-vectors --- include/zenoh-pico/protocol/msg.h | 31 ------ include/zenoh-pico/protocol/msgcodec.h | 4 - src/protocol/msg.c | 58 ------------ src/protocol/msgcodec.c | 84 ----------------- tests/z_msgcodec_test.c | 126 +------------------------ 5 files changed, 3 insertions(+), 300 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index b0a671006..ca6e6634e 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -74,7 +74,6 @@ #define _Z_MID_A_PRIORITY 0x1c #define _Z_MID_A_ROUTING_CONTEXT 0x1d #define _Z_MID_A_REPLY_CONTEXT 0x1e -#define _Z_MID_A_ATTACHMENT 0x1f /*=============================*/ /* Message flags */ @@ -251,32 +250,6 @@ void _z_payload_clear(_z_payload_t *p); /*=============================*/ /* Message decorators */ /*=============================*/ -// # Attachment decorator -// -// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length -// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. -// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve -// the boundary of the serialized messages. The length is encoded as little-endian. -// In any case, the length of a message must not exceed 65_535 bytes. -// -// The Attachment can decorate any message (i.e., TransportMessage and ZenohMessage) and it allows to -// append to the message any additional information. Since the information contained in the -// Attachement is relevant only to the layer that provided them (e.g., Transport, Zenoh, User) it -// is the duty of that layer to serialize and de-serialize the attachment whenever deemed necessary. -// The attachement always contains serialized properties. -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |X|X|Z| ATTCH | -// +-+-+-+---------+ -// ~ Attachment ~ -// +---------------+ -// -typedef struct { - _z_payload_t _payload; - uint8_t _header; -} _z_attachment_t; -void _z_t_msg_clear_attachment(_z_attachment_t *a); /*------------------ ReplyContext Decorator ------------------*/ // The ReplyContext is a message decorator for either: @@ -607,7 +580,6 @@ typedef union { } _z_zenoh_body_t; typedef struct { _z_zenoh_body_t _body; - _z_attachment_t *_attachment; _z_reply_context_t *_reply_context; uint8_t _header; } _z_zenoh_message_t; @@ -1190,8 +1162,6 @@ typedef union { typedef struct { _z_transport_body_t _body; - _z_attachment_t *_attachment; - _z_msg_ext_vec_t _extensions; uint8_t _header; } _z_transport_message_t; void _z_t_msg_clear(_z_transport_message_t *msg); @@ -1225,7 +1195,6 @@ typedef union { typedef struct { _z_scouting_body_t _body; - _z_attachment_t *_attachment; uint8_t _header; } _z_scouting_message_t; void _z_s_msg_clear(_z_scouting_message_t *msg); diff --git a/include/zenoh-pico/protocol/msgcodec.h b/include/zenoh-pico/protocol/msgcodec.h index 0ed763181..6a039755d 100644 --- a/include/zenoh-pico/protocol/msgcodec.h +++ b/include/zenoh-pico/protocol/msgcodec.h @@ -118,10 +118,6 @@ int8_t _z_forget_qle_decl_encode(_z_wbuf_t *buf, uint8_t header, const _z_forget int8_t _z_forget_qle_decl_decode(_z_forget_qle_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); int8_t _z_forget_qle_decl_decode_na(_z_forget_qle_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); -int8_t _z_attachment_encode(_z_wbuf_t *buf, const _z_attachment_t *atch); -int8_t _z_attachment_decode(_z_attachment_t **atch, _z_zbuf_t *buf, uint8_t header); -int8_t _z_attachment_decode_na(_z_attachment_t *atch, _z_zbuf_t *buf, uint8_t header); - int8_t _z_reply_context_encode(_z_wbuf_t *buf, const _z_reply_context_t *rc); int8_t _z_reply_context_decode(_z_reply_context_t **rc, _z_zbuf_t *buf, uint8_t header); int8_t _z_reply_context_decode_na(_z_reply_context_t *rc, _z_zbuf_t *buf, uint8_t header); diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 1a82fca2c..fbf9e4343 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -57,8 +57,6 @@ void _z_locators_clear(_z_locator_array_t *ls) { _z_locator_array_clear(ls); } /*=============================*/ /* Message decorators */ /*=============================*/ -/*------------------ Attachment Decorator ------------------*/ -void _z_t_msg_clear_attachment(_z_attachment_t *a) { _z_payload_clear(&a->_payload); } /*------------------ ReplyContext Decorator ------------------*/ _z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, _Bool is_final) { @@ -291,7 +289,6 @@ _z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_pa _Z_SET_FLAG(msg._header, _Z_FLAG_Z_D); } - msg._attachment = NULL; msg._reply_context = NULL; return msg; @@ -312,7 +309,6 @@ _z_zenoh_message_t _z_msg_make_unit(_Bool can_be_dropped) { _Z_SET_FLAG(msg._header, _Z_FLAG_Z_D); } - msg._attachment = NULL; msg._reply_context = NULL; return msg; @@ -339,7 +335,6 @@ _z_zenoh_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id, _z_zint _Z_SET_FLAG(msg._header, _Z_FLAG_Z_K); } - msg._attachment = NULL; msg._reply_context = NULL; return msg; @@ -372,7 +367,6 @@ _z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, char *parameters, _z_zint _Z_SET_FLAG(msg._header, _Z_FLAG_Z_B); } - msg._attachment = NULL; msg._reply_context = NULL; return msg; @@ -396,10 +390,6 @@ _z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_p /*------------------ Zenoh Message ------------------*/ void _z_msg_clear(_z_zenoh_message_t *msg) { - if (msg->_attachment != NULL) { - _z_t_msg_clear_attachment(msg->_attachment); - z_free(msg->_attachment); - } if (msg->_reply_context != NULL) { _z_msg_clear_reply_context(msg->_reply_context); z_free(msg->_reply_context); @@ -591,8 +581,6 @@ _z_scouting_message_t _z_s_msg_make_scout(z_what_t what, _z_id_t zid) { msg._body._scout._what = what; msg._body._scout._zid = zid; - msg._attachment = NULL; - return msg; } @@ -618,7 +606,6 @@ _z_scouting_message_t _z_s_msg_make_hello(z_whatami_t whatami, _z_id_t zid, _z_l _Z_SET_FLAG(msg._header, _Z_FLAG_T_HELLO_L); } - msg._attachment = NULL; return msg; } @@ -658,9 +645,6 @@ _z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _Z_SET_FLAG(msg._header, _Z_FLAG_T_Z); } - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - return msg; } @@ -698,9 +682,6 @@ _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_id_t zid) _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_S); } - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - return msg; } @@ -725,9 +706,6 @@ _z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_S); } - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - return msg; } @@ -757,9 +735,6 @@ _z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial _Z_SET_FLAG(msg._header, _Z_FLAG_T_OPEN_T); } - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - return msg; } @@ -776,9 +751,6 @@ _z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial _Z_SET_FLAG(msg._header, _Z_FLAG_T_OPEN_T); } - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - return msg; } @@ -800,9 +772,6 @@ _z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_CLOSE_S); } - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - return msg; } @@ -815,9 +784,6 @@ _z_transport_message_t _z_t_msg_make_keep_alive(void) { _z_transport_message_t msg; msg._header = _Z_MID_T_KEEP_ALIVE; - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - return msg; } @@ -840,9 +806,6 @@ _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliabl msg._body._frame._messages = _z_network_message_vec_make(0); - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - return msg; } @@ -857,9 +820,6 @@ _z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_network_message_vec_ msg._body._frame._messages = messages; - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - return msg; } @@ -884,9 +844,6 @@ _z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_payload_t payload msg._body._fragment._sn = sn; msg._body._fragment._payload = payload; - msg._attachment = NULL; - msg._extensions = _z_msg_ext_vec_make(0); - return msg; } @@ -899,8 +856,6 @@ void _z_t_msg_clear_fragment(_z_t_msg_fragment_t *msg) { _z_bytes_clear(&msg->_p /*------------------ Transport Message ------------------*/ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg) { clone->_header = msg->_header; - clone->_attachment = msg->_attachment; - _z_msg_ext_vec_copy(&clone->_extensions, &msg->_extensions); uint8_t mid = _Z_MID(msg->_header); switch (mid) { @@ -939,11 +894,6 @@ void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg) { } void _z_t_msg_clear(_z_transport_message_t *msg) { - if (msg->_attachment != NULL) { - _z_t_msg_clear_attachment(msg->_attachment); - z_free(msg->_attachment); - } - uint8_t mid = _Z_MID(msg->_header); switch (mid) { case _Z_MID_T_JOIN: { @@ -978,14 +928,11 @@ void _z_t_msg_clear(_z_transport_message_t *msg) { _Z_DEBUG("WARNING: Trying to clear transport message with unknown ID(%d)\n", mid); } break; } - - _z_msg_ext_vec_clear(&msg->_extensions); } /*------------------ Scouting Message ------------------*/ void _z_s_msg_copy(_z_scouting_message_t *clone, _z_scouting_message_t *msg) { clone->_header = msg->_header; - clone->_attachment = msg->_attachment; uint8_t mid = _Z_MID(msg->_header); switch (mid) { @@ -1004,11 +951,6 @@ void _z_s_msg_copy(_z_scouting_message_t *clone, _z_scouting_message_t *msg) { } void _z_s_msg_clear(_z_scouting_message_t *msg) { - if (msg->_attachment != NULL) { - _z_t_msg_clear_attachment(msg->_attachment); - z_free(msg->_attachment); - } - uint8_t mid = _Z_MID(msg->_header); switch (mid) { case _Z_MID_SCOUT: { diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 2237a63c5..2da6d730c 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -225,52 +225,6 @@ int8_t _z_locators_decode(_z_locator_array_t *a_loc, _z_zbuf_t *zbf) { return _z /*=============================*/ /* Message decorators */ /*=============================*/ -/*------------------ Attachment Decorator ------------------*/ -int8_t _z_attachment_encode(_z_wbuf_t *wbf, const _z_attachment_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_A_ATTACHMENT\n"); - - // WARNING: we do not support sliced content in zenoh-pico. - // Disable the SLICED flag to be on the safe side. - _Z_EC(_z_wbuf_write(wbf, msg->_header & ~_Z_FLAG_A_Z)) - - ret |= _z_payload_encode(wbf, &msg->_payload); - - return ret; -} - -int8_t _z_attachment_decode_na(_z_attachment_t *atch, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_A_ATTACHMENT\n"); - int8_t ret = _Z_RES_OK; - - atch->_header = header; - - // WARNING: we do not support sliced content in zenoh-pico. - // Return error in case the payload is sliced. - if (_Z_HAS_FLAG(header, _Z_FLAG_A_Z) == false) { - ret |= _z_payload_decode(&atch->_payload, zbf); - } - - return ret; -} - -int8_t _z_attachment_decode(_z_attachment_t **atch, _z_zbuf_t *zbf, uint8_t header) { - int8_t ret = _Z_RES_OK; - - *atch = (_z_attachment_t *)z_malloc(sizeof(_z_attachment_t)); - if (atch != NULL) { - _z_attachment_t *ptr = *atch; - ret |= _z_attachment_decode_na(ptr, zbf, header); - if (ret != _Z_RES_OK) { - z_free(ptr); - *atch = NULL; - } - } else { - ret |= _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - - return ret; -} /*------------------ ReplyContext Decorator ------------------*/ int8_t _z_reply_context_encode(_z_wbuf_t *wbf, const _z_reply_context_t *msg) { @@ -850,9 +804,6 @@ int8_t _z_zenoh_message_encode(_z_wbuf_t *wbf, const _z_zenoh_message_t *msg) { int8_t ret = _Z_RES_OK; // Encode the decorators if present - if (msg->_attachment != NULL) { - _Z_EC(_z_attachment_encode(wbf, msg->_attachment)) - } if (msg->_reply_context != NULL) { _Z_EC(_z_reply_context_encode(wbf, msg->_reply_context)) } @@ -889,7 +840,6 @@ int8_t _z_zenoh_message_encode(_z_wbuf_t *wbf, const _z_zenoh_message_t *msg) { int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; - msg->_attachment = NULL; msg->_reply_context = NULL; _Bool is_last = false; do { @@ -902,10 +852,6 @@ int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { is_last = true; } break; - case _Z_MID_A_ATTACHMENT: { - ret |= _z_attachment_decode(&msg->_attachment, zbf, msg->_header); - } break; - case _Z_MID_A_REPLY_CONTEXT: { ret |= _z_reply_context_decode(&msg->_reply_context, zbf, msg->_header); } break; @@ -1642,52 +1588,39 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t int8_t ret = _Z_RES_OK; // Encode the decorators if present - if (msg->_attachment != NULL) { - _Z_EC(_z_attachment_encode(wbf, msg->_attachment)) - } uint8_t header = msg->_header; - if (_z_msg_ext_vec_is_empty(&msg->_extensions) == false) { - header |= _Z_FLAG_T_Z; - } _Z_EC(_z_wbuf_write(wbf, header)) switch (_Z_MID(msg->_header)) { case _Z_MID_T_FRAME: { ret |= _z_frame_header_encode(wbf, msg->_header, &msg->_body._frame); - ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); ret |= _z_frame_payload_encode(wbf, msg->_header, &msg->_body._frame); } break; case _Z_MID_T_FRAGMENT: { ret |= _z_fragment_header_encode(wbf, msg->_header, &msg->_body._fragment); - ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); ret |= _z_fragment_payload_encode(wbf, msg->_header, &msg->_body._fragment); } break; case _Z_MID_T_KEEP_ALIVE: { ret |= _z_keep_alive_encode(wbf, msg->_header, &msg->_body._keep_alive); - ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; case _Z_MID_T_JOIN: { ret |= _z_join_encode(wbf, msg->_header, &msg->_body._join); - ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; case _Z_MID_T_INIT: { ret |= _z_init_encode(wbf, msg->_header, &msg->_body._init); - ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; case _Z_MID_T_OPEN: { ret |= _z_open_encode(wbf, msg->_header, &msg->_body._open); - ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; case _Z_MID_T_CLOSE: { ret |= _z_close_encode(wbf, msg->_header, &msg->_body._close); - ret |= _z_extensions_encode(wbf, msg->_header, &msg->_extensions); } break; default: { @@ -1702,7 +1635,6 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; - msg->_attachment = NULL; _Bool is_last = false; do { @@ -1712,50 +1644,38 @@ int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *zb switch (mid) { case _Z_MID_T_FRAME: { ret |= _z_frame_header_decode(&msg->_body._frame, zbf, msg->_header); - ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); ret |= _z_frame_payload_decode(&msg->_body._frame, zbf, msg->_header); is_last = true; } break; case _Z_MID_T_FRAGMENT: { ret |= _z_fragment_header_decode(&msg->_body._fragment, zbf, msg->_header); - ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); ret |= _z_fragment_payload_decode(&msg->_body._fragment, zbf, msg->_header); is_last = true; } break; - case _Z_MID_A_ATTACHMENT: { - ret |= _z_attachment_decode(&msg->_attachment, zbf, msg->_header); - ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); - } break; - case _Z_MID_T_KEEP_ALIVE: { ret |= _z_keep_alive_decode(&msg->_body._keep_alive, zbf, msg->_header); - ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; case _Z_MID_T_JOIN: { ret |= _z_join_decode(&msg->_body._join, zbf, msg->_header); - ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; case _Z_MID_T_INIT: { ret |= _z_init_decode(&msg->_body._init, zbf, msg->_header); - ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; case _Z_MID_T_OPEN: { ret |= _z_open_decode(&msg->_body._open, zbf, msg->_header); - ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; case _Z_MID_T_CLOSE: { ret |= _z_close_decode(&msg->_body._close, zbf, msg->_header); - ret |= _z_extensions_decode(&msg->_extensions, zbf, msg->_header); is_last = true; } break; @@ -1888,9 +1808,6 @@ int8_t _z_scouting_message_encode(_z_wbuf_t *wbf, const _z_scouting_message_t *m int8_t ret = _Z_RES_OK; // Encode the decorators if present - if (msg->_attachment != NULL) { - _Z_EC(_z_attachment_encode(wbf, msg->_attachment)) - } uint8_t header = msg->_header; // size_t n_ext = _z_msg_ext_vec_len(&msg->_extensions); @@ -1921,7 +1838,6 @@ int8_t _z_scouting_message_encode(_z_wbuf_t *wbf, const _z_scouting_message_t *m int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; - msg->_attachment = NULL; _Bool is_last = false; do { diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 1e8e0997e..bc96bc96c 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -856,61 +856,6 @@ void data_info_field(void) { /*=============================*/ /* Message decorators */ /*=============================*/ -/*------------------ Attachment decorator ------------------*/ -void print_attachment(_z_attachment_t *att) { - printf(" Header: %x\n", att->_header); - printf(" Payload: "); - print_uint8_array(&att->_payload); - printf("\n"); -} - -_z_attachment_t *gen_attachment(void) { - _z_attachment_t *p_at = (_z_attachment_t *)z_malloc(sizeof(_z_attachment_t)); - - p_at->_header = _Z_MID_A_ATTACHMENT; - // _Z_SET_FLAG(p_at->_header, _Z_FLAGS(gen_uint8())); - p_at->_payload = gen_payload(64); - - return p_at; -} - -void assert_eq_attachment(_z_attachment_t *left, _z_attachment_t *right) { - printf("Header (%x:%x), ", left->_header, right->_header); - assert(left->_header == right->_header); - assert_eq_payload(&left->_payload, &right->_payload); -} - -void attachment_decorator(void) { - printf("\n>> Attachment decorator\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_attachment_t *e_at = gen_attachment(); - - // Encode - int8_t res = _z_attachment_encode(&wbf, e_at); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - uint8_t header = _z_zbuf_read(&zbf); - _z_attachment_t *d_at = NULL; - res = _z_attachment_decode(&d_at, &zbf, header); - assert(res == _Z_RES_OK); - - printf(" "); - assert_eq_attachment(e_at, d_at); - printf("\n"); - - // Free - _z_t_msg_clear_attachment(e_at); - _z_t_msg_clear_attachment(d_at); - z_free(e_at); - z_free(d_at); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} /*------------------ ReplyContext decorator ------------------*/ void print_reply_context(_z_reply_context_t *rc) { @@ -1681,29 +1626,17 @@ _z_zenoh_message_t gen_zenoh_message(void) { break; } - if (gen_bool()) - p_zm._attachment = gen_attachment(); - else - p_zm._attachment = NULL; - - if (gen_bool()) + if (gen_bool()) { p_zm._reply_context = gen_reply_context(); - else + } else { p_zm._reply_context = NULL; + } return p_zm; } void assert_eq_zenoh_message(_z_zenoh_message_t *left, _z_zenoh_message_t *right) { // Test message decorators - if (left->_attachment && right->_attachment) { - printf(" "); - assert_eq_attachment(left->_attachment, right->_attachment); - printf("\n"); - } else { - printf(" Attachment: %p:%p\n", (void *)left->_attachment, (void *)right->_attachment); - assert(left->_attachment == right->_attachment); - } if (left->_reply_context && right->_reply_context) { printf(" "); @@ -1763,11 +1696,6 @@ void zenoh_message(void) { assert(0); break; } - if (e_zm._attachment) { - printf(" Attachment\n"); - print_attachment(e_zm._attachment); - printf("\n"); - } if (e_zm._reply_context) { printf(" Reply Context\n"); print_reply_context(e_zm._reply_context); @@ -2651,7 +2579,6 @@ void frame_message(void) { // Encode int8_t res = _z_frame_header_encode(&wbf, t_msg._header, &e_fr); - res |= _z_extensions_encode(&wbf, t_msg._header, &t_msg._extensions); res |= _z_frame_payload_encode(&wbf, t_msg._header, &e_fr); assert(res == _Z_RES_OK); (void)(res); @@ -2660,7 +2587,6 @@ void frame_message(void) { _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); _z_t_msg_frame_t d_fr; res = _z_frame_header_decode(&d_fr, &zbf, t_msg._header); - res |= _z_extensions_decode(&t_msg._extensions, &zbf, t_msg._header); res |= _z_frame_payload_decode(&d_fr, &zbf, t_msg._header); assert(res == _Z_RES_OK); @@ -2705,7 +2631,6 @@ void fragment_message(void) { // Encode int8_t res = _z_fragment_header_encode(&wbf, t_msg._header, &e_fr); - res |= _z_extensions_encode(&wbf, t_msg._header, &t_msg._extensions); res |= _z_fragment_payload_encode(&wbf, t_msg._header, &e_fr); assert(res == _Z_RES_OK); (void)(res); @@ -2714,7 +2639,6 @@ void fragment_message(void) { _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); _z_t_msg_fragment_t d_fr; res = _z_fragment_header_decode(&d_fr, &zbf, t_msg._header); - res |= _z_extensions_decode(&t_msg._extensions, &zbf, t_msg._header); res |= _z_fragment_payload_decode(&d_fr, &zbf, t_msg._header); assert(res == _Z_RES_OK); @@ -2762,30 +2686,11 @@ _z_transport_message_t gen_transport_message(void) { break; } - if (gen_bool()) { - e_tm._attachment = gen_attachment(); - } else { - e_tm._attachment = NULL; - } - return e_tm; } void assert_eq_transport_message(_z_transport_message_t *left, _z_transport_message_t *right) { // FIXME[protocol]: This is here to set the extensions flags that is only known at encoding time - if (_z_msg_ext_vec_len(&left->_extensions) > (size_t)0) { - left->_header |= _Z_FLAG_T_Z; - } - - // Test message decorators - if (left->_attachment && right->_attachment) { - printf(" "); - assert_eq_attachment(left->_attachment, right->_attachment); - printf("\n"); - } else { - assert(left->_attachment == right->_attachment); - } - // Test message printf(" Header (%x:%x)", left->_header, right->_header); assert(left->_header == right->_header); @@ -2817,15 +2722,6 @@ void assert_eq_transport_message(_z_transport_message_t *left, _z_transport_mess assert(0); break; } - - size_t left_n_ext = _z_msg_ext_vec_len(&left->_extensions); - size_t right_n_ext = _z_msg_ext_vec_len(&right->_extensions); - printf(" # of extensions (%zu:%zu)", left_n_ext, right_n_ext); - assert(left_n_ext == right_n_ext); - for (size_t i = 0; i < left_n_ext; i++) { - assert_eq_message_extension(_z_msg_ext_vec_get(&left->_extensions, i), - _z_msg_ext_vec_get(&right->_extensions, i)); - } } void transport_message(void) { @@ -2877,12 +2773,6 @@ _z_scouting_message_t gen_scouting_message(void) { break; } - if (gen_bool()) { - e_sm._attachment = gen_attachment(); - } else { - e_sm._attachment = NULL; - } - return e_sm; } @@ -2892,15 +2782,6 @@ void assert_eq_scouting_message(_z_scouting_message_t *left, _z_scouting_message // left->_header |= _Z_FLAG_T_Z; // } - // Test message decorators - if (left->_attachment && right->_attachment) { - printf(" "); - assert_eq_attachment(left->_attachment, right->_attachment); - printf("\n"); - } else { - assert(left->_attachment == right->_attachment); - } - // Test message printf(" Header (%x:%x)", left->_header, right->_header); assert(left->_header == right->_header); @@ -3196,7 +3077,6 @@ int main(void) { data_info_field(); // Message decorators - attachment_decorator(); reply_contex_decorator(); // Zenoh declarations From 8772935756243b8b782d9cd8107b219c9cc11608 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Thu, 8 Jun 2023 09:37:12 +0200 Subject: [PATCH 094/181] transport layer audited --- include/zenoh-pico/config.h | 4 - include/zenoh-pico/protocol/msg.h | 41 +--- src/protocol/msg.c | 45 +---- src/protocol/msgcodec.c | 320 +++++++++--------------------- tests/z_msgcodec_test.c | 1 - 5 files changed, 99 insertions(+), 312 deletions(-) diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index 09c916884..5ce74f664 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -155,10 +155,6 @@ #define Z_SN_RESOLUTION 0x02 #endif -#ifndef Z_KID_RESOLUTION -#define Z_KID_RESOLUTION 0x02 -#endif - #ifndef Z_REQ_RESOLUTION #define Z_REQ_RESOLUTION 0x02 #endif diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index ca6e6634e..ce7cd4827 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -16,7 +16,9 @@ #define ZENOH_PICO_PROTOCOL_MSG_H #include +#include +#include "zenoh-pico/api/constants.h" #include "zenoh-pico/collections/array.h" #include "zenoh-pico/collections/element.h" #include "zenoh-pico/collections/string.h" @@ -70,11 +72,6 @@ #define _Z_MID_Z_UNIT 0x0f #define _Z_MID_Z_LINK_STATE_LIST 0x10 -/* Message decorators */ -#define _Z_MID_A_PRIORITY 0x1c -#define _Z_MID_A_ROUTING_CONTEXT 0x1d -#define _Z_MID_A_REPLY_CONTEXT 0x1e - /*=============================*/ /* Message flags */ /*=============================*/ @@ -159,10 +156,6 @@ // #define _Z_FLAG_N_RESPONSE_X 0x20 // 1 << 5 // #define _Z_FLAG_N_RESPONSE_X 0x40 // 1 << 6 -/* Attachment message flags */ -#define _Z_FLAG_A_Z \ - 0x20 // 1 << 5 | MixedSlices if Z==1 then the payload contains a mix of raw and shm_info payload - /* Zenoh message flags */ #define _Z_FLAG_Z_B 0x40 // 1 << 6 | QueryPayload if B==1 then QueryPayload is present #define _Z_FLAG_Z_D 0x20 // 1 << 5 | Dropping if D==1 then the message can be dropped @@ -251,30 +244,6 @@ void _z_payload_clear(_z_payload_t *p); /* Message decorators */ /*=============================*/ -/*------------------ ReplyContext Decorator ------------------*/ -// The ReplyContext is a message decorator for either: -// - the Data messages that results from a query -// - or a Unit message in case the message is a REPLY_FINAL. -// The replier-id (eval or storage id) is represented as a byte-array. -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |X|X|F| R_CTX | -// +-+-+-+---------+ -// ~ qid ~ -// +---------------+ -// ~ replier_id ~ if F==0 -// +---------------+ -// -// - if F==1 then the message is a REPLY_FINAL -// -typedef struct { - _z_id_t _replier_id; - _z_zint_t _qid; - uint8_t _header; -} _z_reply_context_t; -void _z_msg_clear_reply_context(_z_reply_context_t *rc); - // -- Priority decorator // // The Priority is a message decorator containing @@ -580,7 +549,6 @@ typedef union { } _z_zenoh_body_t; typedef struct { _z_zenoh_body_t _body; - _z_reply_context_t *_reply_context; uint8_t _header; } _z_zenoh_message_t; void _z_msg_clear(_z_zenoh_message_t *m); @@ -589,7 +557,6 @@ _Z_ELEM_DEFINE(_z_zenoh_message, _z_zenoh_message_t, _z_noop_size, _z_msg_clear, _Z_VEC_DEFINE(_z_zenoh_message, _z_zenoh_message_t) /*------------------ Builders ------------------*/ -_z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, _Bool is_final); _z_declaration_t _z_msg_make_declaration_resource(_z_zint_t id, _z_keyexpr_t key); _z_declaration_t _z_msg_make_declaration_forget_resource(_z_zint_t rid); _z_declaration_t _z_msg_make_declaration_publisher(_z_keyexpr_t key); @@ -603,8 +570,7 @@ _z_zenoh_message_t _z_msg_make_unit(_Bool can_be_dropped); _z_zenoh_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id, _z_zint_t max_samples, _Bool is_final); _z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, char *parameters, _z_zint_t qid, z_query_target_t target, z_consolidation_mode_t consolidation, _z_value_t value); -_z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_payload_t payload, _Bool can_be_dropped, - _z_reply_context_t *rctx); +_z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_payload_t payload, _Bool can_be_dropped); /*=============================*/ /* Network Messages */ @@ -988,7 +954,6 @@ typedef struct { _z_bytes_t _cookie; uint16_t _batch_size; z_whatami_t _whatami; - uint8_t _key_id_res; uint8_t _req_id_res; uint8_t _seq_num_res; uint8_t _version; diff --git a/src/protocol/msg.c b/src/protocol/msg.c index fbf9e4343..1f0bd0c53 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -54,28 +54,6 @@ void _z_keyexpr_free(_z_keyexpr_t **rk) { /*------------------ Locators Field ------------------*/ void _z_locators_clear(_z_locator_array_t *ls) { _z_locator_array_clear(ls); } -/*=============================*/ -/* Message decorators */ -/*=============================*/ - -/*------------------ ReplyContext Decorator ------------------*/ -_z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, _Bool is_final) { - _z_reply_context_t *rctx = (_z_reply_context_t *)z_malloc(sizeof(_z_reply_context_t)); - if (rctx != NULL) { - rctx->_qid = qid; - rctx->_replier_id = replier_id; - - rctx->_header = _Z_MID_A_REPLY_CONTEXT; - if (is_final == true) { - _Z_SET_FLAG(rctx->_header, _Z_FLAG_Z_F); - } - } - - return rctx; -} - -void _z_msg_clear_reply_context(_z_reply_context_t *rc) {} - /*=============================*/ /* Zenoh Messages */ /*=============================*/ @@ -289,8 +267,6 @@ _z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_pa _Z_SET_FLAG(msg._header, _Z_FLAG_Z_D); } - msg._reply_context = NULL; - return msg; } @@ -309,8 +285,6 @@ _z_zenoh_message_t _z_msg_make_unit(_Bool can_be_dropped) { _Z_SET_FLAG(msg._header, _Z_FLAG_Z_D); } - msg._reply_context = NULL; - return msg; } @@ -335,8 +309,6 @@ _z_zenoh_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id, _z_zint _Z_SET_FLAG(msg._header, _Z_FLAG_Z_K); } - msg._reply_context = NULL; - return msg; } @@ -367,8 +339,6 @@ _z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, char *parameters, _z_zint _Z_SET_FLAG(msg._header, _Z_FLAG_Z_B); } - msg._reply_context = NULL; - return msg; } @@ -380,21 +350,15 @@ void _z_msg_clear_query(_z_msg_query_t *msg) { } /*------------------ Reply Message ------------------*/ -_z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_payload_t payload, _Bool can_be_dropped, - _z_reply_context_t *rctx) { +_z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_payload_t payload, + _Bool can_be_dropped) { _z_zenoh_message_t msg = _z_msg_make_data(key, info, payload, can_be_dropped); - msg._reply_context = rctx; return msg; } /*------------------ Zenoh Message ------------------*/ void _z_msg_clear(_z_zenoh_message_t *msg) { - if (msg->_reply_context != NULL) { - _z_msg_clear_reply_context(msg->_reply_context); - z_free(msg->_reply_context); - } - uint8_t mid = _Z_MID(msg->_header); switch (mid) { case _Z_MID_Z_DATA: @@ -670,14 +634,12 @@ _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_id_t zid) msg._body._init._whatami = whatami; msg._body._init._zid = zid; msg._body._init._seq_num_res = Z_SN_RESOLUTION; - msg._body._init._key_id_res = Z_KID_RESOLUTION; msg._body._init._req_id_res = Z_REQ_RESOLUTION; msg._body._init._batch_size = Z_BATCH_SIZE; _z_bytes_reset(&msg._body._init._cookie); if ((msg._body._init._batch_size != _Z_DEFAULT_BATCH_SIZE) || (msg._body._init._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || - (msg._body._init._key_id_res != _Z_DEFAULT_RESOLUTION_SIZE) || (msg._body._init._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_S); } @@ -694,14 +656,12 @@ _z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, msg._body._init._whatami = whatami; msg._body._init._zid = zid; msg._body._init._seq_num_res = Z_SN_RESOLUTION; - msg._body._init._key_id_res = Z_KID_RESOLUTION; msg._body._init._req_id_res = Z_REQ_RESOLUTION; msg._body._init._batch_size = Z_BATCH_SIZE; msg._body._init._cookie = cookie; if ((msg._body._init._batch_size != _Z_DEFAULT_BATCH_SIZE) || (msg._body._init._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || - (msg._body._init._key_id_res != _Z_DEFAULT_RESOLUTION_SIZE) || (msg._body._init._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_S); } @@ -713,7 +673,6 @@ void _z_t_msg_copy_init(_z_t_msg_init_t *clone, _z_t_msg_init_t *msg) { clone->_version = msg->_version; clone->_whatami = msg->_whatami; clone->_seq_num_res = msg->_seq_num_res; - clone->_key_id_res = msg->_key_id_res; clone->_req_id_res = msg->_req_id_res; clone->_batch_size = msg->_batch_size; memcpy(clone->_zid.id, msg->_zid.id, 16); diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 2da6d730c..d53e6eddd 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -18,6 +18,7 @@ #include #include +#include "zenoh-pico/api/constants.h" #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/link/endpoint.h" #include "zenoh-pico/protocol/core.h" @@ -222,59 +223,6 @@ int8_t _z_locators_decode_na(_z_locator_array_t *a_loc, _z_zbuf_t *zbf) { int8_t _z_locators_decode(_z_locator_array_t *a_loc, _z_zbuf_t *zbf) { return _z_locators_decode_na(a_loc, zbf); } -/*=============================*/ -/* Message decorators */ -/*=============================*/ - -/*------------------ ReplyContext Decorator ------------------*/ -int8_t _z_reply_context_encode(_z_wbuf_t *wbf, const _z_reply_context_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_A_REPLY_CONTEXT\n"); - - _Z_EC(_z_wbuf_write(wbf, msg->_header)) - - _Z_EC(_z_zint_encode(wbf, msg->_qid)) - if (_Z_HAS_FLAG(msg->_header, _Z_FLAG_Z_F) == false) { - _Z_EC(_z_id_encode_as_zbytes(wbf, &msg->_replier_id)) - } - - return ret; -} - -int8_t _z_reply_context_decode_na(_z_reply_context_t *rc, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_A_REPLY_CONTEXT\n"); - int8_t ret = _Z_RES_OK; - - rc->_header = header; - - ret |= _z_zint_decode(&rc->_qid, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_F) == false) { - ret |= _z_id_decode_as_zbytes(&rc->_replier_id, zbf); - } else { - rc->_replier_id = _z_id_empty(); - } - - return ret; -} - -int8_t _z_reply_context_decode(_z_reply_context_t **rc, _z_zbuf_t *zbf, uint8_t header) { - int8_t ret = _Z_RES_OK; - - *rc = (_z_reply_context_t *)z_malloc(sizeof(_z_reply_context_t)); - if (rc != NULL) { - _z_reply_context_t *ptr = *rc; - ret |= _z_reply_context_decode_na(ptr, zbf, header); - if (ret != _Z_RES_OK) { - z_free(ptr); - *rc = NULL; - } - } else { - ret |= _Z_ERR_SYSTEM_OUT_OF_MEMORY; - } - - return ret; -} - /*=============================*/ /* Zenoh Messages */ /*=============================*/ @@ -803,11 +751,6 @@ int8_t _z_query_decode(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t _z_zenoh_message_encode(_z_wbuf_t *wbf, const _z_zenoh_message_t *msg) { int8_t ret = _Z_RES_OK; - // Encode the decorators if present - if (msg->_reply_context != NULL) { - _Z_EC(_z_reply_context_encode(wbf, msg->_reply_context)) - } - _Z_EC(_z_wbuf_write(wbf, msg->_header)) uint8_t mid = _Z_MID(msg->_header); @@ -840,7 +783,6 @@ int8_t _z_zenoh_message_encode(_z_wbuf_t *wbf, const _z_zenoh_message_t *msg) { int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; - msg->_reply_context = NULL; _Bool is_last = false; do { ret |= _z_uint8_decode(&msg->_header, zbf); @@ -852,10 +794,6 @@ int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { is_last = true; } break; - case _Z_MID_A_REPLY_CONTEXT: { - ret |= _z_reply_context_decode(&msg->_reply_context, zbf, msg->_header); - } break; - case _Z_MID_Z_QUERY: { ret |= _z_query_decode(&msg->_body._query, zbf, msg->_header); is_last = true; @@ -871,11 +809,6 @@ int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { is_last = true; } break; - case _Z_MID_A_PRIORITY: { - // Ignore the priority decorator for the time being since zenoh-pico does not - // perform any routing. Hence, priority information does not need to be propagated. - } break; - case _Z_MID_Z_LINK_STATE_LIST: { _Z_DEBUG("WARNING: Link state not supported in zenoh-pico\n"); is_last = true; @@ -1261,7 +1194,6 @@ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg cbyte = 0; cbyte |= (msg->_seq_num_res & 0x03); cbyte |= ((msg->_req_id_res & 0x03) << 2); - cbyte |= ((msg->_key_id_res & 0x03) << 4); _Z_EC(_z_uint8_encode(wbf, cbyte)) _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) } @@ -1289,17 +1221,15 @@ int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { _z_zbuf_read_bytes(zbf, msg->_zid.id, 0, zidlen); } - if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_S) == true) { + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_S) == true)) { cbyte = 0; ret |= _z_uint8_decode(&cbyte, zbf); msg->_seq_num_res = (cbyte & 0x03); msg->_req_id_res = ((cbyte >> 2) & 0x03); - msg->_key_id_res = ((cbyte >> 4) & 0x03); ret |= _z_uint16_decode(&msg->_batch_size, zbf); } else { msg->_seq_num_res = _Z_DEFAULT_RESOLUTION_SIZE; msg->_req_id_res = _Z_DEFAULT_RESOLUTION_SIZE; - msg->_key_id_res = _Z_DEFAULT_RESOLUTION_SIZE; msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; } @@ -1312,6 +1242,10 @@ int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { msg->_cookie = _z_bytes_empty(); } + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { + ret |= _z_msg_ext_skip_non_mandatories(zbf); + } + return ret; } @@ -1344,7 +1278,7 @@ int8_t _z_open_decode_na(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; ret |= _z_zint_decode(&msg->_lease, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_T) == true) { + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_T) == true)) { msg->_lease = msg->_lease * 1000; } @@ -1358,6 +1292,9 @@ int8_t _z_open_decode_na(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header) { } else { msg->_cookie = _z_bytes_empty(); } + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { + ret |= _z_msg_ext_skip_non_mandatories(zbf); + } return ret; } @@ -1411,6 +1348,8 @@ int8_t _z_keep_alive_decode_na(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8 int8_t ret = _Z_RES_OK; _Z_DEBUG("Decoding _Z_MID_T_KEEP_ALIVE\n"); + ret |= _z_msg_ext_skip_non_mandatories(zbf); + return ret; } @@ -1419,139 +1358,89 @@ int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t } /*------------------ Frame Message ------------------*/ -int8_t _z_frame_header_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg) { - (void)(header); +int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_T_FRAME_HEADER\n"); _Z_EC(_z_zint_encode(wbf, msg->_sn)) - return ret; -} - -int8_t _z_frame_payload_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg) { - (void)(header); - - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_T_FRAME_PAYLOAD\n"); - - size_t len = _z_network_message_vec_len(&msg->_messages); - for (size_t i = 0; i < len; i++) { - _Z_EC(_z_network_message_encode(wbf, _z_network_message_vec_get(&msg->_messages, i))) + if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { + ret = _Z_ERR_MESSAGE_SERIALIZATION_FAILED; + } + if (ret == _Z_RES_OK) { + size_t len = _z_network_message_vec_len(&msg->_messages); + for (size_t i = 0; i < len; i++) { + _Z_EC(_z_network_message_encode(wbf, _z_network_message_vec_get(&msg->_messages, i))) + } } return ret; } -int8_t _z_frame_header_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { - (void)(header); - +int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_T_FRAME_HEADER\n"); ret |= _z_zint_decode(&msg->_sn, zbf); - - return ret; -} - -int8_t _z_frame_payload_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { - (void)(header); - - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_T_FRAME_PAYLOAD\n"); - - msg->_messages = _z_network_message_vec_make(_ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE); - while (_z_zbuf_len(zbf) > 0) { - // Mark the reading position of the iobfer - size_t r_pos = _z_zbuf_get_rpos(zbf); - _z_network_message_t *nm = (_z_network_message_t *)z_malloc(sizeof(_z_network_message_t)); - ret |= _z_network_message_decode(nm, zbf); - if (ret == _Z_RES_OK) { - _z_network_message_vec_append(&msg->_messages, nm); - } else { - _z_n_msg_free(&nm); - - _z_zbuf_set_rpos(zbf, r_pos); // Restore the reading position of the iobfer - - // FIXME: Check for the return error, since not all of them means a decoding error - // in this particular case. As of now, we roll-back the reading position - // and return to the Zenoh transport-level decoder. - // https://github.com/eclipse-zenoh/zenoh-pico/pull/132#discussion_r1045593602 - if ((ret & _Z_ERR_MESSAGE_ZENOH_UNKNOWN) == _Z_ERR_MESSAGE_ZENOH_UNKNOWN) { - ret = _Z_RES_OK; + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { + ret |= _z_msg_ext_skip_non_mandatories(zbf); + } + if (ret == _Z_RES_OK) { + msg->_messages = _z_network_message_vec_make(_ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE); + while (_z_zbuf_len(zbf) > 0) { + // Mark the reading position of the iobfer + size_t r_pos = _z_zbuf_get_rpos(zbf); + _z_network_message_t *nm = (_z_network_message_t *)z_malloc(sizeof(_z_network_message_t)); + ret |= _z_network_message_decode(nm, zbf); + if (ret == _Z_RES_OK) { + _z_network_message_vec_append(&msg->_messages, nm); + } else { + _z_n_msg_free(&nm); + + _z_zbuf_set_rpos(zbf, r_pos); // Restore the reading position of the iobfer + + // FIXME: Check for the return error, since not all of them means a decoding error + // in this particular case. As of now, we roll-back the reading position + // and return to the Zenoh transport-level decoder. + // https://github.com/eclipse-zenoh/zenoh-pico/pull/132#discussion_r1045593602 + if ((ret & _Z_ERR_MESSAGE_ZENOH_UNKNOWN) == _Z_ERR_MESSAGE_ZENOH_UNKNOWN) { + ret = _Z_RES_OK; + } + break; } - break; } } - return ret; } -int8_t _z_frame_header_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_frame_header_decode_na(msg, zbf, header); -} - -int8_t _z_frame_payload_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_frame_payload_decode_na(msg, zbf, header); -} - /*------------------ Fragment Message ------------------*/ -int8_t _z_fragment_header_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg) { - (void)(header); - +int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_T_FRAGMENT_HEADER\n"); - + _Z_DEBUG("Encoding _Z_TRANSPORT_FRAGMENT\n"); _Z_EC(_z_zint_encode(wbf, msg->_sn)) + if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { + ret = _Z_ERR_MESSAGE_SERIALIZATION_FAILED; + } + if (ret == _Z_RES_OK) { + _Z_EC(_z_bytes_encode(wbf, &msg->_payload)) + } return ret; } -int8_t _z_fragment_payload_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg) { - (void)(header); - - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_T_FRAGMENT_PAYLOAD\n"); - - _Z_EC(_z_bytes_encode(wbf, &msg->_payload)) - - return ret; -} - -int8_t _z_fragment_header_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { - (void)(header); - +int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_T_FRAGMENT_HEADER\n"); - + _Z_DEBUG("Decoding _Z_TRANSPORT_FRAGMENT\n"); ret |= _z_zint_decode(&msg->_sn, zbf); - return ret; -} - -int8_t _z_fragment_payload_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { - (void)(header); - - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_T_FRAGMENT_PAYLOAD\n"); + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { + ret |= _z_msg_ext_skip_non_mandatories(zbf); + } ret |= _z_bytes_decode(&msg->_payload, zbf); - if (ret != _Z_RES_OK) { - msg->_payload = _z_bytes_empty(); - } return ret; } -int8_t _z_fragment_header_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_fragment_header_decode_na(msg, zbf, header); -} - -int8_t _z_fragment_payload_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_fragment_payload_decode_na(msg, zbf, header); -} - /*------------------ Transport Extensions Message ------------------*/ int8_t _z_extensions_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_ext_vec_t *v_ext) { (void)(header); @@ -1594,13 +1483,11 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t _Z_EC(_z_wbuf_write(wbf, header)) switch (_Z_MID(msg->_header)) { case _Z_MID_T_FRAME: { - ret |= _z_frame_header_encode(wbf, msg->_header, &msg->_body._frame); - ret |= _z_frame_payload_encode(wbf, msg->_header, &msg->_body._frame); + ret |= _z_frame_encode(wbf, msg->_header, &msg->_body._frame); } break; case _Z_MID_T_FRAGMENT: { - ret |= _z_fragment_header_encode(wbf, msg->_header, &msg->_body._fragment); - ret |= _z_fragment_payload_encode(wbf, msg->_header, &msg->_body._fragment); + ret |= _z_fragment_encode(wbf, msg->_header, &msg->_body._fragment); } break; case _Z_MID_T_KEEP_ALIVE: { @@ -1635,65 +1522,46 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; - _Bool is_last = false; - - do { - ret |= _z_uint8_decode(&msg->_header, zbf); // Decode the header - if (ret == _Z_RES_OK) { - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_T_FRAME: { - ret |= _z_frame_header_decode(&msg->_body._frame, zbf, msg->_header); - ret |= _z_frame_payload_decode(&msg->_body._frame, zbf, msg->_header); - is_last = true; - } break; - - case _Z_MID_T_FRAGMENT: { - ret |= _z_fragment_header_decode(&msg->_body._fragment, zbf, msg->_header); - ret |= _z_fragment_payload_decode(&msg->_body._fragment, zbf, msg->_header); - is_last = true; - } break; + ret |= _z_uint8_decode(&msg->_header, zbf); // Decode the header + if (ret == _Z_RES_OK) { + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_T_FRAME: { + ret |= _z_frame_decode(&msg->_body._frame, zbf, msg->_header); + } break; - case _Z_MID_T_KEEP_ALIVE: { - ret |= _z_keep_alive_decode(&msg->_body._keep_alive, zbf, msg->_header); - is_last = true; - } break; + case _Z_MID_T_FRAGMENT: { + ret |= _z_fragment_decode(&msg->_body._fragment, zbf, msg->_header); + } break; - case _Z_MID_T_JOIN: { - ret |= _z_join_decode(&msg->_body._join, zbf, msg->_header); - is_last = true; - } break; + case _Z_MID_T_KEEP_ALIVE: { + ret |= _z_keep_alive_decode(&msg->_body._keep_alive, zbf, msg->_header); + } break; - case _Z_MID_T_INIT: { - ret |= _z_init_decode(&msg->_body._init, zbf, msg->_header); - is_last = true; - } break; + case _Z_MID_T_JOIN: { + ret |= _z_join_decode(&msg->_body._join, zbf, msg->_header); + } break; - case _Z_MID_T_OPEN: { - ret |= _z_open_decode(&msg->_body._open, zbf, msg->_header); - is_last = true; - } break; + case _Z_MID_T_INIT: { + ret |= _z_init_decode(&msg->_body._init, zbf, msg->_header); + } break; - case _Z_MID_T_CLOSE: { - ret |= _z_close_decode(&msg->_body._close, zbf, msg->_header); - is_last = true; - } break; + case _Z_MID_T_OPEN: { + ret |= _z_open_decode(&msg->_body._open, zbf, msg->_header); + } break; - case _Z_MID_A_PRIORITY: { - // Ignore the priority decorator for the time being since zenoh-pico does not - // perform any routing. Hence, priority information does not need to be propagated. - } break; + case _Z_MID_T_CLOSE: { + ret |= _z_close_decode(&msg->_body._close, zbf, msg->_header); + } break; - default: { - _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(%d)\n", mid); - ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; - is_last = true; - } break; - } - } else { - msg->_header = 0xFF; + default: { + _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(%d)\n", mid); + ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; + } break; } - } while ((ret == _Z_RES_OK) && (is_last == false)); + } else { + msg->_header = 0xFF; + } return ret; } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index bc96bc96c..bc29f3b20 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -2901,7 +2901,6 @@ void batch(void) { // _z_transport_message_t z_fragment_header(_Bool is_reliable, _Bool is_fragment, _Bool is_final, _z_zint_t sn) { // // Create the frame session message that carries the zenoh message // _z_transport_message_t t_msg; -// t_msg._attachment = NULL; // t_msg._header = _Z_MID_T_FRAME; // t_msg._body._frame._sn = sn; From 24d833d122277bb03a43b5530edaa8e720912dfc Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Thu, 8 Jun 2023 09:41:02 +0200 Subject: [PATCH 095/181] purge some unused macros --- include/zenoh-pico/protocol/msgcodec.h | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/include/zenoh-pico/protocol/msgcodec.h b/include/zenoh-pico/protocol/msgcodec.h index 6a039755d..1ca9934bb 100644 --- a/include/zenoh-pico/protocol/msgcodec.h +++ b/include/zenoh-pico/protocol/msgcodec.h @@ -23,26 +23,6 @@ #include "zenoh-pico/protocol/msg.h" #include "zenoh-pico/utils/result.h" -/*------------------ Internal Zenoh-net Macros ------------------*/ -#define _Z_DECLARE_ENCODE(name) int8_t _z_##name##_encode(_z_wbuf_t *wbf, uint8_t header, const _z_##name##_t *m); -#define _Z_DECLARE_ENCODE_NOH(name) int8_t _z_##name##_encode(_z_wbuf_t *wbf, const _z_##name##_t *m); - -#define _Z_DECLARE_DECODE_NEW(name) \ - int8_t _z_##name##_decode(_z_##name##_t *t, _z_zbuf_t *zbf, uint8_t header); \ - int8_t _z_##name##_decode_na(_z_##name##_t *t, _z_zbuf_t *zbf, uint8_t header); - -#define _Z_DECLARE_DECODE_NOH_NEW(name) \ - int8_t _z_##name##_decode(_z_##name##_t *t, _z_zbuf_t *zbf); \ - int8_t _z_##name##_decode_na(_z_##name##_t *t, _z_zbuf_t *zbf); - -#define _Z_DECLARE_DECODE(name) \ - _z_##name##_result_t _z_##name##_decode(_z_zbuf_t *zbf, uint8_t header); \ - void _z_##name##_decode_na(_z_zbuf_t *zbf, uint8_t header, _z_##name##_result_t *r); - -#define _Z_DECLARE_DECODE_NOH(name) \ - _z_##name##_result_t _z_##name##_decode(_z_zbuf_t *zbf); \ - void _z_##name##_decode_na(_z_zbuf_t *zbf, _z_##name##_result_t *r); - /*------------------ Zenoh Message ------------------*/ int8_t _z_scouting_message_encode(_z_wbuf_t *buf, const _z_scouting_message_t *msg); int8_t _z_scouting_message_decode(_z_scouting_message_t *msg, _z_zbuf_t *buf); From f354e8c7ade527e8685b0deb22b09aa42e772a91 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Thu, 8 Jun 2023 09:46:54 +0200 Subject: [PATCH 096/181] finish new join encoding --- src/protocol/msgcodec.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index d53e6eddd..68cbdd203 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -1073,14 +1073,12 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_T_JOIN\n"); - // TODO[protocol] add options byte if `O==1` - _Z_EC(_z_wbuf_write(wbf, msg->_version)) 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 |= ((zidlen - 1) & 0x0F) << 4; _Z_EC(_z_uint8_encode(wbf, cbyte)) _Z_EC(_z_wbuf_write_bytes(wbf, msg->_zid.id, 0, zidlen)) @@ -1098,16 +1096,21 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg _Z_EC(_z_zint_encode(wbf, msg->_lease)) } - // TODO[protocol] - // if (_Z_HAS_FLAG(msg->_options, _Z_OPT_JOIN_QOS) == true) { - // for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { - // _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._qos[i]._reliable)) - // _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._qos[i]._best_effort)) - // } - // } else { - _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._plain._reliable)) - _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._plain._best_effort)) - // } + if (msg->_next_sn._is_qos) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true) { + _Z_EC(_z_uint8_encode(wbf, 0x51)) // QOS-ext: (enc=zbuf)(mandatory=true)(id=1) + for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { + _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._qos[i]._reliable)) + _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._qos[i]._best_effort)) + } + } else { + _Z_DEBUG("Attempted to serialize QoS-SN extension, but the header extension flag was unset"); + ret |= _Z_ERR_MESSAGE_SERIALIZATION_FAILED; + } + } else { + _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._plain._reliable)) + _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._plain._best_effort)) + } return ret; } From af589140d4121dc1a05f028138bff7b9e33f9f7f Mon Sep 17 00:00:00 2001 From: OlivierHecart Date: Wed, 7 Jun 2023 15:50:56 +0200 Subject: [PATCH 097/181] Fix docs build --- docs/api.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index c710debfe..1b925de03 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -172,6 +172,7 @@ Macros .. autocmacro:: macros.h::z_clone .. autocmacro:: macros.h::z_drop .. autocmacro:: macros.h::z_closure +.. autocmacro:: macros.h::z_null Primitives ~~~~~~~~~~ @@ -214,7 +215,6 @@ Primitives .. autocfunction:: primitives.h::z_closure_reply .. autocfunction:: primitives.h::z_closure_hello .. autocfunction:: primitives.h::z_closure_zid -.. autocfunction:: primitives.h::z_hello_null .. autocfunction:: primitives.h::z_scout .. autocfunction:: primitives.h::z_open .. autocfunction:: primitives.h::z_close @@ -247,7 +247,6 @@ Primitives .. autocfunction:: primitives.h::z_declare_queryable .. autocfunction:: primitives.h::z_undeclare_queryable .. autocfunction:: primitives.h::z_query_reply -.. autocfunction:: primitives.h::z_reply_null .. autocfunction:: primitives.h::z_reply_is_ok .. autocfunction:: primitives.h::z_reply_ok .. autocfunction:: primitives.h::z_reply_err From a96a91e9733b2bddac1460bf82dfb93615d7331f Mon Sep 17 00:00:00 2001 From: OlivierHecart Date: Fri, 9 Jun 2023 15:32:22 +0200 Subject: [PATCH 098/181] Bump version to 0.10.0.0 --- CMakeLists.txt | 2 +- docs/conf.py | 2 +- include/zenoh-pico.h | 2 +- library.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e917defd..5c1a4c475 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ # cmake_minimum_required(VERSION 3.8) -project(libzenohpico VERSION 0.7.2.0 LANGUAGES C) +project(libzenohpico VERSION 0.10.0.0 LANGUAGES C) option(BUILD_SHARED_LIBS "Build shared libraries if ON, otherwise build static libraries" ON) option(ZENOH_DEBUG "Use this to set the ZENOH_DEBUG variable." 0) diff --git a/docs/conf.py b/docs/conf.py index 0ccb067bf..37ff6729d 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -21,7 +21,7 @@ project = 'zenoh-pico' copyright = '2017, 2022 ZettaScale Technology Inc' author = 'ZettaScale Zenoh team' -release = '0.7.2.0' +release = '0.10.0.0' # -- General configuration --------------------------------------------------- master_doc = 'index' diff --git a/include/zenoh-pico.h b/include/zenoh-pico.h index 67bf4c9e7..9515f25a0 100644 --- a/include/zenoh-pico.h +++ b/include/zenoh-pico.h @@ -15,7 +15,7 @@ #ifndef ZENOH_PICO_H #define ZENOH_PICO_H -#define ZENOH_PICO "0.7.2.0" +#define ZENOH_PICO "0.10.0.0" #define ZENOH_PICO_MAJOR 0 #define ZENOH_PICO_MINOR 7 #define ZENOH_PICO_PATCH 2 diff --git a/library.json b/library.json index 81c1314db..1176941ff 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "zenoh-pico", - "version": "0.7.2.0", + "version": "0.10.0.0", "description": "The Eclipse Zenoh: Zero Overhead Pub/sub, Store/Query and Compute. It unifies data in motion, data in-use, data at rest and computations. It carefully blends traditional pub/sub with geo-distributed storages, queries and computations, while retaining a level of time and space efficiency that is well beyond any of the mainstream stacks. Zenoh-Pico is the implementation able to scale down to extremely constrainded devices and networks.", "keywords": [ "pubsub", From f3f58baa63857331765358e8c94798937330fc66 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 12 Jun 2023 14:01:13 +0200 Subject: [PATCH 099/181] better extension skip, split network layer codecs --- include/zenoh-pico/protocol/extcodec.h | 4 +- include/zenoh-pico/protocol/msg.h | 12 + include/zenoh-pico/protocol/msgcodec.h | 29 +- src/net/primitives.c | 5 +- src/protocol/extcodec.c | 45 +- src/protocol/msg.c | 16 + src/protocol/msgcodec.c | 1105 +--------------------- src/protocol/network_codec.c | 612 ++++++++++++ src/protocol/transport_codec.c | 515 ++++++++++ src/transport/multicast/link/rx.c | 2 +- src/transport/multicast/link/task/read.c | 2 +- src/transport/transport.c | 7 - src/transport/unicast/link/rx.c | 2 +- src/transport/unicast/link/task/read.c | 2 +- tests/z_msgcodec_test.c | 57 +- 15 files changed, 1215 insertions(+), 1200 deletions(-) create mode 100644 src/protocol/network_codec.c create mode 100644 src/protocol/transport_codec.c diff --git a/include/zenoh-pico/protocol/extcodec.h b/include/zenoh-pico/protocol/extcodec.h index f140b82f7..9fc375d8a 100644 --- a/include/zenoh-pico/protocol/extcodec.h +++ b/include/zenoh-pico/protocol/extcodec.h @@ -15,6 +15,8 @@ #ifndef ZENOH_PICO_MSGEXTCODEC_H #define ZENOH_PICO_MSGEXTCODEC_H +#include + #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/iobuf.h" @@ -38,7 +40,7 @@ int8_t _z_msg_ext_decode_iter(_z_zbuf_t *zbf, int8_t (*callback)(_z_msg_ext_t *, * Returns `_Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN` if a mandatory extension is found, * `_Z_RES_OK` otherwise. */ -int8_t _z_msg_ext_skip_non_mandatories(_z_zbuf_t *zbf); +int8_t _z_msg_ext_skip_non_mandatories(_z_zbuf_t *zbf, uint8_t trace_id); #endif /* ZENOH_PICO_MSGEXTCODEC_H */ diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index ce7cd4827..f29fbed5b 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -547,9 +547,21 @@ typedef union { _z_msg_pull_t _pull; _z_msg_unit_t _unit; } _z_zenoh_body_t; + +// TODO[remove] +typedef struct { + _z_id_t _replier_id; + _z_zint_t _qid; + uint8_t _header; +} _z_reply_context_t; +void _z_msg_clear_reply_context(_z_reply_context_t *rc); +_z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, _Bool is_final); +// TODO[remove end] + typedef struct { _z_zenoh_body_t _body; uint8_t _header; + _z_reply_context_t *_reply_context; } _z_zenoh_message_t; void _z_msg_clear(_z_zenoh_message_t *m); void _z_msg_free(_z_zenoh_message_t **m); diff --git a/include/zenoh-pico/protocol/msgcodec.h b/include/zenoh-pico/protocol/msgcodec.h index 1ca9934bb..886cd1667 100644 --- a/include/zenoh-pico/protocol/msgcodec.h +++ b/include/zenoh-pico/protocol/msgcodec.h @@ -30,7 +30,6 @@ int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *buf) int8_t _z_transport_message_encode(_z_wbuf_t *buf, const _z_transport_message_t *msg); int8_t _z_transport_message_decode(_z_transport_message_t *msg, _z_zbuf_t *buf); -int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *buf); int8_t _z_network_message_encode(_z_wbuf_t *buf, const _z_network_message_t *msg); int8_t _z_network_message_decode(_z_network_message_t *msg, _z_zbuf_t *buf); @@ -40,6 +39,8 @@ int8_t _z_zenoh_message_encode(_z_wbuf_t *buf, const _z_zenoh_message_t *msg); int8_t _z_zenoh_message_decode(_z_zenoh_message_t *msg, _z_zbuf_t *buf); int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *buf); +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); #endif /* ZENOH_PICO_MSGCODEC_H */ // NOTE: the following headers are for unit testing only @@ -57,10 +58,6 @@ int8_t _z_subinfo_encode(_z_wbuf_t *buf, const _z_subinfo_t *si); int8_t _z_subinfo_decode(_z_subinfo_t *si, _z_zbuf_t *buf, uint8_t header); int8_t _z_subinfo_decode_na(_z_subinfo_t *si, _z_zbuf_t *buf, uint8_t header); -int8_t _z_keyexpr_encode(_z_wbuf_t *buf, uint8_t header, const _z_keyexpr_t *ke); -int8_t _z_keyexpr_decode(_z_keyexpr_t *ke, _z_zbuf_t *buf, uint8_t header); -int8_t _z_keyexpr_decode_na(_z_keyexpr_t *ke, _z_zbuf_t *buf, uint8_t header); - int8_t _z_data_info_encode(_z_wbuf_t *buf, const _z_data_info_t *di); int8_t _z_data_info_decode(_z_data_info_t *di, _z_zbuf_t *buf); int8_t _z_data_info_decode_na(_z_data_info_t *di, _z_zbuf_t *buf); @@ -137,41 +134,27 @@ int8_t _z_response_final_decode_na(_z_n_msg_response_final_t *msg, _z_zbuf_t *zb // ------------------ Transport Message ------------------ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg); -int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_join_decode(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg); -int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_init_decode(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_open_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_open_t *msg); -int8_t _z_open_decode_na(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_open_decode(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_close_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_close_t *msg); -int8_t _z_close_decode_na(_z_t_msg_close_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_close_decode(_z_t_msg_close_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_keep_alive_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_keep_alive_t *msg); -int8_t _z_keep_alive_decode_na(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_frame_header_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg); -int8_t _z_frame_payload_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg); -int8_t _z_frame_header_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_frame_payload_decode_na(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_frame_header_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_frame_payload_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg); +int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_fragment_header_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg); -int8_t _z_fragment_payload_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg); -int8_t _z_fragment_header_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_fragment_payload_decode_na(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_fragment_header_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_fragment_payload_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg); +int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_extensions_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_ext_vec_t *v_ext); -int8_t _z_extensions_decode_na(_z_msg_ext_vec_t *v_ext, _z_zbuf_t *zbf, uint8_t header); int8_t _z_extensions_decode(_z_msg_ext_vec_t *v_ext, _z_zbuf_t *zbf, uint8_t header); // ------------------ Discovery Message ------------------ diff --git a/src/net/primitives.c b/src/net/primitives.c index 8f751bde2..bd5362542 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -286,18 +286,15 @@ int8_t _z_send_reply(const z_query_t *query, _z_keyexpr_t keyexpr, const uint8_t if (ret == _Z_RES_OK) { // Build the reply context decorator. This is NOT the final reply. _z_id_t zid = ((_z_session_t *)query->_zn)->_local_zid; - _z_reply_context_t *rctx = _z_msg_make_reply_context(query->_qid, zid, false); _z_data_info_t di = {._flags = 0}; // Empty data info _z_payload_t pld = {.len = len, .start = payload}; // Payload _Bool can_be_dropped = false; // Congestion control - _z_zenoh_message_t z_msg = _z_msg_make_reply(keyexpr, di, pld, can_be_dropped, rctx); + _z_zenoh_message_t z_msg = _z_msg_make_reply(keyexpr, di, pld, can_be_dropped); if (_z_send_z_msg(query->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { ret = _Z_ERR_TRANSPORT_TX_FAILED; } - - z_free(rctx); } return ret; diff --git a/src/protocol/extcodec.c b/src/protocol/extcodec.c index 0a2c92247..2f2bc0123 100644 --- a/src/protocol/extcodec.c +++ b/src/protocol/extcodec.c @@ -16,7 +16,9 @@ #include #include +#include +#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/protocol/codec.h" #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/system/platform.h" @@ -166,8 +168,43 @@ int8_t _z_msg_ext_vec_decode(_z_msg_ext_vec_t *extensions, _z_zbuf_t *zbf) { } int8_t _z_msg_ext_skip_non_mandatory(_z_msg_ext_t *extension, void *ctx) { - (void)ctx; - return (extension->_header & _Z_MSG_EXT_FLAG_M) ? _Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN : _Z_RES_OK; + int8_t ret = _Z_RES_OK; + if ((extension->_header & _Z_MSG_EXT_FLAG_M) != 0) { + uint8_t trace_id = *(uint8_t *)ctx; + uint8_t ext_id = _Z_EXT_ID(extension->_header); +#if (ZENOH_DEBUG >= 1) + switch (_Z_EXT_ENC(extension->_header)) { + case _Z_MSG_EXT_ENC_UNIT: { + _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), UNIT", ext_id, + trace_id); + break; + } + case _Z_MSG_EXT_ENC_ZINT: { + _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZINT(%02x)", ext_id, + trace_id, extension->_body._zint); + break; + } + case _Z_MSG_EXT_ENC_ZBUF: { + _z_bytes_t buf = extension->_body._zbuf._val; + char *hex = z_malloc(buf.len * 2 + 1); + for (size_t i = 0; i < buf.len; ++i) { + snprintf(hex + 2 * i, 3, "%02x", buf.start[i]); + } + _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZBUF(%.*s)", ext_id, + trace_id, buf.len * 2, hex); + z_free(hex); + break; + } + default: { + _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), UNKOWN_ENCODING", + ext_id, trace_id); + } + } +#endif + ret = _Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN; + } + + return ret; } int8_t _z_msg_ext_decode_iter(_z_zbuf_t *zbf, int8_t (*callback)(_z_msg_ext_t *, void *), void *context) { int8_t ret = _Z_RES_OK; @@ -183,6 +220,6 @@ int8_t _z_msg_ext_decode_iter(_z_zbuf_t *zbf, int8_t (*callback)(_z_msg_ext_t *, return ret; } -int8_t _z_msg_ext_skip_non_mandatories(_z_zbuf_t *zbf) { - return _z_msg_ext_decode_iter(zbf, _z_msg_ext_skip_non_mandatory, NULL); +int8_t _z_msg_ext_skip_non_mandatories(_z_zbuf_t *zbf, uint8_t trace_id) { + return _z_msg_ext_decode_iter(zbf, _z_msg_ext_skip_non_mandatory, &trace_id); } \ No newline at end of file diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 1f0bd0c53..a6b122fc0 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -26,6 +26,22 @@ /*=============================*/ /* Message fields */ /*=============================*/ +_z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, _Bool is_final) { + _z_reply_context_t *rctx = (_z_reply_context_t *)z_malloc(sizeof(_z_reply_context_t)); + if (rctx != NULL) { + rctx->_qid = qid; + rctx->_replier_id = replier_id; + + rctx->_header = 0; + if (is_final == true) { + _Z_SET_FLAG(rctx->_header, _Z_FLAG_Z_F); + } + } + + return rctx; +} + +void _z_msg_clear_reply_context(_z_reply_context_t *rc) {} /*------------------ Payload field ------------------*/ void _z_payload_clear(_z_payload_t *p) { _z_bytes_clear(p); } diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 68cbdd203..24d3f7cbb 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -98,50 +98,6 @@ int8_t _z_timestamp_decode_na(_z_timestamp_t *ts, _z_zbuf_t *zbf) { int8_t _z_timestamp_decode(_z_timestamp_t *ts, _z_zbuf_t *zbf) { return _z_timestamp_decode_na(ts, zbf); } -/*------------------ SubMode Field ------------------*/ -int8_t _z_subinfo_encode(_z_wbuf_t *wbf, const _z_subinfo_t *fld) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _SUB_MODE\n"); - - uint8_t header = fld->mode; - if ((fld->period.origin != 0) || (fld->period.period != 0) || (fld->period.duration != 0)) { - _Z_SET_FLAG(header, _Z_FLAG_Z_P); - _Z_EC(_z_wbuf_write(wbf, header)) - ret |= _z_period_encode(wbf, &fld->period); - } else { - _Z_EC(_z_wbuf_write(wbf, header)) - } - - return ret; -} - -int8_t _z_subinfo_decode_na(_z_subinfo_t *si, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _SUB_MODE\n"); - int8_t ret = _Z_RES_OK; - - // Decode the header - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_R) == true) { - si->reliability = Z_RELIABILITY_RELIABLE; - } else { - si->reliability = Z_RELIABILITY_BEST_EFFORT; - } - - uint8_t h_subifo; - ret |= _z_uint8_decode(&h_subifo, zbf); - si->mode = _Z_MID(h_subifo); - if (_Z_HAS_FLAG(h_subifo, _Z_FLAG_Z_P) == true) { - ret |= _z_period_decode(&si->period, zbf); - } else { - si->period = (_z_period_t){.origin = 0, .period = 0, .duration = 0}; - } - - return ret; -} - -int8_t _z_subinfo_decode(_z_subinfo_t *si, _z_zbuf_t *zbf, uint8_t header) { - return _z_subinfo_decode_na(si, zbf, header); -} - /*------------------ ResKey Field ------------------*/ int8_t _z_keyexpr_encode(_z_wbuf_t *wbf, _Bool has_suffix, const _z_keyexpr_t *fld) { int8_t ret = _Z_RES_OK; @@ -155,7 +111,7 @@ int8_t _z_keyexpr_encode(_z_wbuf_t *wbf, _Bool has_suffix, const _z_keyexpr_t *f return ret; } -int8_t _z_keyexpr_decode_na(_z_keyexpr_t *ke, _z_zbuf_t *zbf, _Bool has_suffix) { +int8_t _z_keyexpr_decode(_z_keyexpr_t *ke, _z_zbuf_t *zbf, _Bool has_suffix) { _Z_DEBUG("Decoding _RESKEY\n"); int8_t ret = _Z_RES_OK; @@ -175,10 +131,6 @@ int8_t _z_keyexpr_decode_na(_z_keyexpr_t *ke, _z_zbuf_t *zbf, _Bool has_suffix) return ret; } -int8_t _z_keyexpr_decode(_z_keyexpr_t *ke, _z_zbuf_t *zbf, uint8_t header) { - return _z_keyexpr_decode_na(ke, zbf, header); -} - /*------------------ Locators Field ------------------*/ int8_t _z_locators_encode(_z_wbuf_t *wbf, const _z_locator_array_t *la) { int8_t ret = _Z_RES_OK; @@ -226,317 +178,6 @@ int8_t _z_locators_decode(_z_locator_array_t *a_loc, _z_zbuf_t *zbf) { return _z /*=============================*/ /* Zenoh Messages */ /*=============================*/ -/*------------------ Resource Declaration ------------------*/ -int8_t _z_res_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_res_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_RESOURCE\n"); - - _Z_EC(_z_zint_encode(wbf, dcl->_id)) - ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); - - return ret; -} - -int8_t _z_res_decl_decode_na(_z_res_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_RESOURCE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&dcl->_id, zbf); - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - - return ret; -} - -int8_t _z_res_decl_decode(_z_res_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_res_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Publisher Declaration ------------------*/ -int8_t _z_pub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_pub_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_PUBLISHER\n"); - - ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); - - return ret; -} - -int8_t _z_pub_decl_decode_na(_z_pub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_PUBLISHER\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - - return ret; -} - -int8_t _z_pub_decl_decode(_z_pub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_pub_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Subscriber Declaration ------------------*/ -int8_t _z_sub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_sub_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_SUBSCRIBER\n"); - - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key)) - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_S) == true) { - ret |= _z_subinfo_encode(wbf, &dcl->_subinfo); - } - - return ret; -} - -int8_t _z_sub_decl_decode_na(_z_sub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_SUBSCRIBER\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_S) == true) { - ret |= _z_subinfo_decode(&dcl->_subinfo, zbf, header); - } else { - dcl->_subinfo.mode = Z_SUBMODE_PUSH; // Default subscription mode is non-periodic PUSH - dcl->_subinfo.period = (_z_period_t){.origin = 0, .period = 0, .duration = 0}; - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_R) == true) { - dcl->_subinfo.reliability = Z_RELIABILITY_RELIABLE; - } else { - dcl->_subinfo.reliability = Z_RELIABILITY_BEST_EFFORT; - } - } - - return ret; -} - -int8_t _z_sub_decl_decode(_z_sub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_sub_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Queryable Declaration ------------------*/ -int8_t _z_qle_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_qle_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_QUERYABLE\n"); - - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key)); - - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Q) == true) { - _Z_EC(_z_zint_encode(wbf, dcl->_complete)); - _Z_EC(_z_zint_encode(wbf, dcl->_distance)); - } - - return ret; -} - -int8_t _z_qle_decl_decode_na(_z_qle_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_QUERYABLE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Q) == true) { - ret |= _z_zint_decode(&dcl->_complete, zbf); - ret |= _z_zint_decode(&dcl->_distance, zbf); - } else { - dcl->_complete = 0; - dcl->_distance = 0; - } - - return ret; -} - -int8_t _z_qle_decl_decode(_z_qle_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_qle_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Forget Resource Declaration ------------------*/ -int8_t _z_forget_res_decl_encode(_z_wbuf_t *wbf, const _z_forget_res_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_FORGET_RESOURCE\n"); - - ret |= _z_zint_encode(wbf, dcl->_rid); - - return ret; -} - -int8_t _z_forget_res_decl_decode_na(_z_forget_res_decl_t *dcl, _z_zbuf_t *zbf) { - _Z_DEBUG("Decoding _Z_DECL_FORGET_RESOURCE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&dcl->_rid, zbf); - - return ret; -} - -int8_t _z_forget_res_decl_decode(_z_forget_res_decl_t *dcl, _z_zbuf_t *zbf) { - return _z_forget_res_decl_decode_na(dcl, zbf); -} - -/*------------------ Forget Publisher Declaration ------------------*/ -int8_t _z_forget_pub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_forget_pub_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_FORGET_PUBLISHER\n"); - - ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); - - return ret; -} - -int8_t _z_forget_pub_decl_decode_na(_z_forget_pub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_FORGET_PUBLISHER\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - - return ret; -} - -int8_t _z_forget_pub_decl_decode(_z_forget_pub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_forget_pub_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Forget Subscriber Declaration ------------------*/ -int8_t _z_forget_sub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_forget_sub_decl_t *dcl) { - _Z_DEBUG("Encoding _Z_DECL_FORGET_PUBLISHER\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); - - return ret; -} - -int8_t _z_forget_sub_decl_decode_na(_z_forget_sub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_FORGET_PUBLISHER\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - - return ret; -} - -int8_t _z_forget_sub_decl_decode(_z_forget_sub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_forget_sub_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Forget Queryable Declaration ------------------*/ -int8_t _z_forget_qle_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_forget_qle_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_FORGET_QUERYABLE\n"); - - ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); - - return ret; -} - -int8_t _z_forget_qle_decl_decode_na(_z_forget_qle_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_FORGET_QUERYABLE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - - return ret; -} - -int8_t _z_forget_qle_decl_decode(_z_forget_qle_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_forget_qle_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Declaration Field ------------------*/ -int8_t _z_declaration_encode(_z_wbuf_t *wbf, const _z_declaration_t *dcl) { - int8_t ret = _Z_RES_OK; - - _Z_EC(_z_wbuf_write(wbf, dcl->_header)) - uint8_t did = _Z_MID(dcl->_header); - switch (did) { - case _Z_DECL_RESOURCE: { - ret |= _z_res_decl_encode(wbf, dcl->_header, &dcl->_body._res); - } break; - - case _Z_DECL_PUBLISHER: { - ret |= _z_pub_decl_encode(wbf, dcl->_header, &dcl->_body._pub); - } break; - - case _Z_DECL_SUBSCRIBER: { - ret |= _z_sub_decl_encode(wbf, dcl->_header, &dcl->_body._sub); - } break; - - case _Z_DECL_QUERYABLE: { - ret |= _z_qle_decl_encode(wbf, dcl->_header, &dcl->_body._qle); - } break; - - case _Z_DECL_FORGET_RESOURCE: { - ret |= _z_forget_res_decl_encode(wbf, &dcl->_body._forget_res); - } break; - - case _Z_DECL_FORGET_PUBLISHER: { - ret |= _z_forget_pub_decl_encode(wbf, dcl->_header, &dcl->_body._forget_pub); - } break; - - case _Z_DECL_FORGET_SUBSCRIBER: { - ret |= _z_forget_sub_decl_encode(wbf, dcl->_header, &dcl->_body._forget_sub); - } break; - - case _Z_DECL_FORGET_QUERYABLE: { - ret |= _z_forget_qle_decl_encode(wbf, dcl->_header, &dcl->_body._forget_qle); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to encode declaration with unknown ID(%d)\n", did); - ret |= _Z_ERR_MESSAGE_SERIALIZATION_FAILED; - } break; - } - - return ret; -} - -int8_t _z_declaration_decode_na(_z_declaration_t *decl, _z_zbuf_t *zbf) { - int8_t ret = _Z_RES_OK; - - ret |= _z_uint8_decode(&decl->_header, zbf); - if (ret == _Z_RES_OK) { - uint8_t mid = _Z_MID(decl->_header); - - switch (mid) { - case _Z_DECL_RESOURCE: { - ret |= _z_res_decl_decode(&decl->_body._res, zbf, decl->_header); - } break; - - case _Z_DECL_PUBLISHER: { - ret |= _z_pub_decl_decode(&decl->_body._pub, zbf, decl->_header); - } break; - - case _Z_DECL_SUBSCRIBER: { - ret |= _z_sub_decl_decode(&decl->_body._sub, zbf, decl->_header); - } break; - - case _Z_DECL_QUERYABLE: { - ret |= _z_qle_decl_decode(&decl->_body._qle, zbf, decl->_header); - } break; - - case _Z_DECL_FORGET_RESOURCE: { - ret |= _z_forget_res_decl_decode(&decl->_body._forget_res, zbf); - } break; - - case _Z_DECL_FORGET_PUBLISHER: { - ret |= _z_forget_pub_decl_decode(&decl->_body._forget_pub, zbf, decl->_header); - } break; - - case _Z_DECL_FORGET_SUBSCRIBER: { - ret |= _z_forget_sub_decl_decode(&decl->_body._forget_sub, zbf, decl->_header); - } break; - - case _Z_DECL_FORGET_QUERYABLE: { - ret |= _z_forget_qle_decl_decode(&decl->_body._forget_qle, zbf, decl->_header); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to decode declaration with unknown ID(%d)\n", mid); - ret |= _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; - } break; - } - } - - return ret; -} - -int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf) { return _z_declaration_decode_na(decl, zbf); } /*------------------ Data Info Field ------------------*/ int8_t _z_data_info_encode(_z_wbuf_t *wbf, const _z_data_info_t *fld) { @@ -829,750 +470,6 @@ int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { int8_t _z_zenoh_message_decode(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { return _z_zenoh_message_decode_na(msg, zbf); } -/*------------------ Declaration Message ------------------*/ -int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_n_msg_declare_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_N_DECLARE\n"); - - _Z_EC(_z_declaration_encode(wbf, &msg->_declaration)); - - return ret; -} - -int8_t _z_declare_decode_na(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf) { - _Z_DEBUG("Decoding _Z_MID_N_DECLARE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_declaration_decode_na(&msg->_declaration, zbf); - if (ret != _Z_RES_OK) { - // TODO[protocol]: Add empty declaration - } - - return ret; -} - -int8_t _z_declare_decode(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf) { return _z_declare_decode_na(msg, zbf); } - -/*------------------ Push Body Field ------------------*/ -int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { - (void)(wbf); - (void)(pshb); - int8_t ret = _Z_RES_OK; - - return ret; -} - -int8_t _z_push_body_decode_na(_z_push_body_t *pshb, _z_zbuf_t *zbf) { - (void)(zbf); - (void)(pshb); - int8_t ret = _Z_RES_OK; - - return ret; -} - -int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf) { return _z_push_body_decode_na(pshb, zbf); } - -/*------------------ Push Message ------------------*/ -int8_t _z_push_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_push_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_N_PUSH\n"); - - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N), &msg->_key)); - _Z_EC(_z_push_body_encode(wbf, &msg->_body)); - - return ret; -} - -int8_t _z_push_decode_na(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_N_PUSH\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N)); - ret |= _z_push_body_decode(&msg->_body, zbf); - - return ret; -} - -int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_push_decode_na(msg, zbf, header); -} - -/*------------------ Request Body Field ------------------*/ -int8_t _z_request_body_encode(_z_wbuf_t *wbf, const _z_request_body_t *reqb) { - (void)(wbf); - (void)(reqb); - int8_t ret = _Z_RES_OK; - - return ret; -} - -int8_t _z_request_body_decode_na(_z_request_body_t *reqb, _z_zbuf_t *zbf) { - (void)(zbf); - (void)(reqb); - int8_t ret = _Z_RES_OK; - - return ret; -} - -int8_t _z_request_body_decode(_z_request_body_t *reqb, _z_zbuf_t *zbf) { return _z_request_body_decode_na(reqb, zbf); } - -/*------------------ Request Message ------------------*/ -int8_t _z_request_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_request_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_N_REQUEST\n"); - - _Z_EC(_z_zint_encode(wbf, msg->_rid)); - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N), &msg->_key)); - _Z_EC(_z_request_body_encode(wbf, &msg->_body)); - - return ret; -} - -int8_t _z_request_decode_na(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_N_REQUEST\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&msg->_rid, zbf); - ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N)); - ret |= _z_request_body_decode(&msg->_body, zbf); - - return ret; -} - -int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_request_decode_na(msg, zbf, header); -} - -/*------------------ Response Body Field ------------------*/ -int8_t _z_response_body_encode(_z_wbuf_t *wbf, const _z_response_body_t *rspb) { - (void)(wbf); - (void)(rspb); - int8_t ret = _Z_RES_OK; - - return ret; -} - -int8_t _z_response_body_decode_na(_z_response_body_t *rspb, _z_zbuf_t *zbf) { - (void)(zbf); - (void)(rspb); - int8_t ret = _Z_RES_OK; - - return ret; -} - -int8_t _z_response_body_decode(_z_response_body_t *rspb, _z_zbuf_t *zbf) { - return _z_response_body_decode_na(rspb, zbf); -} - -/*------------------ Response Message ------------------*/ -int8_t _z_response_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); - - _Z_EC(_z_zint_encode(wbf, msg->_rid)); - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N), &msg->_key)); - _Z_EC(_z_response_body_encode(wbf, &msg->_body)); - - return ret; -} - -int8_t _z_response_decode_na(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&msg->_rid, zbf); - ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N)); - ret |= _z_response_body_decode(&msg->_body, zbf); - - return ret; -} - -int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_response_decode_na(msg, zbf, header); -} - -/*------------------ Response Final Message ------------------*/ -int8_t _z_response_final_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_final_t *msg) { - (void)(header); - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); - - _Z_EC(_z_zint_encode(wbf, msg->_rid)); - - return ret; -} - -int8_t _z_response_final_decode_na(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { - (void)(header); - _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&msg->_rid, zbf); - - return ret; -} - -int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_response_final_decode_na(msg, zbf, header); -} - -/*------------------ Network Message ------------------*/ -int8_t _z_network_message_encode(_z_wbuf_t *wbf, const _z_network_message_t *msg) { - int8_t ret = _Z_RES_OK; - - uint8_t header = msg->_header; - if (_z_msg_ext_vec_is_empty(&msg->_extensions) == false) { - header |= _Z_FLAG_N_Z; - } - - _Z_EC(_z_wbuf_write(wbf, header)) - switch (_Z_MID(msg->_header)) { - case _Z_MID_N_DECLARE: { - ret |= _z_declare_encode(wbf, &msg->_body._declare); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to encode network message with unknown ID(%d)\n", _Z_MID(msg->_header)); - ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; - } break; - } - - return ret; -} - -int8_t _z_network_message_decode_na(_z_network_message_t *msg, _z_zbuf_t *zbf) { - int8_t ret = _Z_RES_OK; - - ret |= _z_uint8_decode(&msg->_header, zbf); // Decode the header - if (ret == _Z_RES_OK) { - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_N_DECLARE: { - ret |= _z_declare_decode(&msg->_body._declare, zbf); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(%d)\n", mid); - ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; - } break; - } - } - - return ret; -} - -int8_t _z_network_message_decode(_z_network_message_t *n_msg, _z_zbuf_t *zbf) { - return _z_network_message_decode_na(n_msg, zbf); -} - -/*=============================*/ -/* Transport Messages */ -/*=============================*/ -/*------------------ Join Message ------------------*/ -int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_T_JOIN\n"); - - _Z_EC(_z_wbuf_write(wbf, msg->_version)) - - uint8_t cbyte = 0; - cbyte |= (msg->_whatami & 0x03); - uint8_t zidlen = _z_id_len(msg->_zid); - cbyte |= ((zidlen - 1) & 0x0F) << 4; - _Z_EC(_z_uint8_encode(wbf, cbyte)) - _Z_EC(_z_wbuf_write_bytes(wbf, msg->_zid.id, 0, zidlen)) - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S) == true) { - cbyte = 0; - cbyte |= (msg->_seq_num_res & 0x03); - cbyte |= ((msg->_req_id_res & 0x03) << 2); - _Z_EC(_z_uint8_encode(wbf, cbyte)) - _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) - } - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_T) == true) { - _Z_EC(_z_zint_encode(wbf, msg->_lease / 1000)) - } else { - _Z_EC(_z_zint_encode(wbf, msg->_lease)) - } - - if (msg->_next_sn._is_qos) { - if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true) { - _Z_EC(_z_uint8_encode(wbf, 0x51)) // QOS-ext: (enc=zbuf)(mandatory=true)(id=1) - for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { - _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._qos[i]._reliable)) - _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._qos[i]._best_effort)) - } - } else { - _Z_DEBUG("Attempted to serialize QoS-SN extension, but the header extension flag was unset"); - ret |= _Z_ERR_MESSAGE_SERIALIZATION_FAILED; - } - } else { - _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._plain._reliable)) - _Z_EC(_z_zint_encode(wbf, msg->_next_sn._val._plain._best_effort)) - } - - return ret; -} - -int8_t _z_join_decode_ext(_z_msg_ext_t *extension, void *ctx) { - int8_t ret = _Z_RES_OK; - _z_t_msg_join_t *msg = (_z_t_msg_join_t *)ctx; - if (_Z_EXT_FULL_ID(extension->_header) == 0x51) { // QOS: (enc=zbuf)(mandatory=true)(id=1) - msg->_next_sn._is_qos = true; - _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); - for (int i = 0; (ret == _Z_RES_OK) && (i < Z_PRIORITIES_NUM); ++i) { - ret |= _z_zint_decode(&msg->_next_sn._val._plain._reliable, &zbf); - ret |= _z_zint_decode(&msg->_next_sn._val._plain._best_effort, &zbf); - } - } else if (_Z_MSG_EXT_IS_MANDATORY(extension->_header)) { - ret = _Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN; - } - return ret; -} - -int8_t _z_join_decode_na(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_T_JOIN\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_uint8_decode(&msg->_version, zbf); - - uint8_t cbyte = 0; - ret |= _z_uint8_decode(&cbyte, zbf); - msg->_whatami = cbyte & 0x03; - - uint8_t zidlen = ((cbyte & 0xF0) >> 4) + 1; - msg->_zid = _z_id_empty(); - if (ret == _Z_RES_OK) { - _z_zbuf_read_bytes(zbf, msg->_zid.id, 0, zidlen); - } - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S) == true) { - cbyte = 0; - ret |= _z_uint8_decode(&cbyte, zbf); - msg->_seq_num_res = (cbyte & 0x03); - msg->_req_id_res = ((cbyte >> 2) & 0x03); - ret |= _z_uint16_decode(&msg->_batch_size, zbf); - } else { - msg->_seq_num_res = _Z_DEFAULT_RESOLUTION_SIZE; - msg->_req_id_res = _Z_DEFAULT_RESOLUTION_SIZE; - msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; - } - - ret |= _z_zint_decode(&msg->_lease, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_T) == true) { - msg->_lease = msg->_lease * 1000; - } - - msg->_next_sn._is_qos = false; - ret |= _z_zint_decode(&msg->_next_sn._val._plain._reliable, zbf); - ret |= _z_zint_decode(&msg->_next_sn._val._plain._best_effort, zbf); - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { - ret |= _z_msg_ext_decode_iter(zbf, _z_join_decode_ext, msg); - } - - return ret; -} - -int8_t _z_join_decode(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_join_decode_na(msg, zbf, header); -} - -/*------------------ Init Message ------------------*/ -int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg) { - _Z_DEBUG("Encoding _Z_MID_T_INIT\n"); - int8_t ret = _Z_RES_OK; - - _Z_EC(_z_wbuf_write(wbf, msg->_version)) - - 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 - _Z_EC(_z_uint8_encode(wbf, cbyte)) - _Z_EC(_z_wbuf_write_bytes(wbf, msg->_zid.id, 0, zidlen)) - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_S) == true) { - cbyte = 0; - cbyte |= (msg->_seq_num_res & 0x03); - cbyte |= ((msg->_req_id_res & 0x03) << 2); - _Z_EC(_z_uint8_encode(wbf, cbyte)) - _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) - } - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_A) == true) { - _Z_EC(_z_bytes_encode(wbf, &msg->_cookie)) - } - - return ret; -} - -int8_t _z_init_decode_na(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_T_INIT\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_uint8_decode(&msg->_version, zbf); - - uint8_t cbyte = 0; - ret |= _z_uint8_decode(&cbyte, zbf); - msg->_zid = _z_id_empty(); - - if (ret == _Z_RES_OK) { - msg->_whatami = cbyte & 0x03; - uint8_t zidlen = ((cbyte & 0xF0) >> 4) + 1; - _z_zbuf_read_bytes(zbf, msg->_zid.id, 0, zidlen); - } - - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_S) == true)) { - cbyte = 0; - ret |= _z_uint8_decode(&cbyte, zbf); - msg->_seq_num_res = (cbyte & 0x03); - msg->_req_id_res = ((cbyte >> 2) & 0x03); - ret |= _z_uint16_decode(&msg->_batch_size, zbf); - } else { - msg->_seq_num_res = _Z_DEFAULT_RESOLUTION_SIZE; - msg->_req_id_res = _Z_DEFAULT_RESOLUTION_SIZE; - msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; - } - - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_A) == true)) { - ret |= _z_bytes_decode(&msg->_cookie, zbf); - if (ret != _Z_RES_OK) { - msg->_cookie = _z_bytes_empty(); - } - } else { - msg->_cookie = _z_bytes_empty(); - } - - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { - ret |= _z_msg_ext_skip_non_mandatories(zbf); - } - - return ret; -} - -int8_t _z_init_decode(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_init_decode_na(msg, zbf, header); -} - -/*------------------ Open Message ------------------*/ -int8_t _z_open_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_open_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_T_OPEN\n"); - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_T) == true) { - _Z_EC(_z_zint_encode(wbf, msg->_lease / 1000)) - } else { - _Z_EC(_z_zint_encode(wbf, msg->_lease)) - } - - _Z_EC(_z_zint_encode(wbf, msg->_initial_sn)) - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_A) == false) { - _Z_EC(_z_bytes_encode(wbf, &msg->_cookie)) - } - - return ret; -} - -int8_t _z_open_decode_na(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_T_OPEN\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&msg->_lease, zbf); - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_T) == true)) { - msg->_lease = msg->_lease * 1000; - } - - ret |= _z_zint_decode(&msg->_initial_sn, zbf); - - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_A) == false)) { - ret |= _z_bytes_decode(&msg->_cookie, zbf); - if (ret != _Z_RES_OK) { - msg->_cookie = _z_bytes_empty(); - } - } else { - msg->_cookie = _z_bytes_empty(); - } - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { - ret |= _z_msg_ext_skip_non_mandatories(zbf); - } - - return ret; -} - -int8_t _z_open_decode(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_open_decode_na(msg, zbf, header); -} - -/*------------------ Close Message ------------------*/ -int8_t _z_close_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_close_t *msg) { - (void)(header); - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_T_CLOSE\n"); - - ret |= _z_wbuf_write(wbf, msg->_reason); - - return ret; -} - -int8_t _z_close_decode_na(_z_t_msg_close_t *msg, _z_zbuf_t *zbf, uint8_t header) { - (void)(header); - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_T_CLOSE\n"); - - ret |= _z_uint8_decode(&msg->_reason, zbf); - - return ret; -} - -int8_t _z_close_decode(_z_t_msg_close_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_close_decode_na(msg, zbf, header); -} - -/*------------------ Keep Alive Message ------------------*/ -int8_t _z_keep_alive_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_keep_alive_t *msg) { - (void)(wbf); - (void)(header); - (void)(msg); - - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_T_KEEP_ALIVE\n"); - - return ret; -} - -int8_t _z_keep_alive_decode_na(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t header) { - (void)(msg); - (void)(zbf); - (void)(header); - - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_MID_T_KEEP_ALIVE\n"); - - ret |= _z_msg_ext_skip_non_mandatories(zbf); - - return ret; -} - -int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_keep_alive_decode_na(msg, zbf, header); -} - -/*------------------ Frame Message ------------------*/ - -int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg) { - int8_t ret = _Z_RES_OK; - - _Z_EC(_z_zint_encode(wbf, msg->_sn)) - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { - ret = _Z_ERR_MESSAGE_SERIALIZATION_FAILED; - } - if (ret == _Z_RES_OK) { - size_t len = _z_network_message_vec_len(&msg->_messages); - for (size_t i = 0; i < len; i++) { - _Z_EC(_z_network_message_encode(wbf, _z_network_message_vec_get(&msg->_messages, i))) - } - } - - return ret; -} - -int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&msg->_sn, zbf); - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { - ret |= _z_msg_ext_skip_non_mandatories(zbf); - } - if (ret == _Z_RES_OK) { - msg->_messages = _z_network_message_vec_make(_ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE); - while (_z_zbuf_len(zbf) > 0) { - // Mark the reading position of the iobfer - size_t r_pos = _z_zbuf_get_rpos(zbf); - _z_network_message_t *nm = (_z_network_message_t *)z_malloc(sizeof(_z_network_message_t)); - ret |= _z_network_message_decode(nm, zbf); - if (ret == _Z_RES_OK) { - _z_network_message_vec_append(&msg->_messages, nm); - } else { - _z_n_msg_free(&nm); - - _z_zbuf_set_rpos(zbf, r_pos); // Restore the reading position of the iobfer - - // FIXME: Check for the return error, since not all of them means a decoding error - // in this particular case. As of now, we roll-back the reading position - // and return to the Zenoh transport-level decoder. - // https://github.com/eclipse-zenoh/zenoh-pico/pull/132#discussion_r1045593602 - if ((ret & _Z_ERR_MESSAGE_ZENOH_UNKNOWN) == _Z_ERR_MESSAGE_ZENOH_UNKNOWN) { - ret = _Z_RES_OK; - } - break; - } - } - } - return ret; -} - -/*------------------ Fragment Message ------------------*/ -int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_TRANSPORT_FRAGMENT\n"); - _Z_EC(_z_zint_encode(wbf, msg->_sn)) - if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { - ret = _Z_ERR_MESSAGE_SERIALIZATION_FAILED; - } - if (ret == _Z_RES_OK) { - _Z_EC(_z_bytes_encode(wbf, &msg->_payload)) - } - - return ret; -} - -int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Decoding _Z_TRANSPORT_FRAGMENT\n"); - ret |= _z_zint_decode(&msg->_sn, zbf); - - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { - ret |= _z_msg_ext_skip_non_mandatories(zbf); - } - - ret |= _z_bytes_decode(&msg->_payload, zbf); - - return ret; -} - -/*------------------ Transport Extensions Message ------------------*/ -int8_t _z_extensions_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_ext_vec_t *v_ext) { - (void)(header); - int8_t ret = _Z_RES_OK; - - _Z_DEBUG("Encoding _Z_TRANSPORT_EXTENSIONS\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true) { - ret |= _z_msg_ext_vec_encode(wbf, v_ext); - } - - return ret; -} - -int8_t _z_extensions_decode_na(_z_msg_ext_vec_t *v_ext, _z_zbuf_t *zbf, uint8_t header) { - (void)(header); - int8_t ret = _Z_RES_OK; - - _Z_DEBUG("Decoding _Z_TRANSPORT_EXTENSIONS\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true) { - ret |= _z_msg_ext_vec_decode(v_ext, zbf); - } else { - _z_msg_ext_vec_reset(v_ext); - } - - return ret; -} - -int8_t _z_extensions_decode(_z_msg_ext_vec_t *v_ext, _z_zbuf_t *zbf, uint8_t header) { - return _z_extensions_decode_na(v_ext, zbf, header); -} - -/*------------------ Transport Message ------------------*/ -int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t *msg) { - int8_t ret = _Z_RES_OK; - - // Encode the decorators if present - - uint8_t header = msg->_header; - - _Z_EC(_z_wbuf_write(wbf, header)) - switch (_Z_MID(msg->_header)) { - case _Z_MID_T_FRAME: { - ret |= _z_frame_encode(wbf, msg->_header, &msg->_body._frame); - } break; - - case _Z_MID_T_FRAGMENT: { - ret |= _z_fragment_encode(wbf, msg->_header, &msg->_body._fragment); - } break; - - case _Z_MID_T_KEEP_ALIVE: { - ret |= _z_keep_alive_encode(wbf, msg->_header, &msg->_body._keep_alive); - } break; - - case _Z_MID_T_JOIN: { - ret |= _z_join_encode(wbf, msg->_header, &msg->_body._join); - } break; - - case _Z_MID_T_INIT: { - ret |= _z_init_encode(wbf, msg->_header, &msg->_body._init); - } break; - - case _Z_MID_T_OPEN: { - ret |= _z_open_encode(wbf, msg->_header, &msg->_body._open); - } break; - - case _Z_MID_T_CLOSE: { - ret |= _z_close_encode(wbf, msg->_header, &msg->_body._close); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to encode session message with unknown ID(%d)\n", _Z_MID(msg->_header)); - ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; - } break; - } - - return ret; -} - -int8_t _z_transport_message_decode_na(_z_transport_message_t *msg, _z_zbuf_t *zbf) { - int8_t ret = _Z_RES_OK; - - ret |= _z_uint8_decode(&msg->_header, zbf); // Decode the header - if (ret == _Z_RES_OK) { - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_T_FRAME: { - ret |= _z_frame_decode(&msg->_body._frame, zbf, msg->_header); - } break; - - case _Z_MID_T_FRAGMENT: { - ret |= _z_fragment_decode(&msg->_body._fragment, zbf, msg->_header); - } break; - - case _Z_MID_T_KEEP_ALIVE: { - ret |= _z_keep_alive_decode(&msg->_body._keep_alive, zbf, msg->_header); - } break; - - case _Z_MID_T_JOIN: { - ret |= _z_join_decode(&msg->_body._join, zbf, msg->_header); - } break; - - case _Z_MID_T_INIT: { - ret |= _z_init_decode(&msg->_body._init, zbf, msg->_header); - } break; - - case _Z_MID_T_OPEN: { - ret |= _z_open_decode(&msg->_body._open, zbf, msg->_header); - } break; - - case _Z_MID_T_CLOSE: { - ret |= _z_close_decode(&msg->_body._close, zbf, msg->_header); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(%d)\n", mid); - ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; - } break; - } - } else { - msg->_header = 0xFF; - } - - return ret; -} - -int8_t _z_transport_message_decode(_z_transport_message_t *t_msg, _z_zbuf_t *zbf) { - return _z_transport_message_decode_na(t_msg, zbf); -} - /*=============================*/ /* Scouting Messages */ /*=============================*/ diff --git a/src/protocol/network_codec.c b/src/protocol/network_codec.c new file mode 100644 index 000000000..9f410d3a5 --- /dev/null +++ b/src/protocol/network_codec.c @@ -0,0 +1,612 @@ +// +// 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, +// + +#include + +#include "zenoh-pico/protocol/extcodec.h" +#include "zenoh-pico/protocol/msgcodec.h" +#include "zenoh-pico/utils/logging.h" +#include "zenoh-pico/utils/result.h" + +/*------------------ SubMode Field ------------------*/ +int8_t _z_subinfo_encode(_z_wbuf_t *wbf, const _z_subinfo_t *fld) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _SUB_MODE\n"); + + uint8_t header = fld->mode; + if ((fld->period.origin != 0) || (fld->period.period != 0) || (fld->period.duration != 0)) { + _Z_SET_FLAG(header, _Z_FLAG_Z_P); + _Z_EC(_z_wbuf_write(wbf, header)) + ret |= _z_period_encode(wbf, &fld->period); + } else { + _Z_EC(_z_wbuf_write(wbf, header)) + } + + return ret; +} + +int8_t _z_subinfo_decode_na(_z_subinfo_t *si, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _SUB_MODE\n"); + int8_t ret = _Z_RES_OK; + + // Decode the header + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_R) == true) { + si->reliability = Z_RELIABILITY_RELIABLE; + } else { + si->reliability = Z_RELIABILITY_BEST_EFFORT; + } + + uint8_t h_subifo; + ret |= _z_uint8_decode(&h_subifo, zbf); + si->mode = _Z_MID(h_subifo); + if (_Z_HAS_FLAG(h_subifo, _Z_FLAG_Z_P) == true) { + ret |= _z_period_decode(&si->period, zbf); + } else { + si->period = (_z_period_t){.origin = 0, .period = 0, .duration = 0}; + } + + return ret; +} + +int8_t _z_subinfo_decode(_z_subinfo_t *si, _z_zbuf_t *zbf, uint8_t header) { + return _z_subinfo_decode_na(si, zbf, header); +} + +/*------------------ Resource Declaration ------------------*/ +int8_t _z_res_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_res_decl_t *dcl) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_DECL_RESOURCE\n"); + + _Z_EC(_z_zint_encode(wbf, dcl->_id)) + ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); + + return ret; +} + +int8_t _z_res_decl_decode_na(_z_res_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_DECL_RESOURCE\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_zint_decode(&dcl->_id, zbf); + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); + + return ret; +} + +int8_t _z_res_decl_decode(_z_res_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + return _z_res_decl_decode_na(dcl, zbf, header); +} + +/*------------------ Publisher Declaration ------------------*/ +int8_t _z_pub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_pub_decl_t *dcl) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_DECL_PUBLISHER\n"); + + ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); + + return ret; +} + +int8_t _z_pub_decl_decode_na(_z_pub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_DECL_PUBLISHER\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); + + return ret; +} + +int8_t _z_pub_decl_decode(_z_pub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + return _z_pub_decl_decode_na(dcl, zbf, header); +} + +/*------------------ Subscriber Declaration ------------------*/ +int8_t _z_sub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_sub_decl_t *dcl) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_DECL_SUBSCRIBER\n"); + + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key)) + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_S) == true) { + ret |= _z_subinfo_encode(wbf, &dcl->_subinfo); + } + + return ret; +} + +int8_t _z_sub_decl_decode_na(_z_sub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_DECL_SUBSCRIBER\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_S) == true) { + ret |= _z_subinfo_decode(&dcl->_subinfo, zbf, header); + } else { + dcl->_subinfo.mode = Z_SUBMODE_PUSH; // Default subscription mode is non-periodic PUSH + dcl->_subinfo.period = (_z_period_t){.origin = 0, .period = 0, .duration = 0}; + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_R) == true) { + dcl->_subinfo.reliability = Z_RELIABILITY_RELIABLE; + } else { + dcl->_subinfo.reliability = Z_RELIABILITY_BEST_EFFORT; + } + } + + return ret; +} + +int8_t _z_sub_decl_decode(_z_sub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + return _z_sub_decl_decode_na(dcl, zbf, header); +} + +/*------------------ Queryable Declaration ------------------*/ +int8_t _z_qle_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_qle_decl_t *dcl) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_DECL_QUERYABLE\n"); + + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key)); + + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Q) == true) { + _Z_EC(_z_zint_encode(wbf, dcl->_complete)); + _Z_EC(_z_zint_encode(wbf, dcl->_distance)); + } + + return ret; +} + +int8_t _z_qle_decl_decode_na(_z_qle_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_DECL_QUERYABLE\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Q) == true) { + ret |= _z_zint_decode(&dcl->_complete, zbf); + ret |= _z_zint_decode(&dcl->_distance, zbf); + } else { + dcl->_complete = 0; + dcl->_distance = 0; + } + + return ret; +} + +int8_t _z_qle_decl_decode(_z_qle_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + return _z_qle_decl_decode_na(dcl, zbf, header); +} + +/*------------------ Forget Resource Declaration ------------------*/ +int8_t _z_forget_res_decl_encode(_z_wbuf_t *wbf, const _z_forget_res_decl_t *dcl) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_DECL_FORGET_RESOURCE\n"); + + ret |= _z_zint_encode(wbf, dcl->_rid); + + return ret; +} + +int8_t _z_forget_res_decl_decode_na(_z_forget_res_decl_t *dcl, _z_zbuf_t *zbf) { + _Z_DEBUG("Decoding _Z_DECL_FORGET_RESOURCE\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_zint_decode(&dcl->_rid, zbf); + + return ret; +} + +int8_t _z_forget_res_decl_decode(_z_forget_res_decl_t *dcl, _z_zbuf_t *zbf) { + return _z_forget_res_decl_decode_na(dcl, zbf); +} + +/*------------------ Forget Publisher Declaration ------------------*/ +int8_t _z_forget_pub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_forget_pub_decl_t *dcl) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_DECL_FORGET_PUBLISHER\n"); + + ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); + + return ret; +} + +int8_t _z_forget_pub_decl_decode_na(_z_forget_pub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_DECL_FORGET_PUBLISHER\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); + + return ret; +} + +int8_t _z_forget_pub_decl_decode(_z_forget_pub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + return _z_forget_pub_decl_decode_na(dcl, zbf, header); +} + +/*------------------ Forget Subscriber Declaration ------------------*/ +int8_t _z_forget_sub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_forget_sub_decl_t *dcl) { + _Z_DEBUG("Encoding _Z_DECL_FORGET_PUBLISHER\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); + + return ret; +} + +int8_t _z_forget_sub_decl_decode_na(_z_forget_sub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_DECL_FORGET_PUBLISHER\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); + + return ret; +} + +int8_t _z_forget_sub_decl_decode(_z_forget_sub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + return _z_forget_sub_decl_decode_na(dcl, zbf, header); +} + +/*------------------ Forget Queryable Declaration ------------------*/ +int8_t _z_forget_qle_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_forget_qle_decl_t *dcl) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_DECL_FORGET_QUERYABLE\n"); + + ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); + + return ret; +} + +int8_t _z_forget_qle_decl_decode_na(_z_forget_qle_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_DECL_FORGET_QUERYABLE\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); + + return ret; +} + +int8_t _z_forget_qle_decl_decode(_z_forget_qle_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { + return _z_forget_qle_decl_decode_na(dcl, zbf, header); +} + +/*------------------ Declaration Field ------------------*/ +int8_t _z_declaration_encode(_z_wbuf_t *wbf, const _z_declaration_t *dcl) { + int8_t ret = _Z_RES_OK; + + _Z_EC(_z_wbuf_write(wbf, dcl->_header)) + uint8_t did = _Z_MID(dcl->_header); + switch (did) { + case _Z_DECL_RESOURCE: { + ret |= _z_res_decl_encode(wbf, dcl->_header, &dcl->_body._res); + } break; + + case _Z_DECL_PUBLISHER: { + ret |= _z_pub_decl_encode(wbf, dcl->_header, &dcl->_body._pub); + } break; + + case _Z_DECL_SUBSCRIBER: { + ret |= _z_sub_decl_encode(wbf, dcl->_header, &dcl->_body._sub); + } break; + + case _Z_DECL_QUERYABLE: { + ret |= _z_qle_decl_encode(wbf, dcl->_header, &dcl->_body._qle); + } break; + + case _Z_DECL_FORGET_RESOURCE: { + ret |= _z_forget_res_decl_encode(wbf, &dcl->_body._forget_res); + } break; + + case _Z_DECL_FORGET_PUBLISHER: { + ret |= _z_forget_pub_decl_encode(wbf, dcl->_header, &dcl->_body._forget_pub); + } break; + + case _Z_DECL_FORGET_SUBSCRIBER: { + ret |= _z_forget_sub_decl_encode(wbf, dcl->_header, &dcl->_body._forget_sub); + } break; + + case _Z_DECL_FORGET_QUERYABLE: { + ret |= _z_forget_qle_decl_encode(wbf, dcl->_header, &dcl->_body._forget_qle); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to encode declaration with unknown ID(%d)\n", did); + ret |= _Z_ERR_MESSAGE_SERIALIZATION_FAILED; + } break; + } + + return ret; +} + +int8_t _z_declaration_decode_na(_z_declaration_t *decl, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + + ret |= _z_uint8_decode(&decl->_header, zbf); + if (ret == _Z_RES_OK) { + uint8_t mid = _Z_MID(decl->_header); + + switch (mid) { + case _Z_DECL_RESOURCE: { + ret |= _z_res_decl_decode(&decl->_body._res, zbf, decl->_header); + } break; + + case _Z_DECL_PUBLISHER: { + ret |= _z_pub_decl_decode(&decl->_body._pub, zbf, decl->_header); + } break; + + case _Z_DECL_SUBSCRIBER: { + ret |= _z_sub_decl_decode(&decl->_body._sub, zbf, decl->_header); + } break; + + case _Z_DECL_QUERYABLE: { + ret |= _z_qle_decl_decode(&decl->_body._qle, zbf, decl->_header); + } break; + + case _Z_DECL_FORGET_RESOURCE: { + ret |= _z_forget_res_decl_decode(&decl->_body._forget_res, zbf); + } break; + + case _Z_DECL_FORGET_PUBLISHER: { + ret |= _z_forget_pub_decl_decode(&decl->_body._forget_pub, zbf, decl->_header); + } break; + + case _Z_DECL_FORGET_SUBSCRIBER: { + ret |= _z_forget_sub_decl_decode(&decl->_body._forget_sub, zbf, decl->_header); + } break; + + case _Z_DECL_FORGET_QUERYABLE: { + ret |= _z_forget_qle_decl_decode(&decl->_body._forget_qle, zbf, decl->_header); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to decode declaration with unknown ID(%d)\n", mid); + ret |= _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } break; + } + } + + return ret; +} + +int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf) { return _z_declaration_decode_na(decl, zbf); } + +/*------------------ Declaration Message ------------------*/ +int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_n_msg_declare_t *msg) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_N_DECLARE\n"); + + _Z_EC(_z_declaration_encode(wbf, &msg->_declaration)); + + return ret; +} + +int8_t _z_declare_decode_na(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf) { + _Z_DEBUG("Decoding _Z_MID_N_DECLARE\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_declaration_decode_na(&msg->_declaration, zbf); + if (ret != _Z_RES_OK) { + // TODO[protocol]: Add empty declaration + } + + return ret; +} + +int8_t _z_declare_decode(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf) { return _z_declare_decode_na(msg, zbf); } + +/*------------------ Push Body Field ------------------*/ +int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { + (void)(wbf); + (void)(pshb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_push_body_decode_na(_z_push_body_t *pshb, _z_zbuf_t *zbf) { + (void)(zbf); + (void)(pshb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf) { return _z_push_body_decode_na(pshb, zbf); } + +/*------------------ Push Message ------------------*/ +int8_t _z_push_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_push_t *msg) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_N_PUSH\n"); + + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N), &msg->_key)); + _Z_EC(_z_push_body_encode(wbf, &msg->_body)); + + return ret; +} + +int8_t _z_push_decode_na(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_N_PUSH\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N)); + ret |= _z_push_body_decode(&msg->_body, zbf); + + return ret; +} + +int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_push_decode_na(msg, zbf, header); +} + +/*------------------ Request Body Field ------------------*/ +int8_t _z_request_body_encode(_z_wbuf_t *wbf, const _z_request_body_t *reqb) { + (void)(wbf); + (void)(reqb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_request_body_decode_na(_z_request_body_t *reqb, _z_zbuf_t *zbf) { + (void)(zbf); + (void)(reqb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_request_body_decode(_z_request_body_t *reqb, _z_zbuf_t *zbf) { return _z_request_body_decode_na(reqb, zbf); } + +/*------------------ Request Message ------------------*/ +int8_t _z_request_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_request_t *msg) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_N_REQUEST\n"); + + _Z_EC(_z_zint_encode(wbf, msg->_rid)); + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N), &msg->_key)); + _Z_EC(_z_request_body_encode(wbf, &msg->_body)); + + return ret; +} + +int8_t _z_request_decode_na(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_N_REQUEST\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_zint_decode(&msg->_rid, zbf); + ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N)); + ret |= _z_request_body_decode(&msg->_body, zbf); + + return ret; +} + +int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_request_decode_na(msg, zbf, header); +} + +/*------------------ Response Body Field ------------------*/ +int8_t _z_response_body_encode(_z_wbuf_t *wbf, const _z_response_body_t *rspb) { + (void)(wbf); + (void)(rspb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_response_body_decode_na(_z_response_body_t *rspb, _z_zbuf_t *zbf) { + (void)(zbf); + (void)(rspb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_response_body_decode(_z_response_body_t *rspb, _z_zbuf_t *zbf) { + return _z_response_body_decode_na(rspb, zbf); +} + +/*------------------ Response Message ------------------*/ +int8_t _z_response_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_t *msg) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); + + _Z_EC(_z_zint_encode(wbf, msg->_rid)); + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N), &msg->_key)); + _Z_EC(_z_response_body_encode(wbf, &msg->_body)); + + return ret; +} + +int8_t _z_response_decode_na(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_zint_decode(&msg->_rid, zbf); + ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N)); + ret |= _z_response_body_decode(&msg->_body, zbf); + + return ret; +} + +int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_response_decode_na(msg, zbf, header); +} + +/*------------------ Response Final Message ------------------*/ +int8_t _z_response_final_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_final_t *msg) { + (void)(header); + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); + + _Z_EC(_z_zint_encode(wbf, msg->_rid)); + + return ret; +} + +int8_t _z_response_final_decode_na(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { + (void)(header); + _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_zint_decode(&msg->_rid, zbf); + + return ret; +} + +int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_response_final_decode_na(msg, zbf, header); +} + +/*------------------ Network Message ------------------*/ +int8_t _z_network_message_encode(_z_wbuf_t *wbf, const _z_network_message_t *msg) { + int8_t ret = _Z_RES_OK; + + uint8_t header = msg->_header; + if (_z_msg_ext_vec_is_empty(&msg->_extensions) == false) { + header |= _Z_FLAG_N_Z; + } + + _Z_EC(_z_wbuf_write(wbf, header)) + switch (_Z_MID(msg->_header)) { + case _Z_MID_N_DECLARE: { + ret |= _z_declare_encode(wbf, &msg->_body._declare); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to encode network message with unknown ID(%d)\n", _Z_MID(msg->_header)); + ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; + } break; + } + + return ret; +} + +int8_t _z_network_message_decode_na(_z_network_message_t *msg, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + + ret |= _z_uint8_decode(&msg->_header, zbf); // Decode the header + if (ret == _Z_RES_OK) { + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_N_DECLARE: { + ret |= _z_declare_decode(&msg->_body._declare, zbf); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(%d)\n", mid); + ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; + } break; + } + } + + return ret; +} + +int8_t _z_network_message_decode(_z_network_message_t *n_msg, _z_zbuf_t *zbf) { + return _z_network_message_decode_na(n_msg, zbf); +} \ No newline at end of file diff --git a/src/protocol/transport_codec.c b/src/protocol/transport_codec.c new file mode 100644 index 000000000..c138cf9d0 --- /dev/null +++ b/src/protocol/transport_codec.c @@ -0,0 +1,515 @@ +// +// 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, +// + +#include + +#include "zenoh-pico/protocol/extcodec.h" +#include "zenoh-pico/protocol/iobuf.h" +#include "zenoh-pico/protocol/msgcodec.h" +#include "zenoh-pico/utils/logging.h" +#include "zenoh-pico/utils/result.h" +/*------------------ Join Message ------------------*/ +int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_T_JOIN\n"); + + ret = _z_wbuf_write(wbf, msg->_version); + + if (ret == _Z_RES_OK) { + uint8_t cbyte = 0; + cbyte |= (msg->_whatami & 0x03); + uint8_t zidlen = _z_id_len(msg->_zid); + cbyte |= ((zidlen - 1) & 0x0F) << 4; + ret = _z_uint8_encode(wbf, cbyte); + if (ret == _Z_RES_OK) { + ret = _z_wbuf_write_bytes(wbf, msg->_zid.id, 0, zidlen); + } + } + + if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S)) { + uint8_t cbyte = 0; + cbyte |= (msg->_seq_num_res & 0x03); + cbyte |= ((msg->_req_id_res & 0x03) << 2); + ret = _z_uint8_encode(wbf, cbyte); + if (ret == _Z_RES_OK) { + ret = _z_uint16_encode(wbf, msg->_batch_size); + } + } + if (ret == _Z_RES_OK) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_T) == true) { + ret = _z_zint_encode(wbf, msg->_lease / 1000); + } else { + ret = _z_zint_encode(wbf, msg->_lease); + } + } + if (ret == _Z_RES_OK) { + if (msg->_next_sn._is_qos) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { + ret = _z_uint8_encode(wbf, 0x51); // QOS-ext: (enc=zbuf)(mandatory=true)(id=1) + for (uint8_t i = 0; (i < Z_PRIORITIES_NUM) && (ret == _Z_RES_OK); i++) { + ret = _z_zint_encode(wbf, msg->_next_sn._val._qos[i]._reliable); + ret |= _z_zint_encode(wbf, msg->_next_sn._val._qos[i]._best_effort); + } + } else { + _Z_DEBUG("Attempted to serialize QoS-SN extension, but the header extension flag was unset"); + ret |= _Z_ERR_MESSAGE_SERIALIZATION_FAILED; + } + } else { + ret |= _z_zint_encode(wbf, msg->_next_sn._val._plain._reliable); + ret |= _z_zint_encode(wbf, msg->_next_sn._val._plain._best_effort); + } + } + + return ret; +} + +int8_t _z_join_decode_ext(_z_msg_ext_t *extension, void *ctx) { + int8_t ret = _Z_RES_OK; + _z_t_msg_join_t *msg = (_z_t_msg_join_t *)ctx; + if (_Z_EXT_FULL_ID(extension->_header) == 0x51) { // QOS: (enc=zbuf)(mandatory=true)(id=1) + msg->_next_sn._is_qos = true; + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + for (int i = 0; (ret == _Z_RES_OK) && (i < Z_PRIORITIES_NUM); ++i) { + ret |= _z_zint_decode(&msg->_next_sn._val._plain._reliable, &zbf); + ret |= _z_zint_decode(&msg->_next_sn._val._plain._best_effort, &zbf); + } + } else if (_Z_MSG_EXT_IS_MANDATORY(extension->_header)) { + ret = _Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN; + } + return ret; +} + +int8_t _z_join_decode(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_T_JOIN\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_uint8_decode(&msg->_version, zbf); + + uint8_t cbyte = 0; + ret |= _z_uint8_decode(&cbyte, zbf); + msg->_whatami = cbyte & 0x03; + + uint8_t zidlen = ((cbyte & 0xF0) >> 4) + 1; + msg->_zid = _z_id_empty(); + if (ret == _Z_RES_OK) { + if (_z_zbuf_len(zbf) >= zidlen) { + _z_zbuf_read_bytes(zbf, msg->_zid.id, 0, zidlen); + } else { + ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + } + if (ret == _Z_RES_OK) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S) == true) { + cbyte = 0; + ret |= _z_uint8_decode(&cbyte, zbf); + msg->_seq_num_res = (cbyte & 0x03); + msg->_req_id_res = ((cbyte >> 2) & 0x03); + ret |= _z_uint16_decode(&msg->_batch_size, zbf); + } else { + msg->_seq_num_res = _Z_DEFAULT_RESOLUTION_SIZE; + msg->_req_id_res = _Z_DEFAULT_RESOLUTION_SIZE; + msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; + } + } + if (ret == _Z_RES_OK) { + ret |= _z_zint_decode(&msg->_lease, zbf); + if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_T) == true) { + msg->_lease = msg->_lease * 1000; + } + } + if (ret == _Z_RES_OK) { + msg->_next_sn._is_qos = false; + ret |= _z_zint_decode(&msg->_next_sn._val._plain._reliable, zbf); + ret |= _z_zint_decode(&msg->_next_sn._val._plain._best_effort, zbf); + } + if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { + ret |= _z_msg_ext_decode_iter(zbf, _z_join_decode_ext, msg); + } + + return ret; +} + +/*------------------ Init Message ------------------*/ +int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg) { + _Z_DEBUG("Encoding _Z_MID_T_INIT\n"); + int8_t ret = _Z_RES_OK; + + _Z_EC(_z_wbuf_write(wbf, msg->_version)) + + 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 + _Z_EC(_z_uint8_encode(wbf, cbyte)) + _Z_EC(_z_wbuf_write_bytes(wbf, msg->_zid.id, 0, zidlen)) + + if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_S) == true) { + cbyte = 0; + cbyte |= (msg->_seq_num_res & 0x03); + cbyte |= ((msg->_req_id_res & 0x03) << 2); + _Z_EC(_z_uint8_encode(wbf, cbyte)) + _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) + } + + if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_A) == true) { + _Z_EC(_z_bytes_encode(wbf, &msg->_cookie)) + } + + return ret; +} + +int8_t _z_init_decode(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_T_INIT\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_uint8_decode(&msg->_version, zbf); + + uint8_t cbyte = 0; + ret |= _z_uint8_decode(&cbyte, zbf); + msg->_zid = _z_id_empty(); + + if (ret == _Z_RES_OK) { + msg->_whatami = cbyte & 0x03; + uint8_t zidlen = ((cbyte & 0xF0) >> 4) + 1; + if (_z_zbuf_len(zbf) >= zidlen) { + _z_zbuf_read_bytes(zbf, msg->_zid.id, 0, zidlen); + } else { + ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + } + + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_S) == true)) { + cbyte = 0; + ret |= _z_uint8_decode(&cbyte, zbf); + msg->_seq_num_res = (cbyte & 0x03); + msg->_req_id_res = ((cbyte >> 2) & 0x03); + ret |= _z_uint16_decode(&msg->_batch_size, zbf); + } else { + msg->_seq_num_res = _Z_DEFAULT_RESOLUTION_SIZE; + msg->_req_id_res = _Z_DEFAULT_RESOLUTION_SIZE; + msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; + } + + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_A) == true)) { + ret |= _z_bytes_decode(&msg->_cookie, zbf); + if (ret != _Z_RES_OK) { + msg->_cookie = _z_bytes_empty(); + } + } else { + msg->_cookie = _z_bytes_empty(); + } + + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { + ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x01); + } + + return ret; +} + +/*------------------ Open Message ------------------*/ +int8_t _z_open_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_open_t *msg) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_T_OPEN\n"); + + if (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_T) == true) { + _Z_EC(_z_zint_encode(wbf, msg->_lease / 1000)) + } else { + _Z_EC(_z_zint_encode(wbf, msg->_lease)) + } + + _Z_EC(_z_zint_encode(wbf, msg->_initial_sn)) + + if (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_A) == false) { + _Z_EC(_z_bytes_encode(wbf, &msg->_cookie)) + } + + return ret; +} + +int8_t _z_open_decode(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_T_OPEN\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_zint_decode(&msg->_lease, zbf); + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_T) == true)) { + msg->_lease = msg->_lease * 1000; + } + + ret |= _z_zint_decode(&msg->_initial_sn, zbf); + + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_A) == false)) { + ret |= _z_bytes_decode(&msg->_cookie, zbf); + if (ret != _Z_RES_OK) { + msg->_cookie = _z_bytes_empty(); + } + } else { + msg->_cookie = _z_bytes_empty(); + } + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { + ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x02); + } + + return ret; +} + +/*------------------ Close Message ------------------*/ +int8_t _z_close_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_close_t *msg) { + (void)(header); + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_T_CLOSE\n"); + + ret |= _z_wbuf_write(wbf, msg->_reason); + + return ret; +} + +int8_t _z_close_decode(_z_t_msg_close_t *msg, _z_zbuf_t *zbf, uint8_t header) { + (void)(header); + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Decoding _Z_MID_T_CLOSE\n"); + + ret |= _z_uint8_decode(&msg->_reason, zbf); + + return ret; +} + +/*------------------ Keep Alive Message ------------------*/ +int8_t _z_keep_alive_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_keep_alive_t *msg) { + (void)(wbf); + (void)(header); + (void)(msg); + + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_T_KEEP_ALIVE\n"); + + return ret; +} + +int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t header) { + (void)(msg); + (void)(zbf); + (void)(header); + + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Decoding _Z_MID_T_KEEP_ALIVE\n"); + + ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x03); + + return ret; +} + +/*------------------ Frame Message ------------------*/ + +int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg) { + int8_t ret = _Z_RES_OK; + + _Z_EC(_z_zint_encode(wbf, msg->_sn)) + + if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { + ret = _Z_ERR_MESSAGE_SERIALIZATION_FAILED; + } + if (ret == _Z_RES_OK) { + size_t len = _z_network_message_vec_len(&msg->_messages); + for (size_t i = 0; i < len; i++) { + _Z_EC(_z_network_message_encode(wbf, _z_network_message_vec_get(&msg->_messages, i))) + } + } + + return ret; +} + +int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { + int8_t ret = _Z_RES_OK; + + ret |= _z_zint_decode(&msg->_sn, zbf); + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { + ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x04); + } + if (ret == _Z_RES_OK) { + msg->_messages = _z_network_message_vec_make(_ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE); + while (_z_zbuf_len(zbf) > 0) { + // Mark the reading position of the iobfer + size_t r_pos = _z_zbuf_get_rpos(zbf); + _z_network_message_t *nm = (_z_network_message_t *)z_malloc(sizeof(_z_network_message_t)); + ret |= _z_network_message_decode(nm, zbf); + if (ret == _Z_RES_OK) { + _z_network_message_vec_append(&msg->_messages, nm); + } else { + _z_n_msg_free(&nm); + + _z_zbuf_set_rpos(zbf, r_pos); // Restore the reading position of the iobfer + + // FIXME: Check for the return error, since not all of them means a decoding error + // in this particular case. As of now, we roll-back the reading position + // and return to the Zenoh transport-level decoder. + // https://github.com/eclipse-zenoh/zenoh-pico/pull/132#discussion_r1045593602 + if ((ret & _Z_ERR_MESSAGE_ZENOH_UNKNOWN) == _Z_ERR_MESSAGE_ZENOH_UNKNOWN) { + ret = _Z_RES_OK; + } + break; + } + } + } + return ret; +} + +/*------------------ Fragment Message ------------------*/ +int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_TRANSPORT_FRAGMENT\n"); + _Z_EC(_z_zint_encode(wbf, msg->_sn)) + if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { + ret = _Z_ERR_MESSAGE_SERIALIZATION_FAILED; + } + if (ret == _Z_RES_OK) { + _Z_EC(_z_bytes_encode(wbf, &msg->_payload)) + } + + return ret; +} + +int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Decoding _Z_TRANSPORT_FRAGMENT\n"); + ret |= _z_zint_decode(&msg->_sn, zbf); + + if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { + ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x04); + } + + ret |= _z_bytes_decode(&msg->_payload, zbf); + + return ret; +} + +/*------------------ Transport Extensions Message ------------------*/ +int8_t _z_extensions_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_ext_vec_t *v_ext) { + (void)(header); + int8_t ret = _Z_RES_OK; + + _Z_DEBUG("Encoding _Z_TRANSPORT_EXTENSIONS\n"); + if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true) { + ret |= _z_msg_ext_vec_encode(wbf, v_ext); + } + + return ret; +} + +int8_t _z_extensions_decode(_z_msg_ext_vec_t *v_ext, _z_zbuf_t *zbf, uint8_t header) { + (void)(header); + int8_t ret = _Z_RES_OK; + + _Z_DEBUG("Decoding _Z_TRANSPORT_EXTENSIONS\n"); + if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true) { + ret |= _z_msg_ext_vec_decode(v_ext, zbf); + } else { + _z_msg_ext_vec_reset(v_ext); + } + + return ret; +} + +/*------------------ Transport Message ------------------*/ +int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t *msg) { + int8_t ret = _Z_RES_OK; + + // Encode the decorators if present + + uint8_t header = msg->_header; + + _Z_EC(_z_wbuf_write(wbf, header)) + switch (_Z_MID(msg->_header)) { + case _Z_MID_T_FRAME: { + ret |= _z_frame_encode(wbf, msg->_header, &msg->_body._frame); + } break; + + case _Z_MID_T_FRAGMENT: { + ret |= _z_fragment_encode(wbf, msg->_header, &msg->_body._fragment); + } break; + + case _Z_MID_T_KEEP_ALIVE: { + ret |= _z_keep_alive_encode(wbf, msg->_header, &msg->_body._keep_alive); + } break; + + case _Z_MID_T_JOIN: { + ret |= _z_join_encode(wbf, msg->_header, &msg->_body._join); + } break; + + case _Z_MID_T_INIT: { + ret |= _z_init_encode(wbf, msg->_header, &msg->_body._init); + } break; + + case _Z_MID_T_OPEN: { + ret |= _z_open_encode(wbf, msg->_header, &msg->_body._open); + } break; + + case _Z_MID_T_CLOSE: { + ret |= _z_close_encode(wbf, msg->_header, &msg->_body._close); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to encode session message with unknown ID(%d)\n", _Z_MID(msg->_header)); + ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; + } break; + } + + return ret; +} + +int8_t _z_transport_message_decode(_z_transport_message_t *msg, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + + ret |= _z_uint8_decode(&msg->_header, zbf); // Decode the header + if (ret == _Z_RES_OK) { + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_T_FRAME: { + ret |= _z_frame_decode(&msg->_body._frame, zbf, msg->_header); + } break; + + case _Z_MID_T_FRAGMENT: { + ret |= _z_fragment_decode(&msg->_body._fragment, zbf, msg->_header); + } break; + + case _Z_MID_T_KEEP_ALIVE: { + ret |= _z_keep_alive_decode(&msg->_body._keep_alive, zbf, msg->_header); + } break; + + case _Z_MID_T_JOIN: { + ret |= _z_join_decode(&msg->_body._join, zbf, msg->_header); + } break; + + case _Z_MID_T_INIT: { + ret |= _z_init_decode(&msg->_body._init, zbf, msg->_header); + } break; + + case _Z_MID_T_OPEN: { + ret |= _z_open_decode(&msg->_body._open, zbf, msg->_header); + } break; + + case _Z_MID_T_CLOSE: { + ret |= _z_close_decode(&msg->_body._close, zbf, msg->_header); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(%d)\n", mid); + ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; + } break; + } + } else { + msg->_header = 0xFF; + } + + return ret; +} \ No newline at end of file diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 95a1c723e..e7adf38f2 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -90,7 +90,7 @@ int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_me if (ret == _Z_RES_OK) { _Z_DEBUG(">> \t transport_message_decode\n"); - ret = _z_transport_message_decode_na(t_msg, &ztm->_zbuf); + ret = _z_transport_message_decode(t_msg, &ztm->_zbuf); } #if Z_MULTI_THREAD == 1 diff --git a/src/transport/multicast/link/task/read.c b/src/transport/multicast/link/task/read.c index 3100b5f86..925dfd3e0 100644 --- a/src/transport/multicast/link/task/read.c +++ b/src/transport/multicast/link/task/read.c @@ -88,7 +88,7 @@ void *_zp_multicast_read_task(void *ztm_arg) { // Decode one session message _z_transport_message_t t_msg; - ret = _z_transport_message_decode_na(&t_msg, &zbuf); + ret = _z_transport_message_decode(&t_msg, &zbuf); if (ret == _Z_RES_OK) { ret = _z_multicast_handle_transport_message(ztm, &t_msg, &addr); diff --git a/src/transport/transport.c b/src/transport/transport.c index aa0993e0c..52b96e902 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -255,7 +255,6 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * _z_id_t zid = *local_zid; _z_transport_message_t ism = _z_t_msg_make_init_syn(Z_WHATAMI_CLIENT, zid); param->_seq_num_res = ism._body._init._seq_num_res; // The announced sn resolution - param->_key_id_res = ism._body._init._key_id_res; // The announced key id resolution param->_req_id_res = ism._body._init._req_id_res; // The announced req id resolution param->_batch_size = ism._body._init._batch_size; // The announced batch size @@ -279,12 +278,6 @@ int8_t _z_transport_unicast_open_client(_z_transport_unicast_establish_param_t * ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; } - if (iam._body._init._key_id_res <= param->_key_id_res) { - param->_key_id_res = iam._body._init._key_id_res; - } else { - ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; - } - if (iam._body._init._req_id_res <= param->_req_id_res) { param->_req_id_res = iam._body._init._req_id_res; } else { diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index 1e1a40097..682956da3 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -70,7 +70,7 @@ int8_t _z_unicast_recv_t_msg_na(_z_transport_unicast_t *ztu, _z_transport_messag if (ret == _Z_RES_OK) { _Z_DEBUG(">> \t transport_message_decode\n"); - ret = _z_transport_message_decode_na(t_msg, &ztu->_zbuf); + ret = _z_transport_message_decode(t_msg, &ztu->_zbuf); // Mark the session that we have received data if (ret == _Z_RES_OK) { diff --git a/src/transport/unicast/link/task/read.c b/src/transport/unicast/link/task/read.c index af45dc5c9..201b28e1b 100644 --- a/src/transport/unicast/link/task/read.c +++ b/src/transport/unicast/link/task/read.c @@ -85,7 +85,7 @@ void *_zp_unicast_read_task(void *ztu_arg) { // Decode one session message _z_transport_message_t t_msg; - int8_t ret = _z_transport_message_decode_na(&t_msg, &zbuf); + int8_t ret = _z_transport_message_decode(&t_msg, &zbuf); if (ret == _Z_RES_OK) { ret = _z_unicast_handle_transport_message(ztu, &t_msg); diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index bc29f3b20..766dc3c1e 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -895,37 +895,6 @@ void assert_eq_reply_context(_z_reply_context_t *left, _z_reply_context_t *right printf(")"); } -void reply_contex_decorator(void) { - printf("\n>> ReplyContext decorator\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_reply_context_t *e_rc = gen_reply_context(); - - // Encode - int8_t res = _z_reply_context_encode(&wbf, e_rc); - assert(res == _Z_RES_OK); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - uint8_t header = _z_zbuf_read(&zbf); - _z_reply_context_t *d_rc = NULL; - res = _z_reply_context_decode(&d_rc, &zbf, header); - assert(res == _Z_RES_OK); - - printf(" "); - assert_eq_reply_context(e_rc, d_rc); - printf("\n"); - - // Free - _z_msg_clear_reply_context(e_rc); - _z_msg_clear_reply_context(d_rc); - z_free(e_rc); - z_free(d_rc); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - /*=============================*/ /* Declaration Fields */ /*=============================*/ @@ -2222,9 +2191,6 @@ void assert_eq_join_message(_z_t_msg_join_t *left, _z_t_msg_join_t *right, uint8 assert(left->_req_id_res == right->_req_id_res); printf("\n"); - assert(left->_key_id_res == right->_key_id_res); - printf("\n"); - printf(" Batch Size (%hu:%hu)", left->_batch_size, right->_batch_size); assert(left->_batch_size == right->_batch_size); printf("\n"); @@ -2311,10 +2277,6 @@ _z_transport_message_t gen_init_message(void) { t_msg._body._init._seq_num_res = gen_uint8() % 4; } - if (gen_bool()) { - t_msg._body._init._key_id_res = gen_uint8() % 4; - } - if (gen_bool()) { t_msg._body._init._req_id_res = gen_uint8() % 4; } @@ -2344,10 +2306,6 @@ void assert_eq_init_message(_z_t_msg_init_t *left, _z_t_msg_init_t *right, uint8 assert(left->_req_id_res == right->_req_id_res); printf("\n"); - printf(" KeyExpr ID Resolution (%hhu:%hhu)", left->_key_id_res, right->_key_id_res); - assert(left->_key_id_res == right->_key_id_res); - printf("\n"); - printf(" Batch Size (%hu:%hu)", left->_batch_size, right->_batch_size); assert(left->_batch_size == right->_batch_size); printf("\n"); @@ -2578,16 +2536,14 @@ void frame_message(void) { _z_t_msg_frame_t e_fr = t_msg._body._frame; // Encode - int8_t res = _z_frame_header_encode(&wbf, t_msg._header, &e_fr); - res |= _z_frame_payload_encode(&wbf, t_msg._header, &e_fr); + int8_t res = _z_frame_encode(&wbf, t_msg._header, &e_fr); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); _z_t_msg_frame_t d_fr; - res = _z_frame_header_decode(&d_fr, &zbf, t_msg._header); - res |= _z_frame_payload_decode(&d_fr, &zbf, t_msg._header); + res = _z_frame_decode(&d_fr, &zbf, t_msg._header); assert(res == _Z_RES_OK); assert_eq_frame_message(&e_fr, &d_fr, t_msg._header); @@ -2630,16 +2586,14 @@ void fragment_message(void) { _z_t_msg_fragment_t e_fr = t_msg._body._fragment; // Encode - int8_t res = _z_fragment_header_encode(&wbf, t_msg._header, &e_fr); - res |= _z_fragment_payload_encode(&wbf, t_msg._header, &e_fr); + int8_t res = _z_fragment_encode(&wbf, t_msg._header, &e_fr); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); _z_t_msg_fragment_t d_fr; - res = _z_fragment_header_decode(&d_fr, &zbf, t_msg._header); - res |= _z_fragment_payload_decode(&d_fr, &zbf, t_msg._header); + res = _z_fragment_decode(&d_fr, &zbf, t_msg._header); assert(res == _Z_RES_OK); assert_eq_fragment_message(&e_fr, &d_fr, t_msg._header); @@ -3075,9 +3029,6 @@ int main(void) { keyexpr_field(); data_info_field(); - // Message decorators - reply_contex_decorator(); - // Zenoh declarations resource_declaration(); publisher_declaration(); From a2c72f58897eb598701db35ce7f127fe25b5a546 Mon Sep 17 00:00:00 2001 From: Clay Chang Date: Thu, 15 Jun 2023 22:13:27 +0800 Subject: [PATCH 100/181] Correct the ret code in unspported unicast peer mode (#221) --- src/transport/transport.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport/transport.c b/src/transport/transport.c index b9605abb9..576a5e65d 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -359,7 +359,7 @@ int8_t _z_transport_unicast_open_peer(_z_transport_unicast_establish_param_t *pa (void)(param); (void)(zl); (void)(local_zid); - int8_t ret = _Z_ERR_CONFIG_UNSUPPORTED_CLIENT_MULTICAST; + int8_t ret = _Z_ERR_CONFIG_UNSUPPORTED_PEER_UNICAST; // @TODO: not implemented From 5df7a70e711ac0f70dfc53cdf82a477f8b902fb6 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 19 Jun 2023 09:07:04 +0200 Subject: [PATCH 101/181] more codecs --- include/zenoh-pico/protocol/extcodec.h | 7 + include/zenoh-pico/protocol/msg.h | 163 +++++++----- include/zenoh-pico/protocol/msgcodec.h | 12 +- src/net/primitives.c | 3 +- src/protocol/extcodec.c | 65 ++--- src/protocol/msg.c | 28 +-- src/protocol/msgcodec.c | 331 +++++++++++++++++++++---- src/protocol/network_codec.c | 84 ++++--- src/protocol/transport_codec.c | 2 +- src/session/queryable.c | 1 - tests/z_msgcodec_test.c | 8 +- 11 files changed, 503 insertions(+), 201 deletions(-) diff --git a/include/zenoh-pico/protocol/extcodec.h b/include/zenoh-pico/protocol/extcodec.h index 9fc375d8a..cbeb16880 100644 --- a/include/zenoh-pico/protocol/extcodec.h +++ b/include/zenoh-pico/protocol/extcodec.h @@ -41,6 +41,13 @@ int8_t _z_msg_ext_decode_iter(_z_zbuf_t *zbf, int8_t (*callback)(_z_msg_ext_t *, * `_Z_RES_OK` otherwise. */ int8_t _z_msg_ext_skip_non_mandatories(_z_zbuf_t *zbf, uint8_t trace_id); +/** + * Logs an error to debug the unknown extension, returning `_Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN`. + * + * `trace_id` may be any arbitrary value, but is advised to be unique to its call-site, + * to help debugging should it be necessary. + */ +int8_t _z_msg_ext_unknown_error(_z_msg_ext_t *extension, uint8_t trace_id); #endif /* ZENOH_PICO_MSGEXTCODEC_H */ diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index f29fbed5b..117b06e20 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -16,10 +16,12 @@ #define ZENOH_PICO_PROTOCOL_MSG_H #include +#include #include #include "zenoh-pico/api/constants.h" #include "zenoh-pico/collections/array.h" +#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/element.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/link/endpoint.h" @@ -157,6 +159,7 @@ // #define _Z_FLAG_N_RESPONSE_X 0x40 // 1 << 6 /* Zenoh message flags */ +#define _Z_FLAG_Z_Z 0x80 #define _Z_FLAG_Z_B 0x40 // 1 << 6 | QueryPayload if B==1 then QueryPayload is present #define _Z_FLAG_Z_D 0x20 // 1 << 5 | Dropping if D==1 then the message can be dropped #define _Z_FLAG_Z_F \ @@ -447,6 +450,11 @@ typedef struct { uint8_t _kind; } _z_data_info_t; void _z_data_info_clear(_z_data_info_t *di); +typedef struct { + _z_id_t _id; + uint32_t _entity_id; + uint32_t _source_sn; +} _z_source_info_t; /*------------------ Data Message ------------------*/ // 7 6 5 4 3 2 1 0 @@ -502,42 +510,30 @@ typedef struct { void _z_msg_clear_pull(_z_msg_pull_t *msg); /*------------------ Query Message ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |K|B|T| QUERY | -// +-+-+-+---------+ -// ~ ResKey ~ if K==1 then keyexpr is string -// +---------------+ -// ~ parameters ~ -// +---------------+ -// ~ qid ~ -// +---------------+ -// ~ target ~ if T==1. Otherwise target = Z_TARGET_BEST_MATCHING -// +---------------+ -// ~ consolidation ~ -// +---------------+ -// ~ QueryBody ~ if B==1 -// +---------------+ -// -// where, QueryBody data structure is optionally included in Query messages -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+---------+ -// ~ DataInfo ~ -// +---------------+ -// ~ Payload ~ -// +---------------+ -// ``` - +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|C|P| QUERY | +// +-+-+-+---------+ +// ~ params ~ if P==1 -- +// +---------------+ +// ~ consolidation ~ if C==1 -- u8 +// +---------------+ +// ~ [qry_exts] ~ if Z==1 +// +---------------+ +#define _Z_FLAG_Z_Q_P 0x20 // 1 << 7 | Period if P==1 then a period is present typedef struct { - _z_keyexpr_t _key; - _z_zint_t _qid; - char *_parameters; - z_query_target_t _target; - z_consolidation_mode_t _consolidation; - _z_data_info_t _info; + _z_bytes_t _parameters; + _z_source_info_t _info; _z_payload_t _payload; + _z_encoding_t _encoding; + z_consolidation_mode_t _consolidation; } _z_msg_query_t; +typedef struct { + _Bool info; + _Bool body; + _Bool consolidation; +} _z_msg_query_reqexts_t; +_z_msg_query_reqexts_t _z_msg_query_required_extensions(const _z_msg_query_t *msg); void _z_msg_clear_query(_z_msg_query_t *msg); /*------------------ Zenoh Message ------------------*/ @@ -559,9 +555,8 @@ _z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, // TODO[remove end] typedef struct { - _z_zenoh_body_t _body; uint8_t _header; - _z_reply_context_t *_reply_context; + _z_zenoh_body_t _body; } _z_zenoh_message_t; void _z_msg_clear(_z_zenoh_message_t *m); void _z_msg_free(_z_zenoh_message_t **m); @@ -580,7 +575,7 @@ _z_declaration_t _z_msg_make_declaration_forget_queryable(_z_keyexpr_t key); _z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_payload_t payload, _Bool can_be_dropped); _z_zenoh_message_t _z_msg_make_unit(_Bool can_be_dropped); _z_zenoh_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id, _z_zint_t max_samples, _Bool is_final); -_z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, char *parameters, _z_zint_t qid, z_query_target_t target, +_z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, _z_bytes_t parameters, _z_zint_t qid, z_consolidation_mode_t consolidation, _z_value_t value); _z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_payload_t payload, _Bool can_be_dropped); @@ -608,10 +603,45 @@ typedef struct { void _z_n_msg_clear_declare(_z_n_msg_declare_t *dcl); /*------------------ Push Message ------------------*/ -typedef union { + +typedef struct { + _z_timestamp_t _timestamp; + _z_source_info_t _source_info; +} _z_m_push_commons_t; + +typedef struct { + _z_m_push_commons_t _commons; +} _z_m_del_t; +#define _Z_M_DEL_ID 0x02 +#define _Z_FLAG_Z_D_T 0x20 + +typedef struct { + _z_m_push_commons_t _commons; + _z_bytes_t _payload; + _z_encoding_t _encoding; +} _z_m_put_t; +#define _Z_M_PUT_ID 0x01 +#define _Z_FLAG_Z_P_E 0x40 +#define _Z_FLAG_Z_P_T 0x20 + +typedef struct { + _z_m_push_commons_t _commons; + _Bool _is_put; + union { + _z_m_del_t _del; + _z_m_put_t _put; + } _union; } _z_push_body_t; void _z_push_body_clear(_z_push_body_t *msg); +typedef struct { + uint8_t _val; +} _z_n_qos_t; + +#define _z_n_qos_make(express, nodrop, priority) \ + (_z_n_qos_t) { ._val = ((express << 4) | (nodrop << 3) | priority) } +#define _Z_N_QOS_DEFAULT _z_n_qos_make(0, 0, 5) + // Flags: // - N: Named if N==1 then the keyexpr has name/suffix // - M: Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver @@ -621,17 +651,19 @@ void _z_push_body_clear(_z_push_body_t *msg); // +-+-+-+-+-+-+-+-+ // |Z|M|N| PUSH | // +-+-+-+---------+ -// ~ key_scope:z16 ~ +// ~ key_scope:z? ~ // +---------------+ // ~ key_suffix ~ if N==1 -- // +---------------+ // ~ [push_exts] ~ if Z==1 // +---------------+ -// ~ PushBody ~ +// ~ ZenohMessage ~ // +---------------+ // typedef struct { _z_keyexpr_t _key; + _z_timestamp_t _timestamp; + _z_n_qos_t _qos; _z_push_body_t _body; } _z_n_msg_push_t; void _z_n_msg_clear_push(_z_n_msg_push_t *msg); @@ -658,7 +690,7 @@ void _z_request_body_clear(_z_request_body_t *msg); // +---------------+ // ~ [req_exts] ~ if Z==1 // +---------------+ -// ~ RequestBody ~ +// ~ ZenohMessage ~ // +---------------+ // typedef struct { @@ -674,31 +706,32 @@ typedef union { void _z_response_body_clear(_z_response_body_t *msg); // Flags: -// - N: Named if N==1 then the keyexpr has name/suffix -// - M: Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver -// - Z: Extension if Z==1 then at least one extension is present -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|M|N| RESPONSE| -// +-+-+-+---------+ -// ~ request_id:z32~ -// +---------------+ -// ~ key_scope:z16 ~ -// +---------------+ -// ~ key_suffix ~ if N==1 -- -// +---------------+ -// ~ [reply_exts] ~ if Z==1 -// +---------------+ -// ~ ReplyBody ~ -// +---------------+ -// +// - T: Timestamp If T==1 then the timestamp if present +// - E: Encoding If E==1 then the encoding is present +// - Z: Extension If Z==1 then at least one extension is present +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|E|T| REPLY | +// +-+-+-+---------+ +// ~ ts: ~ if T==1 +// +---------------+ +// ~ encoding ~ if E==1 +// +---------------+ +// ~ [repl_exts] ~ if Z==1 +// +---------------+ +// ~ pl: ~ -- Payload +// +---------------+ typedef struct { - _z_zint_t _rid; - _z_keyexpr_t _key; - _z_response_body_t _body; -} _z_n_msg_response_t; -void _z_n_msg_clear_response(_z_n_msg_response_t *msg); + _z_timestamp_t _timestamp; + _z_encoding_t _encoding; + _z_bytes_t _payload; + _z_source_info_t _source_info; + z_consolidation_mode_t _consolidation; +} _z_msg_reply_t; +void _z_msg_clear_reply(_z_msg_reply_t *msg); +#define _Z_FLAG_Z_R_T 0x20 +#define _Z_FLAG_Z_R_E 0x40 /*------------------ Response Final Message ------------------*/ // Flags: @@ -723,7 +756,7 @@ typedef union { _z_n_msg_declare_t _declare; _z_n_msg_push_t _push; _z_n_msg_request_t _request; - _z_n_msg_response_t _response; + _z_msg_reply_t _response; _z_n_msg_response_final_t _response_f; } _z_network_body_t; typedef struct { diff --git a/include/zenoh-pico/protocol/msgcodec.h b/include/zenoh-pico/protocol/msgcodec.h index 886cd1667..9db399917 100644 --- a/include/zenoh-pico/protocol/msgcodec.h +++ b/include/zenoh-pico/protocol/msgcodec.h @@ -15,6 +15,7 @@ #ifndef ZENOH_PICO_MSGCODEC_H #define ZENOH_PICO_MSGCODEC_H +#include #define _ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE 32 #include "zenoh-pico/collections/element.h" @@ -41,6 +42,12 @@ int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *buf); 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); + +int8_t _z_timestamp_encode(_z_wbuf_t *buf, const _z_timestamp_t *ts); +int8_t _z_timestamp_decode(_z_timestamp_t *ts, _z_zbuf_t *buf); + +int8_t _z_push_body_encode(_z_wbuf_t *buf, const _z_push_body_t *ts); +int8_t _z_push_body_decode(_z_push_body_t *ts, _z_zbuf_t *buf); #endif /* ZENOH_PICO_MSGCODEC_H */ // NOTE: the following headers are for unit testing only @@ -50,10 +57,6 @@ int8_t _z_payload_encode(_z_wbuf_t *buf, const _z_payload_t *pld); int8_t _z_payload_decode(_z_payload_t *pld, _z_zbuf_t *buf); int8_t _z_payload_decode_na(_z_payload_t *pld, _z_zbuf_t *buf); -int8_t _z_timestamp_encode(_z_wbuf_t *buf, const _z_timestamp_t *ts); -int8_t _z_timestamp_decode(_z_timestamp_t *ts, _z_zbuf_t *buf); -int8_t _z_timestamp_decode_na(_z_timestamp_t *ts, _z_zbuf_t *buf); - int8_t _z_subinfo_encode(_z_wbuf_t *buf, const _z_subinfo_t *si); int8_t _z_subinfo_decode(_z_subinfo_t *si, _z_zbuf_t *buf, uint8_t header); int8_t _z_subinfo_decode_na(_z_subinfo_t *si, _z_zbuf_t *buf, uint8_t header); @@ -118,7 +121,6 @@ int8_t _z_declare_decode_na(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf); int8_t _z_push_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_push_t *msg); int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_push_decode_na(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header); int8_t _z_request_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_request_t *msg); int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header); diff --git a/src/net/primitives.c b/src/net/primitives.c index bd5362542..7cf167fb9 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -346,8 +346,7 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, ret = _z_register_pending_query(zn, pq); // Add the pending query to the current session if (ret == _Z_RES_OK) { - _z_zenoh_message_t z_msg = - _z_msg_make_query(keyexpr, pq->_parameters, pq->_id, pq->_target, pq->_consolidation, value); + _z_zenoh_message_t z_msg = _z_msg_make_query(keyexpr, pq->_parameters, pq->_id, pq->_consolidation, value); if (_z_send_z_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { _z_unregister_pending_query(zn, pq); diff --git a/src/protocol/extcodec.c b/src/protocol/extcodec.c index 2f2bc0123..c8d088b4d 100644 --- a/src/protocol/extcodec.c +++ b/src/protocol/extcodec.c @@ -167,41 +167,44 @@ int8_t _z_msg_ext_vec_decode(_z_msg_ext_vec_t *extensions, _z_zbuf_t *zbf) { (void *)extensions); } -int8_t _z_msg_ext_skip_non_mandatory(_z_msg_ext_t *extension, void *ctx) { - int8_t ret = _Z_RES_OK; - if ((extension->_header & _Z_MSG_EXT_FLAG_M) != 0) { - uint8_t trace_id = *(uint8_t *)ctx; - uint8_t ext_id = _Z_EXT_ID(extension->_header); +int8_t _z_msg_ext_unknown_error(_z_msg_ext_t *extension, uint8_t trace_id) { + uint8_t ext_id = _Z_EXT_ID(extension->_header); #if (ZENOH_DEBUG >= 1) - switch (_Z_EXT_ENC(extension->_header)) { - case _Z_MSG_EXT_ENC_UNIT: { - _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), UNIT", ext_id, - trace_id); - break; - } - case _Z_MSG_EXT_ENC_ZINT: { - _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZINT(%02x)", ext_id, - trace_id, extension->_body._zint); - break; - } - case _Z_MSG_EXT_ENC_ZBUF: { - _z_bytes_t buf = extension->_body._zbuf._val; - char *hex = z_malloc(buf.len * 2 + 1); - for (size_t i = 0; i < buf.len; ++i) { - snprintf(hex + 2 * i, 3, "%02x", buf.start[i]); - } - _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZBUF(%.*s)", ext_id, - trace_id, buf.len * 2, hex); - z_free(hex); - break; - } - default: { - _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), UNKOWN_ENCODING", - ext_id, trace_id); + switch (_Z_EXT_ENC(extension->_header)) { + case _Z_MSG_EXT_ENC_UNIT: { + _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), UNIT", ext_id, trace_id); + break; + } + case _Z_MSG_EXT_ENC_ZINT: { + _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZINT(%02x)", ext_id, + trace_id, extension->_body._zint); + break; + } + case _Z_MSG_EXT_ENC_ZBUF: { + _z_bytes_t buf = extension->_body._zbuf._val; + char *hex = z_malloc(buf.len * 2 + 1); + for (size_t i = 0; i < buf.len; ++i) { + snprintf(hex + 2 * i, 3, "%02x", buf.start[i]); } + _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZBUF(%.*s)", ext_id, + trace_id, buf.len * 2, hex); + z_free(hex); + break; + } + default: { + _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), UNKOWN_ENCODING", ext_id, + trace_id); } + } #endif - ret = _Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN; + return _Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN; +} + +int8_t _z_msg_ext_skip_non_mandatory(_z_msg_ext_t *extension, void *ctx) { + int8_t ret = _Z_RES_OK; + if ((extension->_header & _Z_MSG_EXT_FLAG_M) != 0) { + uint8_t trace_id = *(uint8_t *)ctx; + ret = _z_msg_ext_unknown_error(extension, trace_id); } return ret; diff --git a/src/protocol/msg.c b/src/protocol/msg.c index a6b122fc0..af5d14d82 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -331,37 +331,31 @@ _z_zenoh_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id, _z_zint void _z_msg_clear_pull(_z_msg_pull_t *msg) { _z_keyexpr_clear(&msg->_key); } /*------------------ Query Message ------------------*/ -_z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, char *parameters, _z_zint_t qid, z_query_target_t target, +_z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, _z_bytes_t parameters, _z_zint_t qid, z_consolidation_mode_t consolidation, _z_value_t value) { _z_zenoh_message_t msg; - msg._body._query._key = key; msg._body._query._parameters = parameters; - msg._body._query._qid = qid; - msg._body._query._target = target; msg._body._query._consolidation = consolidation; (void)memset(&msg._body._query._info, 0, sizeof(msg._body._query._info)); - msg._body._query._info._encoding = value.encoding; + msg._body._query._encoding = value.encoding; msg._body._query._payload = value.payload; msg._header = _Z_MID_Z_QUERY; - if (msg._body._query._target != Z_QUERY_TARGET_BEST_MATCHING) { - _Z_SET_FLAG(msg._header, _Z_FLAG_Z_T); - } - if (msg._body._query._key._suffix != NULL) { - _Z_SET_FLAG(msg._header, _Z_FLAG_Z_K); - } if (msg._body._query._payload.len > 0) { _Z_SET_FLAG(msg._header, _Z_FLAG_Z_B); } return msg; } - +_z_msg_query_reqexts_t _z_msg_query_required_extensions(_z_msg_query_t *msg) { + return (_z_msg_query_reqexts_t){.body = z_bytes_check(&msg->_payload), + .info = _z_id_check(msg->_info._id), + .consolidation = msg->_consolidation != Z_CONSOLIDATION_MODE_AUTO}; +} void _z_msg_clear_query(_z_msg_query_t *msg) { - _z_keyexpr_clear(&msg->_key); - _z_str_clear(msg->_parameters); - _z_bytes_clear(&msg->_info._encoding.suffix); + _z_bytes_clear(&msg->_parameters); + _z_bytes_clear(&msg->_encoding.suffix); _z_bytes_clear(&msg->_payload); } @@ -495,7 +489,7 @@ _z_network_message_t _z_n_msg_make_response(_z_zint_t rid, _z_keyexpr_t key, _z_ return msg; } -void _z_n_msg_clear_response(_z_n_msg_response_t *msg) { +void _z_msg_clear_reply(_z_msg_reply_t *msg) { _z_keyexpr_clear(&msg->_key); _z_response_body_clear(&msg->_body); } @@ -522,7 +516,7 @@ void _z_n_msg_clear(_z_network_message_t *msg) { _z_n_msg_clear_request(&msg->_body._request); break; case _Z_MID_N_RESPONSE: - _z_n_msg_clear_response(&msg->_body._response); + _z_msg_clear_reply(&msg->_body._response); break; case _Z_MID_N_RESPONSE_FINAL: _z_n_msg_clear_response_final(&msg->_body._response_f); diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 24d3f7cbb..fc0962ff3 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -14,13 +14,16 @@ #include "zenoh-pico/protocol/msgcodec.h" +#include #include #include #include #include "zenoh-pico/api/constants.h" +#include "zenoh-pico/api/types.h" #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/link/endpoint.h" +#include "zenoh-pico/protocol/codec.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/extcodec.h" @@ -86,7 +89,7 @@ int8_t _z_timestamp_encode(_z_wbuf_t *wbf, const _z_timestamp_t *ts) { return ret; } -int8_t _z_timestamp_decode_na(_z_timestamp_t *ts, _z_zbuf_t *zbf) { +int8_t _z_timestamp_decode(_z_timestamp_t *ts, _z_zbuf_t *zbf) { _Z_DEBUG("Decoding _TIMESTAMP\n"); int8_t ret = _Z_RES_OK; @@ -96,8 +99,6 @@ int8_t _z_timestamp_decode_na(_z_timestamp_t *ts, _z_zbuf_t *zbf) { return ret; } -int8_t _z_timestamp_decode(_z_timestamp_t *ts, _z_zbuf_t *zbf) { return _z_timestamp_decode_na(ts, zbf); } - /*------------------ ResKey Field ------------------*/ int8_t _z_keyexpr_encode(_z_wbuf_t *wbf, _Bool has_suffix, const _z_keyexpr_t *fld) { int8_t ret = _Z_RES_OK; @@ -295,6 +296,154 @@ int8_t _z_data_decode(_z_msg_data_t *msg, _z_zbuf_t *zbf, uint8_t header) { return _z_data_decode_na(msg, zbf, header); } +int8_t _z_source_info_decode(_z_source_info_t *info, _z_zbuf_t *zbf) { + uint8_t zidlen = 0; + _z_zint_t intbuf; + int8_t ret = _z_uint8_decode(&zidlen, zbf); + if (ret == _Z_RES_OK) { + zidlen >>= 4; + if (_z_zbuf_len(zbf) >= zidlen) { + _z_zbuf_read_bytes(zbf, info->_id.id, 0, zidlen); + } else { + ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + } + if (ret == _Z_RES_OK) { + ret = _z_zint_decode(&intbuf, zbf); + if (intbuf <= UINT32_MAX) { + info->_entity_id = intbuf; + } else { + ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + } + if (ret == _Z_RES_OK) { + ret = _z_zint_decode(&intbuf, zbf); + if (intbuf <= UINT32_MAX) { + info->_source_sn = intbuf; + } else { + ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + } + return ret; +} +int8_t _z_source_info_encode(_z_wbuf_t *wbf, const _z_source_info_t *info) { + int8_t ret = 0; + uint8_t zidlen = _z_id_len(info->_id); + ret |= _z_uint8_encode(wbf, zidlen << 4); + _z_bytes_t zid = _z_bytes_wrap(info->_id.id, zidlen); + ret |= _z_bytes_val_encode(wbf, &zid); + ret |= _z_zint_encode(wbf, info->_entity_id); + ret |= _z_zint_encode(wbf, info->_source_sn); + return ret; +} + +/*------------------ Push Body Field ------------------*/ +int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { + (void)(wbf); + (void)(pshb); + int8_t ret = _Z_RES_OK; + uint8_t header = pshb->_is_put ? _Z_M_PUT_ID : _Z_M_DEL_ID; + _Bool has_source_info = _z_id_check(pshb->_commons._source_info._id); + _Bool has_timestamp = _z_timestamp_check(&pshb->_commons._timestamp); + _Bool has_encoding = false; + if (has_source_info) { + header |= _Z_FLAG_Z_Z; + } + if (pshb->_is_put) { + if (has_timestamp) { + header |= _Z_FLAG_Z_P_T; + } + has_encoding = pshb->_union._put._encoding.prefix != Z_ENCODING_PREFIX_EMPTY || + !_z_bytes_is_empty(&pshb->_union._put._encoding.suffix); + if (has_encoding) { + header |= _Z_FLAG_Z_P_E; + } + } else { + if (has_timestamp) { + header |= _Z_FLAG_Z_D_T; + } + } + ret = _z_uint8_encode(wbf, header); + if ((ret == _Z_RES_OK) && has_timestamp) { + ret = _z_timestamp_encode(wbf, &pshb->_commons._timestamp); + } + + if ((ret == _Z_RES_OK) && has_encoding) { + ret = _z_encoding_prefix_encode(wbf, pshb->_union._put._encoding.prefix); + ret |= _z_bytes_encode(wbf, &pshb->_union._put._encoding.suffix); + } + + if ((ret == _Z_RES_OK) && has_source_info) { + ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x01); + ret |= _z_source_info_encode(wbf, &pshb->_commons._source_info); + } + + if ((ret == _Z_RES_OK) && pshb->_is_put) { + ret = _z_bytes_encode(wbf, &pshb->_union._put._payload); + } + + return ret; +} +int8_t _z_push_body_decode_extensions(_z_msg_ext_t *extension, void *ctx) { + _z_push_body_t *pshb = (_z_push_body_t *)ctx; + int8_t ret = _Z_RES_OK; + switch (_Z_EXT_FULL_ID(extension->_header)) { + case _Z_MSG_EXT_ENC_ZBUF | 0x01: { + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + ret = _z_source_info_decode(&pshb->_commons._source_info, &zbf); + break; + } + default: + if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { + ret = _z_msg_ext_unknown_error(extension, 0x08); + } + } + return ret; +} + +int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf) { + (void)(zbf); + (void)(pshb); + int8_t ret = _Z_RES_OK; + uint8_t header; + ret = _z_uint8_decode(&header, zbf); + if (ret == _Z_RES_OK) { + switch (_Z_MID(header)) { + case _Z_M_PUT_ID: { + pshb->_is_put = true; + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_P_T)) { + ret = _z_timestamp_decode(&pshb->_commons._timestamp, zbf); + } + if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_P_E)) { + ret = _z_encoding_prefix_decode(&pshb->_union._put._encoding.prefix, zbf); + } + if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + ret = _z_msg_ext_decode_iter(zbf, _z_push_body_decode_extensions, pshb); + } + if (ret == _Z_RES_OK) { + ret = _z_bytes_decode(&pshb->_union._put._payload, zbf); + } + break; + } + case _Z_M_DEL_ID: { + pshb->_is_put = false; + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_D_T)) { + ret = _z_timestamp_decode(&pshb->_commons._timestamp, zbf); + } + if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + ret = _z_msg_ext_decode_iter(zbf, _z_push_body_decode_extensions, pshb); + } + break; + } + default: { + ret = _Z_ERR_MESSAGE_ZENOH_UNKNOWN; + } + } + } + + return ret; +} + /*------------------ Pull Message ------------------*/ int8_t _z_pull_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_pull_t *msg) { int8_t ret = _Z_RES_OK; @@ -335,57 +484,153 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_query_t *msg int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_Z_QUERY\n"); - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &msg->_key)) - - _Z_EC(_z_str_encode(wbf, msg->_parameters)) - - _Z_EC(_z_zint_encode(wbf, msg->_qid)) - - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_T) == true) { - _Z_EC(_z_query_target_encode(wbf, msg->_target)) + if (z_bytes_check(&msg->_parameters)) { + assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_P)); + ret = _z_bytes_encode(wbf, &msg->_parameters); + } else { + assert(!_Z_HAS_FLAG(header, _Z_FLAG_Z_P)); } - ret |= _z_consolidation_mode_encode(wbf, msg->_consolidation); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_B) == true) { - _Z_EC(_z_data_info_encode(wbf, &msg->_info)) - _Z_EC(_z_payload_encode(wbf, &msg->_payload)) + _z_msg_query_reqexts_t required_exts = _z_msg_query_required_extensions(msg); + if (required_exts.body || required_exts.consolidation || required_exts.info) { + assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); + } + if ((ret == _Z_RES_OK) && required_exts.body) { + uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x03; + if (required_exts.consolidation || required_exts.info) { + extheader |= _Z_FLAG_Z_Z; + } + ret = _z_uint8_encode(wbf, extheader); + ret |= _z_encoding_prefix_encode(wbf, msg->_encoding.prefix); + ret |= _z_bytes_encode(wbf, &msg->_encoding.suffix); + ret |= _z_bytes_encode(wbf, &msg->_payload); + } + if ((ret == _Z_RES_OK) && required_exts.consolidation) { + uint8_t extheader = _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M | 0x02; + if (required_exts.info) { + extheader |= _Z_FLAG_Z_Z; + } + ret = _z_uint8_encode(wbf, extheader); + ret |= _z_zint_encode(wbf, msg->_consolidation); + } + if ((ret == _Z_RES_OK) && required_exts.info) { + uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01; + ret = _z_source_info_encode(wbf, &msg->_info); } return ret; } -int8_t _z_query_decode_na(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_Z_QUERY\n"); +int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) { + _z_msg_query_t *msg = (_z_msg_query_t *)ctx; int8_t ret = _Z_RES_OK; + switch (_Z_EXT_FULL_ID(extension->_header)) { + case _Z_MSG_EXT_ENC_ZBUF | 0x01: { // Source Info + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + ret = _z_source_info_decode(&msg->_info, &zbf); + break; + } + case _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M | 0x02: { // Consolidation + msg->_consolidation = extension->_body._zint._val; + break; + } + case _Z_MSG_EXT_ENC_ZBUF | 0x03: { // Payload + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + ret = _z_encoding_prefix_decode(&msg->_encoding.prefix, &zbf); + ret |= _z_bytes_decode(&msg->_encoding.suffix, &zbf); + ret |= _z_bytes_decode(&msg->_payload, &zbf); + break; + } + default: + if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { + ret = _z_msg_ext_unknown_error(extension, 0x09); + } + } + return ret; +} - ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - ret |= _z_str_decode(&msg->_parameters, zbf); - ret |= _z_zint_decode(&msg->_qid, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_T) == true) { - ret |= _z_query_target_decode(&msg->_target, zbf); +int8_t _z_query_decode(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_Z_QUERY\n"); + int8_t ret = _Z_RES_OK; + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_P)) { + ret = _z_bytes_decode(&msg->_parameters, zbf); } else { - msg->_target = Z_QUERY_TARGET_BEST_MATCHING; + _z_bytes_clear(&msg->_parameters); } - ret |= _z_consolidation_mode_decode(&msg->_consolidation, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_I) == true) { - ret |= _z_data_info_decode(&msg->_info, zbf); - ret |= _z_payload_decode(&msg->_payload, zbf); - } else { - msg->_info._flags = 0; - msg->_info._kind = Z_SAMPLE_KIND_PUT; - msg->_info._encoding.prefix = Z_ENCODING_PREFIX_EMPTY; - msg->_info._encoding.suffix = _z_bytes_empty(); - msg->_info._source_id = _z_bytes_empty(); - msg->_info._source_sn = 0; - _z_timestamp_reset(&msg->_info._tstamp); - msg->_payload = _z_bytes_empty(); + + if (ret == _Z_RES_OK) { + ret = _z_msg_ext_decode_iter(zbf, _z_query_decode_extensions, msg); } return ret; } -int8_t _z_query_decode(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_query_decode_na(msg, zbf, header); +int8_t _z_reply_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_reply_t *reply) { + int8_t ret = _Z_RES_OK; + if (_z_timestamp_check(&reply->_timestamp)) { + assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_R_T)); + ret = _z_timestamp_encode(wbf, &reply->_timestamp); + } + if ((ret == _Z_RES_OK) && ((reply->_encoding.prefix != 0) || !_z_bytes_is_empty(&reply->_encoding.suffix))) { + assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_R_E)); + ret = _z_encoding_prefix_encode(wbf, reply->_encoding.prefix); + ret |= _z_bytes_encode(wbf, &reply->_encoding.suffix); + } + _Bool has_consolidation_ext = reply->_consolidation != Z_CONSOLIDATION_MODE_AUTO; + if ((ret == _Z_RES_OK) && _z_id_check(reply->_source_info._id)) { + uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01; + if (has_consolidation_ext) { + extheader |= _Z_MSG_EXT_FLAG_Z; + } + ret = _z_uint8_encode(wbf, extheader); + ret |= _z_source_info_encode(wbf, &reply->_source_info); + } + if ((ret == _Z_RES_OK) && has_consolidation_ext) { + ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M | 0x02); + ret |= _z_zint_encode(wbf, reply->_consolidation); + } + if (ret == _Z_RES_OK) { + ret = _z_bytes_encode(wbf, &reply->_payload); + } + return ret; +} +int8_t _z_reply_decode_extension(_z_msg_ext_t *extension, void *ctx) { + int8_t ret = _Z_RES_OK; + _z_msg_reply_t *reply = (_z_msg_reply_t *)ctx; + switch (_Z_EXT_FULL_ID(extension->_header)) { + case _Z_MSG_EXT_ENC_ZBUF | 0x01: { + __auto_type zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + ret = _z_source_info_decode(&reply->_source_info, &zbf); + break; + } + case _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M | 0x02: { + reply->_consolidation = extension->_body._zint._val; + break; + } + default: + if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { + ret = _z_msg_ext_unknown_error(extension, 0x0a); + } + } + return ret; +} +int8_t _z_reply_decode(_z_msg_reply_t *reply, _z_zbuf_t *zbf, uint8_t header) { + int8_t ret = _Z_RES_OK; + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_R_T)) { + ret = _z_timestamp_decode(&reply->_timestamp, zbf); + } + if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_R_E)) { + ret = _z_encoding_prefix_decode(&reply->_encoding.prefix, zbf); + ret |= _z_bytes_decode(&reply->_encoding.suffix, zbf); + } + if (ret == _Z_RES_OK) { + ret = _z_msg_ext_decode_iter(zbf, _z_reply_decode_extension, reply); + } + if (ret == _Z_RES_OK) { + ret = _z_bytes_decode(&reply->_payload, zbf); + } + + return ret; } /*------------------ Zenoh Message ------------------*/ @@ -575,13 +820,7 @@ int8_t _z_hello_decode(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t _z_scouting_message_encode(_z_wbuf_t *wbf, const _z_scouting_message_t *msg) { int8_t ret = _Z_RES_OK; - // Encode the decorators if present - uint8_t header = msg->_header; - // size_t n_ext = _z_msg_ext_vec_len(&msg->_extensions); - // if (n_ext > 0) { - // header |= _Z_FLAG_T_Z; - // } _Z_EC(_z_wbuf_write(wbf, header)) switch (_Z_MID(msg->_header)) { @@ -599,8 +838,6 @@ int8_t _z_scouting_message_encode(_z_wbuf_t *wbf, const _z_scouting_message_t *m } break; } - // ret |= _z_msg_ext_vec_encode(wbf, &msg->_extensions); - return ret; } int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *zbf) { @@ -635,7 +872,7 @@ int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *zbf) } while ((ret == _Z_RES_OK) && (is_last == false)); if ((ret == _Z_RES_OK) && (msg->_header & _Z_MSG_EXT_FLAG_Z) != 0) { - ret |= _z_msg_ext_skip_non_mandatories(zbf); + ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x06); } return ret; diff --git a/src/protocol/network_codec.c b/src/protocol/network_codec.c index 9f410d3a5..9fb3a385a 100644 --- a/src/protocol/network_codec.c +++ b/src/protocol/network_codec.c @@ -12,9 +12,15 @@ // ZettaScale Zenoh Team, // +#include #include +#include "zenoh-pico/protocol/codec.h" +#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/extcodec.h" +#include "zenoh-pico/protocol/iobuf.h" +#include "zenoh-pico/protocol/msg.h" #include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" @@ -399,50 +405,72 @@ int8_t _z_declare_decode_na(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf) { int8_t _z_declare_decode(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf) { return _z_declare_decode_na(msg, zbf); } -/*------------------ Push Body Field ------------------*/ -int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { - (void)(wbf); - (void)(pshb); +/*------------------ Push Message ------------------*/ +int8_t _z_push_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_push_t *msg) { int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_N_PUSH\n"); - return ret; -} + ret = _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N), &msg->_key); -int8_t _z_push_body_decode_na(_z_push_body_t *pshb, _z_zbuf_t *zbf) { - (void)(zbf); - (void)(pshb); - int8_t ret = _Z_RES_OK; + _Bool has_timestamp_ext = _z_timestamp_check(&msg->_timestamp); - return ret; -} + if ((ret == _Z_RES_OK) && msg->_qos._val != _Z_N_QOS_DEFAULT._val) { + ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | 0x01 | (has_timestamp_ext << 7)); + ret |= _z_uint8_encode(wbf, msg->_qos._val); + } -int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf) { return _z_push_body_decode_na(pshb, zbf); } + if ((ret == _Z_RES_OK) && has_timestamp_ext) { + ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x02); + ret |= _z_timestamp_encode(wbf, &msg->_timestamp); + } -/*------------------ Push Message ------------------*/ -int8_t _z_push_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_push_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_N_PUSH\n"); + if (ret == _Z_RES_OK) { + ret = _z_push_body_encode(wbf, &msg->_body); + } - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N), &msg->_key)); - _Z_EC(_z_push_body_encode(wbf, &msg->_body)); + return ret; +} +int8_t _z_push_decode_ext_cb(_z_msg_ext_t *extension, void *ctx) { + int8_t ret = _Z_RES_OK; + _z_n_msg_push_t *msg = (_z_n_msg_push_t *)ctx; + switch (_Z_EXT_FULL_ID(extension->_header)) { + case _Z_MSG_EXT_ENC_ZINT | 0x01: { // QOS ext + msg->_qos = (_z_n_qos_t){._val = extension->_body._zint._val}; + break; + } + case _Z_MSG_EXT_ENC_ZBUF | 0x02: { // Timestamp ext + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + ret = _z_timestamp_decode(&msg->_timestamp, &zbf); + break; + } + default: + if ((extension->_header & _Z_MSG_EXT_FLAG_M) != 0) { + ret = _z_msg_ext_unknown_error(extension, 0x07); + } + } return ret; } -int8_t _z_push_decode_na(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_N_PUSH\n"); int8_t ret = _Z_RES_OK; + _z_timestamp_reset(&msg->_timestamp); ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N)); - ret |= _z_push_body_decode(&msg->_body, zbf); + if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_N_Z)) { + ret = _z_msg_ext_decode_iter(zbf, _z_push_decode_ext_cb, msg); + } + if (ret == _Z_RES_OK) { + ret = _z_push_body_decode(&msg->_body, zbf); + } + if (ret == _Z_RES_OK) { + // TODO[protocol] decode put/del + } return ret; } -int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_push_decode_na(msg, zbf, header); -} - /*------------------ Request Body Field ------------------*/ int8_t _z_request_body_encode(_z_wbuf_t *wbf, const _z_request_body_t *reqb) { (void)(wbf); @@ -511,7 +539,7 @@ int8_t _z_response_body_decode(_z_response_body_t *rspb, _z_zbuf_t *zbf) { } /*------------------ Response Message ------------------*/ -int8_t _z_response_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_t *msg) { +int8_t _z_response_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_reply_t *msg) { int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); @@ -522,7 +550,7 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_respons return ret; } -int8_t _z_response_decode_na(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_response_decode_na(_z_n_msg_reply_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); int8_t ret = _Z_RES_OK; @@ -533,7 +561,7 @@ int8_t _z_response_decode_na(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t h return ret; } -int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_response_decode(_z_n_msg_reply_t *msg, _z_zbuf_t *zbf, uint8_t header) { return _z_response_decode_na(msg, zbf, header); } diff --git a/src/protocol/transport_codec.c b/src/protocol/transport_codec.c index c138cf9d0..08088ca82 100644 --- a/src/protocol/transport_codec.c +++ b/src/protocol/transport_codec.c @@ -385,7 +385,7 @@ int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t head ret |= _z_zint_decode(&msg->_sn, zbf); if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { - ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x04); + ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x05); } ret |= _z_bytes_decode(&msg->_payload, zbf); diff --git a/src/session/queryable.c b/src/session/queryable.c index 2d2e28510..953cdbd3a 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -179,7 +179,6 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query) { // Create the final reply _z_zenoh_message_t z_msg = _z_msg_make_unit(can_be_dropped); - z_msg._reply_context = rctx; if (_z_send_z_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { ret = _Z_ERR_TRANSPORT_TX_FAILED; diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 766dc3c1e..175274c16 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -1929,7 +1929,7 @@ _z_network_message_t gen_response_message(void) { return n_msg; } -void assert_eq_response_message(_z_n_msg_response_t *left, _z_n_msg_response_t *right, uint8_t header) { +void assert_eq_response_message(_z_msg_reply_t *left, _z_msg_reply_t *right, uint8_t header) { printf(" "); assert(left->_rid == right->_rid); printf("\n"); @@ -1949,7 +1949,7 @@ void response_message(void) { _z_network_message_t n_msg = gen_response_message(); assert(_Z_MID(n_msg._header) == _Z_MID_N_RESPONSE); - _z_n_msg_response_t e_re = n_msg._body._response; + _z_msg_reply_t e_re = n_msg._body._response; // Encode int8_t res = _z_response_encode(&wbf, n_msg._header, &e_re); @@ -1958,14 +1958,14 @@ void response_message(void) { // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_n_msg_response_t d_re; + _z_msg_reply_t d_re; res = _z_response_decode(&d_re, &zbf, n_msg._header); assert(res == _Z_RES_OK); assert_eq_response_message(&e_re, &d_re, n_msg._header); // Free - _z_n_msg_clear_response(&d_re); + _z_msg_clear_reply(&d_re); _z_n_msg_clear(&n_msg); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); From f2bb0e1636b80e9f7f6eea4c5fd7fd2054c1dd3e Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 19 Jun 2023 14:49:57 +0200 Subject: [PATCH 102/181] more zenoh-level messages [skip ci] --- include/zenoh-pico/protocol/msg.h | 37 +++++++++--- src/protocol/msg.c | 20 +++---- src/protocol/msgcodec.c | 97 ++++++++++++++++++++++++++----- tests/z_msgcodec_test.c | 20 +------ 4 files changed, 121 insertions(+), 53 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 117b06e20..f09cc33e1 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -520,12 +520,11 @@ void _z_msg_clear_pull(_z_msg_pull_t *msg); // +---------------+ // ~ [qry_exts] ~ if Z==1 // +---------------+ -#define _Z_FLAG_Z_Q_P 0x20 // 1 << 7 | Period if P==1 then a period is present +#define _Z_FLAG_Z_Q_P 0x20 // 1 << 6 | Period if P==1 then a period is present typedef struct { _z_bytes_t _parameters; _z_source_info_t _info; - _z_payload_t _payload; - _z_encoding_t _encoding; + _z_value_t _value; z_consolidation_mode_t _consolidation; } _z_msg_query_t; typedef struct { @@ -550,7 +549,6 @@ typedef struct { _z_zint_t _qid; uint8_t _header; } _z_reply_context_t; -void _z_msg_clear_reply_context(_z_reply_context_t *rc); _z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, _Bool is_final); // TODO[remove end] @@ -724,15 +722,40 @@ void _z_response_body_clear(_z_response_body_t *msg); // +---------------+ typedef struct { _z_timestamp_t _timestamp; - _z_encoding_t _encoding; - _z_bytes_t _payload; + _z_value_t _value; _z_source_info_t _source_info; z_consolidation_mode_t _consolidation; } _z_msg_reply_t; -void _z_msg_clear_reply(_z_msg_reply_t *msg); +void _z_msg_reply_clear(_z_msg_reply_t *msg); #define _Z_FLAG_Z_R_T 0x20 #define _Z_FLAG_Z_R_E 0x40 +// Flags: +// - T: Timestamp If T==1 then the timestamp if present +// - I: Infrastructure If I==1 then the error is related to the infrastructure else to the user +// - Z: Extension If Z==1 then at least one extension is present +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|I|T| ERR | +// +-+-+-+---------+ +// % code:z16 % +// +---------------+ +// ~ ts: ~ if T==1 +// +---------------+ +// ~ [err_exts] ~ if Z==1 +// +---------------+ +#define _Z_FLAG_Z_E_T 0x20 +#define _Z_FLAG_Z_E_I 0x40 +typedef struct { + uint16_t _code; + _Bool _is_infrastructure; + _z_timestamp_t _timestamp; + _z_source_info_t _ext_source_info; + _z_value_t _ext_value; +} _z_msg_err_t; +void _z_msg_err_clear(_z_msg_err_t *err); + /*------------------ Response Final Message ------------------*/ // Flags: // - Z: Extension if Z==1 then at least one extension is present diff --git a/src/protocol/msg.c b/src/protocol/msg.c index af5d14d82..8f7976a40 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -41,7 +41,6 @@ _z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, return rctx; } -void _z_msg_clear_reply_context(_z_reply_context_t *rc) {} /*------------------ Payload field ------------------*/ void _z_payload_clear(_z_payload_t *p) { _z_bytes_clear(p); } @@ -338,25 +337,23 @@ _z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, _z_bytes_t parameters, _z msg._body._query._parameters = parameters; msg._body._query._consolidation = consolidation; (void)memset(&msg._body._query._info, 0, sizeof(msg._body._query._info)); - msg._body._query._encoding = value.encoding; - msg._body._query._payload = value.payload; + msg._body._query._value = value; msg._header = _Z_MID_Z_QUERY; - if (msg._body._query._payload.len > 0) { + if (msg._body._query._value.payload.len > 0) { _Z_SET_FLAG(msg._header, _Z_FLAG_Z_B); } return msg; } -_z_msg_query_reqexts_t _z_msg_query_required_extensions(_z_msg_query_t *msg) { - return (_z_msg_query_reqexts_t){.body = z_bytes_check(&msg->_payload), +_z_msg_query_reqexts_t _z_msg_query_required_extensions(const _z_msg_query_t *msg) { + return (_z_msg_query_reqexts_t){.body = msg->_value.payload.start != NULL, .info = _z_id_check(msg->_info._id), .consolidation = msg->_consolidation != Z_CONSOLIDATION_MODE_AUTO}; } void _z_msg_clear_query(_z_msg_query_t *msg) { _z_bytes_clear(&msg->_parameters); - _z_bytes_clear(&msg->_encoding.suffix); - _z_bytes_clear(&msg->_payload); + _z_value_clear(&msg->_value); } /*------------------ Reply Message ------------------*/ @@ -489,10 +486,7 @@ _z_network_message_t _z_n_msg_make_response(_z_zint_t rid, _z_keyexpr_t key, _z_ return msg; } -void _z_msg_clear_reply(_z_msg_reply_t *msg) { - _z_keyexpr_clear(&msg->_key); - _z_response_body_clear(&msg->_body); -} +void _z_msg_reply_clear(_z_msg_reply_t *msg) { _z_value_clear(&msg->_value); } _z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid) { _z_network_message_t msg; @@ -516,7 +510,7 @@ void _z_n_msg_clear(_z_network_message_t *msg) { _z_n_msg_clear_request(&msg->_body._request); break; case _Z_MID_N_RESPONSE: - _z_msg_clear_reply(&msg->_body._response); + _z_msg_reply_clear(&msg->_body._response); break; case _Z_MID_N_RESPONSE_FINAL: _z_n_msg_clear_response_final(&msg->_body._response_f); diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index fc0962ff3..502f31f85 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -501,9 +501,9 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_query_t *msg extheader |= _Z_FLAG_Z_Z; } ret = _z_uint8_encode(wbf, extheader); - ret |= _z_encoding_prefix_encode(wbf, msg->_encoding.prefix); - ret |= _z_bytes_encode(wbf, &msg->_encoding.suffix); - ret |= _z_bytes_encode(wbf, &msg->_payload); + ret |= _z_encoding_prefix_encode(wbf, msg->_value.encoding.prefix); + ret |= _z_bytes_encode(wbf, &msg->_value.encoding.suffix); + ret |= _z_bytes_encode(wbf, &msg->_value.payload); } if ((ret == _Z_RES_OK) && required_exts.consolidation) { uint8_t extheader = _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M | 0x02; @@ -536,9 +536,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_zbytes_as_zbuf(extension->_body._zbuf._val); - ret = _z_encoding_prefix_decode(&msg->_encoding.prefix, &zbf); - ret |= _z_bytes_decode(&msg->_encoding.suffix, &zbf); - ret |= _z_bytes_decode(&msg->_payload, &zbf); + ret = _z_encoding_prefix_decode(&msg->_value.encoding.prefix, &zbf); + ret |= _z_bytes_decode(&msg->_value.encoding.suffix, &zbf); + ret |= _z_bytes_decode(&msg->_value.payload, &zbf); break; } default: @@ -571,10 +571,11 @@ int8_t _z_reply_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_reply_t *rep assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_R_T)); ret = _z_timestamp_encode(wbf, &reply->_timestamp); } - if ((ret == _Z_RES_OK) && ((reply->_encoding.prefix != 0) || !_z_bytes_is_empty(&reply->_encoding.suffix))) { + if ((ret == _Z_RES_OK) && + ((reply->_value.encoding.prefix != 0) || !_z_bytes_is_empty(&reply->_value.encoding.suffix))) { assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_R_E)); - ret = _z_encoding_prefix_encode(wbf, reply->_encoding.prefix); - ret |= _z_bytes_encode(wbf, &reply->_encoding.suffix); + ret = _z_encoding_prefix_encode(wbf, reply->_value.encoding.prefix); + ret |= _z_bytes_encode(wbf, &reply->_value.encoding.suffix); } _Bool has_consolidation_ext = reply->_consolidation != Z_CONSOLIDATION_MODE_AUTO; if ((ret == _Z_RES_OK) && _z_id_check(reply->_source_info._id)) { @@ -590,7 +591,7 @@ int8_t _z_reply_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_reply_t *rep ret |= _z_zint_encode(wbf, reply->_consolidation); } if (ret == _Z_RES_OK) { - ret = _z_bytes_encode(wbf, &reply->_payload); + ret = _z_bytes_encode(wbf, &reply->_value.payload); } return ret; } @@ -599,7 +600,7 @@ int8_t _z_reply_decode_extension(_z_msg_ext_t *extension, void *ctx) { _z_msg_reply_t *reply = (_z_msg_reply_t *)ctx; switch (_Z_EXT_FULL_ID(extension->_header)) { case _Z_MSG_EXT_ENC_ZBUF | 0x01: { - __auto_type zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); ret = _z_source_info_decode(&reply->_source_info, &zbf); break; } @@ -620,14 +621,82 @@ int8_t _z_reply_decode(_z_msg_reply_t *reply, _z_zbuf_t *zbf, uint8_t header) { ret = _z_timestamp_decode(&reply->_timestamp, zbf); } if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_R_E)) { - ret = _z_encoding_prefix_decode(&reply->_encoding.prefix, zbf); - ret |= _z_bytes_decode(&reply->_encoding.suffix, zbf); + ret = _z_encoding_prefix_decode(&reply->_value.encoding.prefix, zbf); + ret |= _z_bytes_decode(&reply->_value.encoding.suffix, zbf); } if (ret == _Z_RES_OK) { ret = _z_msg_ext_decode_iter(zbf, _z_reply_decode_extension, reply); } if (ret == _Z_RES_OK) { - ret = _z_bytes_decode(&reply->_payload, zbf); + ret = _z_bytes_decode(&reply->_value.payload, zbf); + } + + return ret; +} + +int8_t _z_err_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_err_t *err) { + int8_t ret = _z_zint_encode(wbf, err->_code); + if ((ret == _Z_RES_OK) && _z_timestamp_check(&err->_timestamp)) { + assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_E_T)); + ret = _z_timestamp_encode(wbf, &err->_timestamp); + } + _Bool has_payload_ext = err->_ext_value.payload.start != NULL; + if ((ret == _Z_RES_OK) && _z_id_check(err->_ext_source_info._id)) { + assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); + uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01; + if (has_payload_ext) { + extheader |= _Z_MSG_EXT_FLAG_Z; + } + ret = _z_uint8_encode(wbf, extheader); + ret |= _z_source_info_encode(wbf, &err->_ext_source_info); + } + if ((ret == _Z_RES_OK) && has_payload_ext) { + assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); + ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x02); + ret |= _z_encoding_prefix_encode(wbf, err->_ext_value.encoding.prefix); + ret |= _z_bytes_encode(wbf, &err->_ext_value.encoding.suffix); + ret |= _z_bytes_encode(wbf, &err->_ext_value.payload); + } + return ret; +} +int8_t _z_err_decode_extension(_z_msg_ext_t *extension, void *ctx) { + int8_t ret = _Z_RES_OK; + _z_msg_err_t *reply = (_z_msg_err_t *)ctx; + switch (_Z_EXT_FULL_ID(extension->_header)) { + case _Z_MSG_EXT_ENC_ZBUF | 0x01: { + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + ret = _z_source_info_decode(&reply->_ext_source_info, &zbf); + break; + } + case _Z_MSG_EXT_ENC_ZBUF | 0x02: { + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + ret = _z_encoding_prefix_decode(&reply->_ext_value.encoding.prefix, &zbf); + ret |= _z_bytes_decode(&reply->_ext_value.encoding.suffix, &zbf); + ret |= _z_bytes_decode(&reply->_ext_value.payload, &zbf); + break; + } + default: + if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { + ret = _z_msg_ext_unknown_error(extension, 0x0a); + } + } + return ret; +} +int8_t _z_err_decode(_z_msg_err_t *err, _z_zbuf_t *zbf, uint8_t header) { + int8_t ret = _Z_RES_OK; + _z_zint_t code; + ret = _z_zint_decode(&code, zbf); + if (code <= UINT16_MAX) { + err->_code = code; + } else { + ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + err->_is_infrastructure = _Z_HAS_FLAG(header, _Z_FLAG_Z_E_I); + if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_E_T)) { + ret = _z_timestamp_decode(&err->_timestamp, zbf); + } + if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + ret = _z_msg_ext_decode_iter(zbf, _z_err_decode_extension, err); } return ret; diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 175274c16..f973c6124 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -1501,24 +1501,6 @@ _z_zenoh_message_t gen_query_message(void) { } void assert_eq_query_message(_z_msg_query_t *left, _z_msg_query_t *right, uint8_t header) { - printf(" "); - assert_eq_keyexpr(&left->_key, &right->_key, header); - printf("\n"); - - printf(" Predicate (%s:%s)", left->_parameters, right->_parameters); - assert(_z_str_eq(left->_parameters, right->_parameters) == true); - printf("\n"); - - printf(" Query ID (%zu:%zu)", left->_qid, right->_qid); - assert(left->_qid == right->_qid); - printf("\n"); - - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_T) == true) { - printf("Target (%u:%u), ", left->_target, right->_target); - assert(left->_target == right->_target); - printf("\n"); - } - printf(" Consolidation ( %u:%u)", left->_consolidation, right->_consolidation); assert(left->_consolidation == right->_consolidation); printf("\n"); @@ -1965,7 +1947,7 @@ void response_message(void) { assert_eq_response_message(&e_re, &d_re, n_msg._header); // Free - _z_msg_clear_reply(&d_re); + _z_msg_reply_clear(&d_re); _z_n_msg_clear(&n_msg); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); From ed7ef95f526a91d7cba22fecd422b5104025fada Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 19 Jun 2023 17:18:49 +0200 Subject: [PATCH 103/181] more+ zenoh-level messages [skip ci] --- include/zenoh-pico/protocol/msg.h | 52 ++++++++++----- src/protocol/msgcodec.c | 106 ++++++++++++++++++++---------- 2 files changed, 105 insertions(+), 53 deletions(-) diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index f09cc33e1..68263e902 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -491,23 +491,6 @@ typedef struct { void _z_msg_clear_unit(_z_msg_unit_t *unt); /*------------------ Pull Message ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |K|N|F| PULL | -// +-+-+-+---------+ -// ~ ResKey ~ if K==1 then keyexpr is string -// +---------------+ -// ~ pullid ~ -// +---------------+ -// ~ max_samples ~ if N==1 -// +---------------+ -// -typedef struct { - _z_keyexpr_t _key; - _z_zint_t _pull_id; - _z_zint_t _max_samples; -} _z_msg_pull_t; -void _z_msg_clear_pull(_z_msg_pull_t *msg); /*------------------ Query Message ------------------*/ // 7 6 5 4 3 2 1 0 @@ -539,7 +522,6 @@ void _z_msg_clear_query(_z_msg_query_t *msg); typedef union { _z_msg_data_t _data; _z_msg_query_t _query; - _z_msg_pull_t _pull; _z_msg_unit_t _unit; } _z_zenoh_body_t; @@ -756,6 +738,40 @@ typedef struct { } _z_msg_err_t; void _z_msg_err_clear(_z_msg_err_t *err); +/// Flags: +/// - T: Timestamp If T==1 then the timestamp if present +/// - X: Reserved +/// - Z: Extension If Z==1 then at least one extension is present +/// +/// 7 6 5 4 3 2 1 0 +/// +-+-+-+-+-+-+-+-+ +/// |Z|X|T| ACK | +/// +-+-+-+---------+ +/// ~ ts: ~ if T==1 +/// +---------------+ +/// ~ [err_exts] ~ if Z==1 +/// +---------------+ +typedef struct { + _z_timestamp_t _timestamp; + _z_source_info_t _ext_source_info; +} _z_msg_ack_t; +#define _Z_FLAG_Z_A_T 0x20 + +/// Flags: +/// - T: Timestamp If T==1 then the timestamp if present +/// - X: Reserved +/// - Z: Extension If Z==1 then at least one extension is present +/// +/// 7 6 5 4 3 2 1 0 +/// +-+-+-+-+-+-+-+-+ +/// |Z|X|X| PULL | +/// +---------------+ +/// ~ [pull_exts] ~ if Z==1 +/// +---------------+ +typedef struct { + _z_source_info_t _ext_source_info; +} _z_msg_pull_t; + /*------------------ Response Final Message ------------------*/ // Flags: // - Z: Extension if Z==1 then at least one extension is present diff --git a/src/protocol/msgcodec.c b/src/protocol/msgcodec.c index 502f31f85..72ba8e66e 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/msgcodec.c @@ -444,41 +444,6 @@ int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf) { return ret; } -/*------------------ Pull Message ------------------*/ -int8_t _z_pull_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_pull_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_Z_PULL\n"); - - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &msg->_key)) - - _Z_EC(_z_zint_encode(wbf, msg->_pull_id)) - - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_N) == true) { - _Z_EC(_z_zint_encode(wbf, msg->_max_samples)) - } - - return ret; -} - -int8_t _z_pull_decode_na(_z_msg_pull_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_Z_PULL\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - ret |= _z_zint_decode(&msg->_pull_id, zbf); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_N) == true) { - ret |= _z_zint_decode(&msg->_max_samples, zbf); - } else { - msg->_max_samples = 1; // FIXME: confirm default value - } - - return ret; -} - -int8_t _z_pull_decode(_z_msg_pull_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_pull_decode_na(msg, zbf, header); -} - /*------------------ Query Message ------------------*/ int8_t _z_query_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_query_t *msg) { int8_t ret = _Z_RES_OK; @@ -702,6 +667,77 @@ int8_t _z_err_decode(_z_msg_err_t *err, _z_zbuf_t *zbf, uint8_t header) { return ret; } +int8_t _z_ack_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_ack_t *ack) { + int8_t ret = _Z_RES_OK; + if (_z_timestamp_check(&ack->_timestamp)) { + assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_A_T)); + ret = _z_timestamp_encode(wbf, &ack->_timestamp); + } + if ((ret == _Z_RES_OK) && _z_id_check(ack->_ext_source_info._id)) { + ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x01); + ret |= _z_source_info_encode(wbf, &ack->_ext_source_info); + } + return ret; +} +int8_t _z_ack_decode_extension(_z_msg_ext_t *extension, void *ctx) { + int8_t ret = _Z_RES_OK; + _z_msg_ack_t *ack = (_z_msg_ack_t *)ctx; + switch (_Z_EXT_FULL_ID(extension->_header)) { + case _Z_MSG_EXT_ENC_ZBUF | 0x01: { + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + ret = _z_source_info_decode(&ack->_ext_source_info, &zbf); + break; + } + default: + if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { + ret = _z_msg_ext_unknown_error(extension, 0x0a); + } + } + return ret; +} +int8_t _z_ack_decode(_z_msg_ack_t *ack, _z_zbuf_t *zbf, uint8_t header) { + int8_t ret = _Z_RES_OK; + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_A_T)) { + ret = _z_timestamp_decode(&ack->_timestamp, zbf); + } + if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + ret = _z_msg_ext_decode_iter(zbf, _z_ack_decode_extension, ack); + } + return ret; +} + +int8_t _z_pull_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_pull_t *pull) { + int8_t ret = _Z_RES_OK; + if ((ret == _Z_RES_OK) && _z_id_check(pull->_ext_source_info._id)) { + ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x01); + ret |= _z_source_info_encode(wbf, &pull->_ext_source_info); + } + return ret; +} +int8_t _z_pull_decode_extension(_z_msg_ext_t *extension, void *ctx) { + int8_t ret = _Z_RES_OK; + _z_msg_pull_t *pull = (_z_msg_pull_t *)ctx; + switch (_Z_EXT_FULL_ID(extension->_header)) { + case _Z_MSG_EXT_ENC_ZBUF | 0x01: { + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + ret = _z_source_info_decode(&pull->_ext_source_info, &zbf); + break; + } + default: + if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { + ret = _z_msg_ext_unknown_error(extension, 0x0a); + } + } + return ret; +} +int8_t _z_pull_decode(_z_msg_pull_t *pull, _z_zbuf_t *zbf, uint8_t header) { + int8_t ret = _Z_RES_OK; + if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + ret = _z_msg_ext_decode_iter(zbf, _z_pull_decode_extension, pull); + } + return ret; +} + /*------------------ Zenoh Message ------------------*/ int8_t _z_zenoh_message_encode(_z_wbuf_t *wbf, const _z_zenoh_message_t *msg) { int8_t ret = _Z_RES_OK; From 9b663d5b8aa0e7aa35f0da9285622e0c8cc25649 Mon Sep 17 00:00:00 2001 From: Clay Chang Date: Fri, 23 Jun 2023 01:09:34 +0800 Subject: [PATCH 104/181] Fix a compilation error by moving local_zid declaration (#220) A compilation error (undefined local_zid symbol) caused when neither Z_UNICAST_TRANSPORT nor Z_MULTICAST_TRANSPORT is defined. This patch fixes it. Signed-off-by: Clay Chang --- src/net/session.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/net/session.c b/src/net/session.c index e13f4ca05..76f0e9656 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -26,8 +26,9 @@ int8_t __z_open_inner(_z_session_t *zn, char *locator, z_whatami_t mode) { int8_t ret = _Z_RES_OK; -#if Z_UNICAST_TRANSPORT == 1 || Z_MULTICAST_TRANSPORT == 1 _z_bytes_t local_zid = _z_bytes_empty(); + +#if Z_UNICAST_TRANSPORT == 1 || Z_MULTICAST_TRANSPORT == 1 ret = _z_session_generate_zid(&local_zid, Z_ZID_LENGTH); if (ret == _Z_RES_OK) { ret = _z_new_transport(&zn->_tp, &local_zid, locator, mode); From 3818fec16fe64152855bde4eae2b7f1cf35cd02b Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Thu, 29 Jun 2023 16:11:12 +0200 Subject: [PATCH 105/181] reorg + declarations started [skip ci] --- include/zenoh-pico/api/types.h | 6 +- include/zenoh-pico/net/publish.h | 6 +- include/zenoh-pico/net/session.h | 6 +- include/zenoh-pico/protocol/codec.h | 61 +- include/zenoh-pico/protocol/codec/core.h | 79 ++ .../zenoh-pico/protocol/codec/declarations.h | 37 + .../protocol/{extcodec.h => codec/ext.h} | 6 +- include/zenoh-pico/protocol/codec/message.h | 39 + include/zenoh-pico/protocol/codec/network.h | 25 + include/zenoh-pico/protocol/codec/transport.h | 27 + include/zenoh-pico/protocol/core.h | 11 +- .../zenoh-pico/protocol/definitions/core.h | 45 + .../protocol/definitions/declarations.h | 115 ++ .../zenoh-pico/protocol/definitions/message.h | 182 +++ .../zenoh-pico/protocol/definitions/network.h | 198 ++++ .../protocol/definitions/transport.h | 535 +++++++++ include/zenoh-pico/protocol/keyexpr.h | 2 + include/zenoh-pico/protocol/msg.h | 1030 +---------------- include/zenoh-pico/protocol/msgcodec.h | 39 +- include/zenoh-pico/session/session.h | 7 +- include/zenoh-pico/session/utils.h | 6 +- include/zenoh-pico/transport/manager.h | 6 +- include/zenoh-pico/transport/transport.h | 2 +- src/net/primitives.c | 8 +- src/protocol/codec/declarations.c | 117 ++ src/protocol/{extcodec.c => codec/ext.c} | 4 +- src/protocol/{msgcodec.c => codec/message.c} | 240 ++-- src/protocol/codec/network.c | 333 ++++++ .../{transport_codec.c => codec/transport.c} | 2 +- src/protocol/definitions/declarations.c | 64 + src/protocol/msg.c | 41 +- src/protocol/network_codec.c | 640 ---------- tests/z_msgcodec_test.c | 18 +- 33 files changed, 2009 insertions(+), 1928 deletions(-) create mode 100644 include/zenoh-pico/protocol/codec/core.h create mode 100644 include/zenoh-pico/protocol/codec/declarations.h rename include/zenoh-pico/protocol/{extcodec.h => codec/ext.h} (94%) create mode 100644 include/zenoh-pico/protocol/codec/message.h create mode 100644 include/zenoh-pico/protocol/codec/network.h create mode 100644 include/zenoh-pico/protocol/codec/transport.h create mode 100644 include/zenoh-pico/protocol/definitions/core.h create mode 100644 include/zenoh-pico/protocol/definitions/declarations.h create mode 100644 include/zenoh-pico/protocol/definitions/message.h create mode 100644 include/zenoh-pico/protocol/definitions/network.h create mode 100644 include/zenoh-pico/protocol/definitions/transport.h create mode 100644 src/protocol/codec/declarations.c rename src/protocol/{extcodec.c => codec/ext.c} (99%) rename src/protocol/{msgcodec.c => codec/message.c} (81%) create mode 100644 src/protocol/codec/network.c rename src/protocol/{transport_codec.c => codec/transport.c} (99%) create mode 100644 src/protocol/definitions/declarations.c delete mode 100644 src/protocol/network_codec.c diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index c78df42ba..4e8c48f13 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -11,8 +11,8 @@ // Contributors: // ZettaScale Zenoh Team, -#ifndef ZENOH_PICO_API_TYPES_H -#define ZENOH_PICO_API_TYPES_H +#ifndef INCLUDE_ZENOH_PICO_API_TYPES_H +#define INCLUDE_ZENOH_PICO_API_TYPES_H #include "zenoh-pico/net/publish.h" #include "zenoh-pico/net/query.h" @@ -541,4 +541,4 @@ void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id); } #endif -#endif /* ZENOH_PICO_API_TYPES_H */ +#endif /* INCLUDE_ZENOH_PICO_API_TYPES_H */ diff --git a/include/zenoh-pico/net/publish.h b/include/zenoh-pico/net/publish.h index 638f015b5..0e6ab763c 100644 --- a/include/zenoh-pico/net/publish.h +++ b/include/zenoh-pico/net/publish.h @@ -12,8 +12,8 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_PUBLISH_NETAPI_H -#define ZENOH_PICO_PUBLISH_NETAPI_H +#ifndef INCLUDE_ZENOH_PICO_NET_PUBLISH_H +#define INCLUDE_ZENOH_PICO_NET_PUBLISH_H #include "zenoh-pico/net/session.h" #include "zenoh-pico/protocol/core.h" @@ -32,4 +32,4 @@ typedef struct { void _z_publisher_clear(_z_publisher_t *pub); void _z_publisher_free(_z_publisher_t **pub); -#endif /* ZENOH_PICO_PUBLISH_NETAPI_H */ +#endif /* INCLUDE_ZENOH_PICO_NET_PUBLISH_H */ diff --git a/include/zenoh-pico/net/session.h b/include/zenoh-pico/net/session.h index c92f7a674..7804d8bbc 100644 --- a/include/zenoh-pico/net/session.h +++ b/include/zenoh-pico/net/session.h @@ -12,8 +12,8 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_SESSION_NETAPI_H -#define ZENOH_PICO_SESSION_NETAPI_H +#ifndef INCLUDE_ZENOH_PICO_NET_SESSION_H +#define INCLUDE_ZENOH_PICO_NET_SESSION_H #include "zenoh-pico/config.h" #include "zenoh-pico/session/session.h" @@ -168,4 +168,4 @@ int8_t _zp_start_lease_task(_z_session_t *z); int8_t _zp_stop_lease_task(_z_session_t *z); #endif // Z_MULTI_THREAD == 1 -#endif /* ZENOH_PICO_SESSION_NETAPI_H */ +#endif /* INCLUDE_ZENOH_PICO_NET_SESSION_H */ diff --git a/include/zenoh-pico/protocol/codec.h b/include/zenoh-pico/protocol/codec.h index 91cf4f825..d58d3a66f 100644 --- a/include/zenoh-pico/protocol/codec.h +++ b/include/zenoh-pico/protocol/codec.h @@ -12,59 +12,8 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_PROTOCOL_CODEC_H -#define ZENOH_PICO_PROTOCOL_CODEC_H - -#include - -#include "zenoh-pico/protocol/core.h" -#include "zenoh-pico/protocol/iobuf.h" -#include "zenoh-pico/utils/config.h" -#include "zenoh-pico/utils/result.h" - -#define _Z_EC(fn) \ - if ((fn) != _Z_RES_OK) { \ - return -1; \ - } - -/*------------------ Internal Zenoh-net Macros ------------------*/ -int8_t _z_encoding_prefix_encode(_z_wbuf_t *wbf, z_encoding_prefix_t en); -int8_t _z_encoding_prefix_decode(z_encoding_prefix_t *en, _z_zbuf_t *zbf); -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); -int8_t _z_query_target_encode(_z_wbuf_t *wbf, z_query_target_t en); -int8_t _z_query_target_decode(z_query_target_t *en, _z_zbuf_t *zbf); -int8_t _z_whatami_encode(_z_wbuf_t *wbf, z_whatami_t en); -int8_t _z_whatami_decode(z_whatami_t *en, _z_zbuf_t *zbf); - -int8_t _z_uint_encode(_z_wbuf_t *buf, unsigned int v); -int8_t _z_uint_decode(unsigned int *u, _z_zbuf_t *buf); - -int8_t _z_uint8_encode(_z_wbuf_t *buf, uint8_t v); -int8_t _z_uint8_decode(uint8_t *u8, _z_zbuf_t *buf); - -int8_t _z_uint16_encode(_z_wbuf_t *buf, uint16_t v); -int8_t _z_uint16_decode(uint16_t *u16, _z_zbuf_t *buf); - -int8_t _z_uint64_encode(_z_wbuf_t *buf, uint64_t v); -int8_t _z_uint64_decode(uint64_t *u64, _z_zbuf_t *buf); - -int8_t _z_zint_encode(_z_wbuf_t *buf, _z_zint_t v); -int8_t _z_zint_decode(_z_zint_t *zint, _z_zbuf_t *buf); - -int8_t _z_bytes_val_encode(_z_wbuf_t *buf, const _z_bytes_t *bs); -int8_t _z_bytes_val_decode(_z_bytes_t *bs, _z_zbuf_t *buf); -int8_t _z_bytes_val_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf); - -int8_t _z_bytes_encode(_z_wbuf_t *buf, const _z_bytes_t *bs); -int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *buf); -int8_t _z_bytes_decode_na(_z_bytes_t *bs, _z_zbuf_t *buf); - -int8_t _z_str_encode(_z_wbuf_t *buf, const char *s); -int8_t _z_str_decode(char **str, _z_zbuf_t *buf); - -int8_t _z_period_encode(_z_wbuf_t *wbf, const _z_period_t *m); -int8_t _z_period_decode(_z_period_t *p, _z_zbuf_t *zbf); -int8_t _z_period_decode_na(_z_period_t *p, _z_zbuf_t *zbf); - -#endif /* ZENOH_PICO_PROTOCOL_CODEC_H */ +#ifndef INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_H +#include "zenoh-pico/protocol/codec/core.h" +#include "zenoh-pico/protocol/codec/message.h" +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_H */ diff --git a/include/zenoh-pico/protocol/codec/core.h b/include/zenoh-pico/protocol/codec/core.h new file mode 100644 index 000000000..0db2b49c8 --- /dev/null +++ b/include/zenoh-pico/protocol/codec/core.h @@ -0,0 +1,79 @@ +// +// 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 INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_CORE_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_CORE_H + +#include + +#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/iobuf.h" +#include "zenoh-pico/utils/config.h" +#include "zenoh-pico/utils/result.h" + +#define _Z_EC(expr) \ + { \ + int8_t __res = expr; \ + if (__res != _Z_RES_OK) { \ + return __res; \ + } \ + } +#define _Z_RETURN_IF_ERR(expr) _Z_EC(expr) + +/*------------------ Internal Zenoh-net Macros ------------------*/ +int8_t _z_encoding_prefix_encode(_z_wbuf_t *wbf, z_encoding_prefix_t en); +int8_t _z_encoding_prefix_decode(z_encoding_prefix_t *en, _z_zbuf_t *zbf); +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); +int8_t _z_query_target_encode(_z_wbuf_t *wbf, z_query_target_t en); +int8_t _z_query_target_decode(z_query_target_t *en, _z_zbuf_t *zbf); +int8_t _z_whatami_encode(_z_wbuf_t *wbf, z_whatami_t en); +int8_t _z_whatami_decode(z_whatami_t *en, _z_zbuf_t *zbf); + +int8_t _z_uint_encode(_z_wbuf_t *buf, unsigned int v); +int8_t _z_uint_decode(unsigned int *u, _z_zbuf_t *buf); + +int8_t _z_uint8_encode(_z_wbuf_t *buf, uint8_t v); +int8_t _z_uint8_decode(uint8_t *u8, _z_zbuf_t *buf); + +int8_t _z_uint16_encode(_z_wbuf_t *buf, uint16_t v); +int8_t _z_uint16_decode(uint16_t *u16, _z_zbuf_t *buf); + +int8_t _z_uint64_encode(_z_wbuf_t *buf, uint64_t v); +int8_t _z_uint64_decode(uint64_t *u64, _z_zbuf_t *buf); + +int8_t _z_zint_encode(_z_wbuf_t *buf, _z_zint_t v); +int8_t _z_zint_decode(_z_zint_t *zint, _z_zbuf_t *buf); + +int8_t _z_bytes_val_encode(_z_wbuf_t *buf, const _z_bytes_t *bs); +int8_t _z_bytes_val_decode(_z_bytes_t *bs, _z_zbuf_t *buf); +int8_t _z_bytes_val_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf); + +int8_t _z_bytes_encode(_z_wbuf_t *buf, const _z_bytes_t *bs); +int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *buf); +int8_t _z_bytes_decode_na(_z_bytes_t *bs, _z_zbuf_t *buf); + +int8_t _z_str_encode(_z_wbuf_t *buf, const char *s); +int8_t _z_str_decode(char **str, _z_zbuf_t *buf); + +int8_t _z_period_encode(_z_wbuf_t *wbf, const _z_period_t *m); +int8_t _z_period_decode(_z_period_t *p, _z_zbuf_t *zbf); +int8_t _z_period_decode_na(_z_period_t *p, _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); + +int8_t _z_timestamp_encode(_z_wbuf_t *buf, const _z_timestamp_t *ts); +int8_t _z_timestamp_decode(_z_timestamp_t *ts, _z_zbuf_t *buf); +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_CORE_H */ diff --git a/include/zenoh-pico/protocol/codec/declarations.h b/include/zenoh-pico/protocol/codec/declarations.h new file mode 100644 index 000000000..3e20c8b8a --- /dev/null +++ b/include/zenoh-pico/protocol/codec/declarations.h @@ -0,0 +1,37 @@ +// +// 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 INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_DECLARATIONS_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_DECLARATIONS_H + +#include "zenoh-pico/protocol/definitions/declarations.h" +#include "zenoh-pico/protocol/iobuf.h" +#define _Z_DECL_KEXPR_MID 0 +#define _Z_DECL_KEXPR_FLAG_N 0x20 +#define _Z_UNDECL_KEXPR_MID 1 +#define _Z_DECL_SUBSCRIBER_MID 2 +#define _Z_DECL_SUBSCRIBER_FLAG_N 0x20 +#define _Z_DECL_SUBSCRIBER_FLAG_M 0x40 +#define _Z_UNDECL_SUBSCRIBER_MID 3 +#define _Z_DECL_QUERYABLE_MID 4 +#define _Z_UNDECL_QUERYABLE_MID 5 +#define _Z_DECL_TOKEN_MID 6 +#define _Z_UNDECL_TOKEN_MID 7 +#define _Z_DECL_INTEREST_MID 8 +#define _Z_FINAL_INTEREST_MID 9 +#define _Z_UNDECL_INTEREST_MID 10 +int8_t _z_declaration_encode(_z_wbuf_t *wbf, const _z_declaration_t *decl); +int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf, uint8_t header); + +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_DECLARATIONS_H */ diff --git a/include/zenoh-pico/protocol/extcodec.h b/include/zenoh-pico/protocol/codec/ext.h similarity index 94% rename from include/zenoh-pico/protocol/extcodec.h rename to include/zenoh-pico/protocol/codec/ext.h index cbeb16880..428842aa1 100644 --- a/include/zenoh-pico/protocol/extcodec.h +++ b/include/zenoh-pico/protocol/codec/ext.h @@ -12,8 +12,8 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_MSGEXTCODEC_H -#define ZENOH_PICO_MSGEXTCODEC_H +#ifndef INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_EXTCODEC_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_EXTCODEC_H #include @@ -49,7 +49,7 @@ int8_t _z_msg_ext_skip_non_mandatories(_z_zbuf_t *zbf, uint8_t trace_id); */ int8_t _z_msg_ext_unknown_error(_z_msg_ext_t *extension, uint8_t trace_id); -#endif /* ZENOH_PICO_MSGEXTCODEC_H */ +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_EXTCODEC_H */ // NOTE: the following headers are for unit testing only #ifdef ZENOH_PICO_TEST_H diff --git a/include/zenoh-pico/protocol/codec/message.h b/include/zenoh-pico/protocol/codec/message.h new file mode 100644 index 000000000..0658f67ab --- /dev/null +++ b/include/zenoh-pico/protocol/codec/message.h @@ -0,0 +1,39 @@ +// +// 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 INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_MESSAGE_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_MESSAGE_H + +#include "zenoh-pico/protocol/definitions/network.h" +#include "zenoh-pico/protocol/iobuf.h" + +int8_t _z_push_body_encode(_z_wbuf_t *buf, const _z_push_body_t *ts); +int8_t _z_push_body_decode(_z_push_body_t *ts, _z_zbuf_t *buf, uint8_t header); + +int8_t _z_reply_encode(_z_wbuf_t *wbf, const _z_msg_reply_t *reply); +int8_t _z_reply_decode(_z_msg_reply_t *reply, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_err_encode(_z_wbuf_t *wbf, const _z_msg_err_t *err); +int8_t _z_err_decode(_z_msg_err_t *err, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_ack_encode(_z_wbuf_t *wbf, const _z_msg_ack_t *ack); +int8_t _z_ack_decode(_z_msg_ack_t *ack, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_put_encode(_z_wbuf_t *wbf, const _z_msg_put_t *put); +int8_t _z_put_decode(_z_msg_put_t *put, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_del_encode(_z_wbuf_t *wbf, const _z_msg_del_t *del); +int8_t _z_del_decode(_z_msg_del_t *del, _z_zbuf_t *zbf, uint8_t header); + +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_MESSAGE_H */ diff --git a/include/zenoh-pico/protocol/codec/network.h b/include/zenoh-pico/protocol/codec/network.h new file mode 100644 index 000000000..3f6bd712d --- /dev/null +++ b/include/zenoh-pico/protocol/codec/network.h @@ -0,0 +1,25 @@ +// +// 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 INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_NETWORK_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_NETWORK_H + +#include "zenoh-pico/protocol/definitions/network.h" +#include "zenoh-pico/protocol/iobuf.h" + +int8_t _z_network_message_encode(_z_wbuf_t *buf, const _z_network_message_t *msg); +int8_t _z_network_message_decode(_z_network_message_t *msg, _z_zbuf_t *buf); +int8_t _z_network_message_decode_na(_z_network_message_t *msg, _z_zbuf_t *buf); + +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_NETWORK_H */ diff --git a/include/zenoh-pico/protocol/codec/transport.h b/include/zenoh-pico/protocol/codec/transport.h new file mode 100644 index 000000000..f7e0936ad --- /dev/null +++ b/include/zenoh-pico/protocol/codec/transport.h @@ -0,0 +1,27 @@ +// +// 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 INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_TRANSPORT_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_TRANSPORT_H + +#include "zenoh-pico/protocol/definitions/transport.h" +#include "zenoh-pico/protocol/iobuf.h" +int8_t _z_scouting_message_encode(_z_wbuf_t *buf, const _z_scouting_message_t *msg); +int8_t _z_scouting_message_decode(_z_scouting_message_t *msg, _z_zbuf_t *buf); +int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *buf); + +int8_t _z_transport_message_encode(_z_wbuf_t *buf, const _z_transport_message_t *msg); +int8_t _z_transport_message_decode(_z_transport_message_t *msg, _z_zbuf_t *buf); + +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_TRANSPORT_H */ diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index e6e781ef6..375475fab 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -78,9 +78,12 @@ _Bool _z_timestamp_check(const _z_timestamp_t *stamp); * char *val: A pointer to the string containing the resource name. */ typedef struct { - _z_zint_t _id; + uint16_t _id; + _Bool uses_remote_mapping; const char *_suffix; } _z_keyexpr_t; +static inline _Bool _z_keyexpr_has_suffix(_z_keyexpr_t ke) { return (ke._suffix != NULL) && (ke._suffix[0] != 0); } +static inline _Bool _z_keyexpr_check(_z_keyexpr_t ke) { return (ke._id != 0) || _z_keyexpr_has_suffix(ke); } /** * A zenoh-net data sample. @@ -166,4 +169,10 @@ typedef struct { z_submode_t mode; } _z_subinfo_t; +typedef struct { + _z_id_t _id; + uint32_t _entity_id; + uint32_t _source_sn; +} _z_source_info_t; + #endif /* ZENOH_PICO_PROTOCOL_CORE_H */ diff --git a/include/zenoh-pico/protocol/definitions/core.h b/include/zenoh-pico/protocol/definitions/core.h new file mode 100644 index 000000000..accbfc0d3 --- /dev/null +++ b/include/zenoh-pico/protocol/definitions/core.h @@ -0,0 +1,45 @@ +// +// 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 INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_CORE_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_CORE_H + +#define _Z_DEFAULT_BATCH_SIZE 65535 +#define _Z_DEFAULT_RESOLUTION_SIZE 2 + +#define _Z_DECLARE_CLEAR(layer, name) void _z_##layer##_msg_clear_##name(_z_##name##_t *m, uint8_t header) +#define _Z_DECLARE_CLEAR_NOH(layer, name) void _z_##layer##_msg_clear_##name(_z_##name##_t *m) + +// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length +// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. +// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve +// the boundary of the serialized messages. The length is encoded as little-endian. +// In any case, the length of a message must not exceed 65_535 bytes. +#define _Z_MSG_LEN_ENC_SIZE 2 + +/*=============================*/ +/* Message header */ +/*=============================*/ +#define _Z_MID_MASK 0x1f +#define _Z_FLAGS_MASK 0xe0 + +/*=============================*/ +/* Message helpers */ +/*=============================*/ +#define _Z_MID(h) (_Z_MID_MASK & (h)) +#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) + +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_CORE_H */ diff --git a/include/zenoh-pico/protocol/definitions/declarations.h b/include/zenoh-pico/protocol/definitions/declarations.h new file mode 100644 index 000000000..bda82f0eb --- /dev/null +++ b/include/zenoh-pico/protocol/definitions/declarations.h @@ -0,0 +1,115 @@ +// +// 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 INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_DECLARATIONS_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_DECLARATIONS_H + +#include + +#include "zenoh-pico/protocol/core.h" + +typedef struct { + uint16_t _id; + _z_keyexpr_t _keyexpr; +} _z_decl_kexpr_t; +typedef struct { + uint16_t _id; +} _z_undecl_kexpr_t; + +typedef struct { + _z_keyexpr_t _keyexpr; + uint32_t _id; + struct { + _Bool _pull_mode; + _Bool _reliable; + } _ext_subscriber_info; +} _z_decl_subscriber_t; +typedef struct { + uint32_t _id; + _z_keyexpr_t _ext_keyexpr; +} _z_undecl_subscriber_t; + +typedef struct { + _z_keyexpr_t _keyexpr; + uint32_t _id; + struct { + uint8_t _complete; + uint32_t _distance; + } _ext_queryable_info; +} _z_decl_queryable_t; +typedef struct { + uint32_t _id; + _z_keyexpr_t _ext_keyexpr; +} _z_undecl_queryable_t; + +typedef struct { + _z_keyexpr_t _keyexpr; + uint32_t _id; +} _z_decl_token_t; +typedef struct { + uint32_t _id; + _z_keyexpr_t _ext_keyexpr; +} _z_undecl_token_t; + +#define _Z_INTEREST_FLAG_KEYEXPRS (1) +#define _Z_INTEREST_FLAG_SUBSCRIBERS (1 << 1) +#define _Z_INTEREST_FLAG_QUERYABLES (1 << 2) +#define _Z_INTEREST_FLAG_TOKENS (1 << 3) +#define _Z_INTEREST_FLAG_CURRENT (1 << 5) +#define _Z_INTEREST_FLAG_FUTURE (1 << 6) +#define _Z_INTEREST_FLAG_AGGREGATE (1 << 7) +typedef struct { + _z_keyexpr_t _keyexpr; + uint32_t _id; + uint8_t interest_flags; +} _z_decl_interest_t; +typedef struct { + uint32_t _id; +} _z_final_interest_t; +typedef struct { + uint32_t _id; + _z_keyexpr_t _ext_keyexpr; +} _z_undecl_interest_t; + +typedef struct { + enum { + _Z_DECL_KEXPR, + _Z_UNDECL_KEXPR, + _Z_DECL_SUBSCRIBER, + _Z_UNDECL_SUBSCRIBER, + _Z_DECL_QUERYABLE, + _Z_UNDECL_QUERYABLE, + _Z_DECL_TOKEN, + _Z_UNDECL_TOKEN, + _Z_DECL_INTEREST, + _Z_FINAL_INTEREST, + _Z_UNDECL_INTEREST, + } _tag; + union { + _z_decl_kexpr_t _decl_kexpr; + _z_undecl_kexpr_t _undecl_kexpr; + _z_decl_subscriber_t _decl_subscriber; + _z_undecl_subscriber_t _undecl_subscriber; + _z_decl_queryable_t _decl_queryable; + _z_undecl_queryable_t _undecl_queryable; + _z_decl_token_t _decl_token; + _z_undecl_token_t _undecl_token; + _z_decl_interest_t _decl_interest; + _z_final_interest_t _final_interest; + _z_undecl_interest_t _undecl_interest; + } _body; +} _z_declaration_t; +void _z_declaration_clear(_z_declaration_t* decl); + +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_DECLARATIONS_H */ diff --git a/include/zenoh-pico/protocol/definitions/message.h b/include/zenoh-pico/protocol/definitions/message.h new file mode 100644 index 000000000..30c9c713d --- /dev/null +++ b/include/zenoh-pico/protocol/definitions/message.h @@ -0,0 +1,182 @@ +// +// 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 INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_MESSAGE_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_MESSAGE_H + +#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/definitions/core.h" +/* Zenoh Messages */ +#define _Z_MID_Z_OAM 0x00 +#define _Z_MID_Z_PUT 0x01 +#define _Z_MID_Z_DEL 0x02 +#define _Z_MID_Z_QUERY 0x03 +#define _Z_MID_Z_REPLY 0x04 +#define _Z_MID_Z_ERR 0x05 +#define _Z_MID_Z_ACK 0x06 +#define _Z_MID_Z_PULL 0x07 +#define _Z_MID_Z_LINK_STATE_LIST 0x10 + +/* Zenoh message flags */ +#define _Z_FLAG_Z_Z 0x80 +#define _Z_FLAG_Z_B 0x40 // 1 << 6 | QueryPayload if B==1 then QueryPayload is present +#define _Z_FLAG_Z_D 0x20 // 1 << 5 | Dropping if D==1 then the message can be dropped +#define _Z_FLAG_Z_F \ + 0x20 // 1 << 5 | Final if F==1 then this is the final message (e.g., ReplyContext, Pull) +#define _Z_FLAG_Z_I 0x40 // 1 << 6 | DataInfo if I==1 then DataInfo is present +#define _Z_FLAG_Z_K 0x80 // 1 << 7 | ResourceKey if K==1 then keyexpr is string +#define _Z_FLAG_Z_N 0x40 // 1 << 6 | MaxSamples if N==1 then the MaxSamples is indicated +#define _Z_FLAG_Z_P 0x80 // 1 << 7 | Period if P==1 then a period is present +#define _Z_FLAG_Z_Q 0x40 // 1 << 6 | QueryableKind if Q==1 then the queryable kind is present +#define _Z_FLAG_Z_R \ + 0x20 // 1 << 5 | Reliable if R==1 then it concerns the reliable channel, best-effort otherwise +#define _Z_FLAG_Z_S 0x40 // 1 << 6 | SubMode if S==1 then the declaration SubMode is indicated +#define _Z_FLAG_Z_T 0x20 // 1 << 5 | QueryTarget if T==1 then the query target is present +#define _Z_FLAG_Z_X 0x00 // Unused flags are set to zero + +// Flags: +// - T: Timestamp If T==1 then the timestamp if present +// - E: Encoding If E==1 then the encoding is present +// - Z: Extension If Z==1 then at least one extension is present +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|E|T| REPLY | +// +-+-+-+---------+ +// ~ ts: ~ if T==1 +// +---------------+ +// ~ encoding ~ if E==1 +// +---------------+ +// ~ [repl_exts] ~ if Z==1 +// +---------------+ +// ~ pl: ~ -- Payload +// +---------------+ +typedef struct { + _z_timestamp_t _timestamp; + _z_value_t _value; + _z_source_info_t _ext_source_info; + z_consolidation_mode_t _ext_consolidation; +} _z_msg_reply_t; +void _z_msg_reply_clear(_z_msg_reply_t *msg); +#define _Z_FLAG_Z_R_T 0x20 +#define _Z_FLAG_Z_R_E 0x40 + +// Flags: +// - T: Timestamp If T==1 then the timestamp if present +// - I: Infrastructure If I==1 then the error is related to the infrastructure else to the user +// - Z: Extension If Z==1 then at least one extension is present +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|I|T| ERR | +// +-+-+-+---------+ +// % code:z16 % +// +---------------+ +// ~ ts: ~ if T==1 +// +---------------+ +// ~ [err_exts] ~ if Z==1 +// +---------------+ +#define _Z_FLAG_Z_E_T 0x20 +#define _Z_FLAG_Z_E_I 0x40 +typedef struct { + uint16_t _code; + _Bool _is_infrastructure; + _z_timestamp_t _timestamp; + _z_source_info_t _ext_source_info; + _z_value_t _ext_value; +} _z_msg_err_t; +void _z_msg_err_clear(_z_msg_err_t *err); + +/// Flags: +/// - T: Timestamp If T==1 then the timestamp if present +/// - X: Reserved +/// - Z: Extension If Z==1 then at least one extension is present +/// +/// 7 6 5 4 3 2 1 0 +/// +-+-+-+-+-+-+-+-+ +/// |Z|X|T| ACK | +/// +-+-+-+---------+ +/// ~ ts: ~ if T==1 +/// +---------------+ +/// ~ [err_exts] ~ if Z==1 +/// +---------------+ +typedef struct { + _z_timestamp_t _timestamp; + _z_source_info_t _ext_source_info; +} _z_msg_ack_t; +#define _Z_FLAG_Z_A_T 0x20 + +/// Flags: +/// - T: Timestamp If T==1 then the timestamp if present +/// - X: Reserved +/// - Z: Extension If Z==1 then at least one extension is present +/// +/// 7 6 5 4 3 2 1 0 +/// +-+-+-+-+-+-+-+-+ +/// |Z|X|X| PULL | +/// +---------------+ +/// ~ [pull_exts] ~ if Z==1 +/// +---------------+ +typedef struct { + _z_source_info_t _ext_source_info; +} _z_msg_pull_t; + +typedef struct { + _z_timestamp_t _timestamp; + _z_source_info_t _source_info; +} _z_m_push_commons_t; + +typedef struct { + _z_m_push_commons_t _commons; +} _z_msg_del_t; +#define _Z_M_DEL_ID 0x02 +#define _Z_FLAG_Z_D_T 0x20 + +typedef struct { + _z_m_push_commons_t _commons; + _z_bytes_t _payload; + _z_encoding_t _encoding; +} _z_msg_put_t; +void _z_msg_put_clear(_z_msg_put_t *); +#define _Z_M_PUT_ID 0x01 +#define _Z_FLAG_Z_P_E 0x40 +#define _Z_FLAG_Z_P_T 0x20 + +/*------------------ Query Message ------------------*/ +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|C|P| QUERY | +// +-+-+-+---------+ +// ~ params ~ if P==1 -- +// +---------------+ +// ~ consolidation ~ if C==1 -- u8 +// +---------------+ +// ~ [qry_exts] ~ if Z==1 +// +---------------+ +#define _Z_FLAG_Z_Q_P 0x20 // 1 << 6 | Period if P==1 then a period is present +typedef struct { + _z_bytes_t _parameters; + _z_source_info_t _info; + _z_value_t _value; + z_consolidation_mode_t _consolidation; +} _z_msg_query_t; +typedef struct { + _Bool info; + _Bool body; + _Bool consolidation; +} _z_msg_query_reqexts_t; +_z_msg_query_reqexts_t _z_msg_query_required_extensions(const _z_msg_query_t *msg); +void _z_msg_clear_query(_z_msg_query_t *msg); + +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_MESSAGE_H */ diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h new file mode 100644 index 000000000..add8dc94d --- /dev/null +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -0,0 +1,198 @@ +// +// 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 INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_NETWORK_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_NETWORK_H + +#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/definitions/declarations.h" +#include "zenoh-pico/protocol/definitions/message.h" +#include "zenoh-pico/protocol/ext.h" +/* Network Messages */ +#define _Z_MID_N_OAM 0x1f +#define _Z_MID_N_DECLARE 0x1e +#define _Z_MID_N_PUSH 0x1d +#define _Z_MID_N_REQUEST 0x1c +#define _Z_MID_N_RESPONSE 0x1b +#define _Z_MID_N_RESPONSE_FINAL 0x1a + +/*=============================*/ +/* Network flags */ +/*=============================*/ +#define _Z_FLAG_N_Z 0x80 // 1 << 7 + +// PUSH message flags: +// N Named if N==1 then the key expr has name/suffix +// M Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_N_PUSH_N 0x20 // 1 << 5 +#define _Z_FLAG_N_PUSH_M 0x40 // 1 << 6 + +// REQUEST message flags: +// N Named if N==1 then the key expr has name/suffix +// M Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_N_REQUEST_N 0x20 // 1 << 5 +#define _Z_FLAG_N_REQUEST_M 0x40 // 1 << 6 + +// RESPONSE message flags: +// N Named if N==1 then the key expr has name/suffix +// M Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_N_RESPONSE_N 0x20 // 1 << 5 +#define _Z_FLAG_N_RESPONSE_M 0x40 // 1 << 6 + +typedef struct { + uint8_t _val; +} _z_n_qos_t; + +#define _z_n_qos_make(express, nodrop, priority) \ + (_z_n_qos_t) { ._val = ((express << 4) | (nodrop << 3) | priority) } +#define _Z_N_QOS_DEFAULT _z_n_qos_make(0, 0, 5) + +// RESPONSE FINAL message flags: +// Z Extensions if Z==1 then Zenoh extensions are present +// #define _Z_FLAG_N_RESPONSE_X 0x20 // 1 << 5 +// #define _Z_FLAG_N_RESPONSE_X 0x40 // 1 << 6 + +// Flags: +// - N: Named if N==1 then the keyexpr has name/suffix +// - M: Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver +// - Z: Extension if Z==1 then at least one extension is present +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|M|N| REQUEST | +// +-+-+-+---------+ +// ~ request_id:z32~ +// +---------------+ +// ~ key_scope:z16 ~ +// +---------------+ +// ~ key_suffix ~ if N==1 -- +// +---------------+ +// ~ [req_exts] ~ if Z==1 +// +---------------+ +// ~ ZenohMessage ~ +// +---------------+ +// +typedef struct { + _z_zint_t _rid; + _z_keyexpr_t _key; + // _z_request_body_t _body; +} _z_n_msg_request_t; +void _z_n_msg_clear_request(_z_n_msg_request_t *msg); + +typedef struct { + _Bool _is_put; + union { + _z_msg_del_t _del; + _z_msg_put_t _put; + } _union; +} _z_push_body_t; +void _z_push_body_clear(_z_push_body_t *msg); + +/*------------------ Response Final Message ------------------*/ +// Flags: +// - Z: Extension if Z==1 then at least one extension is present +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|M|N| ResFinal| +// +-+-+-+---------+ +// ~ request_id:z32~ +// +---------------+ +// ~ [reply_exts] ~ if Z==1 +// +---------------+ +// +typedef struct { + _z_zint_t _request_id; +} _z_n_msg_response_final_t; +void _z_n_msg_clear_response_final(_z_n_msg_response_final_t *msg); + +// Flags: +// - N: Named if N==1 then the keyexpr has name/suffix +// - M: Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver +// - Z: Extension if Z==1 then at least one extension is present +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|M|N| PUSH | +// +-+-+-+---------+ +// ~ key_scope:z? ~ +// +---------------+ +// ~ key_suffix ~ if N==1 -- +// +---------------+ +// ~ [push_exts] ~ if Z==1 +// +---------------+ +// ~ ZenohMessage ~ +// +---------------+ +// +typedef struct { + _z_keyexpr_t _key; + _z_timestamp_t _timestamp; + _z_n_qos_t _qos; + _z_push_body_t _body; +} _z_n_msg_push_t; +void _z_n_msg_clear_push(_z_n_msg_push_t *msg); + +/*------------------ Response Message ------------------*/ +typedef struct { + _z_timestamp_t _ext_timestamp; + _z_zint_t _request_id; + _z_keyexpr_t _key; + _z_n_qos_t _ext_qos; + _Bool _uses_sender_mapping; + enum { + _Z_RESPONSE_BODY_REPLY, + _Z_RESPONSE_BODY_ERR, + _Z_RESPONSE_BODY_ACK, + _Z_RESPONSE_BODY_PUT, + _Z_RESPONSE_BODY_DEL, + } _body_tag; + union { + _z_msg_reply_t _reply; + _z_msg_err_t _err; + _z_msg_ack_t _ack; + _z_msg_put_t _put; + _z_msg_del_t _del; + } _body; +} _z_n_msg_response_t; +void _z_n_msg_response_clear(_z_n_msg_response_t *msg); + +typedef struct { + _z_declaration_t _decl; + _z_timestamp_t _ext_timestamp; + uint16_t _ext_nodeid; + _z_n_qos_t _ext_qos; +} _z_n_msg_declare_t; + +/*------------------ Zenoh Message ------------------*/ +typedef union { + _z_n_msg_declare_t _declare; + _z_n_msg_push_t _push; + _z_n_msg_request_t _request; + _z_n_msg_response_t _response; + _z_n_msg_response_final_t _response_f; +} _z_network_body_t; +typedef struct { + _z_network_body_t _body; + _z_msg_ext_vec_t _extensions; + uint8_t _header; +} _z_network_message_t; +void _z_n_msg_clear(_z_network_message_t *m); +void _z_n_msg_free(_z_network_message_t **m); +_Z_ELEM_DEFINE(_z_network_message, _z_network_message_t, _z_noop_size, _z_n_msg_clear, _z_noop_copy) +_Z_VEC_DEFINE(_z_network_message, _z_network_message_t) + +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_NETWORK_H */ diff --git a/include/zenoh-pico/protocol/definitions/transport.h b/include/zenoh-pico/protocol/definitions/transport.h new file mode 100644 index 000000000..bf221968c --- /dev/null +++ b/include/zenoh-pico/protocol/definitions/transport.h @@ -0,0 +1,535 @@ +// +// 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 INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_TRANSPORT_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_TRANSPORT_H + +/* Scouting Messages */ +#include "zenoh-pico/link/endpoint.h" +#include "zenoh-pico/protocol/definitions/network.h" + +#define _Z_MID_SCOUT 0x01 +#define _Z_MID_HELLO 0x02 + +/* Transport Messages */ +#define _Z_MID_T_OAM 0x00 +#define _Z_MID_T_INIT 0x01 +#define _Z_MID_T_OPEN 0x02 +#define _Z_MID_T_CLOSE 0x03 +#define _Z_MID_T_KEEP_ALIVE 0x04 +#define _Z_MID_T_FRAME 0x05 +#define _Z_MID_T_FRAGMENT 0x06 +#define _Z_MID_T_JOIN 0x07 + +/*=============================*/ +/* Message flags */ +/*=============================*/ +#define _Z_FLAG_T_Z 0x80 // 1 << 7 + +// Scout message flags: +// I ZenohID if I==1 then the ZenohID is present +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_T_SCOUT_I 0x08 // 1 << 3 + +// Hello message flags: +// L Locators if L==1 then Locators are present +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_T_HELLO_L 0x20 // 1 << 5 + +// Join message flags: +// T Lease period if T==1 then the lease period is in seconds else in milliseconds +// S Size params if S==1 then size parameters are exchanged +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_T_JOIN_T 0x40 // 1 << 6 +#define _Z_FLAG_T_JOIN_S 0x20 // 1 << 5 + +// Init message flags: +// A Ack if A==1 then the message is an acknowledgment (aka InitAck), otherwise InitSyn +// S Size params if S==1 then size parameters are exchanged +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_T_INIT_A 0x20 // 1 << 5 +#define _Z_FLAG_T_INIT_S 0x40 // 1 << 6 + +// Open message flags: +// A Ack if A==1 then the message is an acknowledgment (aka OpenAck), otherwise OpenSyn +// T Lease period if T==1 then the lease period is in seconds else in milliseconds +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_T_OPEN_A 0x20 // 1 << 5 +#define _Z_FLAG_T_OPEN_T 0x40 // 1 << 6 + +// Frame message flags: +// R Reliable if R==1 it concerns the reliable channel, else the best-effort channel +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_T_FRAME_R 0x20 // 1 << 5 + +// Frame message flags: +// R Reliable if R==1 it concerns the reliable channel, else the best-effort channel +// M More if M==1 then other fragments will follow +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_T_FRAGMENT_R 0x20 // 1 << 5 +#define _Z_FLAG_T_FRAGMENT_M 0x40 // 1 << 6 + +// Close message flags: +// S Session Close if S==1 Session close or S==0 Link close +// Z Extensions if Z==1 then Zenoh extensions are present +#define _Z_FLAG_T_CLOSE_S 0x20 // 1 << 5 + +/*=============================*/ +/* Transport Messages */ +/*=============================*/ +/*------------------ Scout Message ------------------*/ +// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length +// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. +// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve +// the boundary of the serialized messages. The length is encoded as little-endian. +// In any case, the length of a message must not exceed 65_535 bytes. +// +// The SCOUT message can be sent at any point in time to solicit HELLO messages from matching parties. +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|X|X| SCOUT | +// +-+-+-+---------+ +// | version | +// +---------------+ +// |zid_len|I| what| (#)(*) +// +-+-+-+-+-+-+-+-+ +// ~ [u8] ~ if Flag(I)==1 -- ZenohID +// +---------------+ +// +// (#) ZID length. If Flag(I)==1 it indicates how many bytes are used for the ZenohID bytes. +// A ZenohID is minimum 1 byte and maximum 16 bytes. Therefore, the actual lenght is computed as: +// real_zid_len := 1 + zid_len +// +// (*) What. It indicates a bitmap of WhatAmI interests. +// The valid bitflags are: +// - 0b001: Router +// - 0b010: Peer +// - 0b100: Client +// +typedef struct { + _z_id_t _zid; + z_what_t _what; + uint8_t _version; +} _z_s_msg_scout_t; +void _z_s_msg_clear_scout(_z_s_msg_scout_t *msg); + +/*------------------ Hello Message ------------------*/ +// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length +// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. +// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve +// the boundary of the serialized messages. The length is encoded as little-endian. +// In any case, the length of a message must not exceed 65_535 bytes. +// +// The HELLO message is sent in any of the following three cases: +// 1) in response to a SCOUT message; +// 2) to (periodically) advertise (e.g., on multicast) the Peer and the locators it is reachable at; +// 3) in a already established session to update the corresponding peer on the new capabilities +// (i.e., whatmai) and/or new set of locators (i.e., added or deleted). +// Locators are expressed as: +// +// udp/192.168.0.2:1234 +// tcp/192.168.0.2:1234 +// udp/239.255.255.123:5555 +// +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|X|L| HELLO | +// +-+-+-+---------+ +// | version | +// +---------------+ +// |zid_len|X|X|wai| (*) +// +-+-+-+-+-+-+-+-+ +// ~ [u8] ~ -- ZenohID +// +---------------+ +// ~ ~ if Flag(L)==1 -- List of locators +// +---------------+ +// +// (*) WhatAmI. It indicates the role of the zenoh node sending the HELLO message. +// The valid WhatAmI values are: +// - 0b00: Router +// - 0b01: Peer +// - 0b10: Client +// - 0b11: Reserved +// +typedef struct { + _z_id_t _zid; + _z_locator_array_t _locators; + z_whatami_t _whatami; + uint8_t _version; +} _z_s_msg_hello_t; +void _z_s_msg_clear_hello(_z_s_msg_hello_t *msg); + +/*------------------ Join Message ------------------*/ +// # Join message +// +// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length +// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. +// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve +// the boundary of the serialized messages. The length is encoded as little-endian. +// In any case, the length of a message must not exceed 65_535 bytes. +// +// The JOIN message is sent on a multicast Locator to advertise the transport parameters. +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |O|S|T| JOIN | +// +-+-+-+-+-------+ +// ~ |Q~ if O==1 +// +---------------+ +// | v_maj | v_min | -- Protocol Version VMaj.VMin +// +-------+-------+ +// ~ whatami ~ -- Router, Peer or a combination of them +// +---------------+ +// ~ zenoh_id ~ -- PID of the sender of the JOIN message +// +---------------+ +// ~ lease ~ -- Lease period of the sender of the JOIN message(*) +// +---------------+ +// ~ seq_num_res ~ if S==1(*) -- Otherwise 2^28 is assumed(**) +// +---------------+ +// ~ [next_sn] ~ (***) +// +---------------+ +// +// - if Q==1 then the sender supports QoS. +// +// (*) if T==1 then the lease period is expressed in seconds, otherwise in milliseconds +// (**) if S==0 then 2^28 is assumed. +// (***) if Q==1 then 8 sequence numbers are present: one for each priority. +// if Q==0 then only one sequence number is present. +// +typedef struct { + _z_zint_t _reliable; + _z_zint_t _best_effort; +} _z_coundit_sn_t; +typedef struct { + union { + _z_coundit_sn_t _plain; + _z_coundit_sn_t _qos[Z_PRIORITIES_NUM]; + } _val; + _Bool _is_qos; +} _z_conduit_sn_list_t; +typedef struct { + _z_id_t _zid; + _z_zint_t _lease; + _z_conduit_sn_list_t _next_sn; + uint16_t _batch_size; + z_whatami_t _whatami; + uint8_t _req_id_res; + uint8_t _seq_num_res; + uint8_t _version; +} _z_t_msg_join_t; +void _z_t_msg_clear_join(_z_t_msg_join_t *msg); + +/*------------------ Init Message ------------------*/ +// # Init message +// +// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length +// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. +// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve +// the boundary of the serialized messages. The length is encoded as little-endian. +// In any case, the length of a message must not exceed 65_535 bytes. +// +// The INIT message is sent on a specific Locator to initiate a session with the peer associated +// with that Locator. The initiator MUST send an INIT message with the A flag set to 0. If the +// corresponding peer deems appropriate to initialize a session with the initiator, the corresponding +// peer MUST reply with an INIT message with the A flag set to 1. +// +// Flags: +// - A: Ack if A==0 then the message is an InitSyn else it is an InitAck +// - S: Size params if S==1 then size parameters are exchanged +// - Z: Extensions if Z==1 then zenoh extensions will follow. +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|S|A| INIT | +// +-+-+-+---------+ +// | version | +// +---------------+ +// |zid_len|x|x|wai| (#)(*) +// +-------+-+-+---+ +// ~ [u8] ~ -- ZenohID of the sender of the INIT message +// +---------------+ +// |x|x|kid|rid|fsn| \ -- SN/ID resolution (+) +// +---------------+ | if Flag(S)==1 +// | u16 | | -- Batch Size ($) +// | | / +// +---------------+ +// ~ ~ -- if Flag(A)==1 -- Cookie +// +---------------+ +// ~ [InitExts] ~ -- if Flag(Z)==1 +// +---------------+ +// +// If A==1 and S==0 then size parameters are (ie. S flag) are accepted. +// +// (*) WhatAmI. It indicates the role of the zenoh node sending the INIT +// message. +// The valid WhatAmI values are: +// - 0b00: Router +// - 0b01: Peer +// - 0b10: Client +// - 0b11: Reserved +// +// (#) ZID length. It indicates how many bytes are used for the ZenohID bytes. +// A ZenohID is minimum 1 byte and maximum 16 bytes. Therefore, the actual +// lenght is computed as: +// real_zid_len := 1 + zid_len +// +// (+) Sequence Number/ID resolution. It indicates the resolution and +// consequently the wire overhead +// of various SN and ID in Zenoh. +// - fsn: frame/fragment sequence number resolution. Used in Frame/Fragment +// messages. +// - rid: request ID resolution. Used in Request/Response messages. +// - kid: key expression ID resolution. Used in Push/Request/Response +// messages. The valid SN/ID resolution values are: +// - 0b00: 8 bits +// - 0b01: 16 bits +// - 0b10: 32 bits +// - 0b11: 64 bits +// +// ($) Batch Size. It indicates the maximum size of a batch the sender of the +// +typedef struct { + _z_id_t _zid; + _z_bytes_t _cookie; + uint16_t _batch_size; + z_whatami_t _whatami; + uint8_t _req_id_res; + uint8_t _seq_num_res; + uint8_t _version; +} _z_t_msg_init_t; +void _z_t_msg_clear_init(_z_t_msg_init_t *msg); + +/*------------------ Open Message ------------------*/ +// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total lenght +// in bytes of the message, resulting in the maximum lenght of a message being 65_535 bytes. +// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve +// the boundary of the serialized messages. The length is encoded as little-endian. +// In any case, the lenght of a message must not exceed 65_535 bytes. +// +// The OPEN message is sent on a link to finally open an initialized session with the peer. +// +// Flags: +// - A Ack if A==1 then the message is an acknowledgment (aka OpenAck), otherwise OpenSyn +// - T Lease period if T==1 then the lease period is in seconds else in milliseconds +// - Z Extensions if Z==1 then Zenoh extensions are present +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|T|A| OPEN | +// +-+-+-+---------+ +// % lease % -- Lease period of the sender of the OPEN message +// +---------------+ +// % initial_sn % -- Initial SN proposed by the sender of the OPEN(*) +// +---------------+ +// ~ ~ if Flag(A)==0 (**) -- Cookie +// +---------------+ +// ~ [OpenExts] ~ if Flag(Z)==1 +// +---------------+ +// +// (*) The initial sequence number MUST be compatible with the sequence number resolution agreed in the +// [`super::InitSyn`]-[`super::InitAck`] message exchange +// (**) The cookie MUST be the same received in the [`super::InitAck`]from the corresponding zenoh node +// +typedef struct { + _z_zint_t _lease; + _z_zint_t _initial_sn; + _z_bytes_t _cookie; +} _z_t_msg_open_t; +void _z_t_msg_clear_open(_z_t_msg_open_t *msg); + +/*------------------ Close Message ------------------*/ +// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length +// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. +// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve +// the boundary of the serialized messages. The length is encoded as little-endian. +// In any case, the length of a message must not exceed 65_535 bytes. +// +// The CLOSE message is sent in any of the following two cases: +// 1) in response to an OPEN message which is not accepted; +// 2) at any time to arbitrarly close the session with the corresponding peer. +// +// Flags: +// - S: Session Close if S==1 Session close or S==0 Link close +// - X: Reserved +// - Z: Extensions if Z==1 then zenoh extensions will follow. +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|X|S| CLOSE | +// +-+-+-+---------+ +// | Reason | +// +---------------+ +// ~ [CloseExts] ~ if Flag(Z)==1 +// +---------------+ +// +typedef struct { + uint8_t _reason; +} _z_t_msg_close_t; +void _z_t_msg_clear_close(_z_t_msg_close_t *msg); +/*=============================*/ +/* Close reasons */ +/*=============================*/ +#define _Z_CLOSE_GENERIC 0x00 +#define _Z_CLOSE_UNSUPPORTED 0x01 +#define _Z_CLOSE_INVALID 0x02 +#define _Z_CLOSE_MAX_TRANSPORTS 0x03 +#define _Z_CLOSE_MAX_LINKS 0x04 +#define _Z_CLOSE_EXPIRED 0x05 + +/*------------------ Keep Alive Message ------------------*/ +// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length +// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. +// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve +// the boundary of the serialized messages. The length is encoded as little-endian. +// In any case, the length of a message must not exceed 65_535 bytes. +// +// The KEEP_ALIVE message can be sent periodically to avoid the expiration of the session lease +// period in case there are no messages to be sent. +// +// Flags: +// - X: Reserved +// - X: Reserved +// - Z: Extensions If Z==1 then Zenoh extensions will follow. +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|X|X| KALIVE | +// +-+-+-+---------+ +// ~ [KAliveExts] ~ if Flag(Z)==1 +// +---------------+ +// +typedef struct { + uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior +} _z_t_msg_keep_alive_t; +void _z_t_msg_clear_keep_alive(_z_t_msg_keep_alive_t *msg); + +/*------------------ Frame Message ------------------*/ +// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length +// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. +// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve +// the boundary of the serialized messages. The length is encoded as little-endian. +// In any case, the length of a message must not exceed 65_535 bytes. +// +// Flags: +// - R: Reliable If R==1 it concerns the reliable channel, else the best-effort channel +// - X: Reserved +// - Z: Extensions If Z==1 then zenoh extensions will follow. +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|X|R| FRAME | +// +-+-+-+---------+ +// % seq num % +// +---------------+ +// ~ [FrameExts] ~ if Flag(Z)==1 +// +---------------+ +// ~ [NetworkMsg] ~ +// +---------------+ +// +// - if R==1 then the FRAME is sent on the reliable channel, best-effort otherwise. +// +typedef struct { + _z_network_message_vec_t _messages; + _z_zint_t _sn; +} _z_t_msg_frame_t; +void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg); + +/*------------------ Fragment Message ------------------*/ +// The Fragment message is used to transmit on the wire large Zenoh Message that require fragmentation +// because they are larger thatn the maximum batch size (i.e. 2^16-1) and/or the link MTU. +// +// The [`Fragment`] message flow is the following: +// +// Flags: +// - R: Reliable if R==1 it concerns the reliable channel, else the best-effort channel +// - M: More if M==1 then other fragments will follow +// - Z: Extensions if Z==1 then zenoh extensions will follow. +// +// 7 6 5 4 3 2 1 0 +// +-+-+-+-+-+-+-+-+ +// |Z|M|R| FRAGMENT| +// +-+-+-+---------+ +// % seq num % +// +---------------+ +// ~ [FragExts] ~ if Flag(Z)==1 +// +---------------+ +// ~ [u8] ~ +// +---------------+ +// +typedef struct { + _z_bytes_t _payload; + _z_zint_t _sn; +} _z_t_msg_fragment_t; +void _z_t_msg_clear_fragment(_z_t_msg_fragment_t *msg); + +/*------------------ Transport Message ------------------*/ +typedef union { + _z_t_msg_join_t _join; + _z_t_msg_init_t _init; + _z_t_msg_open_t _open; + _z_t_msg_close_t _close; + _z_t_msg_keep_alive_t _keep_alive; + _z_t_msg_frame_t _frame; + _z_t_msg_fragment_t _fragment; +} _z_transport_body_t; + +typedef struct { + _z_transport_body_t _body; + uint8_t _header; +} _z_transport_message_t; +void _z_t_msg_clear(_z_transport_message_t *msg); + +/*------------------ Builders ------------------*/ +_z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_id_t zid, + _z_conduit_sn_list_t next_sn); +_z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_id_t zid); +_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, _z_bytes_t cookie); +_z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie); +_z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial_sn); +_z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only); +_z_transport_message_t _z_t_msg_make_keep_alive(void); +_z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_network_message_vec_t messages, _Bool is_reliable); +_z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable); +_z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_bytes_t messages, _Bool is_reliable, _Bool is_last); + +/*------------------ Copy ------------------*/ +void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg); +void _z_t_msg_copy_join(_z_t_msg_join_t *clone, _z_t_msg_join_t *msg); +void _z_t_msg_copy_init(_z_t_msg_init_t *clone, _z_t_msg_init_t *msg); +void _z_t_msg_copy_open(_z_t_msg_open_t *clone, _z_t_msg_open_t *msg); +void _z_t_msg_copy_close(_z_t_msg_close_t *clone, _z_t_msg_close_t *msg); +void _z_t_msg_copy_keep_alive(_z_t_msg_keep_alive_t *clone, _z_t_msg_keep_alive_t *msg); +void _z_t_msg_copy_frame(_z_t_msg_frame_t *clone, _z_t_msg_frame_t *msg); + +typedef union { + _z_s_msg_scout_t _scout; + _z_s_msg_hello_t _hello; +} _z_scouting_body_t; + +typedef struct { + _z_scouting_body_t _body; + uint8_t _header; +} _z_scouting_message_t; +void _z_s_msg_clear(_z_scouting_message_t *msg); + +_z_scouting_message_t _z_s_msg_make_scout(z_what_t what, _z_id_t zid); +_z_scouting_message_t _z_s_msg_make_hello(z_whatami_t whatami, _z_id_t zid, _z_locator_array_t locators); + +void _z_s_msg_copy(_z_scouting_message_t *clone, _z_scouting_message_t *msg); +void _z_s_msg_copy_scout(_z_s_msg_scout_t *clone, _z_s_msg_scout_t *msg); +void _z_s_msg_copy_hello(_z_s_msg_hello_t *clone, _z_s_msg_hello_t *msg); + +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_TRANSPORT_H */ diff --git a/include/zenoh-pico/protocol/keyexpr.h b/include/zenoh-pico/protocol/keyexpr.h index 1093851dd..288056711 100644 --- a/include/zenoh-pico/protocol/keyexpr.h +++ b/include/zenoh-pico/protocol/keyexpr.h @@ -29,5 +29,7 @@ void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src); _z_keyexpr_t _z_keyexpr_duplicate(const _z_keyexpr_t *src); _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp); void _z_timestamp_reset(_z_timestamp_t *tstamp); +void _z_keyexpr_clear(_z_keyexpr_t *rk); +void _z_keyexpr_free(_z_keyexpr_t **rk); #endif /* ZENOH_PICO_PROTOCOL_KEYEXPR_H */ diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index 68263e902..a09b94e2b 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -12,8 +12,8 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_PROTOCOL_MSG_H -#define ZENOH_PICO_PROTOCOL_MSG_H +#ifndef INCLUDE_ZENOH_PICO_PROTOCOL_MSG_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_MSG_H #include #include @@ -27,212 +27,9 @@ #include "zenoh-pico/link/endpoint.h" #include "zenoh-pico/net/query.h" #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/definitions/transport.h" #include "zenoh-pico/protocol/ext.h" -#define _Z_DEFAULT_BATCH_SIZE 65535 -#define _Z_DEFAULT_RESOLUTION_SIZE 2 - -#define _Z_DECLARE_CLEAR(layer, name) void _z_##layer##_msg_clear_##name(_z_##name##_t *m, uint8_t header) -#define _Z_DECLARE_CLEAR_NOH(layer, name) void _z_##layer##_msg_clear_##name(_z_##name##_t *m) - -// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length -// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. -// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve -// the boundary of the serialized messages. The length is encoded as little-endian. -// In any case, the length of a message must not exceed 65_535 bytes. -#define _Z_MSG_LEN_ENC_SIZE 2 - -/*=============================*/ -/* Message IDs */ -/*=============================*/ -/* Scouting Messages */ -#define _Z_MID_SCOUT 0x01 -#define _Z_MID_HELLO 0x02 - -/* Transport Messages */ -#define _Z_MID_T_OAM 0x00 -#define _Z_MID_T_INIT 0x01 -#define _Z_MID_T_OPEN 0x02 -#define _Z_MID_T_CLOSE 0x03 -#define _Z_MID_T_KEEP_ALIVE 0x04 -#define _Z_MID_T_FRAME 0x05 -#define _Z_MID_T_FRAGMENT 0x06 -#define _Z_MID_T_JOIN 0x07 - -/* Network Messages */ -#define _Z_MID_N_OAM 0x1f -#define _Z_MID_N_DECLARE 0x1e -#define _Z_MID_N_PUSH 0x1d -#define _Z_MID_N_REQUEST 0x1c -#define _Z_MID_N_RESPONSE 0x1b -#define _Z_MID_N_RESPONSE_FINAL 0x1a - -/* Zenoh Messages */ -#define _Z_MID_Z_DATA 0x0c -#define _Z_MID_Z_QUERY 0x0d -#define _Z_MID_Z_PULL 0x0e -#define _Z_MID_Z_UNIT 0x0f -#define _Z_MID_Z_LINK_STATE_LIST 0x10 - -/*=============================*/ -/* Message flags */ -/*=============================*/ -#define _Z_FLAG_T_Z 0x80 // 1 << 7 - -// Scout message flags: -// I ZenohID if I==1 then the ZenohID is present -// Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_T_SCOUT_I 0x08 // 1 << 3 - -// Hello message flags: -// L Locators if L==1 then Locators are present -// Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_T_HELLO_L 0x20 // 1 << 5 - -// Join message flags: -// T Lease period if T==1 then the lease period is in seconds else in milliseconds -// S Size params if S==1 then size parameters are exchanged -// Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_T_JOIN_T 0x40 // 1 << 6 -#define _Z_FLAG_T_JOIN_S 0x20 // 1 << 5 - -// Init message flags: -// A Ack if A==1 then the message is an acknowledgment (aka InitAck), otherwise InitSyn -// S Size params if S==1 then size parameters are exchanged -// Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_T_INIT_A 0x20 // 1 << 5 -#define _Z_FLAG_T_INIT_S 0x40 // 1 << 6 - -// Open message flags: -// A Ack if A==1 then the message is an acknowledgment (aka OpenAck), otherwise OpenSyn -// T Lease period if T==1 then the lease period is in seconds else in milliseconds -// Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_T_OPEN_A 0x20 // 1 << 5 -#define _Z_FLAG_T_OPEN_T 0x40 // 1 << 6 - -// Frame message flags: -// R Reliable if R==1 it concerns the reliable channel, else the best-effort channel -// Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_T_FRAME_R 0x20 // 1 << 5 - -// Frame message flags: -// R Reliable if R==1 it concerns the reliable channel, else the best-effort channel -// M More if M==1 then other fragments will follow -// Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_T_FRAGMENT_R 0x20 // 1 << 5 -#define _Z_FLAG_T_FRAGMENT_M 0x40 // 1 << 6 - -// Close message flags: -// S Session Close if S==1 Session close or S==0 Link close -// Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_T_CLOSE_S 0x20 // 1 << 5 - -/*=============================*/ -/* Network flags */ -/*=============================*/ -#define _Z_FLAG_N_Z 0x80 // 1 << 7 - -// PUSH message flags: -// N Named if N==1 then the key expr has name/suffix -// M Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver -// Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_N_PUSH_N 0x20 // 1 << 5 -#define _Z_FLAG_N_PUSH_M 0x40 // 1 << 6 - -// REQUEST message flags: -// N Named if N==1 then the key expr has name/suffix -// M Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver -// Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_N_REQUEST_N 0x20 // 1 << 5 -#define _Z_FLAG_N_REQUEST_M 0x40 // 1 << 6 - -// RESPONSE message flags: -// N Named if N==1 then the key expr has name/suffix -// M Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver -// Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_N_RESPONSE_N 0x20 // 1 << 5 -#define _Z_FLAG_N_RESPONSE_M 0x40 // 1 << 6 - -// RESPONSE FINAL message flags: -// Z Extensions if Z==1 then Zenoh extensions are present -// #define _Z_FLAG_N_RESPONSE_X 0x20 // 1 << 5 -// #define _Z_FLAG_N_RESPONSE_X 0x40 // 1 << 6 - -/* Zenoh message flags */ -#define _Z_FLAG_Z_Z 0x80 -#define _Z_FLAG_Z_B 0x40 // 1 << 6 | QueryPayload if B==1 then QueryPayload is present -#define _Z_FLAG_Z_D 0x20 // 1 << 5 | Dropping if D==1 then the message can be dropped -#define _Z_FLAG_Z_F \ - 0x20 // 1 << 5 | Final if F==1 then this is the final message (e.g., ReplyContext, Pull) -#define _Z_FLAG_Z_I 0x40 // 1 << 6 | DataInfo if I==1 then DataInfo is present -#define _Z_FLAG_Z_K 0x80 // 1 << 7 | ResourceKey if K==1 then keyexpr is string -#define _Z_FLAG_Z_N 0x40 // 1 << 6 | MaxSamples if N==1 then the MaxSamples is indicated -#define _Z_FLAG_Z_P 0x80 // 1 << 7 | Period if P==1 then a period is present -#define _Z_FLAG_Z_Q 0x40 // 1 << 6 | QueryableKind if Q==1 then the queryable kind is present -#define _Z_FLAG_Z_R \ - 0x20 // 1 << 5 | Reliable if R==1 then it concerns the reliable channel, best-effort otherwise -#define _Z_FLAG_Z_S 0x40 // 1 << 6 | SubMode if S==1 then the declaration SubMode is indicated -#define _Z_FLAG_Z_T 0x20 // 1 << 5 | QueryTarget if T==1 then the query target is present -#define _Z_FLAG_Z_X 0x00 // Unused flags are set to zero - -/*=============================*/ -/* Message header */ -/*=============================*/ -#define _Z_MID_MASK 0x1f -#define _Z_FLAGS_MASK 0xe0 - -/*=============================*/ -/* Message helpers */ -/*=============================*/ -#define _Z_MID(h) (_Z_MID_MASK & (h)) -#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) - -/*=============================*/ -/* Declaration IDs */ -/*=============================*/ -#define _Z_DECL_RESOURCE 0x01 -#define _Z_DECL_PUBLISHER 0x02 -#define _Z_DECL_SUBSCRIBER 0x03 -#define _Z_DECL_QUERYABLE 0x04 -#define _Z_DECL_FORGET_RESOURCE 0x11 -#define _Z_DECL_FORGET_PUBLISHER 0x12 -#define _Z_DECL_FORGET_SUBSCRIBER 0x13 -#define _Z_DECL_FORGET_QUERYABLE 0x14 - -/*=============================*/ -/* Close reasons */ -/*=============================*/ -#define _Z_CLOSE_GENERIC 0x00 -#define _Z_CLOSE_UNSUPPORTED 0x01 -#define _Z_CLOSE_INVALID 0x02 -#define _Z_CLOSE_MAX_TRANSPORTS 0x03 -#define _Z_CLOSE_MAX_LINKS 0x04 -#define _Z_CLOSE_EXPIRED 0x05 - -/*=============================*/ -/* DataInfo flags */ -/*=============================*/ -#define _Z_DATA_INFO_SLICED 0x01 // 1 << 0 -#define _Z_DATA_INFO_KIND 0x02 // 1 << 1 -#define _Z_DATA_INFO_ENC 0x04 // 1 << 2 -#define _Z_DATA_INFO_TSTAMP 0x08 // 1 << 3 -// Reserved: bits 4-6 -#define _Z_DATA_INFO_SRC_ID 0x80 // 1 << 7 -#define _Z_DATA_INFO_SRC_SN 0x100 // 1 << 8 - -/*------------------ Payload field ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// ~ Length ~ -// +---------------+ -// ~ Buffer ~ -// +---------------+ -// -typedef _z_bytes_t _z_payload_t; -void _z_payload_clear(_z_payload_t *p); - /*------------------ Locators Field ------------------*/ // 7 6 5 4 3 2 1 0 // +-+-+-+-+-+-+-+-+ @@ -270,8 +67,6 @@ void _z_payload_clear(_z_payload_t *p); // ~ Suffix ~ if K==1 // +---------------+ // -void _z_keyexpr_clear(_z_keyexpr_t *rk); -void _z_keyexpr_free(_z_keyexpr_t **rk); /*------------------ Resource Declaration ------------------*/ // 7 6 5 4 3 2 1 0 @@ -399,21 +194,6 @@ typedef struct { } _z_forget_qle_decl_t; void _z_declaration_clear_forget_queryable(_z_forget_qle_decl_t *dcl); -typedef struct { - union { - _z_res_decl_t _res; - _z_forget_res_decl_t _forget_res; - _z_pub_decl_t _pub; - _z_forget_pub_decl_t _forget_pub; - _z_sub_decl_t _sub; - _z_forget_sub_decl_t _forget_sub; - _z_qle_decl_t _qle; - _z_forget_qle_decl_t _forget_qle; - } _body; - uint8_t _header; -} _z_declaration_t; -void _z_declaration_clear(_z_declaration_t *dcl); - /*------------------ Timestamp Field ------------------*/ // 7 6 5 4 3 2 1 0 // +-+-+-+---------+ @@ -424,59 +204,6 @@ void _z_declaration_clear(_z_declaration_t *dcl); // void _z_timestamp_clear(_z_timestamp_t *ts); -/*------------------ Data Info Field ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// ~ options ~ -// +---------------+ -// ~ kind ~ if options & (1 << 1) -// +---------------+ -// ~ encoding ~ if options & (1 << 2) -// +---------------+ -// ~ timestamp ~ if options & (1 << 3) -// +---------------+ -// ~ source_id ~ if options & (1 << 7) -// +---------------+ -// ~ source_sn ~ if options & (1 << 8) -// +---------------+ -// -// - if options & (1 << 0) then the payload is sliced -typedef struct { - _z_bytes_t _source_id; - _z_timestamp_t _tstamp; - _z_zint_t _flags; - _z_zint_t _source_sn; - _z_encoding_t _encoding; - uint8_t _kind; -} _z_data_info_t; -void _z_data_info_clear(_z_data_info_t *di); -typedef struct { - _z_id_t _id; - uint32_t _entity_id; - uint32_t _source_sn; -} _z_source_info_t; - -/*------------------ Data Message ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |K|I|D| DATA | -// +-+-+-+---------+ -// ~ ResKey ~ if K==1 then keyexpr is string -// +---------------+ -// ~ DataInfo ~ if I==1 -// +---------------+ -// ~ Payload ~ -// +---------------+ -// -// - if D==1 then the message can be dropped for congestion control reasons. -// -typedef struct { - _z_data_info_t _info; - _z_keyexpr_t _key; - _z_payload_t _payload; -} _z_msg_data_t; -void _z_msg_clear_data(_z_msg_data_t *msg); - /*------------------ Unit Message ------------------*/ // 7 6 5 4 3 2 1 0 // +-+-+-+-+-+-+-+-+ @@ -492,35 +219,8 @@ void _z_msg_clear_unit(_z_msg_unit_t *unt); /*------------------ Pull Message ------------------*/ -/*------------------ Query Message ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|C|P| QUERY | -// +-+-+-+---------+ -// ~ params ~ if P==1 -- -// +---------------+ -// ~ consolidation ~ if C==1 -- u8 -// +---------------+ -// ~ [qry_exts] ~ if Z==1 -// +---------------+ -#define _Z_FLAG_Z_Q_P 0x20 // 1 << 6 | Period if P==1 then a period is present -typedef struct { - _z_bytes_t _parameters; - _z_source_info_t _info; - _z_value_t _value; - z_consolidation_mode_t _consolidation; -} _z_msg_query_t; -typedef struct { - _Bool info; - _Bool body; - _Bool consolidation; -} _z_msg_query_reqexts_t; -_z_msg_query_reqexts_t _z_msg_query_required_extensions(const _z_msg_query_t *msg); -void _z_msg_clear_query(_z_msg_query_t *msg); - /*------------------ Zenoh Message ------------------*/ typedef union { - _z_msg_data_t _data; _z_msg_query_t _query; _z_msg_unit_t _unit; } _z_zenoh_body_t; @@ -534,9 +234,26 @@ typedef struct { _z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, _Bool is_final); // TODO[remove end] +/*=============================*/ +/* Network Messages */ +/*=============================*/ + +/*------------------ Push Message ------------------*/ + +/*------------------ Builders ------------------*/ +_z_network_message_t _z_n_msg_make_declare(_z_declaration_t declarations); +_z_network_message_t _z_n_msg_make_push(_z_keyexpr_t key, _z_push_body_t body, _Bool is_remote_mapping); +// _z_network_message_t _z_n_msg_make_request(_z_zint_t rid, _z_keyexpr_t key, _z_request_body_t body, +// _Bool is_remote_mapping); +// _z_network_message_t _z_n_msg_make_response(_z_zint_t rid, _z_keyexpr_t key, _z_response_body_t body, +// _Bool is_remote_mapping); +_z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid); + typedef struct { - uint8_t _header; - _z_zenoh_body_t _body; + union { + _z_n_msg_response_t _response; + _z_n_msg_response_final_t _final; + } _body; } _z_zenoh_message_t; void _z_msg_clear(_z_zenoh_message_t *m); void _z_msg_free(_z_zenoh_message_t **m); @@ -552,707 +269,12 @@ _z_declaration_t _z_msg_make_declaration_subscriber(_z_keyexpr_t key, _z_subinfo _z_declaration_t _z_msg_make_declaration_forget_subscriber(_z_keyexpr_t key); _z_declaration_t _z_msg_make_declaration_queryable(_z_keyexpr_t key, _z_zint_t complete, _z_zint_t distance); _z_declaration_t _z_msg_make_declaration_forget_queryable(_z_keyexpr_t key); -_z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_payload_t payload, _Bool can_be_dropped); +// _z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool can_be_dropped); _z_zenoh_message_t _z_msg_make_unit(_Bool can_be_dropped); _z_zenoh_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id, _z_zint_t max_samples, _Bool is_final); _z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, _z_bytes_t parameters, _z_zint_t qid, z_consolidation_mode_t consolidation, _z_value_t value); -_z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_payload_t payload, _Bool can_be_dropped); - -/*=============================*/ -/* Network Messages */ -/*=============================*/ -/*------------------ Declaration Message ------------------*/ -// Flags: -// - X: Reserved -// - X: Reserved -// - Z: Extension if Z==1 then at least one extension is present -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|X|X| DECLARE | -// +-+-+-+---------+ -// ~ [decl_exts] ~ -- if Flag(Z)==1 -// +---------------+ -// ~ declaration ~ -// +---------------+ -// -typedef struct { - _z_declaration_t _declaration; -} _z_n_msg_declare_t; -void _z_n_msg_clear_declare(_z_n_msg_declare_t *dcl); - -/*------------------ Push Message ------------------*/ - -typedef struct { - _z_timestamp_t _timestamp; - _z_source_info_t _source_info; -} _z_m_push_commons_t; - -typedef struct { - _z_m_push_commons_t _commons; -} _z_m_del_t; -#define _Z_M_DEL_ID 0x02 -#define _Z_FLAG_Z_D_T 0x20 - -typedef struct { - _z_m_push_commons_t _commons; - _z_bytes_t _payload; - _z_encoding_t _encoding; -} _z_m_put_t; -#define _Z_M_PUT_ID 0x01 -#define _Z_FLAG_Z_P_E 0x40 -#define _Z_FLAG_Z_P_T 0x20 - -typedef struct { - _z_m_push_commons_t _commons; - _Bool _is_put; - union { - _z_m_del_t _del; - _z_m_put_t _put; - } _union; -} _z_push_body_t; -void _z_push_body_clear(_z_push_body_t *msg); - -typedef struct { - uint8_t _val; -} _z_n_qos_t; - -#define _z_n_qos_make(express, nodrop, priority) \ - (_z_n_qos_t) { ._val = ((express << 4) | (nodrop << 3) | priority) } -#define _Z_N_QOS_DEFAULT _z_n_qos_make(0, 0, 5) - -// Flags: -// - N: Named if N==1 then the keyexpr has name/suffix -// - M: Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver -// - Z: Extension if Z==1 then at least one extension is present -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|M|N| PUSH | -// +-+-+-+---------+ -// ~ key_scope:z? ~ -// +---------------+ -// ~ key_suffix ~ if N==1 -- -// +---------------+ -// ~ [push_exts] ~ if Z==1 -// +---------------+ -// ~ ZenohMessage ~ -// +---------------+ -// -typedef struct { - _z_keyexpr_t _key; - _z_timestamp_t _timestamp; - _z_n_qos_t _qos; - _z_push_body_t _body; -} _z_n_msg_push_t; -void _z_n_msg_clear_push(_z_n_msg_push_t *msg); - -/*------------------ Request Message ------------------*/ -typedef union { -} _z_request_body_t; -void _z_request_body_clear(_z_request_body_t *msg); - -// Flags: -// - N: Named if N==1 then the keyexpr has name/suffix -// - M: Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver -// - Z: Extension if Z==1 then at least one extension is present -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|M|N| REQUEST | -// +-+-+-+---------+ -// ~ request_id:z32~ -// +---------------+ -// ~ key_scope:z16 ~ -// +---------------+ -// ~ key_suffix ~ if N==1 -- -// +---------------+ -// ~ [req_exts] ~ if Z==1 -// +---------------+ -// ~ ZenohMessage ~ -// +---------------+ -// -typedef struct { - _z_zint_t _rid; - _z_keyexpr_t _key; - _z_request_body_t _body; -} _z_n_msg_request_t; -void _z_n_msg_clear_request(_z_n_msg_request_t *msg); - -/*------------------ Response Message ------------------*/ -typedef union { -} _z_response_body_t; -void _z_response_body_clear(_z_response_body_t *msg); - -// Flags: -// - T: Timestamp If T==1 then the timestamp if present -// - E: Encoding If E==1 then the encoding is present -// - Z: Extension If Z==1 then at least one extension is present -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|E|T| REPLY | -// +-+-+-+---------+ -// ~ ts: ~ if T==1 -// +---------------+ -// ~ encoding ~ if E==1 -// +---------------+ -// ~ [repl_exts] ~ if Z==1 -// +---------------+ -// ~ pl: ~ -- Payload -// +---------------+ -typedef struct { - _z_timestamp_t _timestamp; - _z_value_t _value; - _z_source_info_t _source_info; - z_consolidation_mode_t _consolidation; -} _z_msg_reply_t; -void _z_msg_reply_clear(_z_msg_reply_t *msg); -#define _Z_FLAG_Z_R_T 0x20 -#define _Z_FLAG_Z_R_E 0x40 - -// Flags: -// - T: Timestamp If T==1 then the timestamp if present -// - I: Infrastructure If I==1 then the error is related to the infrastructure else to the user -// - Z: Extension If Z==1 then at least one extension is present -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|I|T| ERR | -// +-+-+-+---------+ -// % code:z16 % -// +---------------+ -// ~ ts: ~ if T==1 -// +---------------+ -// ~ [err_exts] ~ if Z==1 -// +---------------+ -#define _Z_FLAG_Z_E_T 0x20 -#define _Z_FLAG_Z_E_I 0x40 -typedef struct { - uint16_t _code; - _Bool _is_infrastructure; - _z_timestamp_t _timestamp; - _z_source_info_t _ext_source_info; - _z_value_t _ext_value; -} _z_msg_err_t; -void _z_msg_err_clear(_z_msg_err_t *err); - -/// Flags: -/// - T: Timestamp If T==1 then the timestamp if present -/// - X: Reserved -/// - Z: Extension If Z==1 then at least one extension is present -/// -/// 7 6 5 4 3 2 1 0 -/// +-+-+-+-+-+-+-+-+ -/// |Z|X|T| ACK | -/// +-+-+-+---------+ -/// ~ ts: ~ if T==1 -/// +---------------+ -/// ~ [err_exts] ~ if Z==1 -/// +---------------+ -typedef struct { - _z_timestamp_t _timestamp; - _z_source_info_t _ext_source_info; -} _z_msg_ack_t; -#define _Z_FLAG_Z_A_T 0x20 - -/// Flags: -/// - T: Timestamp If T==1 then the timestamp if present -/// - X: Reserved -/// - Z: Extension If Z==1 then at least one extension is present -/// -/// 7 6 5 4 3 2 1 0 -/// +-+-+-+-+-+-+-+-+ -/// |Z|X|X| PULL | -/// +---------------+ -/// ~ [pull_exts] ~ if Z==1 -/// +---------------+ -typedef struct { - _z_source_info_t _ext_source_info; -} _z_msg_pull_t; - -/*------------------ Response Final Message ------------------*/ -// Flags: -// - Z: Extension if Z==1 then at least one extension is present -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|M|N| ResFinal| -// +-+-+-+---------+ -// ~ request_id:z32~ -// +---------------+ -// ~ [reply_exts] ~ if Z==1 -// +---------------+ -// -typedef struct { - _z_zint_t _rid; -} _z_n_msg_response_final_t; -void _z_n_msg_clear_response_final(_z_n_msg_response_final_t *msg); - -/*------------------ Zenoh Message ------------------*/ -typedef union { - _z_n_msg_declare_t _declare; - _z_n_msg_push_t _push; - _z_n_msg_request_t _request; - _z_msg_reply_t _response; - _z_n_msg_response_final_t _response_f; -} _z_network_body_t; -typedef struct { - _z_network_body_t _body; - _z_msg_ext_vec_t _extensions; - uint8_t _header; -} _z_network_message_t; -void _z_n_msg_clear(_z_network_message_t *m); -void _z_n_msg_free(_z_network_message_t **m); -_Z_ELEM_DEFINE(_z_network_message, _z_network_message_t, _z_noop_size, _z_n_msg_clear, _z_noop_copy) -_Z_VEC_DEFINE(_z_network_message, _z_network_message_t) - -/*------------------ Builders ------------------*/ -_z_network_message_t _z_n_msg_make_declare(_z_declaration_t declarations); -_z_network_message_t _z_n_msg_make_push(_z_keyexpr_t key, _z_push_body_t body, _Bool is_remote_mapping); -_z_network_message_t _z_n_msg_make_request(_z_zint_t rid, _z_keyexpr_t key, _z_request_body_t body, - _Bool is_remote_mapping); -_z_network_message_t _z_n_msg_make_response(_z_zint_t rid, _z_keyexpr_t key, _z_response_body_t body, - _Bool is_remote_mapping); -_z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid); - -/*=============================*/ -/* Transport Messages */ -/*=============================*/ -/*------------------ Scout Message ------------------*/ -// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length -// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. -// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve -// the boundary of the serialized messages. The length is encoded as little-endian. -// In any case, the length of a message must not exceed 65_535 bytes. -// -// The SCOUT message can be sent at any point in time to solicit HELLO messages from matching parties. -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|X|X| SCOUT | -// +-+-+-+---------+ -// | version | -// +---------------+ -// |zid_len|I| what| (#)(*) -// +-+-+-+-+-+-+-+-+ -// ~ [u8] ~ if Flag(I)==1 -- ZenohID -// +---------------+ -// -// (#) ZID length. If Flag(I)==1 it indicates how many bytes are used for the ZenohID bytes. -// A ZenohID is minimum 1 byte and maximum 16 bytes. Therefore, the actual lenght is computed as: -// real_zid_len := 1 + zid_len -// -// (*) What. It indicates a bitmap of WhatAmI interests. -// The valid bitflags are: -// - 0b001: Router -// - 0b010: Peer -// - 0b100: Client -// -typedef struct { - _z_id_t _zid; - z_what_t _what; - uint8_t _version; -} _z_s_msg_scout_t; -void _z_s_msg_clear_scout(_z_s_msg_scout_t *msg); - -/*------------------ Hello Message ------------------*/ -// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length -// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. -// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve -// the boundary of the serialized messages. The length is encoded as little-endian. -// In any case, the length of a message must not exceed 65_535 bytes. -// -// The HELLO message is sent in any of the following three cases: -// 1) in response to a SCOUT message; -// 2) to (periodically) advertise (e.g., on multicast) the Peer and the locators it is reachable at; -// 3) in a already established session to update the corresponding peer on the new capabilities -// (i.e., whatmai) and/or new set of locators (i.e., added or deleted). -// Locators are expressed as: -// -// udp/192.168.0.2:1234 -// tcp/192.168.0.2:1234 -// udp/239.255.255.123:5555 -// -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|X|L| HELLO | -// +-+-+-+---------+ -// | version | -// +---------------+ -// |zid_len|X|X|wai| (*) -// +-+-+-+-+-+-+-+-+ -// ~ [u8] ~ -- ZenohID -// +---------------+ -// ~ ~ if Flag(L)==1 -- List of locators -// +---------------+ -// -// (*) WhatAmI. It indicates the role of the zenoh node sending the HELLO message. -// The valid WhatAmI values are: -// - 0b00: Router -// - 0b01: Peer -// - 0b10: Client -// - 0b11: Reserved -// -typedef struct { - _z_id_t _zid; - _z_locator_array_t _locators; - z_whatami_t _whatami; - uint8_t _version; -} _z_s_msg_hello_t; -void _z_s_msg_clear_hello(_z_s_msg_hello_t *msg); - -/*------------------ Join Message ------------------*/ -// # Join message -// -// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length -// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. -// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve -// the boundary of the serialized messages. The length is encoded as little-endian. -// In any case, the length of a message must not exceed 65_535 bytes. -// -// The JOIN message is sent on a multicast Locator to advertise the transport parameters. -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |O|S|T| JOIN | -// +-+-+-+-+-------+ -// ~ |Q~ if O==1 -// +---------------+ -// | v_maj | v_min | -- Protocol Version VMaj.VMin -// +-------+-------+ -// ~ whatami ~ -- Router, Peer or a combination of them -// +---------------+ -// ~ zenoh_id ~ -- PID of the sender of the JOIN message -// +---------------+ -// ~ lease ~ -- Lease period of the sender of the JOIN message(*) -// +---------------+ -// ~ seq_num_res ~ if S==1(*) -- Otherwise 2^28 is assumed(**) -// +---------------+ -// ~ [next_sn] ~ (***) -// +---------------+ -// -// - if Q==1 then the sender supports QoS. -// -// (*) if T==1 then the lease period is expressed in seconds, otherwise in milliseconds -// (**) if S==0 then 2^28 is assumed. -// (***) if Q==1 then 8 sequence numbers are present: one for each priority. -// if Q==0 then only one sequence number is present. -// -typedef struct { - _z_zint_t _reliable; - _z_zint_t _best_effort; -} _z_coundit_sn_t; -typedef struct { - union { - _z_coundit_sn_t _plain; - _z_coundit_sn_t _qos[Z_PRIORITIES_NUM]; - } _val; - _Bool _is_qos; -} _z_conduit_sn_list_t; -typedef struct { - _z_id_t _zid; - _z_zint_t _lease; - _z_conduit_sn_list_t _next_sn; - uint16_t _batch_size; - z_whatami_t _whatami; - uint8_t _req_id_res; - uint8_t _seq_num_res; - uint8_t _version; -} _z_t_msg_join_t; -void _z_t_msg_clear_join(_z_t_msg_join_t *msg); - -/*------------------ Init Message ------------------*/ -// # Init message -// -// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length -// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. -// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve -// the boundary of the serialized messages. The length is encoded as little-endian. -// In any case, the length of a message must not exceed 65_535 bytes. -// -// The INIT message is sent on a specific Locator to initiate a session with the peer associated -// with that Locator. The initiator MUST send an INIT message with the A flag set to 0. If the -// corresponding peer deems appropriate to initialize a session with the initiator, the corresponding -// peer MUST reply with an INIT message with the A flag set to 1. -// -// Flags: -// - A: Ack if A==0 then the message is an InitSyn else it is an InitAck -// - S: Size params if S==1 then size parameters are exchanged -// - Z: Extensions if Z==1 then zenoh extensions will follow. -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|S|A| INIT | -// +-+-+-+---------+ -// | version | -// +---------------+ -// |zid_len|x|x|wai| (#)(*) -// +-------+-+-+---+ -// ~ [u8] ~ -- ZenohID of the sender of the INIT message -// +---------------+ -// |x|x|kid|rid|fsn| \ -- SN/ID resolution (+) -// +---------------+ | if Flag(S)==1 -// | u16 | | -- Batch Size ($) -// | | / -// +---------------+ -// ~ ~ -- if Flag(A)==1 -- Cookie -// +---------------+ -// ~ [InitExts] ~ -- if Flag(Z)==1 -// +---------------+ -// -// If A==1 and S==0 then size parameters are (ie. S flag) are accepted. -// -// (*) WhatAmI. It indicates the role of the zenoh node sending the INIT -// message. -// The valid WhatAmI values are: -// - 0b00: Router -// - 0b01: Peer -// - 0b10: Client -// - 0b11: Reserved -// -// (#) ZID length. It indicates how many bytes are used for the ZenohID bytes. -// A ZenohID is minimum 1 byte and maximum 16 bytes. Therefore, the actual -// lenght is computed as: -// real_zid_len := 1 + zid_len -// -// (+) Sequence Number/ID resolution. It indicates the resolution and -// consequently the wire overhead -// of various SN and ID in Zenoh. -// - fsn: frame/fragment sequence number resolution. Used in Frame/Fragment -// messages. -// - rid: request ID resolution. Used in Request/Response messages. -// - kid: key expression ID resolution. Used in Push/Request/Response -// messages. The valid SN/ID resolution values are: -// - 0b00: 8 bits -// - 0b01: 16 bits -// - 0b10: 32 bits -// - 0b11: 64 bits -// -// ($) Batch Size. It indicates the maximum size of a batch the sender of the -// -typedef struct { - _z_id_t _zid; - _z_bytes_t _cookie; - uint16_t _batch_size; - z_whatami_t _whatami; - uint8_t _req_id_res; - uint8_t _seq_num_res; - uint8_t _version; -} _z_t_msg_init_t; -void _z_t_msg_clear_init(_z_t_msg_init_t *msg); - -/*------------------ Open Message ------------------*/ -// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total lenght -// in bytes of the message, resulting in the maximum lenght of a message being 65_535 bytes. -// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve -// the boundary of the serialized messages. The length is encoded as little-endian. -// In any case, the lenght of a message must not exceed 65_535 bytes. -// -// The OPEN message is sent on a link to finally open an initialized session with the peer. -// -// Flags: -// - A Ack if A==1 then the message is an acknowledgment (aka OpenAck), otherwise OpenSyn -// - T Lease period if T==1 then the lease period is in seconds else in milliseconds -// - Z Extensions if Z==1 then Zenoh extensions are present -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|T|A| OPEN | -// +-+-+-+---------+ -// % lease % -- Lease period of the sender of the OPEN message -// +---------------+ -// % initial_sn % -- Initial SN proposed by the sender of the OPEN(*) -// +---------------+ -// ~ ~ if Flag(A)==0 (**) -- Cookie -// +---------------+ -// ~ [OpenExts] ~ if Flag(Z)==1 -// +---------------+ -// -// (*) The initial sequence number MUST be compatible with the sequence number resolution agreed in the -// [`super::InitSyn`]-[`super::InitAck`] message exchange -// (**) The cookie MUST be the same received in the [`super::InitAck`]from the corresponding zenoh node -// -typedef struct { - _z_zint_t _lease; - _z_zint_t _initial_sn; - _z_bytes_t _cookie; -} _z_t_msg_open_t; -void _z_t_msg_clear_open(_z_t_msg_open_t *msg); - -/*------------------ Close Message ------------------*/ -// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length -// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. -// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve -// the boundary of the serialized messages. The length is encoded as little-endian. -// In any case, the length of a message must not exceed 65_535 bytes. -// -// The CLOSE message is sent in any of the following two cases: -// 1) in response to an OPEN message which is not accepted; -// 2) at any time to arbitrarly close the session with the corresponding peer. -// -// Flags: -// - S: Session Close if S==1 Session close or S==0 Link close -// - X: Reserved -// - Z: Extensions if Z==1 then zenoh extensions will follow. -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|X|S| CLOSE | -// +-+-+-+---------+ -// | Reason | -// +---------------+ -// ~ [CloseExts] ~ if Flag(Z)==1 -// +---------------+ -// -typedef struct { - uint8_t _reason; -} _z_t_msg_close_t; -void _z_t_msg_clear_close(_z_t_msg_close_t *msg); - -/*------------------ Keep Alive Message ------------------*/ -// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length -// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. -// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve -// the boundary of the serialized messages. The length is encoded as little-endian. -// In any case, the length of a message must not exceed 65_535 bytes. -// -// The KEEP_ALIVE message can be sent periodically to avoid the expiration of the session lease -// period in case there are no messages to be sent. -// -// Flags: -// - X: Reserved -// - X: Reserved -// - Z: Extensions If Z==1 then Zenoh extensions will follow. -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|X|X| KALIVE | -// +-+-+-+---------+ -// ~ [KAliveExts] ~ if Flag(Z)==1 -// +---------------+ -// -typedef struct { - uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior -} _z_t_msg_keep_alive_t; -void _z_t_msg_clear_keep_alive(_z_t_msg_keep_alive_t *msg); - -/*------------------ Frame Message ------------------*/ -// NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length -// in bytes of the message, resulting in the maximum length of a message being 65_535 bytes. -// This is necessary in those stream-oriented transports (e.g., TCP) that do not preserve -// the boundary of the serialized messages. The length is encoded as little-endian. -// In any case, the length of a message must not exceed 65_535 bytes. -// -// Flags: -// - R: Reliable If R==1 it concerns the reliable channel, else the best-effort channel -// - X: Reserved -// - Z: Extensions If Z==1 then zenoh extensions will follow. -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|X|R| FRAME | -// +-+-+-+---------+ -// % seq num % -// +---------------+ -// ~ [FrameExts] ~ if Flag(Z)==1 -// +---------------+ -// ~ [NetworkMsg] ~ -// +---------------+ -// -// - if R==1 then the FRAME is sent on the reliable channel, best-effort otherwise. -// -typedef struct { - _z_network_message_vec_t _messages; - _z_zint_t _sn; -} _z_t_msg_frame_t; -void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg); - -/*------------------ Fragment Message ------------------*/ -// The Fragment message is used to transmit on the wire large Zenoh Message that require fragmentation -// because they are larger thatn the maximum batch size (i.e. 2^16-1) and/or the link MTU. -// -// The [`Fragment`] message flow is the following: -// -// Flags: -// - R: Reliable if R==1 it concerns the reliable channel, else the best-effort channel -// - M: More if M==1 then other fragments will follow -// - Z: Extensions if Z==1 then zenoh extensions will follow. -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |Z|M|R| FRAGMENT| -// +-+-+-+---------+ -// % seq num % -// +---------------+ -// ~ [FragExts] ~ if Flag(Z)==1 -// +---------------+ -// ~ [u8] ~ -// +---------------+ -// -typedef struct { - _z_payload_t _payload; - _z_zint_t _sn; -} _z_t_msg_fragment_t; -void _z_t_msg_clear_fragment(_z_t_msg_fragment_t *msg); - -/*------------------ Transport Message ------------------*/ -typedef union { - _z_t_msg_join_t _join; - _z_t_msg_init_t _init; - _z_t_msg_open_t _open; - _z_t_msg_close_t _close; - _z_t_msg_keep_alive_t _keep_alive; - _z_t_msg_frame_t _frame; - _z_t_msg_fragment_t _fragment; -} _z_transport_body_t; - -typedef struct { - _z_transport_body_t _body; - uint8_t _header; -} _z_transport_message_t; -void _z_t_msg_clear(_z_transport_message_t *msg); - -/*------------------ Builders ------------------*/ -_z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_id_t zid, - _z_conduit_sn_list_t next_sn); -_z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_id_t zid); -_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, _z_bytes_t cookie); -_z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie); -_z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial_sn); -_z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only); -_z_transport_message_t _z_t_msg_make_keep_alive(void); -_z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_network_message_vec_t messages, _Bool is_reliable); -_z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable); -_z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_payload_t messages, _Bool is_reliable, _Bool is_last); - -/*------------------ Copy ------------------*/ -void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg); -void _z_t_msg_copy_join(_z_t_msg_join_t *clone, _z_t_msg_join_t *msg); -void _z_t_msg_copy_init(_z_t_msg_init_t *clone, _z_t_msg_init_t *msg); -void _z_t_msg_copy_open(_z_t_msg_open_t *clone, _z_t_msg_open_t *msg); -void _z_t_msg_copy_close(_z_t_msg_close_t *clone, _z_t_msg_close_t *msg); -void _z_t_msg_copy_keep_alive(_z_t_msg_keep_alive_t *clone, _z_t_msg_keep_alive_t *msg); -void _z_t_msg_copy_frame(_z_t_msg_frame_t *clone, _z_t_msg_frame_t *msg); - -typedef union { - _z_s_msg_scout_t _scout; - _z_s_msg_hello_t _hello; -} _z_scouting_body_t; - -typedef struct { - _z_scouting_body_t _body; - uint8_t _header; -} _z_scouting_message_t; -void _z_s_msg_clear(_z_scouting_message_t *msg); - -_z_scouting_message_t _z_s_msg_make_scout(z_what_t what, _z_id_t zid); -_z_scouting_message_t _z_s_msg_make_hello(z_whatami_t whatami, _z_id_t zid, _z_locator_array_t locators); - -void _z_s_msg_copy(_z_scouting_message_t *clone, _z_scouting_message_t *msg); -void _z_s_msg_copy_scout(_z_s_msg_scout_t *clone, _z_s_msg_scout_t *msg); -void _z_s_msg_copy_hello(_z_s_msg_hello_t *clone, _z_s_msg_hello_t *msg); +// _z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool +// can_be_dropped); -#endif /* ZENOH_PICO_PROTOCOL_MSG_H */ +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_MSG_H */ diff --git a/include/zenoh-pico/protocol/msgcodec.h b/include/zenoh-pico/protocol/msgcodec.h index 9db399917..54c479641 100644 --- a/include/zenoh-pico/protocol/msgcodec.h +++ b/include/zenoh-pico/protocol/msgcodec.h @@ -12,8 +12,8 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_MSGCODEC_H -#define ZENOH_PICO_MSGCODEC_H +#ifndef INCLUDE_ZENOH_PICO_PROTOCOL_MSGCODEC_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_MSGCODEC_H #include #define _ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE 32 @@ -21,41 +21,22 @@ #include "zenoh-pico/collections/element.h" #include "zenoh-pico/link/endpoint.h" #include "zenoh-pico/protocol/codec.h" -#include "zenoh-pico/protocol/msg.h" +#include "zenoh-pico/protocol/definitions/transport.h" #include "zenoh-pico/utils/result.h" /*------------------ Zenoh Message ------------------*/ -int8_t _z_scouting_message_encode(_z_wbuf_t *buf, const _z_scouting_message_t *msg); -int8_t _z_scouting_message_decode(_z_scouting_message_t *msg, _z_zbuf_t *buf); -int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *buf); -int8_t _z_transport_message_encode(_z_wbuf_t *buf, const _z_transport_message_t *msg); -int8_t _z_transport_message_decode(_z_transport_message_t *msg, _z_zbuf_t *buf); - -int8_t _z_network_message_encode(_z_wbuf_t *buf, const _z_network_message_t *msg); -int8_t _z_network_message_decode(_z_network_message_t *msg, _z_zbuf_t *buf); -int8_t _z_network_message_decode_na(_z_network_message_t *msg, _z_zbuf_t *buf); - -int8_t _z_zenoh_message_encode(_z_wbuf_t *buf, const _z_zenoh_message_t *msg); -int8_t _z_zenoh_message_decode(_z_zenoh_message_t *msg, _z_zbuf_t *buf); -int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *buf); - -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); - -int8_t _z_timestamp_encode(_z_wbuf_t *buf, const _z_timestamp_t *ts); -int8_t _z_timestamp_decode(_z_timestamp_t *ts, _z_zbuf_t *buf); - -int8_t _z_push_body_encode(_z_wbuf_t *buf, const _z_push_body_t *ts); -int8_t _z_push_body_decode(_z_push_body_t *ts, _z_zbuf_t *buf); -#endif /* ZENOH_PICO_MSGCODEC_H */ +// int8_t _z_zenoh_message_encode(_z_wbuf_t *buf, const _z_zenoh_message_t *msg); +// int8_t _z_zenoh_message_decode(_z_zenoh_message_t *msg, _z_zbuf_t *buf); +// int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *buf); +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_MSGCODEC_H */ // NOTE: the following headers are for unit testing only #ifdef ZENOH_PICO_TEST_H // ------------------ Message Fields ------------------ -int8_t _z_payload_encode(_z_wbuf_t *buf, const _z_payload_t *pld); -int8_t _z_payload_decode(_z_payload_t *pld, _z_zbuf_t *buf); -int8_t _z_payload_decode_na(_z_payload_t *pld, _z_zbuf_t *buf); +int8_t _z_payload_encode(_z_wbuf_t *buf, const _z_bytes_t *pld); +int8_t _z_payload_decode(_z_bytes_t *pld, _z_zbuf_t *buf); +int8_t _z_payload_decode_na(_z_bytes_t *pld, _z_zbuf_t *buf); int8_t _z_subinfo_encode(_z_wbuf_t *buf, const _z_subinfo_t *si); int8_t _z_subinfo_decode(_z_subinfo_t *si, _z_zbuf_t *buf, uint8_t header); diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index 963475b1d..89e187d3c 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -12,8 +12,8 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_SESSION_TYPES_H -#define ZENOH_PICO_SESSION_TYPES_H +#ifndef INCLUDE_ZENOH_PICO_SESSION_SESSION_H +#define INCLUDE_ZENOH_PICO_SESSION_SESSION_H #include @@ -22,6 +22,7 @@ #include "zenoh-pico/collections/pointer.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" +#include "zenoh-pico/net/query.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/transport/manager.h" @@ -182,4 +183,4 @@ typedef void (*_z_hello_handler_t)(_z_hello_t *hello, struct __z_hello_handler_w int8_t _z_session_generate_zid(_z_id_t *bs, uint8_t size); -#endif /* ZENOH_PICO_SESSION_TYPES_H */ +#endif /* INCLUDE_ZENOH_PICO_SESSION_SESSION_H */ diff --git a/include/zenoh-pico/session/utils.h b/include/zenoh-pico/session/utils.h index 0c625d501..1af0b7114 100644 --- a/include/zenoh-pico/session/utils.h +++ b/include/zenoh-pico/session/utils.h @@ -12,8 +12,8 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_SESSION_UTILS_H -#define ZENOH_PICO_SESSION_UTILS_H +#ifndef INCLUDE_ZENOH_PICO_SESSION_UTILS_H +#define INCLUDE_ZENOH_PICO_SESSION_UTILS_H #include @@ -36,4 +36,4 @@ int8_t _z_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reliability_ int8_t _z_send_n_msg(_z_session_t *zn, _z_network_message_t *n_msg, z_reliability_t reliability, z_congestion_control_t cong_ctrl); -#endif /* ZENOH_PICO_SESSION_UTILS_H */ +#endif /* INCLUDE_ZENOH_PICO_SESSION_UTILS_H */ diff --git a/include/zenoh-pico/transport/manager.h b/include/zenoh-pico/transport/manager.h index d20315876..faac8c25e 100644 --- a/include/zenoh-pico/transport/manager.h +++ b/include/zenoh-pico/transport/manager.h @@ -12,8 +12,8 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_TRANSPORT_MANAGER_H -#define ZENOH_PICO_TRANSPORT_MANAGER_H +#ifndef INCLUDE_ZENOH_PICO_TRANSPORT_MANAGER_H +#define INCLUDE_ZENOH_PICO_TRANSPORT_MANAGER_H #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/link/manager.h" @@ -22,4 +22,4 @@ int8_t _z_new_transport(_z_transport_t *zt, _z_id_t *bs, char *locator, z_whatami_t mode); void _z_free_transport(_z_transport_t **zt); -#endif /* ZENOH_PICO_TRANSPORT_MANAGER_H */ +#endif /* INCLUDE_ZENOH_PICO_TRANSPORT_MANAGER_H */ diff --git a/include/zenoh-pico/transport/transport.h b/include/zenoh-pico/transport/transport.h index 1ee42173f..eff07f138 100644 --- a/include/zenoh-pico/transport/transport.h +++ b/include/zenoh-pico/transport/transport.h @@ -20,7 +20,7 @@ #include "zenoh-pico/config.h" #include "zenoh-pico/link/link.h" #include "zenoh-pico/protocol/core.h" -#include "zenoh-pico/protocol/msg.h" +#include "zenoh-pico/protocol/definitions/transport.h" typedef struct { // Defragmentation buffers diff --git a/src/net/primitives.c b/src/net/primitives.c index 7cf167fb9..d987d1280 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -287,9 +287,9 @@ int8_t _z_send_reply(const z_query_t *query, _z_keyexpr_t keyexpr, const uint8_t // Build the reply context decorator. This is NOT the final reply. _z_id_t zid = ((_z_session_t *)query->_zn)->_local_zid; - _z_data_info_t di = {._flags = 0}; // Empty data info - _z_payload_t pld = {.len = len, .start = payload}; // Payload - _Bool can_be_dropped = false; // Congestion control + _z_data_info_t di = {._flags = 0}; // Empty data info + _z_bytes_t pld = {.len = len, .start = payload}; // Payload + _Bool can_be_dropped = false; // Congestion control _z_zenoh_message_t z_msg = _z_msg_make_reply(keyexpr, di, pld, can_be_dropped); if (_z_send_z_msg(query->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { @@ -312,7 +312,7 @@ int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *pay _Z_SET_FLAG(info._flags, _Z_DATA_INFO_ENC); _Z_SET_FLAG(info._flags, _Z_DATA_INFO_KIND); - _z_payload_t pld = {.len = len, .start = payload}; // Payload + _z_bytes_t pld = {.len = len, .start = payload}; // Payload _Bool can_be_dropped = cong_ctrl == Z_CONGESTION_CONTROL_DROP; // Congestion control _z_zenoh_message_t z_msg = _z_msg_make_data(keyexpr, info, pld, can_be_dropped); diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c new file mode 100644 index 000000000..2a3a783ae --- /dev/null +++ b/src/protocol/codec/declarations.c @@ -0,0 +1,117 @@ +// +// 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, +// + +#include "zenoh-pico/protocol/codec/declarations.h" + +#include +#include + +#include "zenoh-pico/protocol/codec.h" +#include "zenoh-pico/protocol/codec/core.h" +#include "zenoh-pico/protocol/definitions/message.h" + +int8_t _z_decl_kexpr_encode(_z_wbuf_t *wbf, const _z_decl_kexpr_t *decl) { + uint8_t header = _Z_DECL_KEXPR_MID; + int has_kesuffix = _z_keyexpr_has_suffix(decl->_keyexpr); + if (has_kesuffix) { + header |= _Z_DECL_KEXPR_FLAG_N; + } + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, decl->_id)); + _Z_RETURN_IF_ERR(_z_keyexpr_encode(wbf, has_kesuffix, &decl->_keyexpr)) + + return _Z_RES_OK; +} +int8_t _z_undecl_kexpr_encode(_z_wbuf_t *wbf, const _z_undecl_kexpr_t *decl) { + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_UNDECL_KEXPR)); + return _z_zint_encode(wbf, decl->_id); +} +int8_t _z_decl_subscriber_encode(_z_wbuf_t *wbf, const _z_decl_subscriber_t *decl) { + uint8_t header = _Z_DECL_SUBSCRIBER_MID; + _Bool has_submode_ext = decl->_ext_subscriber_info._pull_mode || decl->_ext_subscriber_info._reliable; + int has_kesuffix = _z_keyexpr_has_suffix(decl->_keyexpr); + if (has_submode_ext) { + header |= _Z_FLAG_Z_Z; + } + if (has_kesuffix) { + header |= _Z_DECL_SUBSCRIBER_FLAG_N; + } + if (decl->_keyexpr.uses_remote_mapping) { + header |= _Z_DECL_SUBSCRIBER_FLAG_M; + } + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, decl->_id)); + _Z_RETURN_IF_ERR(_z_keyexpr_encode(wbf, has_kesuffix, &decl->_keyexpr)); + if (has_submode_ext) { + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | 0x01)); + _Z_RETURN_IF_ERR(_z_uint8_encode( + wbf, (decl->_ext_subscriber_info._pull_mode ? 2 : 0) | (decl->_ext_subscriber_info._reliable ? 1 : 0))); + } + + return _Z_RES_OK; +} +int8_t _z_undecl_subscriber_encode(_z_wbuf_t *wbf, const _z_undecl_subscriber_t *decl) { + uint8_t header = _Z_UNDECL_SUBSCRIBER_MID; + if (_z_keyexpr_check(decl->_ext_keyexpr)) { + header |= _Z_FLAG_Z_Z; + } + + return _Z_RES_OK; +} +int8_t _z_decl_queryable_encode(_z_wbuf_t *wbf, const _z_decl_queryable_t *decl) {} +int8_t _z_undecl_queryable_encode(_z_wbuf_t *wbf, const _z_undecl_queryable_t *decl) {} +int8_t _z_decl_token_encode(_z_wbuf_t *wbf, const _z_decl_token_t *decl) {} +int8_t _z_undecl_token_encode(_z_wbuf_t *wbf, const _z_undecl_token_t *decl) {} +int8_t _z_decl_interest_encode(_z_wbuf_t *wbf, const _z_decl_interest_t *decl) {} +int8_t _z_final_interest_encode(_z_wbuf_t *wbf, const _z_final_interest_t *decl) {} +int8_t _z_undecl_interest_encode(_z_wbuf_t *wbf, const _z_undecl_interest_t *decl) {} +int8_t _z_declaration_encode(_z_wbuf_t *wbf, const _z_declaration_t *decl) { + int8_t ret; + switch (decl->_tag) { + case _Z_DECL_KEXPR: { + ret = _z_decl_kexpr_encode(wbf, &decl->_body._decl_kexpr); + } break; + case _Z_UNDECL_KEXPR: { + ret = _z_undecl_kexpr_encode(wbf, &decl->_body._undecl_kexpr); + } break; + case _Z_DECL_SUBSCRIBER: { + ret = _z_decl_subscriber_encode(wbf, &decl->_body._decl_subscriber); + } break; + case _Z_UNDECL_SUBSCRIBER: { + ret = _z_undecl_subscriber_encode(wbf, &decl->_body._undecl_subscriber); + } break; + case _Z_DECL_QUERYABLE: { + ret = _z_decl_queryable_encode(wbf, &decl->_body._decl_queryable); + } break; + case _Z_UNDECL_QUERYABLE: { + ret = _z_undecl_queryable_encode(wbf, &decl->_body._undecl_queryable); + } break; + case _Z_DECL_TOKEN: { + ret = _z_decl_token_encode(wbf, &decl->_body._decl_token); + } break; + case _Z_UNDECL_TOKEN: { + ret = _z_undecl_token_encode(wbf, &decl->_body._undecl_token); + } break; + case _Z_DECL_INTEREST: { + ret = _z_decl_interest_encode(wbf, &decl->_body._decl_interest); + } break; + case _Z_FINAL_INTEREST: { + ret = _z_final_interest_encode(wbf, &decl->_body._final_interest); + } break; + case _Z_UNDECL_INTEREST: { + ret = _z_undecl_interest_encode(wbf, &decl->_body._undecl_interest); + } break; + } + return ret; +} \ No newline at end of file diff --git a/src/protocol/extcodec.c b/src/protocol/codec/ext.c similarity index 99% rename from src/protocol/extcodec.c rename to src/protocol/codec/ext.c index c8d088b4d..f9acc3b7a 100644 --- a/src/protocol/extcodec.c +++ b/src/protocol/codec/ext.c @@ -12,7 +12,7 @@ // ZettaScale Zenoh Team, // -#include "zenoh-pico/protocol/extcodec.h" +#include "zenoh-pico/protocol/ext.h" #include #include @@ -20,7 +20,7 @@ #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/protocol/codec.h" -#include "zenoh-pico/protocol/ext.h" +#include "zenoh-pico/protocol/codec/ext.h" #include "zenoh-pico/system/platform.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" diff --git a/src/protocol/msgcodec.c b/src/protocol/codec/message.c similarity index 81% rename from src/protocol/msgcodec.c rename to src/protocol/codec/message.c index 72ba8e66e..5624dd3ce 100644 --- a/src/protocol/msgcodec.c +++ b/src/protocol/codec/message.c @@ -24,12 +24,11 @@ #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/link/endpoint.h" #include "zenoh-pico/protocol/codec.h" +#include "zenoh-pico/protocol/codec/ext.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/ext.h" -#include "zenoh-pico/protocol/extcodec.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/keyexpr.h" -#include "zenoh-pico/protocol/msg.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" @@ -37,7 +36,7 @@ /* Fields */ /*=============================*/ /*------------------ Payload field ------------------*/ -int8_t _z_payload_encode(_z_wbuf_t *wbf, const _z_payload_t *pld) { +int8_t _z_payload_encode(_z_wbuf_t *wbf, const _z_bytes_t *pld) { int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _PAYLOAD\n"); ret |= _z_bytes_encode(wbf, pld); @@ -45,12 +44,12 @@ int8_t _z_payload_encode(_z_wbuf_t *wbf, const _z_payload_t *pld) { return ret; } -int8_t _z_payload_decode_na(_z_payload_t *pld, _z_zbuf_t *zbf) { +int8_t _z_payload_decode_na(_z_bytes_t *pld, _z_zbuf_t *zbf) { _Z_DEBUG("Decoding _PAYLOAD\n"); return _z_bytes_decode(pld, zbf); } -int8_t _z_payload_decode(_z_payload_t *pld, _z_zbuf_t *zbf) { return _z_payload_decode_na(pld, zbf); } +int8_t _z_payload_decode(_z_bytes_t *pld, _z_zbuf_t *zbf) { return _z_payload_decode_na(pld, zbf); } int8_t _z_id_encode_as_zbytes(_z_wbuf_t *wbf, const _z_id_t *id) { int8_t ret = _Z_RES_OK; @@ -180,122 +179,6 @@ int8_t _z_locators_decode(_z_locator_array_t *a_loc, _z_zbuf_t *zbf) { return _z /* Zenoh Messages */ /*=============================*/ -/*------------------ Data Info Field ------------------*/ -int8_t _z_data_info_encode(_z_wbuf_t *wbf, const _z_data_info_t *fld) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DATA_INFO\n"); - - // Encode the flags - // WARNING: we do not support sliced content in zenoh-pico. - // Disable the SLICED flag to be on the safe side. - _Z_EC(_z_zint_encode(wbf, fld->_flags & ~_Z_DATA_INFO_SLICED)) - - if (_Z_HAS_FLAG(fld->_flags, _Z_DATA_INFO_KIND) == true) { - _Z_EC(_z_uint8_encode(wbf, fld->_kind)) - } - if (_Z_HAS_FLAG(fld->_flags, _Z_DATA_INFO_ENC) == true) { - _Z_EC(_z_encoding_prefix_encode(wbf, fld->_encoding.prefix)) - _Z_EC(_z_bytes_encode(wbf, &fld->_encoding.suffix)) - } - if (_Z_HAS_FLAG(fld->_flags, _Z_DATA_INFO_TSTAMP) == true) { - _Z_EC(_z_timestamp_encode(wbf, &fld->_tstamp)) - } - if (_Z_HAS_FLAG(fld->_flags, _Z_DATA_INFO_SRC_ID) == true) { - _Z_EC(_z_bytes_encode(wbf, &fld->_source_id)) - } - if (_Z_HAS_FLAG(fld->_flags, _Z_DATA_INFO_SRC_SN) == true) { - _Z_EC(_z_zint_encode(wbf, fld->_source_sn)) - } - - return ret; -} - -int8_t _z_data_info_decode_na(_z_data_info_t *di, _z_zbuf_t *zbf) { - _Z_DEBUG("Decoding _Z_DATA_INFO\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&di->_flags, zbf); - // WARNING: we do not support sliced content in zenoh-pico. - // Return error in case the payload is sliced. - if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(di->_flags, _Z_DATA_INFO_SLICED) == false) { - if (_Z_HAS_FLAG(di->_flags, _Z_DATA_INFO_KIND) == true) { - ret |= _z_uint8_decode(&di->_kind, zbf); - } else { - di->_kind = Z_SAMPLE_KIND_PUT; - } - - if (_Z_HAS_FLAG(di->_flags, _Z_DATA_INFO_ENC) == true) { - ret |= _z_encoding_prefix_decode(&di->_encoding.prefix, zbf); - ret |= _z_bytes_decode(&di->_encoding.suffix, zbf); - } else { - di->_encoding.prefix = Z_ENCODING_PREFIX_EMPTY; - di->_encoding.suffix = _z_bytes_empty(); - } - - if (_Z_HAS_FLAG(di->_flags, _Z_DATA_INFO_TSTAMP) == true) { - ret |= _z_timestamp_decode(&di->_tstamp, zbf); - } else { - _z_timestamp_reset(&di->_tstamp); - } - - if (_Z_HAS_FLAG(di->_flags, _Z_DATA_INFO_SRC_ID) == true) { - ret |= _z_bytes_decode(&di->_source_id, zbf); - } else { - di->_source_id = _z_bytes_empty(); - } - - if (_Z_HAS_FLAG(di->_flags, _Z_DATA_INFO_SRC_SN) == true) { - ret |= _z_zint_decode(&di->_source_sn, zbf); - } else { - di->_source_sn = 0; - } - } - - return ret; -} - -int8_t _z_data_info_decode(_z_data_info_t *di, _z_zbuf_t *zbf) { return _z_data_info_decode_na(di, zbf); } - -/*------------------ Data Message ------------------*/ -int8_t _z_data_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_data_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_Z_DATA\n"); - - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &msg->_key)) - - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_I) == true) { - _Z_EC(_z_data_info_encode(wbf, &msg->_info)) - } - _Z_EC(_z_payload_encode(wbf, &msg->_payload)) - - return ret; -} - -int8_t _z_data_decode_na(_z_msg_data_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_Z_DATA\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_I) == true) { - ret |= _z_data_info_decode(&msg->_info, zbf); - } else { - msg->_info._flags = 0; - msg->_info._kind = Z_SAMPLE_KIND_PUT; - msg->_info._encoding.prefix = Z_ENCODING_PREFIX_EMPTY; - msg->_info._encoding.suffix = _z_bytes_empty(); - msg->_info._source_id = _z_bytes_empty(); - msg->_info._source_sn = 0; - _z_timestamp_reset(&msg->_info._tstamp); - } - ret |= _z_payload_decode(&msg->_payload, zbf); - - return ret; -} - -int8_t _z_data_decode(_z_msg_data_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_data_decode_na(msg, zbf, header); -} - int8_t _z_source_info_decode(_z_source_info_t *info, _z_zbuf_t *zbf) { uint8_t zidlen = 0; _z_zint_t intbuf; @@ -343,8 +226,8 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { (void)(pshb); int8_t ret = _Z_RES_OK; uint8_t header = pshb->_is_put ? _Z_M_PUT_ID : _Z_M_DEL_ID; - _Bool has_source_info = _z_id_check(pshb->_commons._source_info._id); - _Bool has_timestamp = _z_timestamp_check(&pshb->_commons._timestamp); + _Bool has_source_info = _z_id_check(pshb->_union._put._commons._source_info._id); + _Bool has_timestamp = _z_timestamp_check(&pshb->_union._put._commons._timestamp); _Bool has_encoding = false; if (has_source_info) { header |= _Z_FLAG_Z_Z; @@ -365,7 +248,7 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { } ret = _z_uint8_encode(wbf, header); if ((ret == _Z_RES_OK) && has_timestamp) { - ret = _z_timestamp_encode(wbf, &pshb->_commons._timestamp); + ret = _z_timestamp_encode(wbf, &pshb->_union._put._commons._timestamp); } if ((ret == _Z_RES_OK) && has_encoding) { @@ -375,7 +258,7 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { if ((ret == _Z_RES_OK) && has_source_info) { ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x01); - ret |= _z_source_info_encode(wbf, &pshb->_commons._source_info); + ret |= _z_source_info_encode(wbf, &pshb->_union._put._commons._source_info); } if ((ret == _Z_RES_OK) && pshb->_is_put) { @@ -390,7 +273,7 @@ int8_t _z_push_body_decode_extensions(_z_msg_ext_t *extension, void *ctx) { switch (_Z_EXT_FULL_ID(extension->_header)) { case _Z_MSG_EXT_ENC_ZBUF | 0x01: { _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); - ret = _z_source_info_decode(&pshb->_commons._source_info, &zbf); + ret = _z_source_info_decode(&pshb->_union._put._commons._source_info, &zbf); break; } default: @@ -401,18 +284,14 @@ int8_t _z_push_body_decode_extensions(_z_msg_ext_t *extension, void *ctx) { return ret; } -int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf) { - (void)(zbf); - (void)(pshb); +int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; - uint8_t header; - ret = _z_uint8_decode(&header, zbf); if (ret == _Z_RES_OK) { switch (_Z_MID(header)) { case _Z_M_PUT_ID: { pshb->_is_put = true; if (_Z_HAS_FLAG(header, _Z_FLAG_Z_P_T)) { - ret = _z_timestamp_decode(&pshb->_commons._timestamp, zbf); + ret = _z_timestamp_decode(&pshb->_union._put._commons._timestamp, zbf); } if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_P_E)) { ret = _z_encoding_prefix_decode(&pshb->_union._put._encoding.prefix, zbf); @@ -428,7 +307,7 @@ int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf) { case _Z_M_DEL_ID: { pshb->_is_put = false; if (_Z_HAS_FLAG(header, _Z_FLAG_Z_D_T)) { - ret = _z_timestamp_decode(&pshb->_commons._timestamp, zbf); + ret = _z_timestamp_decode(&pshb->_union._put._commons._timestamp, zbf); } if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { ret = _z_msg_ext_decode_iter(zbf, _z_push_body_decode_extensions, pshb); @@ -444,6 +323,30 @@ int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf) { return ret; } +int8_t _z_put_encode(_z_wbuf_t *wbf, const _z_msg_put_t *put) { + _z_push_body_t body = {._is_put = true, ._union = {._put = *put}}; + return _z_push_body_encode(wbf, &body); +} +int8_t _z_put_decode(_z_msg_put_t *put, _z_zbuf_t *zbf, uint8_t header) { + assert(_Z_MID(header) == _Z_MID_Z_PUT); + _z_push_body_t body = {._is_put = true, ._union = {._put = *put}}; + int8_t ret = _z_push_body_decode(&body, zbf, header); + *put = body._union._put; + return ret; +} + +int8_t _z_del_encode(_z_wbuf_t *wbf, const _z_msg_del_t *del) { + _z_push_body_t body = {._is_put = false, ._union = {._del = *del}}; + return _z_push_body_encode(wbf, &body); +} +int8_t _z_del_decode(_z_msg_del_t *del, _z_zbuf_t *zbf, uint8_t header) { + assert(_Z_MID(header) == _Z_MID_Z_DEL); + _z_push_body_t body = {._is_put = false, ._union = {._del = *del}}; + int8_t ret = _z_push_body_decode(&body, zbf, header); + *del = body._union._del; + return ret; +} + /*------------------ Query Message ------------------*/ int8_t _z_query_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_query_t *msg) { int8_t ret = _Z_RES_OK; @@ -530,7 +433,19 @@ int8_t _z_query_decode(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header) { return ret; } -int8_t _z_reply_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_reply_t *reply) { +int8_t _z_reply_encode(_z_wbuf_t *wbf, const _z_msg_reply_t *reply) { + uint8_t header = _Z_MID_Z_REPLY; + if (_z_timestamp_check(&reply->_timestamp)) { + header |= _Z_FLAG_Z_R_T; + } + if (reply->_value.encoding.prefix != Z_ENCODING_PREFIX_EMPTY || + !_z_bytes_is_empty(&reply->_value.encoding.suffix)) { + header |= _Z_FLAG_Z_R_E; + } + if (reply->_ext_consolidation != Z_CONSOLIDATION_MODE_AUTO || _z_id_check(reply->_ext_source_info._id)) { + header |= _Z_FLAG_Z_Z; + } + _Z_EC(_z_uint8_encode(wbf, header)); int8_t ret = _Z_RES_OK; if (_z_timestamp_check(&reply->_timestamp)) { assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_R_T)); @@ -542,18 +457,18 @@ int8_t _z_reply_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_reply_t *rep ret = _z_encoding_prefix_encode(wbf, reply->_value.encoding.prefix); ret |= _z_bytes_encode(wbf, &reply->_value.encoding.suffix); } - _Bool has_consolidation_ext = reply->_consolidation != Z_CONSOLIDATION_MODE_AUTO; - if ((ret == _Z_RES_OK) && _z_id_check(reply->_source_info._id)) { + _Bool has_consolidation_ext = reply->_ext_consolidation != Z_CONSOLIDATION_MODE_AUTO; + if ((ret == _Z_RES_OK) && _z_id_check(reply->_ext_source_info._id)) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01; if (has_consolidation_ext) { extheader |= _Z_MSG_EXT_FLAG_Z; } ret = _z_uint8_encode(wbf, extheader); - ret |= _z_source_info_encode(wbf, &reply->_source_info); + ret |= _z_source_info_encode(wbf, &reply->_ext_source_info); } if ((ret == _Z_RES_OK) && has_consolidation_ext) { ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M | 0x02); - ret |= _z_zint_encode(wbf, reply->_consolidation); + ret |= _z_zint_encode(wbf, reply->_ext_consolidation); } if (ret == _Z_RES_OK) { ret = _z_bytes_encode(wbf, &reply->_value.payload); @@ -566,11 +481,11 @@ int8_t _z_reply_decode_extension(_z_msg_ext_t *extension, void *ctx) { switch (_Z_EXT_FULL_ID(extension->_header)) { case _Z_MSG_EXT_ENC_ZBUF | 0x01: { _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); - ret = _z_source_info_decode(&reply->_source_info, &zbf); + ret = _z_source_info_decode(&reply->_ext_source_info, &zbf); break; } case _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M | 0x02: { - reply->_consolidation = extension->_body._zint._val; + reply->_ext_consolidation = extension->_body._zint._val; break; } default: @@ -599,15 +514,24 @@ int8_t _z_reply_decode(_z_msg_reply_t *reply, _z_zbuf_t *zbf, uint8_t header) { return ret; } -int8_t _z_err_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_err_t *err) { - int8_t ret = _z_zint_encode(wbf, err->_code); - if ((ret == _Z_RES_OK) && _z_timestamp_check(&err->_timestamp)) { - assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_E_T)); - ret = _z_timestamp_encode(wbf, &err->_timestamp); +int8_t _z_err_encode(_z_wbuf_t *wbf, const _z_msg_err_t *err) { + int8_t ret = _Z_RES_OK; + uint8_t header = _Z_MID_Z_ERR; + _Bool has_timestamp = _z_timestamp_check(&err->_timestamp); + if (has_timestamp) { + header |= _Z_FLAG_Z_E_T; } _Bool has_payload_ext = err->_ext_value.payload.start != NULL; - if ((ret == _Z_RES_OK) && _z_id_check(err->_ext_source_info._id)) { - assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); + _Bool has_sinfo_ext = _z_id_check(err->_ext_source_info._id); + if (has_sinfo_ext || has_payload_ext) { + header |= _Z_FLAG_Z_Z; + } + ret |= _z_uint8_encode(wbf, header); + ret |= _z_zint_encode(wbf, err->_code); + if ((ret == _Z_RES_OK) && has_timestamp) { + ret = _z_timestamp_encode(wbf, &err->_timestamp); + } + if ((ret == _Z_RES_OK) && has_sinfo_ext) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01; if (has_payload_ext) { extheader |= _Z_MSG_EXT_FLAG_Z; @@ -616,7 +540,6 @@ int8_t _z_err_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_err_t *err) { ret |= _z_source_info_encode(wbf, &err->_ext_source_info); } if ((ret == _Z_RES_OK) && has_payload_ext) { - assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x02); ret |= _z_encoding_prefix_encode(wbf, err->_ext_value.encoding.prefix); ret |= _z_bytes_encode(wbf, &err->_ext_value.encoding.suffix); @@ -667,13 +590,22 @@ int8_t _z_err_decode(_z_msg_err_t *err, _z_zbuf_t *zbf, uint8_t header) { return ret; } -int8_t _z_ack_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_ack_t *ack) { +int8_t _z_ack_encode(_z_wbuf_t *wbf, const _z_msg_ack_t *ack) { int8_t ret = _Z_RES_OK; - if (_z_timestamp_check(&ack->_timestamp)) { - assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_A_T)); + uint8_t header = _Z_MID_Z_ERR; + _Bool has_ts = _z_timestamp_check(&ack->_timestamp); + _Bool has_sinfo_ext = _z_id_check(ack->_ext_source_info._id); + if (has_ts) { + header |= _Z_FLAG_Z_A_T; + } + if (has_sinfo_ext) { + header |= _Z_FLAG_Z_Z; + } + ret = _z_uint8_encode(wbf, header); + if ((ret == _Z_RES_OK) && has_ts) { ret = _z_timestamp_encode(wbf, &ack->_timestamp); } - if ((ret == _Z_RES_OK) && _z_id_check(ack->_ext_source_info._id)) { + if ((ret == _Z_RES_OK) && has_sinfo_ext) { ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x01); ret |= _z_source_info_encode(wbf, &ack->_ext_source_info); } @@ -690,7 +622,7 @@ int8_t _z_ack_decode_extension(_z_msg_ext_t *extension, void *ctx) { } default: if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { - ret = _z_msg_ext_unknown_error(extension, 0x0a); + ret = _z_msg_ext_unknown_error(extension, 0x0b); } } return ret; @@ -725,7 +657,7 @@ int8_t _z_pull_decode_extension(_z_msg_ext_t *extension, void *ctx) { } default: if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { - ret = _z_msg_ext_unknown_error(extension, 0x0a); + ret = _z_msg_ext_unknown_error(extension, 0x0c); } } return ret; diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c new file mode 100644 index 000000000..5061ee2c3 --- /dev/null +++ b/src/protocol/codec/network.c @@ -0,0 +1,333 @@ +// +// 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, +// + +#include +#include +#include + +#include "zenoh-pico/api/constants.h" +#include "zenoh-pico/api/types.h" +#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/protocol/codec.h" +#include "zenoh-pico/protocol/codec/ext.h" +#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/ext.h" +#include "zenoh-pico/protocol/iobuf.h" +#include "zenoh-pico/protocol/msg.h" +#include "zenoh-pico/protocol/msgcodec.h" +#include "zenoh-pico/utils/logging.h" +#include "zenoh-pico/utils/result.h" + +/*------------------ Push Message ------------------*/ + +int8_t _z_push_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_push_t *msg) { + _Z_DEBUG("Encoding _Z_MID_N_PUSH\n"); + + _Z_RETURN_IF_ERR(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N), &msg->_key)); + + _Bool has_timestamp_ext = _z_timestamp_check(&msg->_timestamp); + + if (msg->_qos._val != _Z_N_QOS_DEFAULT._val) { + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | 0x01 | (has_timestamp_ext << 7))); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, msg->_qos._val)); + } + + if (has_timestamp_ext) { + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x02)); + _Z_RETURN_IF_ERR(_z_timestamp_encode(wbf, &msg->_timestamp)); + } + + _Z_RETURN_IF_ERR(_z_push_body_encode(wbf, &msg->_body)); + + return _Z_RES_OK; +} + +int8_t _z_push_decode_ext_cb(_z_msg_ext_t *extension, void *ctx) { + int8_t ret = _Z_RES_OK; + _z_n_msg_push_t *msg = (_z_n_msg_push_t *)ctx; + switch (_Z_EXT_FULL_ID(extension->_header)) { + case _Z_MSG_EXT_ENC_ZINT | 0x01: { // QOS ext + msg->_qos = (_z_n_qos_t){._val = extension->_body._zint._val}; + break; + } + case _Z_MSG_EXT_ENC_ZBUF | 0x02: { // Timestamp ext + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + ret = _z_timestamp_decode(&msg->_timestamp, &zbf); + break; + } + default: + if ((extension->_header & _Z_MSG_EXT_FLAG_M) != 0) { + ret = _z_msg_ext_unknown_error(extension, 0x07); + } + } + return ret; +} + +int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_N_PUSH\n"); + int8_t ret = _Z_RES_OK; + _z_timestamp_reset(&msg->_timestamp); + + ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N)); + if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_N_Z)) { + ret = _z_msg_ext_decode_iter(zbf, _z_push_decode_ext_cb, msg); + } + if (ret == _Z_RES_OK) { + uint8_t msgheader; + _Z_RETURN_IF_ERR(_z_uint8_decode(&msgheader, zbf)); + _Z_RETURN_IF_ERR(_z_push_body_decode(&msg->_body, zbf, msgheader)); + } + + return ret; +} + +/*------------------ Request Body Field ------------------*/ +int8_t _z_request_body_encode(_z_wbuf_t *wbf, const _z_request_body_t *reqb) { + (void)(wbf); + (void)(reqb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_request_body_decode_na(_z_request_body_t *reqb, _z_zbuf_t *zbf) { + (void)(zbf); + (void)(reqb); + int8_t ret = _Z_RES_OK; + + return ret; +} + +int8_t _z_request_body_decode(_z_request_body_t *reqb, _z_zbuf_t *zbf) { return _z_request_body_decode_na(reqb, zbf); } + +/*------------------ Request Message ------------------*/ +int8_t _z_request_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_request_t *msg) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_N_REQUEST\n"); + + _Z_EC(_z_zint_encode(wbf, msg->_rid)); + _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N), &msg->_key)); + _Z_EC(_z_request_body_encode(wbf, &msg->_body)); + + return ret; +} + +int8_t _z_request_decode_na(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_N_REQUEST\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_zint_decode(&msg->_rid, zbf); + ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N)); + ret |= _z_request_body_decode(&msg->_body, zbf); + + return ret; +} + +int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_request_decode_na(msg, zbf, header); +} + +/*------------------ Response Message ------------------*/ +int8_t _z_response_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_t *msg) { + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); + if (msg->_uses_sender_mapping) { + assert(_Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_M)); + } + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_request_id)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_key._id)); + if (msg->_key._suffix != NULL) { + assert(_Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N)); + _Z_RETURN_IF_ERR(_z_str_encode(wbf, msg->_key._suffix)) + } + _Bool has_qos_ext = msg->_ext_qos._val != _Z_N_QOS_DEFAULT._val; + _Bool has_ts_ext = _z_timestamp_check(&msg->_ext_timestamp); + if (has_qos_ext) { + assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); + uint8_t extheader = _Z_MSG_EXT_ENC_ZINT | 0x01; + if (has_ts_ext) { + extheader |= _Z_FLAG_Z_Z; + } + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_ext_qos._val)); + } + if (has_ts_ext) { + assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); + uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x02; + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); + _Z_RETURN_IF_ERR(_z_timestamp_encode(wbf, &msg->_ext_timestamp)); + } + + switch (msg->_body_tag) { + case _Z_RESPONSE_BODY_REPLY: { + _Z_RETURN_IF_ERR(_z_reply_encode(wbf, &msg->_body._reply)); + break; + } + case _Z_RESPONSE_BODY_ERR: { + _Z_RETURN_IF_ERR(_z_err_encode(wbf, &msg->_body._err)); + break; + } + case _Z_RESPONSE_BODY_ACK: { + _Z_RETURN_IF_ERR(_z_ack_encode(wbf, &msg->_body._ack)); + break; + } + case _Z_RESPONSE_BODY_PUT: { + _Z_RETURN_IF_ERR(_z_put_encode(wbf, &msg->_body._put)); + break; + } + case _Z_RESPONSE_BODY_DEL: { + _Z_RETURN_IF_ERR(_z_del_encode(wbf, &msg->_body._del)); + break; + } + } + + return ret; +} +int8_t _z_response_decode_extension(_z_msg_ext_t *extension, void *ctx) { + int8_t ret = _Z_RES_OK; + _z_n_msg_response_t *msg = (_z_n_msg_response_t *)ctx; + switch (_Z_EXT_FULL_ID(extension->_header)) { + case _Z_MSG_EXT_ENC_ZINT | 0x01: { + msg->_ext_qos._val = extension->_body._zint._val; + break; + } + case _Z_MSG_EXT_ENC_ZBUF | 0x02: { + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + ret = _z_timestamp_decode(&msg->_ext_timestamp, &zbf); + break; + } + default: + if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { + ret = _z_msg_ext_unknown_error(extension, 0x0d); + } + } + return ret; +} + +int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header) { + _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); + int8_t ret = _Z_RES_OK; + msg->_uses_sender_mapping = _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_M); + _Z_EC(_z_zint_decode(&msg->_request_id, zbf)); + _Z_EC(_z_zint_decode(&msg->_key._id, zbf)); + if (_Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N)) { + _Z_EC(_z_str_decode((char **)&msg->_key._suffix, zbf)) + } + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + _Z_EC(_z_msg_ext_decode_iter(zbf, _z_response_decode_extension, msg)); + } + + _Z_EC(_z_uint8_decode(&header, zbf)); + + switch (_Z_MID(header)) { + case _Z_MID_Z_REPLY: { + _z_reply_decode(&msg->_body._reply, zbf, header); + break; + } + case _Z_MID_Z_ERR: { + _z_err_decode(&msg->_body._err, zbf, header); + break; + } + case _Z_MID_Z_ACK: { + _z_ack_decode(&msg->_body._ack, zbf, header); + break; + } + case _Z_MID_Z_PUT: { + _z_put_decode(&msg->_body._put, zbf, header); + break; + } + case _Z_MID_Z_DEL: { + _z_del_decode(&msg->_body._del, zbf, header); + break; + } + default: { + ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + } + return ret; +} + +/*------------------ Response Final Message ------------------*/ +int8_t _z_response_final_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_final_t *msg) { + (void)(header); + int8_t ret = _Z_RES_OK; + _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); + + _Z_EC(_z_zint_encode(wbf, msg->_request_id)); + + return ret; +} + +int8_t _z_response_final_decode_na(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { + (void)(header); + _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); + int8_t ret = _Z_RES_OK; + + ret |= _z_zint_decode(&msg->_request_id, zbf); + + return ret; +} + +int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { + return _z_response_final_decode_na(msg, zbf, header); +} + +/*------------------ Network Message ------------------*/ +int8_t _z_network_message_encode(_z_wbuf_t *wbf, const _z_network_message_t *msg) { + int8_t ret = _Z_RES_OK; + + uint8_t header = msg->_header; + if (_z_msg_ext_vec_is_empty(&msg->_extensions) == false) { + header |= _Z_FLAG_N_Z; + } + + _Z_EC(_z_wbuf_write(wbf, header)) + switch (_Z_MID(msg->_header)) { + case _Z_MID_N_DECLARE: { + ret |= _z_declare_encode(wbf, &msg->_body._declare); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to encode network message with unknown ID(%d)\n", _Z_MID(msg->_header)); + ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; + } break; + } + + return ret; +} + +int8_t _z_network_message_decode_na(_z_network_message_t *msg, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + + ret |= _z_uint8_decode(&msg->_header, zbf); // Decode the header + if (ret == _Z_RES_OK) { + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_N_DECLARE: { + ret |= _z_declare_decode(&msg->_body._declare, zbf); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(%d)\n", mid); + ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; + } break; + } + } + + return ret; +} + +int8_t _z_network_message_decode(_z_network_message_t *n_msg, _z_zbuf_t *zbf) { + return _z_network_message_decode_na(n_msg, zbf); +} \ No newline at end of file diff --git a/src/protocol/transport_codec.c b/src/protocol/codec/transport.c similarity index 99% rename from src/protocol/transport_codec.c rename to src/protocol/codec/transport.c index 08088ca82..01684f69c 100644 --- a/src/protocol/transport_codec.c +++ b/src/protocol/codec/transport.c @@ -14,7 +14,7 @@ #include -#include "zenoh-pico/protocol/extcodec.h" +#include "zenoh-pico/protocol/codec/ext.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/utils/logging.h" diff --git a/src/protocol/definitions/declarations.c b/src/protocol/definitions/declarations.c new file mode 100644 index 000000000..094bef4fc --- /dev/null +++ b/src/protocol/definitions/declarations.c @@ -0,0 +1,64 @@ +// +// 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, +// + +#include "zenoh-pico/protocol/definitions/declarations.h" + +#include "zenoh-pico/protocol/keyexpr.h" + +void _z_declaration_clear(_z_declaration_t* decl) { + switch (decl->_tag) { + case _Z_DECL_KEXPR: { + _z_keyexpr_clear(&decl->_body._decl_kexpr._keyexpr); + break; + } + case _Z_UNDECL_KEXPR: { + break; + } + case _Z_DECL_SUBSCRIBER: { + _z_keyexpr_clear(&decl->_body._decl_subscriber._keyexpr); + break; + } + case _Z_UNDECL_SUBSCRIBER: { + _z_keyexpr_clear(&decl->_body._undecl_subscriber._ext_keyexpr); + break; + } + case _Z_DECL_QUERYABLE: { + _z_keyexpr_clear(&decl->_body._decl_queryable._keyexpr); + break; + } + case _Z_UNDECL_QUERYABLE: { + _z_keyexpr_clear(&decl->_body._undecl_queryable._ext_keyexpr); + break; + } + case _Z_DECL_TOKEN: { + _z_keyexpr_clear(&decl->_body._decl_token._keyexpr); + break; + } + case _Z_UNDECL_TOKEN: { + _z_keyexpr_clear(&decl->_body._undecl_token._ext_keyexpr); + break; + } + case _Z_DECL_INTEREST: { + _z_keyexpr_clear(&decl->_body._decl_interest._keyexpr); + break; + } + case _Z_FINAL_INTEREST: { + break; + } + case _Z_UNDECL_INTEREST: { + _z_keyexpr_clear(&decl->_body._undecl_interest._ext_keyexpr); + break; + } + } +} \ No newline at end of file diff --git a/src/protocol/msg.c b/src/protocol/msg.c index 8f7976a40..cc607f564 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -20,6 +20,7 @@ #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/ext.h" +#include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/session/queryable.h" #include "zenoh-pico/utils/logging.h" @@ -42,7 +43,7 @@ _z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, } /*------------------ Payload field ------------------*/ -void _z_payload_clear(_z_payload_t *p) { _z_bytes_clear(p); } +void _z_payload_clear(_z_bytes_t *p) { _z_bytes_clear(p); } /*------------------ Timestamp Field ------------------*/ void _z_timestamp_clear(_z_timestamp_t *ts) {} @@ -262,9 +263,38 @@ void _z_data_info_clear(_z_data_info_t *di) { _z_bytes_clear(&di->_source_id); _z_timestamp_clear(&di->_tstamp); } +void _z_msg_put_clear(_z_msg_put_t *msg) { + _z_bytes_clear(&msg->_encoding.suffix); + _z_bytes_clear(&msg->_payload); + _z_timestamp_clear(&msg->_commons._timestamp); +} +void _z_n_msg_response_clear(_z_n_msg_response_t *msg) { + _z_timestamp_clear(&msg->_ext_timestamp); + _z_keyexpr_clear(&msg->_key); + switch (msg->_body_tag) { + case _Z_RESPONSE_BODY_REPLY: { + _z_msg_reply_clear(&msg->_body._reply); + break; + } + case _Z_RESPONSE_BODY_ERR: { + _z_msg_err_clear(&msg->_body._err); + break; + } + case _Z_RESPONSE_BODY_ACK: { + break; + } + case _Z_RESPONSE_BODY_PUT: { + _z_msg_put_clear(&msg->_body._put); + break; + } + case _Z_RESPONSE_BODY_DEL: { + break; + } + } +} /*------------------ Data Message ------------------*/ -_z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_payload_t payload, _Bool can_be_dropped) { +_z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool can_be_dropped) { _z_zenoh_message_t msg; msg._body._data._key = key; @@ -357,8 +387,7 @@ void _z_msg_clear_query(_z_msg_query_t *msg) { } /*------------------ Reply Message ------------------*/ -_z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_payload_t payload, - _Bool can_be_dropped) { +_z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool can_be_dropped) { _z_zenoh_message_t msg = _z_msg_make_data(key, info, payload, can_be_dropped); return msg; @@ -492,7 +521,7 @@ _z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid) { _z_network_message_t msg; msg._header = _Z_MID_N_RESPONSE_FINAL; - msg._body._response_f._rid = rid; + msg._body._response_f._requestid = rid; msg._extensions = _z_msg_ext_vec_make(0); return msg; @@ -794,7 +823,7 @@ void _z_t_msg_copy_frame(_z_t_msg_frame_t *clone, _z_t_msg_frame_t *msg) { void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg) { _z_network_message_vec_clear(&msg->_messages); } /*------------------ Fragment Message ------------------*/ -_z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_payload_t payload, _Bool is_reliable, _Bool is_last) { +_z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_bytes_t payload, _Bool is_reliable, _Bool is_last) { _z_transport_message_t msg; msg._header = _Z_MID_T_FRAGMENT; if (is_last == true) { diff --git a/src/protocol/network_codec.c b/src/protocol/network_codec.c deleted file mode 100644 index 9fb3a385a..000000000 --- a/src/protocol/network_codec.c +++ /dev/null @@ -1,640 +0,0 @@ -// -// 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, -// - -#include -#include - -#include "zenoh-pico/protocol/codec.h" -#include "zenoh-pico/protocol/core.h" -#include "zenoh-pico/protocol/ext.h" -#include "zenoh-pico/protocol/extcodec.h" -#include "zenoh-pico/protocol/iobuf.h" -#include "zenoh-pico/protocol/msg.h" -#include "zenoh-pico/protocol/msgcodec.h" -#include "zenoh-pico/utils/logging.h" -#include "zenoh-pico/utils/result.h" - -/*------------------ SubMode Field ------------------*/ -int8_t _z_subinfo_encode(_z_wbuf_t *wbf, const _z_subinfo_t *fld) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _SUB_MODE\n"); - - uint8_t header = fld->mode; - if ((fld->period.origin != 0) || (fld->period.period != 0) || (fld->period.duration != 0)) { - _Z_SET_FLAG(header, _Z_FLAG_Z_P); - _Z_EC(_z_wbuf_write(wbf, header)) - ret |= _z_period_encode(wbf, &fld->period); - } else { - _Z_EC(_z_wbuf_write(wbf, header)) - } - - return ret; -} - -int8_t _z_subinfo_decode_na(_z_subinfo_t *si, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _SUB_MODE\n"); - int8_t ret = _Z_RES_OK; - - // Decode the header - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_R) == true) { - si->reliability = Z_RELIABILITY_RELIABLE; - } else { - si->reliability = Z_RELIABILITY_BEST_EFFORT; - } - - uint8_t h_subifo; - ret |= _z_uint8_decode(&h_subifo, zbf); - si->mode = _Z_MID(h_subifo); - if (_Z_HAS_FLAG(h_subifo, _Z_FLAG_Z_P) == true) { - ret |= _z_period_decode(&si->period, zbf); - } else { - si->period = (_z_period_t){.origin = 0, .period = 0, .duration = 0}; - } - - return ret; -} - -int8_t _z_subinfo_decode(_z_subinfo_t *si, _z_zbuf_t *zbf, uint8_t header) { - return _z_subinfo_decode_na(si, zbf, header); -} - -/*------------------ Resource Declaration ------------------*/ -int8_t _z_res_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_res_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_RESOURCE\n"); - - _Z_EC(_z_zint_encode(wbf, dcl->_id)) - ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); - - return ret; -} - -int8_t _z_res_decl_decode_na(_z_res_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_RESOURCE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&dcl->_id, zbf); - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - - return ret; -} - -int8_t _z_res_decl_decode(_z_res_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_res_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Publisher Declaration ------------------*/ -int8_t _z_pub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_pub_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_PUBLISHER\n"); - - ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); - - return ret; -} - -int8_t _z_pub_decl_decode_na(_z_pub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_PUBLISHER\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - - return ret; -} - -int8_t _z_pub_decl_decode(_z_pub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_pub_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Subscriber Declaration ------------------*/ -int8_t _z_sub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_sub_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_SUBSCRIBER\n"); - - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key)) - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_S) == true) { - ret |= _z_subinfo_encode(wbf, &dcl->_subinfo); - } - - return ret; -} - -int8_t _z_sub_decl_decode_na(_z_sub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_SUBSCRIBER\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_S) == true) { - ret |= _z_subinfo_decode(&dcl->_subinfo, zbf, header); - } else { - dcl->_subinfo.mode = Z_SUBMODE_PUSH; // Default subscription mode is non-periodic PUSH - dcl->_subinfo.period = (_z_period_t){.origin = 0, .period = 0, .duration = 0}; - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_R) == true) { - dcl->_subinfo.reliability = Z_RELIABILITY_RELIABLE; - } else { - dcl->_subinfo.reliability = Z_RELIABILITY_BEST_EFFORT; - } - } - - return ret; -} - -int8_t _z_sub_decl_decode(_z_sub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_sub_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Queryable Declaration ------------------*/ -int8_t _z_qle_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_qle_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_QUERYABLE\n"); - - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key)); - - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Q) == true) { - _Z_EC(_z_zint_encode(wbf, dcl->_complete)); - _Z_EC(_z_zint_encode(wbf, dcl->_distance)); - } - - return ret; -} - -int8_t _z_qle_decl_decode_na(_z_qle_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_QUERYABLE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Q) == true) { - ret |= _z_zint_decode(&dcl->_complete, zbf); - ret |= _z_zint_decode(&dcl->_distance, zbf); - } else { - dcl->_complete = 0; - dcl->_distance = 0; - } - - return ret; -} - -int8_t _z_qle_decl_decode(_z_qle_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_qle_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Forget Resource Declaration ------------------*/ -int8_t _z_forget_res_decl_encode(_z_wbuf_t *wbf, const _z_forget_res_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_FORGET_RESOURCE\n"); - - ret |= _z_zint_encode(wbf, dcl->_rid); - - return ret; -} - -int8_t _z_forget_res_decl_decode_na(_z_forget_res_decl_t *dcl, _z_zbuf_t *zbf) { - _Z_DEBUG("Decoding _Z_DECL_FORGET_RESOURCE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&dcl->_rid, zbf); - - return ret; -} - -int8_t _z_forget_res_decl_decode(_z_forget_res_decl_t *dcl, _z_zbuf_t *zbf) { - return _z_forget_res_decl_decode_na(dcl, zbf); -} - -/*------------------ Forget Publisher Declaration ------------------*/ -int8_t _z_forget_pub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_forget_pub_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_FORGET_PUBLISHER\n"); - - ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); - - return ret; -} - -int8_t _z_forget_pub_decl_decode_na(_z_forget_pub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_FORGET_PUBLISHER\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - - return ret; -} - -int8_t _z_forget_pub_decl_decode(_z_forget_pub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_forget_pub_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Forget Subscriber Declaration ------------------*/ -int8_t _z_forget_sub_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_forget_sub_decl_t *dcl) { - _Z_DEBUG("Encoding _Z_DECL_FORGET_PUBLISHER\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); - - return ret; -} - -int8_t _z_forget_sub_decl_decode_na(_z_forget_sub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_FORGET_PUBLISHER\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - - return ret; -} - -int8_t _z_forget_sub_decl_decode(_z_forget_sub_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_forget_sub_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Forget Queryable Declaration ------------------*/ -int8_t _z_forget_qle_decl_encode(_z_wbuf_t *wbf, uint8_t header, const _z_forget_qle_decl_t *dcl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_DECL_FORGET_QUERYABLE\n"); - - ret |= _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K), &dcl->_key); - - return ret; -} - -int8_t _z_forget_qle_decl_decode_na(_z_forget_qle_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_DECL_FORGET_QUERYABLE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_keyexpr_decode(&dcl->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_Z_K)); - - return ret; -} - -int8_t _z_forget_qle_decl_decode(_z_forget_qle_decl_t *dcl, _z_zbuf_t *zbf, uint8_t header) { - return _z_forget_qle_decl_decode_na(dcl, zbf, header); -} - -/*------------------ Declaration Field ------------------*/ -int8_t _z_declaration_encode(_z_wbuf_t *wbf, const _z_declaration_t *dcl) { - int8_t ret = _Z_RES_OK; - - _Z_EC(_z_wbuf_write(wbf, dcl->_header)) - uint8_t did = _Z_MID(dcl->_header); - switch (did) { - case _Z_DECL_RESOURCE: { - ret |= _z_res_decl_encode(wbf, dcl->_header, &dcl->_body._res); - } break; - - case _Z_DECL_PUBLISHER: { - ret |= _z_pub_decl_encode(wbf, dcl->_header, &dcl->_body._pub); - } break; - - case _Z_DECL_SUBSCRIBER: { - ret |= _z_sub_decl_encode(wbf, dcl->_header, &dcl->_body._sub); - } break; - - case _Z_DECL_QUERYABLE: { - ret |= _z_qle_decl_encode(wbf, dcl->_header, &dcl->_body._qle); - } break; - - case _Z_DECL_FORGET_RESOURCE: { - ret |= _z_forget_res_decl_encode(wbf, &dcl->_body._forget_res); - } break; - - case _Z_DECL_FORGET_PUBLISHER: { - ret |= _z_forget_pub_decl_encode(wbf, dcl->_header, &dcl->_body._forget_pub); - } break; - - case _Z_DECL_FORGET_SUBSCRIBER: { - ret |= _z_forget_sub_decl_encode(wbf, dcl->_header, &dcl->_body._forget_sub); - } break; - - case _Z_DECL_FORGET_QUERYABLE: { - ret |= _z_forget_qle_decl_encode(wbf, dcl->_header, &dcl->_body._forget_qle); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to encode declaration with unknown ID(%d)\n", did); - ret |= _Z_ERR_MESSAGE_SERIALIZATION_FAILED; - } break; - } - - return ret; -} - -int8_t _z_declaration_decode_na(_z_declaration_t *decl, _z_zbuf_t *zbf) { - int8_t ret = _Z_RES_OK; - - ret |= _z_uint8_decode(&decl->_header, zbf); - if (ret == _Z_RES_OK) { - uint8_t mid = _Z_MID(decl->_header); - - switch (mid) { - case _Z_DECL_RESOURCE: { - ret |= _z_res_decl_decode(&decl->_body._res, zbf, decl->_header); - } break; - - case _Z_DECL_PUBLISHER: { - ret |= _z_pub_decl_decode(&decl->_body._pub, zbf, decl->_header); - } break; - - case _Z_DECL_SUBSCRIBER: { - ret |= _z_sub_decl_decode(&decl->_body._sub, zbf, decl->_header); - } break; - - case _Z_DECL_QUERYABLE: { - ret |= _z_qle_decl_decode(&decl->_body._qle, zbf, decl->_header); - } break; - - case _Z_DECL_FORGET_RESOURCE: { - ret |= _z_forget_res_decl_decode(&decl->_body._forget_res, zbf); - } break; - - case _Z_DECL_FORGET_PUBLISHER: { - ret |= _z_forget_pub_decl_decode(&decl->_body._forget_pub, zbf, decl->_header); - } break; - - case _Z_DECL_FORGET_SUBSCRIBER: { - ret |= _z_forget_sub_decl_decode(&decl->_body._forget_sub, zbf, decl->_header); - } break; - - case _Z_DECL_FORGET_QUERYABLE: { - ret |= _z_forget_qle_decl_decode(&decl->_body._forget_qle, zbf, decl->_header); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to decode declaration with unknown ID(%d)\n", mid); - ret |= _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; - } break; - } - } - - return ret; -} - -int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf) { return _z_declaration_decode_na(decl, zbf); } - -/*------------------ Declaration Message ------------------*/ -int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_n_msg_declare_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_N_DECLARE\n"); - - _Z_EC(_z_declaration_encode(wbf, &msg->_declaration)); - - return ret; -} - -int8_t _z_declare_decode_na(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf) { - _Z_DEBUG("Decoding _Z_MID_N_DECLARE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_declaration_decode_na(&msg->_declaration, zbf); - if (ret != _Z_RES_OK) { - // TODO[protocol]: Add empty declaration - } - - return ret; -} - -int8_t _z_declare_decode(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf) { return _z_declare_decode_na(msg, zbf); } - -/*------------------ Push Message ------------------*/ -int8_t _z_push_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_push_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_N_PUSH\n"); - - ret = _z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N), &msg->_key); - - _Bool has_timestamp_ext = _z_timestamp_check(&msg->_timestamp); - - if ((ret == _Z_RES_OK) && msg->_qos._val != _Z_N_QOS_DEFAULT._val) { - ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | 0x01 | (has_timestamp_ext << 7)); - ret |= _z_uint8_encode(wbf, msg->_qos._val); - } - - if ((ret == _Z_RES_OK) && has_timestamp_ext) { - ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x02); - ret |= _z_timestamp_encode(wbf, &msg->_timestamp); - } - - if (ret == _Z_RES_OK) { - ret = _z_push_body_encode(wbf, &msg->_body); - } - - return ret; -} - -int8_t _z_push_decode_ext_cb(_z_msg_ext_t *extension, void *ctx) { - int8_t ret = _Z_RES_OK; - _z_n_msg_push_t *msg = (_z_n_msg_push_t *)ctx; - switch (_Z_EXT_FULL_ID(extension->_header)) { - case _Z_MSG_EXT_ENC_ZINT | 0x01: { // QOS ext - msg->_qos = (_z_n_qos_t){._val = extension->_body._zint._val}; - break; - } - case _Z_MSG_EXT_ENC_ZBUF | 0x02: { // Timestamp ext - _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); - ret = _z_timestamp_decode(&msg->_timestamp, &zbf); - break; - } - default: - if ((extension->_header & _Z_MSG_EXT_FLAG_M) != 0) { - ret = _z_msg_ext_unknown_error(extension, 0x07); - } - } - return ret; -} - -int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_N_PUSH\n"); - int8_t ret = _Z_RES_OK; - _z_timestamp_reset(&msg->_timestamp); - - ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N)); - if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_N_Z)) { - ret = _z_msg_ext_decode_iter(zbf, _z_push_decode_ext_cb, msg); - } - if (ret == _Z_RES_OK) { - ret = _z_push_body_decode(&msg->_body, zbf); - } - if (ret == _Z_RES_OK) { - // TODO[protocol] decode put/del - } - - return ret; -} - -/*------------------ Request Body Field ------------------*/ -int8_t _z_request_body_encode(_z_wbuf_t *wbf, const _z_request_body_t *reqb) { - (void)(wbf); - (void)(reqb); - int8_t ret = _Z_RES_OK; - - return ret; -} - -int8_t _z_request_body_decode_na(_z_request_body_t *reqb, _z_zbuf_t *zbf) { - (void)(zbf); - (void)(reqb); - int8_t ret = _Z_RES_OK; - - return ret; -} - -int8_t _z_request_body_decode(_z_request_body_t *reqb, _z_zbuf_t *zbf) { return _z_request_body_decode_na(reqb, zbf); } - -/*------------------ Request Message ------------------*/ -int8_t _z_request_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_request_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_N_REQUEST\n"); - - _Z_EC(_z_zint_encode(wbf, msg->_rid)); - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N), &msg->_key)); - _Z_EC(_z_request_body_encode(wbf, &msg->_body)); - - return ret; -} - -int8_t _z_request_decode_na(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_N_REQUEST\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&msg->_rid, zbf); - ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N)); - ret |= _z_request_body_decode(&msg->_body, zbf); - - return ret; -} - -int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_request_decode_na(msg, zbf, header); -} - -/*------------------ Response Body Field ------------------*/ -int8_t _z_response_body_encode(_z_wbuf_t *wbf, const _z_response_body_t *rspb) { - (void)(wbf); - (void)(rspb); - int8_t ret = _Z_RES_OK; - - return ret; -} - -int8_t _z_response_body_decode_na(_z_response_body_t *rspb, _z_zbuf_t *zbf) { - (void)(zbf); - (void)(rspb); - int8_t ret = _Z_RES_OK; - - return ret; -} - -int8_t _z_response_body_decode(_z_response_body_t *rspb, _z_zbuf_t *zbf) { - return _z_response_body_decode_na(rspb, zbf); -} - -/*------------------ Response Message ------------------*/ -int8_t _z_response_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_reply_t *msg) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); - - _Z_EC(_z_zint_encode(wbf, msg->_rid)); - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N), &msg->_key)); - _Z_EC(_z_response_body_encode(wbf, &msg->_body)); - - return ret; -} - -int8_t _z_response_decode_na(_z_n_msg_reply_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&msg->_rid, zbf); - ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N)); - ret |= _z_response_body_decode(&msg->_body, zbf); - - return ret; -} - -int8_t _z_response_decode(_z_n_msg_reply_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_response_decode_na(msg, zbf, header); -} - -/*------------------ Response Final Message ------------------*/ -int8_t _z_response_final_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_final_t *msg) { - (void)(header); - int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); - - _Z_EC(_z_zint_encode(wbf, msg->_rid)); - - return ret; -} - -int8_t _z_response_final_decode_na(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { - (void)(header); - _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&msg->_rid, zbf); - - return ret; -} - -int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_response_final_decode_na(msg, zbf, header); -} - -/*------------------ Network Message ------------------*/ -int8_t _z_network_message_encode(_z_wbuf_t *wbf, const _z_network_message_t *msg) { - int8_t ret = _Z_RES_OK; - - uint8_t header = msg->_header; - if (_z_msg_ext_vec_is_empty(&msg->_extensions) == false) { - header |= _Z_FLAG_N_Z; - } - - _Z_EC(_z_wbuf_write(wbf, header)) - switch (_Z_MID(msg->_header)) { - case _Z_MID_N_DECLARE: { - ret |= _z_declare_encode(wbf, &msg->_body._declare); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to encode network message with unknown ID(%d)\n", _Z_MID(msg->_header)); - ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; - } break; - } - - return ret; -} - -int8_t _z_network_message_decode_na(_z_network_message_t *msg, _z_zbuf_t *zbf) { - int8_t ret = _Z_RES_OK; - - ret |= _z_uint8_decode(&msg->_header, zbf); // Decode the header - if (ret == _Z_RES_OK) { - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_N_DECLARE: { - ret |= _z_declare_decode(&msg->_body._declare, zbf); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(%d)\n", mid); - ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; - } break; - } - } - - return ret; -} - -int8_t _z_network_message_decode(_z_network_message_t *n_msg, _z_zbuf_t *zbf) { - return _z_network_message_decode_na(n_msg, zbf); -} \ No newline at end of file diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index f973c6124..7f69b785c 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -153,8 +153,8 @@ _z_wbuf_t gen_wbuf(size_t len) { return _z_wbuf_make(len, is_expandable); } -_z_payload_t gen_payload(size_t len) { - _z_payload_t pld; +_z_bytes_t gen_payload(size_t len) { + _z_bytes_t pld; pld._is_alloc = true; pld.len = len; pld.start = (uint8_t *)z_malloc(len); @@ -537,14 +537,14 @@ void message_extension(void) { /* Message Fields */ /*=============================*/ /*------------------ Payload field ------------------*/ -void assert_eq_payload(_z_payload_t *left, _z_payload_t *right) { assert_eq_uint8_array(left, right); } +void assert_eq_payload(_z_bytes_t *left, _z_bytes_t *right) { assert_eq_uint8_array(left, right); } void payload_field(void) { printf("\n>> Payload field\n"); _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - _z_payload_t e_pld = gen_payload(64); + _z_bytes_t e_pld = gen_payload(64); // Encode int8_t res = _z_payload_encode(&wbf, &e_pld); @@ -554,7 +554,7 @@ void payload_field(void) { // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_payload_t d_pld; + _z_bytes_t d_pld; res = _z_payload_decode(&d_pld, &zbf); assert(res == _Z_RES_OK); printf(" "); @@ -1365,7 +1365,7 @@ _z_zenoh_message_t gen_data_message(void) { } _Bool can_be_dropped = gen_bool(); - _z_payload_t payload = gen_payload(1 + gen_zint() % 64); + _z_bytes_t payload = gen_payload(1 + gen_zint() % 64); return _z_msg_make_data(key, info, payload, can_be_dropped); } @@ -1965,7 +1965,7 @@ _z_network_message_t gen_response_final_message(void) { void assert_eq_response_final_message(_z_n_msg_response_final_t *left, _z_n_msg_response_final_t *right) { printf(" "); - assert(left->_rid == right->_rid); + assert(left->_requestid == right->_requestid); printf("\n"); // TODO[protocol]: body @@ -2542,8 +2542,8 @@ _z_transport_message_t gen_fragment_message(void) { _z_zint_t sn = gen_zint(); _Bool is_reliable = gen_bool(); _Bool is_last = gen_bool(); - _z_payload_t payload = gen_bytes(gen_uint16() % 60000); // 60000 is just to make sure that we are not generating a - // payload bigger than what the fragment can hold + _z_bytes_t payload = gen_bytes(gen_uint16() % 60000); // 60000 is just to make sure that we are not generating a + // payload bigger than what the fragment can hold return _z_t_msg_make_fragment(sn, payload, is_reliable, is_last); } From 96b28dbd11737cb98eaa9b22dbcc4ea1d5ddbd9e Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 30 Jun 2023 11:04:50 +0200 Subject: [PATCH 106/181] declarations done [skip ci] --- CMakeLists.txt | 5 +- include/zenoh-pico/protocol/codec/core.h | 4 +- include/zenoh-pico/protocol/codec/message.h | 3 + include/zenoh-pico/protocol/core.h | 5 +- .../zenoh-pico/protocol/definitions/network.h | 21 +- include/zenoh-pico/protocol/iobuf.h | 2 + src/protocol/codec.c | 26 +- src/protocol/codec/core.c | 10 + src/protocol/codec/declarations.c | 320 ++++++++++++++++-- src/protocol/codec/message.c | 23 +- src/protocol/codec/network.c | 127 +++---- 11 files changed, 420 insertions(+), 126 deletions(-) create mode 100644 src/protocol/codec/core.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c8151bb9..c472e7b70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,7 +125,8 @@ file(GLOB PublicHeaders "include/*.h" "include/zenoh-pico/collections/*.h" "include/zenoh-pico/link/*.h" "include/zenoh-pico/link/config/*.h" - "include/zenoh-pico/protocol/*.h" + "include/zenoh-pico/protocol/*/*.h" + "include/zenoh-pico/protocol/*.h" "include/zenoh-pico/session/*.h" "include/zenoh-pico/system/*.h" "include/zenoh-pico/system/link/*.h" @@ -143,7 +144,7 @@ file(GLOB Sources "src/*.c" "src/link/unicast/*.c" "src/link/multicast/*.c" "src/protocol/*.c" - "src/protocol/keyexpr/*.c" + "src/protocol/*/*.c" "src/session/*.c" "src/system/*.c" "src/transport/*.c" diff --git a/include/zenoh-pico/protocol/codec/core.h b/include/zenoh-pico/protocol/codec/core.h index 0db2b49c8..a54a61f70 100644 --- a/include/zenoh-pico/protocol/codec/core.h +++ b/include/zenoh-pico/protocol/codec/core.h @@ -54,6 +54,8 @@ int8_t _z_uint64_encode(_z_wbuf_t *buf, uint64_t v); int8_t _z_uint64_decode(uint64_t *u64, _z_zbuf_t *buf); int8_t _z_zint_encode(_z_wbuf_t *buf, _z_zint_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_zint_decode(_z_zint_t *zint, _z_zbuf_t *buf); int8_t _z_bytes_val_encode(_z_wbuf_t *buf, const _z_bytes_t *bs); @@ -62,7 +64,7 @@ int8_t _z_bytes_val_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf); int8_t _z_bytes_encode(_z_wbuf_t *buf, const _z_bytes_t *bs); int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *buf); -int8_t _z_bytes_decode_na(_z_bytes_t *bs, _z_zbuf_t *buf); +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); int8_t _z_str_decode(char **str, _z_zbuf_t *buf); diff --git a/include/zenoh-pico/protocol/codec/message.h b/include/zenoh-pico/protocol/codec/message.h index 0658f67ab..b95c93950 100644 --- a/include/zenoh-pico/protocol/codec/message.h +++ b/include/zenoh-pico/protocol/codec/message.h @@ -21,6 +21,9 @@ int8_t _z_push_body_encode(_z_wbuf_t *buf, const _z_push_body_t *ts); int8_t _z_push_body_decode(_z_push_body_t *ts, _z_zbuf_t *buf, uint8_t header); +int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *reply); +int8_t _z_query_decode(_z_msg_query_t *reply, _z_zbuf_t *zbf, uint8_t header); + int8_t _z_reply_encode(_z_wbuf_t *wbf, const _z_msg_reply_t *reply); int8_t _z_reply_decode(_z_msg_reply_t *reply, _z_zbuf_t *zbf, uint8_t header); diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 375475fab..15d7f5368 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -79,8 +79,9 @@ _Bool _z_timestamp_check(const _z_timestamp_t *stamp); */ typedef struct { uint16_t _id; - _Bool uses_remote_mapping; - const char *_suffix; + _Bool _uses_remote_mapping; + _Bool is_alloc; + char *_suffix; } _z_keyexpr_t; static inline _Bool _z_keyexpr_has_suffix(_z_keyexpr_t ke) { return (ke._suffix != NULL) && (ke._suffix[0] != 0); } static inline _Bool _z_keyexpr_check(_z_keyexpr_t ke) { return (ke._id != 0) || _z_keyexpr_has_suffix(ke); } diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index add8dc94d..f6316a9de 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -15,6 +15,9 @@ #ifndef INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_NETWORK_H #define INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_NETWORK_H +#include + +#include "zenoh-pico/api/constants.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/definitions/declarations.h" #include "zenoh-pico/protocol/definitions/message.h" @@ -89,7 +92,19 @@ typedef struct { typedef struct { _z_zint_t _rid; _z_keyexpr_t _key; - // _z_request_body_t _body; + _z_n_qos_t ext_qos; + _z_timestamp_t ext_tstamp; + _z_id_t ext_nodeid; + z_query_target_t ext_target; + uint32_t ext_budget; + uint32_t ext_timeout_ms; + enum { _Z_REQUEST_QUERY, _Z_REQUEST_PUT, _Z_REQUEST_DEL, _Z_REQUEST_PULL } _tag; + union { + _z_msg_query_t query; + _z_msg_put_t put; + _z_msg_del_t del; + _z_msg_pull_t pull; + } _body; } _z_n_msg_request_t; void _z_n_msg_clear_request(_z_n_msg_request_t *msg); @@ -152,6 +167,10 @@ typedef struct { _z_zint_t _request_id; _z_keyexpr_t _key; _z_n_qos_t _ext_qos; + struct { + _z_id_t _zid; + uint32_t _eid; + } _ext_responder; _Bool _uses_sender_mapping; enum { _Z_RESPONSE_BODY_REPLY, diff --git a/include/zenoh-pico/protocol/iobuf.h b/include/zenoh-pico/protocol/iobuf.h index 1ffc0b47f..8707bc35c 100644 --- a/include/zenoh-pico/protocol/iobuf.h +++ b/include/zenoh-pico/protocol/iobuf.h @@ -16,6 +16,8 @@ #define ZENOH_PICO_PROTOCOL_IOBUF_H #include +#include +#include #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/element.h" diff --git a/src/protocol/codec.c b/src/protocol/codec.c index d3f86597d..28110a558 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -14,6 +14,9 @@ #include "zenoh-pico/protocol/codec.h" +#include + +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" @@ -211,7 +214,28 @@ int8_t _z_zint_encode(_z_wbuf_t *wbf, _z_zint_t v) { uint8_t c = lv & 0xff; return _z_wbuf_write(wbf, c); } - +int8_t _z_zint16_decode(uint16_t *zint, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + _z_zint_t buf; + _Z_RETURN_IF_ERR(_z_zint_decode(&buf, zbf)); + if (buf <= UINT16_MAX) { + *zint = buf; + } else { + ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + return ret; +} +int8_t _z_zint32_decode(uint32_t *zint, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + _z_zint_t buf; + _Z_RETURN_IF_ERR(_z_zint_decode(&buf, zbf)); + if (buf <= UINT32_MAX) { + *zint = buf; + } else { + ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + return ret; +} int8_t _z_zint_decode(_z_zint_t *zint, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; *zint = 0; diff --git a/src/protocol/codec/core.c b/src/protocol/codec/core.c new file mode 100644 index 000000000..403c28d8c --- /dev/null +++ b/src/protocol/codec/core.c @@ -0,0 +1,10 @@ +#include "zenoh-pico/protocol/codec/core.h" + +#include "zenoh-pico/protocol/iobuf.h" +int8_t _z_zbuf_read_exact(_z_zbuf_t *zbf, uint8_t *dest, size_t length) { + if (length > _z_zbuf_len(zbf)) { + return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + _z_zbuf_read_bytes(zbf, dest, 0, length); + return _Z_RES_OK; +} \ No newline at end of file diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index 2a3a783ae..0b6003f52 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -14,12 +14,36 @@ #include "zenoh-pico/protocol/codec/declarations.h" +#include #include #include +#include #include "zenoh-pico/protocol/codec.h" #include "zenoh-pico/protocol/codec/core.h" +#include "zenoh-pico/protocol/codec/ext.h" +#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/definitions/core.h" +#include "zenoh-pico/protocol/definitions/declarations.h" #include "zenoh-pico/protocol/definitions/message.h" +#include "zenoh-pico/protocol/ext.h" +#include "zenoh-pico/protocol/iobuf.h" +#include "zenoh-pico/protocol/msg.h" +#include "zenoh-pico/session/session.h" +#include "zenoh-pico/system/platform.h" + +int8_t _z_decl_ext_keyexpr_encode(_z_wbuf_t *wbf, _z_keyexpr_t ke, _Bool has_next_ext) { + uint8_t header = _Z_MSG_EXT_ENC_ZBUF | 0x0f | (has_next_ext ? _Z_FLAG_Z_Z : 0); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + _Bool has_kesuffix = _z_keyexpr_has_suffix(ke); + header = (ke._uses_remote_mapping ? 2 : 0) | (has_kesuffix ? 1 : 0); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, ke._id)); + if (has_kesuffix) { + _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, (const uint8_t *)ke._suffix, 0, strlen(ke._suffix))) + } + return _Z_RES_OK; +} int8_t _z_decl_kexpr_encode(_z_wbuf_t *wbf, const _z_decl_kexpr_t *decl) { uint8_t header = _Z_DECL_KEXPR_MID; @@ -33,26 +57,26 @@ int8_t _z_decl_kexpr_encode(_z_wbuf_t *wbf, const _z_decl_kexpr_t *decl) { return _Z_RES_OK; } -int8_t _z_undecl_kexpr_encode(_z_wbuf_t *wbf, const _z_undecl_kexpr_t *decl) { - _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_UNDECL_KEXPR)); - return _z_zint_encode(wbf, decl->_id); -} -int8_t _z_decl_subscriber_encode(_z_wbuf_t *wbf, const _z_decl_subscriber_t *decl) { - uint8_t header = _Z_DECL_SUBSCRIBER_MID; - _Bool has_submode_ext = decl->_ext_subscriber_info._pull_mode || decl->_ext_subscriber_info._reliable; - int has_kesuffix = _z_keyexpr_has_suffix(decl->_keyexpr); - if (has_submode_ext) { + +int8_t _z_decl_commons_encode(_z_wbuf_t *wbf, uint8_t header, _Bool has_extensions, uint32_t id, _z_keyexpr_t keyexpr) { + _Bool has_kesuffix = _z_keyexpr_has_suffix(keyexpr); + if (has_extensions) { header |= _Z_FLAG_Z_Z; } if (has_kesuffix) { header |= _Z_DECL_SUBSCRIBER_FLAG_N; } - if (decl->_keyexpr.uses_remote_mapping) { + if (keyexpr._uses_remote_mapping) { header |= _Z_DECL_SUBSCRIBER_FLAG_M; } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); - _Z_RETURN_IF_ERR(_z_zint_encode(wbf, decl->_id)); - _Z_RETURN_IF_ERR(_z_keyexpr_encode(wbf, has_kesuffix, &decl->_keyexpr)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, id)); + return _z_keyexpr_encode(wbf, has_kesuffix, &keyexpr); +} +int8_t _z_decl_subscriber_encode(_z_wbuf_t *wbf, const _z_decl_subscriber_t *decl) { + uint8_t header = _Z_DECL_SUBSCRIBER_MID; + _Bool has_submode_ext = decl->_ext_subscriber_info._pull_mode || decl->_ext_subscriber_info._reliable; + _Z_RETURN_IF_ERR(_z_decl_commons_encode(wbf, header, has_submode_ext, decl->_id, decl->_keyexpr)); if (has_submode_ext) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | 0x01)); _Z_RETURN_IF_ERR(_z_uint8_encode( @@ -61,21 +85,65 @@ int8_t _z_decl_subscriber_encode(_z_wbuf_t *wbf, const _z_decl_subscriber_t *dec return _Z_RES_OK; } -int8_t _z_undecl_subscriber_encode(_z_wbuf_t *wbf, const _z_undecl_subscriber_t *decl) { - uint8_t header = _Z_UNDECL_SUBSCRIBER_MID; - if (_z_keyexpr_check(decl->_ext_keyexpr)) { +int8_t _z_undecl_kexpr_encode(_z_wbuf_t *wbf, const _z_undecl_kexpr_t *decl) { + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_UNDECL_KEXPR)); + return _z_zint_encode(wbf, decl->_id); +} +int8_t _z_undecl_encode(_z_wbuf_t *wbf, uint8_t header, _z_zint_t decl_id, _z_keyexpr_t ke) { + _Bool has_keyexpr_ext = _z_keyexpr_check(ke); + if (has_keyexpr_ext) { header |= _Z_FLAG_Z_Z; } + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, decl_id)); + if (has_keyexpr_ext) { + _Z_RETURN_IF_ERR(_z_decl_ext_keyexpr_encode(wbf, ke, false)); + } + return _Z_RES_OK; +} +int8_t _z_undecl_subscriber_encode(_z_wbuf_t *wbf, const _z_undecl_subscriber_t *decl) { + return _z_undecl_encode(wbf, _Z_UNDECL_SUBSCRIBER_MID, decl->_id, decl->_ext_keyexpr); +} +int8_t _z_decl_queryable_encode(_z_wbuf_t *wbf, const _z_decl_queryable_t *decl) { + uint8_t header = _Z_DECL_QUERYABLE_MID; + _Bool has_info_ext = (decl->_ext_queryable_info._complete != 0) || (decl->_ext_queryable_info._distance != 0); + _Z_RETURN_IF_ERR(_z_decl_commons_encode(wbf, header, has_info_ext, decl->_id, decl->_keyexpr)); + if (has_info_ext) { + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | 0x01)); + _Z_RETURN_IF_ERR( + _z_zint_encode(wbf, (decl->_ext_queryable_info._distance << 8) | decl->_ext_queryable_info._complete)); + } + return _Z_RES_OK; +} +int8_t _z_undecl_queryable_encode(_z_wbuf_t *wbf, const _z_undecl_queryable_t *decl) { + return _z_undecl_encode(wbf, _Z_UNDECL_QUERYABLE_MID, decl->_id, decl->_ext_keyexpr); +} +int8_t _z_decl_token_encode(_z_wbuf_t *wbf, const _z_decl_token_t *decl) { + uint8_t header = _Z_DECL_TOKEN_MID; + _Z_RETURN_IF_ERR(_z_decl_commons_encode(wbf, header, false, decl->_id, decl->_keyexpr)); + return _Z_RES_OK; +} +int8_t _z_undecl_token_encode(_z_wbuf_t *wbf, const _z_undecl_token_t *decl) { + return _z_undecl_encode(wbf, _Z_UNDECL_TOKEN_MID, decl->_id, decl->_ext_keyexpr); +} +int8_t _z_decl_interest_encode(_z_wbuf_t *wbf, const _z_decl_interest_t *decl) { + uint8_t header = _Z_DECL_INTEREST_MID; + _Z_RETURN_IF_ERR(_z_decl_commons_encode(wbf, header, false, decl->_id, decl->_keyexpr)); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, decl->interest_flags)); return _Z_RES_OK; } -int8_t _z_decl_queryable_encode(_z_wbuf_t *wbf, const _z_decl_queryable_t *decl) {} -int8_t _z_undecl_queryable_encode(_z_wbuf_t *wbf, const _z_undecl_queryable_t *decl) {} -int8_t _z_decl_token_encode(_z_wbuf_t *wbf, const _z_decl_token_t *decl) {} -int8_t _z_undecl_token_encode(_z_wbuf_t *wbf, const _z_undecl_token_t *decl) {} -int8_t _z_decl_interest_encode(_z_wbuf_t *wbf, const _z_decl_interest_t *decl) {} -int8_t _z_final_interest_encode(_z_wbuf_t *wbf, const _z_final_interest_t *decl) {} -int8_t _z_undecl_interest_encode(_z_wbuf_t *wbf, const _z_undecl_interest_t *decl) {} + +int8_t _z_final_interest_encode(_z_wbuf_t *wbf, const _z_final_interest_t *decl) { + uint8_t header = _Z_FINAL_INTEREST_MID; + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, decl->_id)); + return _Z_RES_OK; +} + +int8_t _z_undecl_interest_encode(_z_wbuf_t *wbf, const _z_undecl_interest_t *decl) { + return _z_undecl_encode(wbf, _Z_UNDECL_INTEREST_MID, decl->_id, decl->_ext_keyexpr); +} int8_t _z_declaration_encode(_z_wbuf_t *wbf, const _z_declaration_t *decl) { int8_t ret; switch (decl->_tag) { @@ -114,4 +182,212 @@ int8_t _z_declaration_encode(_z_wbuf_t *wbf, const _z_declaration_t *decl) { } break; } return ret; +} +int8_t _z_decl_kexpr_decode(_z_decl_kexpr_t *decl, _z_zbuf_t *zbf, uint8_t header) { + _Z_RETURN_IF_ERR(_z_zint16_decode(&decl->_id, zbf)); + _Z_RETURN_IF_ERR(_z_keyexpr_decode(&decl->_keyexpr, zbf, _Z_HAS_FLAG(header, _Z_DECL_KEXPR_FLAG_N))); + + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + _Z_RETURN_IF_ERR(_z_msg_ext_skip_non_mandatories(zbf, 0x15)); + } + return _Z_RES_OK; +} +int8_t _z_undecl_kexpr_decode(_z_undecl_kexpr_t *decl, _z_zbuf_t *zbf, uint8_t header) { + _Z_RETURN_IF_ERR(_z_zint16_decode(&decl->_id, zbf)); + + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + _Z_RETURN_IF_ERR(_z_msg_ext_skip_non_mandatories(zbf, 0x10)); + } + return _Z_RES_OK; +} + +int8_t _z_undecl_decode_extensions(_z_msg_ext_t *extension, void *ctx) { + _z_keyexpr_t *ke = (_z_keyexpr_t *)ctx; + switch (extension->_header) { + case _Z_MSG_EXT_ENC_ZBUF | 0x0f: { + _z_zbuf_t _zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t *zbf = &_zbf; + uint8_t header; + _Z_RETURN_IF_ERR(_z_uint8_decode(&header, zbf)); + ke->_uses_remote_mapping = !_Z_HAS_FLAG(header, 2); + _Z_RETURN_IF_ERR(_z_zint16_decode(&ke->_id, zbf)); + if (_Z_HAS_FLAG(header, 1)) { + size_t len = _z_zbuf_len(zbf); + ke->_suffix = z_malloc(len + 1); + if (!ke->_suffix) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + ke->is_alloc = true; + _z_zbuf_read_bytes(zbf, (uint8_t *)ke->_suffix, 0, len); + ke->_suffix[len] = 0; + } + } break; + default: + if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { + return _z_msg_ext_unknown_error(extension, 0x0e); + } + } + return _Z_RES_OK; +} +int8_t _z_undecl_trivial_decode(_z_zbuf_t *zbf, _z_keyexpr_t *_ext_keyexpr, uint32_t *decl_id, uint8_t header) { + _Z_RETURN_IF_ERR(_z_zint32_decode(decl_id, zbf)); + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_undecl_decode_extensions, _ext_keyexpr)); + } + return _Z_RES_OK; +} +int8_t _z_decl_commons_decode(_z_zbuf_t *zbf, uint8_t header, _Bool *has_extensions, uint32_t *id, _z_keyexpr_t *ke) { + *has_extensions = _Z_HAS_FLAG(header, _Z_FLAG_Z_Z); + ke->_uses_remote_mapping = _Z_HAS_FLAG(header, _Z_DECL_SUBSCRIBER_FLAG_M); + _Z_RETURN_IF_ERR(_z_zint16_decode(&ke->_id, zbf)); + if (_Z_HAS_FLAG(header, _Z_DECL_SUBSCRIBER_FLAG_N)) { + _z_zint_t len; + _Z_RETURN_IF_ERR(_z_zint_decode(&len, zbf)); + if (_z_zbuf_len(zbf) < len) { + return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + ke->_suffix = z_malloc(len + 1); + if (ke->_suffix == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + ke->is_alloc = true; + _z_zbuf_read_bytes(zbf, (uint8_t *)ke->_suffix, 0, len); + ke->_suffix[len] = 0; + } else { + ke->_suffix = ""; + ke->is_alloc = false; + } + return _Z_RES_OK; +} +int8_t _z_decl_subscriber_decode_extensions(_z_msg_ext_t *extension, void *ctx) { + _z_decl_subscriber_t *decl = (_z_decl_subscriber_t *)ctx; + switch (extension->_header) { + case _Z_MSG_EXT_ENC_ZINT | 0x01: { + decl->_ext_subscriber_info._pull_mode = _Z_HAS_FLAG(extension->_body._zint._val, 2); + decl->_ext_subscriber_info._reliable = _Z_HAS_FLAG(extension->_body._zint._val, 1); + } break; + default: + if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { + return _z_msg_ext_unknown_error(extension, 0x14); + } + } + return _Z_RES_OK; +} + +int8_t _z_decl_subscriber_decode(_z_decl_subscriber_t *decl, _z_zbuf_t *zbf, uint8_t header) { + _Bool has_ext; + _Z_RETURN_IF_ERR(_z_decl_commons_decode(zbf, header, &has_ext, &decl->_id, &decl->_keyexpr)); + if (has_ext) { + _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_decl_subscriber_decode_extensions, decl)); + } + return _Z_RES_OK; +} +int8_t _z_undecl_subscriber_decode(_z_undecl_subscriber_t *decl, _z_zbuf_t *zbf, uint8_t header) {} +int8_t _z_decl_queryable_decode_extensions(_z_msg_ext_t *extension, void *ctx) { + _z_decl_queryable_t *decl = (_z_decl_queryable_t *)ctx; + switch (extension->_header) { + case _Z_MSG_EXT_ENC_ZINT | 0x01: { + decl->_ext_queryable_info._complete = extension->_body._zint._val & 0xff; + decl->_ext_queryable_info._distance = extension->_body._zint._val >> 8; + } break; + default: + if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { + return _z_msg_ext_unknown_error(extension, 0x11); + } + } + return _Z_RES_OK; +} +int8_t _z_decl_queryable_decode(_z_decl_queryable_t *decl, _z_zbuf_t *zbf, uint8_t header) { + _Bool has_ext; + _Z_RETURN_IF_ERR(_z_decl_commons_decode(zbf, header, &has_ext, &decl->_id, &decl->_keyexpr)); + if (has_ext) { + _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_decl_queryable_decode_extensions, decl)); + } + return _Z_RES_OK; +} +int8_t _z_undecl_queryable_decode(_z_undecl_queryable_t *decl, _z_zbuf_t *zbf, uint8_t header) { + return _z_undecl_trivial_decode(zbf, &decl->_ext_keyexpr, &decl->_id, header); +} +int8_t _z_decl_token_decode(_z_decl_token_t *decl, _z_zbuf_t *zbf, uint8_t header) { + _Bool has_ext; + _Z_RETURN_IF_ERR(_z_decl_commons_decode(zbf, header, &has_ext, &decl->_id, &decl->_keyexpr)); + if (has_ext) { + _Z_RETURN_IF_ERR(_z_msg_ext_skip_non_mandatories(zbf, 0x12)); + } + return _Z_RES_OK; +} +int8_t _z_undecl_token_decode(_z_undecl_token_t *decl, _z_zbuf_t *zbf, uint8_t header) { + return _z_undecl_trivial_decode(zbf, &decl->_ext_keyexpr, &decl->_id, header); +} +int8_t _z_decl_interest_decode(_z_decl_interest_t *decl, _z_zbuf_t *zbf, uint8_t header) { + _Bool has_ext; + _Z_RETURN_IF_ERR(_z_decl_commons_decode(zbf, header, &has_ext, &decl->_id, &decl->_keyexpr)); + _Z_RETURN_IF_ERR(_z_uint8_decode(&decl->interest_flags, zbf)); + if (has_ext) { + _Z_RETURN_IF_ERR(_z_msg_ext_skip_non_mandatories(zbf, 0x13)); + } + return _Z_RES_OK; +} +int8_t _z_final_interest_decode(_z_final_interest_t *decl, _z_zbuf_t *zbf, uint8_t header) { + _Z_RETURN_IF_ERR(_z_zint32_decode(&decl->_id, zbf)); + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + _Z_RETURN_IF_ERR(_z_msg_ext_skip_non_mandatories(zbf, 0x10)); + } + return _Z_RES_OK; +} +int8_t _z_undecl_interest_decode(_z_undecl_interest_t *decl, _z_zbuf_t *zbf, uint8_t header) { + return _z_undecl_trivial_decode(zbf, &decl->_ext_keyexpr, &decl->_id, header); +} +int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf, uint8_t header) { + int8_t ret; + switch (_Z_MID(header)) { + case _Z_DECL_KEXPR_MID: { + decl->_tag = _Z_DECL_KEXPR; + ret = _z_decl_kexpr_decode(&decl->_body._decl_kexpr, zbf, header); + } break; + case _Z_UNDECL_KEXPR_MID: { + decl->_tag = _Z_UNDECL_KEXPR; + ret = _z_undecl_kexpr_decode(&decl->_body._undecl_kexpr, zbf, header); + } break; + case _Z_DECL_SUBSCRIBER_MID: { + decl->_tag = _Z_DECL_SUBSCRIBER; + ret = _z_decl_subscriber_decode(&decl->_body._decl_subscriber, zbf, header); + } break; + case _Z_UNDECL_SUBSCRIBER_MID: { + decl->_tag = _Z_UNDECL_SUBSCRIBER; + ret = _z_undecl_subscriber_decode(&decl->_body._undecl_subscriber, zbf, header); + } break; + case _Z_DECL_QUERYABLE_MID: { + decl->_tag = _Z_DECL_QUERYABLE; + ret = _z_decl_queryable_decode(&decl->_body._decl_queryable, zbf, header); + } break; + case _Z_UNDECL_QUERYABLE_MID: { + decl->_tag = _Z_UNDECL_QUERYABLE; + ret = _z_undecl_queryable_decode(&decl->_body._undecl_queryable, zbf, header); + } break; + case _Z_DECL_TOKEN_MID: { + decl->_tag = _Z_DECL_TOKEN; + ret = _z_decl_token_decode(&decl->_body._decl_token, zbf, header); + } break; + case _Z_UNDECL_TOKEN_MID: { + decl->_tag = _Z_UNDECL_TOKEN; + ret = _z_undecl_token_decode(&decl->_body._undecl_token, zbf, header); + } break; + case _Z_DECL_INTEREST_MID: { + decl->_tag = _Z_DECL_INTEREST; + ret = _z_decl_interest_decode(&decl->_body._decl_interest, zbf, header); + } break; + case _Z_FINAL_INTEREST_MID: { + decl->_tag = _Z_FINAL_INTEREST; + ret = _z_final_interest_decode(&decl->_body._final_interest, zbf, header); + } break; + case _Z_UNDECL_INTEREST_MID: { + decl->_tag = _Z_UNDECL_INTEREST; + ret = _z_undecl_interest_decode(&decl->_body._undecl_interest, zbf, header); + } break; + default: { + ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + } + return ret; } \ No newline at end of file diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 5624dd3ce..dd8fccdff 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -12,7 +12,7 @@ // ZettaScale Zenoh Team, // -#include "zenoh-pico/protocol/msgcodec.h" +#include "zenoh-pico/protocol/definitions/message.h" #include #include @@ -29,6 +29,7 @@ #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/keyexpr.h" +#include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" @@ -348,21 +349,23 @@ int8_t _z_del_decode(_z_msg_del_t *del, _z_zbuf_t *zbf, uint8_t header) { } /*------------------ Query Message ------------------*/ -int8_t _z_query_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_query_t *msg) { +int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *msg) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_Z_QUERY\n"); + uint8_t header = _Z_MID_Z_QUERY; - if (z_bytes_check(&msg->_parameters)) { - assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_P)); - ret = _z_bytes_encode(wbf, &msg->_parameters); - } else { - assert(!_Z_HAS_FLAG(header, _Z_FLAG_Z_P)); + _Bool has_params = z_bytes_check(&msg->_parameters); + if (has_params) { + header |= _Z_FLAG_Z_P; } - _z_msg_query_reqexts_t required_exts = _z_msg_query_required_extensions(msg); if (required_exts.body || required_exts.consolidation || required_exts.info) { - assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); + header |= _Z_FLAG_Z_Z; + } + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + if (has_params) { + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_parameters)); } + if ((ret == _Z_RES_OK) && required_exts.body) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x03; if (required_exts.consolidation || required_exts.info) { diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index 5061ee2c3..e31df9f46 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -20,8 +20,10 @@ #include "zenoh-pico/api/types.h" #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/protocol/codec.h" +#include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/protocol/codec/ext.h" #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/definitions/core.h" #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/msg.h" @@ -92,25 +94,6 @@ int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { return ret; } -/*------------------ Request Body Field ------------------*/ -int8_t _z_request_body_encode(_z_wbuf_t *wbf, const _z_request_body_t *reqb) { - (void)(wbf); - (void)(reqb); - int8_t ret = _Z_RES_OK; - - return ret; -} - -int8_t _z_request_body_decode_na(_z_request_body_t *reqb, _z_zbuf_t *zbf) { - (void)(zbf); - (void)(reqb); - int8_t ret = _Z_RES_OK; - - return ret; -} - -int8_t _z_request_body_decode(_z_request_body_t *reqb, _z_zbuf_t *zbf) { return _z_request_body_decode_na(reqb, zbf); } - /*------------------ Request Message ------------------*/ int8_t _z_request_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_request_t *msg) { int8_t ret = _Z_RES_OK; @@ -118,24 +101,25 @@ int8_t _z_request_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_request_ _Z_EC(_z_zint_encode(wbf, msg->_rid)); _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N), &msg->_key)); - _Z_EC(_z_request_body_encode(wbf, &msg->_body)); - - return ret; -} - -int8_t _z_request_decode_na(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_N_REQUEST\n"); - int8_t ret = _Z_RES_OK; - - ret |= _z_zint_decode(&msg->_rid, zbf); - ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N)); - ret |= _z_request_body_decode(&msg->_body, zbf); + switch (msg->_tag) { + case _Z_REQUEST_QUERY: { + _Z_RETURN_IF_ERR(_z_query_encode(wbf, &msg->_body.query)); + } break; + case _Z_REQUEST_PUT: { + } break; + case _Z_REQUEST_DEL: { + } break; + case _Z_REQUEST_PULL: { + } break; + } return ret; } int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_request_decode_na(msg, zbf, header); + _Z_RETURN_IF_ERR(_z_zint_decode(&msg->_rid, zbf)); + _Z_RETURN_IF_ERR(_z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N))); + return _Z_RES_OK; } /*------------------ Response Message ------------------*/ @@ -153,10 +137,11 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_respons } _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); if (has_qos_ext) { assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); uint8_t extheader = _Z_MSG_EXT_ENC_ZINT | 0x01; - if (has_ts_ext) { + if (has_ts_ext || has_responder_ext) { extheader |= _Z_FLAG_Z_Z; } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); @@ -164,10 +149,20 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_respons } if (has_ts_ext) { assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); - uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x02; + uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x02 | has_responder_ext; _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); _Z_RETURN_IF_ERR(_z_timestamp_encode(wbf, &msg->_ext_timestamp)); } + if (has_responder_ext) { + assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); + uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x02 | has_responder_ext; + _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_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_zint_encode(wbf, msg->_ext_responder._eid)); + } switch (msg->_body_tag) { case _Z_RESPONSE_BODY_REPLY: { @@ -207,6 +202,16 @@ int8_t _z_response_decode_extension(_z_msg_ext_t *extension, void *ctx) { ret = _z_timestamp_decode(&msg->_ext_timestamp, &zbf); break; } + case _Z_MSG_EXT_ENC_ZBUF | 0x03: { + _z_zbuf_t _zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _z_zbuf_t *zbf = &_zbf; + uint8_t header; + _Z_RETURN_IF_ERR(_z_uint8_decode(&header, zbf)); + uint8_t zidlen = (header >> 4) + 1; + _Z_RETURN_IF_ERR(_z_zbuf_read_exact(zbf, msg->_ext_responder._zid.id, zidlen)); + _Z_RETURN_IF_ERR(_z_zint32_decode(&msg->_ext_responder._eid, zbf)); + break; + } default: if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { ret = _z_msg_ext_unknown_error(extension, 0x0d); @@ -220,10 +225,7 @@ int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t head int8_t ret = _Z_RES_OK; msg->_uses_sender_mapping = _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_M); _Z_EC(_z_zint_decode(&msg->_request_id, zbf)); - _Z_EC(_z_zint_decode(&msg->_key._id, zbf)); - if (_Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N)) { - _Z_EC(_z_str_decode((char **)&msg->_key._suffix, zbf)) - } + _Z_RETURN_IF_ERR(_z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N))); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { _Z_EC(_z_msg_ext_decode_iter(zbf, _z_response_decode_extension, msg)); } @@ -282,52 +284,3 @@ int8_t _z_response_final_decode_na(_z_n_msg_response_final_t *msg, _z_zbuf_t *zb int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { return _z_response_final_decode_na(msg, zbf, header); } - -/*------------------ Network Message ------------------*/ -int8_t _z_network_message_encode(_z_wbuf_t *wbf, const _z_network_message_t *msg) { - int8_t ret = _Z_RES_OK; - - uint8_t header = msg->_header; - if (_z_msg_ext_vec_is_empty(&msg->_extensions) == false) { - header |= _Z_FLAG_N_Z; - } - - _Z_EC(_z_wbuf_write(wbf, header)) - switch (_Z_MID(msg->_header)) { - case _Z_MID_N_DECLARE: { - ret |= _z_declare_encode(wbf, &msg->_body._declare); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to encode network message with unknown ID(%d)\n", _Z_MID(msg->_header)); - ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; - } break; - } - - return ret; -} - -int8_t _z_network_message_decode_na(_z_network_message_t *msg, _z_zbuf_t *zbf) { - int8_t ret = _Z_RES_OK; - - ret |= _z_uint8_decode(&msg->_header, zbf); // Decode the header - if (ret == _Z_RES_OK) { - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_N_DECLARE: { - ret |= _z_declare_decode(&msg->_body._declare, zbf); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(%d)\n", mid); - ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; - } break; - } - } - - return ret; -} - -int8_t _z_network_message_decode(_z_network_message_t *n_msg, _z_zbuf_t *zbf) { - return _z_network_message_decode_na(n_msg, zbf); -} \ No newline at end of file From 345ce4fca6b6555e39f23ffc656d60a888fb1bcf Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 30 Jun 2023 15:10:45 +0200 Subject: [PATCH 107/181] More reorg + new constructors for declarations Co-authored-by: Steven Palma --- include/zenoh-pico/protocol/codec/message.h | 7 +- include/zenoh-pico/protocol/core.h | 3 + .../zenoh-pico/protocol/definitions/core.h | 2 + .../protocol/definitions/declarations.h | 18 + .../zenoh-pico/protocol/definitions/message.h | 4 +- .../zenoh-pico/protocol/definitions/network.h | 19 +- include/zenoh-pico/protocol/keyexpr.h | 7 +- include/zenoh-pico/protocol/msg.h | 57 +- src/protocol/codec/message.c | 118 +---- src/protocol/codec/network.c | 89 +++- src/protocol/definitions/core.c | 0 src/protocol/definitions/declarations.c | 53 +- src/protocol/definitions/message.c | 22 + src/protocol/definitions/network.c | 123 +++++ src/protocol/definitions/transport.c | 399 ++++++++++++++ src/protocol/msg.c | 497 +----------------- src/session/utils.c | 10 + tests/z_msgcodec_test.c | 8 +- 18 files changed, 779 insertions(+), 657 deletions(-) create mode 100644 src/protocol/definitions/core.c create mode 100644 src/protocol/definitions/message.c create mode 100644 src/protocol/definitions/network.c create mode 100644 src/protocol/definitions/transport.c diff --git a/include/zenoh-pico/protocol/codec/message.h b/include/zenoh-pico/protocol/codec/message.h index b95c93950..aa292c7e6 100644 --- a/include/zenoh-pico/protocol/codec/message.h +++ b/include/zenoh-pico/protocol/codec/message.h @@ -21,8 +21,11 @@ int8_t _z_push_body_encode(_z_wbuf_t *buf, const _z_push_body_t *ts); int8_t _z_push_body_decode(_z_push_body_t *ts, _z_zbuf_t *buf, uint8_t header); -int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *reply); -int8_t _z_query_decode(_z_msg_query_t *reply, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *query); +int8_t _z_query_decode(_z_msg_query_t *query, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_pull_encode(_z_wbuf_t *wbf, const _z_msg_pull_t *pull); +int8_t _z_pull_decode(_z_msg_pull_t *pull, _z_zbuf_t *zbf, uint8_t header); int8_t _z_reply_encode(_z_wbuf_t *wbf, const _z_msg_reply_t *reply); int8_t _z_reply_decode(_z_msg_reply_t *reply, _z_zbuf_t *zbf, uint8_t header); diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 15d7f5368..16aaa8c8c 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -24,6 +24,9 @@ #include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" +#define _Z_OPTIONAL +#define _Z_MOVE(x) x * + /** * The reserved resource ID indicating a string-only resource key. */ diff --git a/include/zenoh-pico/protocol/definitions/core.h b/include/zenoh-pico/protocol/definitions/core.h index accbfc0d3..5c97d1427 100644 --- a/include/zenoh-pico/protocol/definitions/core.h +++ b/include/zenoh-pico/protocol/definitions/core.h @@ -15,6 +15,8 @@ #ifndef INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_CORE_H #define INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_CORE_H +#include "zenoh-pico/protocol/core.h" + #define _Z_DEFAULT_BATCH_SIZE 65535 #define _Z_DEFAULT_RESOLUTION_SIZE 2 diff --git a/include/zenoh-pico/protocol/definitions/declarations.h b/include/zenoh-pico/protocol/definitions/declarations.h index bda82f0eb..a08f49a24 100644 --- a/include/zenoh-pico/protocol/definitions/declarations.h +++ b/include/zenoh-pico/protocol/definitions/declarations.h @@ -18,6 +18,7 @@ #include #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/keyexpr.h" typedef struct { uint16_t _id; @@ -112,4 +113,21 @@ typedef struct { } _z_declaration_t; void _z_declaration_clear(_z_declaration_t* decl); +_z_declaration_t _z_make_decl_keyexpr(uint16_t id, _Z_MOVE(_z_keyexpr_t) key); +_z_declaration_t _z_make_undecl_keyexpr(uint16_t id); + +_z_declaration_t _z_make_decl_subscriber(_Z_MOVE(_z_keyexpr_t) key, uint32_t id, _Bool reliable, _Bool pull_mode); +_z_declaration_t _z_make_undecl_subscriber(uint32_t id, _Z_OPTIONAL _Z_MOVE(_z_keyexpr_t) key); + +_z_declaration_t _z_make_decl_queryable(_Z_MOVE(_z_keyexpr_t) key, uint32_t id, uint32_t distance, uint8_t complete); +_z_declaration_t _z_make_undecl_queryable(uint32_t id, _Z_OPTIONAL _Z_MOVE(_z_keyexpr_t) key); + +_z_declaration_t _z_make_decl_token(_Z_MOVE(_z_keyexpr_t) key, uint32_t id); +_z_declaration_t _z_make_undecl_token(uint32_t id, _Z_OPTIONAL _Z_MOVE(_z_keyexpr_t) key); +_z_declaration_t _z_make_decl_interest(_Z_MOVE(_z_keyexpr_t) key, uint32_t id); +_z_declaration_t _z_make_undecl_interest(uint32_t id, _Z_OPTIONAL _Z_MOVE(_z_keyexpr_t) key); +_z_declaration_t _z_make_final_decl(uint32_t id) { + return (_z_declaration_t){._tag = _Z_FINAL_INTEREST, ._body = {._final_interest = {._id = id}}}; +} + #endif /* INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_DECLARATIONS_H */ diff --git a/include/zenoh-pico/protocol/definitions/message.h b/include/zenoh-pico/protocol/definitions/message.h index 30c9c713d..aa369fdb6 100644 --- a/include/zenoh-pico/protocol/definitions/message.h +++ b/include/zenoh-pico/protocol/definitions/message.h @@ -131,6 +131,7 @@ typedef struct { typedef struct { _z_source_info_t _ext_source_info; } _z_msg_pull_t; +static inline void _z_msg_pull_clear(_z_msg_pull_t *pull) {} typedef struct { _z_timestamp_t _timestamp; @@ -140,6 +141,7 @@ typedef struct { typedef struct { _z_m_push_commons_t _commons; } _z_msg_del_t; +static inline void _z_msg_del_clear(_z_msg_del_t *del) {} #define _Z_M_DEL_ID 0x02 #define _Z_FLAG_Z_D_T 0x20 @@ -177,6 +179,6 @@ typedef struct { _Bool consolidation; } _z_msg_query_reqexts_t; _z_msg_query_reqexts_t _z_msg_query_required_extensions(const _z_msg_query_t *msg); -void _z_msg_clear_query(_z_msg_query_t *msg); +void _z_msg_query_clear(_z_msg_query_t *msg); #endif /* INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_MESSAGE_H */ diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index f6316a9de..7e84cc895 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -92,9 +92,8 @@ typedef struct { typedef struct { _z_zint_t _rid; _z_keyexpr_t _key; - _z_n_qos_t ext_qos; _z_timestamp_t ext_tstamp; - _z_id_t ext_nodeid; + _z_n_qos_t ext_qos; z_query_target_t ext_target; uint32_t ext_budget; uint32_t ext_timeout_ms; @@ -106,7 +105,16 @@ typedef struct { _z_msg_pull_t pull; } _body; } _z_n_msg_request_t; -void _z_n_msg_clear_request(_z_n_msg_request_t *msg); +typedef struct { + _Bool ext_qos; + _Bool ext_tstamp; + _Bool ext_target; + _Bool ext_budget; + _Bool ext_timeout_ms; + uint8_t n; +} _z_n_msg_request_exts_t; +_z_n_msg_request_exts_t _z_n_msg_request_needed_exts(const _z_n_msg_request_t *msg); +void _z_n_msg_request_clear(_z_n_msg_request_t *msg); typedef struct { _Bool _is_put; @@ -133,7 +141,7 @@ void _z_push_body_clear(_z_push_body_t *msg); typedef struct { _z_zint_t _request_id; } _z_n_msg_response_final_t; -void _z_n_msg_clear_response_final(_z_n_msg_response_final_t *msg); +void _z_n_msg_response_final_clear(_z_n_msg_response_final_t *msg); // Flags: // - N: Named if N==1 then the keyexpr has name/suffix @@ -159,7 +167,7 @@ typedef struct { _z_n_qos_t _qos; _z_push_body_t _body; } _z_n_msg_push_t; -void _z_n_msg_clear_push(_z_n_msg_push_t *msg); +void _z_n_msg_push_clear(_z_n_msg_push_t *msg); /*------------------ Response Message ------------------*/ typedef struct { @@ -195,6 +203,7 @@ typedef struct { uint16_t _ext_nodeid; _z_n_qos_t _ext_qos; } _z_n_msg_declare_t; +static inline void _z_n_msg_declare_clear(_z_n_msg_declare_t *msg) { _z_declaration_clear(&msg->_decl); } /*------------------ Zenoh Message ------------------*/ typedef union { diff --git a/include/zenoh-pico/protocol/keyexpr.h b/include/zenoh-pico/protocol/keyexpr.h index 288056711..3c03e0429 100644 --- a/include/zenoh-pico/protocol/keyexpr.h +++ b/include/zenoh-pico/protocol/keyexpr.h @@ -11,8 +11,8 @@ // Contributors: // ZettaScale Zenoh Team, -#ifndef ZENOH_PICO_PROTOCOL_KEYEXPR_H -#define ZENOH_PICO_PROTOCOL_KEYEXPR_H +#ifndef INCLUDE_ZENOH_PICO_PROTOCOL_KEYEXPR_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_KEYEXPR_H #include @@ -27,9 +27,10 @@ _Bool _z_keyexpr_intersects(const char *lstart, const size_t llen, const char *r /*------------------ clone/Copy/Free helpers ------------------*/ void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src); _z_keyexpr_t _z_keyexpr_duplicate(const _z_keyexpr_t *src); +_z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src); _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp); void _z_timestamp_reset(_z_timestamp_t *tstamp); void _z_keyexpr_clear(_z_keyexpr_t *rk); void _z_keyexpr_free(_z_keyexpr_t **rk); -#endif /* ZENOH_PICO_PROTOCOL_KEYEXPR_H */ +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_KEYEXPR_H */ diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index a09b94e2b..a5a629f72 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -27,8 +27,10 @@ #include "zenoh-pico/link/endpoint.h" #include "zenoh-pico/net/query.h" #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/definitions/network.h" #include "zenoh-pico/protocol/definitions/transport.h" #include "zenoh-pico/protocol/ext.h" +#include "zenoh-pico/protocol/keyexpr.h" /*------------------ Locators Field ------------------*/ // 7 6 5 4 3 2 1 0 @@ -204,42 +206,6 @@ void _z_declaration_clear_forget_queryable(_z_forget_qle_decl_t *dcl); // void _z_timestamp_clear(_z_timestamp_t *ts); -/*------------------ Unit Message ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |X|X|D| UNIT | -// +-+-+-+---------+ -// -// - if D==1 then the message can be dropped for congestion control reasons. -// -typedef struct { - uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior -} _z_msg_unit_t; -void _z_msg_clear_unit(_z_msg_unit_t *unt); - -/*------------------ Pull Message ------------------*/ - -/*------------------ Zenoh Message ------------------*/ -typedef union { - _z_msg_query_t _query; - _z_msg_unit_t _unit; -} _z_zenoh_body_t; - -// TODO[remove] -typedef struct { - _z_id_t _replier_id; - _z_zint_t _qid; - uint8_t _header; -} _z_reply_context_t; -_z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, _Bool is_final); -// TODO[remove end] - -/*=============================*/ -/* Network Messages */ -/*=============================*/ - -/*------------------ Push Message ------------------*/ - /*------------------ Builders ------------------*/ _z_network_message_t _z_n_msg_make_declare(_z_declaration_t declarations); _z_network_message_t _z_n_msg_make_push(_z_keyexpr_t key, _z_push_body_t body, _Bool is_remote_mapping); @@ -249,17 +215,6 @@ _z_network_message_t _z_n_msg_make_push(_z_keyexpr_t key, _z_push_body_t body, _ // _Bool is_remote_mapping); _z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid); -typedef struct { - union { - _z_n_msg_response_t _response; - _z_n_msg_response_final_t _final; - } _body; -} _z_zenoh_message_t; -void _z_msg_clear(_z_zenoh_message_t *m); -void _z_msg_free(_z_zenoh_message_t **m); -_Z_ELEM_DEFINE(_z_zenoh_message, _z_zenoh_message_t, _z_noop_size, _z_msg_clear, _z_noop_copy) -_Z_VEC_DEFINE(_z_zenoh_message, _z_zenoh_message_t) - /*------------------ Builders ------------------*/ _z_declaration_t _z_msg_make_declaration_resource(_z_zint_t id, _z_keyexpr_t key); _z_declaration_t _z_msg_make_declaration_forget_resource(_z_zint_t rid); @@ -270,10 +225,10 @@ _z_declaration_t _z_msg_make_declaration_forget_subscriber(_z_keyexpr_t key); _z_declaration_t _z_msg_make_declaration_queryable(_z_keyexpr_t key, _z_zint_t complete, _z_zint_t distance); _z_declaration_t _z_msg_make_declaration_forget_queryable(_z_keyexpr_t key); // _z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool can_be_dropped); -_z_zenoh_message_t _z_msg_make_unit(_Bool can_be_dropped); -_z_zenoh_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id, _z_zint_t max_samples, _Bool is_final); -_z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, _z_bytes_t parameters, _z_zint_t qid, - z_consolidation_mode_t consolidation, _z_value_t value); +_z_network_message_t _z_msg_make_unit(_Bool can_be_dropped); +_z_network_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id, _z_zint_t max_samples, _Bool is_final); +_z_network_message_t _z_msg_make_query(_z_keyexpr_t key, _z_bytes_t parameters, _z_zint_t qid, + z_consolidation_mode_t consolidation, _z_value_t value); // _z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool // can_be_dropped); diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index dd8fccdff..57f48e378 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -24,6 +24,7 @@ #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/link/endpoint.h" #include "zenoh-pico/protocol/codec.h" +#include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/protocol/codec/ext.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/ext.h" @@ -362,31 +363,32 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *msg) { header |= _Z_FLAG_Z_Z; } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + if (has_params) { _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_parameters)); } - if ((ret == _Z_RES_OK) && required_exts.body) { + if (required_exts.body) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x03; if (required_exts.consolidation || required_exts.info) { extheader |= _Z_FLAG_Z_Z; } - ret = _z_uint8_encode(wbf, extheader); - ret |= _z_encoding_prefix_encode(wbf, msg->_value.encoding.prefix); - ret |= _z_bytes_encode(wbf, &msg->_value.encoding.suffix); - ret |= _z_bytes_encode(wbf, &msg->_value.payload); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); + _Z_RETURN_IF_ERR(_z_encoding_prefix_encode(wbf, msg->_value.encoding.prefix)); + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_value.encoding.suffix)); + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_value.payload)); } - if ((ret == _Z_RES_OK) && required_exts.consolidation) { + if (required_exts.consolidation) { uint8_t extheader = _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M | 0x02; if (required_exts.info) { extheader |= _Z_FLAG_Z_Z; } - ret = _z_uint8_encode(wbf, extheader); - ret |= _z_zint_encode(wbf, msg->_consolidation); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_consolidation)); } - if ((ret == _Z_RES_OK) && required_exts.info) { + if (required_exts.info) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01; - ret = _z_source_info_encode(wbf, &msg->_info); + _Z_RETURN_IF_ERR(_z_source_info_encode(wbf, &msg->_info)); } return ret; @@ -641,11 +643,17 @@ int8_t _z_ack_decode(_z_msg_ack_t *ack, _z_zbuf_t *zbf, uint8_t header) { return ret; } -int8_t _z_pull_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_pull_t *pull) { +int8_t _z_pull_encode(_z_wbuf_t *wbf, const _z_msg_pull_t *pull) { int8_t ret = _Z_RES_OK; - if ((ret == _Z_RES_OK) && _z_id_check(pull->_ext_source_info._id)) { - ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x01); - ret |= _z_source_info_encode(wbf, &pull->_ext_source_info); + uint8_t header = _Z_MID_Z_PULL; + _Bool has_info_ext = _z_id_check(pull->_ext_source_info._id); + if (has_info_ext) { + header |= _Z_FLAG_Z_Z; + } + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + if (has_info_ext) { + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x01)); + _Z_RETURN_IF_ERR(_z_source_info_encode(wbf, &pull->_ext_source_info)); } return ret; } @@ -673,88 +681,6 @@ int8_t _z_pull_decode(_z_msg_pull_t *pull, _z_zbuf_t *zbf, uint8_t header) { return ret; } -/*------------------ Zenoh Message ------------------*/ -int8_t _z_zenoh_message_encode(_z_wbuf_t *wbf, const _z_zenoh_message_t *msg) { - int8_t ret = _Z_RES_OK; - - _Z_EC(_z_wbuf_write(wbf, msg->_header)) - - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_Z_DATA: { - ret |= _z_data_encode(wbf, msg->_header, &msg->_body._data); - } break; - - case _Z_MID_Z_QUERY: { - ret |= _z_query_encode(wbf, msg->_header, &msg->_body._query); - } break; - - case _Z_MID_Z_PULL: { - ret |= _z_pull_encode(wbf, msg->_header, &msg->_body._pull); - } break; - - case _Z_MID_Z_UNIT: { - // Do nothing. Unit messages have no body - } break; - - default: { - _Z_DEBUG("WARNING: Trying to encode message with unknown ID(%d)\n", mid); - ret |= _Z_ERR_MESSAGE_ZENOH_UNKNOWN; - } break; - } - - return ret; -} - -int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { - int8_t ret = _Z_RES_OK; - - _Bool is_last = false; - do { - ret |= _z_uint8_decode(&msg->_header, zbf); - if (ret == _Z_RES_OK) { - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_Z_DATA: { - ret |= _z_data_decode(&msg->_body._data, zbf, msg->_header); - is_last = true; - } break; - - case _Z_MID_Z_QUERY: { - ret |= _z_query_decode(&msg->_body._query, zbf, msg->_header); - is_last = true; - } break; - - case _Z_MID_Z_PULL: { - ret |= _z_pull_decode(&msg->_body._pull, zbf, msg->_header); - is_last = true; - } break; - - case _Z_MID_Z_UNIT: { - // Do nothing. Unit messages have no body. - is_last = true; - } break; - - case _Z_MID_Z_LINK_STATE_LIST: { - _Z_DEBUG("WARNING: Link state not supported in zenoh-pico\n"); - is_last = true; - } break; - - default: { - _Z_DEBUG("WARNING: Trying to decode zenoh message with unknown ID(%d)\n", mid); - ret |= _Z_ERR_MESSAGE_ZENOH_UNKNOWN; - } break; - } - } else { - msg->_header = 0xFF; - } - } while ((ret == _Z_RES_OK) && (is_last == false)); - - return ret; -} - -int8_t _z_zenoh_message_decode(_z_zenoh_message_t *msg, _z_zbuf_t *zbf) { return _z_zenoh_message_decode_na(msg, zbf); } - /*=============================*/ /* Scouting Messages */ /*=============================*/ diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index e31df9f46..1bd9a94ee 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -12,6 +12,8 @@ // ZettaScale Zenoh Team, // +#include "zenoh-pico/protocol/definitions/network.h" + #include #include #include @@ -24,6 +26,7 @@ #include "zenoh-pico/protocol/codec/ext.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/definitions/core.h" +#include "zenoh-pico/protocol/definitions/message.h" #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/msg.h" @@ -102,23 +105,107 @@ int8_t _z_request_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_request_ _Z_EC(_z_zint_encode(wbf, msg->_rid)); _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N), &msg->_key)); + _z_n_msg_request_exts_t exts = _z_n_msg_request_needed_exts(msg); + if (exts.ext_qos) { + exts.n -= 1; + uint8_t extheader = 0x01 | _Z_MSG_EXT_ENC_ZINT | (exts.n ? _Z_FLAG_Z_Z : 0); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->ext_qos._val)); + } + if (exts.ext_tstamp) { + exts.n -= 1; + uint8_t extheader = 0x02 | _Z_MSG_EXT_ENC_ZBUF | (exts.n ? _Z_FLAG_Z_Z : 0); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); + _Z_RETURN_IF_ERR(_z_timestamp_encode(wbf, &msg->ext_tstamp)); + } + if (exts.ext_target) { + exts.n -= 1; + uint8_t extheader = 0x04 | _Z_MSG_EXT_ENC_ZINT | (exts.n ? _Z_FLAG_Z_Z : 0) | _Z_MSG_EXT_FLAG_M; + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->ext_target)); + } + if (exts.ext_budget) { + exts.n -= 1; + uint8_t extheader = 0x05 | _Z_MSG_EXT_ENC_ZINT | (exts.n ? _Z_FLAG_Z_Z : 0); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->ext_budget)); + } + if (exts.ext_timeout_ms) { + exts.n -= 1; + uint8_t extheader = 0x06 | _Z_MSG_EXT_ENC_ZINT | (exts.n ? _Z_FLAG_Z_Z : 0); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->ext_timeout_ms)); + } + switch (msg->_tag) { case _Z_REQUEST_QUERY: { _Z_RETURN_IF_ERR(_z_query_encode(wbf, &msg->_body.query)); } break; case _Z_REQUEST_PUT: { + _Z_RETURN_IF_ERR(_z_put_encode(wbf, &msg->_body.put)); } break; case _Z_REQUEST_DEL: { + _Z_RETURN_IF_ERR(_z_del_encode(wbf, &msg->_body.del)); } break; case _Z_REQUEST_PULL: { + _Z_RETURN_IF_ERR(_z_pull_encode(wbf, &msg->_body.pull)); } break; } return ret; } - +int8_t _z_request_decode_extensions(_z_msg_ext_t *extension, void *ctx) { + _z_n_msg_request_t *msg = (_z_n_msg_request_t *)ctx; + switch (_Z_EXT_FULL_ID(extension->_header)) { + case 0x01 | _Z_MSG_EXT_ENC_ZINT: { // QOS ext + msg->ext_qos = (_z_n_qos_t){._val = extension->_body._zint._val}; + break; + } + case 0x02 | _Z_MSG_EXT_ENC_ZBUF: { // Timestamp ext + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + _Z_RETURN_IF_ERR(_z_timestamp_decode(&msg->ext_tstamp, &zbf)); + break; + } + case 0x04 | _Z_MSG_EXT_ENC_ZINT: { + msg->ext_target = extension->_body._zint._val; + if (msg->ext_target > 2) { + return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + } break; + case 0x05 | _Z_MSG_EXT_ENC_ZINT: { + msg->ext_budget = extension->_body._zint._val; + } break; + case 0x06 | _Z_MSG_EXT_ENC_ZINT: { + msg->ext_timeout_ms = extension->_body._zint._val; + } break; + default: + if ((extension->_header & _Z_MSG_EXT_FLAG_M) != 0) { + return _z_msg_ext_unknown_error(extension, 0x16); + } + } + return _Z_RES_OK; +} int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_RETURN_IF_ERR(_z_zint_decode(&msg->_rid, zbf)); _Z_RETURN_IF_ERR(_z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N))); + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_request_decode_extensions, msg)); + } + switch (_Z_MID(header)) { + case _Z_MID_Z_QUERY: { + _Z_RETURN_IF_ERR(_z_query_decode(&msg->_body.query, zbf, header)); + } break; + case _Z_MID_Z_PUT: { + _Z_RETURN_IF_ERR(_z_put_decode(&msg->_body.put, zbf, header)); + } break; + case _Z_MID_Z_DEL: { + _Z_RETURN_IF_ERR(_z_del_decode(&msg->_body.del, zbf, header)); + } break; + case _Z_MID_Z_PULL: { + _Z_RETURN_IF_ERR(_z_pull_decode(&msg->_body.pull, zbf, header)); + } break; + default: + return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } return _Z_RES_OK; } diff --git a/src/protocol/definitions/core.c b/src/protocol/definitions/core.c new file mode 100644 index 000000000..e69de29bb diff --git a/src/protocol/definitions/declarations.c b/src/protocol/definitions/declarations.c index 094bef4fc..10ff8addf 100644 --- a/src/protocol/definitions/declarations.c +++ b/src/protocol/definitions/declarations.c @@ -61,4 +61,55 @@ void _z_declaration_clear(_z_declaration_t* decl) { break; } } -} \ No newline at end of file +} +_z_declaration_t _z_make_decl_keyexpr(uint16_t id, _Z_MOVE(_z_keyexpr_t) key) { + return (_z_declaration_t){._tag = _Z_DECL_KEXPR, + ._body = {._decl_kexpr = {._id = id, ._keyexpr = _z_keyexpr_steal(key)}}}; +} +_z_declaration_t _z_make_undecl_keyexpr(uint16_t id) { + return (_z_declaration_t){._tag = _Z_UNDECL_KEXPR, ._body = {._undecl_kexpr = {._id = id}}}; +} +_z_declaration_t _z_make_decl_subscriber(_Z_MOVE(_z_keyexpr_t) key, uint32_t id, _Bool reliable, _Bool pull_mode) { + return (_z_declaration_t){ + ._tag = _Z_DECL_SUBSCRIBER, + ._body = {._decl_subscriber = {._id = id, + ._keyexpr = _z_keyexpr_steal(key), + ._ext_subscriber_info = {._pull_mode = pull_mode, ._reliable = reliable}}}}; +} +_z_declaration_t _z_make_undecl_subscriber(uint32_t id, _Z_OPTIONAL _Z_MOVE(_z_keyexpr_t) key) { + return (_z_declaration_t){._tag = _Z_UNDECL_SUBSCRIBER, + ._body = {._undecl_subscriber = {._id = id, ._ext_keyexpr = _z_keyexpr_steal(key)}}}; +} +_z_declaration_t _z_make_decl_queryable(_Z_MOVE(_z_keyexpr_t) key, uint32_t id, uint32_t distance, uint8_t complete) { + return (_z_declaration_t){ + ._tag = _Z_DECL_QUERYABLE, + ._body = {._decl_queryable = {._id = id, + ._keyexpr = _z_keyexpr_steal(key), + ._ext_queryable_info = {._complete = complete, ._distance = distance}}}}; +} +_z_declaration_t _z_make_undecl_queryable(uint32_t id, _Z_OPTIONAL _Z_MOVE(_z_keyexpr_t) key) { + return (_z_declaration_t){._tag = _Z_UNDECL_QUERYABLE, + ._body = {._undecl_queryable = {._id = id, ._ext_keyexpr = _z_keyexpr_steal(key)}}}; +} +_z_declaration_t _z_make_decl_token(_Z_MOVE(_z_keyexpr_t) key, uint32_t id) { + return (_z_declaration_t){._tag = _Z_DECL_TOKEN, + ._body = {._decl_token = { + ._id = id, + ._keyexpr = _z_keyexpr_steal(key), + }}}; +} +_z_declaration_t _z_make_undecl_token(uint32_t id, _Z_OPTIONAL _Z_MOVE(_z_keyexpr_t) key) { + return (_z_declaration_t){._tag = _Z_UNDECL_TOKEN, + ._body = {._undecl_token = {._id = id, ._ext_keyexpr = _z_keyexpr_steal(key)}}}; +} +_z_declaration_t _z_make_undecl_interest(uint32_t id, _Z_OPTIONAL _Z_MOVE(_z_keyexpr_t) key) { + return (_z_declaration_t){._tag = _Z_UNDECL_TOKEN, + ._body = {._undecl_token = {._id = id, ._ext_keyexpr = _z_keyexpr_steal(key)}}}; +} +_z_declaration_t _z_make_decl_interest(_Z_MOVE(_z_keyexpr_t) key, uint32_t id) { + return (_z_declaration_t){._tag = _Z_DECL_TOKEN, + ._body = {._decl_token = { + ._id = id, + ._keyexpr = _z_keyexpr_steal(key), + }}}; +} diff --git a/src/protocol/definitions/message.c b/src/protocol/definitions/message.c new file mode 100644 index 000000000..616f79890 --- /dev/null +++ b/src/protocol/definitions/message.c @@ -0,0 +1,22 @@ +#include "zenoh-pico/protocol/definitions/message.h" + +#include "zenoh-pico/protocol/msg.h" + +void _z_msg_reply_clear(_z_msg_reply_t *msg) { _z_value_clear(&msg->_value); } + +void _z_msg_put_clear(_z_msg_put_t *msg) { + _z_bytes_clear(&msg->_encoding.suffix); + _z_bytes_clear(&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->_value.payload.start != NULL, + .info = _z_id_check(msg->_info._id), + .consolidation = msg->_consolidation != Z_CONSOLIDATION_MODE_AUTO}; +} + +void _z_msg_query_clear(_z_msg_query_t *msg) { + _z_bytes_clear(&msg->_parameters); + _z_value_clear(&msg->_value); +} diff --git a/src/protocol/definitions/network.c b/src/protocol/definitions/network.c new file mode 100644 index 000000000..7496430c9 --- /dev/null +++ b/src/protocol/definitions/network.c @@ -0,0 +1,123 @@ +#include "zenoh-pico/protocol/definitions/network.h" + +#include "zenoh-pico/api/constants.h" +#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/msg.h" +#include "zenoh-pico/utils/logging.h" + +_z_n_msg_request_exts_t _z_n_msg_request_needed_exts(const _z_n_msg_request_t *msg) { + _z_n_msg_request_exts_t ret = {.n = 0, + .ext_budget = msg->ext_budget != 0, + .ext_target = msg->ext_target != Z_QUERY_TARGET_BEST_MATCHING, + .ext_qos = msg->ext_qos._val != _Z_N_QOS_DEFAULT._val, + .ext_timeout_ms = msg->ext_timeout_ms != 0, + .ext_tstamp = _z_timestamp_check(&msg->ext_tstamp)}; + if (ret.ext_budget) { + ret.n += 1; + } + if (ret.ext_target) { + ret.n += 1; + } + if (ret.ext_qos) { + ret.n += 1; + } + if (ret.ext_timeout_ms) { + ret.n += 1; + } + if (ret.ext_tstamp) { + ret.n += 1; + } + return ret; +} + +void _z_n_msg_request_clear(_z_n_msg_request_t *msg) { + _z_keyexpr_clear(&msg->_key); + switch (msg->_tag) { + case _Z_REQUEST_QUERY: { + _z_msg_query_clear(&msg->_body.query); + } break; + case _Z_REQUEST_PUT: { + _z_msg_put_clear(&msg->_body.put); + } break; + case _Z_REQUEST_DEL: { + _z_msg_del_clear(&msg->_body.del); + } break; + case _Z_REQUEST_PULL: { + _z_msg_pull_clear(&msg->_body.pull); + } break; + } +} + +/*=============================*/ +/* Network Messages */ +/*=============================*/ +void _z_push_body_clear(_z_push_body_t *msg) { (void)(msg); } + +void _z_n_msg_response_final_clear(_z_n_msg_response_final_t *msg) { (void)(msg); } + +void _z_n_msg_push_clear(_z_n_msg_push_t *msg) { + _z_keyexpr_clear(&msg->_key); + _z_push_body_clear(&msg->_body); +} + +void _z_n_msg_response_clear(_z_n_msg_response_t *msg) { + _z_timestamp_clear(&msg->_ext_timestamp); + _z_keyexpr_clear(&msg->_key); + switch (msg->_body_tag) { + case _Z_RESPONSE_BODY_REPLY: { + _z_msg_reply_clear(&msg->_body._reply); + break; + } + case _Z_RESPONSE_BODY_ERR: { + _z_msg_err_clear(&msg->_body._err); + break; + } + case _Z_RESPONSE_BODY_ACK: { + break; + } + case _Z_RESPONSE_BODY_PUT: { + _z_msg_put_clear(&msg->_body._put); + break; + } + case _Z_RESPONSE_BODY_DEL: { + break; + } + } +} + +void _z_n_msg_clear(_z_network_message_t *msg) { + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_N_PUSH: + _z_n_msg_push_clear(&msg->_body._push); + break; + case _Z_MID_N_REQUEST: + _z_n_msg_request_clear(&msg->_body._request); + break; + case _Z_MID_N_RESPONSE: + _z_n_msg_response_clear(&msg->_body._response); + break; + case _Z_MID_N_RESPONSE_FINAL: + _z_n_msg_response_final_clear(&msg->_body._response_f); + break; + case _Z_MID_N_DECLARE: + _z_n_msg_declare_clear(&msg->_body._declare); + break; + default: + _Z_DEBUG("WARNING: Trying to clear network message with unknown ID(%d)\n", mid); + break; + } + + _z_msg_ext_vec_clear(&msg->_extensions); +} + +void _z_n_msg_free(_z_network_message_t **msg) { + _z_network_message_t *ptr = *msg; + + if (ptr != NULL) { + _z_n_msg_clear(ptr); + + z_free(ptr); + *msg = NULL; + } +} \ No newline at end of file diff --git a/src/protocol/definitions/transport.c b/src/protocol/definitions/transport.c new file mode 100644 index 000000000..bfbd600b7 --- /dev/null +++ b/src/protocol/definitions/transport.c @@ -0,0 +1,399 @@ +#include "zenoh-pico/protocol/definitions/transport.h" + +#include "zenoh-pico/utils/logging.h" + +void _z_s_msg_clear_scout(_z_s_msg_scout_t *msg) {} + +/*------------------ Locators Field ------------------*/ +void _z_locators_clear(_z_locator_array_t *ls) { _z_locator_array_clear(ls); } + +void _z_s_msg_clear_hello(_z_s_msg_hello_t *msg) { _z_locators_clear(&msg->_locators); } + +void _z_t_msg_clear_join(_z_t_msg_join_t *msg) {} + +void _z_t_msg_clear_init(_z_t_msg_init_t *msg) { _z_bytes_clear(&msg->_cookie); } + +void _z_t_msg_clear_open(_z_t_msg_open_t *msg) { _z_bytes_clear(&msg->_cookie); } + +void _z_t_msg_clear_close(_z_t_msg_close_t *msg) { (void)(msg); } + +void _z_t_msg_clear_keep_alive(_z_t_msg_keep_alive_t *msg) { (void)(msg); } + +void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg) { _z_network_message_vec_clear(&msg->_messages); } + +void _z_t_msg_clear_fragment(_z_t_msg_fragment_t *msg) { _z_bytes_clear(&msg->_payload); } + +void _z_t_msg_clear(_z_transport_message_t *msg) { + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_T_JOIN: { + _z_t_msg_clear_join(&msg->_body._join); + } break; + + case _Z_MID_T_INIT: { + _z_t_msg_clear_init(&msg->_body._init); + } break; + + case _Z_MID_T_OPEN: { + _z_t_msg_clear_open(&msg->_body._open); + } break; + + case _Z_MID_T_CLOSE: { + _z_t_msg_clear_close(&msg->_body._close); + } break; + + case _Z_MID_T_KEEP_ALIVE: { + _z_t_msg_clear_keep_alive(&msg->_body._keep_alive); + } break; + + case _Z_MID_T_FRAME: { + _z_t_msg_clear_frame(&msg->_body._frame); + } break; + + case _Z_MID_T_FRAGMENT: { + _z_t_msg_clear_fragment(&msg->_body._fragment); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to clear transport message with unknown ID(%d)\n", mid); + } break; + } +} + +/*------------------ Join Message ------------------*/ +_z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_id_t zid, + _z_conduit_sn_list_t next_sn) { + _z_transport_message_t msg; + msg._header = _Z_MID_T_JOIN; + + msg._body._join._version = Z_PROTO_VERSION; + msg._body._join._whatami = whatami; + msg._body._join._lease = lease; + msg._body._join._seq_num_res = Z_SN_RESOLUTION; + msg._body._join._req_id_res = Z_REQ_RESOLUTION; + msg._body._join._batch_size = Z_BATCH_SIZE; + msg._body._join._next_sn = next_sn; + msg._body._join._zid = zid; + + if ((lease % 1000) == 0) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_JOIN_T); + } + + if ((Z_BATCH_SIZE != _Z_DEFAULT_BATCH_SIZE) || (Z_SN_RESOLUTION != _Z_DEFAULT_RESOLUTION_SIZE) || + (Z_REQ_RESOLUTION != _Z_DEFAULT_RESOLUTION_SIZE)) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_JOIN_S); + } + + if (next_sn._is_qos) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_Z); + } + + return msg; +} + +/*------------------ Init Message ------------------*/ +_z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_id_t zid) { + _z_transport_message_t msg; + msg._header = _Z_MID_T_INIT; + + msg._body._init._version = Z_PROTO_VERSION; + msg._body._init._whatami = whatami; + msg._body._init._zid = zid; + msg._body._init._seq_num_res = Z_SN_RESOLUTION; + msg._body._init._req_id_res = Z_REQ_RESOLUTION; + msg._body._init._batch_size = Z_BATCH_SIZE; + _z_bytes_reset(&msg._body._init._cookie); + + if ((msg._body._init._batch_size != _Z_DEFAULT_BATCH_SIZE) || + (msg._body._init._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || + (msg._body._init._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_S); + } + + return msg; +} + +_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, _z_bytes_t cookie) { + _z_transport_message_t msg; + msg._header = _Z_MID_T_INIT; + _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_A); + + msg._body._init._version = Z_PROTO_VERSION; + msg._body._init._whatami = whatami; + msg._body._init._zid = zid; + msg._body._init._seq_num_res = Z_SN_RESOLUTION; + msg._body._init._req_id_res = Z_REQ_RESOLUTION; + msg._body._init._batch_size = Z_BATCH_SIZE; + msg._body._init._cookie = cookie; + + if ((msg._body._init._batch_size != _Z_DEFAULT_BATCH_SIZE) || + (msg._body._init._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || + (msg._body._init._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_S); + } + + return msg; +} + +/*------------------ Open Message ------------------*/ +_z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie) { + _z_transport_message_t msg; + msg._header = _Z_MID_T_OPEN; + + msg._body._open._lease = lease; + msg._body._open._initial_sn = initial_sn; + msg._body._open._cookie = cookie; + + if ((lease % 1000) == 0) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_OPEN_T); + } + + return msg; +} + +_z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial_sn) { + _z_transport_message_t msg; + msg._header = _Z_MID_T_OPEN; + _Z_SET_FLAG(msg._header, _Z_FLAG_T_OPEN_A); + + msg._body._open._lease = lease; + msg._body._open._initial_sn = initial_sn; + _z_bytes_reset(&msg._body._open._cookie); + + if ((lease % 1000) == 0) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_OPEN_T); + } + + return msg; +} + +/*------------------ Close Message ------------------*/ +_z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only) { + _z_transport_message_t msg; + msg._header = _Z_MID_T_CLOSE; + + msg._body._close._reason = reason; + if (link_only == false) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_CLOSE_S); + } + + return msg; +} + +/*------------------ Keep Alive Message ------------------*/ +_z_transport_message_t _z_t_msg_make_keep_alive(void) { + _z_transport_message_t msg; + msg._header = _Z_MID_T_KEEP_ALIVE; + + return msg; +} + +_z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_network_message_vec_t messages, _Bool is_reliable) { + _z_transport_message_t msg; + msg._header = _Z_MID_T_FRAME; + + msg._body._frame._sn = sn; + if (is_reliable == true) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAME_R); + } + + msg._body._frame._messages = messages; + + return msg; +} + +/*------------------ Frame Message ------------------*/ +_z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable) { + _z_transport_message_t msg; + msg._header = _Z_MID_T_FRAME; + + msg._body._frame._sn = sn; + if (is_reliable == true) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAME_R); + } + + msg._body._frame._messages = _z_network_message_vec_make(0); + + return msg; +} + +/*------------------ Fragment Message ------------------*/ +_z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_bytes_t payload, _Bool is_reliable, _Bool is_last) { + _z_transport_message_t msg; + msg._header = _Z_MID_T_FRAGMENT; + if (is_last == true) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAGMENT_M); + } + if (is_reliable == true) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAGMENT_R); + } + + msg._body._fragment._sn = sn; + msg._body._fragment._payload = payload; + + return msg; +} + +void _z_t_msg_copy_fragment(_z_t_msg_fragment_t *clone, _z_t_msg_fragment_t *msg) { + _z_bytes_copy(&clone->_payload, &msg->_payload); +} + +void _z_t_msg_copy_join(_z_t_msg_join_t *clone, _z_t_msg_join_t *msg) { + clone->_version = msg->_version; + clone->_whatami = msg->_whatami; + clone->_lease = msg->_lease; + clone->_seq_num_res = msg->_seq_num_res; + clone->_req_id_res = msg->_req_id_res; + clone->_batch_size = msg->_batch_size; + clone->_next_sn = msg->_next_sn; + memcpy(clone->_zid.id, msg->_zid.id, 16); +} + +void _z_t_msg_copy_init(_z_t_msg_init_t *clone, _z_t_msg_init_t *msg) { + clone->_version = msg->_version; + clone->_whatami = msg->_whatami; + clone->_seq_num_res = msg->_seq_num_res; + clone->_req_id_res = msg->_req_id_res; + clone->_batch_size = msg->_batch_size; + memcpy(clone->_zid.id, msg->_zid.id, 16); + _z_bytes_copy(&clone->_cookie, &msg->_cookie); +} + +void _z_t_msg_copy_open(_z_t_msg_open_t *clone, _z_t_msg_open_t *msg) { + clone->_lease = msg->_lease; + clone->_initial_sn = msg->_initial_sn; + _z_bytes_copy(&clone->_cookie, &msg->_cookie); +} + +void _z_t_msg_copy_close(_z_t_msg_close_t *clone, _z_t_msg_close_t *msg) { clone->_reason = msg->_reason; } + +void _z_t_msg_copy_keep_alive(_z_t_msg_keep_alive_t *clone, _z_t_msg_keep_alive_t *msg) { + (void)(clone); + (void)(msg); +} + +void _z_t_msg_copy_frame(_z_t_msg_frame_t *clone, _z_t_msg_frame_t *msg) { + clone->_sn = msg->_sn; + _z_network_message_vec_copy(&clone->_messages, &msg->_messages); +} + +/*------------------ Transport Message ------------------*/ +void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg) { + clone->_header = msg->_header; + + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_T_JOIN: { + _z_t_msg_copy_join(&clone->_body._join, &msg->_body._join); + } break; + + case _Z_MID_T_INIT: { + _z_t_msg_copy_init(&clone->_body._init, &msg->_body._init); + } break; + + case _Z_MID_T_OPEN: { + _z_t_msg_copy_open(&clone->_body._open, &msg->_body._open); + } break; + + case _Z_MID_T_CLOSE: { + _z_t_msg_copy_close(&clone->_body._close, &msg->_body._close); + } break; + + case _Z_MID_T_KEEP_ALIVE: { + _z_t_msg_copy_keep_alive(&clone->_body._keep_alive, &msg->_body._keep_alive); + } break; + + case _Z_MID_T_FRAME: { + _z_t_msg_copy_frame(&clone->_body._frame, &msg->_body._frame); + } break; + + case _Z_MID_T_FRAGMENT: { + _z_t_msg_copy_fragment(&clone->_body._fragment, &msg->_body._fragment); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to copy transport message with unknown ID(%d)\n", mid); + } break; + } +} + +void _z_s_msg_clear(_z_scouting_message_t *msg) { + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_SCOUT: { + _z_s_msg_clear_scout(&msg->_body._scout); + } break; + + case _Z_MID_HELLO: { + _z_s_msg_clear_hello(&msg->_body._hello); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to clear session message with unknown ID(%d)\n", mid); + } break; + } +} + +/*=============================*/ +/* Transport Messages */ +/*=============================*/ +/*------------------ Scout Message ------------------*/ +_z_scouting_message_t _z_s_msg_make_scout(z_what_t what, _z_id_t zid) { + _z_scouting_message_t msg; + msg._header = _Z_MID_SCOUT; + + msg._body._scout._version = Z_PROTO_VERSION; + msg._body._scout._what = what; + msg._body._scout._zid = zid; + + return msg; +} + +/*------------------ Hello Message ------------------*/ +_z_scouting_message_t _z_s_msg_make_hello(z_whatami_t whatami, _z_id_t zid, _z_locator_array_t locators) { + _z_scouting_message_t msg; + msg._header = _Z_MID_HELLO; + + msg._body._hello._version = Z_PROTO_VERSION; + msg._body._hello._whatami = whatami; + msg._body._hello._zid = zid; + msg._body._hello._locators = locators; + + if (_z_locator_array_is_empty(&locators) == false) { + _Z_SET_FLAG(msg._header, _Z_FLAG_T_HELLO_L); + } + + return msg; +} + +void _z_s_msg_copy_scout(_z_s_msg_scout_t *clone, _z_s_msg_scout_t *msg) { + clone->_what = msg->_what; + clone->_version = msg->_version; + memcpy(clone->_zid.id, msg->_zid.id, 16); +} + +void _z_s_msg_copy_hello(_z_s_msg_hello_t *clone, _z_s_msg_hello_t *msg) { + _z_locator_array_copy(&clone->_locators, &msg->_locators); + memcpy(clone->_zid.id, msg->_zid.id, 16); + clone->_whatami = msg->_whatami; +} + +/*------------------ Scouting Message ------------------*/ +void _z_s_msg_copy(_z_scouting_message_t *clone, _z_scouting_message_t *msg) { + clone->_header = msg->_header; + + uint8_t mid = _Z_MID(msg->_header); + switch (mid) { + case _Z_MID_SCOUT: { + _z_s_msg_copy_scout(&clone->_body._scout, &msg->_body._scout); + } break; + + case _Z_MID_HELLO: { + _z_s_msg_copy_hello(&clone->_body._hello, &msg->_body._hello); + } break; + + default: { + _Z_DEBUG("WARNING: Trying to copy session message with unknown ID(%d)\n", mid); + } break; + } +} \ No newline at end of file diff --git a/src/protocol/msg.c b/src/protocol/msg.c index cc607f564..ddb39352e 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -51,8 +51,9 @@ void _z_timestamp_clear(_z_timestamp_t *ts) {} /*------------------ ResKey Field ------------------*/ void _z_keyexpr_clear(_z_keyexpr_t *rk) { rk->_id = 0; - if (rk->_suffix != NULL) { + if (rk->_suffix != NULL && rk->is_alloc) { _z_str_clear((char *)rk->_suffix); + rk->is_alloc = false; } } @@ -67,9 +68,6 @@ void _z_keyexpr_free(_z_keyexpr_t **rk) { } } -/*------------------ Locators Field ------------------*/ -void _z_locators_clear(_z_locator_array_t *ls) { _z_locator_array_clear(ls); } - /*=============================*/ /* Zenoh Messages */ /*=============================*/ @@ -263,35 +261,6 @@ void _z_data_info_clear(_z_data_info_t *di) { _z_bytes_clear(&di->_source_id); _z_timestamp_clear(&di->_tstamp); } -void _z_msg_put_clear(_z_msg_put_t *msg) { - _z_bytes_clear(&msg->_encoding.suffix); - _z_bytes_clear(&msg->_payload); - _z_timestamp_clear(&msg->_commons._timestamp); -} -void _z_n_msg_response_clear(_z_n_msg_response_t *msg) { - _z_timestamp_clear(&msg->_ext_timestamp); - _z_keyexpr_clear(&msg->_key); - switch (msg->_body_tag) { - case _Z_RESPONSE_BODY_REPLY: { - _z_msg_reply_clear(&msg->_body._reply); - break; - } - case _Z_RESPONSE_BODY_ERR: { - _z_msg_err_clear(&msg->_body._err); - break; - } - case _Z_RESPONSE_BODY_ACK: { - break; - } - case _Z_RESPONSE_BODY_PUT: { - _z_msg_put_clear(&msg->_body._put); - break; - } - case _Z_RESPONSE_BODY_DEL: { - break; - } - } -} /*------------------ Data Message ------------------*/ _z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool can_be_dropped) { @@ -376,15 +345,6 @@ _z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, _z_bytes_t parameters, _z return msg; } -_z_msg_query_reqexts_t _z_msg_query_required_extensions(const _z_msg_query_t *msg) { - return (_z_msg_query_reqexts_t){.body = msg->_value.payload.start != NULL, - .info = _z_id_check(msg->_info._id), - .consolidation = msg->_consolidation != Z_CONSOLIDATION_MODE_AUTO}; -} -void _z_msg_clear_query(_z_msg_query_t *msg) { - _z_bytes_clear(&msg->_parameters); - _z_value_clear(&msg->_value); -} /*------------------ Reply Message ------------------*/ _z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool can_be_dropped) { @@ -404,7 +364,7 @@ void _z_msg_clear(_z_zenoh_message_t *msg) { _z_msg_clear_pull(&msg->_body._pull); break; case _Z_MID_Z_QUERY: - _z_msg_clear_query(&msg->_body._query); + _z_msg_query_clear(&msg->_body._query); break; case _Z_MID_Z_UNIT: _z_msg_clear_unit(&msg->_body._unit); @@ -426,11 +386,6 @@ void _z_msg_free(_z_zenoh_message_t **msg) { } } -/*=============================*/ -/* Network Messages */ -/*=============================*/ -void _z_push_body_clear(_z_push_body_t *msg) { (void)(msg); } - void _z_request_body_clear(_z_request_body_t *msg) { (void)(msg); } void _z_response_body_clear(_z_response_body_t *msg) { (void)(msg); } @@ -465,11 +420,6 @@ _z_network_message_t _z_n_msg_make_push(_z_keyexpr_t key, _z_push_body_t body, _ return msg; } -void _z_n_msg_clear_push(_z_n_msg_push_t *msg) { - _z_keyexpr_clear(&msg->_key); - _z_push_body_clear(&msg->_body); -} - _z_network_message_t _z_n_msg_make_request(_z_zint_t rid, _z_keyexpr_t key, _z_request_body_t body, _Bool is_remote_mapping) { _z_network_message_t msg; @@ -490,11 +440,6 @@ _z_network_message_t _z_n_msg_make_request(_z_zint_t rid, _z_keyexpr_t key, _z_r return msg; } -void _z_n_msg_clear_request(_z_n_msg_request_t *msg) { - _z_keyexpr_clear(&msg->_key); - _z_request_body_clear(&msg->_body); -} - _z_network_message_t _z_n_msg_make_response(_z_zint_t rid, _z_keyexpr_t key, _z_response_body_t body, _Bool is_remote_mapping) { _z_network_message_t msg; @@ -515,8 +460,6 @@ _z_network_message_t _z_n_msg_make_response(_z_zint_t rid, _z_keyexpr_t key, _z_ return msg; } -void _z_msg_reply_clear(_z_msg_reply_t *msg) { _z_value_clear(&msg->_value); } - _z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid) { _z_network_message_t msg; msg._header = _Z_MID_N_RESPONSE_FINAL; @@ -525,436 +468,4 @@ _z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid) { msg._extensions = _z_msg_ext_vec_make(0); return msg; -} - -void _z_n_msg_clear_response_final(_z_n_msg_response_final_t *msg) { (void)(msg); } - -void _z_n_msg_clear(_z_network_message_t *msg) { - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_N_PUSH: - _z_n_msg_clear_push(&msg->_body._push); - break; - case _Z_MID_N_REQUEST: - _z_n_msg_clear_request(&msg->_body._request); - break; - case _Z_MID_N_RESPONSE: - _z_msg_reply_clear(&msg->_body._response); - break; - case _Z_MID_N_RESPONSE_FINAL: - _z_n_msg_clear_response_final(&msg->_body._response_f); - break; - case _Z_MID_N_DECLARE: - _z_n_msg_clear_declare(&msg->_body._declare); - break; - default: - _Z_DEBUG("WARNING: Trying to clear network message with unknown ID(%d)\n", mid); - break; - } - - _z_msg_ext_vec_clear(&msg->_extensions); -} - -void _z_n_msg_free(_z_network_message_t **msg) { - _z_network_message_t *ptr = *msg; - - if (ptr != NULL) { - _z_n_msg_clear(ptr); - - z_free(ptr); - *msg = NULL; - } -} - -/*=============================*/ -/* Transport Messages */ -/*=============================*/ -/*------------------ Scout Message ------------------*/ -_z_scouting_message_t _z_s_msg_make_scout(z_what_t what, _z_id_t zid) { - _z_scouting_message_t msg; - msg._header = _Z_MID_SCOUT; - - msg._body._scout._version = Z_PROTO_VERSION; - msg._body._scout._what = what; - msg._body._scout._zid = zid; - - return msg; -} - -void _z_s_msg_copy_scout(_z_s_msg_scout_t *clone, _z_s_msg_scout_t *msg) { - clone->_what = msg->_what; - clone->_version = msg->_version; - memcpy(clone->_zid.id, msg->_zid.id, 16); -} - -void _z_s_msg_clear_scout(_z_s_msg_scout_t *msg) {} - -/*------------------ Hello Message ------------------*/ -_z_scouting_message_t _z_s_msg_make_hello(z_whatami_t whatami, _z_id_t zid, _z_locator_array_t locators) { - _z_scouting_message_t msg; - msg._header = _Z_MID_HELLO; - - msg._body._hello._version = Z_PROTO_VERSION; - msg._body._hello._whatami = whatami; - msg._body._hello._zid = zid; - msg._body._hello._locators = locators; - - if (_z_locator_array_is_empty(&locators) == false) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_HELLO_L); - } - - return msg; -} - -void _z_s_msg_copy_hello(_z_s_msg_hello_t *clone, _z_s_msg_hello_t *msg) { - _z_locator_array_copy(&clone->_locators, &msg->_locators); - memcpy(clone->_zid.id, msg->_zid.id, 16); - clone->_whatami = msg->_whatami; -} - -void _z_s_msg_clear_hello(_z_s_msg_hello_t *msg) { _z_locators_clear(&msg->_locators); } - -/*------------------ Join Message ------------------*/ -_z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _z_id_t zid, - _z_conduit_sn_list_t next_sn) { - _z_transport_message_t msg; - msg._header = _Z_MID_T_JOIN; - - msg._body._join._version = Z_PROTO_VERSION; - msg._body._join._whatami = whatami; - msg._body._join._lease = lease; - msg._body._join._seq_num_res = Z_SN_RESOLUTION; - msg._body._join._req_id_res = Z_REQ_RESOLUTION; - msg._body._join._batch_size = Z_BATCH_SIZE; - msg._body._join._next_sn = next_sn; - msg._body._join._zid = zid; - - if ((lease % 1000) == 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_JOIN_T); - } - - if ((Z_BATCH_SIZE != _Z_DEFAULT_BATCH_SIZE) || (Z_SN_RESOLUTION != _Z_DEFAULT_RESOLUTION_SIZE) || - (Z_REQ_RESOLUTION != _Z_DEFAULT_RESOLUTION_SIZE)) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_JOIN_S); - } - - if (next_sn._is_qos) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_Z); - } - - return msg; -} - -void _z_t_msg_copy_join(_z_t_msg_join_t *clone, _z_t_msg_join_t *msg) { - clone->_version = msg->_version; - clone->_whatami = msg->_whatami; - clone->_lease = msg->_lease; - clone->_seq_num_res = msg->_seq_num_res; - clone->_req_id_res = msg->_req_id_res; - clone->_batch_size = msg->_batch_size; - clone->_next_sn = msg->_next_sn; - memcpy(clone->_zid.id, msg->_zid.id, 16); -} - -void _z_t_msg_clear_join(_z_t_msg_join_t *msg) {} - -/*------------------ Init Message ------------------*/ -_z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_id_t zid) { - _z_transport_message_t msg; - msg._header = _Z_MID_T_INIT; - - msg._body._init._version = Z_PROTO_VERSION; - msg._body._init._whatami = whatami; - msg._body._init._zid = zid; - msg._body._init._seq_num_res = Z_SN_RESOLUTION; - msg._body._init._req_id_res = Z_REQ_RESOLUTION; - msg._body._init._batch_size = Z_BATCH_SIZE; - _z_bytes_reset(&msg._body._init._cookie); - - if ((msg._body._init._batch_size != _Z_DEFAULT_BATCH_SIZE) || - (msg._body._init._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || - (msg._body._init._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_S); - } - - return msg; -} - -_z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, _z_bytes_t cookie) { - _z_transport_message_t msg; - msg._header = _Z_MID_T_INIT; - _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_A); - - msg._body._init._version = Z_PROTO_VERSION; - msg._body._init._whatami = whatami; - msg._body._init._zid = zid; - msg._body._init._seq_num_res = Z_SN_RESOLUTION; - msg._body._init._req_id_res = Z_REQ_RESOLUTION; - msg._body._init._batch_size = Z_BATCH_SIZE; - msg._body._init._cookie = cookie; - - if ((msg._body._init._batch_size != _Z_DEFAULT_BATCH_SIZE) || - (msg._body._init._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || - (msg._body._init._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_S); - } - - return msg; -} - -void _z_t_msg_copy_init(_z_t_msg_init_t *clone, _z_t_msg_init_t *msg) { - clone->_version = msg->_version; - clone->_whatami = msg->_whatami; - clone->_seq_num_res = msg->_seq_num_res; - clone->_req_id_res = msg->_req_id_res; - clone->_batch_size = msg->_batch_size; - memcpy(clone->_zid.id, msg->_zid.id, 16); - _z_bytes_copy(&clone->_cookie, &msg->_cookie); -} - -void _z_t_msg_clear_init(_z_t_msg_init_t *msg) { _z_bytes_clear(&msg->_cookie); } - -/*------------------ Open Message ------------------*/ -_z_transport_message_t _z_t_msg_make_open_syn(_z_zint_t lease, _z_zint_t initial_sn, _z_bytes_t cookie) { - _z_transport_message_t msg; - msg._header = _Z_MID_T_OPEN; - - msg._body._open._lease = lease; - msg._body._open._initial_sn = initial_sn; - msg._body._open._cookie = cookie; - - if ((lease % 1000) == 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_OPEN_T); - } - - return msg; -} - -_z_transport_message_t _z_t_msg_make_open_ack(_z_zint_t lease, _z_zint_t initial_sn) { - _z_transport_message_t msg; - msg._header = _Z_MID_T_OPEN; - _Z_SET_FLAG(msg._header, _Z_FLAG_T_OPEN_A); - - msg._body._open._lease = lease; - msg._body._open._initial_sn = initial_sn; - _z_bytes_reset(&msg._body._open._cookie); - - if ((lease % 1000) == 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_OPEN_T); - } - - return msg; -} - -void _z_t_msg_copy_open(_z_t_msg_open_t *clone, _z_t_msg_open_t *msg) { - clone->_lease = msg->_lease; - clone->_initial_sn = msg->_initial_sn; - _z_bytes_copy(&clone->_cookie, &msg->_cookie); -} - -void _z_t_msg_clear_open(_z_t_msg_open_t *msg) { _z_bytes_clear(&msg->_cookie); } - -/*------------------ Close Message ------------------*/ -_z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only) { - _z_transport_message_t msg; - msg._header = _Z_MID_T_CLOSE; - - msg._body._close._reason = reason; - if (link_only == false) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_CLOSE_S); - } - - return msg; -} - -void _z_t_msg_copy_close(_z_t_msg_close_t *clone, _z_t_msg_close_t *msg) { clone->_reason = msg->_reason; } - -void _z_t_msg_clear_close(_z_t_msg_close_t *msg) { (void)(msg); } - -/*------------------ Keep Alive Message ------------------*/ -_z_transport_message_t _z_t_msg_make_keep_alive(void) { - _z_transport_message_t msg; - msg._header = _Z_MID_T_KEEP_ALIVE; - - return msg; -} - -void _z_t_msg_copy_keep_alive(_z_t_msg_keep_alive_t *clone, _z_t_msg_keep_alive_t *msg) { - (void)(clone); - (void)(msg); -} - -void _z_t_msg_clear_keep_alive(_z_t_msg_keep_alive_t *msg) { (void)(msg); } - -/*------------------ Frame Message ------------------*/ -_z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable) { - _z_transport_message_t msg; - msg._header = _Z_MID_T_FRAME; - - msg._body._frame._sn = sn; - if (is_reliable == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAME_R); - } - - msg._body._frame._messages = _z_network_message_vec_make(0); - - return msg; -} - -_z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_network_message_vec_t messages, _Bool is_reliable) { - _z_transport_message_t msg; - msg._header = _Z_MID_T_FRAME; - - msg._body._frame._sn = sn; - if (is_reliable == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAME_R); - } - - msg._body._frame._messages = messages; - - return msg; -} - -void _z_t_msg_copy_frame(_z_t_msg_frame_t *clone, _z_t_msg_frame_t *msg) { - clone->_sn = msg->_sn; - _z_network_message_vec_copy(&clone->_messages, &msg->_messages); -} - -void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg) { _z_network_message_vec_clear(&msg->_messages); } - -/*------------------ Fragment Message ------------------*/ -_z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_bytes_t payload, _Bool is_reliable, _Bool is_last) { - _z_transport_message_t msg; - msg._header = _Z_MID_T_FRAGMENT; - if (is_last == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAGMENT_M); - } - if (is_reliable == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAGMENT_R); - } - - msg._body._fragment._sn = sn; - msg._body._fragment._payload = payload; - - return msg; -} - -void _z_t_msg_copy_fragment(_z_t_msg_fragment_t *clone, _z_t_msg_fragment_t *msg) { - _z_bytes_copy(&clone->_payload, &msg->_payload); -} - -void _z_t_msg_clear_fragment(_z_t_msg_fragment_t *msg) { _z_bytes_clear(&msg->_payload); } - -/*------------------ Transport Message ------------------*/ -void _z_t_msg_copy(_z_transport_message_t *clone, _z_transport_message_t *msg) { - clone->_header = msg->_header; - - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_T_JOIN: { - _z_t_msg_copy_join(&clone->_body._join, &msg->_body._join); - } break; - - case _Z_MID_T_INIT: { - _z_t_msg_copy_init(&clone->_body._init, &msg->_body._init); - } break; - - case _Z_MID_T_OPEN: { - _z_t_msg_copy_open(&clone->_body._open, &msg->_body._open); - } break; - - case _Z_MID_T_CLOSE: { - _z_t_msg_copy_close(&clone->_body._close, &msg->_body._close); - } break; - - case _Z_MID_T_KEEP_ALIVE: { - _z_t_msg_copy_keep_alive(&clone->_body._keep_alive, &msg->_body._keep_alive); - } break; - - case _Z_MID_T_FRAME: { - _z_t_msg_copy_frame(&clone->_body._frame, &msg->_body._frame); - } break; - - case _Z_MID_T_FRAGMENT: { - _z_t_msg_copy_fragment(&clone->_body._fragment, &msg->_body._fragment); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to copy transport message with unknown ID(%d)\n", mid); - } break; - } -} - -void _z_t_msg_clear(_z_transport_message_t *msg) { - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_T_JOIN: { - _z_t_msg_clear_join(&msg->_body._join); - } break; - - case _Z_MID_T_INIT: { - _z_t_msg_clear_init(&msg->_body._init); - } break; - - case _Z_MID_T_OPEN: { - _z_t_msg_clear_open(&msg->_body._open); - } break; - - case _Z_MID_T_CLOSE: { - _z_t_msg_clear_close(&msg->_body._close); - } break; - - case _Z_MID_T_KEEP_ALIVE: { - _z_t_msg_clear_keep_alive(&msg->_body._keep_alive); - } break; - - case _Z_MID_T_FRAME: { - _z_t_msg_clear_frame(&msg->_body._frame); - } break; - - case _Z_MID_T_FRAGMENT: { - _z_t_msg_clear_fragment(&msg->_body._fragment); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to clear transport message with unknown ID(%d)\n", mid); - } break; - } -} - -/*------------------ Scouting Message ------------------*/ -void _z_s_msg_copy(_z_scouting_message_t *clone, _z_scouting_message_t *msg) { - clone->_header = msg->_header; - - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_SCOUT: { - _z_s_msg_copy_scout(&clone->_body._scout, &msg->_body._scout); - } break; - - case _Z_MID_HELLO: { - _z_s_msg_copy_hello(&clone->_body._hello, &msg->_body._hello); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to copy session message with unknown ID(%d)\n", mid); - } break; - } -} - -void _z_s_msg_clear(_z_scouting_message_t *msg) { - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_SCOUT: { - _z_s_msg_clear_scout(&msg->_body._scout); - } break; - - case _Z_MID_HELLO: { - _z_s_msg_clear_hello(&msg->_body._hello); - } break; - - default: { - _Z_DEBUG("WARNING: Trying to clear session message with unknown ID(%d)\n", mid); - } break; - } -} +} \ No newline at end of file diff --git a/src/session/utils.c b/src/session/utils.c index 70e5a320f..e76454591 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -17,6 +17,7 @@ #include #include "zenoh-pico/config.h" +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/session/query.h" #include "zenoh-pico/session/queryable.h" #include "zenoh-pico/session/resource.h" @@ -26,6 +27,7 @@ void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src) { dst->_id = src->_id; dst->_suffix = src->_suffix ? _z_str_clone(src->_suffix) : NULL; + dst->is_alloc = true; } _z_keyexpr_t _z_keyexpr_duplicate(const _z_keyexpr_t *src) { @@ -34,6 +36,14 @@ _z_keyexpr_t _z_keyexpr_duplicate(const _z_keyexpr_t *src) { return dst; } +_z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src) { + _z_keyexpr_t stolen = *src; + src->is_alloc = false; + src->_id = 0; + src->_suffix = NULL; + return stolen; +} + _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp) { _z_timestamp_t ts; ts.id = tstamp->id; diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 7f69b785c..4324d0326 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -1540,7 +1540,7 @@ void query_message(void) { assert_eq_query_message(&e_qy, &d_qy, e_hdr); // Free - _z_msg_clear_query(&d_qy); + _z_msg_query_clear(&d_qy); _z_msg_clear(&z_msg); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); @@ -1839,7 +1839,7 @@ void push_message(void) { assert_eq_push_message(&e_pu, &d_pu, n_msg._header); // Free - _z_n_msg_clear_push(&d_pu); + _z_n_msg_push_clear(&d_pu); _z_n_msg_clear(&n_msg); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); @@ -1893,7 +1893,7 @@ void request_message(void) { assert_eq_request_message(&e_re, &d_re, n_msg._header); // Free - _z_n_msg_clear_request(&d_re); + _z_n_msg_request_clear(&d_re); _z_n_msg_clear(&n_msg); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); @@ -1995,7 +1995,7 @@ void response_final_message(void) { assert_eq_response_final_message(&e_re, &d_re); // Free - _z_n_msg_clear_response_final(&d_re); + _z_n_msg_response_final_clear(&d_re); _z_n_msg_clear(&n_msg); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); From 60e9c7f87e7346ac1f51f20d83f70dccfc57b487 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 30 Jun 2023 17:19:17 +0200 Subject: [PATCH 108/181] codecs should be done [skip ci] --- CMakeLists.txt | 38 +--- .../zenoh-pico/protocol/codec/declarations.h | 2 +- include/zenoh-pico/protocol/codec/network.h | 17 +- .../protocol/definitions/declarations.h | 5 +- .../zenoh-pico/protocol/definitions/network.h | 6 +- src/protocol/codec/declarations.c | 8 +- src/protocol/codec/message.c | 2 +- src/protocol/codec/network.c | 174 ++++++++++++++---- src/protocol/codec/transport.c | 1 + src/protocol/definitions/declarations.c | 3 + src/protocol/definitions/network.c | 20 +- 11 files changed, 177 insertions(+), 99 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c472e7b70..7c2996a7a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -119,44 +119,10 @@ elseif(CMAKE_BUILD_TYPE MATCHES "RELEASE") endif() endif() -file(GLOB PublicHeaders "include/*.h" - "include/zenoh-pico/*.h" - "include/zenoh-pico/api/*.h" - "include/zenoh-pico/collections/*.h" - "include/zenoh-pico/link/*.h" - "include/zenoh-pico/link/config/*.h" - "include/zenoh-pico/protocol/*/*.h" - "include/zenoh-pico/protocol/*.h" - "include/zenoh-pico/session/*.h" - "include/zenoh-pico/system/*.h" - "include/zenoh-pico/system/link/*.h" - "include/zenoh-pico/transport/*.h" - "include/zenoh-pico/utils/*.h" -) +file(GLOB_RECURSE PublicHeaders "include/*.h") target_include_directories(${Libname} PUBLIC ${PROJECT_SOURCE_DIR}/include) -file(GLOB Sources "src/*.c" - "src/api/*.c" - "src/net/*.c" - "src/collections/*.c" - "src/link/*.c" - "src/link/config/*.c" - "src/link/unicast/*.c" - "src/link/multicast/*.c" - "src/protocol/*.c" - "src/protocol/*/*.c" - "src/session/*.c" - "src/system/*.c" - "src/transport/*.c" - "src/transport/common/*.c" - "src/transport/unicast/*.c" - "src/transport/unicast/link/*.c" - "src/transport/unicast/link/task/*.c" - "src/transport/multicast/*.c" - "src/transport/multicast/link/*.c" - "src/transport/multicast/link/task/*.c" - "src/utils/*.c" -) +file(GLOB_RECURSE Sources "src/*.c") if(WITH_ZEPHYR) file (GLOB Sources_Zephyr "src/system/zephyr/*.c") diff --git a/include/zenoh-pico/protocol/codec/declarations.h b/include/zenoh-pico/protocol/codec/declarations.h index 3e20c8b8a..1d140f3b5 100644 --- a/include/zenoh-pico/protocol/codec/declarations.h +++ b/include/zenoh-pico/protocol/codec/declarations.h @@ -32,6 +32,6 @@ #define _Z_FINAL_INTEREST_MID 9 #define _Z_UNDECL_INTEREST_MID 10 int8_t _z_declaration_encode(_z_wbuf_t *wbf, const _z_declaration_t *decl); -int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf); #endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_DECLARATIONS_H */ diff --git a/include/zenoh-pico/protocol/codec/network.h b/include/zenoh-pico/protocol/codec/network.h index 3f6bd712d..ef42cc991 100644 --- a/include/zenoh-pico/protocol/codec/network.h +++ b/include/zenoh-pico/protocol/codec/network.h @@ -15,11 +15,22 @@ #ifndef INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_NETWORK_H #define INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_NETWORK_H +#include + #include "zenoh-pico/protocol/definitions/network.h" #include "zenoh-pico/protocol/iobuf.h" +int8_t _z_push_encode(_z_wbuf_t *wbf, const _z_n_msg_push_t *msg); +int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_request_encode(_z_wbuf_t *wbf, const _z_n_msg_request_t *msg); +int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg); +int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_response_final_encode(_z_wbuf_t *wbf, const _z_n_msg_response_final_t *msg); +int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_n_msg_declare_t *decl); +int8_t _z_declare_decode(_z_n_msg_declare_t *decl, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_network_message_encode(_z_wbuf_t *buf, const _z_network_message_t *msg); -int8_t _z_network_message_decode(_z_network_message_t *msg, _z_zbuf_t *buf); -int8_t _z_network_message_decode_na(_z_network_message_t *msg, _z_zbuf_t *buf); +int8_t _z_network_message_encode(_z_wbuf_t *wbf, const _z_network_message_t *msg); +int8_t _z_network_message_decode(_z_network_message_t *msg, _z_zbuf_t *zbf); #endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_NETWORK_H */ diff --git a/include/zenoh-pico/protocol/definitions/declarations.h b/include/zenoh-pico/protocol/definitions/declarations.h index a08f49a24..c403ca06c 100644 --- a/include/zenoh-pico/protocol/definitions/declarations.h +++ b/include/zenoh-pico/protocol/definitions/declarations.h @@ -124,10 +124,9 @@ _z_declaration_t _z_make_undecl_queryable(uint32_t id, _Z_OPTIONAL _Z_MOVE(_z_ke _z_declaration_t _z_make_decl_token(_Z_MOVE(_z_keyexpr_t) key, uint32_t id); _z_declaration_t _z_make_undecl_token(uint32_t id, _Z_OPTIONAL _Z_MOVE(_z_keyexpr_t) key); + _z_declaration_t _z_make_decl_interest(_Z_MOVE(_z_keyexpr_t) key, uint32_t id); _z_declaration_t _z_make_undecl_interest(uint32_t id, _Z_OPTIONAL _Z_MOVE(_z_keyexpr_t) key); -_z_declaration_t _z_make_final_decl(uint32_t id) { - return (_z_declaration_t){._tag = _Z_FINAL_INTEREST, ._body = {._final_interest = {._id = id}}}; -} +_z_declaration_t _z_make_final_decl(uint32_t id); #endif /* INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_DECLARATIONS_H */ diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index 7e84cc895..00825df88 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -200,7 +200,6 @@ void _z_n_msg_response_clear(_z_n_msg_response_t *msg); typedef struct { _z_declaration_t _decl; _z_timestamp_t _ext_timestamp; - uint16_t _ext_nodeid; _z_n_qos_t _ext_qos; } _z_n_msg_declare_t; static inline void _z_n_msg_declare_clear(_z_n_msg_declare_t *msg) { _z_declaration_clear(&msg->_decl); } @@ -211,12 +210,11 @@ typedef union { _z_n_msg_push_t _push; _z_n_msg_request_t _request; _z_n_msg_response_t _response; - _z_n_msg_response_final_t _response_f; + _z_n_msg_response_final_t _response_final; } _z_network_body_t; typedef struct { + enum { _Z_N_DECLARE, _Z_N_PUSH, _Z_N_REQUEST, _Z_N_RESPONSE, _Z_N_RESPONSE_FINAL } _tag; _z_network_body_t _body; - _z_msg_ext_vec_t _extensions; - uint8_t _header; } _z_network_message_t; void _z_n_msg_clear(_z_network_message_t *m); void _z_n_msg_free(_z_network_message_t **m); diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index 0b6003f52..dbf93607b 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -282,7 +282,9 @@ int8_t _z_decl_subscriber_decode(_z_decl_subscriber_t *decl, _z_zbuf_t *zbf, uin } return _Z_RES_OK; } -int8_t _z_undecl_subscriber_decode(_z_undecl_subscriber_t *decl, _z_zbuf_t *zbf, uint8_t header) {} +int8_t _z_undecl_subscriber_decode(_z_undecl_subscriber_t *decl, _z_zbuf_t *zbf, uint8_t header) { + return _z_undecl_trivial_decode(zbf, &decl->_ext_keyexpr, &decl->_id, header); +} int8_t _z_decl_queryable_decode_extensions(_z_msg_ext_t *extension, void *ctx) { _z_decl_queryable_t *decl = (_z_decl_queryable_t *)ctx; switch (extension->_header) { @@ -338,8 +340,10 @@ int8_t _z_final_interest_decode(_z_final_interest_t *decl, _z_zbuf_t *zbf, uint8 int8_t _z_undecl_interest_decode(_z_undecl_interest_t *decl, _z_zbuf_t *zbf, uint8_t header) { return _z_undecl_trivial_decode(zbf, &decl->_ext_keyexpr, &decl->_id, header); } -int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf) { int8_t ret; + uint8_t header; + _Z_RETURN_IF_ERR(_z_uint8_decode(&header, zbf)); switch (_Z_MID(header)) { case _Z_DECL_KEXPR_MID: { decl->_tag = _Z_DECL_KEXPR; diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 57f48e378..e0eea6640 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -117,7 +117,7 @@ int8_t _z_keyexpr_decode(_z_keyexpr_t *ke, _z_zbuf_t *zbf, _Bool has_suffix) { _Z_DEBUG("Decoding _RESKEY\n"); int8_t ret = _Z_RES_OK; - ret |= _z_zint_decode(&ke->_id, zbf); + ret |= _z_zint16_decode(&ke->_id, zbf); if (has_suffix == true) { char *str = NULL; ret |= _z_str_decode(&str, zbf); diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index 1bd9a94ee..13a5cb421 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -23,7 +23,9 @@ #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/protocol/codec.h" #include "zenoh-pico/protocol/codec/core.h" +#include "zenoh-pico/protocol/codec/declarations.h" #include "zenoh-pico/protocol/codec/ext.h" +#include "zenoh-pico/protocol/codec/network.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/definitions/core.h" #include "zenoh-pico/protocol/definitions/message.h" @@ -36,12 +38,18 @@ /*------------------ Push Message ------------------*/ -int8_t _z_push_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_push_t *msg) { - _Z_DEBUG("Encoding _Z_MID_N_PUSH\n"); - - _Z_RETURN_IF_ERR(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N), &msg->_key)); - +int8_t _z_push_encode(_z_wbuf_t *wbf, const _z_n_msg_push_t *msg) { + uint8_t header = _Z_MID_N_PUSH | (msg->_key._uses_remote_mapping ? _Z_FLAG_N_REQUEST_M : 0); + _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); _Bool has_timestamp_ext = _z_timestamp_check(&msg->_timestamp); + if (has_suffix) { + header |= _Z_FLAG_N_REQUEST_N; + } + if (has_timestamp_ext) { + header |= _Z_FLAG_N_Z; + } + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + _Z_RETURN_IF_ERR(_z_keyexpr_encode(wbf, has_suffix, &msg->_key)); if (msg->_qos._val != _Z_N_QOS_DEFAULT._val) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | 0x01 | (has_timestamp_ext << 7))); @@ -80,11 +88,9 @@ int8_t _z_push_decode_ext_cb(_z_msg_ext_t *extension, void *ctx) { } int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { - _Z_DEBUG("Decoding _Z_MID_N_PUSH\n"); int8_t ret = _Z_RES_OK; - _z_timestamp_reset(&msg->_timestamp); - ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N)); + msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_M); if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_N_Z)) { ret = _z_msg_ext_decode_iter(zbf, _z_push_decode_ext_cb, msg); } @@ -98,12 +104,16 @@ int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { } /*------------------ Request Message ------------------*/ -int8_t _z_request_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_request_t *msg) { +int8_t _z_request_encode(_z_wbuf_t *wbf, const _z_n_msg_request_t *msg) { int8_t ret = _Z_RES_OK; - _Z_DEBUG("Encoding _Z_MID_N_REQUEST\n"); - + uint8_t header = _Z_MID_N_REQUEST | (msg->_key._uses_remote_mapping ? _Z_FLAG_N_REQUEST_M : 0); + _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); + if (has_suffix) { + header |= _Z_FLAG_N_REQUEST_N; + } + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); _Z_EC(_z_zint_encode(wbf, msg->_rid)); - _Z_EC(_z_keyexpr_encode(wbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N), &msg->_key)); + _Z_EC(_z_keyexpr_encode(wbf, has_suffix, &msg->_key)); _z_n_msg_request_exts_t exts = _z_n_msg_request_needed_exts(msg); if (exts.ext_qos) { @@ -187,6 +197,7 @@ int8_t _z_request_decode_extensions(_z_msg_ext_t *extension, void *ctx) { int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_RETURN_IF_ERR(_z_zint_decode(&msg->_rid, zbf)); _Z_RETURN_IF_ERR(_z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N))); + msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_M); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_request_decode_extensions, msg)); } @@ -210,39 +221,48 @@ int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header } /*------------------ Response Message ------------------*/ -int8_t _z_response_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_t *msg) { +int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { int8_t ret = _Z_RES_OK; + uint8_t header = _Z_MID_N_RESPONSE; _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); + _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); + uint8_t n_ext = (has_qos_ext ? 1 : 0) + (has_ts_ext ? 1 : 0) + (has_responder_ext ? 1 : 0); if (msg->_uses_sender_mapping) { - assert(_Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_M)); + _Z_SET_FLAG(header, _Z_FLAG_N_RESPONSE_M); + } + if (msg->_key._suffix != NULL) { + _Z_SET_FLAG(header, _Z_FLAG_N_RESPONSE_N); } + if (n_ext != 0) { + _Z_SET_FLAG(header, _Z_FLAG_Z_Z); + } + + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_request_id)); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_key._id)); if (msg->_key._suffix != NULL) { - assert(_Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N)); _Z_RETURN_IF_ERR(_z_str_encode(wbf, msg->_key._suffix)) } - _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); if (has_qos_ext) { - assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); + n_ext -= 1; uint8_t extheader = _Z_MSG_EXT_ENC_ZINT | 0x01; - if (has_ts_ext || has_responder_ext) { + if (n_ext != 0) { extheader |= _Z_FLAG_Z_Z; } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_ext_qos._val)); } if (has_ts_ext) { - assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); - uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x02 | has_responder_ext; + n_ext -= 1; + uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x02 | (n_ext != 0 ? _Z_FLAG_Z_Z : 0); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); _Z_RETURN_IF_ERR(_z_timestamp_encode(wbf, &msg->_ext_timestamp)); } if (has_responder_ext) { - assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)); - uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x02 | has_responder_ext; + n_ext -= 1; + uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x02 | (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; @@ -317,9 +337,10 @@ int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t head _Z_EC(_z_msg_ext_decode_iter(zbf, _z_response_decode_extension, msg)); } - _Z_EC(_z_uint8_decode(&header, zbf)); + uint8_t inner_header; + _Z_EC(_z_uint8_decode(&inner_header, zbf)); - switch (_Z_MID(header)) { + switch (_Z_MID(inner_header)) { case _Z_MID_Z_REPLY: { _z_reply_decode(&msg->_body._reply, zbf, header); break; @@ -348,26 +369,107 @@ int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t head } /*------------------ Response Final Message ------------------*/ -int8_t _z_response_final_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_final_t *msg) { - (void)(header); +int8_t _z_response_final_encode(_z_wbuf_t *wbf, const _z_n_msg_response_final_t *msg) { int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); - + uint8_t header = _Z_MID_N_RESPONSE_FINAL; + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); _Z_EC(_z_zint_encode(wbf, msg->_request_id)); return ret; } - -int8_t _z_response_final_decode_na(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { (void)(header); - _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); int8_t ret = _Z_RES_OK; - ret |= _z_zint_decode(&msg->_request_id, zbf); - return ret; } -int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_response_final_decode_na(msg, zbf, header); +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) { + header |= _Z_FLAG_N_Z; + } + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + 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))); + _Z_RETURN_IF_ERR(_z_zint_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))); + _Z_RETURN_IF_ERR(_z_timestamp_encode(wbf, &decl->_ext_timestamp)); + } + return _z_declaration_encode(wbf, &decl->_decl); +} +int8_t _z_declare_decode_extensions(_z_msg_ext_t *extension, void *ctx) { + _z_n_msg_declare_t *decl = (_z_n_msg_declare_t *)ctx; + switch (_Z_EXT_FULL_ID(extension->_header)) { + case _Z_MSG_EXT_ENC_ZINT | 0x01: { + decl->_ext_qos._val = extension->_body._zint._val; + break; + } + case _Z_MSG_EXT_ENC_ZBUF | 0x02: { + _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); + return _z_timestamp_decode(&decl->_ext_timestamp, &zbf); + } + default: + if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { + return _z_msg_ext_unknown_error(extension, 0x19); + } + } + return _Z_RES_OK; +} +int8_t _z_declare_decode(_z_n_msg_declare_t *decl, _z_zbuf_t *zbf, uint8_t header) { + if (_Z_HAS_FLAG(header, _Z_FLAG_N_Z)) { + _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_declare_decode_extensions, decl)) + } + return _z_declaration_decode(&decl->_decl, zbf); +} + +int8_t _z_network_message_encode(_z_wbuf_t *wbf, const _z_network_message_t *msg) { + switch (msg->_tag) { + case _Z_N_DECLARE: { + return _z_declare_encode(wbf, &msg->_body._declare); + } break; + case _Z_N_PUSH: { + return _z_push_encode(wbf, &msg->_body._push); + } break; + case _Z_N_REQUEST: { + return _z_request_encode(wbf, &msg->_body._request); + } break; + case _Z_N_RESPONSE: { + return _z_response_encode(wbf, &msg->_body._response); + } break; + case _Z_N_RESPONSE_FINAL: { + return _z_response_final_encode(wbf, &msg->_body._response_final); + } break; + } +} +int8_t _z_network_message_decode(_z_network_message_t *msg, _z_zbuf_t *zbf) { + uint8_t header; + _Z_RETURN_IF_ERR(_z_uint8_decode(&header, zbf)); + switch (_Z_MID(header)) { + case _Z_MID_N_DECLARE: { + return _z_declare_decode(&msg->_body._declare, zbf, header); + } break; + case _Z_MID_N_PUSH: { + return _z_push_decode(&msg->_body._push, zbf, header); + } break; + case _Z_MID_N_REQUEST: { + return _z_request_decode(&msg->_body._request, zbf, header); + } break; + case _Z_MID_N_RESPONSE: { + return _z_response_decode(&msg->_body._response, zbf, header); + } break; + case _Z_MID_N_RESPONSE_FINAL: { + return _z_response_final_decode(&msg->_body._response_final, zbf, header); + } break; + default: + return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } } diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index 01684f69c..600b98bae 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -15,6 +15,7 @@ #include #include "zenoh-pico/protocol/codec/ext.h" +#include "zenoh-pico/protocol/codec/network.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/utils/logging.h" diff --git a/src/protocol/definitions/declarations.c b/src/protocol/definitions/declarations.c index 10ff8addf..a2d13991a 100644 --- a/src/protocol/definitions/declarations.c +++ b/src/protocol/definitions/declarations.c @@ -113,3 +113,6 @@ _z_declaration_t _z_make_decl_interest(_Z_MOVE(_z_keyexpr_t) key, uint32_t id) { ._keyexpr = _z_keyexpr_steal(key), }}}; } +_z_declaration_t _z_make_final_decl(uint32_t id) { + return (_z_declaration_t){._tag = _Z_FINAL_INTEREST, ._body = {._final_interest = {._id = id}}}; +} diff --git a/src/protocol/definitions/network.c b/src/protocol/definitions/network.c index 7496430c9..c64047ee5 100644 --- a/src/protocol/definitions/network.c +++ b/src/protocol/definitions/network.c @@ -86,29 +86,23 @@ void _z_n_msg_response_clear(_z_n_msg_response_t *msg) { } void _z_n_msg_clear(_z_network_message_t *msg) { - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_N_PUSH: + switch (msg->_tag) { + case _Z_N_PUSH: _z_n_msg_push_clear(&msg->_body._push); break; - case _Z_MID_N_REQUEST: + case _Z_N_REQUEST: _z_n_msg_request_clear(&msg->_body._request); break; - case _Z_MID_N_RESPONSE: + case _Z_N_RESPONSE: _z_n_msg_response_clear(&msg->_body._response); break; - case _Z_MID_N_RESPONSE_FINAL: - _z_n_msg_response_final_clear(&msg->_body._response_f); + case _Z_N_RESPONSE_FINAL: + _z_n_msg_response_final_clear(&msg->_body._response_final); break; - case _Z_MID_N_DECLARE: + case _Z_N_DECLARE: _z_n_msg_declare_clear(&msg->_body._declare); break; - default: - _Z_DEBUG("WARNING: Trying to clear network message with unknown ID(%d)\n", mid); - break; } - - _z_msg_ext_vec_clear(&msg->_extensions); } void _z_n_msg_free(_z_network_message_t **msg) { From c3320a2149c866cd98a26b1b6bef1f416cd2efa5 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 3 Jul 2023 13:45:43 +0200 Subject: [PATCH 109/181] pre-merge commit [skip ci] --- include/zenoh-pico/protocol/core.h | 7 ++++-- .../zenoh-pico/protocol/definitions/network.h | 4 ++++ include/zenoh-pico/protocol/keyexpr.h | 2 +- include/zenoh-pico/protocol/msg.h | 9 -------- src/net/memory.c | 2 +- src/protocol/codec/declarations.c | 6 ++--- src/protocol/core.c | 6 ++++- src/protocol/definitions/network.c | 20 ++++++++++++++++- src/protocol/msg.c | 22 ++----------------- src/session/queryable.c | 4 ++-- src/session/utils.c | 6 ++--- 11 files changed, 45 insertions(+), 43 deletions(-) diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 16aaa8c8c..ada0b37ae 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -23,6 +23,7 @@ #include "zenoh-pico/collections/element.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" +#include "zenoh-pico/system/platform.h" #define _Z_OPTIONAL #define _Z_MOVE(x) x * @@ -70,7 +71,8 @@ typedef struct { } _z_timestamp_t; _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp); -void _z_timestamp_reset(_z_timestamp_t *tstamp); +_z_timestamp_t _z_timestamp_null(); +void _z_timestamp_clear(_z_timestamp_t *tstamp); _Bool _z_timestamp_check(const _z_timestamp_t *stamp); /** @@ -83,7 +85,7 @@ _Bool _z_timestamp_check(const _z_timestamp_t *stamp); typedef struct { uint16_t _id; _Bool _uses_remote_mapping; - _Bool is_alloc; + _Bool _owns_suffix; char *_suffix; } _z_keyexpr_t; static inline _Bool _z_keyexpr_has_suffix(_z_keyexpr_t ke) { return (ke._suffix != NULL) && (ke._suffix[0] != 0); } @@ -178,5 +180,6 @@ typedef struct { uint32_t _entity_id; uint32_t _source_sn; } _z_source_info_t; +_z_source_info_t _z_source_info_null(); #endif /* ZENOH_PICO_PROTOCOL_CORE_H */ diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index 00825df88..136d9542e 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -22,6 +22,7 @@ #include "zenoh-pico/protocol/definitions/declarations.h" #include "zenoh-pico/protocol/definitions/message.h" #include "zenoh-pico/protocol/ext.h" +#include "zenoh-pico/protocol/keyexpr.h" /* Network Messages */ #define _Z_MID_N_OAM 0x1f #define _Z_MID_N_DECLARE 0x1e @@ -220,5 +221,8 @@ void _z_n_msg_clear(_z_network_message_t *m); void _z_n_msg_free(_z_network_message_t **m); _Z_ELEM_DEFINE(_z_network_message, _z_network_message_t, _z_noop_size, _z_n_msg_clear, _z_noop_copy) _Z_VEC_DEFINE(_z_network_message, _z_network_message_t) +typedef _z_network_message_t _z_zenoh_message_t; + +_z_network_message_t _z_msg_make_pull(_Z_MOVE(_z_keyexpr_t) key, _z_zint_t pull_id); #endif /* INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_NETWORK_H */ diff --git a/include/zenoh-pico/protocol/keyexpr.h b/include/zenoh-pico/protocol/keyexpr.h index 3c03e0429..7113303d2 100644 --- a/include/zenoh-pico/protocol/keyexpr.h +++ b/include/zenoh-pico/protocol/keyexpr.h @@ -29,7 +29,7 @@ void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src); _z_keyexpr_t _z_keyexpr_duplicate(const _z_keyexpr_t *src); _z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src); _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp); -void _z_timestamp_reset(_z_timestamp_t *tstamp); +void _z_timestamp_clear(_z_timestamp_t *tstamp); void _z_keyexpr_clear(_z_keyexpr_t *rk); void _z_keyexpr_free(_z_keyexpr_t **rk); diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h index a5a629f72..9a1242e03 100644 --- a/include/zenoh-pico/protocol/msg.h +++ b/include/zenoh-pico/protocol/msg.h @@ -216,17 +216,8 @@ _z_network_message_t _z_n_msg_make_push(_z_keyexpr_t key, _z_push_body_t body, _ _z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid); /*------------------ Builders ------------------*/ -_z_declaration_t _z_msg_make_declaration_resource(_z_zint_t id, _z_keyexpr_t key); -_z_declaration_t _z_msg_make_declaration_forget_resource(_z_zint_t rid); -_z_declaration_t _z_msg_make_declaration_publisher(_z_keyexpr_t key); -_z_declaration_t _z_msg_make_declaration_forget_publisher(_z_keyexpr_t key); -_z_declaration_t _z_msg_make_declaration_subscriber(_z_keyexpr_t key, _z_subinfo_t subinfo); -_z_declaration_t _z_msg_make_declaration_forget_subscriber(_z_keyexpr_t key); -_z_declaration_t _z_msg_make_declaration_queryable(_z_keyexpr_t key, _z_zint_t complete, _z_zint_t distance); -_z_declaration_t _z_msg_make_declaration_forget_queryable(_z_keyexpr_t key); // _z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool can_be_dropped); _z_network_message_t _z_msg_make_unit(_Bool can_be_dropped); -_z_network_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id, _z_zint_t max_samples, _Bool is_final); _z_network_message_t _z_msg_make_query(_z_keyexpr_t key, _z_bytes_t parameters, _z_zint_t qid, z_consolidation_mode_t consolidation, _z_value_t value); // _z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool diff --git a/src/net/memory.c b/src/net/memory.c index 33f7582ec..1b90e7f55 100644 --- a/src/net/memory.c +++ b/src/net/memory.c @@ -96,4 +96,4 @@ void _z_value_free(_z_value_t **value) { z_free(ptr); *value = NULL; } -} +} \ No newline at end of file diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index dbf93607b..4c48b387a 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -217,7 +217,7 @@ int8_t _z_undecl_decode_extensions(_z_msg_ext_t *extension, void *ctx) { if (!ke->_suffix) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - ke->is_alloc = true; + ke->owns_suffix = true; _z_zbuf_read_bytes(zbf, (uint8_t *)ke->_suffix, 0, len); ke->_suffix[len] = 0; } @@ -250,12 +250,12 @@ int8_t _z_decl_commons_decode(_z_zbuf_t *zbf, uint8_t header, _Bool *has_extensi if (ke->_suffix == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - ke->is_alloc = true; + ke->owns_suffix = true; _z_zbuf_read_bytes(zbf, (uint8_t *)ke->_suffix, 0, len); ke->_suffix[len] = 0; } else { ke->_suffix = ""; - ke->is_alloc = false; + ke->owns_suffix = false; } return _Z_RES_OK; } diff --git a/src/protocol/core.c b/src/protocol/core.c index d014d7473..144edff52 100644 --- a/src/protocol/core.c +++ b/src/protocol/core.c @@ -36,4 +36,8 @@ _z_id_t _z_id_empty() { 0, 0, }}; -} \ No newline at end of file +} +_z_source_info_t _z_source_info_null() { + 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}; } diff --git a/src/protocol/definitions/network.c b/src/protocol/definitions/network.c index c64047ee5..0885b4a84 100644 --- a/src/protocol/definitions/network.c +++ b/src/protocol/definitions/network.c @@ -114,4 +114,22 @@ void _z_n_msg_free(_z_network_message_t **msg) { z_free(ptr); *msg = NULL; } -} \ No newline at end of file +} +_z_network_message_t _z_msg_make_pull(_Z_MOVE(_z_keyexpr_t) key, _z_zint_t pull_id) { + _z_network_message_t ret = {._tag = _Z_N_REQUEST, + ._body = {._request = { + ._rid = pull_id, + ._key = _z_keyexpr_steal(key), + ._tag = _Z_REQUEST_PULL, + ._body = + { + .pull = {._ext_source_info = _z_source_info_null()}, + }, + .ext_budget = 0, + .ext_qos = _Z_N_QOS_DEFAULT, + .ext_target = Z_QUERY_TARGET_BEST_MATCHING, + .ext_tstamp = _z_timestamp_null(), + .ext_timeout_ms = 0, + }}}; + return ret; +} diff --git a/src/protocol/msg.c b/src/protocol/msg.c index ddb39352e..21e7bea94 100644 --- a/src/protocol/msg.c +++ b/src/protocol/msg.c @@ -24,24 +24,6 @@ #include "zenoh-pico/session/queryable.h" #include "zenoh-pico/utils/logging.h" -/*=============================*/ -/* Message fields */ -/*=============================*/ -_z_reply_context_t *_z_msg_make_reply_context(_z_zint_t qid, _z_id_t replier_id, _Bool is_final) { - _z_reply_context_t *rctx = (_z_reply_context_t *)z_malloc(sizeof(_z_reply_context_t)); - if (rctx != NULL) { - rctx->_qid = qid; - rctx->_replier_id = replier_id; - - rctx->_header = 0; - if (is_final == true) { - _Z_SET_FLAG(rctx->_header, _Z_FLAG_Z_F); - } - } - - return rctx; -} - /*------------------ Payload field ------------------*/ void _z_payload_clear(_z_bytes_t *p) { _z_bytes_clear(p); } @@ -51,9 +33,9 @@ void _z_timestamp_clear(_z_timestamp_t *ts) {} /*------------------ ResKey Field ------------------*/ void _z_keyexpr_clear(_z_keyexpr_t *rk) { rk->_id = 0; - if (rk->_suffix != NULL && rk->is_alloc) { + if (rk->_suffix != NULL && rk->owns_suffix) { _z_str_clear((char *)rk->_suffix); - rk->is_alloc = false; + rk->owns_suffix = false; } } diff --git a/src/session/queryable.c b/src/session/queryable.c index 953cdbd3a..acd3caa96 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -157,8 +157,8 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query) { q._qid = query->_qid; q._key = key; q._parameters = query->_parameters; - q._value.encoding = query->_info._encoding; - q._value.payload = query->_payload; + q._value.encoding = query->_value.encoding; + q._value.payload = query->_value.payload; q._anyke = (strstr(q._parameters, Z_SELECTOR_QUERY_MATCH) == NULL) ? false : true; _z_questionable_sptr_list_t *xs = qles; while (xs != NULL) { diff --git a/src/session/utils.c b/src/session/utils.c index e76454591..1fd35b366 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -27,7 +27,7 @@ void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src) { dst->_id = src->_id; dst->_suffix = src->_suffix ? _z_str_clone(src->_suffix) : NULL; - dst->is_alloc = true; + dst->owns_suffix = true; } _z_keyexpr_t _z_keyexpr_duplicate(const _z_keyexpr_t *src) { @@ -38,7 +38,7 @@ _z_keyexpr_t _z_keyexpr_duplicate(const _z_keyexpr_t *src) { _z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src) { _z_keyexpr_t stolen = *src; - src->is_alloc = false; + src->owns_suffix = false; src->_id = 0; src->_suffix = NULL; return stolen; @@ -51,7 +51,7 @@ _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp) { return ts; } -void _z_timestamp_reset(_z_timestamp_t *tstamp) { +void _z_timestamp_clear(_z_timestamp_t *tstamp) { memset(&tstamp->id, 0, sizeof(_z_id_t)); tstamp->time = 0; } From 8d12c848529e893d8a32c43213d505cf4e48550c Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Tue, 4 Jul 2023 17:48:56 +0200 Subject: [PATCH 110/181] pre-cmake [skip ci] --- include/zenoh-pico/collections/bytes.h | 1 + include/zenoh-pico/net/primitives.h | 15 +- include/zenoh-pico/net/query.h | 2 +- include/zenoh-pico/net/session.h | 19 +- include/zenoh-pico/protocol/codec/core.h | 3 +- include/zenoh-pico/protocol/core.h | 7 + .../zenoh-pico/protocol/definitions/network.h | 34 +- include/zenoh-pico/protocol/keyexpr.h | 3 +- include/zenoh-pico/protocol/msg.h | 226 --------- include/zenoh-pico/session/queryable.h | 2 +- include/zenoh-pico/session/resource.h | 16 +- include/zenoh-pico/session/session.h | 3 +- include/zenoh-pico/session/utils.h | 2 - include/zenoh-pico/transport/link/tx.h | 4 +- include/zenoh-pico/transport/utils.h | 2 +- src/api/api.c | 21 +- src/collections/bytes.c | 5 + src/net/primitives.c | 149 +++--- src/net/publish.c | 2 - src/net/resource.c | 30 +- src/protocol/codec.c | 16 +- src/protocol/codec/declarations.c | 7 +- src/protocol/codec/ext.c | 6 +- src/protocol/codec/message.c | 66 +-- src/protocol/codec/network.c | 23 +- src/protocol/codec/transport.c | 30 +- src/protocol/core.c | 8 + src/protocol/definitions/message.c | 2 - src/protocol/definitions/network.c | 133 ++++- src/protocol/keyexpr.c | 50 +- src/protocol/msg.c | 453 ------------------ src/session/query.c | 20 +- src/session/queryable.c | 27 +- src/session/resource.c | 20 +- src/session/rx.c | 85 ++-- src/session/scout.c | 2 +- src/session/subscription.c | 2 +- src/session/tx.c | 24 +- src/session/utils.c | 20 - src/transport/common/rx.c | 1 + src/transport/common/tx.c | 2 +- src/transport/multicast/link/rx.c | 7 +- src/transport/multicast/link/task/read.c | 1 + src/transport/multicast/link/tx.c | 60 +-- src/transport/unicast/link/rx.c | 4 +- src/transport/unicast/link/task/read.c | 1 + src/transport/unicast/link/tx.c | 65 +-- tests/z_msgcodec_test.c | 161 +------ 48 files changed, 556 insertions(+), 1286 deletions(-) delete mode 100644 include/zenoh-pico/protocol/msg.h delete mode 100644 src/protocol/msg.c diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index 8d04e7914..acc8503aa 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -37,6 +37,7 @@ _z_bytes_t _z_bytes_empty(void); int8_t _z_bytes_init(_z_bytes_t *bs, size_t capacity); _z_bytes_t _z_bytes_make(size_t capacity); _z_bytes_t _z_bytes_wrap(const uint8_t *bs, size_t len); +_z_bytes_t _z_bytes_steal(_z_bytes_t *b); void _z_bytes_copy(_z_bytes_t *dst, const _z_bytes_t *src); _z_bytes_t _z_bytes_duplicate(const _z_bytes_t *src); diff --git a/include/zenoh-pico/net/primitives.h b/include/zenoh-pico/net/primitives.h index c9fc92834..1ebcf43aa 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -14,6 +14,9 @@ #ifndef ZENOH_PICO_PRIMITIVES_NETAPI_H #define ZENOH_PICO_PRIMITIVES_NETAPI_H +#include + +#include "zenoh-pico/api/constants.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/net/publish.h" #include "zenoh-pico/net/query.h" @@ -52,7 +55,7 @@ void _z_scout(const z_what_t what, const _z_id_t zid, const char *locator, const * Returns: * A numerical id of the declared resource. */ -_z_zint_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr); +uint16_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr); /** * Associate a numerical id with the given resource key. @@ -66,7 +69,7 @@ _z_zint_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr); * Returns: * 0 if success, or a negative value identifying the error. */ -int8_t _z_undeclare_resource(_z_session_t *zn, const _z_zint_t rid); +int8_t _z_undeclare_resource(_z_session_t *zn, uint16_t rid); /** * Declare a :c:type:`_z_publisher_t` for the given resource key. @@ -173,7 +176,8 @@ int8_t _z_undeclare_queryable(_z_queryable_t *qle); * ``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); + const _z_encoding_t encoding, const z_sample_kind_t kind, const z_congestion_control_t cong_ctrl, + z_priority_t priority); /** * Pull data for a pull mode :c:type:`_z_subscriber_t`. The pulled data will be provided @@ -217,9 +221,8 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, * Parameters: * query: The query to reply to. The caller keeps its ownership. * key: The resource key of this reply. The caller keeps the ownership. - * payload: The value of this reply. - * len: The length of the value of this reply. + * payload: The value of this reply, the caller keeps ownership. */ -int8_t _z_send_reply(const z_query_t *query, _z_keyexpr_t keyexpr, const uint8_t *payload, const size_t len); +int8_t _z_send_reply(const z_query_t *query, const _z_keyexpr_t keyexpr, const _z_value_t payload); #endif /* ZENOH_PICO_PRIMITIVES_NETAPI_H */ diff --git a/include/zenoh-pico/net/query.h b/include/zenoh-pico/net/query.h index 104b98432..3815860dc 100644 --- a/include/zenoh-pico/net/query.h +++ b/include/zenoh-pico/net/query.h @@ -23,7 +23,7 @@ typedef struct { _z_value_t _value; _z_keyexpr_t _key; - _z_zint_t _qid; + _z_zint_t _rid; void *_zn; // FIXME: _z_session_t *zn; char *_parameters; _Bool _anyke; diff --git a/include/zenoh-pico/net/session.h b/include/zenoh-pico/net/session.h index 7804d8bbc..64e8d1ba6 100644 --- a/include/zenoh-pico/net/session.h +++ b/include/zenoh-pico/net/session.h @@ -15,10 +15,26 @@ #ifndef INCLUDE_ZENOH_PICO_NET_SESSION_H #define INCLUDE_ZENOH_PICO_NET_SESSION_H +#include + +#include "zenoh-pico/collections/element.h" +#include "zenoh-pico/collections/list.h" #include "zenoh-pico/config.h" +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/session/session.h" #include "zenoh-pico/utils/config.h" +typedef struct { + _z_resource_list_t *resources; + _z_subscription_sptr_list_t *subscriptions; +} _z_remote_t; +static void _z_remote_drop(_z_remote_t *remote) { + _z_resource_list_free(&remote->resources); + _z_subscription_sptr_list_free(&remote->subscriptions); +} +_Z_ELEM_DEFINE(_z_remote, _z_remote_t, _z_noop_size, _z_remote_drop, _z_noop_copy) +_Z_LIST_DEFINE(_z_remote, _z_remote_t) + /** * A zenoh-net session. */ @@ -34,10 +50,11 @@ typedef struct { _z_id_t _local_zid; // Session counters - _z_zint_t _resource_id; + uint16_t _resource_id; _z_zint_t _entity_id; _z_zint_t _pull_id; _z_zint_t _query_id; + _z_zint_t _interest_id; // Session declarations _z_resource_list_t *_local_resources; diff --git a/include/zenoh-pico/protocol/codec/core.h b/include/zenoh-pico/protocol/codec/core.h index a54a61f70..241ce5712 100644 --- a/include/zenoh-pico/protocol/codec/core.h +++ b/include/zenoh-pico/protocol/codec/core.h @@ -22,14 +22,13 @@ #include "zenoh-pico/utils/config.h" #include "zenoh-pico/utils/result.h" -#define _Z_EC(expr) \ +#define _Z_RETURN_IF_ERR(expr) \ { \ int8_t __res = expr; \ if (__res != _Z_RES_OK) { \ return __res; \ } \ } -#define _Z_RETURN_IF_ERR(expr) _Z_EC(expr) /*------------------ Internal Zenoh-net Macros ------------------*/ int8_t _z_encoding_prefix_encode(_z_wbuf_t *wbf, z_encoding_prefix_t en); diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index ada0b37ae..2500fb73b 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -120,6 +120,8 @@ typedef struct { _z_bytes_t payload; _z_encoding_t encoding; } _z_value_t; +_z_value_t _z_value_null(void); +_z_value_t _z_value_steal(_z_value_t *value); void _z_value_clear(_z_value_t *src); void _z_value_free(_z_value_t **hello); @@ -182,4 +184,9 @@ typedef struct { } _z_source_info_t; _z_source_info_t _z_source_info_null(); +typedef struct { + uint32_t _request_id; + uint32_t _entity_id; +} _z_reply_context_t; + #endif /* ZENOH_PICO_PROTOCOL_CORE_H */ diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index 136d9542e..ffbdbee63 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -18,6 +18,7 @@ #include #include "zenoh-pico/api/constants.h" +#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/definitions/declarations.h" #include "zenoh-pico/protocol/definitions/message.h" @@ -100,7 +101,7 @@ typedef struct { uint32_t ext_timeout_ms; enum { _Z_REQUEST_QUERY, _Z_REQUEST_PUT, _Z_REQUEST_DEL, _Z_REQUEST_PULL } _tag; union { - _z_msg_query_t query; + _z_msg_query_t _query; _z_msg_put_t put; _z_msg_del_t del; _z_msg_pull_t pull; @@ -122,8 +123,10 @@ typedef struct { union { _z_msg_del_t _del; _z_msg_put_t _put; - } _union; + } _body; } _z_push_body_t; +_z_push_body_t _z_push_body_null(); +_z_push_body_t _z_push_body_steal(_z_push_body_t *msg); void _z_push_body_clear(_z_push_body_t *msg); /*------------------ Response Final Message ------------------*/ @@ -180,14 +183,13 @@ typedef struct { _z_id_t _zid; uint32_t _eid; } _ext_responder; - _Bool _uses_sender_mapping; enum { _Z_RESPONSE_BODY_REPLY, _Z_RESPONSE_BODY_ERR, _Z_RESPONSE_BODY_ACK, _Z_RESPONSE_BODY_PUT, _Z_RESPONSE_BODY_DEL, - } _body_tag; + } _tag; union { _z_msg_reply_t _reply; _z_msg_err_t _err; @@ -217,12 +219,32 @@ typedef struct { enum { _Z_N_DECLARE, _Z_N_PUSH, _Z_N_REQUEST, _Z_N_RESPONSE, _Z_N_RESPONSE_FINAL } _tag; _z_network_body_t _body; } _z_network_message_t; +typedef _z_network_message_t _z_zenoh_message_t; void _z_n_msg_clear(_z_network_message_t *m); void _z_n_msg_free(_z_network_message_t **m); +inline static void _z_msg_clear(_z_zenoh_message_t *msg) { _z_n_msg_clear(msg); } +inline static void _z_msg_free(_z_zenoh_message_t **msg) { _z_n_msg_free(msg); } _Z_ELEM_DEFINE(_z_network_message, _z_network_message_t, _z_noop_size, _z_n_msg_clear, _z_noop_copy) _Z_VEC_DEFINE(_z_network_message, _z_network_message_t) -typedef _z_network_message_t _z_zenoh_message_t; -_z_network_message_t _z_msg_make_pull(_Z_MOVE(_z_keyexpr_t) key, _z_zint_t pull_id); +_z_network_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id); +_z_network_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes_t) parameters, _z_zint_t qid, + z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value); +_z_network_message_t _z_n_msg_make_reply(_z_zint_t rid, _Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_value_t) value); +_z_network_message_t _z_n_msg_make_ack(_z_zint_t rid, _Z_MOVE(_z_keyexpr_t) key) { + return (_z_network_message_t){ + ._tag = _Z_N_RESPONSE, + ._body._response = + { + ._tag = _Z_RESPONSE_BODY_ACK, + ._request_id = rid, + ._key = _z_keyexpr_steal(key), + ._body._ack = {._timestamp = _z_timestamp_null(), ._ext_source_info = _z_source_info_null()}, + }, + }; +} +_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); +_z_network_message_t _z_n_msg_make_push(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_push_body_t) body); #endif /* INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_NETWORK_H */ diff --git a/include/zenoh-pico/protocol/keyexpr.h b/include/zenoh-pico/protocol/keyexpr.h index 7113303d2..b12694d57 100644 --- a/include/zenoh-pico/protocol/keyexpr.h +++ b/include/zenoh-pico/protocol/keyexpr.h @@ -26,7 +26,8 @@ _Bool _z_keyexpr_intersects(const char *lstart, const size_t llen, const char *r /*------------------ clone/Copy/Free helpers ------------------*/ void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src); -_z_keyexpr_t _z_keyexpr_duplicate(const _z_keyexpr_t *src); +_z_keyexpr_t _z_keyexpr_duplicate(_z_keyexpr_t src); +_z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src); _z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src); _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp); void _z_timestamp_clear(_z_timestamp_t *tstamp); diff --git a/include/zenoh-pico/protocol/msg.h b/include/zenoh-pico/protocol/msg.h deleted file mode 100644 index 9a1242e03..000000000 --- a/include/zenoh-pico/protocol/msg.h +++ /dev/null @@ -1,226 +0,0 @@ -// -// 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 INCLUDE_ZENOH_PICO_PROTOCOL_MSG_H -#define INCLUDE_ZENOH_PICO_PROTOCOL_MSG_H - -#include -#include -#include - -#include "zenoh-pico/api/constants.h" -#include "zenoh-pico/collections/array.h" -#include "zenoh-pico/collections/bytes.h" -#include "zenoh-pico/collections/element.h" -#include "zenoh-pico/collections/string.h" -#include "zenoh-pico/link/endpoint.h" -#include "zenoh-pico/net/query.h" -#include "zenoh-pico/protocol/core.h" -#include "zenoh-pico/protocol/definitions/network.h" -#include "zenoh-pico/protocol/definitions/transport.h" -#include "zenoh-pico/protocol/ext.h" -#include "zenoh-pico/protocol/keyexpr.h" - -/*------------------ Locators Field ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// ~ Num of Locs ~ -// +---------------+ -// ~ [Locator] ~ -// +---------------+ -// -// NOTE: Locators is an array of strings and are encoded as such - -/*=============================*/ -/* Message decorators */ -/*=============================*/ - -// -- Priority decorator -// -// The Priority is a message decorator containing -// informations related to the priority of the frame/zenoh message. -// -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// | ID | Prio | -// +-+-+-+---------+ -// -// WARNING: zenoh-pico does not support priorities and QoS. - -/*=============================*/ -/* Zenoh Messages */ -/*=============================*/ -/*------------------ ResKey Field ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// ~ RID ~ -// +---------------+ -// ~ Suffix ~ if K==1 -// +---------------+ -// - -/*------------------ Resource Declaration ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |K|X|X| RES | -// +---------------+ -// ~ RID ~ -// +---------------+ -// ~ ResKey ~ if K==1 then keyexpr is string -// +---------------+ -// -typedef struct { - _z_keyexpr_t _key; - _z_zint_t _id; -} _z_res_decl_t; -void _z_declaration_clear_resource(_z_res_decl_t *dcl); - -/*------------------ Forget Resource Declaration ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |X|X|X| F_RES | -// +---------------+ -// ~ RID ~ -// +---------------+ -// -typedef struct { - _z_zint_t _rid; -} _z_forget_res_decl_t; -void _z_declaration_clear_forget_resource(_z_forget_res_decl_t *dcl); - -/*------------------ Publisher Declaration ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |K|X|X| PUB | -// +---------------+ -// ~ ResKey ~ if K==1 then keyexpr is string -// +---------------+ -// -typedef struct { - _z_keyexpr_t _key; -} _z_pub_decl_t; -void _z_declaration_clear_publisher(_z_pub_decl_t *dcl); - -/*------------------ Forget Publisher Declaration ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |K|X|X| F_PUB | -// +---------------+ -// ~ ResKey ~ if K==1 then keyexpr is string -// +---------------+ -// -typedef struct { - _z_keyexpr_t _key; -} _z_forget_pub_decl_t; -void _z_declaration_clear_forget_publisher(_z_forget_pub_decl_t *dcl); - -/*------------------ SubInfo Field ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+---------+ -// |P|X|X| SM_ID | -// +---------------+ -// ~ Period ~ if P==1 -// +---------------+ -// -void _z_subinfo_clear(_z_subinfo_t *si); - -/*------------------ Subscriber Declaration ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |K|S|R| SUB | -// +---------------+ -// ~ ResKey ~ if K==1 then keyexpr is string -// +---------------+ -// ~ SubInfo ~ if S==1. Otherwise: SubMode=Push -// +---------------+ -// -// - if R==1 then the subscription is reliable, best-effort otherwise. -// -typedef struct { - _z_keyexpr_t _key; - _z_subinfo_t _subinfo; -} _z_sub_decl_t; -void _z_declaration_clear_subscriber(_z_sub_decl_t *dcl); - -/*------------------ Forget Subscriber Message ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |K|X|X| F_SUB | -// +---------------+ -// ~ ResKey ~ if K==1 then keyexpr is string -// +---------------+ -// -typedef struct { - _z_keyexpr_t _key; -} _z_forget_sub_decl_t; -void _z_declaration_clear_forget_subscriber(_z_forget_sub_decl_t *dcl); - -/*------------------ Queryable Declaration ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |K|Q|X| QABLE | -// +---------------+ -// ~ ResKey ~ if K==1 then keyexpr is string -// +---------------+ -// ~ QablInfo ~ if Q==1 -// +---------------+ -// -typedef struct { - _z_keyexpr_t _key; - _z_zint_t _complete; - _z_zint_t _distance; -} _z_qle_decl_t; -void _z_declaration_clear_queryable(_z_qle_decl_t *dcl); - -/*------------------ Forget Queryable Declaration ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+-+-+-+-+-+ -// |K|X|X| F_QABLE | -// +---------------+ -// ~ ResKey ~ if K==1 then keyexpr is string -// +---------------+ -// -typedef struct { - _z_keyexpr_t _key; -} _z_forget_qle_decl_t; -void _z_declaration_clear_forget_queryable(_z_forget_qle_decl_t *dcl); - -/*------------------ Timestamp Field ------------------*/ -// 7 6 5 4 3 2 1 0 -// +-+-+-+---------+ -// ~ Time ~ Encoded as _z_zint_t -// +---------------+ -// ~ ID ~ -// +---------------+ -// -void _z_timestamp_clear(_z_timestamp_t *ts); - -/*------------------ Builders ------------------*/ -_z_network_message_t _z_n_msg_make_declare(_z_declaration_t declarations); -_z_network_message_t _z_n_msg_make_push(_z_keyexpr_t key, _z_push_body_t body, _Bool is_remote_mapping); -// _z_network_message_t _z_n_msg_make_request(_z_zint_t rid, _z_keyexpr_t key, _z_request_body_t body, -// _Bool is_remote_mapping); -// _z_network_message_t _z_n_msg_make_response(_z_zint_t rid, _z_keyexpr_t key, _z_response_body_t body, -// _Bool is_remote_mapping); -_z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid); - -/*------------------ Builders ------------------*/ -// _z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool can_be_dropped); -_z_network_message_t _z_msg_make_unit(_Bool can_be_dropped); -_z_network_message_t _z_msg_make_query(_z_keyexpr_t key, _z_bytes_t parameters, _z_zint_t qid, - z_consolidation_mode_t consolidation, _z_value_t value); -// _z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool -// can_be_dropped); - -#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_MSG_H */ diff --git a/include/zenoh-pico/session/queryable.h b/include/zenoh-pico/session/queryable.h index a86c3248a..d6218b262 100644 --- a/include/zenoh-pico/session/queryable.h +++ b/include/zenoh-pico/session/queryable.h @@ -27,7 +27,7 @@ _z_questionable_sptr_t *_z_get_questionable_by_id(_z_session_t *zn, const _z_zin _z_questionable_sptr_list_t *_z_get_questionable_by_key(_z_session_t *zn, const _z_keyexpr_t key); _z_questionable_sptr_t *_z_register_questionable(_z_session_t *zn, _z_questionable_t *q); -int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query); +int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, const _z_keyexpr_t q_key, uint32_t qid); void _z_unregister_questionable(_z_session_t *zn, _z_questionable_sptr_t *q); void _z_flush_questionables(_z_session_t *zn); diff --git a/include/zenoh-pico/session/resource.h b/include/zenoh-pico/session/resource.h index 6a64eb057..85b26c2dc 100644 --- a/include/zenoh-pico/session/resource.h +++ b/include/zenoh-pico/session/resource.h @@ -12,8 +12,10 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_SESSION_RESOURCE_H -#define ZENOH_PICO_SESSION_RESOURCE_H +#ifndef INCLUDE_ZENOH_PICO_SESSION_RESOURCE_H +#define INCLUDE_ZENOH_PICO_SESSION_RESOURCE_H + +#include #include "zenoh-pico/net/session.h" @@ -21,16 +23,16 @@ _z_zint_t _z_get_entity_id(_z_session_t *zn); /*------------------ Resource ------------------*/ -_z_zint_t _z_get_resource_id(_z_session_t *zn); +uint16_t _z_get_resource_id(_z_session_t *zn); _z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint8_t is_local, _z_zint_t rid); _z_resource_t *_z_get_resource_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr); -_z_keyexpr_t _z_get_expanded_key_from_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr); +_z_keyexpr_t _z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr); int8_t _z_register_resource(_z_session_t *zn, uint8_t is_local, _z_resource_t *res); void _z_unregister_resource(_z_session_t *zn, uint8_t is_local, _z_resource_t *res); void _z_flush_resources(_z_session_t *zn); -_z_keyexpr_t __unsafe_z_get_expanded_key_from_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr); +_z_keyexpr_t __unsafe_z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr); _z_resource_t *__unsafe_z_get_resource_by_id(_z_session_t *zn, uint8_t is_local, _z_zint_t id); -_z_resource_t *__unsafe_z_get_resource_matching_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr); +_z_resource_t *__unsafe_z_get_resource_matching_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr); -#endif /* ZENOH_PICO_SESSION_RESOURCE_H */ +#endif /* INCLUDE_ZENOH_PICO_SESSION_RESOURCE_H */ diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index 89e187d3c..45830d762 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -16,6 +16,7 @@ #define INCLUDE_ZENOH_PICO_SESSION_SESSION_H #include +#include #include "zenoh-pico/collections/element.h" #include "zenoh-pico/collections/list.h" @@ -71,7 +72,7 @@ void _z_reply_free(_z_reply_t **hello); typedef struct { _z_keyexpr_t _key; - _z_zint_t _id; + uint16_t _id; } _z_resource_t; _Bool _z_resource_eq(const _z_resource_t *one, const _z_resource_t *two); diff --git a/include/zenoh-pico/session/utils.h b/include/zenoh-pico/session/utils.h index 1af0b7114..8cde42aa6 100644 --- a/include/zenoh-pico/session/utils.h +++ b/include/zenoh-pico/session/utils.h @@ -31,8 +31,6 @@ void _z_session_clear(_z_session_t *zn); void _z_session_free(_z_session_t **zn); int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *z_msg); -int8_t _z_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reliability_t reliability, - z_congestion_control_t cong_ctrl); int8_t _z_send_n_msg(_z_session_t *zn, _z_network_message_t *n_msg, z_reliability_t reliability, z_congestion_control_t cong_ctrl); diff --git a/include/zenoh-pico/transport/link/tx.h b/include/zenoh-pico/transport/link/tx.h index 833b496ab..2f119dd0b 100644 --- a/include/zenoh-pico/transport/link/tx.h +++ b/include/zenoh-pico/transport/link/tx.h @@ -29,9 +29,9 @@ int8_t _z_unicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reli int8_t _z_multicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reliability_t reliability, z_congestion_control_t cong_ctrl); -int8_t _z_unicast_send_n_msg(_z_session_t *zn, _z_network_message_t *z_msg, z_reliability_t reliability, +int8_t _z_unicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *z_msg, z_reliability_t reliability, z_congestion_control_t cong_ctrl); -int8_t _z_multicast_send_n_msg(_z_session_t *zn, _z_network_message_t *z_msg, z_reliability_t reliability, +int8_t _z_multicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *z_msg, z_reliability_t reliability, z_congestion_control_t cong_ctrl); int8_t _z_send_t_msg(_z_transport_t *zt, const _z_transport_message_t *t_msg); diff --git a/include/zenoh-pico/transport/utils.h b/include/zenoh-pico/transport/utils.h index b2de33c5c..a53267680 100644 --- a/include/zenoh-pico/transport/utils.h +++ b/include/zenoh-pico/transport/utils.h @@ -18,7 +18,7 @@ #include #include "zenoh-pico/protocol/core.h" -#include "zenoh-pico/protocol/msg.h" +#include "zenoh-pico/protocol/definitions/transport.h" /*------------------ SN helpers ------------------*/ _z_zint_t _z_sn_max(uint8_t bits); diff --git a/src/api/api.c b/src/api/api.c index e818d0c31..673885989 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -75,7 +75,7 @@ z_bytes_t z_keyexpr_as_bytes(z_keyexpr_t keyexpr) { z_owned_str_t zp_keyexpr_resolve(z_session_t zs, z_keyexpr_t keyexpr) { z_owned_str_t ret = {._value = NULL}; - _z_keyexpr_t ekey = _z_get_expanded_key_from_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); + _z_keyexpr_t ekey = _z_get_expanded_key_from_key(zs._val, &keyexpr); ret._value = (char *)ekey._suffix; // ekey will be out of scope so // - suffix can be safely casted as non-const // - suffix does not need to be copied @@ -621,7 +621,7 @@ int8_t z_put(z_session_t zs, z_keyexpr_t keyexpr, const uint8_t *payload, z_zint opt.priority = options->priority; } ret = _z_write(zs._val, keyexpr, (const uint8_t *)payload, payload_len, opt.encoding, Z_SAMPLE_KIND_PUT, - opt.congestion_control); + opt.congestion_control, opt.priority); return ret; } @@ -634,7 +634,8 @@ int8_t z_delete(z_session_t zs, z_keyexpr_t keyexpr, const z_delete_options_t *o opt.congestion_control = options->congestion_control; opt.priority = options->priority; } - ret = _z_write(zs._val, keyexpr, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, opt.congestion_control); + ret = _z_write(zs._val, keyexpr, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, opt.congestion_control, + opt.priority); return ret; } @@ -784,7 +785,7 @@ int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t l } ret = _z_write(pub._val->_zn, pub._val->_key, payload, len, opt.encoding, Z_SAMPLE_KIND_PUT, - pub._val->_congestion_control); + pub._val->_congestion_control, pub._val->_priority); return ret; } @@ -792,7 +793,7 @@ int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t l int8_t z_publisher_delete(const z_publisher_t pub, const z_publisher_delete_options_t *options) { (void)(options); return _z_write(pub._val->_zn, pub._val->_key, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, - pub._val->_congestion_control); + pub._val->_congestion_control, pub._val->_priority); } z_subscriber_options_t z_subscriber_options_default(void) { @@ -927,8 +928,14 @@ z_query_reply_options_t z_query_reply_options_default(void) { int8_t z_query_reply(const z_query_t *query, const z_keyexpr_t keyexpr, const uint8_t *payload, size_t payload_len, const z_query_reply_options_t *options) { - (void)(options); - return _z_send_reply(query, keyexpr, payload, payload_len); + _z_value_t value = {.payload = + { + .start = payload, + ._is_alloc = false, + .len = payload_len, + }, + .encoding = {.prefix = options->encoding.prefix, .suffix = options->encoding.suffix}}; + return _z_send_reply(query, keyexpr, value); } _Bool z_reply_is_ok(const z_owned_reply_t *reply) { diff --git a/src/collections/bytes.c b/src/collections/bytes.c index 0a228fd38..d2a8939b0 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -100,3 +100,8 @@ _z_bytes_t _z_bytes_duplicate(const _z_bytes_t *src) { } _Bool _z_bytes_is_empty(const _z_bytes_t *bs) { return bs->len == 0; } +_z_bytes_t _z_bytes_steal(_z_bytes_t *b) { + _z_bytes_t ret = *b; + *b = _z_bytes_empty(); + return ret; +} diff --git a/src/net/primitives.c b/src/net/primitives.c index d987d1280..7b935da6c 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -14,10 +14,17 @@ #include "zenoh-pico/net/primitives.h" #include +#include +#include +#include "zenoh-pico/api/constants.h" +#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/config.h" #include "zenoh-pico/net/logger.h" #include "zenoh-pico/net/memory.h" +#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/definitions/declarations.h" +#include "zenoh-pico/protocol/definitions/network.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/session/query.h" #include "zenoh-pico/session/queryable.h" @@ -42,7 +49,7 @@ void _z_scout(const z_what_t what, const _z_id_t zid, const char *locator, const } /*------------------ Resource Declaration ------------------*/ -_z_zint_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr) { +uint16_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr) { _z_zint_t ret = Z_RESOURCE_ID_NONE; if (zn->_tp._type == @@ -51,10 +58,11 @@ _z_zint_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr) { _z_resource_t *r = (_z_resource_t *)z_malloc(sizeof(_z_resource_t)); if (r != NULL) { r->_id = _z_get_resource_id(zn); - r->_key = _z_keyexpr_duplicate(&keyexpr); + r->_key = _z_keyexpr_duplicate(keyexpr); if (_z_register_resource(zn, _Z_RESOURCE_IS_LOCAL, r) == _Z_RES_OK) { // Build the declare message to send on the wire - _z_declaration_t declaration = _z_msg_make_declaration_resource(r->_id, _z_keyexpr_duplicate(&keyexpr)); + _z_keyexpr_t alias = _z_keyexpr_alias(r->_key); + _z_declaration_t declaration = _z_make_decl_keyexpr(r->_id, &alias); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { ret = r->_id; @@ -71,13 +79,13 @@ _z_zint_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr) { return ret; } -int8_t _z_undeclare_resource(_z_session_t *zn, const _z_zint_t rid) { +int8_t _z_undeclare_resource(_z_session_t *zn, uint16_t rid) { int8_t ret = _Z_RES_OK; _z_resource_t *r = _z_get_resource_by_id(zn, _Z_RESOURCE_IS_LOCAL, rid); if (r != NULL) { // Build the declare message to send on the wire - _z_declaration_t declaration = _z_msg_make_declaration_forget_resource(rid); + _z_declaration_t declaration = _z_make_undecl_keyexpr(rid); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { _z_unregister_resource(zn, _Z_RESOURCE_IS_LOCAL, r); // Only if message is send, local resource is removed @@ -98,19 +106,13 @@ _z_publisher_t *_z_declare_publisher(_z_session_t *zn, _z_keyexpr_t keyexpr, z_c _z_publisher_t *ret = (_z_publisher_t *)z_malloc(sizeof(_z_publisher_t)); if (ret != NULL) { ret->_zn = zn; - ret->_key = _z_keyexpr_duplicate(&keyexpr); + ret->_key = _z_keyexpr_duplicate(keyexpr); ret->_id = _z_get_entity_id(zn); ret->_congestion_control = congestion_control; ret->_priority = priority; // Build the declare message to send on the wire - _z_declaration_t declaration = _z_msg_make_declaration_publisher(_z_keyexpr_duplicate(&keyexpr)); - _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { - // ret = _Z_ERR_TRANSPORT_TX_FAILED; - _z_publisher_free(&ret); - } - _z_n_msg_clear(&n_msg); + _z_declare_resource(zn, keyexpr); } return ret; @@ -121,12 +123,7 @@ int8_t _z_undeclare_publisher(_z_publisher_t *pub) { if (pub != NULL) { // Build the declare message to send on the wire - _z_declaration_t declaration = _z_msg_make_declaration_forget_publisher(_z_keyexpr_duplicate(&pub->_key)); - _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(pub->_zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { - ret = _Z_ERR_TRANSPORT_TX_FAILED; - } - _z_n_msg_clear(&n_msg); + _z_undeclare_resource(pub->_zn, pub->_key._id); } else { ret = _Z_ERR_ENTITY_UNKNOWN; } @@ -139,7 +136,7 @@ _z_subscriber_t *_z_declare_subscriber(_z_session_t *zn, _z_keyexpr_t keyexpr, _ _z_data_handler_t callback, _z_drop_handler_t dropper, void *arg) { _z_subscription_t s; s._id = _z_get_entity_id(zn); - s._key = _z_get_expanded_key_from_key(zn, _Z_RESOURCE_IS_LOCAL, &keyexpr); + s._key = _z_get_expanded_key_from_key(zn, &keyexpr); s._info = sub_info; s._callback = callback; s._dropper = dropper; @@ -155,7 +152,8 @@ _z_subscriber_t *_z_declare_subscriber(_z_session_t *zn, _z_keyexpr_t keyexpr, _ // Do not drop it by the end of this function. if (sp_s != NULL) { // Build the declare message to send on the wire - _z_declaration_t declaration = _z_msg_make_declaration_subscriber(_z_keyexpr_duplicate(&keyexpr), sub_info); + _z_declaration_t declaration = _z_make_decl_subscriber( + &keyexpr, s._id, sub_info.reliability == Z_RELIABILITY_RELIABLE, sub_info.mode == Z_SUBMODE_PULL); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { // ret = _Z_ERR_TRANSPORT_TX_FAILED; @@ -180,8 +178,7 @@ int8_t _z_undeclare_subscriber(_z_subscriber_t *sub) { _z_subscription_sptr_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_id); if (s != NULL) { // Build the declare message to send on the wire - _z_declaration_t declaration = - _z_msg_make_declaration_forget_subscriber(_z_keyexpr_duplicate(&s->ptr->_key)); + _z_declaration_t declaration = _z_make_undecl_subscriber(sub->_id, &s->ptr->_key); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); if (_z_send_n_msg(sub->_zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { // Only if message is successfully send, local subscription state can be removed @@ -205,7 +202,7 @@ _z_queryable_t *_z_declare_queryable(_z_session_t *zn, _z_keyexpr_t keyexpr, _Bo _z_questionable_handler_t callback, _z_drop_handler_t dropper, void *arg) { _z_questionable_t q; q._id = _z_get_entity_id(zn); - q._key = _z_get_expanded_key_from_key(zn, _Z_RESOURCE_IS_LOCAL, &keyexpr); + q._key = _z_get_expanded_key_from_key(zn, &keyexpr); q._complete = complete; q._callback = callback; q._dropper = dropper; @@ -221,8 +218,8 @@ _z_queryable_t *_z_declare_queryable(_z_session_t *zn, _z_keyexpr_t keyexpr, _Bo // Do not drop it by the end of this function. if (sp_q != NULL) { // Build the declare message to send on the wire - _z_declaration_t declaration = _z_msg_make_declaration_queryable( - _z_keyexpr_duplicate(&keyexpr), q._complete, _Z_QUERYABLE_DISTANCE_DEFAULT); + _z_declaration_t declaration = + _z_make_decl_queryable(&keyexpr, q._id, q._complete, _Z_QUERYABLE_DISTANCE_DEFAULT); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { // ret = _Z_ERR_TRANSPORT_TX_FAILED; @@ -247,8 +244,7 @@ int8_t _z_undeclare_queryable(_z_queryable_t *qle) { _z_questionable_sptr_t *q = _z_get_questionable_by_id(qle->_zn, qle->_id); if (q != NULL) { // Build the declare message to send on the wire - _z_declaration_t declaration = - _z_msg_make_declaration_forget_queryable(_z_keyexpr_duplicate(&q->ptr->_key)); + _z_declaration_t declaration = _z_make_undecl_queryable(qle->_id, &q->ptr->_key); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); if (_z_send_n_msg(qle->_zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { // Only if message is successfully send, local queryable state can be removed @@ -268,14 +264,14 @@ int8_t _z_undeclare_queryable(_z_queryable_t *qle) { return ret; } -int8_t _z_send_reply(const z_query_t *query, _z_keyexpr_t keyexpr, const uint8_t *payload, const size_t len) { +int8_t _z_send_reply(const z_query_t *query, _z_keyexpr_t keyexpr, const _z_value_t payload) { int8_t ret = _Z_RES_OK; _z_keyexpr_t q_ke; _z_keyexpr_t r_ke; if (query->_anyke == false) { - q_ke = _z_get_expanded_key_from_key(query->_zn, _Z_RESOURCE_IS_LOCAL, &query->_key); - r_ke = _z_get_expanded_key_from_key(query->_zn, _Z_RESOURCE_IS_LOCAL, &keyexpr); + q_ke = _z_get_expanded_key_from_key(query->_zn, &query->_key); + r_ke = _z_get_expanded_key_from_key(query->_zn, &keyexpr); if (_z_keyexpr_intersects(q_ke._suffix, strlen(q_ke._suffix), r_ke._suffix, strlen(r_ke._suffix)) == false) { ret = _Z_ERR_KEYEXPR_NOT_MATCH; } @@ -286,15 +282,29 @@ int8_t _z_send_reply(const z_query_t *query, _z_keyexpr_t keyexpr, const uint8_t if (ret == _Z_RES_OK) { // Build the reply context decorator. This is NOT the final reply. _z_id_t zid = ((_z_session_t *)query->_zn)->_local_zid; - - _z_data_info_t di = {._flags = 0}; // Empty data info - _z_bytes_t pld = {.len = len, .start = payload}; // Payload - _Bool can_be_dropped = false; // Congestion control - _z_zenoh_message_t z_msg = _z_msg_make_reply(keyexpr, di, pld, can_be_dropped); - - if (_z_send_z_msg(query->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + _z_keyexpr_t ke = _z_keyexpr_alias(keyexpr); + _z_zenoh_message_t z_msg = { + ._tag = _Z_N_RESPONSE, + ._body._response = + { + ._request_id = query->_rid, + ._key = ke, + ._ext_responder = {._zid = zid, ._eid = 0}, + ._ext_qos = _Z_N_QOS_DEFAULT, + ._ext_timestamp = _z_timestamp_null(), + ._tag = _Z_RESPONSE_BODY_REPLY, + ._body._reply = {._value = payload, + ._timestamp = _z_timestamp_null(), + ._ext_consolidation = Z_CONSOLIDATION_MODE_AUTO, + ._ext_source_info = _z_source_info_null()}, + }, + }; + + if (_z_send_n_msg(query->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { ret = _Z_ERR_TRANSPORT_TX_FAILED; } + + // Freeing z_msg is unnecessary, as all of its components are aliased } return ret; @@ -302,24 +312,54 @@ int8_t _z_send_reply(const z_query_t *query, _z_keyexpr_t keyexpr, const uint8_t /*------------------ 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) { + const _z_encoding_t encoding, const z_sample_kind_t kind, const z_congestion_control_t cong_ctrl, + z_priority_t priority) { int8_t ret = _Z_RES_OK; - // @TODO: accept `const z_priority_t priority` as additional parameter - - // Data info - _z_data_info_t info = {._flags = 0, ._encoding = encoding, ._kind = kind}; - _Z_SET_FLAG(info._flags, _Z_DATA_INFO_ENC); - _Z_SET_FLAG(info._flags, _Z_DATA_INFO_KIND); - _z_bytes_t pld = {.len = len, .start = payload}; // Payload _Bool can_be_dropped = cong_ctrl == Z_CONGESTION_CONTROL_DROP; // Congestion control - _z_zenoh_message_t z_msg = _z_msg_make_data(keyexpr, info, pld, can_be_dropped); + _z_network_message_t msg; + switch (kind) { + case Z_SAMPLE_KIND_PUT: + msg = (_z_network_message_t){ + ._tag = _Z_N_PUSH, + ._body._push = + { + ._key = keyexpr, + ._qos = _z_n_qos_make(0, cong_ctrl == Z_CONGESTION_CONTROL_BLOCK, priority), + ._timestamp = _z_timestamp_null(), + ._body._is_put = true, + ._body._body._put = + { + ._commons = {._timestamp = _z_timestamp_null(), ._source_info = _z_source_info_null()}, + ._payload = _z_bytes_wrap(payload, len), + ._encoding = encoding, + }, + }, + }; + break; + case Z_SAMPLE_KIND_DELETE: + msg = (_z_network_message_t){ + ._tag = _Z_N_PUSH, + ._body._push = + { + ._key = keyexpr, + ._qos = _z_n_qos_make(0, cong_ctrl == Z_CONGESTION_CONTROL_BLOCK, priority), + ._timestamp = _z_timestamp_null(), + ._body._is_put = false, + ._body._body._del = {._commons = {._timestamp = _z_timestamp_null(), + ._source_info = _z_source_info_null()}}, + }, + }; + break; + } - if (_z_send_z_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, cong_ctrl) != _Z_RES_OK) { + if (_z_send_n_msg(zn, &msg, Z_RELIABILITY_RELIABLE, cong_ctrl) != _Z_RES_OK) { ret = _Z_ERR_TRANSPORT_TX_FAILED; } + // Freeing z_msg is unnecessary, as all of its components are aliased + return ret; } @@ -333,7 +373,7 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, _z_pending_query_t *pq = (_z_pending_query_t *)z_malloc(sizeof(_z_pending_query_t)); if (pq != NULL) { pq->_id = _z_get_query_id(zn); - pq->_key = _z_get_expanded_key_from_key(zn, _Z_RESOURCE_IS_LOCAL, &keyexpr); + pq->_key = _z_get_expanded_key_from_key(zn, &keyexpr); pq->_parameters = _z_str_clone(parameters); pq->_target = target; pq->_consolidation = consolidation; @@ -346,9 +386,11 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, ret = _z_register_pending_query(zn, pq); // Add the pending query to the current session if (ret == _Z_RES_OK) { - _z_zenoh_message_t z_msg = _z_msg_make_query(keyexpr, pq->_parameters, pq->_id, pq->_consolidation, value); + _z_bytes_t params = _z_bytes_wrap((uint8_t *)pq->_parameters, strlen(pq->_parameters)); + _z_keyexpr_t k = _z_keyexpr_duplicate(keyexpr); + _z_zenoh_message_t z_msg = _z_msg_make_query(&k, ¶ms, pq->_id, pq->_consolidation, &value); - if (_z_send_z_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + if (_z_send_n_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { _z_unregister_pending_query(zn, pq); ret = _Z_ERR_TRANSPORT_TX_FAILED; } @@ -369,9 +411,8 @@ int8_t _z_subscriber_pull(const _z_subscriber_t *sub) { _z_zint_t pull_id = _z_get_pull_id(sub->_zn); _z_zint_t max_samples = 0; // @TODO: get the correct value for max_sample _Bool is_final = true; - _z_zenoh_message_t z_msg = _z_msg_make_pull(s->ptr->_key, pull_id, max_samples, is_final); - - if (_z_send_z_msg(sub->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + _z_zenoh_message_t z_msg = _z_msg_make_pull(_z_keyexpr_alias(s->ptr->_key), pull_id); + if (_z_send_n_msg(sub->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { ret = _Z_ERR_TRANSPORT_TX_FAILED; } } else { diff --git a/src/net/publish.c b/src/net/publish.c index 97f33ba13..7ef50abd2 100644 --- a/src/net/publish.c +++ b/src/net/publish.c @@ -16,8 +16,6 @@ #include -#include "zenoh-pico/protocol/msg.h" - void _z_publisher_clear(_z_publisher_t *pub) { _z_keyexpr_clear(&pub->_key); } void _z_publisher_free(_z_publisher_t **pub) { diff --git a/src/net/resource.c b/src/net/resource.c index 8ce48333f..c0fc38dd0 100644 --- a/src/net/resource.c +++ b/src/net/resource.c @@ -16,24 +16,22 @@ #include -_z_keyexpr_t _z_rname(const char *rname) { - _z_keyexpr_t rk; - rk._id = Z_RESOURCE_ID_NONE; - rk._suffix = NULL; - if (rname != NULL) { - rk._suffix = rname; - } +#include "zenoh-pico/protocol/core.h" - return rk; +_z_keyexpr_t _z_rname(const char *rname) { + return (_z_keyexpr_t){ + ._id = Z_RESOURCE_ID_NONE, + ._uses_remote_mapping = false, + ._owns_suffix = false, + ._suffix = (char *)rname, + }; } _z_keyexpr_t _z_rid_with_suffix(_z_zint_t rid, const char *suffix) { - _z_keyexpr_t rk; - rk._id = rid; - rk._suffix = NULL; - if (suffix != NULL) { - rk._suffix = _z_str_clone(suffix); - } - - return rk; + return (_z_keyexpr_t){ + ._id = rid, + ._uses_remote_mapping = false, + ._owns_suffix = false, + ._suffix = (char *)suffix, + }; } diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 28110a558..51815dcc8 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -22,8 +22,8 @@ /*------------------ period ------------------*/ int8_t _z_period_encode(_z_wbuf_t *buf, const _z_period_t *tp) { - _Z_EC(_z_uint_encode(buf, tp->origin)) - _Z_EC(_z_uint_encode(buf, tp->period)) + _Z_RETURN_IF_ERR(_z_uint_encode(buf, tp->origin)) + _Z_RETURN_IF_ERR(_z_uint_encode(buf, tp->period)) return _z_uint_encode(buf, tp->duration); } @@ -106,7 +106,7 @@ int8_t _z_uint_encode(_z_wbuf_t *wbf, unsigned int uint) { while (lv > (unsigned int)0x7f) { uint8_t c = (uint8_t)(lv & (unsigned int)0x7f) | (uint8_t)0x80; - _Z_EC(_z_wbuf_write(wbf, c)) + _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, c)) lv = lv >> (unsigned int)7; } @@ -175,7 +175,7 @@ int8_t _z_uint64_encode(_z_wbuf_t *wbf, uint64_t u64) { while (lv > (uint64_t)0x7f) { uint8_t c = (uint8_t)(lv & (uint64_t)0x7f) | (uint8_t)0x80; - _Z_EC(_z_wbuf_write(wbf, c)) + _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, c)) lv = lv >> (_z_zint_t)7; } @@ -207,7 +207,7 @@ int8_t _z_zint_encode(_z_wbuf_t *wbf, _z_zint_t v) { while (lv > 0x7f) { uint8_t c = (lv & 0x7f) | 0x80; - _Z_EC(_z_wbuf_write(wbf, c)) + _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, c)) lv = lv >> (_z_zint_t)7; } @@ -270,8 +270,8 @@ int8_t _z_bytes_val_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { int8_t ret = _Z_RES_OK; - _Z_EC(_z_zint_encode(wbf, bs->len)) - _Z_EC(_z_bytes_val_encode(wbf, bs)) + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, bs->len)) + _Z_RETURN_IF_ERR(_z_bytes_val_encode(wbf, bs)) return ret; } @@ -313,7 +313,7 @@ int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *zbf) { return _z_bytes_decode_ /*------------------ string with null terminator ------------------*/ int8_t _z_str_encode(_z_wbuf_t *wbf, const char *s) { size_t len = strlen(s); - _Z_EC(_z_zint_encode(wbf, len)) + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, len)) // Note that this does not put the string terminator on the wire. return _z_wbuf_write_bytes(wbf, (const uint8_t *)s, 0, len); } diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index 4c48b387a..e7097384d 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -28,7 +28,6 @@ #include "zenoh-pico/protocol/definitions/message.h" #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/iobuf.h" -#include "zenoh-pico/protocol/msg.h" #include "zenoh-pico/session/session.h" #include "zenoh-pico/system/platform.h" @@ -217,7 +216,7 @@ int8_t _z_undecl_decode_extensions(_z_msg_ext_t *extension, void *ctx) { if (!ke->_suffix) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - ke->owns_suffix = true; + ke->_owns_suffix = true; _z_zbuf_read_bytes(zbf, (uint8_t *)ke->_suffix, 0, len); ke->_suffix[len] = 0; } @@ -250,12 +249,12 @@ int8_t _z_decl_commons_decode(_z_zbuf_t *zbf, uint8_t header, _Bool *has_extensi if (ke->_suffix == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - ke->owns_suffix = true; + ke->_owns_suffix = true; _z_zbuf_read_bytes(zbf, (uint8_t *)ke->_suffix, 0, len); ke->_suffix[len] = 0; } else { ke->_suffix = ""; - ke->owns_suffix = false; + ke->_owns_suffix = false; } return _Z_RES_OK; } diff --git a/src/protocol/codec/ext.c b/src/protocol/codec/ext.c index f9acc3b7a..8e6876336 100644 --- a/src/protocol/codec/ext.c +++ b/src/protocol/codec/ext.c @@ -43,7 +43,7 @@ int8_t _z_msg_ext_decode_unit_na(_z_msg_ext_unit_t *ext, _z_zbuf_t *zbf) { retur int8_t _z_msg_ext_encode_zint(_z_wbuf_t *wbf, const _z_msg_ext_zint_t *ext) { int8_t ret = _Z_RES_OK; - _Z_EC(_z_zint_encode(wbf, ext->_val)) + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, ext->_val)) return ret; } @@ -57,7 +57,7 @@ int8_t _z_msg_ext_decode_zint_na(_z_msg_ext_zint_t *ext, _z_zbuf_t *zbf) { retur int8_t _z_msg_ext_encode_zbuf(_z_wbuf_t *wbf, const _z_msg_ext_zbuf_t *ext) { int8_t ret = _Z_RES_OK; - _Z_EC(_z_bytes_encode(wbf, &ext->_val)) + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &ext->_val)) return ret; } @@ -73,7 +73,7 @@ int8_t _z_msg_ext_decode_zbuf_na(_z_msg_ext_zbuf_t *ext, _z_zbuf_t *zbf) { retur int8_t _z_msg_ext_encode(_z_wbuf_t *wbf, const _z_msg_ext_t *ext, _Bool has_next) { int8_t ret = _Z_RES_OK; - _Z_EC(_z_wbuf_write(wbf, _Z_EXT_FULL_ID(ext->_header) | (has_next << 7))) + _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, _Z_EXT_FULL_ID(ext->_header) | (has_next << 7))) uint8_t enc = _Z_EXT_ENC(ext->_header); switch (enc) { diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index e0eea6640..643261f04 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -84,7 +84,7 @@ int8_t _z_timestamp_encode(_z_wbuf_t *wbf, const _z_timestamp_t *ts) { int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _TIMESTAMP\n"); - _Z_EC(_z_uint64_encode(wbf, ts->time)) + _Z_RETURN_IF_ERR(_z_uint64_encode(wbf, ts->time)) ret |= _z_id_encode_as_zbytes(wbf, &ts->id); return ret; @@ -105,9 +105,9 @@ int8_t _z_keyexpr_encode(_z_wbuf_t *wbf, _Bool has_suffix, const _z_keyexpr_t *f int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _RESKEY\n"); - _Z_EC(_z_zint_encode(wbf, fld->_id)) + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, fld->_id)) if (has_suffix == true) { - _Z_EC(_z_str_encode(wbf, fld->_suffix)) + _Z_RETURN_IF_ERR(_z_str_encode(wbf, fld->_suffix)) } return ret; @@ -137,10 +137,10 @@ int8_t _z_keyexpr_decode(_z_keyexpr_t *ke, _z_zbuf_t *zbf, _Bool has_suffix) { int8_t _z_locators_encode(_z_wbuf_t *wbf, const _z_locator_array_t *la) { int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _LOCATORS\n"); - _Z_EC(_z_zint_encode(wbf, la->_len)) + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, la->_len)) for (size_t i = 0; i < la->_len; i++) { char *s = _z_locator_to_str(&la->_val[i]); - _Z_EC(_z_str_encode(wbf, s)) + _Z_RETURN_IF_ERR(_z_str_encode(wbf, s)) z_free(s); } @@ -228,8 +228,8 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { (void)(pshb); int8_t ret = _Z_RES_OK; uint8_t header = pshb->_is_put ? _Z_M_PUT_ID : _Z_M_DEL_ID; - _Bool has_source_info = _z_id_check(pshb->_union._put._commons._source_info._id); - _Bool has_timestamp = _z_timestamp_check(&pshb->_union._put._commons._timestamp); + _Bool has_source_info = _z_id_check(pshb->_body._put._commons._source_info._id); + _Bool has_timestamp = _z_timestamp_check(&pshb->_body._put._commons._timestamp); _Bool has_encoding = false; if (has_source_info) { header |= _Z_FLAG_Z_Z; @@ -238,8 +238,8 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { if (has_timestamp) { header |= _Z_FLAG_Z_P_T; } - has_encoding = pshb->_union._put._encoding.prefix != Z_ENCODING_PREFIX_EMPTY || - !_z_bytes_is_empty(&pshb->_union._put._encoding.suffix); + has_encoding = pshb->_body._put._encoding.prefix != Z_ENCODING_PREFIX_EMPTY || + !_z_bytes_is_empty(&pshb->_body._put._encoding.suffix); if (has_encoding) { header |= _Z_FLAG_Z_P_E; } @@ -250,21 +250,21 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { } ret = _z_uint8_encode(wbf, header); if ((ret == _Z_RES_OK) && has_timestamp) { - ret = _z_timestamp_encode(wbf, &pshb->_union._put._commons._timestamp); + ret = _z_timestamp_encode(wbf, &pshb->_body._put._commons._timestamp); } if ((ret == _Z_RES_OK) && has_encoding) { - ret = _z_encoding_prefix_encode(wbf, pshb->_union._put._encoding.prefix); - ret |= _z_bytes_encode(wbf, &pshb->_union._put._encoding.suffix); + ret = _z_encoding_prefix_encode(wbf, pshb->_body._put._encoding.prefix); + ret |= _z_bytes_encode(wbf, &pshb->_body._put._encoding.suffix); } if ((ret == _Z_RES_OK) && has_source_info) { ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x01); - ret |= _z_source_info_encode(wbf, &pshb->_union._put._commons._source_info); + ret |= _z_source_info_encode(wbf, &pshb->_body._put._commons._source_info); } if ((ret == _Z_RES_OK) && pshb->_is_put) { - ret = _z_bytes_encode(wbf, &pshb->_union._put._payload); + ret = _z_bytes_encode(wbf, &pshb->_body._put._payload); } return ret; @@ -275,7 +275,7 @@ int8_t _z_push_body_decode_extensions(_z_msg_ext_t *extension, void *ctx) { switch (_Z_EXT_FULL_ID(extension->_header)) { case _Z_MSG_EXT_ENC_ZBUF | 0x01: { _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); - ret = _z_source_info_decode(&pshb->_union._put._commons._source_info, &zbf); + ret = _z_source_info_decode(&pshb->_body._put._commons._source_info, &zbf); break; } default: @@ -293,23 +293,23 @@ int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf, uint8_t header) case _Z_M_PUT_ID: { pshb->_is_put = true; if (_Z_HAS_FLAG(header, _Z_FLAG_Z_P_T)) { - ret = _z_timestamp_decode(&pshb->_union._put._commons._timestamp, zbf); + ret = _z_timestamp_decode(&pshb->_body._put._commons._timestamp, zbf); } if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_P_E)) { - ret = _z_encoding_prefix_decode(&pshb->_union._put._encoding.prefix, zbf); + ret = _z_encoding_prefix_decode(&pshb->_body._put._encoding.prefix, zbf); } if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { ret = _z_msg_ext_decode_iter(zbf, _z_push_body_decode_extensions, pshb); } if (ret == _Z_RES_OK) { - ret = _z_bytes_decode(&pshb->_union._put._payload, zbf); + ret = _z_bytes_decode(&pshb->_body._put._payload, zbf); } break; } case _Z_M_DEL_ID: { pshb->_is_put = false; if (_Z_HAS_FLAG(header, _Z_FLAG_Z_D_T)) { - ret = _z_timestamp_decode(&pshb->_union._put._commons._timestamp, zbf); + ret = _z_timestamp_decode(&pshb->_body._put._commons._timestamp, zbf); } if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { ret = _z_msg_ext_decode_iter(zbf, _z_push_body_decode_extensions, pshb); @@ -326,26 +326,26 @@ int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf, uint8_t header) } int8_t _z_put_encode(_z_wbuf_t *wbf, const _z_msg_put_t *put) { - _z_push_body_t body = {._is_put = true, ._union = {._put = *put}}; + _z_push_body_t body = {._is_put = true, ._body = {._put = *put}}; return _z_push_body_encode(wbf, &body); } int8_t _z_put_decode(_z_msg_put_t *put, _z_zbuf_t *zbf, uint8_t header) { assert(_Z_MID(header) == _Z_MID_Z_PUT); - _z_push_body_t body = {._is_put = true, ._union = {._put = *put}}; + _z_push_body_t body = {._is_put = true, ._body = {._put = *put}}; int8_t ret = _z_push_body_decode(&body, zbf, header); - *put = body._union._put; + *put = body._body._put; return ret; } int8_t _z_del_encode(_z_wbuf_t *wbf, const _z_msg_del_t *del) { - _z_push_body_t body = {._is_put = false, ._union = {._del = *del}}; + _z_push_body_t body = {._is_put = false, ._body = {._del = *del}}; return _z_push_body_encode(wbf, &body); } int8_t _z_del_decode(_z_msg_del_t *del, _z_zbuf_t *zbf, uint8_t header) { assert(_Z_MID(header) == _Z_MID_Z_DEL); - _z_push_body_t body = {._is_put = false, ._union = {._del = *del}}; + _z_push_body_t body = {._is_put = false, ._body = {._del = *del}}; int8_t ret = _z_push_body_decode(&body, zbf, header); - *del = body._union._del; + *del = body._body._del; return ret; } @@ -450,7 +450,7 @@ int8_t _z_reply_encode(_z_wbuf_t *wbf, const _z_msg_reply_t *reply) { if (reply->_ext_consolidation != Z_CONSOLIDATION_MODE_AUTO || _z_id_check(reply->_ext_source_info._id)) { header |= _Z_FLAG_Z_Z; } - _Z_EC(_z_uint8_encode(wbf, header)); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); int8_t ret = _Z_RES_OK; if (_z_timestamp_check(&reply->_timestamp)) { assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_R_T)); @@ -690,7 +690,7 @@ int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_scout_t *m (void)(header); _Z_DEBUG("Encoding _Z_MID_SCOUT\n"); - _Z_EC(_z_uint8_encode(wbf, msg->_version)) + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, msg->_version)) uint8_t cbyte = 0; cbyte |= (msg->_what & 0x07); @@ -699,7 +699,7 @@ int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_scout_t *m _Z_SET_FLAG(cbyte, _Z_FLAG_T_SCOUT_I); cbyte |= ((zid_len - 1) & 0x0F) << 4; } - _Z_EC(_z_uint8_encode(wbf, cbyte)) + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, cbyte)) ret |= _z_wbuf_write_bytes(wbf, msg->_zid.id, 0, zid_len); @@ -734,16 +734,16 @@ int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *m int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_HELLO\n"); - _Z_EC(_z_uint8_encode(wbf, msg->_version)) + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, msg->_version)) uint8_t zidlen = _z_id_len(msg->_zid); uint8_t cbyte = 0; cbyte |= (msg->_whatami & 0x03); cbyte |= ((zidlen - 1) & 0x0F) << 4; - _Z_EC(_z_uint8_encode(wbf, cbyte)) - _Z_EC(_z_bytes_val_encode(wbf, &(_z_bytes_t){.start = msg->_zid.id, .len = zidlen, ._is_alloc = false})); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, cbyte)) + _Z_RETURN_IF_ERR(_z_bytes_val_encode(wbf, &(_z_bytes_t){.start = msg->_zid.id, .len = zidlen, ._is_alloc = false})); if (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true) { - _Z_EC(_z_locators_encode(wbf, &msg->_locators)) + _Z_RETURN_IF_ERR(_z_locators_encode(wbf, &msg->_locators)) } return ret; @@ -788,7 +788,7 @@ int8_t _z_scouting_message_encode(_z_wbuf_t *wbf, const _z_scouting_message_t *m uint8_t header = msg->_header; - _Z_EC(_z_wbuf_write(wbf, header)) + _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, header)) switch (_Z_MID(msg->_header)) { case _Z_MID_SCOUT: { ret |= _z_scout_encode(wbf, msg->_header, &msg->_body._scout); diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index 13a5cb421..6cf48ff62 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -31,7 +31,6 @@ #include "zenoh-pico/protocol/definitions/message.h" #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/iobuf.h" -#include "zenoh-pico/protocol/msg.h" #include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" @@ -112,8 +111,8 @@ int8_t _z_request_encode(_z_wbuf_t *wbf, const _z_n_msg_request_t *msg) { header |= _Z_FLAG_N_REQUEST_N; } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); - _Z_EC(_z_zint_encode(wbf, msg->_rid)); - _Z_EC(_z_keyexpr_encode(wbf, has_suffix, &msg->_key)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_rid)); + _Z_RETURN_IF_ERR(_z_keyexpr_encode(wbf, has_suffix, &msg->_key)); _z_n_msg_request_exts_t exts = _z_n_msg_request_needed_exts(msg); if (exts.ext_qos) { @@ -149,7 +148,7 @@ int8_t _z_request_encode(_z_wbuf_t *wbf, const _z_n_msg_request_t *msg) { switch (msg->_tag) { case _Z_REQUEST_QUERY: { - _Z_RETURN_IF_ERR(_z_query_encode(wbf, &msg->_body.query)); + _Z_RETURN_IF_ERR(_z_query_encode(wbf, &msg->_body._query)); } break; case _Z_REQUEST_PUT: { _Z_RETURN_IF_ERR(_z_put_encode(wbf, &msg->_body.put)); @@ -203,7 +202,7 @@ int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header } switch (_Z_MID(header)) { case _Z_MID_Z_QUERY: { - _Z_RETURN_IF_ERR(_z_query_decode(&msg->_body.query, zbf, header)); + _Z_RETURN_IF_ERR(_z_query_decode(&msg->_body._query, zbf, header)); } break; case _Z_MID_Z_PUT: { _Z_RETURN_IF_ERR(_z_put_decode(&msg->_body.put, zbf, header)); @@ -229,7 +228,7 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { _Bool has_ts_ext = _z_timestamp_check(&msg->_ext_timestamp); _Bool has_responder_ext = _z_id_check(msg->_ext_responder._zid); uint8_t n_ext = (has_qos_ext ? 1 : 0) + (has_ts_ext ? 1 : 0) + (has_responder_ext ? 1 : 0); - if (msg->_uses_sender_mapping) { + if (msg->_key._uses_remote_mapping) { _Z_SET_FLAG(header, _Z_FLAG_N_RESPONSE_M); } if (msg->_key._suffix != NULL) { @@ -271,7 +270,7 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_ext_responder._eid)); } - switch (msg->_body_tag) { + switch (msg->_tag) { case _Z_RESPONSE_BODY_REPLY: { _Z_RETURN_IF_ERR(_z_reply_encode(wbf, &msg->_body._reply)); break; @@ -330,15 +329,15 @@ int8_t _z_response_decode_extension(_z_msg_ext_t *extension, void *ctx) { int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); int8_t ret = _Z_RES_OK; - msg->_uses_sender_mapping = _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_M); - _Z_EC(_z_zint_decode(&msg->_request_id, zbf)); + msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_M); + _Z_RETURN_IF_ERR(_z_zint_decode(&msg->_request_id, zbf)); _Z_RETURN_IF_ERR(_z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N))); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { - _Z_EC(_z_msg_ext_decode_iter(zbf, _z_response_decode_extension, msg)); + _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_response_decode_extension, msg)); } uint8_t inner_header; - _Z_EC(_z_uint8_decode(&inner_header, zbf)); + _Z_RETURN_IF_ERR(_z_uint8_decode(&inner_header, zbf)); switch (_Z_MID(inner_header)) { case _Z_MID_Z_REPLY: { @@ -374,7 +373,7 @@ int8_t _z_response_final_encode(_z_wbuf_t *wbf, const _z_n_msg_response_final_t _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); uint8_t header = _Z_MID_N_RESPONSE_FINAL; _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); - _Z_EC(_z_zint_encode(wbf, msg->_request_id)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_request_id)); return ret; } diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index 600b98bae..6f2874a18 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -146,25 +146,25 @@ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg _Z_DEBUG("Encoding _Z_MID_T_INIT\n"); int8_t ret = _Z_RES_OK; - _Z_EC(_z_wbuf_write(wbf, msg->_version)) + _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, msg->_version)) 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 - _Z_EC(_z_uint8_encode(wbf, cbyte)) - _Z_EC(_z_wbuf_write_bytes(wbf, msg->_zid.id, 0, zidlen)) + _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_INIT_S) == true) { cbyte = 0; cbyte |= (msg->_seq_num_res & 0x03); cbyte |= ((msg->_req_id_res & 0x03) << 2); - _Z_EC(_z_uint8_encode(wbf, cbyte)) - _Z_EC(_z_uint16_encode(wbf, msg->_batch_size)) + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, cbyte)) + _Z_RETURN_IF_ERR(_z_uint16_encode(wbf, msg->_batch_size)) } if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_A) == true) { - _Z_EC(_z_bytes_encode(wbf, &msg->_cookie)) + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_cookie)) } return ret; @@ -224,15 +224,15 @@ int8_t _z_open_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_open_t *msg _Z_DEBUG("Encoding _Z_MID_T_OPEN\n"); if (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_T) == true) { - _Z_EC(_z_zint_encode(wbf, msg->_lease / 1000)) + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_lease / 1000)) } else { - _Z_EC(_z_zint_encode(wbf, msg->_lease)) + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_lease)) } - _Z_EC(_z_zint_encode(wbf, msg->_initial_sn)) + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_initial_sn)) if (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_A) == false) { - _Z_EC(_z_bytes_encode(wbf, &msg->_cookie)) + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_cookie)) } return ret; @@ -315,7 +315,7 @@ int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg) { int8_t ret = _Z_RES_OK; - _Z_EC(_z_zint_encode(wbf, msg->_sn)) + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_sn)) if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { ret = _Z_ERR_MESSAGE_SERIALIZATION_FAILED; @@ -323,7 +323,7 @@ int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *m if (ret == _Z_RES_OK) { size_t len = _z_network_message_vec_len(&msg->_messages); for (size_t i = 0; i < len; i++) { - _Z_EC(_z_network_message_encode(wbf, _z_network_message_vec_get(&msg->_messages, i))) + _Z_RETURN_IF_ERR(_z_network_message_encode(wbf, _z_network_message_vec_get(&msg->_messages, i))) } } @@ -369,12 +369,12 @@ int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg) { int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_TRANSPORT_FRAGMENT\n"); - _Z_EC(_z_zint_encode(wbf, msg->_sn)) + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_sn)) if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { ret = _Z_ERR_MESSAGE_SERIALIZATION_FAILED; } if (ret == _Z_RES_OK) { - _Z_EC(_z_bytes_encode(wbf, &msg->_payload)) + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_payload)) } return ret; @@ -429,7 +429,7 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t uint8_t header = msg->_header; - _Z_EC(_z_wbuf_write(wbf, header)) + _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, header)) switch (_Z_MID(msg->_header)) { case _Z_MID_T_FRAME: { ret |= _z_frame_encode(wbf, msg->_header, &msg->_body._frame); diff --git a/src/protocol/core.c b/src/protocol/core.c index 144edff52..403325c55 100644 --- a/src/protocol/core.c +++ b/src/protocol/core.c @@ -1,5 +1,7 @@ #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/api/primitives.h" + uint8_t _z_id_len(_z_id_t id) { uint8_t len = 16; while (len > 0) { @@ -41,3 +43,9 @@ _z_source_info_t _z_source_info_null() { 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_value_t _z_value_null(void) { return (_z_value_t){.payload = _z_bytes_empty(), .encoding = z_encoding_default()}; } +_z_value_t _z_value_steal(_z_value_t *value) { + _z_value_t ret = *value; + *value = _z_value_null(); + return ret; +} diff --git a/src/protocol/definitions/message.c b/src/protocol/definitions/message.c index 616f79890..a0587e6b5 100644 --- a/src/protocol/definitions/message.c +++ b/src/protocol/definitions/message.c @@ -1,7 +1,5 @@ #include "zenoh-pico/protocol/definitions/message.h" -#include "zenoh-pico/protocol/msg.h" - void _z_msg_reply_clear(_z_msg_reply_t *msg) { _z_value_clear(&msg->_value); } void _z_msg_put_clear(_z_msg_put_t *msg) { diff --git a/src/protocol/definitions/network.c b/src/protocol/definitions/network.c index 0885b4a84..52d8031e2 100644 --- a/src/protocol/definitions/network.c +++ b/src/protocol/definitions/network.c @@ -2,7 +2,7 @@ #include "zenoh-pico/api/constants.h" #include "zenoh-pico/protocol/core.h" -#include "zenoh-pico/protocol/msg.h" +#include "zenoh-pico/protocol/definitions/message.h" #include "zenoh-pico/utils/logging.h" _z_n_msg_request_exts_t _z_n_msg_request_needed_exts(const _z_n_msg_request_t *msg) { @@ -34,7 +34,7 @@ void _z_n_msg_request_clear(_z_n_msg_request_t *msg) { _z_keyexpr_clear(&msg->_key); switch (msg->_tag) { case _Z_REQUEST_QUERY: { - _z_msg_query_clear(&msg->_body.query); + _z_msg_query_clear(&msg->_body._query); } break; case _Z_REQUEST_PUT: { _z_msg_put_clear(&msg->_body.put); @@ -51,7 +51,21 @@ void _z_n_msg_request_clear(_z_n_msg_request_t *msg) { /*=============================*/ /* Network Messages */ /*=============================*/ -void _z_push_body_clear(_z_push_body_t *msg) { (void)(msg); } +void _z_push_body_clear(_z_push_body_t *msg) { + if (msg->_is_put) { + _z_msg_put_clear(&msg->_body._put); + } +} +_z_push_body_t _z_push_body_steal(_z_push_body_t *msg) { + _z_push_body_t ret = *msg; + *msg = _z_push_body_null(); + return ret; +} +_z_push_body_t _z_push_body_null() { + return (_z_push_body_t){ + ._is_put = false, + ._body._del._commons = {._timestamp = _z_timestamp_null(), ._source_info = _z_source_info_null()}}; +} void _z_n_msg_response_final_clear(_z_n_msg_response_final_t *msg) { (void)(msg); } @@ -63,7 +77,7 @@ void _z_n_msg_push_clear(_z_n_msg_push_t *msg) { void _z_n_msg_response_clear(_z_n_msg_response_t *msg) { _z_timestamp_clear(&msg->_ext_timestamp); _z_keyexpr_clear(&msg->_key); - switch (msg->_body_tag) { + switch (msg->_tag) { case _Z_RESPONSE_BODY_REPLY: { _z_msg_reply_clear(&msg->_body._reply); break; @@ -115,21 +129,100 @@ void _z_n_msg_free(_z_network_message_t **msg) { *msg = NULL; } } -_z_network_message_t _z_msg_make_pull(_Z_MOVE(_z_keyexpr_t) key, _z_zint_t pull_id) { - _z_network_message_t ret = {._tag = _Z_N_REQUEST, - ._body = {._request = { - ._rid = pull_id, - ._key = _z_keyexpr_steal(key), - ._tag = _Z_REQUEST_PULL, - ._body = - { - .pull = {._ext_source_info = _z_source_info_null()}, - }, - .ext_budget = 0, - .ext_qos = _Z_N_QOS_DEFAULT, - .ext_target = Z_QUERY_TARGET_BEST_MATCHING, - .ext_tstamp = _z_timestamp_null(), - .ext_timeout_ms = 0, - }}}; +_z_network_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id) { + _z_network_message_t ret = { + ._tag = _Z_N_REQUEST, + ._body = + { + ._request = + { + ._rid = pull_id, + ._key = key, + ._tag = _Z_REQUEST_PULL, + ._body = + { + .pull = {._ext_source_info = _z_source_info_null()}, + }, + .ext_budget = 0, + .ext_qos = _Z_N_QOS_DEFAULT, + .ext_target = Z_QUERY_TARGET_BEST_MATCHING, + .ext_tstamp = _z_timestamp_null(), + .ext_timeout_ms = 0, + }, + }, + }; return ret; } +_z_zenoh_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes_t) parameters, _z_zint_t qid, + z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value) { + return (_z_zenoh_message_t){ + ._tag = _Z_N_REQUEST, + ._body._request = + { + ._rid = qid, + ._key = _z_keyexpr_steal(key), + ._tag = _Z_REQUEST_QUERY, + ._body._query = + { + ._parameters = _z_bytes_steal(parameters), + ._consolidation = consolidation, + ._value = _z_value_steal(value), + ._info = _z_source_info_null(), + }, + .ext_budget = 0, + .ext_qos = _Z_N_QOS_DEFAULT, + .ext_target = Z_QUERY_TARGET_BEST_MATCHING, + .ext_timeout_ms = 0, + .ext_tstamp = _z_timestamp_null(), + }, + }; +} +_z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid) { + return (_z_zenoh_message_t){ + ._tag = _Z_N_RESPONSE_FINAL, + ._body = {._response_final = {._request_id = rid}}, + }; +} +_z_network_message_t _z_n_msg_make_declare(_z_declaration_t declaration) { + return (_z_network_message_t){ + ._tag = _Z_N_DECLARE, + ._body._declare = + { + ._decl = declaration, + ._ext_qos = _Z_N_QOS_DEFAULT, + ._ext_timestamp = _z_timestamp_null(), + }, + }; +} +_z_network_message_t _z_n_msg_make_push(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_push_body_t) body) { + return (_z_network_message_t){ + ._tag = _Z_N_PUSH, + ._body._push = {._key = _z_keyexpr_steal(key), ._body = _z_push_body_steal(body)}, + }; +} +_z_network_message_t _z_n_msg_make_reply(_z_zint_t rid, _Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_value_t) value) { + return (_z_network_message_t){ + ._tag = _Z_N_RESPONSE, + ._body._response = + { + ._key = _z_keyexpr_steal(key), + ._tag = _Z_RESPONSE_BODY_REPLY, + ._request_id = rid, + ._body._reply = + { + ._timestamp = _z_timestamp_null(), + ._value = _z_value_steal(value), + ._ext_source_info = _z_source_info_null(), + ._ext_consolidation = Z_CONSOLIDATION_MODE_AUTO, + }, + ._ext_qos = _Z_N_QOS_DEFAULT, + ._ext_timestamp = _z_timestamp_null(), + ._ext_responder = + { + ._eid = 0, + ._zid = _z_id_empty(), + }, + }, + + }; +} diff --git a/src/protocol/keyexpr.c b/src/protocol/keyexpr.c index e94b484dc..e9e6d98e6 100644 --- a/src/protocol/keyexpr.c +++ b/src/protocol/keyexpr.c @@ -21,6 +21,54 @@ #include "zenoh-pico/utils/pointers.h" #include "zenoh-pico/utils/string.h" +void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src) { + dst->_id = src->_id; + dst->_suffix = src->_suffix ? _z_str_clone(src->_suffix) : NULL; + dst->_uses_remote_mapping = src->_uses_remote_mapping; + dst->_owns_suffix = true; +} + +_z_keyexpr_t _z_keyexpr_duplicate(_z_keyexpr_t src) { + _z_keyexpr_t dst; + _z_keyexpr_copy(&dst, &src); + return dst; +} + +_z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src) { + _z_keyexpr_t stolen = *src; + src->_owns_suffix = false; + src->_id = 0; + src->_suffix = NULL; + return stolen; +} + +void _z_keyexpr_clear(_z_keyexpr_t *rk) { + rk->_id = 0; + if (rk->_suffix != NULL && rk->_owns_suffix) { + _z_str_clear((char *)rk->_suffix); + rk->_owns_suffix = false; + } +} + +void _z_keyexpr_free(_z_keyexpr_t **rk) { + _z_keyexpr_t *ptr = *rk; + + if (ptr != NULL) { + _z_keyexpr_clear(ptr); + + z_free(ptr); + *rk = NULL; + } +} +_z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src) { + return (_z_keyexpr_t){ + ._id = src._id, + ._owns_suffix = false, + ._suffix = src._suffix, + ._uses_remote_mapping = src._uses_remote_mapping, + }; +} + /*------------------ Canonize helpers ------------------*/ zp_keyexpr_canon_status_t __zp_canon_prefix(const char *start, size_t *len) { zp_keyexpr_canon_status_t ret = Z_KEYEXPR_CANON_SUCCESS; @@ -745,4 +793,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); } +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 diff --git a/src/protocol/msg.c b/src/protocol/msg.c deleted file mode 100644 index 21e7bea94..000000000 --- a/src/protocol/msg.c +++ /dev/null @@ -1,453 +0,0 @@ -// -// 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, -// - -#include "zenoh-pico/protocol/msg.h" - -#include -#include - -#include "zenoh-pico/collections/bytes.h" -#include "zenoh-pico/protocol/core.h" -#include "zenoh-pico/protocol/ext.h" -#include "zenoh-pico/protocol/iobuf.h" -#include "zenoh-pico/session/queryable.h" -#include "zenoh-pico/utils/logging.h" - -/*------------------ Payload field ------------------*/ -void _z_payload_clear(_z_bytes_t *p) { _z_bytes_clear(p); } - -/*------------------ Timestamp Field ------------------*/ -void _z_timestamp_clear(_z_timestamp_t *ts) {} - -/*------------------ ResKey Field ------------------*/ -void _z_keyexpr_clear(_z_keyexpr_t *rk) { - rk->_id = 0; - if (rk->_suffix != NULL && rk->owns_suffix) { - _z_str_clear((char *)rk->_suffix); - rk->owns_suffix = false; - } -} - -void _z_keyexpr_free(_z_keyexpr_t **rk) { - _z_keyexpr_t *ptr = *rk; - - if (ptr != NULL) { - _z_keyexpr_clear(ptr); - - z_free(ptr); - *rk = NULL; - } -} - -/*=============================*/ -/* Zenoh Messages */ -/*=============================*/ -/*------------------ Resource Declaration ------------------*/ -_z_declaration_t _z_msg_make_declaration_resource(_z_zint_t id, _z_keyexpr_t key) { - _z_declaration_t decl; - - decl._body._res._id = id; - decl._body._res._key = key; - - decl._header = _Z_DECL_RESOURCE; - if (decl._body._res._key._suffix != NULL) { - _Z_SET_FLAG(decl._header, _Z_FLAG_Z_K); - } - - return decl; -} - -void _z_declaration_clear_resource(_z_res_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } - -/*------------------ Forget Resource Declaration ------------------*/ -_z_declaration_t _z_msg_make_declaration_forget_resource(_z_zint_t rid) { - _z_declaration_t decl; - - decl._body._forget_res._rid = rid; - - decl._header = _Z_DECL_FORGET_RESOURCE; - - return decl; -} - -void _z_declaration_clear_forget_resource(_z_forget_res_decl_t *dcl) { (void)(dcl); } - -/*------------------ Publisher Declaration ------------------*/ -_z_declaration_t _z_msg_make_declaration_publisher(_z_keyexpr_t key) { - _z_declaration_t decl; - - decl._body._pub._key = key; - - decl._header = _Z_DECL_PUBLISHER; - if (key._suffix != NULL) { - _Z_SET_FLAG(decl._header, _Z_FLAG_Z_K); - } - - return decl; -} - -void _z_declaration_clear_publisher(_z_pub_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } - -/*------------------ Forget Publisher Declaration ------------------*/ -_z_declaration_t _z_msg_make_declaration_forget_publisher(_z_keyexpr_t key) { - _z_declaration_t decl; - - decl._body._forget_pub._key = key; - - decl._header = _Z_DECL_FORGET_PUBLISHER; - if (key._suffix != NULL) { - _Z_SET_FLAG(decl._header, _Z_FLAG_Z_K); - } - - return decl; -} - -void _z_declaration_clear_forget_publisher(_z_forget_pub_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } - -/*------------------ Subscriber Declaration ------------------*/ -_z_declaration_t _z_msg_make_declaration_subscriber(_z_keyexpr_t key, _z_subinfo_t subinfo) { - _z_declaration_t decl; - - decl._body._sub._key = key; - decl._body._sub._subinfo = subinfo; - - decl._header = _Z_DECL_SUBSCRIBER; - if (key._suffix != NULL) { - _Z_SET_FLAG(decl._header, _Z_FLAG_Z_K); - } - if (subinfo.mode != Z_SUBMODE_PUSH) { - _Z_SET_FLAG(decl._header, _Z_FLAG_Z_S); - } - if (subinfo.reliability == Z_RELIABILITY_RELIABLE) { - _Z_SET_FLAG(decl._header, _Z_FLAG_Z_R); - } - - return decl; -} - -void _z_subinfo_clear(_z_subinfo_t *si) { - (void)(si); - // Nothing to clear -} - -void _z_declaration_clear_subscriber(_z_sub_decl_t *dcl) { - _z_keyexpr_clear(&dcl->_key); - _z_subinfo_clear(&dcl->_subinfo); -} - -/*------------------ Forget Subscriber Declaration ------------------*/ -_z_declaration_t _z_msg_make_declaration_forget_subscriber(_z_keyexpr_t key) { - _z_declaration_t decl; - - decl._body._forget_sub._key = key; - - decl._header = _Z_DECL_FORGET_SUBSCRIBER; - if (key._suffix != NULL) { - _Z_SET_FLAG(decl._header, _Z_FLAG_Z_K); - } - - return decl; -} - -void _z_declaration_clear_forget_subscriber(_z_forget_sub_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } - -/*------------------ Queryable Declaration ------------------*/ -_z_declaration_t _z_msg_make_declaration_queryable(_z_keyexpr_t key, _z_zint_t complete, _z_zint_t distance) { - _z_declaration_t decl; - - decl._body._qle._key = key; - - decl._header = _Z_DECL_QUERYABLE; - if (key._suffix != NULL) { - _Z_SET_FLAG(decl._header, _Z_FLAG_Z_K); - } - - decl._body._qle._complete = complete; - decl._body._qle._distance = distance; - if ((decl._body._qle._complete != _Z_QUERYABLE_COMPLETE_DEFAULT) || - (decl._body._qle._distance != _Z_QUERYABLE_DISTANCE_DEFAULT)) { - _Z_SET_FLAG(decl._header, _Z_FLAG_Z_Q); - } - - return decl; -} - -void _z_declaration_clear_queryable(_z_qle_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } - -/*------------------ Forget Queryable Declaration ------------------*/ -_z_declaration_t _z_msg_make_declaration_forget_queryable(_z_keyexpr_t key) { - _z_declaration_t decl; - - decl._body._forget_qle._key = key; - - decl._header = _Z_DECL_FORGET_QUERYABLE; - if (key._suffix != NULL) { - _Z_SET_FLAG(decl._header, _Z_FLAG_Z_K); - } - - return decl; -} - -void _z_declaration_clear_forget_queryable(_z_forget_qle_decl_t *dcl) { _z_keyexpr_clear(&dcl->_key); } - -/*------------------ Declare ------------------*/ -void _z_declaration_clear(_z_declaration_t *dcl) { - uint8_t did = _Z_MID(dcl->_header); - switch (did) { - case _Z_DECL_RESOURCE: - _z_declaration_clear_resource(&dcl->_body._res); - break; - case _Z_DECL_PUBLISHER: - _z_declaration_clear_publisher(&dcl->_body._pub); - break; - case _Z_DECL_SUBSCRIBER: - _z_declaration_clear_subscriber(&dcl->_body._sub); - break; - case _Z_DECL_QUERYABLE: - _z_declaration_clear_queryable(&dcl->_body._qle); - break; - case _Z_DECL_FORGET_RESOURCE: - _z_declaration_clear_forget_resource(&dcl->_body._forget_res); - break; - case _Z_DECL_FORGET_PUBLISHER: - _z_declaration_clear_forget_publisher(&dcl->_body._forget_pub); - break; - case _Z_DECL_FORGET_SUBSCRIBER: - _z_declaration_clear_forget_subscriber(&dcl->_body._forget_sub); - break; - case _Z_DECL_FORGET_QUERYABLE: - _z_declaration_clear_forget_queryable(&dcl->_body._forget_qle); - break; - default: - _Z_DEBUG("WARNING: Trying to clear declaration with unknown ID(%d)\n", did); - break; - } -} - -/*------------------ Data Info Field ------------------*/ -// @TODO: implement builder for _z_data_info_t - -void _z_data_info_clear(_z_data_info_t *di) { - _z_bytes_clear(&di->_encoding.suffix); - _z_bytes_clear(&di->_source_id); - _z_timestamp_clear(&di->_tstamp); -} - -/*------------------ Data Message ------------------*/ -_z_zenoh_message_t _z_msg_make_data(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool can_be_dropped) { - _z_zenoh_message_t msg; - - msg._body._data._key = key; - msg._body._data._info = info; - msg._body._data._payload = payload; - - msg._header = _Z_MID_Z_DATA; - if (msg._body._data._info._flags != 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_Z_I); - } - if (msg._body._data._key._suffix != NULL) { - _Z_SET_FLAG(msg._header, _Z_FLAG_Z_K); - } - if (can_be_dropped == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_Z_D); - } - - return msg; -} - -void _z_msg_clear_data(_z_msg_data_t *msg) { - _z_keyexpr_clear(&msg->_key); - _z_data_info_clear(&msg->_info); - _z_payload_clear(&msg->_payload); -} - -/*------------------ Unit Message ------------------*/ -_z_zenoh_message_t _z_msg_make_unit(_Bool can_be_dropped) { - _z_zenoh_message_t msg; - - msg._header = _Z_MID_Z_UNIT; - if (can_be_dropped == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_Z_D); - } - - return msg; -} - -void _z_msg_clear_unit(_z_msg_unit_t *unt) { (void)(unt); } - -/*------------------ Pull Message ------------------*/ -_z_zenoh_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id, _z_zint_t max_samples, _Bool is_final) { - _z_zenoh_message_t msg; - - msg._body._pull._key = key; - msg._body._pull._pull_id = pull_id; - msg._body._pull._max_samples = max_samples; - - msg._header = _Z_MID_Z_PULL; - if (is_final == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_Z_F); - } - if (max_samples == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_Z_N); - } - if (msg._body._pull._key._suffix != NULL) { - _Z_SET_FLAG(msg._header, _Z_FLAG_Z_K); - } - - return msg; -} - -void _z_msg_clear_pull(_z_msg_pull_t *msg) { _z_keyexpr_clear(&msg->_key); } - -/*------------------ Query Message ------------------*/ -_z_zenoh_message_t _z_msg_make_query(_z_keyexpr_t key, _z_bytes_t parameters, _z_zint_t qid, - z_consolidation_mode_t consolidation, _z_value_t value) { - _z_zenoh_message_t msg; - - msg._body._query._parameters = parameters; - msg._body._query._consolidation = consolidation; - (void)memset(&msg._body._query._info, 0, sizeof(msg._body._query._info)); - msg._body._query._value = value; - - msg._header = _Z_MID_Z_QUERY; - if (msg._body._query._value.payload.len > 0) { - _Z_SET_FLAG(msg._header, _Z_FLAG_Z_B); - } - - return msg; -} - -/*------------------ Reply Message ------------------*/ -_z_zenoh_message_t _z_msg_make_reply(_z_keyexpr_t key, _z_data_info_t info, _z_bytes_t payload, _Bool can_be_dropped) { - _z_zenoh_message_t msg = _z_msg_make_data(key, info, payload, can_be_dropped); - - return msg; -} - -/*------------------ Zenoh Message ------------------*/ -void _z_msg_clear(_z_zenoh_message_t *msg) { - uint8_t mid = _Z_MID(msg->_header); - switch (mid) { - case _Z_MID_Z_DATA: - _z_msg_clear_data(&msg->_body._data); - break; - case _Z_MID_Z_PULL: - _z_msg_clear_pull(&msg->_body._pull); - break; - case _Z_MID_Z_QUERY: - _z_msg_query_clear(&msg->_body._query); - break; - case _Z_MID_Z_UNIT: - _z_msg_clear_unit(&msg->_body._unit); - break; - default: - _Z_DEBUG("WARNING: Trying to clear message with unknown ID(%d)\n", mid); - break; - } -} - -void _z_msg_free(_z_zenoh_message_t **msg) { - _z_zenoh_message_t *ptr = *msg; - - if (ptr != NULL) { - _z_msg_clear(ptr); - - z_free(ptr); - *msg = NULL; - } -} - -void _z_request_body_clear(_z_request_body_t *msg) { (void)(msg); } - -void _z_response_body_clear(_z_response_body_t *msg) { (void)(msg); } - -_z_network_message_t _z_n_msg_make_declare(_z_declaration_t declaration) { - _z_network_message_t msg; - msg._header = _Z_MID_N_DECLARE; - - msg._body._declare._declaration = declaration; - msg._extensions = _z_msg_ext_vec_make(0); - - return msg; -} - -void _z_n_msg_clear_declare(_z_n_msg_declare_t *msg) { _z_declaration_clear(&msg->_declaration); } - -_z_network_message_t _z_n_msg_make_push(_z_keyexpr_t key, _z_push_body_t body, _Bool is_remote_mapping) { - _z_network_message_t msg; - msg._header = _Z_MID_N_PUSH; - - msg._body._push._key = key; - if (key._suffix != NULL) { - _Z_SET_FLAG(msg._header, _Z_FLAG_N_PUSH_N); - } - if (is_remote_mapping == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_N_PUSH_M); - } - msg._body._push._body = body; - - msg._extensions = _z_msg_ext_vec_make(0); - - return msg; -} - -_z_network_message_t _z_n_msg_make_request(_z_zint_t rid, _z_keyexpr_t key, _z_request_body_t body, - _Bool is_remote_mapping) { - _z_network_message_t msg; - msg._header = _Z_MID_N_REQUEST; - - msg._body._request._rid = rid; - msg._body._request._key = key; - if (key._suffix != NULL) { - _Z_SET_FLAG(msg._header, _Z_FLAG_N_REQUEST_N); - } - if (is_remote_mapping == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_N_REQUEST_M); - } - msg._body._request._body = body; - - msg._extensions = _z_msg_ext_vec_make(0); - - return msg; -} - -_z_network_message_t _z_n_msg_make_response(_z_zint_t rid, _z_keyexpr_t key, _z_response_body_t body, - _Bool is_remote_mapping) { - _z_network_message_t msg; - msg._header = _Z_MID_N_RESPONSE; - - msg._body._response._rid = rid; - msg._body._response._key = key; - if (key._suffix != NULL) { - _Z_SET_FLAG(msg._header, _Z_FLAG_N_RESPONSE_N); - } - if (is_remote_mapping == true) { - _Z_SET_FLAG(msg._header, _Z_FLAG_N_RESPONSE_M); - } - msg._body._response._body = body; - - msg._extensions = _z_msg_ext_vec_make(0); - - return msg; -} - -_z_network_message_t _z_n_msg_make_response_final(_z_zint_t rid) { - _z_network_message_t msg; - msg._header = _Z_MID_N_RESPONSE_FINAL; - - msg._body._response_f._requestid = rid; - msg._extensions = _z_msg_ext_vec_make(0); - - return msg; -} \ No newline at end of file diff --git a/src/session/query.c b/src/session/query.c index 8c3042671..34ac9fb1f 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -143,20 +143,16 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_reply_context_t const _z_timestamp_t timestamp) { int8_t ret = _Z_RES_OK; - if (_Z_HAS_FLAG(reply_context->_header, _Z_FLAG_Z_F) == true) { - ret = _Z_ERR_MESSAGE_FLAG_UNEXPECTED; - } - #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - _z_pending_query_t *pen_qry = __unsafe__z_get_pending_query_by_id(zn, reply_context->_qid); + _z_pending_query_t *pen_qry = __unsafe__z_get_pending_query_by_id(zn, reply_context->_request_id); if ((ret == _Z_RES_OK) && (pen_qry == NULL)) { ret = _Z_ERR_ENTITY_UNKNOWN; } - _z_keyexpr_t expanded_ke = __unsafe_z_get_expanded_key_from_key(zn, _Z_RESOURCE_IS_REMOTE, &keyexpr); + _z_keyexpr_t expanded_ke = __unsafe_z_get_expanded_key_from_key(zn, &keyexpr); if ((ret == _Z_RES_OK) && ((pen_qry->_anykey == false) && (_z_keyexpr_intersects(pen_qry->_key._suffix, strlen(pen_qry->_key._suffix), keyexpr._suffix, strlen(keyexpr._suffix)) == false))) { @@ -166,7 +162,7 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_reply_context_t // Build the reply _z_reply_t reply; reply._tag = Z_REPLY_TAG_DATA; - reply.data.replier_id = reply_context->_replier_id; + reply.data.replier_id = zn->_local_zid; reply.data.sample.keyexpr = expanded_ke; _z_bytes_copy(&reply.data.sample.payload, &payload); reply.data.sample.encoding.prefix = encoding.prefix; @@ -206,7 +202,7 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_reply_context_t _z_reply_t partial_reply; (void)memset(&partial_reply, 0, sizeof(_z_reply_t)); // Avoid warnings on uninitialised values on the reply - partial_reply.data.sample.keyexpr = _z_keyexpr_duplicate(&reply.data.sample.keyexpr); + partial_reply.data.sample.keyexpr = _z_keyexpr_duplicate(reply.data.sample.keyexpr); pen_rep->_reply = partial_reply; } else { pen_rep->_reply = reply; // Store the whole reply in the latest mode @@ -242,14 +238,8 @@ int8_t _z_trigger_query_reply_final(_z_session_t *zn, const _z_reply_context_t * _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(reply_context->_header, _Z_FLAG_Z_F) == - false)) { // FIXME: to be checked, but this might never - // happen with the current version of the protocol - ret = _Z_ERR_MESSAGE_FLAG_UNEXPECTED; - } - // Final reply received for unknown query id - _z_pending_query_t *pen_qry = __unsafe__z_get_pending_query_by_id(zn, reply_context->_qid); + _z_pending_query_t *pen_qry = __unsafe__z_get_pending_query_by_id(zn, reply_context->_request_id); if ((ret == _Z_RES_OK) && (pen_qry == NULL)) { ret = _Z_ERR_ENTITY_UNKNOWN; } diff --git a/src/session/queryable.c b/src/session/queryable.c index acd3caa96..20cf3ace6 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -13,10 +13,13 @@ // #include +#include +#include #include "zenoh-pico/config.h" #include "zenoh-pico/net/resource.h" #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/definitions/network.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/session/resource.h" #include "zenoh-pico/session/utils.h" @@ -105,7 +108,7 @@ _z_questionable_sptr_list_t *_z_get_questionable_by_key(_z_session_t *zn, const _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - _z_keyexpr_t key = __unsafe_z_get_expanded_key_from_key(zn, _Z_RESOURCE_IS_LOCAL, keyexpr); + _z_keyexpr_t key = __unsafe_z_get_expanded_key_from_key(zn, keyexpr); _z_questionable_sptr_list_t *qles = __unsafe_z_get_questionable_by_key(zn, key); #if Z_MULTI_THREAD == 1 @@ -136,14 +139,14 @@ _z_questionable_sptr_t *_z_register_questionable(_z_session_t *zn, _z_questionab return ret; } -int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query) { +int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, const _z_keyexpr_t q_key, uint32_t qid) { int8_t ret = _Z_RES_OK; #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - _z_keyexpr_t key = __unsafe_z_get_expanded_key_from_key(zn, _Z_RESOURCE_IS_REMOTE, &query->_key); + _z_keyexpr_t key = __unsafe_z_get_expanded_key_from_key(zn, &q_key); if (key._suffix != NULL) { _z_questionable_sptr_list_t *qles = __unsafe_z_get_questionable_by_key(zn, key); @@ -154,9 +157,12 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query) { // Build the query z_query_t q; q._zn = zn; - q._qid = query->_qid; + q._rid = qid; q._key = key; - q._parameters = query->_parameters; + char params[query->_parameters.len + 1]; + memcpy(params, query->_parameters.start, query->_parameters.len); + params[query->_parameters.len] = 0; + q._parameters = params; q._value.encoding = query->_value.encoding; q._value.payload = query->_value.payload; q._anyke = (strstr(q._parameters, Z_SELECTOR_QUERY_MATCH) == NULL) ? false : true; @@ -171,16 +177,9 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query) { _z_questionable_sptr_list_free(&qles); // Send the final reply - // Final flagged reply context does not encode the ZID - _z_reply_context_t *rctx = _z_msg_make_reply_context(query->_qid, _z_id_empty(), true); - - // Congestion control - _Bool can_be_dropped = false; - // Create the final reply - _z_zenoh_message_t z_msg = _z_msg_make_unit(can_be_dropped); - - if (_z_send_z_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { + _z_zenoh_message_t z_msg = _z_n_msg_make_response_final(q._rid); + if (_z_send_n_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { ret = _Z_ERR_TRANSPORT_TX_FAILED; } _z_msg_clear(&z_msg); diff --git a/src/session/resource.c b/src/session/resource.c index efa3f3acc..e90f1fde2 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -15,7 +15,9 @@ #include "zenoh-pico/session/resource.h" #include +#include +#include "zenoh-pico/api/types.h" #include "zenoh-pico/config.h" #include "zenoh-pico/utils/logging.h" @@ -37,7 +39,7 @@ void _z_resource_free(_z_resource_t **res) { /*------------------ Entity ------------------*/ _z_zint_t _z_get_entity_id(_z_session_t *zn) { return zn->_entity_id++; } -_z_zint_t _z_get_resource_id(_z_session_t *zn) { return zn->_resource_id++; } +uint16_t _z_get_resource_id(_z_session_t *zn) { return zn->_resource_id++; } /*------------------ Resource ------------------*/ _z_resource_t *__z_get_resource_by_id(_z_resource_list_t *rl, const _z_zint_t id) { @@ -149,8 +151,8 @@ _z_resource_t *__unsafe_z_get_resource_by_id(_z_session_t *zn, uint8_t is_local, * Make sure that the following mutexes are locked before calling this function: * - zn->_mutex_inner */ -_z_resource_t *__unsafe_z_get_resource_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr) { - _z_resource_list_t *decls = (is_local == _Z_RESOURCE_IS_LOCAL) ? zn->_local_resources : zn->_remote_resources; +_z_resource_t *__unsafe_z_get_resource_by_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { + _z_resource_list_t *decls = !keyexpr->_uses_remote_mapping ? zn->_local_resources : zn->_remote_resources; return __z_get_resource_by_key(decls, keyexpr); } @@ -159,8 +161,8 @@ _z_resource_t *__unsafe_z_get_resource_by_key(_z_session_t *zn, uint8_t is_local * Make sure that the following mutexes are locked before calling this function: * - zn->_mutex_inner */ -_z_keyexpr_t __unsafe_z_get_expanded_key_from_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr) { - _z_resource_list_t *decls = (is_local == _Z_RESOURCE_IS_LOCAL) ? zn->_local_resources : zn->_remote_resources; +_z_keyexpr_t __unsafe_z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { + _z_resource_list_t *decls = keyexpr->_uses_remote_mapping ? zn->_remote_resources : zn->_local_resources; return __z_get_expanded_key_from_key(decls, keyexpr); } @@ -183,7 +185,7 @@ _z_resource_t *_z_get_resource_by_key(_z_session_t *zn, uint8_t is_local, const _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - _z_resource_t *res = __unsafe_z_get_resource_by_key(zn, is_local, keyexpr); + _z_resource_t *res = __unsafe_z_get_resource_by_key(zn, keyexpr); #if Z_MULTI_THREAD == 1 _z_mutex_unlock(&zn->_mutex_inner); @@ -192,12 +194,12 @@ _z_resource_t *_z_get_resource_by_key(_z_session_t *zn, uint8_t is_local, const return res; } -_z_keyexpr_t _z_get_expanded_key_from_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr) { +_z_keyexpr_t _z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - - _z_keyexpr_t res = __unsafe_z_get_expanded_key_from_key(zn, is_local, keyexpr); + _Bool is_local = !keyexpr->_uses_remote_mapping; + _z_keyexpr_t res = __unsafe_z_get_expanded_key_from_key(zn, keyexpr); #if Z_MULTI_THREAD == 1 _z_mutex_unlock(&zn->_mutex_inner); diff --git a/src/session/rx.c b/src/session/rx.c index 83bd206e8..4c445fe32 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -13,56 +13,73 @@ // #include +#include +#include "zenoh-pico/api/constants.h" +#include "zenoh-pico/api/primitives.h" +#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/protocol/definitions/message.h" +#include "zenoh-pico/protocol/definitions/network.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/session/query.h" #include "zenoh-pico/session/queryable.h" #include "zenoh-pico/session/resource.h" #include "zenoh-pico/session/subscription.h" +#include "zenoh-pico/session/utils.h" #include "zenoh-pico/utils/logging.h" /*------------------ Handle message ------------------*/ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg) { int8_t ret = _Z_RES_OK; - - switch (_Z_MID(msg->_header)) { - case _Z_MID_Z_DATA: { - _Z_INFO("Received _Z_MID_Z_DATA message %d\n", msg->_header); - if (msg->_reply_context != NULL) { // This is some data from a query - ret = _z_trigger_query_reply_partial(zn, msg->_reply_context, msg->_body._data._key, - msg->_body._data._payload, msg->_body._data._info._encoding, - msg->_body._data._info._kind, msg->_body._data._info._tstamp); - } else { // This is pure data - ret = _z_trigger_subscriptions(zn, msg->_body._data._key, msg->_body._data._payload, - msg->_body._data._info._encoding, msg->_body._data._info._kind, - msg->_body._data._info._tstamp); - } + switch (msg->_tag) { + case _Z_N_DECLARE: { } break; - - case _Z_MID_Z_PULL: { - _Z_INFO("Received _Z_PULL message\n"); - // TODO: not supported yet - } break; - - case _Z_MID_Z_QUERY: { - _Z_INFO("Received _Z_QUERY message\n"); - _z_trigger_queryables(zn, &msg->_body._query); + case _Z_N_PUSH: { + _z_n_msg_push_t push = msg->_body._push; + _z_bytes_t payload = push._body._is_put ? push._body._body._put._payload : _z_bytes_empty(); + _z_encoding_t encoding = push._body._is_put ? push._body._body._put._encoding : z_encoding_default(); + int 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); } break; - - case _Z_MID_Z_UNIT: { - _Z_INFO("Received _Z_UNIT message\n"); - // This might be the final reply - if (msg->_reply_context != NULL) { - _z_trigger_query_reply_final(zn, msg->_reply_context); + case _Z_N_REQUEST: { + _z_n_msg_request_t req = msg->_body._request; + switch (req._tag) { + case _Z_REQUEST_QUERY: { + _z_msg_query_t *query = &req._body._query; + ret = _z_trigger_queryables(zn, query, req._key, req._rid); + } break; + case _Z_REQUEST_PUT: { + _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); + if (ret == _Z_RES_OK) { + _z_network_message_t ack = _z_n_msg_make_ack(req._rid, &req._key); + ret = _z_send_n_msg(zn, &ack, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK); + _z_network_message_t final = _z_n_msg_make_response_final(req._rid); + ret |= _z_send_n_msg(zn, &final, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK); + } + } break; + case _Z_REQUEST_DEL: { + _z_msg_del_t del = req._body.del; + int8_t result = _z_trigger_subscriptions(zn, req._key, _z_bytes_empty(), z_encoding_default(), + Z_SAMPLE_KIND_PUT, del._commons._timestamp); + if (ret == _Z_RES_OK) { + _z_network_message_t ack = _z_n_msg_make_ack(req._rid, &req._key); + ret = _z_send_n_msg(zn, &ack, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK); + _z_network_message_t final = _z_n_msg_make_response_final(req._rid); + ret |= _z_send_n_msg(zn, &final, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK); + } + } break; + case _Z_REQUEST_PULL: { // TODO + } break; } } break; - - default: { - _Z_ERROR("Unknown zenoh message ID\n"); - ret = _Z_ERR_MESSAGE_ZENOH_UNKNOWN; - } + case _Z_N_RESPONSE: { + } break; + case _Z_N_RESPONSE_FINAL: { + } break; } - + _z_msg_clear(msg); return ret; } diff --git a/src/session/scout.c b/src/session/scout.c index 33d3afae4..d7e34eb7e 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -16,8 +16,8 @@ #include #include "zenoh-pico/link/manager.h" +#include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/protocol/core.h" -#include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/utils/logging.h" #if Z_SCOUTING_UDP == 1 && Z_LINK_UDP_UNICAST == 0 diff --git a/src/session/subscription.c b/src/session/subscription.c index f7e002b33..6d03f2536 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -157,7 +157,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - _z_keyexpr_t key = __unsafe_z_get_expanded_key_from_key(zn, _Z_RESOURCE_IS_REMOTE, &keyexpr); + _z_keyexpr_t key = __unsafe_z_get_expanded_key_from_key(zn, &keyexpr); if (key._suffix != NULL) { _z_subscription_sptr_list_t *subs = __unsafe_z_get_subscriptions_by_key(zn, _Z_RESOURCE_IS_LOCAL, key); diff --git a/src/session/tx.c b/src/session/tx.c index ef33760ad..ae3697b42 100644 --- a/src/session/tx.c +++ b/src/session/tx.c @@ -16,29 +16,7 @@ #include "zenoh-pico/utils/logging.h" -int8_t _z_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reliability_t reliability, - z_congestion_control_t cong_ctrl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG(">> send zenoh message\n"); - -#if Z_UNICAST_TRANSPORT == 1 - if (zn->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { - ret = _z_unicast_send_z_msg(zn, z_msg, reliability, cong_ctrl); - } else -#endif // Z_UNICAST_TRANSPORT == 1 -#if Z_MULTICAST_TRANSPORT == 1 - if (zn->_tp._type == _Z_TRANSPORT_MULTICAST_TYPE) { - ret = _z_multicast_send_z_msg(zn, z_msg, reliability, cong_ctrl); - } else -#endif // Z_MULTICAST_TRANSPORT == 1 - { - ret = _Z_ERR_TRANSPORT_NOT_AVAILABLE; - } - - return ret; -} - -int8_t _z_send_n_msg(_z_session_t *zn, _z_network_message_t *z_msg, z_reliability_t reliability, +int8_t _z_send_n_msg(_z_session_t *zn, const _z_network_message_t *z_msg, z_reliability_t reliability, z_congestion_control_t cong_ctrl) { int8_t ret = _Z_RES_OK; _Z_DEBUG(">> send network message\n"); diff --git a/src/session/utils.c b/src/session/utils.c index 1fd35b366..215c2717f 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -24,26 +24,6 @@ #include "zenoh-pico/session/subscription.h" /*------------------ clone helpers ------------------*/ -void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src) { - dst->_id = src->_id; - dst->_suffix = src->_suffix ? _z_str_clone(src->_suffix) : NULL; - dst->owns_suffix = true; -} - -_z_keyexpr_t _z_keyexpr_duplicate(const _z_keyexpr_t *src) { - _z_keyexpr_t dst; - _z_keyexpr_copy(&dst, src); - return dst; -} - -_z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src) { - _z_keyexpr_t stolen = *src; - src->owns_suffix = false; - src->_id = 0; - src->_suffix = NULL; - return stolen; -} - _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp) { _z_timestamp_t ts; ts.id = tstamp->id; diff --git a/src/transport/common/rx.c b/src/transport/common/rx.c index 9a0e72490..32f09902b 100644 --- a/src/transport/common/rx.c +++ b/src/transport/common/rx.c @@ -16,6 +16,7 @@ #include +#include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/utils/logging.h" /*------------------ Reception helper ------------------*/ diff --git a/src/transport/common/tx.c b/src/transport/common/tx.c index e1f8146d3..d0340bdb5 100644 --- a/src/transport/common/tx.c +++ b/src/transport/common/tx.c @@ -14,7 +14,7 @@ #include "zenoh-pico/transport/link/tx.h" -#include "zenoh-pico/protocol/msgcodec.h" +#include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/utils/logging.h" /*------------------ Transmission helper ------------------*/ diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index e7adf38f2..78995de07 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -17,6 +17,8 @@ #include #include "zenoh-pico/config.h" +#include "zenoh-pico/protocol/codec/network.h" +#include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/session/utils.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" @@ -148,8 +150,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t // Handle all the zenoh message, one by one size_t len = _z_vec_len(&t_msg->_body._frame._messages); for (size_t i = 0; i < len; i++) { - _z_handle_zenoh_message(ztm->_session, - (_z_zenoh_message_t *)_z_vec_get(&t_msg->_body._frame._messages, i)); + _z_handle_zenoh_message(ztm->_session, _z_network_message_vec_get(&t_msg->_body._frame._messages, i)); } break; @@ -186,7 +187,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t _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_zenoh_message_decode(&zm, &zbf); + int8_t ret = _z_network_message_decode(&zm, &zbf); if (ret == _Z_RES_OK) { _z_handle_zenoh_message(ztm->_session, &zm); _z_msg_clear(&zm); // Clear must be explicitly called for fragmented zenoh messages. Non-fragmented diff --git a/src/transport/multicast/link/task/read.c b/src/transport/multicast/link/task/read.c index 925dfd3e0..45753857a 100644 --- a/src/transport/multicast/link/task/read.c +++ b/src/transport/multicast/link/task/read.c @@ -17,6 +17,7 @@ #include #include "zenoh-pico/config.h" +#include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/transport/link/rx.h" #include "zenoh-pico/utils/logging.h" diff --git a/src/transport/multicast/link/tx.c b/src/transport/multicast/link/tx.c index c310273dc..234929a20 100644 --- a/src/transport/multicast/link/tx.c +++ b/src/transport/multicast/link/tx.c @@ -15,7 +15,8 @@ #include "zenoh-pico/transport/link/tx.h" #include "zenoh-pico/config.h" -#include "zenoh-pico/protocol/msgcodec.h" +#include "zenoh-pico/protocol/codec/network.h" +#include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" @@ -69,62 +70,7 @@ int8_t _z_multicast_send_t_msg(_z_transport_multicast_t *ztm, const _z_transport return ret; } -int8_t _z_multicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reliability_t reliability, - z_congestion_control_t cong_ctrl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG(">> send zenoh message\n"); - - _z_transport_multicast_t *ztm = &zn->_tp._transport._multicast; - - // Acquire the lock and drop the message if needed - _Bool drop = false; - if (cong_ctrl == Z_CONGESTION_CONTROL_BLOCK) { -#if Z_MULTI_THREAD == 1 - _z_mutex_lock(&ztm->_mutex_tx); -#endif // Z_MULTI_THREAD == 1 - } else { -#if Z_MULTI_THREAD == 1 - int8_t locked = _z_mutex_trylock(&ztm->_mutex_tx); - if (locked != (int8_t)0) { - _Z_INFO("Dropping zenoh message because of congestion control\n"); - // We failed to acquire the lock, drop the message - drop = true; - } -#endif // Z_MULTI_THREAD == 1 - } - - if (drop == false) { - // Prepare the buffer eventually reserving space for the message length - __unsafe_z_prepare_wbuf(&ztm->_wbuf, _Z_LINK_IS_STREAMED(ztm->_link._capabilities)); - - _z_zint_t sn = __unsafe_z_multicast_get_sn(ztm, reliability); // Get the next sequence number - - _z_transport_message_t t_msg = _z_t_msg_make_frame_header(sn, reliability); - ret = _z_transport_message_encode(&ztm->_wbuf, &t_msg); // Encode the frame header - if (ret == _Z_RES_OK) { - ret = _z_zenoh_message_encode(&ztm->_wbuf, z_msg); // Encode the zenoh message - if (ret == _Z_RES_OK) { - // Write the message legnth in the reserved space if needed - __unsafe_z_finalize_wbuf(&ztm->_wbuf, _Z_LINK_IS_STREAMED(ztm->_link._capabilities)); - - ret = _z_link_send_wbuf(&ztm->_link, &ztm->_wbuf); // Send the wbuf on the socket - if (ret == _Z_RES_OK) { - ztm->_transmitted = true; // Mark the session that we have transmitted data - } - } else { - // TODO[protocol]: Fragmentation goes here - } - } - -#if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&ztm->_mutex_tx); -#endif // Z_MULTI_THREAD == 1 - } - - return ret; -} - -int8_t _z_multicast_send_n_msg(_z_session_t *zn, _z_network_message_t *n_msg, z_reliability_t reliability, +int8_t _z_multicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg, z_reliability_t reliability, z_congestion_control_t cong_ctrl) { int8_t ret = _Z_RES_OK; _Z_DEBUG(">> send network message\n"); diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index 682956da3..a5db3524d 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -17,6 +17,8 @@ #include #include "zenoh-pico/config.h" +#include "zenoh-pico/protocol/codec/network.h" +#include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/session/utils.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" @@ -151,7 +153,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_zenoh_message_decode(&zm, &zbf); + int8_t ret = _z_network_message_decode(&zm, &zbf); if (ret == _Z_RES_OK) { _z_handle_zenoh_message(ztu->_session, &zm); _z_msg_clear(&zm); // Clear must be explicitly called for fragmented zenoh messages. Non-fragmented diff --git a/src/transport/unicast/link/task/read.c b/src/transport/unicast/link/task/read.c index 201b28e1b..31bc35609 100644 --- a/src/transport/unicast/link/task/read.c +++ b/src/transport/unicast/link/task/read.c @@ -17,6 +17,7 @@ #include #include "zenoh-pico/config.h" +#include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/transport/link/rx.h" #include "zenoh-pico/utils/logging.h" diff --git a/src/transport/unicast/link/tx.c b/src/transport/unicast/link/tx.c index 37017a176..4e56517bd 100644 --- a/src/transport/unicast/link/tx.c +++ b/src/transport/unicast/link/tx.c @@ -15,6 +15,8 @@ #include "zenoh-pico/transport/link/tx.h" #include "zenoh-pico/config.h" +#include "zenoh-pico/protocol/codec/network.h" +#include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/transport/utils.h" @@ -70,68 +72,7 @@ int8_t _z_unicast_send_t_msg(_z_transport_unicast_t *ztu, const _z_transport_mes return ret; } -int8_t _z_unicast_send_z_msg(_z_session_t *zn, _z_zenoh_message_t *z_msg, z_reliability_t reliability, - z_congestion_control_t cong_ctrl) { - int8_t ret = _Z_RES_OK; - _Z_DEBUG(">> send zenoh message\n"); - - _z_transport_unicast_t *ztu = &zn->_tp._transport._unicast; - - // Acquire the lock and drop the message if needed - _Bool drop = false; - if (cong_ctrl == Z_CONGESTION_CONTROL_BLOCK) { -#if Z_MULTI_THREAD == 1 - _z_mutex_lock(&ztu->_mutex_tx); -#endif // Z_MULTI_THREAD == 1 - } else { -#if Z_MULTI_THREAD == 1 - int8_t locked = _z_mutex_trylock(&ztu->_mutex_tx); - if (locked != (int8_t)0) { - _Z_INFO("Dropping zenoh message because of congestion control\n"); - // We failed to acquire the lock, drop the message - drop = true; - } -#endif // Z_MULTI_THREAD == 1 - } - - if (drop == false) { - // Prepare the buffer eventually reserving space for the message length - __unsafe_z_prepare_wbuf(&ztu->_wbuf, _Z_LINK_IS_STREAMED(ztu->_link._capabilities)); - - _z_zint_t sn = __unsafe_z_unicast_get_sn(ztu, reliability); // Get the next sequence number - - _z_transport_message_t t_msg = _z_t_msg_make_frame_header(sn, reliability); - ret = _z_transport_message_encode(&ztu->_wbuf, &t_msg); // Encode the frame header - if (ret == _Z_RES_OK) { - ret = _z_zenoh_message_encode(&ztu->_wbuf, z_msg); // Encode the zenoh message - if (ret == _Z_RES_OK) { - // Write the message legnth in the reserved space if needed - __unsafe_z_finalize_wbuf(&ztu->_wbuf, _Z_LINK_IS_STREAMED(ztu->_link._capabilities)); - - if (ztu->_wbuf._ioss._len == 1) { - ret = _z_link_send_wbuf(&ztu->_link, &ztu->_wbuf); // Send the wbuf on the socket - } else { - // Change the MID - - // for () - } - if (ret == _Z_RES_OK) { - ztu->_transmitted = true; // Mark the session that we have transmitted data - } - } else { - // TODO[protocol]: Fragmentation goes here - } - } - -#if Z_MULTI_THREAD == 1 - _z_mutex_unlock(&ztu->_mutex_tx); -#endif // Z_MULTI_THREAD == 1 - } - - return ret; -} - -int8_t _z_unicast_send_n_msg(_z_session_t *zn, _z_network_message_t *n_msg, z_reliability_t reliability, +int8_t _z_unicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg, z_reliability_t reliability, z_congestion_control_t cong_ctrl) { int8_t ret = _Z_RES_OK; _Z_DEBUG(">> send network message\n"); diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 4324d0326..5ad0c2650 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -562,8 +562,8 @@ void payload_field(void) { printf("\n"); // Free - _z_payload_clear(&e_pld); - _z_payload_clear(&d_pld); + _z_bytes_clear(&e_pld); + _z_bytes_clear(&d_pld); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } @@ -740,161 +740,6 @@ void keyexpr_field(void) { _z_wbuf_clear(&wbf); } -/*------------------ DataInfo field ------------------*/ -_z_data_info_t gen_data_info(void) { - _z_data_info_t di; - - di._flags = 0; - - if (gen_bool()) { - di._kind = gen_uint8(); - _Z_SET_FLAG(di._flags, _Z_DATA_INFO_KIND); - } else { - di._kind = 0; - } - - if (gen_bool()) { - di._encoding.prefix = gen_uint8(); - if (gen_bool()) { - di._encoding.suffix = gen_bytes(8); - } else { - di._encoding.suffix = _z_bytes_empty(); - } - _Z_SET_FLAG(di._flags, _Z_DATA_INFO_ENC); - } else { - di._encoding.prefix = 0; - di._encoding.suffix = _z_bytes_empty(); - } - - if (gen_bool()) { - di._tstamp = gen_timestamp(); - _Z_SET_FLAG(di._flags, _Z_DATA_INFO_TSTAMP); - } else { - _z_timestamp_reset(&di._tstamp); - } - - // WARNING: we do not support sliced content in zenoh-pico. - - if (gen_bool()) { - di._source_id = gen_bytes(16); - _Z_SET_FLAG(di._flags, _Z_DATA_INFO_SRC_ID); - } else { - di._source_id = _z_bytes_empty(); - } - if (gen_bool()) { - di._source_sn = gen_zint(); - _Z_SET_FLAG(di._flags, _Z_DATA_INFO_SRC_SN); - } else { - di._source_sn = 0; - } - - return di; -} - -void assert_eq_data_info(_z_data_info_t *left, _z_data_info_t *right) { - printf("DataInfo -> "); - printf("Flags (%zu:%zu), ", left->_flags, right->_flags); - assert(left->_flags == right->_flags); - - if (_Z_HAS_FLAG(left->_flags, _Z_DATA_INFO_KIND) == true) { - printf("Kind (%d:%d), ", left->_kind, right->_kind); - assert(left->_kind == right->_kind); - } - if (_Z_HAS_FLAG(left->_flags, _Z_DATA_INFO_ENC) == true) { - printf("Encoding (%u %.*s:%u %.*s), ", left->_encoding.prefix, (int)left->_encoding.suffix.len, - left->_encoding.suffix.start, right->_encoding.prefix, (int)right->_encoding.suffix.len, - right->_encoding.suffix.start); - assert(left->_encoding.prefix == right->_encoding.prefix); - assert_eq_uint8_array(&left->_encoding.suffix, &right->_encoding.suffix); - } - if (_Z_HAS_FLAG(left->_flags, _Z_DATA_INFO_TSTAMP) == true) { - printf("Tstamp -> "); - assert_eq_timestamp(&left->_tstamp, &right->_tstamp); - printf(", "); - } - - if (_Z_HAS_FLAG(left->_flags, _Z_DATA_INFO_SRC_ID) == true) { - printf("Src ID -> "); - assert_eq_uint8_array(&left->_source_id, &right->_source_id); - printf(", "); - } - if (_Z_HAS_FLAG(left->_flags, _Z_DATA_INFO_SRC_SN) == true) { - printf("Src SN (%zu:%zu), ", left->_source_sn, right->_source_sn); - assert(left->_source_sn == right->_source_sn); - } -} - -void data_info_field(void) { - printf("\n>> DataInfo field\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_data_info_t e_di = gen_data_info(); - - // Encode - int8_t res = _z_data_info_encode(&wbf, &e_di); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_data_info_t d_di; - res = _z_data_info_decode(&d_di, &zbf); - assert(res == _Z_RES_OK); - - printf(" "); - assert_eq_data_info(&e_di, &d_di); - printf("\n"); - - // Free - _z_data_info_clear(&d_di); - _z_data_info_clear(&e_di); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*=============================*/ -/* Message decorators */ -/*=============================*/ - -/*------------------ ReplyContext decorator ------------------*/ -void print_reply_context(_z_reply_context_t *rc) { - printf(" Header: %x\n", rc->_header); - printf(" QID: %zu\n", rc->_qid); - if (_Z_HAS_FLAG(rc->_header, _Z_FLAG_Z_F) == false) { - printf(" Replier ID: "); - print_uint8_array((_z_bytes_t *)&rc->_replier_id); - } - printf("\n"); -} - -_z_reply_context_t *gen_reply_context(void) { - _z_zint_t qid = gen_zint(); - _z_id_t replier_id = gen_zid(); - _Bool is_final = gen_bool(); - if (is_final == true) { - replier_id = _z_id_empty(); - } - - return _z_msg_make_reply_context(qid, replier_id, is_final); -} - -void assert_eq_reply_context(_z_reply_context_t *left, _z_reply_context_t *right) { - printf("Header (%x:%x), ", left->_header, right->_header); - assert(left->_header == right->_header); - - printf("QID (%zu:%zu), ", left->_qid, right->_qid); - assert(left->_qid == right->_qid); - - printf("Replier ID ("); - if (_Z_HAS_FLAG(left->_header, _Z_FLAG_Z_F) == false) { - assert(memcmp(left->_replier_id.id, right->_replier_id.id, 16) == 0); - } else { - printf("NULL:NULL"); - } - printf(")"); -} - /*=============================*/ /* Declaration Fields */ /*=============================*/ @@ -1714,7 +1559,7 @@ void declare_message(void) { assert_eq_declare_message(&e_dcl, &d_dcl); // Free - _z_n_msg_clear_declare(&d_dcl); + _z_n_msg_declare_clear(&d_dcl); _z_n_msg_clear(&n_msg); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); From 29aa8f7eeb7ec86b8f9e3372cb0fbdb7ca63f858 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Tue, 4 Jul 2023 17:52:49 +0200 Subject: [PATCH 111/181] pre-cmake [skip ci] --- CMakeLists.txt | 334 ++++++++++++++++++++++++++----------------------- 1 file changed, 177 insertions(+), 157 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 847a8256e..bedc8229f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") endif() set(Libname "zenohpico") + if(BUILD_SHARED_LIBS) add_library(${Libname} SHARED) else() @@ -48,6 +49,7 @@ if(NOT CMAKE_C_STANDARD) message(STATUS "Setting C99 as the C Standard") endif() endif() + set(CMAKE_C_STANDARD_REQUIRED TRUE) add_definition(ZENOH_C_STANDARD=${CMAKE_C_STANDARD}) @@ -59,6 +61,7 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE RELEASE) endif() + string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE) if(CMAKE_SYSTEM_NAME MATCHES "Linux") @@ -119,22 +122,41 @@ elseif(CMAKE_BUILD_TYPE MATCHES "RELEASE") endif() endif() -file(GLOB_RECURSE PublicHeaders "include/*.h") +file(GLOB_RECURSE PublicHeaders + "include/zenoh-pico/api/*.h" + "include/zenoh-pico/collections/*.h" + "include/zenoh-pico/link/*.h" + "include/zenoh-pico/net/*.h" + "include/zenoh-pico/protocol/*.h" + "include/zenoh-pico/session/*.h" + "include/zenoh-pico/transport/*.h" + "include/zenoh-pico/utils/*.h" + "include/zenoh-pico/config.h" +) target_include_directories(${Libname} PUBLIC ${PROJECT_SOURCE_DIR}/include) -file(GLOB_RECURSE Sources "src/*.c") +file(GLOB_RECURSE Sources + "src/api/*.c" + "src/collections/*.c" + "src/link/*.c" + "src/net/*.c" + "src/protocol/*.c" + "src/session/*.c" + "src/transport/*.c" + "src/utils/*.c" +) if(WITH_ZEPHYR) - file (GLOB Sources_Zephyr "src/system/zephyr/*.c") + file(GLOB Sources_Zephyr "src/system/zephyr/*.c") list(APPEND Sources ${Sources_Zephyr}) elseif(CMAKE_SYSTEM_NAME MATCHES "Linux" OR CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "BSD") - file (GLOB Sources_Unix "src/system/unix/*.c") + file(GLOB Sources_Unix "src/system/unix/*.c") list(APPEND Sources ${Sources_Unix}) elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten") - file (GLOB Sources_Emscripten "src/system/emscripten/*.c") + file(GLOB Sources_Emscripten "src/system/emscripten/*.c") list(APPEND Sources ${Sources_Emscripten}) elseif(CMAKE_SYSTEM_NAME MATCHES "Windows") - file (GLOB Sources_Windows "src/system/windows/*.c") + file(GLOB Sources_Windows "src/system/windows/*.c") list(APPEND Sources ${Sources_Windows}) endif() @@ -157,177 +179,175 @@ endif() # Build tests, examples, intallation only when project is root # if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) - -option(PACKAGING "Use option on Linux to produce Debian and RPM packages." OFF) -option(BUILD_EXAMPLES "Use this to also build the examples." ON) -option(BUILD_TOOLS "Use this to also build the tools." OFF) -option(BUILD_TESTING "Use this to also build tests." ON) -option(BUILD_INTEGRATION "Use this to also build integration tests." OFF) - -message(STATUS "Produce Debian and RPM packages: ${PACKAGING}") -message(STATUS "Build examples: ${BUILD_EXAMPLES}") -message(STATUS "Build tools: ${BUILD_TOOLS}") -message(STATUS "Build tests: ${BUILD_TESTING}") -message(STATUS "Build integration: ${BUILD_INTEGRATION}") - -install(TARGETS ${Libname} - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib - COMPONENT Library -) -install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/zenoh-pico.h - DESTINATION include - COMPONENT Headers -) -install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/zenoh-pico - DESTINATION include - COMPONENT Headers -) - -if(BUILD_EXAMPLES) - add_subdirectory(examples) -endif() - -if(UNIX OR MSVC) - if(BUILD_TOOLS) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tools) - add_executable(z_keyexpr_canonizer ${PROJECT_SOURCE_DIR}/tools/z_keyexpr_canonizer.c) - target_link_libraries(z_keyexpr_canonizer ${Libname}) + option(PACKAGING "Use option on Linux to produce Debian and RPM packages." OFF) + option(BUILD_EXAMPLES "Use this to also build the examples." ON) + option(BUILD_TOOLS "Use this to also build the tools." OFF) + option(BUILD_TESTING "Use this to also build tests." ON) + option(BUILD_INTEGRATION "Use this to also build integration tests." OFF) + + message(STATUS "Produce Debian and RPM packages: ${PACKAGING}") + message(STATUS "Build examples: ${BUILD_EXAMPLES}") + message(STATUS "Build tools: ${BUILD_TOOLS}") + message(STATUS "Build tests: ${BUILD_TESTING}") + message(STATUS "Build integration: ${BUILD_INTEGRATION}") + + install(TARGETS ${Libname} + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + COMPONENT Library + ) + install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/zenoh-pico.h + DESTINATION include + COMPONENT Headers + ) + install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/zenoh-pico + DESTINATION include + COMPONENT Headers + ) + + if(BUILD_EXAMPLES) + add_subdirectory(examples) endif() - if(BUILD_TESTING) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") - - add_executable(z_data_struct_test ${PROJECT_SOURCE_DIR}/tests/z_data_struct_test.c) - add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c) - add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c) - add_executable(z_msgcodec_test ${PROJECT_SOURCE_DIR}/tests/z_msgcodec_test.c) - add_executable(z_keyexpr_test ${PROJECT_SOURCE_DIR}/tests/z_keyexpr_test.c) - add_executable(z_api_null_drop_test ${PROJECT_SOURCE_DIR}/tests/z_api_null_drop_test.c) - add_executable(z_api_double_drop_test ${PROJECT_SOURCE_DIR}/tests/z_api_double_drop_test.c) - - target_link_libraries(z_data_struct_test ${Libname}) - target_link_libraries(z_endpoint_test ${Libname}) - target_link_libraries(z_iobuf_test ${Libname}) - target_link_libraries(z_msgcodec_test ${Libname}) - target_link_libraries(z_keyexpr_test ${Libname}) - target_link_libraries(z_api_null_drop_test ${Libname}) - target_link_libraries(z_api_double_drop_test ${Libname}) - - enable_testing() - add_test(z_data_struct_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_data_struct_test) - add_test(z_endpoint_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_endpoint_test) - add_test(z_iobuf_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iobuf_test) - add_test(z_msgcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_msgcodec_test) - 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) - endif() - - if(BUILD_MULTICAST) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") - - if(CMAKE_C_STANDARD MATCHES "11") - add_executable(z_peer_multicast_test ${PROJECT_SOURCE_DIR}/tests/z_peer_multicast_test.c) - target_link_libraries(z_peer_multicast_test ${Libname}) + if(UNIX OR MSVC) + if(BUILD_TOOLS) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tools) + add_executable(z_keyexpr_canonizer ${PROJECT_SOURCE_DIR}/tools/z_keyexpr_canonizer.c) + target_link_libraries(z_keyexpr_canonizer ${Libname}) + endif() - configure_file(${PROJECT_SOURCE_DIR}/tests/multicast.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh COPYONLY) + if(BUILD_TESTING) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") + + add_executable(z_data_struct_test ${PROJECT_SOURCE_DIR}/tests/z_data_struct_test.c) + add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c) + add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c) + add_executable(z_msgcodec_test ${PROJECT_SOURCE_DIR}/tests/z_msgcodec_test.c) + add_executable(z_keyexpr_test ${PROJECT_SOURCE_DIR}/tests/z_keyexpr_test.c) + add_executable(z_api_null_drop_test ${PROJECT_SOURCE_DIR}/tests/z_api_null_drop_test.c) + add_executable(z_api_double_drop_test ${PROJECT_SOURCE_DIR}/tests/z_api_double_drop_test.c) + + target_link_libraries(z_data_struct_test ${Libname}) + target_link_libraries(z_endpoint_test ${Libname}) + target_link_libraries(z_iobuf_test ${Libname}) + target_link_libraries(z_msgcodec_test ${Libname}) + target_link_libraries(z_keyexpr_test ${Libname}) + target_link_libraries(z_api_null_drop_test ${Libname}) + target_link_libraries(z_api_double_drop_test ${Libname}) enable_testing() - add_test(z_peer_multicast_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh z_peer_multicast_test) + add_test(z_data_struct_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_data_struct_test) + add_test(z_endpoint_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_endpoint_test) + add_test(z_iobuf_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iobuf_test) + add_test(z_msgcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_msgcodec_test) + 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) endif() - endif() - if(BUILD_INTEGRATION) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") + if(BUILD_MULTICAST) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") - if(CMAKE_C_STANDARD MATCHES "11") - add_executable(z_client_test ${PROJECT_SOURCE_DIR}/tests/z_client_test.c) - add_executable(z_api_alignment_test ${PROJECT_SOURCE_DIR}/tests/z_api_alignment_test.c) - add_executable(z_session_test ${PROJECT_SOURCE_DIR}/tests/z_session_test.c) + if(CMAKE_C_STANDARD MATCHES "11") + add_executable(z_peer_multicast_test ${PROJECT_SOURCE_DIR}/tests/z_peer_multicast_test.c) + target_link_libraries(z_peer_multicast_test ${Libname}) - target_link_libraries(z_client_test ${Libname}) - target_link_libraries(z_api_alignment_test ${Libname}) - target_link_libraries(z_session_test ${Libname}) + configure_file(${PROJECT_SOURCE_DIR}/tests/multicast.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh COPYONLY) - configure_file(${PROJECT_SOURCE_DIR}/tests/routed.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh COPYONLY) - configure_file(${PROJECT_SOURCE_DIR}/tests/api.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh COPYONLY) - - enable_testing() - add_test(z_client_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh z_client_test) - add_test(z_api_alignment_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh z_api_alignment_test) - add_test(z_session_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh z_session_test) + enable_testing() + add_test(z_peer_multicast_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh z_peer_multicast_test) + endif() endif() - endif() -endif() -# For packaging -if(PACKAGING) - set(CPACK_PACKAGE_DIRECTORY "${CMAKE_BINARY_DIR}/packages") - - set(CPACK_COMPONENTS_ALL Library Headers) - set(CPACK_COMPONENT_LIBRARY_GROUP "lib") - set(CPACK_COMPONENT_HEADERS_GROUP "dev") - set(CPACK_COMPONENT_HEADERS_DEPENDS Library) - - set(CPACK_PACKAGE_CHECKSUM MD5) - set(CPACK_PACKAGE_VENDOR "The Eclipse Foundation") - set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) - set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR}) - set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH}) - set(CPACK_COMPONENT_LIB_DESCRIPTION "The C client library for Eclipse zenoh targeting pico devices") - set(CPACK_COMPONENT_DEV_DESCRIPTION "${CPACK_COMPONENT_LIB_DESCRIPTION} - devel files") - - # Sources package - set(CPACK_SOURCE_GENERATOR "TGZ") - set(CPACK_SOURCE_IGNORE_FILES "/.git/;/.github/;/build/;/crossbuilds/") - set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-src-${PROJECT_VERSION}") - - if(PACKAGING MATCHES "DEB") - if(NOT DEBARCH) - set(DEBARCH ${CMAKE_SYSTEM_PROCESSOR}) - endif() + if(BUILD_INTEGRATION) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") - message(STATUS "Configure DEB packaging for Linux ${DEBARCH}") + if(CMAKE_C_STANDARD MATCHES "11") + add_executable(z_client_test ${PROJECT_SOURCE_DIR}/tests/z_client_test.c) + add_executable(z_api_alignment_test ${PROJECT_SOURCE_DIR}/tests/z_api_alignment_test.c) + add_executable(z_session_test ${PROJECT_SOURCE_DIR}/tests/z_session_test.c) - if(CPACK_GENERATOR) - set(CPACK_GENERATOR "${CPACK_GENERATOR};DEB") - else() - set(CPACK_GENERATOR "DEB") - endif() + target_link_libraries(z_client_test ${Libname}) + target_link_libraries(z_api_alignment_test ${Libname}) + target_link_libraries(z_session_test ${Libname}) - # DEB package - set(CPACK_DEBIAN_PACKAGE_MAINTAINER "ZettaScale Zenoh Team, ") - set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE ${DEBARCH}) - set(CPACK_DEB_COMPONENT_INSTALL ON) - set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT) - set(CPACK_DEBIAN_LIB_PACKAGE_NAME ${PROJECT_NAME}) # avoid "-lib" suffix for "lib" package - set(CPACK_DEBIAN_LIB_PACKAGE_DEPENDS "libc6 (>=2.12)") - set(CPACK_DEBIAN_DEV_PACKAGE_DEPENDS "${CPACK_DEBIAN_LIB_PACKAGE_NAME} (=${PROJECT_VERSION})") - endif() + configure_file(${PROJECT_SOURCE_DIR}/tests/routed.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh COPYONLY) + configure_file(${PROJECT_SOURCE_DIR}/tests/api.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh COPYONLY) - if(PACKAGING MATCHES "RPM") - if(NOT RPMARCH) - set(RPMARCH ${CMAKE_SYSTEM_PROCESSOR}) + enable_testing() + add_test(z_client_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh z_client_test) + add_test(z_api_alignment_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh z_api_alignment_test) + add_test(z_session_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh z_session_test) + endif() endif() + endif() - message(STATUS "Configure RPM packaging for Linux ${RPMARCH}") + # For packaging + if(PACKAGING) + set(CPACK_PACKAGE_DIRECTORY "${CMAKE_BINARY_DIR}/packages") + + set(CPACK_COMPONENTS_ALL Library Headers) + set(CPACK_COMPONENT_LIBRARY_GROUP "lib") + set(CPACK_COMPONENT_HEADERS_GROUP "dev") + set(CPACK_COMPONENT_HEADERS_DEPENDS Library) + + set(CPACK_PACKAGE_CHECKSUM MD5) + set(CPACK_PACKAGE_VENDOR "The Eclipse Foundation") + set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) + set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR}) + set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH}) + set(CPACK_COMPONENT_LIB_DESCRIPTION "The C client library for Eclipse zenoh targeting pico devices") + set(CPACK_COMPONENT_DEV_DESCRIPTION "${CPACK_COMPONENT_LIB_DESCRIPTION} - devel files") + + # Sources package + set(CPACK_SOURCE_GENERATOR "TGZ") + set(CPACK_SOURCE_IGNORE_FILES "/.git/;/.github/;/build/;/crossbuilds/") + set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-src-${PROJECT_VERSION}") + + if(PACKAGING MATCHES "DEB") + if(NOT DEBARCH) + set(DEBARCH ${CMAKE_SYSTEM_PROCESSOR}) + endif() + + message(STATUS "Configure DEB packaging for Linux ${DEBARCH}") + + if(CPACK_GENERATOR) + set(CPACK_GENERATOR "${CPACK_GENERATOR};DEB") + else() + set(CPACK_GENERATOR "DEB") + endif() + + # DEB package + set(CPACK_DEBIAN_PACKAGE_MAINTAINER "ZettaScale Zenoh Team, ") + set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE ${DEBARCH}) + set(CPACK_DEB_COMPONENT_INSTALL ON) + set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT) + set(CPACK_DEBIAN_LIB_PACKAGE_NAME ${PROJECT_NAME}) # avoid "-lib" suffix for "lib" package + set(CPACK_DEBIAN_LIB_PACKAGE_DEPENDS "libc6 (>=2.12)") + set(CPACK_DEBIAN_DEV_PACKAGE_DEPENDS "${CPACK_DEBIAN_LIB_PACKAGE_NAME} (=${PROJECT_VERSION})") + endif() - if(CPACK_GENERATOR) - set(CPACK_GENERATOR "${CPACK_GENERATOR};RPM") - else() - set(CPACK_GENERATOR "RPM") + if(PACKAGING MATCHES "RPM") + if(NOT RPMARCH) + set(RPMARCH ${CMAKE_SYSTEM_PROCESSOR}) + endif() + + message(STATUS "Configure RPM packaging for Linux ${RPMARCH}") + + if(CPACK_GENERATOR) + set(CPACK_GENERATOR "${CPACK_GENERATOR};RPM") + else() + set(CPACK_GENERATOR "RPM") + endif() + + # RPM package + set(CPACK_RPM_PACKAGE_ARCHITECTURE ${RPMARCH}) + set(CPACK_RPM_COMPONENT_INSTALL ON) + set(CPACK_RPM_FILE_NAME RPM-DEFAULT) + set(CPACK_RPM_LIB_PACKAGE_NAME ${PROJECT_NAME}) # avoid "-lib" suffix for "lib" package + set(CPACK_RPM_DEV_PACKAGE_REQUIRES "${CPACK_RPM_LIB_PACKAGE_NAME} = ${PROJECT_VERSION}") endif() - # RPM package - set(CPACK_RPM_PACKAGE_ARCHITECTURE ${RPMARCH}) - set(CPACK_RPM_COMPONENT_INSTALL ON) - set(CPACK_RPM_FILE_NAME RPM-DEFAULT) - set(CPACK_RPM_LIB_PACKAGE_NAME ${PROJECT_NAME}) # avoid "-lib" suffix for "lib" package - set(CPACK_RPM_DEV_PACKAGE_REQUIRES "${CPACK_RPM_LIB_PACKAGE_NAME} = ${PROJECT_VERSION}") + include(CPack) endif() - - include(CPack) -endif() - endif() From ef3e9569b1ef8332acae8408bb9f564636aeb41b Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Tue, 4 Jul 2023 17:58:05 +0200 Subject: [PATCH 112/181] now to fix the tests [skip ci] --- include/zenoh-pico/protocol/core.h | 4 ++-- include/zenoh-pico/protocol/definitions/message.h | 4 ++-- include/zenoh-pico/protocol/definitions/network.h | 15 ++------------- src/protocol/definitions/message.c | 4 ++++ src/protocol/definitions/network.c | 12 ++++++++++++ tests/z_msgcodec_test.c | 4 +--- 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 2500fb73b..bbf835d26 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -71,7 +71,7 @@ typedef struct { } _z_timestamp_t; _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp); -_z_timestamp_t _z_timestamp_null(); +_z_timestamp_t _z_timestamp_null(void); void _z_timestamp_clear(_z_timestamp_t *tstamp); _Bool _z_timestamp_check(const _z_timestamp_t *stamp); @@ -182,7 +182,7 @@ typedef struct { uint32_t _entity_id; uint32_t _source_sn; } _z_source_info_t; -_z_source_info_t _z_source_info_null(); +_z_source_info_t _z_source_info_null(void); typedef struct { uint32_t _request_id; diff --git a/include/zenoh-pico/protocol/definitions/message.h b/include/zenoh-pico/protocol/definitions/message.h index aa369fdb6..385ff7ba3 100644 --- a/include/zenoh-pico/protocol/definitions/message.h +++ b/include/zenoh-pico/protocol/definitions/message.h @@ -131,7 +131,7 @@ typedef struct { typedef struct { _z_source_info_t _ext_source_info; } _z_msg_pull_t; -static inline void _z_msg_pull_clear(_z_msg_pull_t *pull) {} +static inline void _z_msg_pull_clear(_z_msg_pull_t *pull) { (void)pull; } typedef struct { _z_timestamp_t _timestamp; @@ -141,7 +141,7 @@ typedef struct { typedef struct { _z_m_push_commons_t _commons; } _z_msg_del_t; -static inline void _z_msg_del_clear(_z_msg_del_t *del) {} +static inline void _z_msg_del_clear(_z_msg_del_t *del) { (void)del; } #define _Z_M_DEL_ID 0x02 #define _Z_FLAG_Z_D_T 0x20 diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index ffbdbee63..de992c5e8 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -125,7 +125,7 @@ typedef struct { _z_msg_put_t _put; } _body; } _z_push_body_t; -_z_push_body_t _z_push_body_null(); +_z_push_body_t _z_push_body_null(void); _z_push_body_t _z_push_body_steal(_z_push_body_t *msg); void _z_push_body_clear(_z_push_body_t *msg); @@ -231,18 +231,7 @@ _z_network_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id); _z_network_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes_t) parameters, _z_zint_t qid, z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value); _z_network_message_t _z_n_msg_make_reply(_z_zint_t rid, _Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_value_t) value); -_z_network_message_t _z_n_msg_make_ack(_z_zint_t rid, _Z_MOVE(_z_keyexpr_t) key) { - return (_z_network_message_t){ - ._tag = _Z_N_RESPONSE, - ._body._response = - { - ._tag = _Z_RESPONSE_BODY_ACK, - ._request_id = rid, - ._key = _z_keyexpr_steal(key), - ._body._ack = {._timestamp = _z_timestamp_null(), ._ext_source_info = _z_source_info_null()}, - }, - }; -} +_z_network_message_t _z_n_msg_make_ack(_z_zint_t rid, _Z_MOVE(_z_keyexpr_t) key); _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); _z_network_message_t _z_n_msg_make_push(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_push_body_t) body); diff --git a/src/protocol/definitions/message.c b/src/protocol/definitions/message.c index a0587e6b5..677b20bdb 100644 --- a/src/protocol/definitions/message.c +++ b/src/protocol/definitions/message.c @@ -18,3 +18,7 @@ void _z_msg_query_clear(_z_msg_query_t *msg) { _z_bytes_clear(&msg->_parameters); _z_value_clear(&msg->_value); } +void _z_msg_err_clear(_z_msg_err_t *err) { + _z_timestamp_clear(&err->_timestamp); + _z_value_clear(&err->_ext_value); +} diff --git a/src/protocol/definitions/network.c b/src/protocol/definitions/network.c index 52d8031e2..f5d6938c8 100644 --- a/src/protocol/definitions/network.c +++ b/src/protocol/definitions/network.c @@ -226,3 +226,15 @@ _z_network_message_t _z_n_msg_make_reply(_z_zint_t rid, _Z_MOVE(_z_keyexpr_t) ke }; } +_z_network_message_t _z_n_msg_make_ack(_z_zint_t rid, _Z_MOVE(_z_keyexpr_t) key) { + return (_z_network_message_t){ + ._tag = _Z_N_RESPONSE, + ._body._response = + { + ._tag = _Z_RESPONSE_BODY_ACK, + ._request_id = rid, + ._key = _z_keyexpr_steal(key), + ._body._ack = {._timestamp = _z_timestamp_null(), ._ext_source_info = _z_source_info_null()}, + }, + }; +} diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 5ad0c2650..678b221e2 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -26,12 +26,10 @@ #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/string.h" +#include "zenoh-pico/protocol/codec/ext.h" #include "zenoh-pico/protocol/ext.h" -#include "zenoh-pico/protocol/extcodec.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/keyexpr.h" -#include "zenoh-pico/protocol/msg.h" -#include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/system/platform.h" #define RUNS 1000 From 2ba610c8776c1cbf68683cdf7a3dd3838b97cea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Thu, 6 Jul 2023 11:52:27 +0200 Subject: [PATCH 113/181] Fix ping-pong examples (#226) (#227) (#228) * Fix ping-pong examples (#226) (#227) * Fix ping-pong examples (#226) (#227) * Address review comments and stop tasks * C99 uses the explicit way * Fix variable name --- examples/unix/c11/z_ping.c | 29 +++++++++++++++++++++++++-- examples/unix/c11/z_pong.c | 27 ++++++++++++++++++++++++- examples/unix/c99/z_ping.c | 40 +++++++++++++++++++++++++++++++------- examples/unix/c99/z_pong.c | 39 +++++++++++++++++++++++++++++++------ examples/windows/z_ping.c | 30 +++++++++++++++++++++++++--- examples/windows/z_pong.c | 27 ++++++++++++++++++++++++- 6 files changed, 172 insertions(+), 20 deletions(-) diff --git a/examples/unix/c11/z_ping.c b/examples/unix/c11/z_ping.c index 2472196fe..d4ff47adb 100644 --- a/examples/unix/c11/z_ping.c +++ b/examples/unix/c11/z_ping.c @@ -46,11 +46,32 @@ int main(int argc, char** argv) { _z_condvar_init(&cond); z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_move(config)); + if (!z_check(session)) { + printf("Unable to open session!\n"); + return -1; + } + + if (zp_start_read_task(z_loan(session), NULL) < 0 || zp_start_lease_task(z_loan(session), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_publisher_t pub = z_declare_publisher(z_loan(session), ping, NULL); - z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)(&pub)); + if (!z_check(pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + z_owned_closure_sample_t respond = z_closure(callback, drop, NULL); z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), pong, z_move(respond), NULL); + if (!z_check(sub)) { + printf("Unable to declare subscriber for key expression.\n"); + return -1; + } + uint8_t* data = z_malloc(args.size); for (unsigned int i = 0; i < args.size; i++) { data[i] = i % 10; @@ -79,8 +100,12 @@ int main(int argc, char** argv) { _z_mutex_unlock(&mutex); z_free(results); z_free(data); - z_drop(z_move(sub)); z_drop(z_move(pub)); + z_drop(z_move(sub)); + + zp_stop_read_task(z_loan(session)); + zp_stop_lease_task(z_loan(session)); + z_close(z_move(session)); } diff --git a/examples/unix/c11/z_pong.c b/examples/unix/c11/z_pong.c index 7f0731478..c0d1cd0b0 100644 --- a/examples/unix/c11/z_pong.c +++ b/examples/unix/c11/z_pong.c @@ -19,13 +19,38 @@ int main(int argc, char** argv) { (void)argv; z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_move(config)); - z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + if (!z_check(session)) { + printf("Unable to open session!\n"); + return -1; + } + + if (zp_start_read_task(z_loan(session), NULL) < 0 || zp_start_lease_task(z_loan(session), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); z_owned_publisher_t pub = z_declare_publisher(z_loan(session), pong, NULL); + if (!z_check(pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub)); z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), ping, z_move(respond), NULL); + if (!z_check(sub)) { + printf("Unable to declare subscriber for key expression.\n"); + return -1; + } + while (getchar() != 'q') { } + z_drop(z_move(sub)); + + zp_stop_read_task(z_loan(session)); + zp_stop_lease_task(z_loan(session)); + z_close(z_move(session)); } \ No newline at end of file diff --git a/examples/unix/c99/z_ping.c b/examples/unix/c99/z_ping.c index 9bbb18695..379afd986 100644 --- a/examples/unix/c99/z_ping.c +++ b/examples/unix/c99/z_ping.c @@ -44,12 +44,34 @@ int main(int argc, char** argv) { _z_mutex_init(&mutex); _z_condvar_init(&cond); z_owned_config_t config = z_config_default(); - z_owned_session_t session = z_open(z_move(config)); + z_owned_session_t session = z_open(z_config_move(&config)); + if (!z_session_check(&session)) { + printf("Unable to open session!\n"); + return -1; + } + + if (zp_start_read_task(z_session_loan(&session), NULL) < 0 || + zp_start_lease_task(z_session_loan(&session), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); - z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); z_owned_publisher_t pub = z_declare_publisher(z_session_loan(&session), ping, NULL); - z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)(&pub)); - z_owned_subscriber_t sub = z_declare_subscriber(z_session_loan(&session), pong, z_move(respond), NULL); + if (!z_publisher_check(&pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_closure_sample_t respond = z_closure_sample(callback, drop, NULL); + z_owned_subscriber_t sub = + z_declare_subscriber(z_session_loan(&session), pong, z_closure_sample_move(&respond), NULL); + if (!z_subscriber_check(&sub)) { + printf("Unable to declare subscriber for key expression.\n"); + return -1; + } + uint8_t* data = z_malloc(args.size); for (unsigned int i = 0; i < args.size; i++) { data[i] = i % 10; @@ -78,9 +100,13 @@ int main(int argc, char** argv) { _z_mutex_unlock(&mutex); z_free(results); z_free(data); - z_undeclare_subscriber(z_move(sub)); - z_undeclare_publisher(z_move(pub)); - z_close(z_move(session)); + z_undeclare_subscriber(z_subscriber_move(&sub)); + z_undeclare_publisher(z_publisher_move(&pub)); + + zp_stop_read_task(z_session_loan(&session)); + zp_stop_lease_task(z_session_loan(&session)); + + z_close(z_session_move(&session)); } char* getopt(int argc, char** argv, char option) { diff --git a/examples/unix/c99/z_pong.c b/examples/unix/c99/z_pong.c index 49c99c488..1f0955d56 100644 --- a/examples/unix/c99/z_pong.c +++ b/examples/unix/c99/z_pong.c @@ -19,14 +19,41 @@ int main(int argc, char** argv) { (void)argc; (void)argv; z_owned_config_t config = z_config_default(); - z_owned_session_t session = z_open(z_move(config)); - z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + z_owned_session_t session = z_open(z_config_move(&config)); + if (!z_session_check(&session)) { + printf("Unable to open session!\n"); + return -1; + } + + if (zp_start_read_task(z_session_loan(&session), NULL) < 0 || + zp_start_lease_task(z_session_loan(&session), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); z_owned_publisher_t pub = z_declare_publisher(z_session_loan(&session), pong, NULL); - z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub)); - z_owned_subscriber_t sub = z_declare_subscriber(z_session_loan(&session), ping, z_move(respond), NULL); + if (!z_publisher_check(&pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + z_owned_closure_sample_t respond = z_closure_sample(callback, drop, (void*)z_publisher_move(&pub)); + z_owned_subscriber_t sub = + z_declare_subscriber(z_session_loan(&session), ping, z_closure_sample_move(&respond), NULL); + if (!z_subscriber_check(&sub)) { + printf("Unable to declare subscriber for key expression.\n"); + return -1; + } + while (getchar() != 'q') { } - z_undeclare_subscriber(z_move(sub)); - z_close(z_move(session)); + + z_undeclare_subscriber(z_subscriber_move(&sub)); + + zp_stop_read_task(z_session_loan(&session)); + zp_stop_lease_task(z_session_loan(&session)); + + z_close(z_session_move(&session)); } \ No newline at end of file diff --git a/examples/windows/z_ping.c b/examples/windows/z_ping.c index 1ce6759ef..fe25ed3c9 100644 --- a/examples/windows/z_ping.c +++ b/examples/windows/z_ping.c @@ -44,11 +44,31 @@ int main(int argc, char** argv) { _z_condvar_init(&cond); z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_move(config)); + if (!z_check(session)) { + printf("Unable to open session!\n"); + return -1; + } + + if (zp_start_read_task(z_loan(session), NULL) < 0 || zp_start_lease_task(z_loan(session), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); - z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); z_owned_publisher_t pub = z_declare_publisher(z_loan(session), ping, NULL); - z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)(&pub)); + if (!z_check(pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_closure_sample_t respond = z_closure(callback, drop, NULL); z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), pong, z_move(respond), NULL); + if (!z_check(sub)) { + printf("Unable to declare subscriber for key expression.\n"); + return -1; + } + uint8_t* data = z_malloc(args.size); for (unsigned int i = 0; i < args.size; i++) { data[i] = i % 10; @@ -77,8 +97,12 @@ int main(int argc, char** argv) { _z_mutex_unlock(&mutex); z_free(results); z_free(data); - z_drop(z_move(sub)); z_drop(z_move(pub)); + z_drop(z_move(sub)); + + zp_stop_read_task(z_loan(session)); + zp_stop_lease_task(z_loan(session)); + z_close(z_move(session)); } diff --git a/examples/windows/z_pong.c b/examples/windows/z_pong.c index 7f0731478..c0d1cd0b0 100644 --- a/examples/windows/z_pong.c +++ b/examples/windows/z_pong.c @@ -19,13 +19,38 @@ int main(int argc, char** argv) { (void)argv; z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_move(config)); - z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + if (!z_check(session)) { + printf("Unable to open session!\n"); + return -1; + } + + if (zp_start_read_task(z_loan(session), NULL) < 0 || zp_start_lease_task(z_loan(session), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); z_owned_publisher_t pub = z_declare_publisher(z_loan(session), pong, NULL); + if (!z_check(pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub)); z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), ping, z_move(respond), NULL); + if (!z_check(sub)) { + printf("Unable to declare subscriber for key expression.\n"); + return -1; + } + while (getchar() != 'q') { } + z_drop(z_move(sub)); + + zp_stop_read_task(z_loan(session)); + zp_stop_lease_task(z_loan(session)); + z_close(z_move(session)); } \ No newline at end of file From 7fe228bf7f9ea244a50b4a4624bcd5934f074735 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 7 Jul 2023 14:00:33 +0200 Subject: [PATCH 114/181] part of tests passing [skip ci] --- CMakeLists.txt | 9 +- include/zenoh-pico/protocol/codec/core.h | 7 + .../zenoh-pico/protocol/codec/declarations.h | 23 + include/zenoh-pico/protocol/codec/message.h | 3 + .../protocol/definitions/declarations.h | 13 +- .../zenoh-pico/protocol/definitions/message.h | 8 +- .../zenoh-pico/protocol/definitions/network.h | 16 +- include/zenoh-pico/protocol/ext.h | 3 +- include/zenoh-pico/protocol/keyexpr.h | 3 + include/zenoh-pico/protocol/msgcodec.h | 3 - src/protocol/codec.c | 38 + src/protocol/codec/declarations.c | 45 +- src/protocol/codec/ext.c | 17 +- src/protocol/codec/message.c | 160 +- src/protocol/codec/network.c | 41 +- src/protocol/core.c | 1 + src/protocol/definitions/declarations.c | 13 +- src/protocol/definitions/message.c | 8 +- src/protocol/definitions/network.c | 44 +- src/protocol/iobuf.c | 2 +- src/session/queryable.c | 4 +- src/session/rx.c | 4 +- src/transport/multicast/link/rx.c | 2 +- tests/z_msgcodec_test.c | 2533 ++++------------- 24 files changed, 880 insertions(+), 2120 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bedc8229f..042c2855a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,7 +62,7 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE RELEASE) endif() -string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE) +string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UPPER) if(CMAKE_SYSTEM_NAME MATCHES "Linux") add_definition(ZENOH_LINUX) @@ -104,7 +104,7 @@ endif() set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) -if(CMAKE_BUILD_TYPE MATCHES "DEBUG") +if(CMAKE_BUILD_TYPE_UPPER MATCHES "DEBUG") if(UNIX) add_compile_options(-c -Wall -Wextra -Werror -Wunused -Wstrict-prototypes -pipe -g -O0) elseif(MSVC) @@ -112,7 +112,7 @@ if(CMAKE_BUILD_TYPE MATCHES "DEBUG") 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") +elseif(CMAKE_BUILD_TYPE_UPPER MATCHES "RELEASE") if(UNIX) add_compile_options(-pipe -O3) elseif(MSVC) @@ -217,8 +217,7 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) endif() if(BUILD_TESTING) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") - + # set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests") add_executable(z_data_struct_test ${PROJECT_SOURCE_DIR}/tests/z_data_struct_test.c) add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c) add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c) diff --git a/include/zenoh-pico/protocol/codec/core.h b/include/zenoh-pico/protocol/codec/core.h index 241ce5712..222fb5922 100644 --- a/include/zenoh-pico/protocol/codec/core.h +++ b/include/zenoh-pico/protocol/codec/core.h @@ -15,6 +15,7 @@ #ifndef INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_CORE_H #define INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_CORE_H +#include #include #include "zenoh-pico/protocol/core.h" @@ -52,9 +53,12 @@ int8_t _z_uint16_decode(uint16_t *u16, _z_zbuf_t *buf); int8_t _z_uint64_encode(_z_wbuf_t *buf, uint64_t v); int8_t _z_uint64_decode(uint64_t *u64, _z_zbuf_t *buf); +uint8_t _z_zint_len(_z_zint_t v); int8_t _z_zint_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_zint_decode(_z_zint_t *zint, _z_zbuf_t *buf); int8_t _z_bytes_val_encode(_z_wbuf_t *buf, const _z_bytes_t *bs); @@ -62,6 +66,7 @@ int8_t _z_bytes_val_decode(_z_bytes_t *bs, _z_zbuf_t *buf); int8_t _z_bytes_val_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf); int8_t _z_bytes_encode(_z_wbuf_t *buf, const _z_bytes_t *bs); +size_t _z_bytes_encode_len(const _z_bytes_t *bs); int8_t _z_bytes_decode(_z_bytes_t *bs, _z_zbuf_t *buf); int8_t _z_zbuf_read_exact(_z_zbuf_t *zbf, uint8_t *dest, size_t length); @@ -76,5 +81,7 @@ int8_t _z_keyexpr_encode(_z_wbuf_t *buf, _Bool has_suffix, const _z_keyexpr_t *k int8_t _z_keyexpr_decode(_z_keyexpr_t *ke, _z_zbuf_t *buf, _Bool has_suffix); int8_t _z_timestamp_encode(_z_wbuf_t *buf, const _z_timestamp_t *ts); +int8_t _z_timestamp_encode_ext(_z_wbuf_t *buf, const _z_timestamp_t *ts); int8_t _z_timestamp_decode(_z_timestamp_t *ts, _z_zbuf_t *buf); + #endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_CORE_H */ diff --git a/include/zenoh-pico/protocol/codec/declarations.h b/include/zenoh-pico/protocol/codec/declarations.h index 1d140f3b5..1e4ffa631 100644 --- a/include/zenoh-pico/protocol/codec/declarations.h +++ b/include/zenoh-pico/protocol/codec/declarations.h @@ -31,6 +31,29 @@ #define _Z_DECL_INTEREST_MID 8 #define _Z_FINAL_INTEREST_MID 9 #define _Z_UNDECL_INTEREST_MID 10 +int8_t _z_decl_kexpr_encode(_z_wbuf_t *wbf, const _z_decl_kexpr_t *decl); +int8_t _z_decl_kexpr_decode(_z_decl_kexpr_t *decl, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_undecl_kexpr_encode(_z_wbuf_t *wbf, const _z_undecl_kexpr_t *decl); +int8_t _z_undecl_kexpr_decode(_z_undecl_kexpr_t *decl, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_decl_subscriber_encode(_z_wbuf_t *wbf, const _z_decl_subscriber_t *decl); +int8_t _z_decl_subscriber_decode(_z_decl_subscriber_t *decl, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_undecl_subscriber_encode(_z_wbuf_t *wbf, const _z_undecl_subscriber_t *decl); +int8_t _z_undecl_subscriber_decode(_z_undecl_subscriber_t *decl, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_decl_queryable_encode(_z_wbuf_t *wbf, const _z_decl_queryable_t *decl); +int8_t _z_decl_queryable_decode(_z_decl_queryable_t *decl, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_undecl_queryable_encode(_z_wbuf_t *wbf, const _z_undecl_queryable_t *decl); +int8_t _z_undecl_queryable_decode(_z_undecl_queryable_t *decl, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_decl_token_encode(_z_wbuf_t *wbf, const _z_decl_token_t *decl); +int8_t _z_decl_token_decode(_z_decl_token_t *decl, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_undecl_token_encode(_z_wbuf_t *wbf, const _z_undecl_token_t *decl); +int8_t _z_undecl_token_decode(_z_undecl_token_t *decl, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_decl_interest_encode(_z_wbuf_t *wbf, const _z_decl_interest_t *decl); +int8_t _z_decl_interest_decode(_z_decl_interest_t *decl, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_final_interest_encode(_z_wbuf_t *wbf, const _z_final_interest_t *decl); +int8_t _z_final_interest_decode(_z_final_interest_t *decl, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_undecl_interest_encode(_z_wbuf_t *wbf, const _z_undecl_interest_t *decl); +int8_t _z_undecl_interest_decode(_z_undecl_interest_t *decl, _z_zbuf_t *zbf, uint8_t header); + int8_t _z_declaration_encode(_z_wbuf_t *wbf, const _z_declaration_t *decl); int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf); diff --git a/include/zenoh-pico/protocol/codec/message.h b/include/zenoh-pico/protocol/codec/message.h index aa292c7e6..89080475a 100644 --- a/include/zenoh-pico/protocol/codec/message.h +++ b/include/zenoh-pico/protocol/codec/message.h @@ -36,6 +36,9 @@ int8_t _z_err_decode(_z_msg_err_t *err, _z_zbuf_t *zbf, uint8_t header); int8_t _z_ack_encode(_z_wbuf_t *wbf, const _z_msg_ack_t *ack); int8_t _z_ack_decode(_z_msg_ack_t *ack, _z_zbuf_t *zbf, uint8_t header); +int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb); +int8_t _z_push_body_decode(_z_push_body_t *body, _z_zbuf_t *zbf, uint8_t header); + int8_t _z_put_encode(_z_wbuf_t *wbf, const _z_msg_put_t *put); int8_t _z_put_decode(_z_msg_put_t *put, _z_zbuf_t *zbf, uint8_t header); diff --git a/include/zenoh-pico/protocol/definitions/declarations.h b/include/zenoh-pico/protocol/definitions/declarations.h index c403ca06c..9f81ce9b5 100644 --- a/include/zenoh-pico/protocol/definitions/declarations.h +++ b/include/zenoh-pico/protocol/definitions/declarations.h @@ -24,9 +24,11 @@ typedef struct { uint16_t _id; _z_keyexpr_t _keyexpr; } _z_decl_kexpr_t; +_z_decl_kexpr_t _z_decl_kexpr_null(void); typedef struct { uint16_t _id; } _z_undecl_kexpr_t; +_z_undecl_kexpr_t _z_undecl_kexpr_null(void); typedef struct { _z_keyexpr_t _keyexpr; @@ -34,12 +36,14 @@ typedef struct { struct { _Bool _pull_mode; _Bool _reliable; - } _ext_subscriber_info; + } _ext_subinfo; } _z_decl_subscriber_t; +_z_decl_subscriber_t _z_decl_subscriber_null(void); typedef struct { uint32_t _id; _z_keyexpr_t _ext_keyexpr; } _z_undecl_subscriber_t; +_z_undecl_subscriber_t _z_undecl_subscriber_null(void); typedef struct { _z_keyexpr_t _keyexpr; @@ -49,19 +53,23 @@ typedef struct { uint32_t _distance; } _ext_queryable_info; } _z_decl_queryable_t; +_z_decl_queryable_t _z_decl_queryable_null(void); typedef struct { uint32_t _id; _z_keyexpr_t _ext_keyexpr; } _z_undecl_queryable_t; +_z_undecl_queryable_t _z_undecl_queryable_null(void); typedef struct { _z_keyexpr_t _keyexpr; uint32_t _id; } _z_decl_token_t; +_z_decl_token_t _z_decl_token_null(void); typedef struct { uint32_t _id; _z_keyexpr_t _ext_keyexpr; } _z_undecl_token_t; +_z_undecl_token_t _z_undecl_token_null(void); #define _Z_INTEREST_FLAG_KEYEXPRS (1) #define _Z_INTEREST_FLAG_SUBSCRIBERS (1 << 1) @@ -75,13 +83,16 @@ typedef struct { uint32_t _id; uint8_t interest_flags; } _z_decl_interest_t; +_z_decl_interest_t _z_decl_interest_null(void); typedef struct { uint32_t _id; } _z_final_interest_t; +_z_final_interest_t _z_final_interest_null(void); typedef struct { uint32_t _id; _z_keyexpr_t _ext_keyexpr; } _z_undecl_interest_t; +_z_undecl_interest_t _z_undecl_interest_null(void); typedef struct { enum { diff --git a/include/zenoh-pico/protocol/definitions/message.h b/include/zenoh-pico/protocol/definitions/message.h index 385ff7ba3..703ed6172 100644 --- a/include/zenoh-pico/protocol/definitions/message.h +++ b/include/zenoh-pico/protocol/definitions/message.h @@ -37,7 +37,7 @@ #define _Z_FLAG_Z_I 0x40 // 1 << 6 | DataInfo if I==1 then DataInfo is present #define _Z_FLAG_Z_K 0x80 // 1 << 7 | ResourceKey if K==1 then keyexpr is string #define _Z_FLAG_Z_N 0x40 // 1 << 6 | MaxSamples if N==1 then the MaxSamples is indicated -#define _Z_FLAG_Z_P 0x80 // 1 << 7 | Period if P==1 then a period is present +#define _Z_FLAG_Z_P 0x20 // 1 << 7 | Period if P==1 then a period is present #define _Z_FLAG_Z_Q 0x40 // 1 << 6 | QueryableKind if Q==1 then the queryable kind is present #define _Z_FLAG_Z_R \ 0x20 // 1 << 5 | Reliable if R==1 then it concerns the reliable channel, best-effort otherwise @@ -169,9 +169,9 @@ void _z_msg_put_clear(_z_msg_put_t *); #define _Z_FLAG_Z_Q_P 0x20 // 1 << 6 | Period if P==1 then a period is present typedef struct { _z_bytes_t _parameters; - _z_source_info_t _info; - _z_value_t _value; - z_consolidation_mode_t _consolidation; + _z_source_info_t _ext_info; + _z_value_t _ext_value; + z_consolidation_mode_t _ext_consolidation; } _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 de992c5e8..f6db78d76 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -94,17 +94,17 @@ typedef struct { typedef struct { _z_zint_t _rid; _z_keyexpr_t _key; - _z_timestamp_t ext_tstamp; - _z_n_qos_t ext_qos; - z_query_target_t ext_target; - uint32_t ext_budget; - uint32_t ext_timeout_ms; + _z_timestamp_t _ext_timestamp; + _z_n_qos_t _ext_qos; + z_query_target_t _ext_target; + uint32_t _ext_budget; + uint32_t _ext_timeout_ms; enum { _Z_REQUEST_QUERY, _Z_REQUEST_PUT, _Z_REQUEST_DEL, _Z_REQUEST_PULL } _tag; union { _z_msg_query_t _query; - _z_msg_put_t put; - _z_msg_del_t del; - _z_msg_pull_t pull; + _z_msg_put_t _put; + _z_msg_del_t _del; + _z_msg_pull_t _pull; } _body; } _z_n_msg_request_t; typedef struct { diff --git a/include/zenoh-pico/protocol/ext.h b/include/zenoh-pico/protocol/ext.h index 3a81075d5..118c1ed14 100644 --- a/include/zenoh-pico/protocol/ext.h +++ b/include/zenoh-pico/protocol/ext.h @@ -16,6 +16,7 @@ #define ZENOH_PICO_PROTOCOL_EXTENSION_H #include +#include #include "zenoh-pico/protocol/core.h" @@ -61,7 +62,7 @@ void _z_msg_ext_clear_unit(_z_msg_ext_unit_t *ext); /*------------------ ZID Extension ------------------*/ typedef struct { - _z_zint_t _val; + uint64_t _val; } _z_msg_ext_zint_t; void _z_msg_ext_clear_zint(_z_msg_ext_zint_t *ext); diff --git a/include/zenoh-pico/protocol/keyexpr.h b/include/zenoh-pico/protocol/keyexpr.h index b12694d57..99622c11b 100644 --- a/include/zenoh-pico/protocol/keyexpr.h +++ b/include/zenoh-pico/protocol/keyexpr.h @@ -29,6 +29,9 @@ void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src); _z_keyexpr_t _z_keyexpr_duplicate(_z_keyexpr_t src); _z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src); _z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src); +static inline _z_keyexpr_t _z_keyexpr_null(void) { + return (_z_keyexpr_t){._id = 0, ._owns_suffix = false, ._suffix = NULL, ._uses_remote_mapping = false}; +} _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp); void _z_timestamp_clear(_z_timestamp_t *tstamp); void _z_keyexpr_clear(_z_keyexpr_t *rk); diff --git a/include/zenoh-pico/protocol/msgcodec.h b/include/zenoh-pico/protocol/msgcodec.h index 54c479641..d87c166a9 100644 --- a/include/zenoh-pico/protocol/msgcodec.h +++ b/include/zenoh-pico/protocol/msgcodec.h @@ -37,9 +37,6 @@ int8_t _z_payload_encode(_z_wbuf_t *buf, const _z_bytes_t *pld); int8_t _z_payload_decode(_z_bytes_t *pld, _z_zbuf_t *buf); int8_t _z_payload_decode_na(_z_bytes_t *pld, _z_zbuf_t *buf); - -int8_t _z_subinfo_encode(_z_wbuf_t *buf, const _z_subinfo_t *si); -int8_t _z_subinfo_decode(_z_subinfo_t *si, _z_zbuf_t *buf, uint8_t header); int8_t _z_subinfo_decode_na(_z_subinfo_t *si, _z_zbuf_t *buf, uint8_t header); int8_t _z_data_info_encode(_z_wbuf_t *buf, const _z_data_info_t *di); diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 51815dcc8..f84b722b2 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -202,6 +202,14 @@ int8_t _z_uint64_decode(uint64_t *u64, _z_zbuf_t *zbf) { } /*------------------ z_zint ------------------*/ +uint8_t _z_zint_len(_z_zint_t v) { + uint8_t len = 1; + while (v > 0x7f) { + v >>= 7; + len++; + } + return len; +} int8_t _z_zint_encode(_z_wbuf_t *wbf, _z_zint_t v) { _z_zint_t lv = v; @@ -214,6 +222,18 @@ int8_t _z_zint_encode(_z_wbuf_t *wbf, _z_zint_t v) { uint8_t c = lv & 0xff; return _z_wbuf_write(wbf, c); } +int8_t _z_zint64_encode(_z_wbuf_t *wbf, uint64_t v) { + uint64_t lv = v; + + while (lv > 0x7f) { + uint8_t c = (lv & 0x7f) | 0x80; + _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, c)) + lv = lv >> (uint64_t)7; + } + + uint8_t c = lv & 0xff; + return _z_wbuf_write(wbf, c); +} int8_t _z_zint16_decode(uint16_t *zint, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; _z_zint_t buf; @@ -253,6 +273,23 @@ int8_t _z_zint_decode(_z_zint_t *zint, _z_zbuf_t *zbf) { return ret; } +int8_t _z_zint64_decode(uint64_t *zint, _z_zbuf_t *zbf) { + int8_t ret = _Z_RES_OK; + *zint = 0; + + uint8_t i = 0; + uint8_t u8 = 0; + do { + if (_z_uint8_decode(&u8, zbf) == _Z_RES_OK) { + *zint = *zint | (((uint64_t)u8 & 0x7f) << i); + i = i + (uint8_t)7; + } else { + ret |= _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + } while (u8 > (uint8_t)0x7f); + + return ret; +} /*------------------ uint8_array ------------------*/ int8_t _z_bytes_val_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { @@ -275,6 +312,7 @@ int8_t _z_bytes_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { return ret; } +size_t _z_bytes_encode_len(const _z_bytes_t *bs) { return _z_zint_len(bs->len) + bs->len; } int8_t _z_bytes_val_decode_na(_z_bytes_t *bs, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index e7097384d..cfccd81a5 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -28,18 +29,20 @@ #include "zenoh-pico/protocol/definitions/message.h" #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/iobuf.h" +#include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/session/session.h" #include "zenoh-pico/system/platform.h" int8_t _z_decl_ext_keyexpr_encode(_z_wbuf_t *wbf, _z_keyexpr_t ke, _Bool has_next_ext) { uint8_t header = _Z_MSG_EXT_ENC_ZBUF | 0x0f | (has_next_ext ? _Z_FLAG_Z_Z : 0); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); - _Bool has_kesuffix = _z_keyexpr_has_suffix(ke); - header = (ke._uses_remote_mapping ? 2 : 0) | (has_kesuffix ? 1 : 0); + uint32_t kelen = _z_keyexpr_has_suffix(ke) ? strlen(ke._suffix) : 0; + header = (ke._uses_remote_mapping ? 2 : 0) | (kelen != 0 ? 1 : 0); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, 1 + kelen + _z_zint_len(ke._id))); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, ke._id)); - if (has_kesuffix) { - _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, (const uint8_t *)ke._suffix, 0, strlen(ke._suffix))) + if (kelen) { + _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, (const uint8_t *)ke._suffix, 0, kelen)) } return _Z_RES_OK; } @@ -65,7 +68,7 @@ int8_t _z_decl_commons_encode(_z_wbuf_t *wbf, uint8_t header, _Bool has_extensio if (has_kesuffix) { header |= _Z_DECL_SUBSCRIBER_FLAG_N; } - if (keyexpr._uses_remote_mapping) { + if (!keyexpr._uses_remote_mapping) { header |= _Z_DECL_SUBSCRIBER_FLAG_M; } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); @@ -74,12 +77,12 @@ int8_t _z_decl_commons_encode(_z_wbuf_t *wbf, uint8_t header, _Bool has_extensio } int8_t _z_decl_subscriber_encode(_z_wbuf_t *wbf, const _z_decl_subscriber_t *decl) { uint8_t header = _Z_DECL_SUBSCRIBER_MID; - _Bool has_submode_ext = decl->_ext_subscriber_info._pull_mode || decl->_ext_subscriber_info._reliable; + _Bool has_submode_ext = decl->_ext_subinfo._pull_mode || decl->_ext_subinfo._reliable; _Z_RETURN_IF_ERR(_z_decl_commons_encode(wbf, header, has_submode_ext, decl->_id, decl->_keyexpr)); if (has_submode_ext) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | 0x01)); - _Z_RETURN_IF_ERR(_z_uint8_encode( - wbf, (decl->_ext_subscriber_info._pull_mode ? 2 : 0) | (decl->_ext_subscriber_info._reliable ? 1 : 0))); + _Z_RETURN_IF_ERR( + _z_uint8_encode(wbf, (decl->_ext_subinfo._pull_mode ? 2 : 0) | (decl->_ext_subinfo._reliable ? 1 : 0))); } return _Z_RES_OK; @@ -109,8 +112,8 @@ int8_t _z_decl_queryable_encode(_z_wbuf_t *wbf, const _z_decl_queryable_t *decl) _Z_RETURN_IF_ERR(_z_decl_commons_encode(wbf, header, has_info_ext, decl->_id, decl->_keyexpr)); if (has_info_ext) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | 0x01)); - _Z_RETURN_IF_ERR( - _z_zint_encode(wbf, (decl->_ext_queryable_info._distance << 8) | decl->_ext_queryable_info._complete)); + _Z_RETURN_IF_ERR(_z_zint64_encode( + wbf, ((uint64_t)decl->_ext_queryable_info._distance << 8) | decl->_ext_queryable_info._complete)); } return _Z_RES_OK; } @@ -183,6 +186,7 @@ int8_t _z_declaration_encode(_z_wbuf_t *wbf, const _z_declaration_t *decl) { return ret; } int8_t _z_decl_kexpr_decode(_z_decl_kexpr_t *decl, _z_zbuf_t *zbf, uint8_t header) { + *decl = _z_decl_kexpr_null(); _Z_RETURN_IF_ERR(_z_zint16_decode(&decl->_id, zbf)); _Z_RETURN_IF_ERR(_z_keyexpr_decode(&decl->_keyexpr, zbf, _Z_HAS_FLAG(header, _Z_DECL_KEXPR_FLAG_N))); @@ -192,6 +196,7 @@ int8_t _z_decl_kexpr_decode(_z_decl_kexpr_t *decl, _z_zbuf_t *zbf, uint8_t heade return _Z_RES_OK; } int8_t _z_undecl_kexpr_decode(_z_undecl_kexpr_t *decl, _z_zbuf_t *zbf, uint8_t header) { + *decl = _z_undecl_kexpr_null(); _Z_RETURN_IF_ERR(_z_zint16_decode(&decl->_id, zbf)); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { @@ -238,6 +243,7 @@ int8_t _z_undecl_trivial_decode(_z_zbuf_t *zbf, _z_keyexpr_t *_ext_keyexpr, uint int8_t _z_decl_commons_decode(_z_zbuf_t *zbf, uint8_t header, _Bool *has_extensions, uint32_t *id, _z_keyexpr_t *ke) { *has_extensions = _Z_HAS_FLAG(header, _Z_FLAG_Z_Z); ke->_uses_remote_mapping = _Z_HAS_FLAG(header, _Z_DECL_SUBSCRIBER_FLAG_M); + _Z_RETURN_IF_ERR(_z_zint32_decode(id, zbf)); _Z_RETURN_IF_ERR(_z_zint16_decode(&ke->_id, zbf)); if (_Z_HAS_FLAG(header, _Z_DECL_SUBSCRIBER_FLAG_N)) { _z_zint_t len; @@ -253,7 +259,7 @@ int8_t _z_decl_commons_decode(_z_zbuf_t *zbf, uint8_t header, _Bool *has_extensi _z_zbuf_read_bytes(zbf, (uint8_t *)ke->_suffix, 0, len); ke->_suffix[len] = 0; } else { - ke->_suffix = ""; + ke->_suffix = NULL; ke->_owns_suffix = false; } return _Z_RES_OK; @@ -262,8 +268,8 @@ int8_t _z_decl_subscriber_decode_extensions(_z_msg_ext_t *extension, void *ctx) _z_decl_subscriber_t *decl = (_z_decl_subscriber_t *)ctx; switch (extension->_header) { case _Z_MSG_EXT_ENC_ZINT | 0x01: { - decl->_ext_subscriber_info._pull_mode = _Z_HAS_FLAG(extension->_body._zint._val, 2); - decl->_ext_subscriber_info._reliable = _Z_HAS_FLAG(extension->_body._zint._val, 1); + decl->_ext_subinfo._pull_mode = _Z_HAS_FLAG(extension->_body._zint._val, 2); + decl->_ext_subinfo._reliable = _Z_HAS_FLAG(extension->_body._zint._val, 1); } break; default: if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { @@ -275,6 +281,7 @@ int8_t _z_decl_subscriber_decode_extensions(_z_msg_ext_t *extension, void *ctx) int8_t _z_decl_subscriber_decode(_z_decl_subscriber_t *decl, _z_zbuf_t *zbf, uint8_t header) { _Bool has_ext; + *decl = _z_decl_subscriber_null(); _Z_RETURN_IF_ERR(_z_decl_commons_decode(zbf, header, &has_ext, &decl->_id, &decl->_keyexpr)); if (has_ext) { _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_decl_subscriber_decode_extensions, decl)); @@ -282,14 +289,16 @@ int8_t _z_decl_subscriber_decode(_z_decl_subscriber_t *decl, _z_zbuf_t *zbf, uin return _Z_RES_OK; } int8_t _z_undecl_subscriber_decode(_z_undecl_subscriber_t *decl, _z_zbuf_t *zbf, uint8_t header) { + *decl = _z_undecl_subscriber_null(); return _z_undecl_trivial_decode(zbf, &decl->_ext_keyexpr, &decl->_id, header); } int8_t _z_decl_queryable_decode_extensions(_z_msg_ext_t *extension, void *ctx) { _z_decl_queryable_t *decl = (_z_decl_queryable_t *)ctx; switch (extension->_header) { case _Z_MSG_EXT_ENC_ZINT | 0x01: { - decl->_ext_queryable_info._complete = extension->_body._zint._val & 0xff; - decl->_ext_queryable_info._distance = extension->_body._zint._val >> 8; + uint64_t val = extension->_body._zint._val; + decl->_ext_queryable_info._complete = val & 0xff; + decl->_ext_queryable_info._distance = (uint32_t)(val >> 8); } break; default: if (_Z_HAS_FLAG(extension->_header, _Z_MSG_EXT_FLAG_M)) { @@ -300,6 +309,7 @@ int8_t _z_decl_queryable_decode_extensions(_z_msg_ext_t *extension, void *ctx) { } int8_t _z_decl_queryable_decode(_z_decl_queryable_t *decl, _z_zbuf_t *zbf, uint8_t header) { _Bool has_ext; + *decl = _z_decl_queryable_null(); _Z_RETURN_IF_ERR(_z_decl_commons_decode(zbf, header, &has_ext, &decl->_id, &decl->_keyexpr)); if (has_ext) { _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_decl_queryable_decode_extensions, decl)); @@ -307,10 +317,12 @@ int8_t _z_decl_queryable_decode(_z_decl_queryable_t *decl, _z_zbuf_t *zbf, uint8 return _Z_RES_OK; } int8_t _z_undecl_queryable_decode(_z_undecl_queryable_t *decl, _z_zbuf_t *zbf, uint8_t header) { + *decl = _z_undecl_queryable_null(); return _z_undecl_trivial_decode(zbf, &decl->_ext_keyexpr, &decl->_id, header); } int8_t _z_decl_token_decode(_z_decl_token_t *decl, _z_zbuf_t *zbf, uint8_t header) { _Bool has_ext; + *decl = _z_decl_token_null(); _Z_RETURN_IF_ERR(_z_decl_commons_decode(zbf, header, &has_ext, &decl->_id, &decl->_keyexpr)); if (has_ext) { _Z_RETURN_IF_ERR(_z_msg_ext_skip_non_mandatories(zbf, 0x12)); @@ -322,6 +334,7 @@ int8_t _z_undecl_token_decode(_z_undecl_token_t *decl, _z_zbuf_t *zbf, uint8_t h } int8_t _z_decl_interest_decode(_z_decl_interest_t *decl, _z_zbuf_t *zbf, uint8_t header) { _Bool has_ext; + *decl = _z_decl_interest_null(); _Z_RETURN_IF_ERR(_z_decl_commons_decode(zbf, header, &has_ext, &decl->_id, &decl->_keyexpr)); _Z_RETURN_IF_ERR(_z_uint8_decode(&decl->interest_flags, zbf)); if (has_ext) { @@ -330,6 +343,7 @@ int8_t _z_decl_interest_decode(_z_decl_interest_t *decl, _z_zbuf_t *zbf, uint8_t return _Z_RES_OK; } int8_t _z_final_interest_decode(_z_final_interest_t *decl, _z_zbuf_t *zbf, uint8_t header) { + *decl = _z_final_interest_null(); _Z_RETURN_IF_ERR(_z_zint32_decode(&decl->_id, zbf)); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_skip_non_mandatories(zbf, 0x10)); @@ -337,6 +351,7 @@ int8_t _z_final_interest_decode(_z_final_interest_t *decl, _z_zbuf_t *zbf, uint8 return _Z_RES_OK; } int8_t _z_undecl_interest_decode(_z_undecl_interest_t *decl, _z_zbuf_t *zbf, uint8_t header) { + *decl = _z_undecl_interest_null(); return _z_undecl_trivial_decode(zbf, &decl->_ext_keyexpr, &decl->_id, header); } int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf) { diff --git a/src/protocol/codec/ext.c b/src/protocol/codec/ext.c index 8e6876336..ff096e25e 100644 --- a/src/protocol/codec/ext.c +++ b/src/protocol/codec/ext.c @@ -43,13 +43,13 @@ int8_t _z_msg_ext_decode_unit_na(_z_msg_ext_unit_t *ext, _z_zbuf_t *zbf) { retur int8_t _z_msg_ext_encode_zint(_z_wbuf_t *wbf, const _z_msg_ext_zint_t *ext) { int8_t ret = _Z_RES_OK; - _Z_RETURN_IF_ERR(_z_zint_encode(wbf, ext->_val)) + _Z_RETURN_IF_ERR(_z_zint64_encode(wbf, ext->_val)) return ret; } int8_t _z_msg_ext_decode_zint(_z_msg_ext_zint_t *ext, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; - ret |= _z_zint_decode(&ext->_val, zbf); + ret |= _z_zint64_decode(&ext->_val, zbf); return ret; } @@ -130,7 +130,7 @@ int8_t _z_msg_ext_decode(_z_msg_ext_t *ext, _z_zbuf_t *zbf, _Bool *has_next) { ret |= _z_msg_ext_unknown_body_decode(&ext->_body, _Z_EXT_ENC(ext->_header), zbf); } *has_next = (ext->_header & _Z_MSG_EXT_FLAG_Z) != 0; - ext->_header |= _Z_EXT_FULL_ID_MASK; + ext->_header &= _Z_EXT_FULL_ID_MASK; return ret; } @@ -172,11 +172,12 @@ int8_t _z_msg_ext_unknown_error(_z_msg_ext_t *extension, uint8_t trace_id) { #if (ZENOH_DEBUG >= 1) switch (_Z_EXT_ENC(extension->_header)) { case _Z_MSG_EXT_ENC_UNIT: { - _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), UNIT", ext_id, trace_id); + _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), UNIT\n", ext_id, + trace_id); break; } case _Z_MSG_EXT_ENC_ZINT: { - _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZINT(%02x)", ext_id, + _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZINT(%02x)\n", ext_id, trace_id, extension->_body._zint); break; } @@ -186,14 +187,14 @@ int8_t _z_msg_ext_unknown_error(_z_msg_ext_t *extension, uint8_t trace_id) { for (size_t i = 0; i < buf.len; ++i) { snprintf(hex + 2 * i, 3, "%02x", buf.start[i]); } - _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZBUF(%.*s)", ext_id, + _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZBUF(%.*s)\n", ext_id, trace_id, buf.len * 2, hex); z_free(hex); break; } default: { - _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), UNKOWN_ENCODING", ext_id, - trace_id); + _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), UNKOWN_ENCODING\n", + ext_id, trace_id); } } #endif diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 643261f04..51b2342d2 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -27,6 +27,8 @@ #include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/protocol/codec/ext.h" #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/definitions/core.h" +#include "zenoh-pico/protocol/definitions/transport.h" #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/keyexpr.h" @@ -57,9 +59,10 @@ int8_t _z_id_encode_as_zbytes(_z_wbuf_t *wbf, const _z_id_t *id) { int8_t ret = _Z_RES_OK; uint8_t len = _z_id_len(*id); - if (id->id[len] != 0) { - ret |= _z_wbuf_write(wbf, len); - ret |= _z_wbuf_write_bytes(wbf, id->id, 0, len); + if (len != 0) { + printf("ZIDLEN: %d\n", len); + _z_bytes_t buf = _z_bytes_wrap(id->id, len); + ret = _z_bytes_encode(wbf, &buf); } else { _Z_DEBUG("Attempted to encode invalid ID 0"); ret = _Z_ERR_MESSAGE_ZENOH_UNKNOWN; @@ -89,6 +92,11 @@ 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) { + int8_t ret = _Z_RES_OK; + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, _z_zint_len(ts->time) + 1 + _z_id_len(ts->id))); + return _z_timestamp_encode(wbf, ts); +} int8_t _z_timestamp_decode(_z_timestamp_t *ts, _z_zbuf_t *zbf) { _Z_DEBUG("Decoding _TIMESTAMP\n"); @@ -221,6 +229,18 @@ int8_t _z_source_info_encode(_z_wbuf_t *wbf, const _z_source_info_t *info) { ret |= _z_zint_encode(wbf, info->_source_sn); return ret; } +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_zint_encode(wbf, len)); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, zidlen << 4)); + _z_bytes_t zid = _z_bytes_wrap(info->_id.id, zidlen); + _Z_RETURN_IF_ERR(_z_bytes_val_encode(wbf, &zid)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, info->_entity_id)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, info->_source_sn)); + return ret; +} /*------------------ Push Body Field ------------------*/ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { @@ -260,7 +280,7 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { if ((ret == _Z_RES_OK) && has_source_info) { ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x01); - ret |= _z_source_info_encode(wbf, &pshb->_body._put._commons._source_info); + ret |= _z_source_info_encode_ext(wbf, &pshb->_body._put._commons._source_info); } if ((ret == _Z_RES_OK) && pshb->_is_put) { @@ -292,27 +312,30 @@ int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf, uint8_t header) switch (_Z_MID(header)) { case _Z_M_PUT_ID: { pshb->_is_put = true; + pshb->_body._put = (_z_msg_put_t){0}; if (_Z_HAS_FLAG(header, _Z_FLAG_Z_P_T)) { - ret = _z_timestamp_decode(&pshb->_body._put._commons._timestamp, zbf); + _Z_RETURN_IF_ERR(_z_timestamp_decode(&pshb->_body._put._commons._timestamp, zbf)); } if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_P_E)) { - ret = _z_encoding_prefix_decode(&pshb->_body._put._encoding.prefix, zbf); + _Z_RETURN_IF_ERR(_z_encoding_prefix_decode(&pshb->_body._put._encoding.prefix, zbf)); + _Z_RETURN_IF_ERR(_z_bytes_decode(&pshb->_body._put._encoding.suffix, zbf)); } if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { - ret = _z_msg_ext_decode_iter(zbf, _z_push_body_decode_extensions, pshb); + _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_push_body_decode_extensions, pshb)); } if (ret == _Z_RES_OK) { - ret = _z_bytes_decode(&pshb->_body._put._payload, zbf); + _Z_RETURN_IF_ERR(_z_bytes_decode(&pshb->_body._put._payload, zbf)); } break; } case _Z_M_DEL_ID: { pshb->_is_put = false; + pshb->_body._del = (_z_msg_del_t){0}; if (_Z_HAS_FLAG(header, _Z_FLAG_Z_D_T)) { - ret = _z_timestamp_decode(&pshb->_body._put._commons._timestamp, zbf); + _Z_RETURN_IF_ERR(_z_timestamp_decode(&pshb->_body._put._commons._timestamp, zbf)); } if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { - ret = _z_msg_ext_decode_iter(zbf, _z_push_body_decode_extensions, pshb); + _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_push_body_decode_extensions, pshb)); } break; } @@ -374,9 +397,13 @@ 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_encoding_prefix_encode(wbf, msg->_value.encoding.prefix)); - _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_value.encoding.suffix)); - _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_value.payload)); + _Z_RETURN_IF_ERR(_z_zint_encode( + wbf, 1 + _z_zint_len(msg->_ext_value.encoding.prefix) + _z_zint_len(msg->_ext_value.encoding.suffix.len) + + msg->_ext_value.encoding.suffix.len + _z_zint_len(msg->_ext_value.payload.len) + + msg->_ext_value.payload.len)); + _Z_RETURN_IF_ERR(_z_encoding_prefix_encode(wbf, msg->_ext_value.encoding.prefix)); + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_ext_value.encoding.suffix)); + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_ext_value.payload)); } if (required_exts.consolidation) { uint8_t extheader = _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M | 0x02; @@ -384,11 +411,11 @@ 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_zint_encode(wbf, msg->_consolidation)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_ext_consolidation)); } if (required_exts.info) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01; - _Z_RETURN_IF_ERR(_z_source_info_encode(wbf, &msg->_info)); + _Z_RETURN_IF_ERR(_z_source_info_encode_ext(wbf, &msg->_ext_info)); } return ret; @@ -400,18 +427,18 @@ int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) { switch (_Z_EXT_FULL_ID(extension->_header)) { case _Z_MSG_EXT_ENC_ZBUF | 0x01: { // Source Info _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); - ret = _z_source_info_decode(&msg->_info, &zbf); + ret = _z_source_info_decode(&msg->_ext_info, &zbf); break; } case _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M | 0x02: { // Consolidation - msg->_consolidation = extension->_body._zint._val; + msg->_ext_consolidation = extension->_body._zint._val; break; } case _Z_MSG_EXT_ENC_ZBUF | 0x03: { // Payload _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); - ret = _z_encoding_prefix_decode(&msg->_value.encoding.prefix, &zbf); - ret |= _z_bytes_decode(&msg->_value.encoding.suffix, &zbf); - ret |= _z_bytes_decode(&msg->_value.payload, &zbf); + ret = _z_encoding_prefix_decode(&msg->_ext_value.encoding.prefix, &zbf); + ret |= _z_bytes_decode(&msg->_ext_value.encoding.suffix, &zbf); + ret |= _z_bytes_decode(&msg->_ext_value.payload, &zbf); break; } default: @@ -424,6 +451,7 @@ int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) { int8_t _z_query_decode(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_Z_QUERY\n"); + *msg = (_z_msg_query_t){0}; int8_t ret = _Z_RES_OK; if (_Z_HAS_FLAG(header, _Z_FLAG_Z_P)) { ret = _z_bytes_decode(&msg->_parameters, zbf); @@ -454,30 +482,27 @@ int8_t _z_reply_encode(_z_wbuf_t *wbf, const _z_msg_reply_t *reply) { int8_t ret = _Z_RES_OK; if (_z_timestamp_check(&reply->_timestamp)) { assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_R_T)); - ret = _z_timestamp_encode(wbf, &reply->_timestamp); + _Z_RETURN_IF_ERR(_z_timestamp_encode(wbf, &reply->_timestamp)); } - if ((ret == _Z_RES_OK) && - ((reply->_value.encoding.prefix != 0) || !_z_bytes_is_empty(&reply->_value.encoding.suffix))) { + if (((reply->_value.encoding.prefix != 0) || !_z_bytes_is_empty(&reply->_value.encoding.suffix))) { assert(_Z_HAS_FLAG(header, _Z_FLAG_Z_R_E)); - ret = _z_encoding_prefix_encode(wbf, reply->_value.encoding.prefix); - ret |= _z_bytes_encode(wbf, &reply->_value.encoding.suffix); + _Z_RETURN_IF_ERR(_z_encoding_prefix_encode(wbf, reply->_value.encoding.prefix)); + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &reply->_value.encoding.suffix)); } _Bool has_consolidation_ext = reply->_ext_consolidation != Z_CONSOLIDATION_MODE_AUTO; - if ((ret == _Z_RES_OK) && _z_id_check(reply->_ext_source_info._id)) { + if (_z_id_check(reply->_ext_source_info._id)) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01; if (has_consolidation_ext) { extheader |= _Z_MSG_EXT_FLAG_Z; } - ret = _z_uint8_encode(wbf, extheader); - ret |= _z_source_info_encode(wbf, &reply->_ext_source_info); - } - if ((ret == _Z_RES_OK) && has_consolidation_ext) { - ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M | 0x02); - ret |= _z_zint_encode(wbf, reply->_ext_consolidation); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); + _Z_RETURN_IF_ERR(_z_source_info_encode_ext(wbf, &reply->_ext_source_info)); } - if (ret == _Z_RES_OK) { - ret = _z_bytes_encode(wbf, &reply->_value.payload); + if (has_consolidation_ext) { + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M | 0x02)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, reply->_ext_consolidation)); } + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &reply->_value.payload)); return ret; } int8_t _z_reply_decode_extension(_z_msg_ext_t *extension, void *ctx) { @@ -502,19 +527,19 @@ int8_t _z_reply_decode_extension(_z_msg_ext_t *extension, void *ctx) { } int8_t _z_reply_decode(_z_msg_reply_t *reply, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; + *reply = (_z_msg_reply_t){0}; + reply->_ext_consolidation = Z_CONSOLIDATION_MODE_AUTO; if (_Z_HAS_FLAG(header, _Z_FLAG_Z_R_T)) { - ret = _z_timestamp_decode(&reply->_timestamp, zbf); - } - if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_R_E)) { - ret = _z_encoding_prefix_decode(&reply->_value.encoding.prefix, zbf); - ret |= _z_bytes_decode(&reply->_value.encoding.suffix, zbf); + _Z_RETURN_IF_ERR(_z_timestamp_decode(&reply->_timestamp, zbf)); } - if (ret == _Z_RES_OK) { - ret = _z_msg_ext_decode_iter(zbf, _z_reply_decode_extension, reply); + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_R_E)) { + _Z_RETURN_IF_ERR(_z_encoding_prefix_decode(&reply->_value.encoding.prefix, zbf)); + _Z_RETURN_IF_ERR(_z_bytes_decode(&reply->_value.encoding.suffix, zbf)); } - if (ret == _Z_RES_OK) { - ret = _z_bytes_decode(&reply->_value.payload, zbf); + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_reply_decode_extension, reply)); } + _Z_RETURN_IF_ERR(_z_bytes_decode(&reply->_value.payload, zbf)); return ret; } @@ -526,29 +551,36 @@ int8_t _z_err_encode(_z_wbuf_t *wbf, const _z_msg_err_t *err) { if (has_timestamp) { header |= _Z_FLAG_Z_E_T; } - _Bool has_payload_ext = err->_ext_value.payload.start != NULL; + if (err->_is_infrastructure) { + header |= _Z_FLAG_Z_E_I; + } + _Bool has_payload_ext = err->_ext_value.payload.start != NULL || err->_ext_value.encoding.prefix != 0 || + !_z_bytes_is_empty(&err->_ext_value.encoding.suffix); _Bool has_sinfo_ext = _z_id_check(err->_ext_source_info._id); if (has_sinfo_ext || has_payload_ext) { header |= _Z_FLAG_Z_Z; } - ret |= _z_uint8_encode(wbf, header); - ret |= _z_zint_encode(wbf, err->_code); - if ((ret == _Z_RES_OK) && has_timestamp) { - ret = _z_timestamp_encode(wbf, &err->_timestamp); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, err->_code)); + if (has_timestamp) { + _Z_RETURN_IF_ERR(_z_timestamp_encode(wbf, &err->_timestamp)); } - if ((ret == _Z_RES_OK) && has_sinfo_ext) { + if (has_sinfo_ext) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01; if (has_payload_ext) { extheader |= _Z_MSG_EXT_FLAG_Z; } - ret = _z_uint8_encode(wbf, extheader); - ret |= _z_source_info_encode(wbf, &err->_ext_source_info); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); + _Z_RETURN_IF_ERR(_z_source_info_encode_ext(wbf, &err->_ext_source_info)); } - if ((ret == _Z_RES_OK) && has_payload_ext) { - ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x02); - ret |= _z_encoding_prefix_encode(wbf, err->_ext_value.encoding.prefix); - ret |= _z_bytes_encode(wbf, &err->_ext_value.encoding.suffix); - ret |= _z_bytes_encode(wbf, &err->_ext_value.payload); + if (has_payload_ext) { + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x02)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, _z_zint_len(err->_ext_value.encoding.prefix) + + _z_bytes_encode_len(&err->_ext_value.encoding.suffix) + + _z_bytes_encode_len(&err->_ext_value.payload))); + _Z_RETURN_IF_ERR(_z_encoding_prefix_encode(wbf, err->_ext_value.encoding.prefix)); + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &err->_ext_value.encoding.suffix)); + _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &err->_ext_value.payload)); } return ret; } @@ -576,6 +608,7 @@ int8_t _z_err_decode_extension(_z_msg_ext_t *extension, void *ctx) { return ret; } int8_t _z_err_decode(_z_msg_err_t *err, _z_zbuf_t *zbf, uint8_t header) { + *err = (_z_msg_err_t){0}; int8_t ret = _Z_RES_OK; _z_zint_t code; ret = _z_zint_decode(&code, zbf); @@ -612,7 +645,7 @@ int8_t _z_ack_encode(_z_wbuf_t *wbf, const _z_msg_ack_t *ack) { } if ((ret == _Z_RES_OK) && has_sinfo_ext) { ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x01); - ret |= _z_source_info_encode(wbf, &ack->_ext_source_info); + ret |= _z_source_info_encode_ext(wbf, &ack->_ext_source_info); } return ret; } @@ -634,6 +667,7 @@ int8_t _z_ack_decode_extension(_z_msg_ext_t *extension, void *ctx) { } int8_t _z_ack_decode(_z_msg_ack_t *ack, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; + *ack = (_z_msg_ack_t){0}; if (_Z_HAS_FLAG(header, _Z_FLAG_Z_A_T)) { ret = _z_timestamp_decode(&ack->_timestamp, zbf); } @@ -653,7 +687,7 @@ int8_t _z_pull_encode(_z_wbuf_t *wbf, const _z_msg_pull_t *pull) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); if (has_info_ext) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x01)); - _Z_RETURN_IF_ERR(_z_source_info_encode(wbf, &pull->_ext_source_info)); + _Z_RETURN_IF_ERR(_z_source_info_encode_ext(wbf, &pull->_ext_source_info)); } return ret; } @@ -674,6 +708,7 @@ int8_t _z_pull_decode_extension(_z_msg_ext_t *extension, void *ctx) { return ret; } int8_t _z_pull_decode(_z_msg_pull_t *pull, _z_zbuf_t *zbf, uint8_t header) { + *pull = (_z_msg_pull_t){0}; int8_t ret = _Z_RES_OK; if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { ret = _z_msg_ext_decode_iter(zbf, _z_pull_decode_extension, pull); @@ -706,10 +741,11 @@ int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_scout_t *m return ret; } -int8_t _z_scout_decode_na(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_scout_decode(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; (void)(header); _Z_DEBUG("Decoding _Z_MID_SCOUT\n"); + *msg = (_z_s_msg_scout_t){0}; ret |= _z_uint8_decode(&msg->_version, zbf); @@ -725,10 +761,6 @@ int8_t _z_scout_decode_na(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) return ret; } -int8_t _z_scout_decode(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header) { - return _z_scout_decode_na(msg, zbf, header); -} - /*------------------ Hello Message ------------------*/ int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *msg) { int8_t ret = _Z_RES_OK; @@ -752,6 +784,7 @@ int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *m int8_t _z_hello_decode_na(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_HELLO\n"); int8_t ret = _Z_RES_OK; + *msg = (_z_s_msg_hello_t){0}; ret |= _z_uint8_decode(&msg->_version, zbf); @@ -808,6 +841,7 @@ int8_t _z_scouting_message_encode(_z_wbuf_t *wbf, const _z_scouting_message_t *m } int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; + *msg = (_z_scouting_message_t){0}; _Bool is_last = false; diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index 6cf48ff62..06bc21bb6 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -57,7 +57,7 @@ int8_t _z_push_encode(_z_wbuf_t *wbf, const _z_n_msg_push_t *msg) { if (has_timestamp_ext) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x02)); - _Z_RETURN_IF_ERR(_z_timestamp_encode(wbf, &msg->_timestamp)); + _Z_RETURN_IF_ERR(_z_timestamp_encode_ext(wbf, &msg->_timestamp)); } _Z_RETURN_IF_ERR(_z_push_body_encode(wbf, &msg->_body)); @@ -119,31 +119,31 @@ int8_t _z_request_encode(_z_wbuf_t *wbf, const _z_n_msg_request_t *msg) { exts.n -= 1; uint8_t extheader = 0x01 | _Z_MSG_EXT_ENC_ZINT | (exts.n ? _Z_FLAG_Z_Z : 0); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); - _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->ext_qos._val)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_ext_qos._val)); } if (exts.ext_tstamp) { exts.n -= 1; uint8_t extheader = 0x02 | _Z_MSG_EXT_ENC_ZBUF | (exts.n ? _Z_FLAG_Z_Z : 0); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); - _Z_RETURN_IF_ERR(_z_timestamp_encode(wbf, &msg->ext_tstamp)); + _Z_RETURN_IF_ERR(_z_timestamp_encode_ext(wbf, &msg->_ext_timestamp)); } if (exts.ext_target) { exts.n -= 1; uint8_t extheader = 0x04 | _Z_MSG_EXT_ENC_ZINT | (exts.n ? _Z_FLAG_Z_Z : 0) | _Z_MSG_EXT_FLAG_M; _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); - _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->ext_target)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_ext_target)); } if (exts.ext_budget) { exts.n -= 1; uint8_t extheader = 0x05 | _Z_MSG_EXT_ENC_ZINT | (exts.n ? _Z_FLAG_Z_Z : 0); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); - _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->ext_budget)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_ext_budget)); } if (exts.ext_timeout_ms) { exts.n -= 1; uint8_t extheader = 0x06 | _Z_MSG_EXT_ENC_ZINT | (exts.n ? _Z_FLAG_Z_Z : 0); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); - _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->ext_timeout_ms)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_ext_timeout_ms)); } switch (msg->_tag) { @@ -151,13 +151,13 @@ int8_t _z_request_encode(_z_wbuf_t *wbf, const _z_n_msg_request_t *msg) { _Z_RETURN_IF_ERR(_z_query_encode(wbf, &msg->_body._query)); } break; case _Z_REQUEST_PUT: { - _Z_RETURN_IF_ERR(_z_put_encode(wbf, &msg->_body.put)); + _Z_RETURN_IF_ERR(_z_put_encode(wbf, &msg->_body._put)); } break; case _Z_REQUEST_DEL: { - _Z_RETURN_IF_ERR(_z_del_encode(wbf, &msg->_body.del)); + _Z_RETURN_IF_ERR(_z_del_encode(wbf, &msg->_body._del)); } break; case _Z_REQUEST_PULL: { - _Z_RETURN_IF_ERR(_z_pull_encode(wbf, &msg->_body.pull)); + _Z_RETURN_IF_ERR(_z_pull_encode(wbf, &msg->_body._pull)); } break; } return ret; @@ -166,25 +166,25 @@ int8_t _z_request_decode_extensions(_z_msg_ext_t *extension, void *ctx) { _z_n_msg_request_t *msg = (_z_n_msg_request_t *)ctx; switch (_Z_EXT_FULL_ID(extension->_header)) { case 0x01 | _Z_MSG_EXT_ENC_ZINT: { // QOS ext - msg->ext_qos = (_z_n_qos_t){._val = extension->_body._zint._val}; + msg->_ext_qos = (_z_n_qos_t){._val = extension->_body._zint._val}; break; } case 0x02 | _Z_MSG_EXT_ENC_ZBUF: { // Timestamp ext _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); - _Z_RETURN_IF_ERR(_z_timestamp_decode(&msg->ext_tstamp, &zbf)); + _Z_RETURN_IF_ERR(_z_timestamp_decode(&msg->_ext_timestamp, &zbf)); break; } case 0x04 | _Z_MSG_EXT_ENC_ZINT: { - msg->ext_target = extension->_body._zint._val; - if (msg->ext_target > 2) { + msg->_ext_target = extension->_body._zint._val; + if (msg->_ext_target > 2) { return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } } break; case 0x05 | _Z_MSG_EXT_ENC_ZINT: { - msg->ext_budget = extension->_body._zint._val; + msg->_ext_budget = extension->_body._zint._val; } break; case 0x06 | _Z_MSG_EXT_ENC_ZINT: { - msg->ext_timeout_ms = extension->_body._zint._val; + msg->_ext_timeout_ms = extension->_body._zint._val; } break; default: if ((extension->_header & _Z_MSG_EXT_FLAG_M) != 0) { @@ -205,13 +205,13 @@ int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header _Z_RETURN_IF_ERR(_z_query_decode(&msg->_body._query, zbf, header)); } break; case _Z_MID_Z_PUT: { - _Z_RETURN_IF_ERR(_z_put_decode(&msg->_body.put, zbf, header)); + _Z_RETURN_IF_ERR(_z_put_decode(&msg->_body._put, zbf, header)); } break; case _Z_MID_Z_DEL: { - _Z_RETURN_IF_ERR(_z_del_decode(&msg->_body.del, zbf, header)); + _Z_RETURN_IF_ERR(_z_del_decode(&msg->_body._del, zbf, header)); } break; case _Z_MID_Z_PULL: { - _Z_RETURN_IF_ERR(_z_pull_decode(&msg->_body.pull, zbf, header)); + _Z_RETURN_IF_ERR(_z_pull_decode(&msg->_body._pull, zbf, header)); } break; default: return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; @@ -257,7 +257,7 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { n_ext -= 1; uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x02 | (n_ext != 0 ? _Z_FLAG_Z_Z : 0); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); - _Z_RETURN_IF_ERR(_z_timestamp_encode(wbf, &msg->_ext_timestamp)); + _Z_RETURN_IF_ERR(_z_timestamp_encode_ext(wbf, &msg->_ext_timestamp)); } if (has_responder_ext) { n_ext -= 1; @@ -266,6 +266,7 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { uint8_t zidlen = _z_id_len(msg->_ext_responder._zid); extheader = (zidlen - 1) << 4; _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, zidlen + 1 + _z_zint_len(msg->_ext_responder._eid))); _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, msg->_ext_responder._zid.id, 0, zidlen)); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_ext_responder._eid)); } @@ -401,7 +402,7 @@ int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_n_msg_declare_t *decl) { 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))); - _Z_RETURN_IF_ERR(_z_timestamp_encode(wbf, &decl->_ext_timestamp)); + _Z_RETURN_IF_ERR(_z_timestamp_encode_ext(wbf, &decl->_ext_timestamp)); } return _z_declaration_encode(wbf, &decl->_decl); } diff --git a/src/protocol/core.c b/src/protocol/core.c index 403325c55..c5cfc0c3f 100644 --- a/src/protocol/core.c +++ b/src/protocol/core.c @@ -7,6 +7,7 @@ uint8_t _z_id_len(_z_id_t id) { while (len > 0) { --len; if (id.id[len] != 0) { + ++len; break; } } diff --git a/src/protocol/definitions/declarations.c b/src/protocol/definitions/declarations.c index a2d13991a..ca1572bcd 100644 --- a/src/protocol/definitions/declarations.c +++ b/src/protocol/definitions/declarations.c @@ -74,7 +74,7 @@ _z_declaration_t _z_make_decl_subscriber(_Z_MOVE(_z_keyexpr_t) key, uint32_t id, ._tag = _Z_DECL_SUBSCRIBER, ._body = {._decl_subscriber = {._id = id, ._keyexpr = _z_keyexpr_steal(key), - ._ext_subscriber_info = {._pull_mode = pull_mode, ._reliable = reliable}}}}; + ._ext_subinfo = {._pull_mode = pull_mode, ._reliable = reliable}}}}; } _z_declaration_t _z_make_undecl_subscriber(uint32_t id, _Z_OPTIONAL _Z_MOVE(_z_keyexpr_t) key) { return (_z_declaration_t){._tag = _Z_UNDECL_SUBSCRIBER, @@ -116,3 +116,14 @@ _z_declaration_t _z_make_decl_interest(_Z_MOVE(_z_keyexpr_t) key, uint32_t id) { _z_declaration_t _z_make_final_decl(uint32_t id) { return (_z_declaration_t){._tag = _Z_FINAL_INTEREST, ._body = {._final_interest = {._id = id}}}; } +_z_decl_kexpr_t _z_decl_kexpr_null(void) { return (_z_decl_kexpr_t){0}; } +_z_decl_subscriber_t _z_decl_subscriber_null(void) { return (_z_decl_subscriber_t){0}; } +_z_decl_queryable_t _z_decl_queryable_null(void) { return (_z_decl_queryable_t){0}; } +_z_decl_token_t _z_decl_token_null(void) { return (_z_decl_token_t){0}; } +_z_decl_interest_t _z_decl_interest_null(void) { return (_z_decl_interest_t){0}; } +_z_undecl_kexpr_t _z_undecl_kexpr_null(void) { return (_z_undecl_kexpr_t){0}; } +_z_undecl_subscriber_t _z_undecl_subscriber_null(void) { return (_z_undecl_subscriber_t){0}; } +_z_undecl_queryable_t _z_undecl_queryable_null(void) { return (_z_undecl_queryable_t){0}; } +_z_undecl_token_t _z_undecl_token_null(void) { return (_z_undecl_token_t){0}; } +_z_undecl_interest_t _z_undecl_interest_null(void) { return (_z_undecl_interest_t){0}; } +_z_final_interest_t _z_final_interest_null(void) { return (_z_final_interest_t){0}; } diff --git a/src/protocol/definitions/message.c b/src/protocol/definitions/message.c index 677b20bdb..cfbb81f24 100644 --- a/src/protocol/definitions/message.c +++ b/src/protocol/definitions/message.c @@ -9,14 +9,14 @@ void _z_msg_put_clear(_z_msg_put_t *msg) { } _z_msg_query_reqexts_t _z_msg_query_required_extensions(const _z_msg_query_t *msg) { - return (_z_msg_query_reqexts_t){.body = msg->_value.payload.start != NULL, - .info = _z_id_check(msg->_info._id), - .consolidation = msg->_consolidation != Z_CONSOLIDATION_MODE_AUTO}; + return (_z_msg_query_reqexts_t){.body = msg->_ext_value.payload.start != NULL, + .info = _z_id_check(msg->_ext_info._id), + .consolidation = msg->_ext_consolidation != Z_CONSOLIDATION_MODE_AUTO}; } void _z_msg_query_clear(_z_msg_query_t *msg) { _z_bytes_clear(&msg->_parameters); - _z_value_clear(&msg->_value); + _z_value_clear(&msg->_ext_value); } void _z_msg_err_clear(_z_msg_err_t *err) { _z_timestamp_clear(&err->_timestamp); diff --git a/src/protocol/definitions/network.c b/src/protocol/definitions/network.c index f5d6938c8..c161847d0 100644 --- a/src/protocol/definitions/network.c +++ b/src/protocol/definitions/network.c @@ -7,11 +7,11 @@ _z_n_msg_request_exts_t _z_n_msg_request_needed_exts(const _z_n_msg_request_t *msg) { _z_n_msg_request_exts_t ret = {.n = 0, - .ext_budget = msg->ext_budget != 0, - .ext_target = msg->ext_target != Z_QUERY_TARGET_BEST_MATCHING, - .ext_qos = msg->ext_qos._val != _Z_N_QOS_DEFAULT._val, - .ext_timeout_ms = msg->ext_timeout_ms != 0, - .ext_tstamp = _z_timestamp_check(&msg->ext_tstamp)}; + .ext_budget = msg->_ext_budget != 0, + .ext_target = msg->_ext_target != Z_QUERY_TARGET_BEST_MATCHING, + .ext_qos = msg->_ext_qos._val != _Z_N_QOS_DEFAULT._val, + .ext_timeout_ms = msg->_ext_timeout_ms != 0, + .ext_tstamp = _z_timestamp_check(&msg->_ext_timestamp)}; if (ret.ext_budget) { ret.n += 1; } @@ -37,13 +37,13 @@ void _z_n_msg_request_clear(_z_n_msg_request_t *msg) { _z_msg_query_clear(&msg->_body._query); } break; case _Z_REQUEST_PUT: { - _z_msg_put_clear(&msg->_body.put); + _z_msg_put_clear(&msg->_body._put); } break; case _Z_REQUEST_DEL: { - _z_msg_del_clear(&msg->_body.del); + _z_msg_del_clear(&msg->_body._del); } break; case _Z_REQUEST_PULL: { - _z_msg_pull_clear(&msg->_body.pull); + _z_msg_pull_clear(&msg->_body._pull); } break; } } @@ -141,13 +141,13 @@ _z_network_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id) { ._tag = _Z_REQUEST_PULL, ._body = { - .pull = {._ext_source_info = _z_source_info_null()}, + ._pull = {._ext_source_info = _z_source_info_null()}, }, - .ext_budget = 0, - .ext_qos = _Z_N_QOS_DEFAULT, - .ext_target = Z_QUERY_TARGET_BEST_MATCHING, - .ext_tstamp = _z_timestamp_null(), - .ext_timeout_ms = 0, + ._ext_budget = 0, + ._ext_qos = _Z_N_QOS_DEFAULT, + ._ext_target = Z_QUERY_TARGET_BEST_MATCHING, + ._ext_timestamp = _z_timestamp_null(), + ._ext_timeout_ms = 0, }, }, }; @@ -165,15 +165,15 @@ _z_zenoh_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes ._body._query = { ._parameters = _z_bytes_steal(parameters), - ._consolidation = consolidation, - ._value = _z_value_steal(value), - ._info = _z_source_info_null(), + ._ext_consolidation = consolidation, + ._ext_value = _z_value_steal(value), + ._ext_info = _z_source_info_null(), }, - .ext_budget = 0, - .ext_qos = _Z_N_QOS_DEFAULT, - .ext_target = Z_QUERY_TARGET_BEST_MATCHING, - .ext_timeout_ms = 0, - .ext_tstamp = _z_timestamp_null(), + ._ext_budget = 0, + ._ext_qos = _Z_N_QOS_DEFAULT, + ._ext_target = Z_QUERY_TARGET_BEST_MATCHING, + ._ext_timeout_ms = 0, + ._ext_timestamp = _z_timestamp_null(), }, }; } diff --git a/src/protocol/iobuf.c b/src/protocol/iobuf.c index 9652d79a8..3af776758 100644 --- a/src/protocol/iobuf.c +++ b/src/protocol/iobuf.c @@ -88,7 +88,7 @@ void _z_iosli_write_bytes(_z_iosli_t *ios, const uint8_t *bs, size_t offset, siz 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); - ios->_w_pos = ios->_w_pos + length; + ios->_w_pos += length; } void _z_iosli_put(_z_iosli_t *ios, uint8_t b, size_t pos) { diff --git a/src/session/queryable.c b/src/session/queryable.c index 20cf3ace6..1883e0f23 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -163,8 +163,8 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons memcpy(params, query->_parameters.start, query->_parameters.len); params[query->_parameters.len] = 0; q._parameters = params; - q._value.encoding = query->_value.encoding; - q._value.payload = query->_value.payload; + q._value.encoding = query->_ext_value.encoding; + q._value.payload = query->_ext_value.payload; q._anyke = (strstr(q._parameters, Z_SELECTOR_QUERY_MATCH) == NULL) ? false : true; _z_questionable_sptr_list_t *xs = qles; while (xs != NULL) { diff --git a/src/session/rx.c b/src/session/rx.c index 4c445fe32..10e16b06d 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -50,7 +50,7 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg) { ret = _z_trigger_queryables(zn, query, req._key, req._rid); } break; case _Z_REQUEST_PUT: { - _z_msg_put_t put = req._body.put; + _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); if (ret == _Z_RES_OK) { @@ -61,7 +61,7 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg) { } } break; case _Z_REQUEST_DEL: { - _z_msg_del_t del = req._body.del; + _z_msg_del_t del = req._body._del; int8_t result = _z_trigger_subscriptions(zn, req._key, _z_bytes_empty(), z_encoding_default(), Z_SAMPLE_KIND_PUT, del._commons._timestamp); if (ret == _Z_RES_OK) { diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 78995de07..676636403 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -187,7 +187,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t _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_zenoh_message(ztm->_session, &zm); _z_msg_clear(&zm); // Clear must be explicitly called for fragmented zenoh messages. Non-fragmented diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 678b221e2..ef1231d62 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -12,21 +12,29 @@ // ZettaScale Zenoh Team, // -#include -#include - -#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/api/constants.h" +#include "zenoh-pico/protocol/codec/message.h" +#include "zenoh-pico/protocol/codec/network.h" +#include "zenoh-pico/protocol/definitions/message.h" #define ZENOH_PICO_TEST_H #include +#include #include #include #include #include +#include #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/string.h" +#include "zenoh-pico/protocol/codec/core.h" +#include "zenoh-pico/protocol/codec/declarations.h" #include "zenoh-pico/protocol/codec/ext.h" +#include "zenoh-pico/protocol/codec/transport.h" +#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/definitions/declarations.h" +#include "zenoh-pico/protocol/definitions/network.h" #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/keyexpr.h" @@ -257,7 +265,7 @@ void assert_eq_iosli(_z_iosli_t *left, _z_iosli_t *right) { printf(")"); } -void assert_eq_uint8_array(_z_bytes_t *left, _z_bytes_t *right) { +void assert_eq_uint8_array(const _z_bytes_t *left, const _z_bytes_t *right) { printf("Array -> "); printf("Length (%zu:%zu), ", left->len, right->len); @@ -441,101 +449,11 @@ void zbuf_extension(void) { _z_wbuf_clear(&wbf); } -_z_msg_ext_t gen_message_extension(void) { - _z_msg_ext_t p_zm; - - uint8_t mids[] = {_Z_MOCK_EXTENSION_UNIT, _Z_MOCK_EXTENSION_ZINT, _Z_MOCK_EXTENSION_ZBUF}; - uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); - - switch (mids[i]) { - case _Z_MOCK_EXTENSION_UNIT: - p_zm = gen_unit_extension(); - break; - case _Z_MOCK_EXTENSION_ZINT: - p_zm = gen_zint_extension(); - break; - case _Z_MOCK_EXTENSION_ZBUF: - p_zm = gen_zbuf_extension(); - break; - default: - assert(0); - break; - } - - return p_zm; -} - -void assert_eq_message_extension(_z_msg_ext_t *left, _z_msg_ext_t *right) { - printf(" Header (%x:%x)", left->_header, right->_header); - assert(left->_header == right->_header); - printf("\n"); - - switch (_Z_EXT_ID(left->_header)) { - case _Z_MOCK_EXTENSION_UNIT: - assert_eq_unit_extension(&left->_body._unit, &left->_body._unit); - break; - case _Z_MOCK_EXTENSION_ZINT: - assert_eq_zint_extension(&left->_body._zint, &left->_body._zint); - break; - case _Z_MOCK_EXTENSION_ZBUF: - assert_eq_zbuf_extension(&left->_body._zbuf, &left->_body._zbuf); - break; - default: - assert(0); - break; - } -} - -void message_extension(void) { - printf("\n>> Message extension\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_msg_ext_t e_me = gen_message_extension(); - - printf(" - "); - switch (_Z_EXT_ID(e_me._header)) { - case _Z_MOCK_EXTENSION_UNIT: - printf("UNIT extension"); - break; - case _Z_MOCK_EXTENSION_ZINT: - printf("ZINT extension"); - break; - case _Z_MOCK_EXTENSION_ZBUF: - printf("ZBUF extension"); - break; - default: - assert(0); - break; - } - printf("\n"); - - // Encode - int8_t res = _z_msg_ext_encode(&wbf, &e_me, true); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_msg_ext_t d_me; - _Bool has_next; - res = _z_msg_ext_decode(&d_me, &zbf, &has_next); - assert(res == _Z_RES_OK); - - assert_eq_message_extension(&e_me, &d_me); - - // Free - _z_msg_ext_clear(&e_me); - _z_msg_ext_clear(&d_me); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - /*=============================*/ /* Message Fields */ /*=============================*/ /*------------------ Payload field ------------------*/ -void assert_eq_payload(_z_bytes_t *left, _z_bytes_t *right) { assert_eq_uint8_array(left, right); } +void assert_eq_bytes(const _z_bytes_t *left, const _z_bytes_t *right) { assert_eq_uint8_array(left, right); } void payload_field(void) { printf("\n>> Payload field\n"); @@ -545,7 +463,7 @@ void payload_field(void) { _z_bytes_t e_pld = gen_payload(64); // Encode - int8_t res = _z_payload_encode(&wbf, &e_pld); + int8_t res = _z_bytes_encode(&wbf, &e_pld); assert(res == _Z_RES_OK); (void)(res); @@ -553,10 +471,10 @@ void payload_field(void) { _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); _z_bytes_t d_pld; - res = _z_payload_decode(&d_pld, &zbf); + res = _z_bytes_decode(&d_pld, &zbf); assert(res == _Z_RES_OK); printf(" "); - assert_eq_payload(&e_pld, &d_pld); + assert_eq_bytes(&e_pld, &d_pld); printf("\n"); // Free @@ -566,6 +484,24 @@ void payload_field(void) { _z_wbuf_clear(&wbf); } +_z_source_info_t gen_source_info(void) { + return (_z_source_info_t){._id = gen_zid(), ._source_sn = gen_uint64(), ._entity_id = gen_uint64()}; +} + +void assert_eq_source_info(const _z_source_info_t *left, const _z_source_info_t *right) { + assert(left->_source_sn == right->_source_sn); + assert(left->_entity_id == right->_entity_id); + assert(memcmp(left->_id.id, right->_id.id, 16) == 0); +} +_z_encoding_t gen_encoding(void) { return (_z_encoding_t){.prefix = gen_uint64(), .suffix = gen_bytes(16)}; } +void assert_eq_encoding(const _z_encoding_t *left, const _z_encoding_t *right) { + assert(left->prefix == right->prefix); + assert_eq_bytes(&left->suffix, &right->suffix); +} +void assert_eq_value(const _z_value_t *left, const _z_value_t *right) { + assert_eq_encoding(&left->encoding, &right->encoding); + assert_eq_bytes(&left->payload, &right->payload); +} /*------------------ Timestamp field ------------------*/ _z_timestamp_t gen_timestamp(void) { _z_timestamp_t ts; @@ -575,15 +511,19 @@ _z_timestamp_t gen_timestamp(void) { return ts; } -void assert_eq_timestamp(_z_timestamp_t *left, _z_timestamp_t *right) { +void assert_eq_timestamp(const _z_timestamp_t *left, const _z_timestamp_t *right) { printf("Timestamp -> "); printf("Time (%llu:%llu), ", (unsigned long long)left->time, (unsigned long long)right->time); assert(left->time == right->time); printf("ID ("); + for (int i = 0; i < 16; i++) { + printf("%02x:%02x, ", left->id.id[i], right->id.id[i]); + } + printf(")\n"); assert(memcmp(left->id.id, right->id.id, 16) == 0); - printf(")"); } +/*------------------ Timestamp field ------------------*/ void timestamp_field(void) { printf("\n>> Timestamp field\n"); @@ -601,6 +541,7 @@ void timestamp_field(void) { _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); print_wbuf(&wbf); print_iosli(&zbf._ios); + printf("\n"); _z_timestamp_t d_ts; res = _z_timestamp_decode(&d_ts, &zbf); assert(res == _Z_RES_OK); @@ -652,54 +593,30 @@ void assert_eq_subinfo(_z_subinfo_t *left, _z_subinfo_t *right) { assert(left->period.duration == right->period.duration); } -void subinfo_field(void) { - printf("\n>> SubInfo field\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_subinfo_t e_sm = gen_subinfo(); - - // Encode - uint8_t header = e_sm.reliability == Z_RELIABILITY_RELIABLE ? _Z_FLAG_Z_R : 0; - int8_t res = _z_subinfo_encode(&wbf, &e_sm); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_subinfo_t d_sm; - res = _z_subinfo_decode(&d_sm, &zbf, header); - assert(res == _Z_RES_OK); - - printf(" "); - assert_eq_subinfo(&e_sm, &d_sm); - printf("\n"); - - // Free - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - /*------------------ ResKey field ------------------*/ _z_keyexpr_t gen_keyexpr(void) { _z_keyexpr_t key; key._id = gen_zint(); _Bool is_numerical = gen_bool(); - if (is_numerical == true) + if (is_numerical == true) { key._suffix = NULL; - else + key._owns_suffix = false; + } else { key._suffix = gen_str(gen_zint() % 16); - + key._owns_suffix = true; + } + key._uses_remote_mapping = gen_bool(); return key; } -void assert_eq_keyexpr(_z_keyexpr_t *left, _z_keyexpr_t *right, uint8_t header) { +void assert_eq_keyexpr(const _z_keyexpr_t *left, const _z_keyexpr_t *right) { printf("ResKey -> "); - printf("ID (%zu:%zu), ", left->_id, right->_id); + printf("ID (%u:%u), ", left->_id, right->_id); assert(left->_id == right->_id); + assert(!(_z_keyexpr_has_suffix(*left) ^ _z_keyexpr_has_suffix(*right))); printf("Name ("); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_K) == true) { + if (_z_keyexpr_has_suffix(*left)) { printf("%s:%s", left->_suffix, right->_suffix); assert(_z_str_eq(left->_suffix, right->_suffix) == true); } else { @@ -728,7 +645,7 @@ void keyexpr_field(void) { assert(res == _Z_RES_OK); printf(" "); - assert_eq_keyexpr(&e_rk, &d_rk, header); + assert_eq_keyexpr(&e_rk, &d_rk); printf("\n"); // Free @@ -742,20 +659,14 @@ void keyexpr_field(void) { /* Declaration Fields */ /*=============================*/ /*------------------ Resource declaration ------------------*/ -_z_res_decl_t gen_resource_declaration(uint8_t *header) { - _z_res_decl_t e_rd; - - e_rd._id = gen_zint(); - e_rd._key = gen_keyexpr(); - _Z_SET_FLAG(*header, (e_rd._key._suffix) ? _Z_FLAG_Z_K : 0); - - return e_rd; +_z_decl_kexpr_t gen_resource_declaration(void) { + return (_z_decl_kexpr_t){._id = gen_zint(), ._keyexpr = gen_keyexpr()}; } -void assert_eq_resource_declaration(_z_res_decl_t *left, _z_res_decl_t *right, uint8_t header) { - printf("RID (%zu:%zu), ", left->_id, right->_id); +void assert_eq_resource_declaration(const _z_decl_kexpr_t *left, const _z_decl_kexpr_t *right) { + printf("RID (%u:%u), ", left->_id, right->_id); assert(left->_id == right->_id); - assert_eq_keyexpr(&left->_key, &right->_key, header); + assert_eq_keyexpr(&left->_keyexpr, &right->_keyexpr); } void resource_declaration(void) { @@ -763,95 +674,47 @@ void resource_declaration(void) { _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - uint8_t e_hdr = 0; - _z_res_decl_t e_rd = gen_resource_declaration(&e_hdr); - - // Encode - int8_t res = _z_res_decl_encode(&wbf, e_hdr, &e_rd); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_res_decl_t d_rd; - res = _z_res_decl_decode(&d_rd, &zbf, e_hdr); - assert(res == _Z_RES_OK); - - printf(" "); - assert_eq_resource_declaration(&e_rd, &d_rd, e_hdr); - printf("\n"); - - // Free - _z_declaration_clear_resource(&e_rd); - _z_declaration_clear_resource(&d_rd); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ Publisher declaration ------------------*/ -_z_pub_decl_t gen_publisher_declaration(uint8_t *header) { - _z_pub_decl_t e_pd; - - e_pd._key = gen_keyexpr(); - _Z_SET_FLAG(*header, (e_pd._key._suffix) ? _Z_FLAG_Z_K : 0); - - return e_pd; -} - -void assert_eq_publisher_declaration(_z_pub_decl_t *left, _z_pub_decl_t *right, uint8_t header) { - assert_eq_keyexpr(&left->_key, &right->_key, header); -} - -void publisher_declaration(void) { - printf("\n>> Publisher declaration\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - uint8_t e_hdr = 0; - _z_pub_decl_t e_pd = gen_publisher_declaration(&e_hdr); + _z_decl_kexpr_t e_rd = gen_resource_declaration(); // Encode - int8_t res = _z_pub_decl_encode(&wbf, e_hdr, &e_pd); + int8_t res = _z_decl_kexpr_encode(&wbf, &e_rd); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_pub_decl_t d_pd; - res = _z_pub_decl_decode(&d_pd, &zbf, e_hdr); + _z_decl_kexpr_t d_rd; + uint8_t e_hdr; + _z_uint8_decode(&e_hdr, &zbf); + res = _z_decl_kexpr_decode(&d_rd, &zbf, e_hdr); assert(res == _Z_RES_OK); printf(" "); - assert_eq_publisher_declaration(&e_pd, &d_pd, e_hdr); + assert_eq_resource_declaration(&e_rd, &d_rd); printf("\n"); // Free - _z_declaration_clear_publisher(&e_pd); - _z_declaration_clear_publisher(&d_pd); + _z_keyexpr_clear(&e_rd._keyexpr); + _z_keyexpr_clear(&d_rd._keyexpr); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } /*------------------ Subscriber declaration ------------------*/ -_z_sub_decl_t gen_subscriber_declaration(uint8_t *header) { - _z_sub_decl_t e_sd; - - e_sd._subinfo = gen_subinfo(); - if (e_sd._subinfo.mode != Z_SUBMODE_PUSH || e_sd._subinfo.period.period != 0) _Z_SET_FLAG(*header, _Z_FLAG_Z_S); - if (e_sd._subinfo.reliability == Z_RELIABILITY_RELIABLE) _Z_SET_FLAG(*header, _Z_FLAG_Z_R); - - e_sd._key = gen_keyexpr(); - if (e_sd._key._suffix) _Z_SET_FLAG(*header, _Z_FLAG_Z_K); - +_z_decl_subscriber_t gen_subscriber_declaration(void) { + __auto_type subinfo = gen_subinfo(); + _z_decl_subscriber_t e_sd = {._keyexpr = gen_keyexpr(), + ._id = gen_uint64(), + ._ext_subinfo = {._pull_mode = subinfo.mode == Z_SUBMODE_PULL, + ._reliable = subinfo.reliability == Z_RELIABILITY_RELIABLE}}; return e_sd; } -void assert_eq_subscriber_declaration(_z_sub_decl_t *left, _z_sub_decl_t *right, uint8_t header) { - assert_eq_keyexpr(&left->_key, &right->_key, header); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_S) == true) { - printf(", "); - assert_eq_subinfo(&left->_subinfo, &right->_subinfo); - } +void assert_eq_subscriber_declaration(_z_decl_subscriber_t *left, _z_decl_subscriber_t *right) { + assert_eq_keyexpr(&left->_keyexpr, &right->_keyexpr); + assert(left->_id == right->_id); + assert(left->_ext_subinfo._pull_mode == right->_ext_subinfo._pull_mode); + assert(left->_ext_subinfo._reliable == right->_ext_subinfo._reliable); } void subscriber_declaration(void) { @@ -859,56 +722,47 @@ void subscriber_declaration(void) { _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - uint8_t e_hdr = 0; - _z_sub_decl_t e_sd = gen_subscriber_declaration(&e_hdr); - + _z_decl_subscriber_t e_sd = gen_subscriber_declaration(); // Encode - int8_t res = _z_sub_decl_encode(&wbf, e_hdr, &e_sd); + int8_t res = _z_decl_subscriber_encode(&wbf, &e_sd); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_sub_decl_t d_sd; - res = _z_sub_decl_decode(&d_sd, &zbf, e_hdr); + _z_decl_subscriber_t d_sd; + uint8_t e_hdr; + _z_uint8_decode(&e_hdr, &zbf); + res = _z_decl_subscriber_decode(&d_sd, &zbf, e_hdr); assert(res == _Z_RES_OK); printf(" "); - assert_eq_subscriber_declaration(&e_sd, &d_sd, e_hdr); + assert_eq_subscriber_declaration(&e_sd, &d_sd); printf("\n"); // Free - _z_declaration_clear_subscriber(&e_sd); - _z_declaration_clear_subscriber(&d_sd); + _z_keyexpr_clear(&e_sd._keyexpr); + _z_keyexpr_clear(&d_sd._keyexpr); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } /*------------------ Queryable declaration ------------------*/ -_z_qle_decl_t gen_queryable_declaration(uint8_t *header) { - _z_qle_decl_t e_qd; - - e_qd._key = gen_keyexpr(); - _Z_SET_FLAG(*header, (e_qd._key._suffix) ? _Z_FLAG_Z_K : 0); - - if (gen_bool()) { - e_qd._complete = gen_zint(); - e_qd._distance = gen_zint(); - _Z_SET_FLAG(*header, _Z_FLAG_Z_Q); - } +_z_decl_queryable_t gen_queryable_declaration(void) { + _z_decl_queryable_t e_qd = {._keyexpr = gen_keyexpr(), + ._id = gen_uint64(), + ._ext_queryable_info = {._complete = gen_zint(), ._distance = gen_zint()}}; return e_qd; } -void assert_eq_queryable_declaration(_z_qle_decl_t *left, _z_qle_decl_t *right, uint8_t header) { - assert_eq_keyexpr(&left->_key, &right->_key, header); +void assert_eq_queryable_declaration(_z_decl_queryable_t *left, _z_decl_queryable_t *right) { + assert_eq_keyexpr(&left->_keyexpr, &right->_keyexpr); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_I) == true) { - printf("Complete (%zu:%zu), ", left->_complete, right->_complete); - assert(left->_complete == right->_complete); - printf("Distance (%zu:%zu), ", left->_distance, right->_distance); - assert(left->_distance == right->_distance); - } + printf("Complete (%u:%u), ", left->_ext_queryable_info._complete, right->_ext_queryable_info._complete); + assert(left->_ext_queryable_info._complete == right->_ext_queryable_info._complete); + printf("Distance (%u:%u), ", left->_ext_queryable_info._distance, right->_ext_queryable_info._distance); + assert(left->_ext_queryable_info._distance == right->_ext_queryable_info._distance); } void queryable_declaration(void) { @@ -916,43 +770,44 @@ void queryable_declaration(void) { _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - uint8_t e_hdr = 0; - _z_qle_decl_t e_qd = gen_queryable_declaration(&e_hdr); + _z_decl_queryable_t e_qd = gen_queryable_declaration(); // Encode - int8_t res = _z_qle_decl_encode(&wbf, e_hdr, &e_qd); + int8_t res = _z_decl_queryable_encode(&wbf, &e_qd); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_qle_decl_t d_qd; - res = _z_qle_decl_decode(&d_qd, &zbf, e_hdr); + _z_decl_queryable_t d_qd; + uint8_t e_hdr = 0; + _z_uint8_decode(&e_hdr, &zbf); + res = _z_decl_queryable_decode(&d_qd, &zbf, e_hdr); assert(res == _Z_RES_OK); printf(" "); - assert_eq_queryable_declaration(&e_qd, &d_qd, e_hdr); + assert_eq_queryable_declaration(&e_qd, &d_qd); printf("\n"); // Free - _z_declaration_clear_queryable(&e_qd); - _z_declaration_clear_queryable(&d_qd); + _z_keyexpr_clear(&e_qd._keyexpr); + _z_keyexpr_clear(&d_qd._keyexpr); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } /*------------------ Forget Resource declaration ------------------*/ -_z_forget_res_decl_t gen_forget_resource_declaration(void) { - _z_forget_res_decl_t e_frd; +_z_undecl_kexpr_t gen_forget_resource_declaration(void) { + _z_undecl_kexpr_t e_frd; - e_frd._rid = gen_zint(); + e_frd._id = gen_zint(); return e_frd; } -void assert_eq_forget_resource_declaration(_z_forget_res_decl_t *left, _z_forget_res_decl_t *right) { - printf("RID (%zu:%zu)", left->_rid, right->_rid); - assert(left->_rid == right->_rid); +void assert_eq_forget_resource_declaration(_z_undecl_kexpr_t *left, _z_undecl_kexpr_t *right) { + printf("RID (%u:%u)", left->_id, right->_id); + assert(left->_id == right->_id); } void forget_resource_declaration(void) { @@ -960,17 +815,19 @@ void forget_resource_declaration(void) { _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - _z_forget_res_decl_t e_frd = gen_forget_resource_declaration(); + _z_undecl_kexpr_t e_frd = gen_forget_resource_declaration(); // Encode - int8_t res = _z_forget_res_decl_encode(&wbf, &e_frd); + int8_t res = _z_undecl_kexpr_encode(&wbf, &e_frd); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_forget_res_decl_t d_frd; - res = _z_forget_res_decl_decode(&d_frd, &zbf); + _z_undecl_kexpr_t d_frd; + uint8_t hdr; + _z_uint8_decode(&hdr, &zbf); + res = _z_undecl_kexpr_decode(&d_frd, &zbf, hdr); assert(res == _Z_RES_OK); printf(" "); @@ -983,62 +840,15 @@ void forget_resource_declaration(void) { _z_wbuf_clear(&wbf); } -/*------------------ Forget Publisher declaration ------------------*/ -_z_forget_pub_decl_t gen_forget_publisher_declaration(uint8_t *header) { - _z_forget_pub_decl_t e_fpd; - - e_fpd._key = gen_keyexpr(); - _Z_SET_FLAG(*header, (e_fpd._key._suffix) ? _Z_FLAG_Z_K : 0); - - return e_fpd; -} - -void assert_eq_forget_publisher_declaration(_z_forget_pub_decl_t *left, _z_forget_pub_decl_t *right, uint8_t header) { - assert_eq_keyexpr(&left->_key, &right->_key, header); -} - -void forget_publisher_declaration(void) { - printf("\n>> Forget publisher declaration\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - uint8_t e_hdr = 0; - _z_forget_pub_decl_t e_fpd = gen_forget_publisher_declaration(&e_hdr); - - // Encode - int8_t res = _z_forget_pub_decl_encode(&wbf, e_hdr, &e_fpd); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_forget_pub_decl_t d_fpd; - res = _z_forget_pub_decl_decode(&d_fpd, &zbf, e_hdr); - assert(res == _Z_RES_OK); - - printf(" "); - assert_eq_forget_publisher_declaration(&e_fpd, &d_fpd, e_hdr); - printf("\n"); - - // Free - _z_declaration_clear_forget_publisher(&e_fpd); - _z_declaration_clear_forget_publisher(&d_fpd); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - /*------------------ Forget Subscriber declaration ------------------*/ -_z_forget_sub_decl_t gen_forget_subscriber_declaration(uint8_t *header) { - _z_forget_sub_decl_t e_fsd; - - e_fsd._key = gen_keyexpr(); - _Z_SET_FLAG(*header, (e_fsd._key._suffix) ? _Z_FLAG_Z_K : 0); - +_z_undecl_subscriber_t gen_forget_subscriber_declaration(void) { + _z_undecl_subscriber_t e_fsd = {._ext_keyexpr = gen_keyexpr(), ._id = gen_uint64()}; return e_fsd; } -void assert_eq_forget_subscriber_declaration(_z_forget_sub_decl_t *left, _z_forget_sub_decl_t *right, uint8_t header) { - assert_eq_keyexpr(&left->_key, &right->_key, header); +void assert_eq_forget_subscriber_declaration(_z_undecl_subscriber_t *left, _z_undecl_subscriber_t *right) { + assert_eq_keyexpr(&left->_ext_keyexpr, &right->_ext_keyexpr); + assert(left->_id == right->_id); } void forget_subscriber_declaration(void) { @@ -1046,43 +856,42 @@ void forget_subscriber_declaration(void) { _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - uint8_t e_hdr = 0; - _z_forget_sub_decl_t e_fsd = gen_forget_subscriber_declaration(&e_hdr); + _z_undecl_subscriber_t e_fsd = gen_forget_subscriber_declaration(); // Encode - int8_t res = _z_forget_sub_decl_encode(&wbf, e_hdr, &e_fsd); + int8_t res = _z_undecl_subscriber_encode(&wbf, &e_fsd); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_forget_sub_decl_t d_fsd; - res = _z_forget_sub_decl_decode(&d_fsd, &zbf, e_hdr); + _z_undecl_subscriber_t d_fsd = { + ._id = 0, ._ext_keyexpr = {._id = 0, ._owns_suffix = false, ._suffix = NULL, ._uses_remote_mapping = false}}; + uint8_t e_hdr = 0; + _z_uint8_decode(&e_hdr, &zbf); + res = _z_undecl_subscriber_decode(&d_fsd, &zbf, e_hdr); assert(res == _Z_RES_OK); printf(" "); - assert_eq_forget_subscriber_declaration(&e_fsd, &d_fsd, e_hdr); + assert_eq_forget_subscriber_declaration(&e_fsd, &d_fsd); printf("\n"); // Free - _z_declaration_clear_forget_subscriber(&e_fsd); - _z_declaration_clear_forget_subscriber(&d_fsd); + _z_keyexpr_clear(&e_fsd._ext_keyexpr); + _z_keyexpr_clear(&d_fsd._ext_keyexpr); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } /*------------------ Forget Queryable declaration ------------------*/ -_z_forget_qle_decl_t gen_forget_queryable_declaration(uint8_t *header) { - _z_forget_qle_decl_t e_fqd; - - e_fqd._key = gen_keyexpr(); - _Z_SET_FLAG(*header, (e_fqd._key._suffix) ? _Z_FLAG_Z_K : 0); - +_z_undecl_queryable_t gen_forget_queryable_declaration(void) { + _z_undecl_queryable_t e_fqd = {._ext_keyexpr = gen_keyexpr(), ._id = gen_zint()}; return e_fqd; } -void assert_eq_forget_queryable_declaration(_z_forget_qle_decl_t *left, _z_forget_qle_decl_t *right, uint8_t header) { - assert_eq_keyexpr(&left->_key, &right->_key, header); +void assert_eq_forget_queryable_declaration(_z_undecl_queryable_t *left, _z_undecl_queryable_t *right) { + assert_eq_keyexpr(&left->_ext_keyexpr, &right->_ext_keyexpr); + assert(left->_id == right->_id); } void forget_queryable_declaration(void) { @@ -1090,65 +899,63 @@ void forget_queryable_declaration(void) { _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - uint8_t e_hdr = 0; - _z_forget_qle_decl_t e_fqd = gen_forget_queryable_declaration(&e_hdr); + _z_undecl_queryable_t e_fqd = gen_forget_queryable_declaration(); // Encode - int8_t res = _z_forget_qle_decl_encode(&wbf, e_hdr, &e_fqd); + int8_t res = _z_undecl_queryable_encode(&wbf, &e_fqd); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_forget_qle_decl_t d_fqd; - res = _z_forget_qle_decl_decode(&d_fqd, &zbf, e_hdr); + uint8_t e_hdr = 0; + _z_uint8_decode(&e_hdr, &zbf); + _z_undecl_queryable_t d_fqd = {._ext_keyexpr = _z_keyexpr_null()}; + res = _z_undecl_queryable_decode(&d_fqd, &zbf, e_hdr); assert(res == _Z_RES_OK); printf(" "); - assert_eq_forget_queryable_declaration(&e_fqd, &d_fqd, e_hdr); + assert_eq_forget_queryable_declaration(&e_fqd, &d_fqd); printf("\n"); // Free - _z_declaration_clear_forget_queryable(&e_fqd); - _z_declaration_clear_forget_queryable(&d_fqd); + _z_keyexpr_clear(&e_fqd._ext_keyexpr); + _z_keyexpr_clear(&d_fqd._ext_keyexpr); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } /*------------------ Declaration ------------------*/ _z_declaration_t gen_declaration(void) { - uint8_t decl[] = {_Z_DECL_RESOURCE, _Z_DECL_PUBLISHER, _Z_DECL_SUBSCRIBER, - _Z_DECL_QUERYABLE, _Z_DECL_FORGET_RESOURCE, _Z_DECL_FORGET_PUBLISHER, - _Z_DECL_FORGET_SUBSCRIBER, _Z_DECL_FORGET_QUERYABLE}; + uint8_t decl[] = { + _Z_DECL_KEXPR, _Z_UNDECL_KEXPR, _Z_DECL_SUBSCRIBER, + _Z_UNDECL_SUBSCRIBER, _Z_DECL_QUERYABLE, _Z_UNDECL_QUERYABLE, + }; _z_declaration_t d; - d._header = decl[gen_uint8() % (sizeof(decl) / sizeof(uint8_t))]; - - switch (d._header) { - case _Z_DECL_RESOURCE: - d._body._res = gen_resource_declaration(&d._header); - break; - case _Z_DECL_PUBLISHER: - d._body._pub = gen_publisher_declaration(&d._header); - break; - case _Z_DECL_SUBSCRIBER: - d._body._sub = gen_subscriber_declaration(&d._header); - break; - case _Z_DECL_QUERYABLE: - d._body._qle = gen_queryable_declaration(&d._header); - break; - case _Z_DECL_FORGET_RESOURCE: - d._body._forget_res = gen_forget_resource_declaration(); - break; - case _Z_DECL_FORGET_PUBLISHER: - d._body._forget_pub = gen_forget_publisher_declaration(&d._header); - break; - case _Z_DECL_FORGET_SUBSCRIBER: - d._body._forget_sub = gen_forget_subscriber_declaration(&d._header); - break; - case _Z_DECL_FORGET_QUERYABLE: - d._body._forget_qle = gen_forget_queryable_declaration(&d._header); - break; + d._tag = decl[gen_uint8() % (sizeof(decl) / sizeof(uint8_t))]; + + switch (d._tag) { + case _Z_DECL_KEXPR: { + d._body._decl_kexpr = gen_resource_declaration(); + } break; + case _Z_UNDECL_KEXPR: { + d._body._undecl_kexpr = gen_forget_resource_declaration(); + } break; + case _Z_DECL_SUBSCRIBER: { + d._body._decl_subscriber = gen_subscriber_declaration(); + } break; + case _Z_UNDECL_SUBSCRIBER: { + d._body._undecl_subscriber = gen_forget_subscriber_declaration(); + } break; + case _Z_DECL_QUERYABLE: { + d._body._decl_queryable = gen_queryable_declaration(); + } break; + case _Z_UNDECL_QUERYABLE: { + d._body._undecl_queryable = gen_forget_queryable_declaration(); + } break; + default: + assert(false); } return d; @@ -1156,1682 +963,496 @@ _z_declaration_t gen_declaration(void) { void assert_eq_declaration(_z_declaration_t *left, _z_declaration_t *right) { printf("Declaration -> "); - printf("Header (%x:%x), ", left->_header, right->_header); - assert(left->_header == right->_header); + printf("Header (%x:%x), ", left->_tag, right->_tag); + assert(left->_tag == right->_tag); - switch (left->_header) { - case _Z_DECL_RESOURCE: - assert_eq_resource_declaration(&left->_body._res, &right->_body._res, left->_header); - break; - case _Z_DECL_PUBLISHER: - assert_eq_publisher_declaration(&left->_body._pub, &right->_body._pub, left->_header); + switch (left->_tag) { + case _Z_DECL_KEXPR: + assert_eq_resource_declaration(&left->_body._decl_kexpr, &right->_body._decl_kexpr); break; case _Z_DECL_SUBSCRIBER: - assert_eq_subscriber_declaration(&left->_body._sub, &right->_body._sub, left->_header); + assert_eq_subscriber_declaration(&left->_body._decl_subscriber, &right->_body._decl_subscriber); break; case _Z_DECL_QUERYABLE: - assert_eq_queryable_declaration(&left->_body._qle, &right->_body._qle, left->_header); - break; - case _Z_DECL_FORGET_RESOURCE: - assert_eq_forget_resource_declaration(&left->_body._forget_res, &right->_body._forget_res); + assert_eq_queryable_declaration(&left->_body._decl_queryable, &right->_body._decl_queryable); break; - case _Z_DECL_FORGET_PUBLISHER: - assert_eq_forget_publisher_declaration(&left->_body._forget_pub, &right->_body._forget_pub, left->_header); + case _Z_UNDECL_KEXPR: + assert_eq_forget_resource_declaration(&left->_body._undecl_kexpr, &right->_body._undecl_kexpr); break; - case _Z_DECL_FORGET_SUBSCRIBER: - assert_eq_forget_subscriber_declaration(&left->_body._forget_sub, &right->_body._forget_sub, left->_header); + case _Z_UNDECL_SUBSCRIBER: + assert_eq_forget_subscriber_declaration(&left->_body._undecl_subscriber, &right->_body._undecl_subscriber); break; - case _Z_DECL_FORGET_QUERYABLE: - assert_eq_forget_queryable_declaration(&left->_body._forget_qle, &right->_body._forget_qle, left->_header); + case _Z_UNDECL_QUERYABLE: + assert_eq_forget_queryable_declaration(&left->_body._undecl_queryable, &right->_body._undecl_queryable); break; + default: + assert(false); } } -/*=============================*/ -/* Zenoh Messages */ -/*=============================*/ -/*------------------ Data message ------------------*/ -_z_zenoh_message_t gen_data_message(void) { - _z_keyexpr_t key = gen_keyexpr(); - - _z_data_info_t info; - if (gen_bool()) { - info = gen_data_info(); - } else { - info._flags = 0; - info._kind = Z_SAMPLE_KIND_PUT; - info._encoding.prefix = Z_ENCODING_PREFIX_EMPTY; - info._encoding.suffix = _z_bytes_empty(); - info._source_id = _z_bytes_empty(); - info._source_sn = 0; - _z_timestamp_reset(&info._tstamp); - } - - _Bool can_be_dropped = gen_bool(); - _z_bytes_t payload = gen_payload(1 + gen_zint() % 64); +/*------------------ Declare message ------------------*/ +_z_network_message_t gen_declare_message(void) { + _z_declaration_t declaration = gen_declaration(); - return _z_msg_make_data(key, info, payload, can_be_dropped); + return _z_n_msg_make_declare(declaration); } -void assert_eq_data_message(_z_msg_data_t *left, _z_msg_data_t *right, uint8_t header) { - printf(" "); - assert_eq_keyexpr(&left->_key, &right->_key, header); - printf("\n"); - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_I) == true) { - printf(" "); - assert_eq_data_info(&left->_info, &right->_info); - printf("\n"); - } +void assert_eq_declare_message(_z_n_msg_declare_t *left, _z_n_msg_declare_t *right) { printf(" "); - assert_eq_payload(&left->_payload, &right->_payload); + assert_eq_declaration(&left->_decl, &right->_decl); printf("\n"); } -void data_message(void) { - printf("\n>> Data message\n"); +void declare_message(void) { + printf("\n>> Declare message\n"); _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - _z_zenoh_message_t z_msg = gen_data_message(); - assert(_Z_MID(z_msg._header) == _Z_MID_Z_DATA); - - _z_msg_data_t e_da = z_msg._body._data; + _z_network_message_t n_msg = gen_declare_message(); // Encode - int8_t res = _z_data_encode(&wbf, z_msg._header, &e_da); + int8_t res = _z_network_message_encode(&wbf, &n_msg); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_msg_data_t d_da; - res = _z_data_decode(&d_da, &zbf, z_msg._header); + _z_network_message_t d_dcl = {0}; + res = _z_network_message_decode(&d_dcl, &zbf); assert(res == _Z_RES_OK); - assert_eq_data_message(&e_da, &d_da, z_msg._header); + assert_eq_declare_message(&n_msg._body._declare, &d_dcl._body._declare); // Free - _z_msg_clear_data(&d_da); - _z_msg_clear(&z_msg); + _z_n_msg_clear(&d_dcl); + _z_n_msg_clear(&n_msg); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } -/*------------------ Pull message ------------------*/ -_z_zenoh_message_t gen_pull_message(void) { - _z_keyexpr_t key = gen_keyexpr(); - _z_zint_t pull_id = gen_zint(); - _z_zint_t max_samples = gen_bool() ? gen_zint() : 0; - _Bool is_final = gen_bool(); - - return _z_msg_make_pull(key, pull_id, max_samples, is_final); +/*=============================*/ +/* Zenoh Messages */ +/*=============================*/ +/*------------------ Push message ------------------*/ +_z_push_body_t gen_push_body(void) { + _Bool isput = gen_bool(); + _z_timestamp_t ts = gen_bool() ? gen_timestamp() : _z_timestamp_null(); + _z_source_info_t sinfo = gen_bool() ? gen_source_info() : _z_source_info_null(); + _z_m_push_commons_t commons = {._source_info = sinfo, ._timestamp = ts}; + if (isput) { + return (_z_push_body_t){._is_put = true, + ._body._put = { + ._commons = commons, + ._payload = gen_bytes(64), + ._encoding = gen_encoding(), + }}; + } else { + return (_z_push_body_t){._is_put = false, ._body._del = {._commons = commons}}; + } } -void assert_eq_pull_message(_z_msg_pull_t *left, _z_msg_pull_t *right, uint8_t header) { - printf(" "); - assert_eq_keyexpr(&left->_key, &right->_key, header); - printf("\n"); - - printf(" Pull ID (%zu:%zu)", left->_pull_id, right->_pull_id); - assert(left->_pull_id == right->_pull_id); - printf("\n"); - - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_N) == true) { - printf(" Max samples (%zu:%zu)", left->_max_samples, right->_max_samples); - assert(left->_max_samples == right->_max_samples); - printf("\n"); +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_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); + } else { + assert_eq_timestamp(&left->_body._del._commons._timestamp, &right->_body._del._commons._timestamp); + assert_eq_source_info(&left->_body._del._commons._source_info, &right->_body._del._commons._source_info); } } -void pull_message(void) { - printf("\n>> Pull message\n"); +void push_body_message(void) { + printf("\n>> Put/Del message\n"); _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - _z_zenoh_message_t z_msg = gen_pull_message(); - assert(_Z_MID(z_msg._header) == _Z_MID_Z_PULL); - - _z_msg_pull_t e_pu = z_msg._body._pull; + __auto_type e_da = gen_push_body(); // Encode - int8_t res = _z_pull_encode(&wbf, z_msg._header, &e_pu); + int8_t res = _z_push_body_encode(&wbf, &e_da); assert(res == _Z_RES_OK); (void)(res); // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_msg_pull_t d_pu; - res = _z_pull_decode(&d_pu, &zbf, z_msg._header); + _z_push_body_t d_da = {0}; + uint8_t header = _z_zbuf_read(&zbf); + res = _z_push_body_decode(&d_da, &zbf, header); assert(res == _Z_RES_OK); - assert_eq_pull_message(&e_pu, &d_pu, z_msg._header); + assert_eq_push_body(&e_da, &d_da); // Free - _z_msg_clear_pull(&d_pu); - _z_msg_clear(&z_msg); + _z_push_body_clear(&d_da); + _z_push_body_clear(&e_da); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } -/*------------------ Query message ------------------*/ -_z_zenoh_message_t gen_query_message(void) { - _z_keyexpr_t key = gen_keyexpr(); - char *parameters = gen_str(gen_uint8() % 16); - _z_zint_t qid = gen_zint(); +/*------------------ Pull message ------------------*/ +_z_msg_pull_t gen_pull_message(void) { return (_z_msg_pull_t){._ext_source_info = _z_source_info_null()}; } - z_query_target_t target; - if (gen_bool()) { - uint8_t tgt[] = {Z_QUERY_TARGET_BEST_MATCHING, Z_QUERY_TARGET_ALL_COMPLETE, Z_QUERY_TARGET_ALL}; - target = tgt[gen_uint8() % (sizeof(tgt) / sizeof(uint8_t))]; - } else { - target = Z_QUERY_TARGET_BEST_MATCHING; - } +void assert_eq_pull_message(_z_msg_pull_t *left, _z_msg_pull_t *right) { + assert_eq_source_info(&left->_ext_source_info, &right->_ext_source_info); +} + +void pull_message(void) { + printf("\n>> Pull message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type e_pull_msg = gen_pull_message(); - uint8_t con[] = {Z_CONSOLIDATION_MODE_LATEST, Z_CONSOLIDATION_MODE_MONOTONIC, Z_CONSOLIDATION_MODE_NONE}; - z_consolidation_mode_t consolidation; - consolidation = con[gen_uint8() % (sizeof(con) / sizeof(uint8_t))]; + assert(_z_pull_encode(&wbf, &e_pull_msg) == _Z_RES_OK); - _z_value_t value; - if (gen_bool()) { - value = gen_value(); - } else { - value.encoding.prefix = Z_ENCODING_PREFIX_EMPTY; - value.encoding.suffix = _z_bytes_empty(); - value.payload = _z_bytes_empty(); - } + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + __auto_type header = _z_zbuf_read(&zbf); + + _z_msg_pull_t d_pull_msg; + assert(_z_pull_decode(&d_pull_msg, &zbf, header) == _Z_RES_OK); + assert_eq_pull_message(&e_pull_msg, &d_pull_msg); - return _z_msg_make_query(key, parameters, qid, target, consolidation, value); + _z_msg_pull_clear(&e_pull_msg); + _z_msg_pull_clear(&d_pull_msg); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); } -void assert_eq_query_message(_z_msg_query_t *left, _z_msg_query_t *right, uint8_t header) { - printf(" Consolidation ( %u:%u)", left->_consolidation, right->_consolidation); - assert(left->_consolidation == right->_consolidation); - printf("\n"); +_z_msg_query_t gen_query(void) { + return (_z_msg_query_t){ + ._ext_consolidation = (gen_uint8() % 4) - 1, + ._ext_info = gen_source_info(), + ._parameters = gen_bytes(16), + ._ext_value = gen_bool() ? gen_value() : _z_value_null(), + }; +} - if (_Z_HAS_FLAG(header, _Z_FLAG_Z_B) == true) { - printf(" "); - assert_eq_data_info(&left->_info, &right->_info); - printf("\n"); - printf(" "); - assert_eq_payload(&left->_payload, &right->_payload); - printf("\n"); - } +void assert_eq_query(const _z_msg_query_t *left, const _z_msg_query_t *right) { + assert(left->_ext_consolidation == right->_ext_consolidation); + assert_eq_source_info(&left->_ext_info, &right->_ext_info); + assert_eq_bytes(&left->_parameters, &right->_parameters); + assert_eq_value(&left->_ext_value, &right->_ext_value); } void query_message(void) { printf("\n>> Query message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_query(); + assert(_z_query_encode(&wbf, &expected) == _Z_RES_OK); + _z_msg_query_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + __auto_type header = _z_zbuf_read(&zbf); + __auto_type res = _z_query_decode(&decoded, &zbf, header); + assert(_Z_RES_OK == res); + assert_eq_query(&expected, &decoded); + _z_msg_query_clear(&decoded); + _z_msg_query_clear(&expected); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} - // Initialize - uint8_t e_hdr = 0; - _z_zenoh_message_t z_msg = gen_query_message(); - assert(_Z_MID(z_msg._header) == _Z_MID_Z_QUERY); +_z_msg_err_t gen_err(void) { + return (_z_msg_err_t){ + ._code = gen_uint16(), + ._is_infrastructure = gen_bool(), + ._timestamp = gen_timestamp(), + ._ext_source_info = gen_bool() ? gen_source_info() : _z_source_info_null(), + ._ext_value = gen_bool() ? gen_value() : _z_value_null(), + }; +} + +void assert_eq_err(const _z_msg_err_t *left, const _z_msg_err_t *right) { + assert(left->_code == right->_code); + assert(left->_is_infrastructure == right->_is_infrastructure); + assert_eq_timestamp(&left->_timestamp, &right->_timestamp); + assert_eq_source_info(&left->_ext_source_info, &right->_ext_source_info); + assert_eq_value(&left->_ext_value, &right->_ext_value); +} + +void err_message(void) { + printf("\n>> Err message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_err(); + assert(_z_err_encode(&wbf, &expected) == _Z_RES_OK); + _z_msg_err_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + __auto_type header = _z_zbuf_read(&zbf); + assert(_Z_RES_OK == _z_err_decode(&decoded, &zbf, header)); + assert_eq_err(&expected, &decoded); + _z_msg_err_clear(&decoded); + _z_msg_err_clear(&expected); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} - _z_msg_query_t e_qy = z_msg._body._query; +_z_msg_ack_t gen_ack(void) { + return (_z_msg_ack_t){ + ._ext_source_info = gen_bool() ? gen_source_info() : _z_source_info_null(), + ._timestamp = gen_timestamp(), + }; +} - // Encode - int8_t res = _z_query_encode(&wbf, e_hdr, &e_qy); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_msg_query_t d_qy; - res = _z_query_decode(&d_qy, &zbf, e_hdr); - assert(res == _Z_RES_OK); - - assert_eq_query_message(&e_qy, &d_qy, e_hdr); - - // Free - _z_msg_query_clear(&d_qy); - _z_msg_clear(&z_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ Unit message ------------------*/ -_z_zenoh_message_t gen_unit_message(void) { - _Bool can_be_dropped = gen_bool(); - return _z_msg_make_unit(can_be_dropped); -} - -/*------------------ Zenoh message ------------------*/ -_z_zenoh_message_t gen_zenoh_message(void) { - _z_zenoh_message_t p_zm; - - uint8_t mids[] = {_Z_MID_Z_DATA, _Z_MID_Z_PULL, _Z_MID_Z_QUERY, _Z_MID_Z_UNIT}; - uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); - - switch (mids[i]) { - case _Z_MID_Z_DATA: - p_zm = gen_data_message(); - break; - case _Z_MID_Z_PULL: - p_zm = gen_pull_message(); - break; - case _Z_MID_Z_QUERY: - p_zm = gen_query_message(); - break; - case _Z_MID_Z_UNIT: - p_zm = gen_unit_message(); - break; - default: - assert(0); - break; - } - - if (gen_bool()) { - p_zm._reply_context = gen_reply_context(); - } else { - p_zm._reply_context = NULL; - } - - return p_zm; -} - -void assert_eq_zenoh_message(_z_zenoh_message_t *left, _z_zenoh_message_t *right) { - // Test message decorators - - if (left->_reply_context && right->_reply_context) { - printf(" "); - assert_eq_reply_context(left->_reply_context, right->_reply_context); - printf("\n"); - } else { - printf(" Reply Context: %p:%p\n", (void *)left->_reply_context, (void *)right->_reply_context); - assert(left->_reply_context == right->_reply_context); - } - - // Test message - printf(" Header (%x:%x)", left->_header, right->_header); - assert(left->_header == right->_header); - printf("\n"); - - switch (_Z_MID(left->_header)) { - case _Z_MID_Z_DATA: - assert_eq_data_message(&left->_body._data, &right->_body._data, left->_header); - break; - case _Z_MID_Z_PULL: - assert_eq_pull_message(&left->_body._pull, &right->_body._pull, left->_header); - break; - case _Z_MID_Z_QUERY: - assert_eq_query_message(&left->_body._query, &right->_body._query, left->_header); - break; - case _Z_MID_Z_UNIT: - // Do nothing. Unit messages have no body - break; - default: - assert(0); - break; - } -} - -void zenoh_message(void) { - printf("\n>> Zenoh message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_zenoh_message_t e_zm = gen_zenoh_message(); - - printf(" - "); - switch (_Z_MID(e_zm._header)) { - case _Z_MID_Z_DATA: - printf("Data message"); - break; - case _Z_MID_Z_PULL: - printf("Pull message"); - break; - case _Z_MID_Z_QUERY: - printf("Query message"); - break; - case _Z_MID_Z_UNIT: - printf("Unit message"); - break; - default: - assert(0); - break; - } - if (e_zm._reply_context) { - printf(" Reply Context\n"); - print_reply_context(e_zm._reply_context); - printf("\n"); - } - printf("\n"); - - // Encode - int8_t res = _z_zenoh_message_encode(&wbf, &e_zm); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_zenoh_message_t d_zm; - res = _z_zenoh_message_decode(&d_zm, &zbf); - assert(res == _Z_RES_OK); - - assert_eq_zenoh_message(&e_zm, &d_zm); - - // Free - _z_msg_clear(&e_zm); - _z_msg_clear(&d_zm); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*=============================*/ -/* Network Messages */ -/*=============================*/ -/*------------------ Declare message ------------------*/ -_z_network_message_t gen_declare_message(void) { - _z_declaration_t declaration = gen_declaration(); - - return _z_n_msg_make_declare(declaration); -} - -void assert_eq_declare_message(_z_n_msg_declare_t *left, _z_n_msg_declare_t *right) { - printf(" "); - assert_eq_declaration(&left->_declaration, &right->_declaration); - printf("\n"); -} - -void declare_message(void) { - printf("\n>> Declare message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_network_message_t n_msg = gen_declare_message(); - assert(_Z_MID(n_msg._header) == _Z_MID_N_DECLARE); - - _z_n_msg_declare_t e_dcl = n_msg._body._declare; - - // Encode - int8_t res = _z_declare_encode(&wbf, &e_dcl); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_n_msg_declare_t d_dcl; - res = _z_declare_decode(&d_dcl, &zbf); - assert(res == _Z_RES_OK); - - assert_eq_declare_message(&e_dcl, &d_dcl); - - // Free - _z_n_msg_declare_clear(&d_dcl); - _z_n_msg_clear(&n_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ Network message ------------------*/ -_z_network_message_t gen_network_message(void) { - _z_network_message_t p_nm; - - uint8_t mids[] = {_Z_MID_N_DECLARE}; - uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); - - switch (mids[i]) { - case _Z_MID_N_DECLARE: - p_nm = gen_declare_message(); - break; - default: - assert(0); - break; - } - - return p_nm; -} - -void assert_eq_network_message(_z_network_message_t *left, _z_network_message_t *right) { - // Test message - printf(" Header (%x:%x)", left->_header, right->_header); - assert(left->_header == right->_header); - printf("\n"); - - switch (_Z_MID(left->_header)) { - case _Z_MID_N_DECLARE: - assert_eq_declare_message(&left->_body._declare, &right->_body._declare); - break; - default: - assert(0); - break; - } -} - -void network_message(void) { - printf("\n>> Network message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_network_message_t e_nm = gen_network_message(); - - printf(" - "); - switch (_Z_MID(e_nm._header)) { - case _Z_MID_N_DECLARE: - printf("Declare message"); - break; - default: - assert(0); - break; - } - printf("\n"); - - // Encode - int8_t res = _z_network_message_encode(&wbf, &e_nm); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_network_message_t d_nm; - res = _z_network_message_decode(&d_nm, &zbf); - assert(res == _Z_RES_OK); - - assert_eq_network_message(&e_nm, &d_nm); - - // Free - _z_n_msg_clear(&e_nm); - _z_n_msg_clear(&d_nm); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*=============================*/ -/* Network Messages */ -/*=============================*/ -/*------------------ Push Message ------------------*/ -_z_network_message_t gen_push_message(void) { - _z_network_message_t n_msg; - - _z_keyexpr_t key = gen_keyexpr(); - _Bool is_remote = gen_bool(); - n_msg = _z_n_msg_make_push(key, (_z_push_body_t){}, is_remote); // TODO[protocol]: body - - return n_msg; -} - -void assert_eq_push_message(_z_n_msg_push_t *left, _z_n_msg_push_t *right, uint8_t header) { - printf(" "); - assert_eq_keyexpr(&left->_key, &right->_key, header); - printf("\n"); - - // TODO[protocol]: body -} - -void push_message(void) { - printf("\n>> Push message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_network_message_t n_msg = gen_push_message(); - assert(_Z_MID(n_msg._header) == _Z_MID_N_PUSH); - - _z_n_msg_push_t e_pu = n_msg._body._push; - - // Encode - int8_t res = _z_push_encode(&wbf, n_msg._header, &e_pu); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_n_msg_push_t d_pu; - res = _z_push_decode(&d_pu, &zbf, n_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_push_message(&e_pu, &d_pu, n_msg._header); - - // Free - _z_n_msg_push_clear(&d_pu); - _z_n_msg_clear(&n_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ Request Message ------------------*/ -_z_network_message_t gen_request_message(void) { - _z_network_message_t n_msg; - - _z_zint_t rid = gen_zint(); - _z_keyexpr_t key = gen_keyexpr(); - _Bool is_remote = gen_bool(); - n_msg = _z_n_msg_make_request(rid, key, (_z_request_body_t){}, is_remote); // TODO[protocol]: body - - return n_msg; -} - -void assert_eq_request_message(_z_n_msg_request_t *left, _z_n_msg_request_t *right, uint8_t header) { - printf(" "); - assert(left->_rid == right->_rid); - printf("\n"); - - printf(" "); - assert_eq_keyexpr(&left->_key, &right->_key, header); - printf("\n"); - - // TODO[protocol]: body -} - -void request_message(void) { - printf("\n>> Request message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_network_message_t n_msg = gen_request_message(); - assert(_Z_MID(n_msg._header) == _Z_MID_N_REQUEST); - - _z_n_msg_request_t e_re = n_msg._body._request; - - // Encode - int8_t res = _z_request_encode(&wbf, n_msg._header, &e_re); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_n_msg_request_t d_re; - res = _z_request_decode(&d_re, &zbf, n_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_request_message(&e_re, &d_re, n_msg._header); - - // Free - _z_n_msg_request_clear(&d_re); - _z_n_msg_clear(&n_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ Response Message ------------------*/ -_z_network_message_t gen_response_message(void) { - _z_network_message_t n_msg; - - _z_zint_t rid = gen_zint(); - _z_keyexpr_t key = gen_keyexpr(); - _Bool is_remote = gen_bool(); - n_msg = _z_n_msg_make_response(rid, key, (_z_response_body_t){}, is_remote); // TODO[protocol]: body - - return n_msg; -} - -void assert_eq_response_message(_z_msg_reply_t *left, _z_msg_reply_t *right, uint8_t header) { - printf(" "); - assert(left->_rid == right->_rid); - printf("\n"); - - printf(" "); - assert_eq_keyexpr(&left->_key, &right->_key, header); - printf("\n"); - - // TODO[protocol]: body -} - -void response_message(void) { - printf("\n>> Response message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_network_message_t n_msg = gen_response_message(); - assert(_Z_MID(n_msg._header) == _Z_MID_N_RESPONSE); - - _z_msg_reply_t e_re = n_msg._body._response; - - // Encode - int8_t res = _z_response_encode(&wbf, n_msg._header, &e_re); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_msg_reply_t d_re; - res = _z_response_decode(&d_re, &zbf, n_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_response_message(&e_re, &d_re, n_msg._header); - - // Free - _z_msg_reply_clear(&d_re); - _z_n_msg_clear(&n_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ Response Final Message ------------------*/ -_z_network_message_t gen_response_final_message(void) { - _z_network_message_t n_msg; - - _z_zint_t rid = gen_zint(); - n_msg = _z_n_msg_make_response_final(rid); - - return n_msg; -} - -void assert_eq_response_final_message(_z_n_msg_response_final_t *left, _z_n_msg_response_final_t *right) { - printf(" "); - assert(left->_requestid == right->_requestid); - printf("\n"); - - // TODO[protocol]: body -} - -void response_final_message(void) { - printf("\n>> Response Final message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_network_message_t n_msg = gen_response_final_message(); - assert(_Z_MID(n_msg._header) == _Z_MID_N_RESPONSE_FINAL); - - _z_n_msg_response_final_t e_re = n_msg._body._response_f; - - // Encode - int8_t res = _z_response_final_encode(&wbf, n_msg._header, &e_re); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_n_msg_response_final_t d_re; - res = _z_response_final_decode(&d_re, &zbf, n_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_response_final_message(&e_re, &d_re); - - // Free - _z_n_msg_response_final_clear(&d_re); - _z_n_msg_clear(&n_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*=============================*/ -/* Scouting Messages */ -/*=============================*/ -/*------------------ Scout Message ------------------*/ -_z_scouting_message_t gen_scout_message(void) { - z_what_t what = gen_uint8() % 7; - _z_id_t zid = gen_zid(); - return _z_s_msg_make_scout(what, zid); -} - -void assert_eq_scout_message(_z_s_msg_scout_t *left, _z_s_msg_scout_t *right, uint8_t header) { - (void)(header); - printf(" Version (%u:%u)\n", left->_version, right->_version); - assert(left->_version == right->_version); - printf(" What (%u:%u)\n", left->_what, right->_what); - assert(left->_what == right->_what); - printf(" "); - assert(memcmp(left->_zid.id, right->_zid.id, 16) == 0); - printf("\n"); -} - -void scout_message(void) { - printf("\n>> Scout message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_scouting_message_t s_msg = gen_scout_message(); - _z_s_msg_scout_t e_sc = s_msg._body._scout; - - // Encode - int8_t res = _z_scout_encode(&wbf, s_msg._header, &e_sc); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_s_msg_scout_t d_sc; - res = _z_scout_decode(&d_sc, &zbf, s_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_scout_message(&e_sc, &d_sc, s_msg._header); - - // Free - _z_s_msg_clear_scout(&d_sc); - _z_s_msg_clear(&s_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ Hello Message ------------------*/ -_z_scouting_message_t gen_hello_message(void) { - z_whatami_t whatami = (gen_uint8() % 2) + 1; - _z_id_t zid = gen_zid(); - - _z_locator_array_t locators; - if (gen_bool() == true) - locators = gen_locator_array((gen_uint8() % 4) + 1); - else - locators = gen_locator_array(0); - - return _z_s_msg_make_hello(whatami, zid, locators); -} - -void assert_eq_hello_message(_z_s_msg_hello_t *left, _z_s_msg_hello_t *right, uint8_t header) { - printf(" Version (%u:%u)", left->_version, right->_version); - assert(left->_version == right->_version); - printf("\n"); - - printf(" What (%u:%u)", left->_whatami, right->_whatami); - assert(left->_whatami == right->_whatami); - printf("\n"); - - printf(" "); - assert(memcmp(left->_zid.id, right->_zid.id, 16) == 0); - printf("\n"); - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_HELLO_L) == true) { - printf(" "); - assert_eq_locator_array(&left->_locators, &right->_locators); - printf("\n"); - } -} - -void hello_message(void) { - printf("\n>> Hello message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_scouting_message_t s_msg = gen_hello_message(); - assert(_Z_MID(s_msg._header) == _Z_MID_HELLO); - - _z_s_msg_hello_t e_he = s_msg._body._hello; - - // Encode - int8_t res = _z_hello_encode(&wbf, s_msg._header, &e_he); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_s_msg_hello_t d_he; - res = _z_hello_decode(&d_he, &zbf, s_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_hello_message(&e_he, &d_he, s_msg._header); - - // Free - _z_s_msg_clear_hello(&d_he); - _z_s_msg_clear(&s_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*=============================*/ -/* Transport Messages */ -/*=============================*/ -/*------------------ Join Message ------------------*/ -_z_transport_message_t gen_join_message(void) { - z_whatami_t whatami = (gen_uint8() % 2) + 1; - _z_id_t zid = gen_zid(); - _z_zint_t lease = gen_bool() ? gen_zint() * 1000 : gen_zint(); - - _z_conduit_sn_list_t next_sn; - if (gen_bool()) { - for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { - next_sn._val._qos[i]._reliable = gen_zint(); - next_sn._val._qos[i]._best_effort = gen_zint(); - } - } else { - next_sn._val._plain._reliable = gen_zint(); - next_sn._val._plain._best_effort = gen_zint(); - } - - _z_transport_message_t t_msg = _z_t_msg_make_join(whatami, lease, zid, next_sn); - - if (gen_bool()) { - t_msg._body._join._batch_size = gen_uint16(); - } - - if (gen_bool()) { - t_msg._body._join._seq_num_res = gen_uint8() % 4; - } - - if (gen_bool()) { - t_msg._body._join._req_id_res = gen_uint8() % 4; - } - - return t_msg; -} - -void assert_eq_join_message(_z_t_msg_join_t *left, _z_t_msg_join_t *right, uint8_t header) { - printf(" Version (%u:%u)", left->_version, right->_version); - assert(left->_version == right->_version); - printf("\n"); - - printf(" WhatAmI (%u:%u)", left->_whatami, right->_whatami); - assert(left->_whatami == right->_whatami); - printf("\n"); - - printf(" "); - assert(memcmp(left->_zid.id, right->_zid.id, 16) == 0); - printf("\n"); - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S) == true) { - printf(" SN Resolution (%hhu:%hhu)", left->_seq_num_res, right->_seq_num_res); - assert(left->_seq_num_res == right->_seq_num_res); - printf("\n"); - - printf(" Request ID Resolution (%hhu:%hhu)", left->_req_id_res, right->_req_id_res); - assert(left->_req_id_res == right->_req_id_res); - printf("\n"); - - printf(" Batch Size (%hu:%hu)", left->_batch_size, right->_batch_size); - assert(left->_batch_size == right->_batch_size); - printf("\n"); - } - - printf(" Lease (%zu:%zu)", left->_lease, right->_lease); - assert(left->_lease == right->_lease); - printf("\n"); - - printf(" Next SNs: "); - // TODO[protocol] - // if (_Z_HAS_FLAG(left->_options, _Z_OPT_JOIN_QOS) == true) { - // assert(left->_next_sn._is_qos == true); - // assert(right->_next_sn._is_qos == true); - - // for (uint8_t i = 0; i < Z_PRIORITIES_NUM; i++) { - // printf("R:%zu:%zu ", left->_next_sn._val._qos[i]._reliable, right->_next_sn._val._qos[i]._reliable); - // assert(left->_next_sn._val._qos[i]._reliable == right->_next_sn._val._qos[i]._reliable); - // printf("B:%zu:%zu ", left->_next_sn._val._qos[i]._best_effort, - // right->_next_sn._val._qos[i]._best_effort); assert(left->_next_sn._val._qos[i]._best_effort == - // right->_next_sn._val._qos[i]._best_effort); - // } - // printf("\n"); - // } else { - // assert(left->_next_sn._is_qos == false); - // assert(right->_next_sn._is_qos == false); - - printf("R: %zu:%zu", left->_next_sn._val._plain._reliable, right->_next_sn._val._plain._reliable); - assert(left->_next_sn._val._plain._reliable == right->_next_sn._val._plain._reliable); - printf("B: %zu:%zu", left->_next_sn._val._plain._best_effort, right->_next_sn._val._plain._best_effort); - assert(left->_next_sn._val._plain._best_effort == right->_next_sn._val._plain._best_effort); - printf("\n"); - // } -} - -void join_message(void) { - printf("\n>> Join message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_transport_message_t t_msg = gen_join_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_T_JOIN); - - _z_t_msg_join_t e_it = t_msg._body._join; - - // Encode - int8_t res = _z_join_encode(&wbf, t_msg._header, &t_msg._body._join); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_t_msg_join_t d_it; - res = _z_join_decode(&d_it, &zbf, t_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_join_message(&e_it, &d_it, t_msg._header); - - // Free - _z_t_msg_clear_join(&d_it); - _z_t_msg_clear(&t_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ Init Message ------------------*/ -_z_transport_message_t gen_init_message(void) { - z_whatami_t whatami = (gen_uint8() % 2) + 1; - _z_id_t zid = gen_zid(); - - _z_transport_message_t t_msg; - if (gen_bool()) { - t_msg = _z_t_msg_make_init_syn(whatami, zid); - } else { - _z_bytes_t cookie = gen_bytes(64); - t_msg = _z_t_msg_make_init_ack(whatami, zid, cookie); - } - - if (gen_bool()) { - t_msg._body._init._batch_size = gen_uint16(); - } - - if (gen_bool()) { - t_msg._body._init._seq_num_res = gen_uint8() % 4; - } - - if (gen_bool()) { - t_msg._body._init._req_id_res = gen_uint8() % 4; - } - - return t_msg; -} - -void assert_eq_init_message(_z_t_msg_init_t *left, _z_t_msg_init_t *right, uint8_t header) { - printf(" Version (%u:%u)", left->_version, right->_version); - assert(left->_version == right->_version); - printf("\n"); - - printf(" WhatAmI (%u:%u)", left->_whatami, right->_whatami); - assert(left->_whatami == right->_whatami); - printf("\n"); - - printf(" "); - assert(memcmp(left->_zid.id, right->_zid.id, 16) == 0); - printf("\n"); - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_S) == true) { - printf(" SN Resolution (%hhu:%hhu)", left->_seq_num_res, right->_seq_num_res); - assert(left->_seq_num_res == right->_seq_num_res); - printf("\n"); - - printf(" Request ID Resolution (%hhu:%hhu)", left->_req_id_res, right->_req_id_res); - assert(left->_req_id_res == right->_req_id_res); - printf("\n"); - - printf(" Batch Size (%hu:%hu)", left->_batch_size, right->_batch_size); - assert(left->_batch_size == right->_batch_size); - printf("\n"); - } - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_A) == true) { - printf(" "); - assert_eq_uint8_array(&left->_cookie, &right->_cookie); - printf("\n"); - } -} - -void init_message(void) { - printf("\n>> Init message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_transport_message_t t_msg = gen_init_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_T_INIT); - - _z_t_msg_init_t e_it = t_msg._body._init; - - // Encode - int8_t res = _z_init_encode(&wbf, t_msg._header, &e_it); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_t_msg_init_t d_it; - res = _z_init_decode(&d_it, &zbf, t_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_init_message(&e_it, &d_it, t_msg._header); - - // Free - _z_t_msg_clear_init(&d_it); - _z_t_msg_clear(&t_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ Open Message ------------------*/ -_z_transport_message_t gen_open_message(void) { - _z_zint_t lease = gen_bool() ? gen_zint() * 1000 : gen_zint(); - _z_zint_t initial_sn = gen_zint(); - - if (gen_bool()) { - _z_bytes_t cookie = gen_bytes(64); - return _z_t_msg_make_open_syn(lease, initial_sn, cookie); - } else { - return _z_t_msg_make_open_ack(lease, initial_sn); - } -} - -void assert_eq_open_message(_z_t_msg_open_t *left, _z_t_msg_open_t *right, uint8_t header) { - printf(" Lease (%zu:%zu)", left->_lease, right->_lease); - assert(left->_lease == right->_lease); - printf("\n"); - - printf(" Initial SN (%zu:%zu)", left->_initial_sn, right->_initial_sn); - assert(left->_initial_sn == right->_initial_sn); - printf("\n"); - - if (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_A) == false) { - printf(" "); - assert_eq_uint8_array(&left->_cookie, &right->_cookie); - printf("\n"); - } -} - -void open_message(void) { - printf("\n>> Open message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_transport_message_t t_msg = gen_open_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_T_OPEN); - - _z_t_msg_open_t e_op = t_msg._body._open; - - // Encode - int8_t res = _z_open_encode(&wbf, t_msg._header, &e_op); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_t_msg_open_t d_op; - res = _z_open_decode(&d_op, &zbf, t_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_open_message(&e_op, &d_op, t_msg._header); - - // Free - _z_t_msg_clear_open(&d_op); - _z_t_msg_clear(&t_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ Close Message ------------------*/ -_z_transport_message_t gen_close_message(void) { - uint8_t reason = gen_uint8(); - _Bool link_only = gen_bool(); - - return _z_t_msg_make_close(reason, link_only); -} - -void assert_eq_close_message(_z_t_msg_close_t *left, _z_t_msg_close_t *right, uint8_t header) { - (void)(header); - - printf(" Reason (%u:%u)", left->_reason, right->_reason); - assert(left->_reason == right->_reason); - printf("\n"); -} - -void close_message(void) { - printf("\n>> Close message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_transport_message_t t_msg = gen_close_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_T_CLOSE); - - _z_t_msg_close_t e_cl = t_msg._body._close; - - // Encode - int8_t res = _z_close_encode(&wbf, t_msg._header, &e_cl); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_t_msg_close_t d_cl; - res = _z_close_decode(&d_cl, &zbf, t_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_close_message(&e_cl, &d_cl, t_msg._header); - - // Free - _z_t_msg_clear_close(&d_cl); - _z_t_msg_clear(&t_msg); - _z_zbuf_clear(&zbf); - _z_wbuf_clear(&wbf); -} - -/*------------------ KeepAlive Message ------------------*/ -_z_transport_message_t gen_keep_alive_message(void) { return _z_t_msg_make_keep_alive(); } - -void assert_eq_keep_alive_message(_z_t_msg_keep_alive_t *left, _z_t_msg_keep_alive_t *right, uint8_t header) { - (void)(left); - (void)(right); - (void)(header); +void assert_eq_ack(const _z_msg_ack_t *left, const _z_msg_ack_t *right) { + assert_eq_timestamp(&left->_timestamp, &right->_timestamp); + assert_eq_source_info(&left->_ext_source_info, &right->_ext_source_info); } -void keep_alive_message(void) { - printf("\n>> KeepAlive message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_transport_message_t t_msg = gen_keep_alive_message(); - _z_t_msg_keep_alive_t e_ka = t_msg._body._keep_alive; - assert(_Z_MID(t_msg._header) == _Z_MID_T_KEEP_ALIVE); - - // Encode - int8_t res = _z_keep_alive_encode(&wbf, t_msg._header, &e_ka); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_t_msg_keep_alive_t d_ka; - res = _z_keep_alive_decode(&d_ka, &zbf, t_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_keep_alive_message(&e_ka, &d_ka, t_msg._header); - - // Free - _z_t_msg_clear_keep_alive(&d_ka); - _z_t_msg_clear(&t_msg); +void ack_message(void) { + printf("\n>> Ack message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_ack(); + assert(_z_ack_encode(&wbf, &expected) == _Z_RES_OK); + _z_msg_ack_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + __auto_type header = _z_zbuf_read(&zbf); + assert(_Z_RES_OK == _z_ack_decode(&decoded, &zbf, header)); + assert_eq_ack(&expected, &decoded); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } -/*------------------ Frame Message ------------------*/ -_z_transport_message_t gen_frame_message(void) { - _z_zint_t sn = gen_zint(); - _Bool is_reliable = gen_bool(); - - _z_network_message_vec_t messages; - _z_zint_t num = (gen_zint() % 4) + 1; - messages = _z_network_message_vec_make(num); - for (_z_zint_t i = 0; i < num; i++) { - _z_network_message_t *p_zm = (_z_network_message_t *)z_malloc(sizeof(_z_network_message_t)); - *p_zm = gen_network_message(); - _z_network_message_vec_append(&messages, p_zm); - } - - return _z_t_msg_make_frame(sn, messages, is_reliable); -} - -void assert_eq_frame_message(_z_t_msg_frame_t *left, _z_t_msg_frame_t *right, uint8_t header) { - (void)(header); - - printf(" SN (%zu:%zu)", left->_sn, right->_sn); - assert(left->_sn == right->_sn); - printf("\n"); - - size_t l_len = _z_vec_len(&left->_messages); - size_t r_len = _z_vec_len(&right->_messages); - printf(" Lenght (%zu:%zu)", l_len, r_len); - assert(l_len == r_len); - - for (size_t i = 0; i < l_len; i++) - assert_eq_network_message((_z_network_message_t *)_z_vec_get(&left->_messages, i), - (_z_network_message_t *)_z_vec_get(&right->_messages, i)); -} - -void frame_message(void) { - printf("\n>> Frame message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_transport_message_t t_msg = gen_frame_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_T_FRAME); - - _z_t_msg_frame_t e_fr = t_msg._body._frame; - - // Encode - int8_t res = _z_frame_encode(&wbf, t_msg._header, &e_fr); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_t_msg_frame_t d_fr; - res = _z_frame_decode(&d_fr, &zbf, t_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_frame_message(&e_fr, &d_fr, t_msg._header); - - // Frame - _z_t_msg_clear_frame(&d_fr); - _z_t_msg_clear(&t_msg); +_z_msg_reply_t gen_reply(void) { + return (_z_msg_reply_t){ + ._ext_source_info = gen_bool() ? gen_source_info() : _z_source_info_null(), + ._timestamp = gen_timestamp(), + ._ext_consolidation = (gen_uint8() % 4) - 1, + ._value = gen_value(), + }; +} + +void assert_eq_reply(const _z_msg_reply_t *left, const _z_msg_reply_t *right) { + assert_eq_timestamp(&left->_timestamp, &right->_timestamp); + assert_eq_source_info(&left->_ext_source_info, &right->_ext_source_info); + assert(left->_ext_consolidation == right->_ext_consolidation); + assert_eq_value(&left->_value, &right->_value); +} + +void reply_message(void) { + printf("\n>> Reply message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_reply(); + assert(_z_reply_encode(&wbf, &expected) == _Z_RES_OK); + _z_msg_reply_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + __auto_type header = _z_zbuf_read(&zbf); + assert(_Z_RES_OK == _z_reply_decode(&decoded, &zbf, header)); + assert_eq_reply(&expected, &decoded); + _z_msg_reply_clear(&decoded); + _z_msg_reply_clear(&expected); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } -/*------------------ Fragment Message ------------------*/ -_z_transport_message_t gen_fragment_message(void) { - _z_zint_t sn = gen_zint(); - _Bool is_reliable = gen_bool(); - _Bool is_last = gen_bool(); - _z_bytes_t payload = gen_bytes(gen_uint16() % 60000); // 60000 is just to make sure that we are not generating a - // payload bigger than what the fragment can hold - return _z_t_msg_make_fragment(sn, payload, is_reliable, is_last); +_z_n_msg_push_t gen_push(void) { + return (_z_n_msg_push_t){ + ._body = gen_push_body(), + ._key = gen_keyexpr(), + ._qos = _z_n_qos_make(gen_bool(), gen_bool(), gen_uint8() % 8), + ._timestamp = gen_timestamp(), + }; } -void assert_eq_fragment_message(_z_t_msg_fragment_t *left, _z_t_msg_fragment_t *right, uint8_t header) { - (void)(header); - - printf(" SN (%zu:%zu)", left->_sn, right->_sn); - assert(left->_sn == right->_sn); - printf("\n"); - - assert_eq_payload(&left->_payload, &right->_payload); +void assert_eq_push(const _z_n_msg_push_t *left, const _z_n_msg_push_t *right) { + assert_eq_timestamp(&left->_timestamp, &right->_timestamp); + assert_eq_keyexpr(&left->_key, &right->_key); + assert(left->_qos._val == right->_qos._val); + assert_eq_push_body(&left->_body, &right->_body); } -void fragment_message(void) { - printf("\n>> Fragment message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_transport_message_t t_msg = gen_fragment_message(); - assert(_Z_MID(t_msg._header) == _Z_MID_T_FRAGMENT); - - _z_t_msg_fragment_t e_fr = t_msg._body._fragment; - - // Encode - int8_t res = _z_fragment_encode(&wbf, t_msg._header, &e_fr); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_t_msg_fragment_t d_fr; - res = _z_fragment_decode(&d_fr, &zbf, t_msg._header); - assert(res == _Z_RES_OK); - - assert_eq_fragment_message(&e_fr, &d_fr, t_msg._header); - - // Frame - _z_t_msg_clear_fragment(&d_fr); - _z_t_msg_clear(&t_msg); +void push_message(void) { + printf("\n>> Push message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_push(); + assert(_z_push_encode(&wbf, &expected) == _Z_RES_OK); + _z_n_msg_push_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + __auto_type header = _z_zbuf_read(&zbf); + assert(_Z_RES_OK == _z_push_decode(&decoded, &zbf, header)); + assert_eq_push(&expected, &decoded); + _z_n_msg_push_clear(&decoded); + _z_n_msg_push_clear(&expected); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } -/*------------------ Transport Message ------------------*/ -_z_transport_message_t gen_transport_message(void) { - _z_transport_message_t e_tm; - - uint8_t mids[] = {_Z_MID_T_JOIN, _Z_MID_T_INIT, _Z_MID_T_OPEN, _Z_MID_T_CLOSE, - _Z_MID_T_KEEP_ALIVE, _Z_MID_T_FRAME, _Z_MID_T_FRAGMENT}; - - uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); - switch (mids[i]) { - case _Z_MID_T_JOIN: - e_tm = gen_join_message(); - break; - case _Z_MID_T_INIT: - e_tm = gen_init_message(); - break; - case _Z_MID_T_OPEN: - e_tm = gen_open_message(); - break; - case _Z_MID_T_CLOSE: - e_tm = gen_close_message(); - break; - case _Z_MID_T_KEEP_ALIVE: - e_tm = gen_keep_alive_message(); - break; - case _Z_MID_T_FRAME: - e_tm = gen_frame_message(); - break; - case _Z_MID_T_FRAGMENT: - e_tm = gen_fragment_message(); - break; - default: - assert(0); - break; +_z_n_msg_request_t gen_request(void) { + _z_n_msg_request_t request = { + ._rid = gen_uint64(), + ._key = gen_keyexpr(), + ._ext_qos = gen_bool() ? _z_n_qos_make(gen_bool(), gen_bool(), gen_uint8() % 8) : _Z_N_QOS_DEFAULT, + ._ext_timestamp = gen_bool() ? gen_timestamp() : _z_timestamp_null(), + ._ext_target = gen_uint8() % 3, + ._ext_budget = gen_bool() ? gen_uint64() : 0, + ._ext_timeout_ms = gen_bool() ? gen_uint64() : 0, + }; + switch (gen_uint8() % 4) { + case 0: { + request._tag = _Z_REQUEST_QUERY; + request._body._query = gen_query(); + } break; + case 1: { + request._tag = _Z_REQUEST_PULL; + request._body._pull = + (_z_msg_pull_t){._ext_source_info = gen_bool() ? gen_source_info() : _z_source_info_null()}; + } break; + default: { + _z_push_body_t body = gen_push_body(); + if (body._is_put) { + request._tag = _Z_REQUEST_PUT; + request._body._put = body._body._put; + } else { + request._tag = _Z_REQUEST_DEL; + request._body._del = body._body._del; + } + } } - - return e_tm; + return request; } -void assert_eq_transport_message(_z_transport_message_t *left, _z_transport_message_t *right) { - // FIXME[protocol]: This is here to set the extensions flags that is only known at encoding time - // Test message - printf(" Header (%x:%x)", left->_header, right->_header); - assert(left->_header == right->_header); - printf("\n"); - - switch (_Z_MID(left->_header)) { - case _Z_MID_T_JOIN: - assert_eq_join_message(&left->_body._join, &right->_body._join, left->_header); - break; - case _Z_MID_T_INIT: - assert_eq_init_message(&left->_body._init, &right->_body._init, left->_header); - break; - case _Z_MID_T_OPEN: - assert_eq_open_message(&left->_body._open, &right->_body._open, left->_header); - break; - case _Z_MID_T_CLOSE: - assert_eq_close_message(&left->_body._close, &right->_body._close, left->_header); - break; - case _Z_MID_T_KEEP_ALIVE: - assert_eq_keep_alive_message(&left->_body._keep_alive, &right->_body._keep_alive, left->_header); - break; - case _Z_MID_T_FRAME: - assert_eq_frame_message(&left->_body._frame, &right->_body._frame, left->_header); - break; - case _Z_MID_T_FRAGMENT: - assert_eq_fragment_message(&left->_body._fragment, &right->_body._fragment, left->_header); - break; - default: - assert(0); - break; +void assert_eq_request(const _z_n_msg_request_t *left, const _z_n_msg_request_t *right) { + assert(left->_rid == right->_rid); + assert_eq_keyexpr(&left->_key, &right->_key); + assert(left->_ext_qos._val == right->_ext_qos._val); + assert_eq_timestamp(&left->_ext_timestamp, &right->_ext_timestamp); + assert(left->_ext_target == right->_ext_target); + assert(left->_ext_budget == right->_ext_budget); + assert(left->_ext_timeout_ms == right->_ext_timeout_ms); + assert(left->_tag == right->_tag); + switch (left->_tag) { + case _Z_REQUEST_QUERY: { + assert_eq_query(&left->_body._query, &right->_body._query); + } break; + case _Z_REQUEST_PUT: { + assert_eq_push_body(&(_z_push_body_t){._is_put = true, ._body._put = left->_body._put}, + &(_z_push_body_t){._is_put = true, ._body._put = right->_body._put}); + } break; + case _Z_REQUEST_DEL: { + assert_eq_push_body(&(_z_push_body_t){._is_put = false, ._body._del = left->_body._del}, + &(_z_push_body_t){._is_put = false, ._body._del = right->_body._del}); + } break; + case _Z_REQUEST_PULL: { + assert_eq_source_info(&left->_body._pull._ext_source_info, &right->_body._pull._ext_source_info); + } break; } } -void transport_message(void) { - printf("\n>> Transport message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_transport_message_t e_tm = gen_transport_message(); - printf(" - "); - print_transport_message_type(e_tm._header); - printf("\n"); - - // Encode - int8_t res = _z_transport_message_encode(&wbf, &e_tm); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_transport_message_t d_sm; - res = _z_transport_message_decode(&d_sm, &zbf); - assert(res == _Z_RES_OK); - - assert_eq_transport_message(&e_tm, &d_sm); - - // Free - _z_t_msg_clear(&e_tm); - _z_t_msg_clear(&d_sm); +void request_message(void) { + printf("\n>> Request message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_request(); + assert(_z_request_encode(&wbf, &expected) == _Z_RES_OK); + _z_n_msg_request_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + __auto_type header = _z_zbuf_read(&zbf); + assert(_Z_RES_OK == _z_request_decode(&decoded, &zbf, header)); + assert_eq_request(&expected, &decoded); + _z_n_msg_request_clear(&decoded); + _z_n_msg_request_clear(&expected); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } -/*------------------ Scouting Message ------------------*/ -_z_scouting_message_t gen_scouting_message(void) { - _z_scouting_message_t e_sm; - - uint8_t mids[] = {_Z_MID_SCOUT, _Z_MID_HELLO}; - - uint8_t i = gen_uint8() % (sizeof(mids) / sizeof(uint8_t)); - switch (mids[i]) { - case _Z_MID_SCOUT: - e_sm = gen_scout_message(); - break; - case _Z_MID_HELLO: - e_sm = gen_hello_message(); - break; - default: - assert(0); - break; +_z_n_msg_response_t gen_response(void) { + _z_n_msg_response_t ret = { + ._key = gen_keyexpr(), + ._request_id = gen_uint64(), + ._ext_qos = _z_n_qos_make(gen_bool(), gen_bool(), gen_uint8() % 8), + ._ext_timestamp = gen_bool() ? gen_timestamp() : _z_timestamp_null(), + ._ext_responder = {._eid = gen_uint16(), ._zid = gen_zid()}, + }; + switch (gen_uint() % 5) { + case 0: { + ret._tag = _Z_RESPONSE_BODY_ACK; + ret._body._ack = gen_ack(); + } break; + case 1: { + ret._tag = _Z_RESPONSE_BODY_ERR; + ret._body._err = gen_err(); + } break; + case 2: { + ret._tag = _Z_RESPONSE_BODY_ACK; + ret._body._reply = gen_reply(); + } break; + default: { + __auto_type body = gen_push_body(); + if (body._is_put) { + ret._tag = _Z_RESPONSE_BODY_PUT; + ret._body._put = body._body._put; + } else { + ret._tag = _Z_RESPONSE_BODY_DEL; + ret._body._del = body._body._del; + } + } break; } - - return e_sm; + return ret; } -void assert_eq_scouting_message(_z_scouting_message_t *left, _z_scouting_message_t *right) { - // FIXME[protocol]: This is here to set the extensions flags that is only known at encoding time - // if (_z_msg_ext_vec_len(&left->_extensions) > (size_t)0) { - // left->_header |= _Z_FLAG_T_Z; - // } - - // Test message - printf(" Header (%x:%x)", left->_header, right->_header); - assert(left->_header == right->_header); - printf("\n"); - - switch (_Z_MID(left->_header)) { - case _Z_MID_SCOUT: - assert_eq_scout_message(&left->_body._scout, &right->_body._scout, left->_header); - break; - case _Z_MID_HELLO: - assert_eq_hello_message(&left->_body._hello, &right->_body._hello, left->_header); - break; - default: - assert(0); - break; +void assert_eq_response(const _z_n_msg_response_t *left, const _z_n_msg_response_t *right) { + assert_eq_keyexpr(&left->_key, &right->_key); + assert_eq_timestamp(&left->_ext_timestamp, &right->_ext_timestamp); + assert(left->_request_id == right->_request_id); + assert(left->_ext_qos._val == right->_ext_qos._val); + assert(left->_ext_responder._eid == right->_ext_responder._eid); + assert(memcmp(left->_ext_responder._zid.id, right->_ext_responder._zid.id, 16) == 0); + assert(left->_tag == right->_tag); + switch (left->_tag) { + case _Z_RESPONSE_BODY_REPLY: { + assert_eq_reply(&left->_body._reply, &right->_body._reply); + } break; + case _Z_RESPONSE_BODY_ERR: { + assert_eq_err(&left->_body._err, &right->_body._err); + } break; + case _Z_RESPONSE_BODY_ACK: { + assert_eq_ack(&left->_body._ack, &right->_body._ack); + } break; + case _Z_RESPONSE_BODY_PUT: { + assert_eq_push_body(&(_z_push_body_t){._is_put = true, ._body._put = left->_body._put}, + &(_z_push_body_t){._is_put = true, ._body._put = right->_body._put}); + } break; + case _Z_RESPONSE_BODY_DEL: { + assert_eq_push_body(&(_z_push_body_t){._is_put = false, ._body._del = left->_body._del}, + &(_z_push_body_t){._is_put = false, ._body._del = right->_body._del}); + } break; } - - // size_t left_n_ext = _z_msg_ext_vec_len(&left->_extensions); - // size_t right_n_ext = _z_msg_ext_vec_len(&right->_extensions); - // printf(" # of extensions (%zu:%zu)", left_n_ext, right_n_ext); - // assert(left_n_ext == right_n_ext); - // for (size_t i = 0; i < left_n_ext; i++) { - // assert_eq_message_extension(_z_msg_ext_vec_get(&left->_extensions, i), - // _z_msg_ext_vec_get(&right->_extensions, i)); - // } } -void scouting_message(void) { - printf("\n>> Scouting message\n"); - _z_wbuf_t wbf = gen_wbuf(65535); - - // Initialize - _z_scouting_message_t e_tm = gen_scouting_message(); - printf(" - "); - print_scouting_message_type(e_tm._header); - printf("\n"); - - // Encode - int8_t res = _z_scouting_message_encode(&wbf, &e_tm); - assert(res == _Z_RES_OK); - (void)(res); - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_scouting_message_t d_sm; - res = _z_scouting_message_decode(&d_sm, &zbf); - assert(res == _Z_RES_OK); - - assert_eq_scouting_message(&e_tm, &d_sm); - - // Free - _z_s_msg_clear(&e_tm); - _z_s_msg_clear(&d_sm); +void response_message(void) { + printf("\n>> Response message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_response(); + assert(_z_response_encode(&wbf, &expected) == _Z_RES_OK); + _z_n_msg_response_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + __auto_type header = _z_zbuf_read(&zbf); + assert(_Z_RES_OK == _z_response_decode(&decoded, &zbf, header)); + assert_eq_response(&expected, &decoded); + _z_n_msg_response_clear(&decoded); + _z_n_msg_response_clear(&expected); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } -/*------------------ Batch ------------------*/ -void batch(void) { - printf("\n>> Batch\n"); - uint8_t bef_num = (gen_uint8() % 3); - uint8_t frm_num = 2 + (gen_uint8() % 3); - uint8_t aft_num = (gen_uint8() % 3); - uint8_t tot_num = bef_num + frm_num + aft_num; - _z_wbuf_t wbf = gen_wbuf(tot_num * 1024); - - // Initialize - _z_transport_message_t *e_tm = (_z_transport_message_t *)z_malloc(tot_num * sizeof(_z_transport_message_t)); - for (uint8_t i = 0; i < bef_num; i++) { - // Initialize random transport message - e_tm[i] = gen_transport_message(); - // Encode - int8_t res = _z_transport_message_encode(&wbf, &e_tm[i]); - assert(res == _Z_RES_OK); - (void)(res); - } - for (uint8_t i = bef_num; i < bef_num + frm_num; i++) { - // Initialize a frame message - e_tm[i] = gen_frame_message(); - // Encode - int8_t res = _z_transport_message_encode(&wbf, &e_tm[i]); - assert(res == _Z_RES_OK); - (void)(res); - } - for (uint8_t i = bef_num + frm_num; i < bef_num + frm_num + aft_num; i++) { - // Initialize random transport message - e_tm[i] = gen_transport_message(); - // Encode - int8_t res = _z_transport_message_encode(&wbf, &e_tm[i]); - assert(res == _Z_RES_OK); - (void)(res); - } - - // Decode - _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - for (uint8_t i = 0; i < tot_num; i++) { - _z_transport_message_t d_sm; - int8_t res = _z_transport_message_decode(&d_sm, &zbf); - assert(res == _Z_RES_OK); - - printf(" - "); - print_transport_message_type(d_sm._header); - printf("\n"); - assert_eq_transport_message(&e_tm[i], &d_sm); - - // Free - _z_t_msg_clear(&e_tm[i]); - _z_t_msg_clear(&d_sm); - } - - z_free(e_tm); +_z_n_msg_response_final_t gen_response_final(void) { return (_z_n_msg_response_final_t){._request_id = gen_uint64()}; } +void assert_eq_response_final(const _z_n_msg_response_final_t *left, const _z_n_msg_response_final_t *right) { + assert(left->_request_id == right->_request_id); +} +void response_final_message(void) { + printf("\n>> Request Final message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_response_final(); + assert(_z_response_final_encode(&wbf, &expected) == _Z_RES_OK); + _z_n_msg_response_final_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + __auto_type header = _z_zbuf_read(&zbf); + assert(_Z_RES_OK == _z_response_final_decode(&decoded, &zbf, header)); + assert_eq_response_final(&expected, &decoded); + _z_n_msg_response_final_clear(&decoded); + _z_n_msg_response_final_clear(&expected); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } -/*------------------ Fragmentation ------------------*/ -// _z_transport_message_t z_fragment_header(_Bool is_reliable, _Bool is_fragment, _Bool is_final, _z_zint_t sn) { -// // Create the frame session message that carries the zenoh message -// _z_transport_message_t t_msg; -// t_msg._header = _Z_MID_T_FRAME; -// t_msg._body._frame._sn = sn; - -// if (is_reliable == true) { -// _Z_SET_FLAG(t_msg._header, _Z_FLAG_T_FRAGMENT_R); -// }; - -// if (is_fragment == true) { -// _Z_SET_FLAG(t_msg._header, _Z_FLAG_T_F); - -// if (is_final == true) { -// _Z_SET_FLAG(t_msg._header, _Z_FLAG_T_E); -// } - -// // Do not add the payload -// t_msg._body._frame._payload._fragment.len = 0; -// t_msg._body._frame._payload._fragment.start = NULL; -// } else { -// // Do not allocate the vector containing the messages -// t_msg._body._frame._payload._messages = _z_network_message_vec_make(0); -// } - -// return t_msg; -// } - -// void z_wbuf_prepare(_z_wbuf_t *wbf) { -// // Clear the buffer for serialization -// _z_wbuf_reset(wbf); - -// for (size_t i = 0; i < _z_wbuf_space_left(wbf); i++) _z_wbuf_put(wbf, 0xff, i); -// } - -// int8_t z_serialize_zenoh_fragment(_z_wbuf_t *dst, _z_wbuf_t *src, _Bool is_reliable, size_t sn) { -// // Assume first that this is not the final fragment -// _Bool is_final = false; -// do { -// // Mark the buffer for the writing operation -// size_t w_pos = _z_wbuf_get_wpos(dst); -// // Get the frame header -// _z_transport_message_t f_hdr = z_fragment_header(is_reliable, true, is_final, sn); -// // Encode the frame header -// int8_t res = _z_transport_message_encode(dst, &f_hdr); -// if (res == _Z_RES_OK) { -// size_t space_left = _z_wbuf_space_left(dst); -// size_t bytes_left = _z_wbuf_len(src); -// // Check if it is really the final fragment -// if ((is_final == false) && (bytes_left <= space_left)) { -// // Revert the buffer -// _z_wbuf_set_wpos(dst, w_pos); -// // It is really the finally fragment, reserialize the header -// is_final = true; -// continue; -// } -// // Write the fragment -// size_t to_copy = bytes_left <= space_left ? bytes_left : space_left; -// printf(" -Bytes left: %zu, Space left: %zu, Fragment size: %zu, Is final: %d\n", bytes_left, space_left, -// to_copy, is_final); -// return _z_wbuf_siphon(dst, src, to_copy); -// } else { -// return 0; -// } -// } while (1); -// } - -// void fragmentation(void) { -// printf("\n>> Fragmentation\n"); -// size_t len = 16; -// _z_wbuf_t wbf = _z_wbuf_make(len, false); -// _z_wbuf_t fbf = _z_wbuf_make(len, true); -// _z_wbuf_t dbf = _z_wbuf_make(0, true); - -// _z_zenoh_message_t e_zm; - -// do { -// _z_wbuf_reset(&fbf); -// // Generate and serialize the message -// e_zm = gen_zenoh_message(); -// _z_zenoh_message_encode(&fbf, &e_zm); -// // Check that the message actually requires fragmentation -// if (_z_wbuf_len(&fbf) <= len) -// _z_msg_clear(&e_zm); -// else -// break; -// } while (1); - -// // Fragment the message -// _Bool is_reliable = gen_bool(); -// // Fix the sn resoulution to 128 in such a way it requires only 1 byte for encoding -// size_t seq_num_res = 128; -// size_t sn = seq_num_res - 1; - -// printf(" - Message serialized\n"); -// print_wbuf(&fbf); - -// printf(" - Start fragmenting\n"); -// while (_z_wbuf_len(&fbf) > 0) { -// // Clear the buffer for serialization -// z_wbuf_prepare(&wbf); - -// // Get the fragment sequence number -// sn = (sn + 1) % seq_num_res; - -// size_t written = _z_wbuf_len(&fbf); -// int8_t res = z_serialize_zenoh_fragment(&wbf, &fbf, is_reliable, sn); -// assert(res == _Z_RES_OK); -// (void)(res); -// written = written - _z_wbuf_len(&fbf); - -// printf(" -Encoded Fragment: "); -// print_wbuf(&wbf); - -// // Decode the message -// _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - -// _z_transport_message_t r_sm; -// res = _z_transport_message_decode(&r_sm, &zbf); -// assert(res == _Z_RES_OK); - -// _z_bytes_t fragment = r_sm._body._frame._payload._fragment; -// printf(" -Decoded Fragment length: %zu\n", fragment.len); -// assert(fragment.len == written); - -// // Create an iosli for decoding -// _z_wbuf_write_bytes(&dbf, fragment.start, 0, fragment.len); - -// print_wbuf(&dbf); - -// // Free the read buffer -// _z_zbuf_clear(&zbf); -// } - -// printf(" - Start defragmenting\n"); -// print_wbuf(&dbf); -// _z_zbuf_t zbf = _z_wbuf_to_zbuf(&dbf); -// printf(" Defragmented: "); -// print_iosli(&zbf._ios); -// printf("\n"); - -// _z_zenoh_message_t d_zm; -// int8_t res = _z_zenoh_message_decode(&d_zm, &zbf); -// assert(res == _Z_RES_OK); - -// assert_eq_zenoh_message(&e_zm, &d_zm); -// _z_msg_clear(&e_zm); -// _z_msg_clear(&d_zm); - -// _z_wbuf_clear(&dbf); -// _z_wbuf_clear(&wbf); -// _z_wbuf_clear(&fbf); -// _z_zbuf_clear(&zbf); -// } - /*=============================*/ /* Main */ /*=============================*/ @@ -2841,57 +1462,51 @@ int main(void) { for (unsigned int i = 0; i < RUNS; i++) { printf("\n\n== RUN %u", i); - // Message extensions - unit_extension(); - zint_extension(); - zbuf_extension(); - message_extension(); - // Message fields payload_field(); timestamp_field(); - subinfo_field(); keyexpr_field(); - data_info_field(); // Zenoh declarations resource_declaration(); - publisher_declaration(); subscriber_declaration(); queryable_declaration(); forget_resource_declaration(); - forget_publisher_declaration(); forget_subscriber_declaration(); forget_queryable_declaration(); // Zenoh messages declare_message(); - data_message(); + push_body_message(); pull_message(); - query_message(); - zenoh_message(); - - // Network messages - push_message(); - request_message(); - response_message(); - response_final_message(); - - // Transport messages - join_message(); - init_message(); - open_message(); - close_message(); - keep_alive_message(); - frame_message(); - fragment_message(); - transport_message(); - batch(); + // query_message(); + err_message(); + ack_message(); + reply_message(); + + // // Network messages + // push_message(); + // request_message(); + // response_message(); + // response_final_message(); + + // YAY, LET'S TRY IT! + // wohoo! + // // Transport messages + // join_message(); + // init_message(); + // open_message(); + // close_message(); + // keep_alive_message(); + // frame_message(); + // fragment_message(); + // transport_message(); + // batch(); // fragmentation(); // Scouting messages - scout_message(); - hello_message(); + // scout_message(); + // hello_message(); } return 0; From c25ace38ecca43cf977cfba5ec75a91c5ec598b9 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 7 Jul 2023 18:00:59 +0200 Subject: [PATCH 115/181] network layer tests passing --- src/protocol/codec/ext.c | 8 ++++- src/protocol/codec/message.c | 39 ++++++++++---------- src/protocol/codec/network.c | 58 ++++++++++++++++++++---------- src/protocol/definitions/message.c | 6 +++- tests/z_msgcodec_test.c | 22 ++++++------ 5 files changed, 83 insertions(+), 50 deletions(-) diff --git a/src/protocol/codec/ext.c b/src/protocol/codec/ext.c index ff096e25e..a5ce38e3f 100644 --- a/src/protocol/codec/ext.c +++ b/src/protocol/codec/ext.c @@ -178,7 +178,7 @@ int8_t _z_msg_ext_unknown_error(_z_msg_ext_t *extension, uint8_t trace_id) { } case _Z_MSG_EXT_ENC_ZINT: { _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZINT(%02x)\n", ext_id, - trace_id, extension->_body._zint); + trace_id, extension->_body._zint._val); break; } case _Z_MSG_EXT_ENC_ZBUF: { @@ -215,7 +215,13 @@ int8_t _z_msg_ext_decode_iter(_z_zbuf_t *zbf, int8_t (*callback)(_z_msg_ext_t *, _Bool has_next = true; while (has_next && ret == _Z_RES_OK) { _z_msg_ext_t ext = _z_msg_ext_make_unit(0); + size_t start = zbf->_ios._r_pos; ret |= _z_msg_ext_decode(&ext, zbf, &has_next); + printf("EXT (%d): ", ret); + for (; start < zbf->_ios._r_pos; start++) { + printf("%02x ", zbf->_ios._buf[start]); + } + printf("\n"); if (ret == _Z_RES_OK) { ret |= callback(&ext, context); _z_msg_ext_clear(&ext); diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 51b2342d2..6c79af01d 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -247,7 +247,7 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { (void)(wbf); (void)(pshb); int8_t ret = _Z_RES_OK; - uint8_t header = pshb->_is_put ? _Z_M_PUT_ID : _Z_M_DEL_ID; + uint8_t header = pshb->_is_put ? _Z_MID_Z_PUT : _Z_MID_Z_DEL; _Bool has_source_info = _z_id_check(pshb->_body._put._commons._source_info._id); _Bool has_timestamp = _z_timestamp_check(&pshb->_body._put._commons._timestamp); _Bool has_encoding = false; @@ -310,7 +310,7 @@ int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf, uint8_t header) int8_t ret = _Z_RES_OK; if (ret == _Z_RES_OK) { switch (_Z_MID(header)) { - case _Z_M_PUT_ID: { + case _Z_MID_Z_PUT: { pshb->_is_put = true; pshb->_body._put = (_z_msg_put_t){0}; if (_Z_HAS_FLAG(header, _Z_FLAG_Z_P_T)) { @@ -328,7 +328,7 @@ int8_t _z_push_body_decode(_z_push_body_t *pshb, _z_zbuf_t *zbf, uint8_t header) } break; } - case _Z_M_DEL_ID: { + case _Z_MID_Z_DEL: { pshb->_is_put = false; pshb->_body._del = (_z_msg_del_t){0}; if (_Z_HAS_FLAG(header, _Z_FLAG_Z_D_T)) { @@ -397,10 +397,9 @@ 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_zint_encode( - wbf, 1 + _z_zint_len(msg->_ext_value.encoding.prefix) + _z_zint_len(msg->_ext_value.encoding.suffix.len) + - msg->_ext_value.encoding.suffix.len + _z_zint_len(msg->_ext_value.payload.len) + - msg->_ext_value.payload.len)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, _z_zint_len(msg->_ext_value.encoding.prefix) + + _z_bytes_encode_len(&msg->_ext_value.encoding.suffix) + + _z_bytes_encode_len(&msg->_ext_value.payload))); _Z_RETURN_IF_ERR(_z_encoding_prefix_encode(wbf, msg->_ext_value.encoding.prefix)); _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_ext_value.encoding.suffix)); _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_ext_value.payload)); @@ -415,6 +414,7 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *msg) { } if (required_exts.info) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01; + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, extheader)); _Z_RETURN_IF_ERR(_z_source_info_encode_ext(wbf, &msg->_ext_info)); } @@ -452,15 +452,16 @@ int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) { int8_t _z_query_decode(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_Z_QUERY\n"); *msg = (_z_msg_query_t){0}; + msg->_ext_consolidation = Z_CONSOLIDATION_MODE_AUTO; int8_t ret = _Z_RES_OK; if (_Z_HAS_FLAG(header, _Z_FLAG_Z_P)) { - ret = _z_bytes_decode(&msg->_parameters, zbf); + _Z_RETURN_IF_ERR(_z_bytes_decode(&msg->_parameters, zbf)); } else { _z_bytes_clear(&msg->_parameters); } - if (ret == _Z_RES_OK) { - ret = _z_msg_ext_decode_iter(zbf, _z_query_decode_extensions, msg); + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_query_decode_extensions, msg)); } return ret; @@ -630,7 +631,7 @@ int8_t _z_err_decode(_z_msg_err_t *err, _z_zbuf_t *zbf, uint8_t header) { int8_t _z_ack_encode(_z_wbuf_t *wbf, const _z_msg_ack_t *ack) { int8_t ret = _Z_RES_OK; - uint8_t header = _Z_MID_Z_ERR; + uint8_t header = _Z_MID_Z_ACK; _Bool has_ts = _z_timestamp_check(&ack->_timestamp); _Bool has_sinfo_ext = _z_id_check(ack->_ext_source_info._id); if (has_ts) { @@ -639,13 +640,13 @@ int8_t _z_ack_encode(_z_wbuf_t *wbf, const _z_msg_ack_t *ack) { if (has_sinfo_ext) { header |= _Z_FLAG_Z_Z; } - ret = _z_uint8_encode(wbf, header); - if ((ret == _Z_RES_OK) && has_ts) { - ret = _z_timestamp_encode(wbf, &ack->_timestamp); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + if (has_ts) { + _Z_RETURN_IF_ERR(_z_timestamp_encode(wbf, &ack->_timestamp)); } - if ((ret == _Z_RES_OK) && has_sinfo_ext) { - ret = _z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x01); - ret |= _z_source_info_encode_ext(wbf, &ack->_ext_source_info); + if (has_sinfo_ext) { + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | 0x01)); + _Z_RETURN_IF_ERR(_z_source_info_encode_ext(wbf, &ack->_ext_source_info)); } return ret; } @@ -669,9 +670,9 @@ int8_t _z_ack_decode(_z_msg_ack_t *ack, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; *ack = (_z_msg_ack_t){0}; if (_Z_HAS_FLAG(header, _Z_FLAG_Z_A_T)) { - ret = _z_timestamp_decode(&ack->_timestamp, zbf); + _Z_RETURN_IF_ERR(_z_timestamp_decode(&ack->_timestamp, zbf)); } - if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { ret = _z_msg_ext_decode_iter(zbf, _z_ack_decode_extension, ack); } return ret; diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index 06bc21bb6..ddea6a32f 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -88,6 +88,8 @@ int8_t _z_push_decode_ext_cb(_z_msg_ext_t *extension, void *ctx) { int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; + *msg = (_z_n_msg_push_t){0}; + msg->_qos = _Z_N_QOS_DEFAULT; ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N)); msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_M); if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_N_Z)) { @@ -110,11 +112,11 @@ int8_t _z_request_encode(_z_wbuf_t *wbf, const _z_n_msg_request_t *msg) { if (has_suffix) { header |= _Z_FLAG_N_REQUEST_N; } - _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); + _z_n_msg_request_exts_t exts = _z_n_msg_request_needed_exts(msg); + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header | (exts.n != 0 ? _Z_FLAG_Z_Z : 0))); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_rid)); _Z_RETURN_IF_ERR(_z_keyexpr_encode(wbf, has_suffix, &msg->_key)); - _z_n_msg_request_exts_t exts = _z_n_msg_request_needed_exts(msg); if (exts.ext_qos) { exts.n -= 1; uint8_t extheader = 0x01 | _Z_MSG_EXT_ENC_ZINT | (exts.n ? _Z_FLAG_Z_Z : 0); @@ -174,7 +176,7 @@ int8_t _z_request_decode_extensions(_z_msg_ext_t *extension, void *ctx) { _Z_RETURN_IF_ERR(_z_timestamp_decode(&msg->_ext_timestamp, &zbf)); break; } - case 0x04 | _Z_MSG_EXT_ENC_ZINT: { + case 0x04 | _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M: { msg->_ext_target = extension->_body._zint._val; if (msg->_ext_target > 2) { return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; @@ -193,25 +195,33 @@ int8_t _z_request_decode_extensions(_z_msg_ext_t *extension, void *ctx) { } return _Z_RES_OK; } -int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header) { +int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, const uint8_t header) { + *msg = (_z_n_msg_request_t){0}; + msg->_ext_qos = _Z_N_QOS_DEFAULT; _Z_RETURN_IF_ERR(_z_zint_decode(&msg->_rid, zbf)); _Z_RETURN_IF_ERR(_z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N))); msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_M); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_request_decode_extensions, msg)); } - switch (_Z_MID(header)) { + uint8_t zheader; + _Z_RETURN_IF_ERR(_z_uint8_decode(&zheader, zbf)); + switch (_Z_MID(zheader)) { case _Z_MID_Z_QUERY: { - _Z_RETURN_IF_ERR(_z_query_decode(&msg->_body._query, zbf, header)); + msg->_tag = _Z_REQUEST_QUERY; + _Z_RETURN_IF_ERR(_z_query_decode(&msg->_body._query, zbf, zheader)); } break; case _Z_MID_Z_PUT: { - _Z_RETURN_IF_ERR(_z_put_decode(&msg->_body._put, zbf, header)); + msg->_tag = _Z_REQUEST_PUT; + _Z_RETURN_IF_ERR(_z_put_decode(&msg->_body._put, zbf, zheader)); } break; case _Z_MID_Z_DEL: { - _Z_RETURN_IF_ERR(_z_del_decode(&msg->_body._del, zbf, header)); + msg->_tag = _Z_REQUEST_DEL; + _Z_RETURN_IF_ERR(_z_del_decode(&msg->_body._del, zbf, zheader)); } break; case _Z_MID_Z_PULL: { - _Z_RETURN_IF_ERR(_z_pull_decode(&msg->_body._pull, zbf, header)); + msg->_tag = _Z_REQUEST_PULL; + _Z_RETURN_IF_ERR(_z_pull_decode(&msg->_body._pull, zbf, zheader)); } break; default: return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; @@ -228,10 +238,11 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { _Bool has_ts_ext = _z_timestamp_check(&msg->_ext_timestamp); _Bool has_responder_ext = _z_id_check(msg->_ext_responder._zid); uint8_t 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 (msg->_key._uses_remote_mapping) { _Z_SET_FLAG(header, _Z_FLAG_N_RESPONSE_M); } - if (msg->_key._suffix != NULL) { + if (has_suffix) { _Z_SET_FLAG(header, _Z_FLAG_N_RESPONSE_N); } if (n_ext != 0) { @@ -241,7 +252,7 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_request_id)); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_key._id)); - if (msg->_key._suffix != NULL) { + if (has_suffix) { _Z_RETURN_IF_ERR(_z_str_encode(wbf, msg->_key._suffix)) } if (has_qos_ext) { @@ -261,12 +272,12 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { } if (has_responder_ext) { n_ext -= 1; - uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x02 | (n_ext != 0 ? _Z_FLAG_Z_Z : 0); + 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_uint8_encode(wbf, extheader)); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, zidlen + 1 + _z_zint_len(msg->_ext_responder._eid))); + _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_zint_encode(wbf, msg->_ext_responder._eid)); } @@ -329,6 +340,8 @@ int8_t _z_response_decode_extension(_z_msg_ext_t *extension, void *ctx) { int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_N_RESPONSE\n"); + *msg = (_z_n_msg_response_t){0}; + msg->_ext_qos = _Z_N_QOS_DEFAULT; int8_t ret = _Z_RES_OK; msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_M); _Z_RETURN_IF_ERR(_z_zint_decode(&msg->_request_id, zbf)); @@ -342,23 +355,28 @@ int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t head switch (_Z_MID(inner_header)) { case _Z_MID_Z_REPLY: { - _z_reply_decode(&msg->_body._reply, zbf, header); + msg->_tag = _Z_RESPONSE_BODY_REPLY; + ret = _z_reply_decode(&msg->_body._reply, zbf, inner_header); break; } case _Z_MID_Z_ERR: { - _z_err_decode(&msg->_body._err, zbf, header); + msg->_tag = _Z_RESPONSE_BODY_ERR; + ret = _z_err_decode(&msg->_body._err, zbf, inner_header); break; } case _Z_MID_Z_ACK: { - _z_ack_decode(&msg->_body._ack, zbf, header); + msg->_tag = _Z_RESPONSE_BODY_ACK; + ret = _z_ack_decode(&msg->_body._ack, zbf, inner_header); break; } case _Z_MID_Z_PUT: { - _z_put_decode(&msg->_body._put, zbf, header); + msg->_tag = _Z_RESPONSE_BODY_PUT; + ret = _z_put_decode(&msg->_body._put, zbf, inner_header); break; } case _Z_MID_Z_DEL: { - _z_del_decode(&msg->_body._del, zbf, header); + msg->_tag = _Z_RESPONSE_BODY_DEL; + ret = _z_del_decode(&msg->_body._del, zbf, inner_header); break; } default: { @@ -380,6 +398,8 @@ int8_t _z_response_final_encode(_z_wbuf_t *wbf, const _z_n_msg_response_final_t } int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { (void)(header); + + *msg = (_z_n_msg_response_final_t){0}; int8_t ret = _Z_RES_OK; ret |= _z_zint_decode(&msg->_request_id, zbf); return ret; @@ -425,6 +445,8 @@ int8_t _z_declare_decode_extensions(_z_msg_ext_t *extension, void *ctx) { return _Z_RES_OK; } int8_t _z_declare_decode(_z_n_msg_declare_t *decl, _z_zbuf_t *zbf, uint8_t header) { + *decl = (_z_n_msg_declare_t){0}; + decl->_ext_qos = _Z_N_QOS_DEFAULT; if (_Z_HAS_FLAG(header, _Z_FLAG_N_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_declare_decode_extensions, decl)) } diff --git a/src/protocol/definitions/message.c b/src/protocol/definitions/message.c index cfbb81f24..9620161f2 100644 --- a/src/protocol/definitions/message.c +++ b/src/protocol/definitions/message.c @@ -1,5 +1,7 @@ #include "zenoh-pico/protocol/definitions/message.h" +#include "zenoh-pico/collections/bytes.h" + void _z_msg_reply_clear(_z_msg_reply_t *msg) { _z_value_clear(&msg->_value); } void _z_msg_put_clear(_z_msg_put_t *msg) { @@ -9,7 +11,9 @@ void _z_msg_put_clear(_z_msg_put_t *msg) { } _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.start != NULL, + return (_z_msg_query_reqexts_t){.body = msg->_ext_value.payload.start != NULL || + msg->_ext_value.encoding.prefix != 0 || + !_z_bytes_is_empty(&msg->_ext_value.encoding.suffix), .info = _z_id_check(msg->_ext_info._id), .consolidation = msg->_ext_consolidation != Z_CONSOLIDATION_MODE_AUTO}; } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index ef1231d62..836573ac9 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -1345,7 +1345,8 @@ void request_message(void) { _z_n_msg_request_t decoded; __auto_type zbf = _z_wbuf_to_zbuf(&wbf); __auto_type header = _z_zbuf_read(&zbf); - assert(_Z_RES_OK == _z_request_decode(&decoded, &zbf, header)); + int8_t ret = _z_request_decode(&decoded, &zbf, header); + assert(_Z_RES_OK == ret); assert_eq_request(&expected, &decoded); _z_n_msg_request_clear(&decoded); _z_n_msg_request_clear(&expected); @@ -1425,7 +1426,8 @@ void response_message(void) { _z_n_msg_response_t decoded; __auto_type zbf = _z_wbuf_to_zbuf(&wbf); __auto_type header = _z_zbuf_read(&zbf); - assert(_Z_RES_OK == _z_response_decode(&decoded, &zbf, header)); + int8_t ret = _z_response_decode(&decoded, &zbf, header); + assert(_Z_RES_OK == ret); assert_eq_response(&expected, &decoded); _z_n_msg_response_clear(&decoded); _z_n_msg_response_clear(&expected); @@ -1479,19 +1481,17 @@ int main(void) { declare_message(); push_body_message(); pull_message(); - // query_message(); + query_message(); err_message(); ack_message(); reply_message(); - // // Network messages - // push_message(); - // request_message(); - // response_message(); - // response_final_message(); + // Network messages + push_message(); + request_message(); + response_message(); + response_final_message(); - // YAY, LET'S TRY IT! - // wohoo! // // Transport messages // join_message(); // init_message(); @@ -1504,7 +1504,7 @@ int main(void) { // batch(); // fragmentation(); - // Scouting messages + // // Scouting messages // scout_message(); // hello_message(); } From 4b29c0287f96edba2c684cc65a7e09bc9f3c1bc9 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Tue, 11 Jul 2023 16:40:11 +0200 Subject: [PATCH 116/181] just a few more tests, new keyexpr resolution checked --- include/zenoh-pico/protocol/codec/transport.h | 17 ++ .../protocol/definitions/transport.h | 18 +- src/protocol/codec/message.c | 21 +- src/protocol/codec/network.c | 7 +- src/protocol/codec/transport.c | 85 +++--- src/protocol/definitions/message.c | 10 +- src/protocol/definitions/transport.c | 36 +-- src/session/resource.c | 1 - tests/z_msgcodec_test.c | 256 +++++++++++++++++- 9 files changed, 354 insertions(+), 97 deletions(-) diff --git a/include/zenoh-pico/protocol/codec/transport.h b/include/zenoh-pico/protocol/codec/transport.h index f7e0936ad..344081992 100644 --- a/include/zenoh-pico/protocol/codec/transport.h +++ b/include/zenoh-pico/protocol/codec/transport.h @@ -24,4 +24,21 @@ int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *buf) int8_t _z_transport_message_encode(_z_wbuf_t *buf, const _z_transport_message_t *msg); int8_t _z_transport_message_decode(_z_transport_message_t *msg, _z_zbuf_t *buf); +int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg); +int8_t _z_join_decode(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg); +int8_t _z_init_decode(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_open_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_open_t *msg); +int8_t _z_open_decode(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_close_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_close_t *msg); +int8_t _z_close_decode(_z_t_msg_close_t *msg, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_keep_alive_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_keep_alive_t *msg); +int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg); +int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); #endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_TRANSPORT_H */ diff --git a/include/zenoh-pico/protocol/definitions/transport.h b/include/zenoh-pico/protocol/definitions/transport.h index bf221968c..80f3683ca 100644 --- a/include/zenoh-pico/protocol/definitions/transport.h +++ b/include/zenoh-pico/protocol/definitions/transport.h @@ -123,7 +123,7 @@ typedef struct { z_what_t _what; uint8_t _version; } _z_s_msg_scout_t; -void _z_s_msg_clear_scout(_z_s_msg_scout_t *msg); +void _z_s_msg_scout_clear(_z_s_msg_scout_t *msg); /*------------------ Hello Message ------------------*/ // NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length @@ -170,7 +170,7 @@ typedef struct { z_whatami_t _whatami; uint8_t _version; } _z_s_msg_hello_t; -void _z_s_msg_clear_hello(_z_s_msg_hello_t *msg); +void _z_s_msg_hello_clear(_z_s_msg_hello_t *msg); /*------------------ Join Message ------------------*/ // # Join message @@ -230,7 +230,7 @@ typedef struct { uint8_t _seq_num_res; uint8_t _version; } _z_t_msg_join_t; -void _z_t_msg_clear_join(_z_t_msg_join_t *msg); +void _z_t_msg_join_clear(_z_t_msg_join_t *msg); /*------------------ Init Message ------------------*/ // # Init message @@ -310,7 +310,7 @@ typedef struct { uint8_t _seq_num_res; uint8_t _version; } _z_t_msg_init_t; -void _z_t_msg_clear_init(_z_t_msg_init_t *msg); +void _z_t_msg_init_clear(_z_t_msg_init_t *msg); /*------------------ Open Message ------------------*/ // NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total lenght @@ -348,7 +348,7 @@ typedef struct { _z_zint_t _initial_sn; _z_bytes_t _cookie; } _z_t_msg_open_t; -void _z_t_msg_clear_open(_z_t_msg_open_t *msg); +void _z_t_msg_open_clear(_z_t_msg_open_t *msg); /*------------------ Close Message ------------------*/ // NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length @@ -378,7 +378,7 @@ void _z_t_msg_clear_open(_z_t_msg_open_t *msg); typedef struct { uint8_t _reason; } _z_t_msg_close_t; -void _z_t_msg_clear_close(_z_t_msg_close_t *msg); +void _z_t_msg_close_clear(_z_t_msg_close_t *msg); /*=============================*/ /* Close reasons */ /*=============================*/ @@ -414,7 +414,7 @@ void _z_t_msg_clear_close(_z_t_msg_close_t *msg); typedef struct { uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior } _z_t_msg_keep_alive_t; -void _z_t_msg_clear_keep_alive(_z_t_msg_keep_alive_t *msg); +void _z_t_msg_keep_alive_clear(_z_t_msg_keep_alive_t *msg); /*------------------ Frame Message ------------------*/ // NOTE: 16 bits (2 bytes) may be prepended to the serialized message indicating the total length @@ -445,7 +445,7 @@ typedef struct { _z_network_message_vec_t _messages; _z_zint_t _sn; } _z_t_msg_frame_t; -void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg); +void _z_t_msg_frame_clear(_z_t_msg_frame_t *msg); /*------------------ Fragment Message ------------------*/ // The Fragment message is used to transmit on the wire large Zenoh Message that require fragmentation @@ -473,7 +473,7 @@ typedef struct { _z_bytes_t _payload; _z_zint_t _sn; } _z_t_msg_fragment_t; -void _z_t_msg_clear_fragment(_z_t_msg_fragment_t *msg); +void _z_t_msg_fragment_clear(_z_t_msg_fragment_t *msg); /*------------------ Transport Message ------------------*/ typedef union { diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 6c79af01d..68474cbac 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -126,11 +126,13 @@ int8_t _z_keyexpr_decode(_z_keyexpr_t *ke, _z_zbuf_t *zbf, _Bool has_suffix) { int8_t ret = _Z_RES_OK; ret |= _z_zint16_decode(&ke->_id, zbf); + ke->_owns_suffix = false; if (has_suffix == true) { char *str = NULL; ret |= _z_str_decode(&str, zbf); if (ret == _Z_RES_OK) { ke->_suffix = str; + ke->_owns_suffix = true; } else { ke->_suffix = NULL; } @@ -248,7 +250,9 @@ int8_t _z_push_body_encode(_z_wbuf_t *wbf, const _z_push_body_t *pshb) { (void)(pshb); int8_t ret = _Z_RES_OK; uint8_t header = pshb->_is_put ? _Z_MID_Z_PUT : _Z_MID_Z_DEL; - _Bool has_source_info = _z_id_check(pshb->_body._put._commons._source_info._id); + _Bool has_source_info = _z_id_check(pshb->_body._put._commons._source_info._id) || + pshb->_body._put._commons._source_info._source_sn != 0 || + pshb->_body._put._commons._source_info._entity_id != 0; _Bool has_timestamp = _z_timestamp_check(&pshb->_body._put._commons._timestamp); _Bool has_encoding = false; if (has_source_info) { @@ -476,7 +480,9 @@ int8_t _z_reply_encode(_z_wbuf_t *wbf, const _z_msg_reply_t *reply) { !_z_bytes_is_empty(&reply->_value.encoding.suffix)) { header |= _Z_FLAG_Z_R_E; } - if (reply->_ext_consolidation != Z_CONSOLIDATION_MODE_AUTO || _z_id_check(reply->_ext_source_info._id)) { + _Bool has_sourceinfo = _z_id_check(reply->_ext_source_info._id) || reply->_ext_source_info._source_sn != 0 || + reply->_ext_source_info._entity_id != 0; + if (reply->_ext_consolidation != Z_CONSOLIDATION_MODE_AUTO || has_sourceinfo) { header |= _Z_FLAG_Z_Z; } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); @@ -491,7 +497,7 @@ int8_t _z_reply_encode(_z_wbuf_t *wbf, const _z_msg_reply_t *reply) { _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &reply->_value.encoding.suffix)); } _Bool has_consolidation_ext = reply->_ext_consolidation != Z_CONSOLIDATION_MODE_AUTO; - if (_z_id_check(reply->_ext_source_info._id)) { + if (has_sourceinfo) { uint8_t extheader = _Z_MSG_EXT_ENC_ZBUF | 0x01; if (has_consolidation_ext) { extheader |= _Z_MSG_EXT_FLAG_Z; @@ -557,7 +563,8 @@ int8_t _z_err_encode(_z_wbuf_t *wbf, const _z_msg_err_t *err) { } _Bool has_payload_ext = err->_ext_value.payload.start != NULL || err->_ext_value.encoding.prefix != 0 || !_z_bytes_is_empty(&err->_ext_value.encoding.suffix); - _Bool has_sinfo_ext = _z_id_check(err->_ext_source_info._id); + _Bool has_sinfo_ext = _z_id_check(err->_ext_source_info._id) || err->_ext_source_info._entity_id != 0 || + err->_ext_source_info._source_sn != 0; if (has_sinfo_ext || has_payload_ext) { header |= _Z_FLAG_Z_Z; } @@ -633,7 +640,8 @@ int8_t _z_ack_encode(_z_wbuf_t *wbf, const _z_msg_ack_t *ack) { int8_t ret = _Z_RES_OK; uint8_t header = _Z_MID_Z_ACK; _Bool has_ts = _z_timestamp_check(&ack->_timestamp); - _Bool has_sinfo_ext = _z_id_check(ack->_ext_source_info._id); + _Bool has_sinfo_ext = _z_id_check(ack->_ext_source_info._id) || ack->_ext_source_info._source_sn != 0 || + ack->_ext_source_info._entity_id != 0; if (has_ts) { header |= _Z_FLAG_Z_A_T; } @@ -681,7 +689,8 @@ int8_t _z_ack_decode(_z_msg_ack_t *ack, _z_zbuf_t *zbf, uint8_t header) { int8_t _z_pull_encode(_z_wbuf_t *wbf, const _z_msg_pull_t *pull) { int8_t ret = _Z_RES_OK; uint8_t header = _Z_MID_Z_PULL; - _Bool has_info_ext = _z_id_check(pull->_ext_source_info._id); + _Bool has_info_ext = _z_id_check(pull->_ext_source_info._id) || pull->_ext_source_info._source_sn != 0 || + pull->_ext_source_info._entity_id != 0; if (has_info_ext) { header |= _Z_FLAG_Z_Z; } diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index ddea6a32f..a522af3d7 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -236,7 +236,7 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { _Z_DEBUG("Encoding _Z_MID_N_RESPONSE\n"); _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); + _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); _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); if (msg->_key._uses_remote_mapping) { @@ -477,18 +477,23 @@ int8_t _z_network_message_decode(_z_network_message_t *msg, _z_zbuf_t *zbf) { _Z_RETURN_IF_ERR(_z_uint8_decode(&header, zbf)); switch (_Z_MID(header)) { case _Z_MID_N_DECLARE: { + msg->_tag = _Z_N_DECLARE; return _z_declare_decode(&msg->_body._declare, zbf, header); } break; case _Z_MID_N_PUSH: { + msg->_tag = _Z_N_PUSH; return _z_push_decode(&msg->_body._push, zbf, header); } break; case _Z_MID_N_REQUEST: { + msg->_tag = _Z_N_REQUEST; return _z_request_decode(&msg->_body._request, zbf, header); } break; case _Z_MID_N_RESPONSE: { + msg->_tag = _Z_N_RESPONSE; return _z_response_decode(&msg->_body._response, zbf, header); } break; case _Z_MID_N_RESPONSE_FINAL: { + msg->_tag = _Z_N_RESPONSE_FINAL; return _z_response_final_decode(&msg->_body._response_final, zbf, header); } break; default: diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index 6f2874a18..75f0b0aba 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -12,10 +12,14 @@ // ZettaScale Zenoh Team, // +#include #include +#include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/protocol/codec/ext.h" #include "zenoh-pico/protocol/codec/network.h" +#include "zenoh-pico/protocol/definitions/core.h" +#include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/utils/logging.h" @@ -25,50 +29,45 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg int8_t ret = _Z_RES_OK; _Z_DEBUG("Encoding _Z_MID_T_JOIN\n"); - ret = _z_wbuf_write(wbf, msg->_version); + _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, msg->_version)); - if (ret == _Z_RES_OK) { - uint8_t cbyte = 0; - cbyte |= (msg->_whatami & 0x03); - uint8_t zidlen = _z_id_len(msg->_zid); - cbyte |= ((zidlen - 1) & 0x0F) << 4; - ret = _z_uint8_encode(wbf, cbyte); - if (ret == _Z_RES_OK) { - ret = _z_wbuf_write_bytes(wbf, msg->_zid.id, 0, zidlen); - } - } + uint8_t cbyte = 0; + cbyte |= (msg->_whatami & 0x03); + uint8_t zidlen = _z_id_len(msg->_zid); + cbyte |= ((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 ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S)) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_S)) { uint8_t cbyte = 0; cbyte |= (msg->_seq_num_res & 0x03); cbyte |= ((msg->_req_id_res & 0x03) << 2); - ret = _z_uint8_encode(wbf, cbyte); - if (ret == _Z_RES_OK) { - ret = _z_uint16_encode(wbf, msg->_batch_size); - } - } - if (ret == _Z_RES_OK) { - if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_T) == true) { - ret = _z_zint_encode(wbf, msg->_lease / 1000); - } else { - ret = _z_zint_encode(wbf, msg->_lease); - } + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, cbyte)); + _Z_RETURN_IF_ERR(_z_uint16_encode(wbf, msg->_batch_size)); } - if (ret == _Z_RES_OK) { - if (msg->_next_sn._is_qos) { - if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { - ret = _z_uint8_encode(wbf, 0x51); // QOS-ext: (enc=zbuf)(mandatory=true)(id=1) - for (uint8_t i = 0; (i < Z_PRIORITIES_NUM) && (ret == _Z_RES_OK); i++) { - ret = _z_zint_encode(wbf, msg->_next_sn._val._qos[i]._reliable); - ret |= _z_zint_encode(wbf, msg->_next_sn._val._qos[i]._best_effort); - } - } else { - _Z_DEBUG("Attempted to serialize QoS-SN extension, but the header extension flag was unset"); - ret |= _Z_ERR_MESSAGE_SERIALIZATION_FAILED; + if (_Z_HAS_FLAG(header, _Z_FLAG_T_JOIN_T) == true) { + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_lease / 1000)); + } else { + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_lease)); + } + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_next_sn._val._plain._reliable)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_next_sn._val._plain._best_effort)); + if (msg->_next_sn._is_qos) { + if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { + _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZBUF | _Z_MSG_EXT_FLAG_M | 1)); + size_t len = 0; + for (uint8_t i = 0; (i < Z_PRIORITIES_NUM) && (ret == _Z_RES_OK); i++) { + len += _z_zint_len(msg->_next_sn._val._qos[i]._reliable) + + _z_zint_len(msg->_next_sn._val._qos[i]._best_effort); + } + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, len)); + for (uint8_t i = 0; (i < Z_PRIORITIES_NUM) && (ret == _Z_RES_OK); i++) { + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_next_sn._val._qos[i]._reliable)); + _Z_RETURN_IF_ERR(_z_zint_encode(wbf, msg->_next_sn._val._qos[i]._best_effort)); } } else { - ret |= _z_zint_encode(wbf, msg->_next_sn._val._plain._reliable); - ret |= _z_zint_encode(wbf, msg->_next_sn._val._plain._best_effort); + _Z_DEBUG("Attempted to serialize QoS-SN extension, but the header extension flag was unset"); + ret |= _Z_ERR_MESSAGE_SERIALIZATION_FAILED; } } @@ -78,12 +77,13 @@ int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg int8_t _z_join_decode_ext(_z_msg_ext_t *extension, void *ctx) { int8_t ret = _Z_RES_OK; _z_t_msg_join_t *msg = (_z_t_msg_join_t *)ctx; - if (_Z_EXT_FULL_ID(extension->_header) == 0x51) { // QOS: (enc=zbuf)(mandatory=true)(id=1) + if (_Z_EXT_FULL_ID(extension->_header) == + (_Z_MSG_EXT_ENC_ZBUF | _Z_MSG_EXT_FLAG_M | 1)) { // QOS: (enc=zbuf)(mandatory=true)(id=1) msg->_next_sn._is_qos = true; _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); for (int i = 0; (ret == _Z_RES_OK) && (i < Z_PRIORITIES_NUM); ++i) { - ret |= _z_zint_decode(&msg->_next_sn._val._plain._reliable, &zbf); - ret |= _z_zint_decode(&msg->_next_sn._val._plain._best_effort, &zbf); + ret |= _z_zint_decode(&msg->_next_sn._val._qos[i]._reliable, &zbf); + ret |= _z_zint_decode(&msg->_next_sn._val._qos[i]._best_effort, &zbf); } } else if (_Z_MSG_EXT_IS_MANDATORY(extension->_header)) { ret = _Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN; @@ -204,9 +204,6 @@ int8_t _z_init_decode(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_A) == true)) { ret |= _z_bytes_decode(&msg->_cookie, zbf); - if (ret != _Z_RES_OK) { - msg->_cookie = _z_bytes_empty(); - } } else { msg->_cookie = _z_bytes_empty(); } @@ -305,7 +302,9 @@ int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t int8_t ret = _Z_RES_OK; _Z_DEBUG("Decoding _Z_MID_T_KEEP_ALIVE\n"); - ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x03); + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x03); + } return ret; } diff --git a/src/protocol/definitions/message.c b/src/protocol/definitions/message.c index 9620161f2..fc6b14491 100644 --- a/src/protocol/definitions/message.c +++ b/src/protocol/definitions/message.c @@ -11,11 +11,11 @@ void _z_msg_put_clear(_z_msg_put_t *msg) { } _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.start != NULL || - msg->_ext_value.encoding.prefix != 0 || - !_z_bytes_is_empty(&msg->_ext_value.encoding.suffix), - .info = _z_id_check(msg->_ext_info._id), - .consolidation = msg->_ext_consolidation != Z_CONSOLIDATION_MODE_AUTO}; + return (_z_msg_query_reqexts_t){ + .body = msg->_ext_value.payload.start != NULL || msg->_ext_value.encoding.prefix != 0 || + !_z_bytes_is_empty(&msg->_ext_value.encoding.suffix), + .info = _z_id_check(msg->_ext_info._id) || msg->_ext_info._entity_id != 0 || msg->_ext_info._source_sn != 0, + .consolidation = msg->_ext_consolidation != Z_CONSOLIDATION_MODE_AUTO}; } void _z_msg_query_clear(_z_msg_query_t *msg) { diff --git a/src/protocol/definitions/transport.c b/src/protocol/definitions/transport.c index bfbd600b7..2af2f11e0 100644 --- a/src/protocol/definitions/transport.c +++ b/src/protocol/definitions/transport.c @@ -2,56 +2,56 @@ #include "zenoh-pico/utils/logging.h" -void _z_s_msg_clear_scout(_z_s_msg_scout_t *msg) {} +void _z_s_msg_scout_clear(_z_s_msg_scout_t *msg) {} /*------------------ Locators Field ------------------*/ void _z_locators_clear(_z_locator_array_t *ls) { _z_locator_array_clear(ls); } -void _z_s_msg_clear_hello(_z_s_msg_hello_t *msg) { _z_locators_clear(&msg->_locators); } +void _z_s_msg_hello_clear(_z_s_msg_hello_t *msg) { _z_locators_clear(&msg->_locators); } -void _z_t_msg_clear_join(_z_t_msg_join_t *msg) {} +void _z_t_msg_join_clear(_z_t_msg_join_t *msg) {} -void _z_t_msg_clear_init(_z_t_msg_init_t *msg) { _z_bytes_clear(&msg->_cookie); } +void _z_t_msg_init_clear(_z_t_msg_init_t *msg) { _z_bytes_clear(&msg->_cookie); } -void _z_t_msg_clear_open(_z_t_msg_open_t *msg) { _z_bytes_clear(&msg->_cookie); } +void _z_t_msg_open_clear(_z_t_msg_open_t *msg) { _z_bytes_clear(&msg->_cookie); } -void _z_t_msg_clear_close(_z_t_msg_close_t *msg) { (void)(msg); } +void _z_t_msg_close_clear(_z_t_msg_close_t *msg) { (void)(msg); } -void _z_t_msg_clear_keep_alive(_z_t_msg_keep_alive_t *msg) { (void)(msg); } +void _z_t_msg_keep_alive_clear(_z_t_msg_keep_alive_t *msg) { (void)(msg); } -void _z_t_msg_clear_frame(_z_t_msg_frame_t *msg) { _z_network_message_vec_clear(&msg->_messages); } +void _z_t_msg_frame_clear(_z_t_msg_frame_t *msg) { _z_network_message_vec_clear(&msg->_messages); } -void _z_t_msg_clear_fragment(_z_t_msg_fragment_t *msg) { _z_bytes_clear(&msg->_payload); } +void _z_t_msg_fragment_clear(_z_t_msg_fragment_t *msg) { _z_bytes_clear(&msg->_payload); } void _z_t_msg_clear(_z_transport_message_t *msg) { uint8_t mid = _Z_MID(msg->_header); switch (mid) { case _Z_MID_T_JOIN: { - _z_t_msg_clear_join(&msg->_body._join); + _z_t_msg_join_clear(&msg->_body._join); } break; case _Z_MID_T_INIT: { - _z_t_msg_clear_init(&msg->_body._init); + _z_t_msg_init_clear(&msg->_body._init); } break; case _Z_MID_T_OPEN: { - _z_t_msg_clear_open(&msg->_body._open); + _z_t_msg_open_clear(&msg->_body._open); } break; case _Z_MID_T_CLOSE: { - _z_t_msg_clear_close(&msg->_body._close); + _z_t_msg_close_clear(&msg->_body._close); } break; case _Z_MID_T_KEEP_ALIVE: { - _z_t_msg_clear_keep_alive(&msg->_body._keep_alive); + _z_t_msg_keep_alive_clear(&msg->_body._keep_alive); } break; case _Z_MID_T_FRAME: { - _z_t_msg_clear_frame(&msg->_body._frame); + _z_t_msg_frame_clear(&msg->_body._frame); } break; case _Z_MID_T_FRAGMENT: { - _z_t_msg_clear_fragment(&msg->_body._fragment); + _z_t_msg_fragment_clear(&msg->_body._fragment); } break; default: { @@ -321,11 +321,11 @@ void _z_s_msg_clear(_z_scouting_message_t *msg) { uint8_t mid = _Z_MID(msg->_header); switch (mid) { case _Z_MID_SCOUT: { - _z_s_msg_clear_scout(&msg->_body._scout); + _z_s_msg_scout_clear(&msg->_body._scout); } break; case _Z_MID_HELLO: { - _z_s_msg_clear_hello(&msg->_body._hello); + _z_s_msg_hello_clear(&msg->_body._hello); } break; default: { diff --git a/src/session/resource.c b/src/session/resource.c index e90f1fde2..68859175e 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -198,7 +198,6 @@ _z_keyexpr_t _z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t * #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - _Bool is_local = !keyexpr->_uses_remote_mapping; _z_keyexpr_t res = __unsafe_z_get_expanded_key_from_key(zn, keyexpr); #if Z_MULTI_THREAD == 1 diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 836573ac9..f3523d21f 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -16,6 +16,7 @@ #include "zenoh-pico/protocol/codec/message.h" #include "zenoh-pico/protocol/codec/network.h" #include "zenoh-pico/protocol/definitions/message.h" +#include "zenoh-pico/protocol/definitions/transport.h" #define ZENOH_PICO_TEST_H #include @@ -710,7 +711,7 @@ _z_decl_subscriber_t gen_subscriber_declaration(void) { return e_sd; } -void assert_eq_subscriber_declaration(_z_decl_subscriber_t *left, _z_decl_subscriber_t *right) { +void assert_eq_subscriber_declaration(const _z_decl_subscriber_t *left, const _z_decl_subscriber_t *right) { assert_eq_keyexpr(&left->_keyexpr, &right->_keyexpr); assert(left->_id == right->_id); assert(left->_ext_subinfo._pull_mode == right->_ext_subinfo._pull_mode); @@ -756,7 +757,7 @@ _z_decl_queryable_t gen_queryable_declaration(void) { return e_qd; } -void assert_eq_queryable_declaration(_z_decl_queryable_t *left, _z_decl_queryable_t *right) { +void assert_eq_queryable_declaration(const _z_decl_queryable_t *left, const _z_decl_queryable_t *right) { assert_eq_keyexpr(&left->_keyexpr, &right->_keyexpr); printf("Complete (%u:%u), ", left->_ext_queryable_info._complete, right->_ext_queryable_info._complete); @@ -805,7 +806,7 @@ _z_undecl_kexpr_t gen_forget_resource_declaration(void) { return e_frd; } -void assert_eq_forget_resource_declaration(_z_undecl_kexpr_t *left, _z_undecl_kexpr_t *right) { +void assert_eq_forget_resource_declaration(const _z_undecl_kexpr_t *left, const _z_undecl_kexpr_t *right) { printf("RID (%u:%u)", left->_id, right->_id); assert(left->_id == right->_id); } @@ -846,7 +847,7 @@ _z_undecl_subscriber_t gen_forget_subscriber_declaration(void) { return e_fsd; } -void assert_eq_forget_subscriber_declaration(_z_undecl_subscriber_t *left, _z_undecl_subscriber_t *right) { +void assert_eq_forget_subscriber_declaration(const _z_undecl_subscriber_t *left, const _z_undecl_subscriber_t *right) { assert_eq_keyexpr(&left->_ext_keyexpr, &right->_ext_keyexpr); assert(left->_id == right->_id); } @@ -889,7 +890,7 @@ _z_undecl_queryable_t gen_forget_queryable_declaration(void) { return e_fqd; } -void assert_eq_forget_queryable_declaration(_z_undecl_queryable_t *left, _z_undecl_queryable_t *right) { +void assert_eq_forget_queryable_declaration(const _z_undecl_queryable_t *left, const _z_undecl_queryable_t *right) { assert_eq_keyexpr(&left->_ext_keyexpr, &right->_ext_keyexpr); assert(left->_id == right->_id); } @@ -961,7 +962,7 @@ _z_declaration_t gen_declaration(void) { return d; } -void assert_eq_declaration(_z_declaration_t *left, _z_declaration_t *right) { +void assert_eq_declaration(const _z_declaration_t *left, const _z_declaration_t *right) { printf("Declaration -> "); printf("Header (%x:%x), ", left->_tag, right->_tag); assert(left->_tag == right->_tag); @@ -1447,7 +1448,8 @@ void response_final_message(void) { _z_n_msg_response_final_t decoded; __auto_type zbf = _z_wbuf_to_zbuf(&wbf); __auto_type header = _z_zbuf_read(&zbf); - assert(_Z_RES_OK == _z_response_final_decode(&decoded, &zbf, header)); + int8_t ret = _z_response_final_decode(&decoded, &zbf, header); + assert(_Z_RES_OK == ret); assert_eq_response_final(&expected, &decoded); _z_n_msg_response_final_clear(&decoded); _z_n_msg_response_final_clear(&expected); @@ -1455,6 +1457,232 @@ void response_final_message(void) { _z_wbuf_clear(&wbf); } +_z_transport_message_t gen_join(void) { + _z_conduit_sn_list_t conduit = {._is_qos = gen_bool()}; + if (conduit._is_qos) { + for (int i = 0; i < Z_PRIORITIES_NUM; i++) { + conduit._val._qos[i]._best_effort = gen_uint64(); + conduit._val._qos[i]._reliable = gen_uint64(); + } + } else { + conduit._val._plain._best_effort = gen_uint64(); + conduit._val._plain._reliable = gen_uint64(); + } + return _z_t_msg_make_join(gen_uint8() % 3, gen_uint64(), gen_zid(), conduit); +} +void assert_eq_join(const _z_t_msg_join_t *left, const _z_t_msg_join_t *right) { + assert(memcmp(left->_zid.id, right->_zid.id, 16) == 0); + assert(left->_lease == right->_lease); + assert(left->_batch_size == right->_batch_size); + assert(left->_whatami == right->_whatami); + assert(left->_req_id_res == right->_req_id_res); + assert(left->_seq_num_res == right->_seq_num_res); + assert(left->_version == right->_version); + assert(left->_next_sn._is_qos == right->_next_sn._is_qos); + if (left->_next_sn._is_qos) { + for (int i = 0; i < Z_PRIORITIES_NUM; i++) { + assert(left->_next_sn._val._qos[i]._best_effort == right->_next_sn._val._qos[i]._best_effort); + assert(left->_next_sn._val._qos[i]._reliable == right->_next_sn._val._qos[i]._reliable); + } + } else { + assert(left->_next_sn._val._plain._best_effort == right->_next_sn._val._plain._best_effort); + assert(left->_next_sn._val._plain._reliable == right->_next_sn._val._plain._reliable); + } +} +void join_message(void) { + printf("\n>> Join message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_join(); + assert(_z_join_encode(&wbf, expected._header, &expected._body._join) == _Z_RES_OK); + _z_t_msg_join_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + int8_t ret = _z_join_decode(&decoded, &zbf, expected._header); + assert(_Z_RES_OK == ret); + assert_eq_join(&expected._body._join, &decoded); + _z_t_msg_join_clear(&decoded); + _z_t_msg_clear(&expected); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +_z_transport_message_t gen_init(void) { + if (gen_bool()) { + return _z_t_msg_make_init_syn(gen_uint8() % 3, gen_zid()); + } else { + return _z_t_msg_make_init_ack(gen_uint8() % 3, gen_zid(), gen_bytes(16)); + } +} +void assert_eq_init(const _z_t_msg_init_t *left, const _z_t_msg_init_t *right) { + assert(left->_batch_size == right->_batch_size); + assert(left->_req_id_res == right->_req_id_res); + assert(left->_seq_num_res == right->_seq_num_res); + assert_eq_bytes(&left->_cookie, &right->_cookie); + assert(memcmp(left->_zid.id, right->_zid.id, 16) == 0); + assert(left->_version == right->_version); + assert(left->_whatami == right->_whatami); +} +void init_message(void) { + printf("\n>> Init message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_init(); + assert(_z_init_encode(&wbf, expected._header, &expected._body._init) == _Z_RES_OK); + _z_t_msg_init_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + int8_t ret = _z_init_decode(&decoded, &zbf, expected._header); + assert(_Z_RES_OK == ret); + assert_eq_init(&expected._body._init, &decoded); + _z_t_msg_init_clear(&decoded); + _z_t_msg_clear(&expected); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +_z_transport_message_t gen_open(void) { + if (gen_bool()) { + return _z_t_msg_make_open_syn(gen_uint(), gen_uint(), gen_bytes(16)); + } else { + return _z_t_msg_make_open_ack(gen_uint(), gen_uint()); + } +} +void assert_eq_open(const _z_t_msg_open_t *left, const _z_t_msg_open_t *right) { + assert_eq_bytes(&left->_cookie, &right->_cookie); + assert(left->_initial_sn == right->_initial_sn); + assert(left->_lease == right->_lease); +} +void open_message(void) { + printf("\n>> open message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_open(); + assert(_z_open_encode(&wbf, expected._header, &expected._body._open) == _Z_RES_OK); + _z_t_msg_open_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + int8_t ret = _z_open_decode(&decoded, &zbf, expected._header); + assert(_Z_RES_OK == ret); + assert_eq_open(&expected._body._open, &decoded); + _z_t_msg_open_clear(&decoded); + _z_t_msg_clear(&expected); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +_z_transport_message_t gen_close(void) { return _z_t_msg_make_close(gen_uint(), gen_bool()); } +void assert_eq_close(const _z_t_msg_close_t *left, const _z_t_msg_close_t *right) { + assert(left->_reason == right->_reason); +} +void close_message(void) { + printf("\n>> close message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_close(); + assert(_z_close_encode(&wbf, expected._header, &expected._body._close) == _Z_RES_OK); + _z_t_msg_close_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + int8_t ret = _z_close_decode(&decoded, &zbf, expected._header); + assert(_Z_RES_OK == ret); + assert_eq_close(&expected._body._close, &decoded); + _z_t_msg_close_clear(&decoded); + _z_t_msg_clear(&expected); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +_z_transport_message_t gen_keep_alive(void) { return _z_t_msg_make_keep_alive(); } +void assert_eq_keep_alive(const _z_t_msg_keep_alive_t *left, const _z_t_msg_keep_alive_t *right) { + (void)left; + (void)right; +} +void keep_alive_message(void) { + printf("\n>> keep_alive message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_keep_alive(); + assert(_z_keep_alive_encode(&wbf, expected._header, &expected._body._keep_alive) == _Z_RES_OK); + _z_t_msg_keep_alive_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + int8_t ret = _z_keep_alive_decode(&decoded, &zbf, expected._header); + assert(_Z_RES_OK == ret); + assert_eq_keep_alive(&expected._body._keep_alive, &decoded); + _z_t_msg_keep_alive_clear(&decoded); + _z_t_msg_clear(&expected); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} +_z_network_message_t gen_net_msg(void) { + switch (gen_uint8() % 5) { + case 0: { + return gen_declare_message(); + } break; + case 1: { + return (_z_network_message_t){._tag = _Z_N_PUSH, ._body._push = gen_push()}; + } break; + case 2: { + return (_z_network_message_t){._tag = _Z_N_REQUEST, ._body._request = gen_request()}; + } break; + case 3: { + return (_z_network_message_t){._tag = _Z_N_RESPONSE, ._body._response = gen_response()}; + } break; + case 4: { + return (_z_network_message_t){._tag = _Z_N_RESPONSE_FINAL, ._body._response_final = gen_response_final()}; + } break; + } + assert(false); +} +void assert_eq_net_msg(const _z_network_message_t *left, const _z_network_message_t *right) { + assert(left->_tag == right->_tag); + switch (left->_tag) { + case _Z_N_DECLARE: { + assert_eq_declaration(&left->_body._declare._decl, &right->_body._declare._decl); + assert_eq_timestamp(&left->_body._declare._ext_timestamp, &right->_body._declare._ext_timestamp); + assert(left->_body._declare._ext_qos._val == right->_body._declare._ext_qos._val); + } break; + case _Z_N_PUSH: { + assert_eq_push(&left->_body._push, &right->_body._push); + } break; + case _Z_N_REQUEST: { + assert_eq_request(&left->_body._request, &right->_body._request); + } break; + case _Z_N_RESPONSE: { + assert_eq_response(&left->_body._response, &right->_body._response); + } break; + case _Z_N_RESPONSE_FINAL: { + assert_eq_response_final(&left->_body._response_final, &right->_body._response_final); + } break; + } +} +_z_network_message_vec_t gen_net_msgs(size_t n) { + _z_network_message_vec_t ret = _z_network_message_vec_make(n); + for (size_t i = 0; i < n; i++) { + _z_network_message_t *msg = (_z_network_message_t *)z_malloc(sizeof(_z_network_message_t)); + *msg = gen_net_msg(); + _z_network_message_vec_append(&ret, msg); + } + return ret; +} + +_z_transport_message_t gen_frame(void) { + return _z_t_msg_make_frame(gen_uint(), gen_net_msgs(gen_uint8() % 16), gen_bool()); +} +void assert_eq_frame(const _z_t_msg_frame_t *left, const _z_t_msg_frame_t *right) { + assert(left->_sn == right->_sn); + assert(left->_messages._len == right->_messages._len); + for (size_t i = 0; i < left->_messages._len; i++) { + assert_eq_net_msg(left->_messages._val[i], right->_messages._val[i]); + } +} +void frame_message(void) { + printf("\n>> frame message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_frame(); + assert(_z_frame_encode(&wbf, expected._header, &expected._body._frame) == _Z_RES_OK); + _z_t_msg_frame_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + int8_t ret = _z_frame_decode(&decoded, &zbf, expected._header); + assert(_Z_RES_OK == ret); + assert_eq_frame(&expected._body._frame, &decoded); + _z_t_msg_frame_clear(&decoded); + _z_t_msg_clear(&expected); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + /*=============================*/ /* Main */ /*=============================*/ @@ -1492,13 +1720,13 @@ int main(void) { response_message(); response_final_message(); - // // Transport messages - // join_message(); - // init_message(); - // open_message(); - // close_message(); - // keep_alive_message(); - // frame_message(); + // Transport messages + join_message(); + init_message(); + open_message(); + close_message(); + keep_alive_message(); + frame_message(); // fragment_message(); // transport_message(); // batch(); From 3d9c4354c63082e802998415a76f2f2afb29e675 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Tue, 11 Jul 2023 18:09:42 +0200 Subject: [PATCH 117/181] more tests passing --- include/zenoh-pico/protocol/codec/transport.h | 9 +- include/zenoh-pico/protocol/msgcodec.h | 149 ------------------ include/zenoh-pico/transport/link/rx.h | 1 - src/protocol/codec/message.c | 1 - src/protocol/codec/network.c | 2 +- src/protocol/codec/transport.c | 14 +- src/session/rx.c | 1 - src/transport/unicast/link/tx.c | 1 - tests/z_msgcodec_test.c | 99 +++++++++++- 9 files changed, 116 insertions(+), 161 deletions(-) delete mode 100644 include/zenoh-pico/protocol/msgcodec.h diff --git a/include/zenoh-pico/protocol/codec/transport.h b/include/zenoh-pico/protocol/codec/transport.h index 344081992..0feaf1784 100644 --- a/include/zenoh-pico/protocol/codec/transport.h +++ b/include/zenoh-pico/protocol/codec/transport.h @@ -17,9 +17,10 @@ #include "zenoh-pico/protocol/definitions/transport.h" #include "zenoh-pico/protocol/iobuf.h" +#define _ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE 32 + int8_t _z_scouting_message_encode(_z_wbuf_t *buf, const _z_scouting_message_t *msg); int8_t _z_scouting_message_decode(_z_scouting_message_t *msg, _z_zbuf_t *buf); -int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *buf); int8_t _z_transport_message_encode(_z_wbuf_t *buf, const _z_transport_message_t *msg); int8_t _z_transport_message_decode(_z_transport_message_t *msg, _z_zbuf_t *buf); @@ -41,4 +42,10 @@ int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg); int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg); +int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); + +int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t *msg); +int8_t _z_transport_message_decode(_z_transport_message_t *msg, _z_zbuf_t *zbf); #endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_TRANSPORT_H */ diff --git a/include/zenoh-pico/protocol/msgcodec.h b/include/zenoh-pico/protocol/msgcodec.h deleted file mode 100644 index d87c166a9..000000000 --- a/include/zenoh-pico/protocol/msgcodec.h +++ /dev/null @@ -1,149 +0,0 @@ -// -// 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 INCLUDE_ZENOH_PICO_PROTOCOL_MSGCODEC_H -#define INCLUDE_ZENOH_PICO_PROTOCOL_MSGCODEC_H - -#include -#define _ZENOH_PICO_FRAME_MESSAGES_VEC_SIZE 32 - -#include "zenoh-pico/collections/element.h" -#include "zenoh-pico/link/endpoint.h" -#include "zenoh-pico/protocol/codec.h" -#include "zenoh-pico/protocol/definitions/transport.h" -#include "zenoh-pico/utils/result.h" - -/*------------------ Zenoh Message ------------------*/ - -// int8_t _z_zenoh_message_encode(_z_wbuf_t *buf, const _z_zenoh_message_t *msg); -// int8_t _z_zenoh_message_decode(_z_zenoh_message_t *msg, _z_zbuf_t *buf); -// int8_t _z_zenoh_message_decode_na(_z_zenoh_message_t *msg, _z_zbuf_t *buf); -#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_MSGCODEC_H */ - -// NOTE: the following headers are for unit testing only -#ifdef ZENOH_PICO_TEST_H -// ------------------ Message Fields ------------------ -int8_t _z_payload_encode(_z_wbuf_t *buf, const _z_bytes_t *pld); -int8_t _z_payload_decode(_z_bytes_t *pld, _z_zbuf_t *buf); -int8_t _z_payload_decode_na(_z_bytes_t *pld, _z_zbuf_t *buf); -int8_t _z_subinfo_decode_na(_z_subinfo_t *si, _z_zbuf_t *buf, uint8_t header); - -int8_t _z_data_info_encode(_z_wbuf_t *buf, const _z_data_info_t *di); -int8_t _z_data_info_decode(_z_data_info_t *di, _z_zbuf_t *buf); -int8_t _z_data_info_decode_na(_z_data_info_t *di, _z_zbuf_t *buf); - -// ------------------ Zenoh Message ------------------ -int8_t _z_res_decl_encode(_z_wbuf_t *buf, uint8_t header, const _z_res_decl_t *dcl); -int8_t _z_res_decl_decode(_z_res_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); -int8_t _z_res_decl_decode_na(_z_res_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); - -int8_t _z_pub_decl_encode(_z_wbuf_t *buf, uint8_t header, const _z_pub_decl_t *dcl); -int8_t _z_pub_decl_decode(_z_pub_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); -int8_t _z_pub_decl_decode_na(_z_pub_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); - -int8_t _z_sub_decl_encode(_z_wbuf_t *buf, uint8_t header, const _z_sub_decl_t *dcl); -int8_t _z_sub_decl_decode(_z_sub_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); -int8_t _z_sub_decl_decode_na(_z_sub_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); - -int8_t _z_qle_decl_encode(_z_wbuf_t *buf, uint8_t header, const _z_qle_decl_t *dcl); -int8_t _z_qle_decl_decode(_z_qle_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); -int8_t _z_qle_decl_decode_na(_z_qle_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); - -int8_t _z_forget_res_decl_encode(_z_wbuf_t *buf, const _z_forget_res_decl_t *dcl); -int8_t _z_forget_res_decl_decode(_z_forget_res_decl_t *dcl, _z_zbuf_t *buf); -int8_t _z_forget_res_decl_decode_na(_z_forget_res_decl_t *dcl, _z_zbuf_t *buf); - -int8_t _z_forget_pub_decl_encode(_z_wbuf_t *buf, uint8_t header, const _z_forget_pub_decl_t *dcl); -int8_t _z_forget_pub_decl_decode(_z_forget_pub_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); -int8_t _z_forget_pub_decl_decode_na(_z_forget_pub_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); - -int8_t _z_forget_sub_decl_encode(_z_wbuf_t *buf, uint8_t header, const _z_forget_sub_decl_t *dcl); -int8_t _z_forget_sub_decl_decode(_z_forget_sub_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); -int8_t _z_forget_sub_decl_decode_na(_z_forget_sub_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); - -int8_t _z_forget_qle_decl_encode(_z_wbuf_t *buf, uint8_t header, const _z_forget_qle_decl_t *dcl); -int8_t _z_forget_qle_decl_decode(_z_forget_qle_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); -int8_t _z_forget_qle_decl_decode_na(_z_forget_qle_decl_t *dcl, _z_zbuf_t *buf, uint8_t header); - -int8_t _z_reply_context_encode(_z_wbuf_t *buf, const _z_reply_context_t *rc); -int8_t _z_reply_context_decode(_z_reply_context_t **rc, _z_zbuf_t *buf, uint8_t header); -int8_t _z_reply_context_decode_na(_z_reply_context_t *rc, _z_zbuf_t *buf, uint8_t header); - -int8_t _z_data_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_data_t *msg); -int8_t _z_data_decode_na(_z_msg_data_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_data_decode(_z_msg_data_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_pull_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_pull_t *msg); -int8_t _z_pull_decode_na(_z_msg_pull_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_pull_decode(_z_msg_pull_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_query_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_query_t *msg); -int8_t _z_query_decode_na(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_query_decode(_z_msg_query_t *msg, _z_zbuf_t *zbf, uint8_t header); - -// ------------------ Network Message ------------------ -int8_t _z_declare_encode(_z_wbuf_t *wbf, const _z_n_msg_declare_t *msg); -int8_t _z_declare_decode(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf); -int8_t _z_declare_decode_na(_z_n_msg_declare_t *msg, _z_zbuf_t *zbf); - -int8_t _z_push_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_push_t *msg); -int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_request_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_request_t *msg); -int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_request_decode_na(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_response_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_t *msg); -int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_response_decode_na(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_response_final_encode(_z_wbuf_t *wbf, uint8_t header, const _z_n_msg_response_final_t *msg); -int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_response_final_decode_na(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header); - -// ------------------ Transport Message ------------------ -int8_t _z_join_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_join_t *msg); -int8_t _z_join_decode(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg); -int8_t _z_init_decode(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_open_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_open_t *msg); -int8_t _z_open_decode(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_close_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_close_t *msg); -int8_t _z_close_decode(_z_t_msg_close_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_keep_alive_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_keep_alive_t *msg); -int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *msg); -int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragment_t *msg); -int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_extensions_encode(_z_wbuf_t *wbf, uint8_t header, const _z_msg_ext_vec_t *v_ext); -int8_t _z_extensions_decode(_z_msg_ext_vec_t *v_ext, _z_zbuf_t *zbf, uint8_t header); - -// ------------------ Discovery Message ------------------ -int8_t _z_scout_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_scout_t *msg); -int8_t _z_scout_decode_na(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_scout_decode(_z_s_msg_scout_t *msg, _z_zbuf_t *zbf, uint8_t header); - -int8_t _z_hello_encode(_z_wbuf_t *wbf, uint8_t header, const _z_s_msg_hello_t *msg); -int8_t _z_hello_decode_na(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header); -int8_t _z_hello_decode(_z_s_msg_hello_t *msg, _z_zbuf_t *zbf, uint8_t header); - -#endif /* ZENOH_PICO_TEST_H */ diff --git a/include/zenoh-pico/transport/link/rx.h b/include/zenoh-pico/transport/link/rx.h index b1c5f7c6a..f4c428a64 100644 --- a/include/zenoh-pico/transport/link/rx.h +++ b/include/zenoh-pico/transport/link/rx.h @@ -16,7 +16,6 @@ #define ZENOH_PICO_TRANSPORT_LINK_RX_H #include "zenoh-pico/link/link.h" -#include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/transport/transport.h" /*------------------ Transmission and Reception helpers ------------------*/ diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 68474cbac..141294d67 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -32,7 +32,6 @@ #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/keyexpr.h" -#include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index a522af3d7..cac062fc5 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -31,7 +31,6 @@ #include "zenoh-pico/protocol/definitions/message.h" #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/iobuf.h" -#include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" @@ -380,6 +379,7 @@ int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t head break; } default: { + _Z_ERROR("Unknown N_MID: %d\n", _Z_MID(inner_header)); ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } } diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index 75f0b0aba..9c7281b2f 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -12,16 +12,18 @@ // ZettaScale Zenoh Team, // +#include "zenoh-pico/protocol/definitions/transport.h" + #include #include #include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/protocol/codec/ext.h" #include "zenoh-pico/protocol/codec/network.h" +#include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/protocol/definitions/core.h" #include "zenoh-pico/protocol/ext.h" #include "zenoh-pico/protocol/iobuf.h" -#include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" /*------------------ Join Message ------------------*/ @@ -94,6 +96,7 @@ int8_t _z_join_decode_ext(_z_msg_ext_t *extension, void *ctx) { int8_t _z_join_decode(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_T_JOIN\n"); int8_t ret = _Z_RES_OK; + *msg = (_z_t_msg_join_t){0}; ret |= _z_uint8_decode(&msg->_version, zbf); @@ -172,6 +175,7 @@ int8_t _z_init_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_init_t *msg int8_t _z_init_decode(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_T_INIT\n"); + *msg = (_z_t_msg_init_t){0}; int8_t ret = _Z_RES_OK; ret |= _z_uint8_decode(&msg->_version, zbf); @@ -238,6 +242,7 @@ int8_t _z_open_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_open_t *msg int8_t _z_open_decode(_z_t_msg_open_t *msg, _z_zbuf_t *zbf, uint8_t header) { _Z_DEBUG("Decoding _Z_MID_T_OPEN\n"); int8_t ret = _Z_RES_OK; + *msg = (_z_t_msg_open_t){0}; ret |= _z_zint_decode(&msg->_lease, zbf); if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_OPEN_T) == true)) { @@ -275,6 +280,7 @@ int8_t _z_close_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_close_t *m int8_t _z_close_decode(_z_t_msg_close_t *msg, _z_zbuf_t *zbf, uint8_t header) { (void)(header); int8_t ret = _Z_RES_OK; + *msg = (_z_t_msg_close_t){0}; _Z_DEBUG("Decoding _Z_MID_T_CLOSE\n"); ret |= _z_uint8_decode(&msg->_reason, zbf); @@ -298,6 +304,7 @@ int8_t _z_keep_alive_decode(_z_t_msg_keep_alive_t *msg, _z_zbuf_t *zbf, uint8_t (void)(msg); (void)(zbf); (void)(header); + *msg = (_z_t_msg_keep_alive_t){0}; int8_t ret = _Z_RES_OK; _Z_DEBUG("Decoding _Z_MID_T_KEEP_ALIVE\n"); @@ -331,6 +338,7 @@ int8_t _z_frame_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_frame_t *m int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; + *msg = (_z_t_msg_frame_t){0}; ret |= _z_zint_decode(&msg->_sn, zbf); if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_Z) == true)) { @@ -381,6 +389,8 @@ int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragmen int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; + *msg = (_z_t_msg_fragment_t){0}; + _Z_DEBUG("Decoding _Z_TRANSPORT_FRAGMENT\n"); ret |= _z_zint_decode(&msg->_sn, zbf); @@ -424,8 +434,6 @@ int8_t _z_extensions_decode(_z_msg_ext_vec_t *v_ext, _z_zbuf_t *zbf, uint8_t hea int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t *msg) { int8_t ret = _Z_RES_OK; - // Encode the decorators if present - uint8_t header = msg->_header; _Z_RETURN_IF_ERR(_z_wbuf_write(wbf, header)) diff --git a/src/session/rx.c b/src/session/rx.c index 10e16b06d..b865b1785 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -21,7 +21,6 @@ #include "zenoh-pico/protocol/definitions/message.h" #include "zenoh-pico/protocol/definitions/network.h" #include "zenoh-pico/protocol/keyexpr.h" -#include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/session/query.h" #include "zenoh-pico/session/queryable.h" #include "zenoh-pico/session/resource.h" diff --git a/src/transport/unicast/link/tx.c b/src/transport/unicast/link/tx.c index 4e56517bd..2482ec2b0 100644 --- a/src/transport/unicast/link/tx.c +++ b/src/transport/unicast/link/tx.c @@ -18,7 +18,6 @@ #include "zenoh-pico/protocol/codec/network.h" #include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/protocol/iobuf.h" -#include "zenoh-pico/protocol/msgcodec.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index f3523d21f..aa9d640ee 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -190,7 +190,7 @@ _z_id_t gen_zid(void) { for (uint8_t i = 0; i < len; i++) { uint8_t byte = gen_uint8(); id.id[i] = byte; - hash ^= byte; + hash ^= i * byte; } id.id[0] = hash; return id; @@ -1683,6 +1683,99 @@ void frame_message(void) { _z_wbuf_clear(&wbf); } +_z_transport_message_t gen_fragment(void) { + return _z_t_msg_make_fragment(gen_uint(), gen_bytes(gen_uint8()), gen_bool(), gen_bool()); +} +void assert_eq_fragment(const _z_t_msg_fragment_t *left, const _z_t_msg_fragment_t *right) { + assert(left->_sn == right->_sn); + assert_eq_bytes(&left->_payload, &right->_payload); +} +void fragment_message(void) { + printf("\n>> fragment message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_fragment(); + assert(_z_fragment_encode(&wbf, expected._header, &expected._body._fragment) == _Z_RES_OK); + _z_t_msg_fragment_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + int8_t ret = _z_fragment_decode(&decoded, &zbf, expected._header); + assert(_Z_RES_OK == ret); + assert_eq_fragment(&expected._body._fragment, &decoded); + _z_t_msg_fragment_clear(&decoded); + _z_t_msg_clear(&expected); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + +_z_transport_message_t gen_transport(void) { + switch (gen_uint8() % 7) { + case 0: { + return gen_join(); + }; + case 1: { + return gen_init(); + }; + case 2: { + return gen_open(); + }; + case 3: { + return gen_close(); + }; + case 4: { + return gen_keep_alive(); + }; + case 5: { + return gen_frame(); + }; + case 6: { + return gen_fragment(); + }; + } + assert(false); +} +void assert_eq_transport(const _z_transport_message_t *left, const _z_transport_message_t *right) { + assert(left->_header == right->_header); + switch (_Z_MID(left->_header)) { + case _Z_MID_T_JOIN: { + assert_eq_join(&left->_body._join, &right->_body._join); + } break; + case _Z_MID_T_CLOSE: { + assert_eq_close(&left->_body._close, &right->_body._close); + } break; + case _Z_MID_T_INIT: { + assert_eq_init(&left->_body._init, &right->_body._init); + } break; + case _Z_MID_T_OPEN: { + assert_eq_open(&left->_body._open, &right->_body._open); + } break; + case _Z_MID_T_KEEP_ALIVE: { + assert_eq_keep_alive(&left->_body._keep_alive, &right->_body._keep_alive); + } break; + case _Z_MID_T_FRAME: { + assert_eq_frame(&left->_body._frame, &right->_body._frame); + } break; + case _Z_MID_T_FRAGMENT: { + assert_eq_fragment(&left->_body._fragment, &right->_body._fragment); + } break; + default: + assert(false); + } +} +void transport_message(void) { + printf("\n>> transport message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_transport(); + assert(_z_transport_message_encode(&wbf, &expected) == _Z_RES_OK); + _z_transport_message_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + int8_t ret = _z_transport_message_decode(&decoded, &zbf); + assert(_Z_RES_OK == ret); + assert_eq_transport(&expected, &decoded); + _z_t_msg_clear(&decoded); + _z_t_msg_clear(&expected); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + /*=============================*/ /* Main */ /*=============================*/ @@ -1727,8 +1820,8 @@ int main(void) { close_message(); keep_alive_message(); frame_message(); - // fragment_message(); - // transport_message(); + fragment_message(); + transport_message(); // batch(); // fragmentation(); From 6cc8f780278a1434ce8af828f9534cff89252c4f Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 12 Jul 2023 09:24:15 +0200 Subject: [PATCH 118/181] tests done, checking compilation on CI --- include/zenoh-pico/protocol/keyexpr.h | 2 +- src/protocol/keyexpr.c | 6 ++-- tests/z_msgcodec_test.c | 50 +++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/include/zenoh-pico/protocol/keyexpr.h b/include/zenoh-pico/protocol/keyexpr.h index 99622c11b..49c088a13 100644 --- a/include/zenoh-pico/protocol/keyexpr.h +++ b/include/zenoh-pico/protocol/keyexpr.h @@ -30,7 +30,7 @@ _z_keyexpr_t _z_keyexpr_duplicate(_z_keyexpr_t src); _z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src); _z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src); static inline _z_keyexpr_t _z_keyexpr_null(void) { - return (_z_keyexpr_t){._id = 0, ._owns_suffix = false, ._suffix = NULL, ._uses_remote_mapping = false}; + return (_z_keyexpr_t){._id = 0, ._uses_remote_mapping = false, ._owns_suffix = false, ._suffix = NULL}; } _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp); void _z_timestamp_clear(_z_timestamp_t *tstamp); diff --git a/src/protocol/keyexpr.c b/src/protocol/keyexpr.c index e9e6d98e6..675a90dd9 100644 --- a/src/protocol/keyexpr.c +++ b/src/protocol/keyexpr.c @@ -36,9 +36,7 @@ _z_keyexpr_t _z_keyexpr_duplicate(_z_keyexpr_t src) { _z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src) { _z_keyexpr_t stolen = *src; - src->_owns_suffix = false; - src->_id = 0; - src->_suffix = NULL; + *src = _z_keyexpr_null(); return stolen; } @@ -63,9 +61,9 @@ void _z_keyexpr_free(_z_keyexpr_t **rk) { _z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src) { return (_z_keyexpr_t){ ._id = src._id, + ._uses_remote_mapping = src._uses_remote_mapping, ._owns_suffix = false, ._suffix = src._suffix, - ._uses_remote_mapping = src._uses_remote_mapping, }; } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index aa9d640ee..d4f09a72e 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -302,7 +302,7 @@ void assert_eq_str_array(_z_str_array_t *left, _z_str_array_t *right) { printf(")"); } -void assert_eq_locator_array(_z_locator_array_t *left, _z_locator_array_t *right) { +void assert_eq_locator_array(const _z_locator_array_t *left, const _z_locator_array_t *right) { printf("Locators -> "); printf("Length (%zu:%zu), ", left->_len, right->_len); @@ -1776,6 +1776,47 @@ void transport_message(void) { _z_wbuf_clear(&wbf); } +_z_scouting_message_t gen_scouting(void) { + if (gen_bool()) { + return _z_s_msg_make_scout(gen_uint8() % 3, gen_zid()); + } else { + return _z_s_msg_make_hello(gen_uint8() % 3, gen_zid(), gen_locator_array((gen_uint8() % 16) + 1)); + } +} +void assert_eq_scouting(const _z_scouting_message_t *left, const _z_scouting_message_t *right) { + assert(left->_header == right->_header); + switch (_Z_MID(left->_header)) { + case _Z_MID_SCOUT: { + assert(left->_body._scout._version == right->_body._scout._version); + assert(left->_body._scout._what == right->_body._scout._what); + assert(memcmp(left->_body._scout._zid.id, right->_body._scout._zid.id, 16) == 0); + } break; + case _Z_MID_HELLO: { + assert(left->_body._hello._version == right->_body._hello._version); + assert(left->_body._hello._whatami == right->_body._hello._whatami); + assert(memcmp(left->_body._hello._zid.id, right->_body._hello._zid.id, 16) == 0); + assert_eq_locator_array(&left->_body._hello._locators, &right->_body._hello._locators); + } break; + default: + assert(false); + } +} +void scouting_message(void) { + printf("\n>> scouting message\n"); + __auto_type wbf = gen_wbuf(UINT16_MAX); + __auto_type expected = gen_scouting(); + assert(_z_scouting_message_encode(&wbf, &expected) == _Z_RES_OK); + _z_scouting_message_t decoded; + __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + int8_t ret = _z_scouting_message_decode(&decoded, &zbf); + assert(_Z_RES_OK == ret); + assert_eq_scouting(&expected, &decoded); + _z_s_msg_clear(&decoded); + _z_s_msg_clear(&expected); + _z_zbuf_clear(&zbf); + _z_wbuf_clear(&wbf); +} + /*=============================*/ /* Main */ /*=============================*/ @@ -1822,12 +1863,9 @@ int main(void) { frame_message(); fragment_message(); transport_message(); - // batch(); - // fragmentation(); - // // Scouting messages - // scout_message(); - // hello_message(); + // Scouting messages + scouting_message(); } return 0; From 3f202415753efe5ee2c267b096d4c2dbca0cb4db Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 12 Jul 2023 09:49:04 +0200 Subject: [PATCH 119/181] fix lints --- tests/z_msgcodec_test.c | 8 ++++---- tests/z_peer_multicast_test.c | 8 ++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index d4f09a72e..a699bc2d7 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -1619,11 +1619,11 @@ _z_network_message_t gen_net_msg(void) { case 3: { return (_z_network_message_t){._tag = _Z_N_RESPONSE, ._body._response = gen_response()}; } break; - case 4: { + case 4: + default: { return (_z_network_message_t){._tag = _Z_N_RESPONSE_FINAL, ._body._response_final = gen_response_final()}; } break; } - assert(false); } void assert_eq_net_msg(const _z_network_message_t *left, const _z_network_message_t *right) { assert(left->_tag == right->_tag); @@ -1726,11 +1726,11 @@ _z_transport_message_t gen_transport(void) { case 5: { return gen_frame(); }; - case 6: { + case 6: + default: { return gen_fragment(); }; } - assert(false); } void assert_eq_transport(const _z_transport_message_t *left, const _z_transport_message_t *right) { assert(left->_header == right->_header); diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 3c5d29a0a..2056c1c86 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -18,6 +18,8 @@ #include #include "zenoh-pico.h" +#include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/protocol/core.h" #define MSG 10 #define MSG_LEN 1024 @@ -68,7 +70,8 @@ int main(int argc, char **argv) { z_owned_session_t s1 = z_open(z_move(config)); assert(z_check(s1)); - z_string_t zid1 = _z_string_from_bytes(&z_loan(s1)._val->_local_zid); + _z_bytes_t id_as_bytes = _z_bytes_wrap(z_loan(s1)._val->_local_zid.id, _z_id_len(z_loan(s1)._val->_local_zid)); + z_string_t zid1 = _z_string_from_bytes(&id_as_bytes); printf("Session 1 with PID: %s\n", zid1.val); _z_string_clear(&zid1); @@ -84,7 +87,8 @@ int main(int argc, char **argv) { z_owned_session_t s2 = z_open(z_move(config)); assert(z_check(s2)); - z_string_t zid2 = _z_string_from_bytes(&z_loan(s2)._val->_local_zid); + id_as_bytes = _z_bytes_wrap(z_loan(s2)._val->_local_zid.id, _z_id_len(z_loan(s2)._val->_local_zid)); + z_string_t zid2 = _z_string_from_bytes(&id_as_bytes); printf("Session 2 with PID: %s\n", zid2.val); _z_string_clear(&zid2); From 16be0dd7c3f2fc38915a1af63dbec193af1970ea Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 12 Jul 2023 09:52:41 +0200 Subject: [PATCH 120/181] fix lints (more) --- tests/z_client_test.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/tests/z_client_test.c b/tests/z_client_test.c index f226c1962..a48d482ea 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -17,6 +17,8 @@ #include #include "zenoh-pico.h" +#include "zenoh-pico/api/types.h" +#include "zenoh-pico/collections/string.h" #define MSG 1000 #define MSG_LEN 1024 @@ -95,6 +97,11 @@ void data_handler(const z_sample_t *sample, void *arg) { free(res); } +z_string_t format_id(const z_id_t *id) { + _z_bytes_t id_as_bytes = _z_bytes_wrap(id->id, _z_id_len(*id)); + return _z_string_from_bytes(&id_as_bytes); +} + int main(int argc, char **argv) { setvbuf(stdout, NULL, _IOLBF, 1024); @@ -110,7 +117,7 @@ int main(int argc, char **argv) { z_owned_session_t s1 = z_open(z_move(config)); assert(z_check(s1)); - z_string_t zid1 = _z_string_from_bytes(&z_loan(s1)._val->_local_zid); + z_string_t zid1 = format_id(&z_loan(s1)._val->_local_zid); printf("Session 1 with PID: %s\n", zid1.val); _z_string_clear(&zid1); @@ -125,7 +132,7 @@ int main(int argc, char **argv) { z_owned_session_t s2 = z_open(z_move(config)); assert(z_check(s2)); - z_string_t zid2 = _z_string_from_bytes(&z_loan(s2)._val->_local_zid); + z_string_t zid2 = format_id(&z_loan(s2)._val->_local_zid); printf("Session 2 with PID: %s\n", zid2.val); _z_string_clear(&zid2); @@ -140,7 +147,7 @@ int main(int argc, char **argv) { for (unsigned int i = 0; i < SET; i++) { snprintf(s1_res, 64, "%s%u", uri, i); z_owned_keyexpr_t expr = z_declare_keyexpr(z_loan(s1), z_keyexpr(s1_res)); - printf("Declared resource on session 1: %zu %s\n", z_loan(expr)._id, z_loan(expr)._suffix); + printf("Declared resource on session 1: %u %s\n", z_loan(expr)._id, z_loan(expr)._suffix); rids1[i] = expr; } @@ -149,7 +156,7 @@ int main(int argc, char **argv) { for (unsigned int i = 0; i < SET; i++) { snprintf(s1_res, 64, "%s%u", uri, i); z_owned_keyexpr_t expr = z_declare_keyexpr(z_loan(s2), z_keyexpr(s1_res)); - printf("Declared resource on session 2: %zu %s\n", z_loan(expr)._id, z_loan(expr)._suffix); + printf("Declared resource on session 2: %u %s\n", z_loan(expr)._id, z_loan(expr)._suffix); rids2[i] = expr; } @@ -161,7 +168,7 @@ int main(int argc, char **argv) { z_owned_subscriber_t *sub = (z_owned_subscriber_t *)z_malloc(sizeof(z_owned_subscriber_t)); *sub = z_declare_subscriber(z_loan(s2), z_loan(rids2[i]), &callback, NULL); assert(z_check(*sub)); - printf("Declared subscription on session 2: %zu %zu %s\n", z_subscriber_loan(sub)._val->_id, + printf("Declared subscription on session 2: %zu %u %s\n", z_subscriber_loan(sub)._val->_id, z_loan(rids2[i])._id, ""); subs2 = _z_list_push(subs2, sub); } @@ -201,7 +208,7 @@ int main(int argc, char **argv) { z_put_options_t opt = z_put_options_default(); opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; z_put(z_loan(s1), z_loan(rids1[i]), (const uint8_t *)payload, len, &opt); - printf("Wrote data from session 1: %zu %zu b\t(%u/%u)\n", z_loan(rids1[i])._id, len, n * SET + (i + 1), + printf("Wrote data from session 1: %u %zu b\t(%u/%u)\n", z_loan(rids1[i])._id, len, n * SET + (i + 1), total); } } @@ -293,14 +300,14 @@ int main(int argc, char **argv) { // Undeclare resources on both sessions for (unsigned int i = 0; i < SET; i++) { - printf("Undeclared resource on session 1: %zu\n", z_loan(rids1[i])._id); + printf("Undeclared resource on session 1: %u\n", z_loan(rids1[i])._id); z_undeclare_keyexpr(z_loan(s1), z_move(rids1[i])); } z_sleep_s(SLEEP); for (unsigned int i = 0; i < SET; i++) { - printf("Undeclared resource on session 2: %zu\n", z_loan(rids2[i])._id); + printf("Undeclared resource on session 2: %u\n", z_loan(rids2[i])._id); z_undeclare_keyexpr(z_loan(s2), z_move(rids2[i])); } From 0d66e6e86dc568d51b218574635b866500e9c699 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 12 Jul 2023 10:11:09 +0200 Subject: [PATCH 121/181] apparently 50 years is not enough to get a decent formatting story... --- tests/z_msgcodec_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index a699bc2d7..f8532592a 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -374,7 +374,7 @@ _z_msg_ext_t gen_zint_extension(void) { } void assert_eq_zint_extension(_z_msg_ext_zint_t *left, _z_msg_ext_zint_t *right) { - printf(" ZINT (%zu:%zu), ", left->_val, right->_val); + printf(" ZINT (%ju:%ju), ", (uintmax_t)left->_val, (uintmax_t)right->_val); assert(left->_val == right->_val); } From e89610c034cf3decabb5ee3e8eee3828ce81fc1c Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 12 Jul 2023 10:21:26 +0200 Subject: [PATCH 122/181] fix -pedantic --- CMakeLists.txt | 2 +- examples/unix/c11/z_ping.c | 2 +- examples/unix/c11/z_pong.c | 2 +- include/zenoh-pico/api/macros.h | 4 +- include/zenoh-pico/link/config/udp.h | 2 +- include/zenoh-pico/session/session.h | 4 +- tests/z_msgcodec_test.c | 134 +++++++++++++-------------- 7 files changed, 75 insertions(+), 75 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 042c2855a..ba9a41fa1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -106,7 +106,7 @@ find_package(Threads REQUIRED) if(CMAKE_BUILD_TYPE_UPPER MATCHES "DEBUG") if(UNIX) - add_compile_options(-c -Wall -Wextra -Werror -Wunused -Wstrict-prototypes -pipe -g -O0) + add_compile_options(-c -Wall -Wextra -Werror -Wunused -Wstrict-prototypes -pedantic -pipe -g -O0) elseif(MSVC) add_compile_options(/W4 /WX /Od) elseif(CMAKE_SYSTEM_NAME MATCHES "Generic") diff --git a/examples/unix/c11/z_ping.c b/examples/unix/c11/z_ping.c index 2472196fe..eee910620 100644 --- a/examples/unix/c11/z_ping.c +++ b/examples/unix/c11/z_ping.c @@ -125,4 +125,4 @@ struct args_t parse_args(int argc, char** argv) { .number_of_pings = number_of_pings, .warmup_ms = warmup_ms, }; -} \ No newline at end of file +} diff --git a/examples/unix/c11/z_pong.c b/examples/unix/c11/z_pong.c index 7f0731478..d1fa347a4 100644 --- a/examples/unix/c11/z_pong.c +++ b/examples/unix/c11/z_pong.c @@ -28,4 +28,4 @@ int main(int argc, char** argv) { } z_drop(z_move(sub)); z_close(z_move(session)); -} \ No newline at end of file +} diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 23ed7eb21..f6b6077fb 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -230,7 +230,7 @@ * Returns: * Returns the new closure. */ -#define z_closure(...) _z_closure_overloader(__VA_ARGS__, 0, 0) +#define z_closure(...) _z_closure_overloader(__VA_ARGS__, 0, 0, 0) #else @@ -343,4 +343,4 @@ inline void z_call(const z_owned_closure_zid_t &closure, const z_id_t *zid) #endif /* ZENOH_C_STANDARD != 99 */ -#endif /* ZENOH_PICO_API_MACROS_H */ \ No newline at end of file +#endif /* ZENOH_PICO_API_MACROS_H */ diff --git a/include/zenoh-pico/link/config/udp.h b/include/zenoh-pico/link/config/udp.h index e7a4dffc9..36e5b08c9 100644 --- a/include/zenoh-pico/link/config/udp.h +++ b/include/zenoh-pico/link/config/udp.h @@ -44,4 +44,4 @@ int8_t _z_udp_config_from_str(_z_str_intmap_t *strint, const char *s); int8_t _z_udp_config_from_strn(_z_str_intmap_t *strint, const char *s, size_t n); #endif -#endif /* ZENOH_PICO_LINK_CONFIG_UDP_H */ \ No newline at end of file +#endif /* ZENOH_PICO_LINK_CONFIG_UDP_H */ diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index 45830d762..808cb69c5 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -99,7 +99,7 @@ typedef struct { _Bool _z_subscription_eq(const _z_subscription_t *one, const _z_subscription_t *two); void _z_subscription_clear(_z_subscription_t *sub); -_Z_POINTER_DEFINE(_z_subscription, _z_subscription); +_Z_POINTER_DEFINE(_z_subscription, _z_subscription) _Z_ELEM_DEFINE(_z_subscriber, _z_subscription_t, _z_noop_size, _z_subscription_clear, _z_noop_copy) _Z_ELEM_DEFINE(_z_subscription_sptr, _z_subscription_sptr_t, _z_noop_size, _z_subscription_sptr_drop, _z_noop_copy) _Z_LIST_DEFINE(_z_subscription_sptr, _z_subscription_sptr_t) @@ -126,7 +126,7 @@ typedef struct { _Bool _z_questionable_eq(const _z_questionable_t *one, const _z_questionable_t *two); void _z_questionable_clear(_z_questionable_t *res); -_Z_POINTER_DEFINE(_z_questionable, _z_questionable); +_Z_POINTER_DEFINE(_z_questionable, _z_questionable) _Z_ELEM_DEFINE(_z_questionable, _z_questionable_t, _z_noop_size, _z_questionable_clear, _z_noop_copy) _Z_ELEM_DEFINE(_z_questionable_sptr, _z_questionable_sptr_t, _z_noop_size, _z_questionable_sptr_drop, _z_noop_copy) _Z_LIST_DEFINE(_z_questionable_sptr, _z_questionable_sptr_t) diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index f8532592a..3e483eede 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -703,7 +703,7 @@ void resource_declaration(void) { /*------------------ Subscriber declaration ------------------*/ _z_decl_subscriber_t gen_subscriber_declaration(void) { - __auto_type subinfo = gen_subinfo(); + _z_subinfo_t subinfo = gen_subinfo(); _z_decl_subscriber_t e_sd = {._keyexpr = gen_keyexpr(), ._id = gen_uint64(), ._ext_subinfo = {._pull_mode = subinfo.mode == Z_SUBMODE_PULL, @@ -1070,7 +1070,7 @@ void push_body_message(void) { _z_wbuf_t wbf = gen_wbuf(65535); // Initialize - __auto_type e_da = gen_push_body(); + _z_push_body_t e_da = gen_push_body(); // Encode int8_t res = _z_push_body_encode(&wbf, &e_da); @@ -1102,13 +1102,13 @@ void assert_eq_pull_message(_z_msg_pull_t *left, _z_msg_pull_t *right) { void pull_message(void) { printf("\n>> Pull message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type e_pull_msg = gen_pull_message(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_msg_pull_t e_pull_msg = gen_pull_message(); assert(_z_pull_encode(&wbf, &e_pull_msg) == _Z_RES_OK); - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); - __auto_type header = _z_zbuf_read(&zbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + uint8_t header = _z_zbuf_read(&zbf); _z_msg_pull_t d_pull_msg; assert(_z_pull_decode(&d_pull_msg, &zbf, header) == _Z_RES_OK); @@ -1138,13 +1138,13 @@ void assert_eq_query(const _z_msg_query_t *left, const _z_msg_query_t *right) { void query_message(void) { printf("\n>> Query message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_query(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_msg_query_t expected = gen_query(); assert(_z_query_encode(&wbf, &expected) == _Z_RES_OK); _z_msg_query_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); - __auto_type header = _z_zbuf_read(&zbf); - __auto_type res = _z_query_decode(&decoded, &zbf, header); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + uint8_t header = _z_zbuf_read(&zbf); + int8_t res = _z_query_decode(&decoded, &zbf, header); assert(_Z_RES_OK == res); assert_eq_query(&expected, &decoded); _z_msg_query_clear(&decoded); @@ -1173,12 +1173,12 @@ void assert_eq_err(const _z_msg_err_t *left, const _z_msg_err_t *right) { void err_message(void) { printf("\n>> Err message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_err(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_msg_err_t expected = gen_err(); assert(_z_err_encode(&wbf, &expected) == _Z_RES_OK); _z_msg_err_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); - __auto_type header = _z_zbuf_read(&zbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + uint8_t header = _z_zbuf_read(&zbf); assert(_Z_RES_OK == _z_err_decode(&decoded, &zbf, header)); assert_eq_err(&expected, &decoded); _z_msg_err_clear(&decoded); @@ -1201,12 +1201,12 @@ void assert_eq_ack(const _z_msg_ack_t *left, const _z_msg_ack_t *right) { void ack_message(void) { printf("\n>> Ack message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_ack(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_msg_ack_t expected = gen_ack(); assert(_z_ack_encode(&wbf, &expected) == _Z_RES_OK); _z_msg_ack_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); - __auto_type header = _z_zbuf_read(&zbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + uint8_t header = _z_zbuf_read(&zbf); assert(_Z_RES_OK == _z_ack_decode(&decoded, &zbf, header)); assert_eq_ack(&expected, &decoded); _z_zbuf_clear(&zbf); @@ -1231,12 +1231,12 @@ void assert_eq_reply(const _z_msg_reply_t *left, const _z_msg_reply_t *right) { void reply_message(void) { printf("\n>> Reply message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_reply(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_msg_reply_t expected = gen_reply(); assert(_z_reply_encode(&wbf, &expected) == _Z_RES_OK); _z_msg_reply_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); - __auto_type header = _z_zbuf_read(&zbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + uint8_t header = _z_zbuf_read(&zbf); assert(_Z_RES_OK == _z_reply_decode(&decoded, &zbf, header)); assert_eq_reply(&expected, &decoded); _z_msg_reply_clear(&decoded); @@ -1263,12 +1263,12 @@ void assert_eq_push(const _z_n_msg_push_t *left, const _z_n_msg_push_t *right) { void push_message(void) { printf("\n>> Push message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_push(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_n_msg_push_t expected = gen_push(); assert(_z_push_encode(&wbf, &expected) == _Z_RES_OK); _z_n_msg_push_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); - __auto_type header = _z_zbuf_read(&zbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + uint8_t header = _z_zbuf_read(&zbf); assert(_Z_RES_OK == _z_push_decode(&decoded, &zbf, header)); assert_eq_push(&expected, &decoded); _z_n_msg_push_clear(&decoded); @@ -1340,12 +1340,12 @@ void assert_eq_request(const _z_n_msg_request_t *left, const _z_n_msg_request_t void request_message(void) { printf("\n>> Request message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_request(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_n_msg_request_t expected = gen_request(); assert(_z_request_encode(&wbf, &expected) == _Z_RES_OK); _z_n_msg_request_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); - __auto_type header = _z_zbuf_read(&zbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + uint8_t header = _z_zbuf_read(&zbf); int8_t ret = _z_request_decode(&decoded, &zbf, header); assert(_Z_RES_OK == ret); assert_eq_request(&expected, &decoded); @@ -1377,7 +1377,7 @@ _z_n_msg_response_t gen_response(void) { ret._body._reply = gen_reply(); } break; default: { - __auto_type body = gen_push_body(); + _z_push_body_t body = gen_push_body(); if (body._is_put) { ret._tag = _Z_RESPONSE_BODY_PUT; ret._body._put = body._body._put; @@ -1421,12 +1421,12 @@ void assert_eq_response(const _z_n_msg_response_t *left, const _z_n_msg_response void response_message(void) { printf("\n>> Response message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_response(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_n_msg_response_t expected = gen_response(); assert(_z_response_encode(&wbf, &expected) == _Z_RES_OK); _z_n_msg_response_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); - __auto_type header = _z_zbuf_read(&zbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + uint8_t header = _z_zbuf_read(&zbf); int8_t ret = _z_response_decode(&decoded, &zbf, header); assert(_Z_RES_OK == ret); assert_eq_response(&expected, &decoded); @@ -1442,12 +1442,12 @@ void assert_eq_response_final(const _z_n_msg_response_final_t *left, const _z_n_ } void response_final_message(void) { printf("\n>> Request Final message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_response_final(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_n_msg_response_final_t expected = gen_response_final(); assert(_z_response_final_encode(&wbf, &expected) == _Z_RES_OK); _z_n_msg_response_final_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); - __auto_type header = _z_zbuf_read(&zbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); + uint8_t header = _z_zbuf_read(&zbf); int8_t ret = _z_response_final_decode(&decoded, &zbf, header); assert(_Z_RES_OK == ret); assert_eq_response_final(&expected, &decoded); @@ -1491,11 +1491,11 @@ void assert_eq_join(const _z_t_msg_join_t *left, const _z_t_msg_join_t *right) { } void join_message(void) { printf("\n>> Join message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_join(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_transport_message_t expected = gen_join(); assert(_z_join_encode(&wbf, expected._header, &expected._body._join) == _Z_RES_OK); _z_t_msg_join_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); int8_t ret = _z_join_decode(&decoded, &zbf, expected._header); assert(_Z_RES_OK == ret); assert_eq_join(&expected._body._join, &decoded); @@ -1523,11 +1523,11 @@ void assert_eq_init(const _z_t_msg_init_t *left, const _z_t_msg_init_t *right) { } void init_message(void) { printf("\n>> Init message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_init(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_transport_message_t expected = gen_init(); assert(_z_init_encode(&wbf, expected._header, &expected._body._init) == _Z_RES_OK); _z_t_msg_init_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); int8_t ret = _z_init_decode(&decoded, &zbf, expected._header); assert(_Z_RES_OK == ret); assert_eq_init(&expected._body._init, &decoded); @@ -1551,11 +1551,11 @@ void assert_eq_open(const _z_t_msg_open_t *left, const _z_t_msg_open_t *right) { } void open_message(void) { printf("\n>> open message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_open(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_transport_message_t expected = gen_open(); assert(_z_open_encode(&wbf, expected._header, &expected._body._open) == _Z_RES_OK); _z_t_msg_open_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); int8_t ret = _z_open_decode(&decoded, &zbf, expected._header); assert(_Z_RES_OK == ret); assert_eq_open(&expected._body._open, &decoded); @@ -1571,11 +1571,11 @@ void assert_eq_close(const _z_t_msg_close_t *left, const _z_t_msg_close_t *right } void close_message(void) { printf("\n>> close message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_close(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_transport_message_t expected = gen_close(); assert(_z_close_encode(&wbf, expected._header, &expected._body._close) == _Z_RES_OK); _z_t_msg_close_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); int8_t ret = _z_close_decode(&decoded, &zbf, expected._header); assert(_Z_RES_OK == ret); assert_eq_close(&expected._body._close, &decoded); @@ -1592,11 +1592,11 @@ void assert_eq_keep_alive(const _z_t_msg_keep_alive_t *left, const _z_t_msg_keep } void keep_alive_message(void) { printf("\n>> keep_alive message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_keep_alive(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_transport_message_t expected = gen_keep_alive(); assert(_z_keep_alive_encode(&wbf, expected._header, &expected._body._keep_alive) == _Z_RES_OK); _z_t_msg_keep_alive_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); int8_t ret = _z_keep_alive_decode(&decoded, &zbf, expected._header); assert(_Z_RES_OK == ret); assert_eq_keep_alive(&expected._body._keep_alive, &decoded); @@ -1669,11 +1669,11 @@ void assert_eq_frame(const _z_t_msg_frame_t *left, const _z_t_msg_frame_t *right } void frame_message(void) { printf("\n>> frame message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_frame(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_transport_message_t expected = gen_frame(); assert(_z_frame_encode(&wbf, expected._header, &expected._body._frame) == _Z_RES_OK); _z_t_msg_frame_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); int8_t ret = _z_frame_decode(&decoded, &zbf, expected._header); assert(_Z_RES_OK == ret); assert_eq_frame(&expected._body._frame, &decoded); @@ -1692,11 +1692,11 @@ void assert_eq_fragment(const _z_t_msg_fragment_t *left, const _z_t_msg_fragment } void fragment_message(void) { printf("\n>> fragment message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_fragment(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_transport_message_t expected = gen_fragment(); assert(_z_fragment_encode(&wbf, expected._header, &expected._body._fragment) == _Z_RES_OK); _z_t_msg_fragment_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); int8_t ret = _z_fragment_decode(&decoded, &zbf, expected._header); assert(_Z_RES_OK == ret); assert_eq_fragment(&expected._body._fragment, &decoded); @@ -1762,11 +1762,11 @@ void assert_eq_transport(const _z_transport_message_t *left, const _z_transport_ } void transport_message(void) { printf("\n>> transport message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_transport(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_transport_message_t expected = gen_transport(); assert(_z_transport_message_encode(&wbf, &expected) == _Z_RES_OK); _z_transport_message_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); int8_t ret = _z_transport_message_decode(&decoded, &zbf); assert(_Z_RES_OK == ret); assert_eq_transport(&expected, &decoded); @@ -1803,11 +1803,11 @@ void assert_eq_scouting(const _z_scouting_message_t *left, const _z_scouting_mes } void scouting_message(void) { printf("\n>> scouting message\n"); - __auto_type wbf = gen_wbuf(UINT16_MAX); - __auto_type expected = gen_scouting(); + _z_wbuf_t wbf = gen_wbuf(UINT16_MAX); + _z_scouting_message_t expected = gen_scouting(); assert(_z_scouting_message_encode(&wbf, &expected) == _Z_RES_OK); _z_scouting_message_t decoded; - __auto_type zbf = _z_wbuf_to_zbuf(&wbf); + _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); int8_t ret = _z_scouting_message_decode(&decoded, &zbf); assert(_Z_RES_OK == ret); assert_eq_scouting(&expected, &decoded); From 7009d4278e71867b0163698eed567283f5334052 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 12 Jul 2023 10:37:08 +0200 Subject: [PATCH 123/181] fix fprintzid --- examples/arduino/z_scout.ino | 9 +++++---- examples/espidf/z_scout.c | 9 +++++---- examples/mbed/z_scout.cpp | 9 +++++---- examples/unix/c99/z_scout.c | 9 +++++---- examples/windows/z_scout.c | 9 +++++---- examples/zephyr/z_scout.c | 9 +++++---- 6 files changed, 30 insertions(+), 24 deletions(-) diff --git a/examples/arduino/z_scout.ino b/examples/arduino/z_scout.ino index bc7e2f17d..c57608159 100644 --- a/examples/arduino/z_scout.ino +++ b/examples/arduino/z_scout.ino @@ -20,13 +20,14 @@ #define SSID "SSID" #define PASS "PASS" -void fprintzid(z_bytes_t zid) { - if (zid.start == NULL) { +void fprintzid(z_id_t zid) { + unsigned int zidlen = _z_id_len(zid); + if (zidlen == 0) { Serial.print("None"); } else { Serial.print("Some("); - for (unsigned int i = 0; i < zid.len; i++) { - Serial.print(zid.start[i], HEX); + for (unsigned int i = 0; i < zidlen; i++) { + Serial.print(zid.id[i], HEX); } Serial.print(")"); } diff --git a/examples/espidf/z_scout.c b/examples/espidf/z_scout.c index 96be38a7e..3b45a5a6a 100644 --- a/examples/espidf/z_scout.c +++ b/examples/espidf/z_scout.c @@ -86,13 +86,14 @@ void wifi_init_sta(void) { vEventGroupDelete(s_event_group_handler); } -void fprintzid(FILE *stream, z_bytes_t zid) { - if (zid.start == NULL) { +void fprintzid(FILE *stream, z_id_t zid) { + unsigned int zidlen = _z_id_len(zid); + if (zidlen == 0) { fprintf(stream, "None"); } else { fprintf(stream, "Some("); - for (unsigned int i = 0; i < zid.len; i++) { - fprintf(stream, "%02X", (int)zid.start[i]); + for (unsigned int i = 0; i < zidlen; i++) { + fprintf(stream, "%02X", (int)zid.id[i]); } fprintf(stream, ")"); } diff --git a/examples/mbed/z_scout.cpp b/examples/mbed/z_scout.cpp index 8d3ee55cc..809df76e3 100644 --- a/examples/mbed/z_scout.cpp +++ b/examples/mbed/z_scout.cpp @@ -16,13 +16,14 @@ #include #include -void fprintzid(FILE *stream, z_bytes_t zid) { - if (zid.start == NULL) { +void fprintzid(FILE *stream, z_id_t zid) { + unsigned int zidlen = _z_id_len(zid); + if (zidlen == 0) { fprintf(stream, "None"); } else { fprintf(stream, "Some("); - for (unsigned int i = 0; i < zid.len; i++) { - fprintf(stream, "%02X", (int)zid.start[i]); + for (unsigned int i = 0; i < zidlen; i++) { + fprintf(stream, "%02X", (int)zid.id[i]); } fprintf(stream, ")"); } diff --git a/examples/unix/c99/z_scout.c b/examples/unix/c99/z_scout.c index c2eb2096a..3dafdfad4 100644 --- a/examples/unix/c99/z_scout.c +++ b/examples/unix/c99/z_scout.c @@ -17,13 +17,14 @@ #include #include -void fprintzid(FILE *stream, z_bytes_t zid) { - if (zid.start == NULL) { +void fprintzid(FILE *stream, z_id_t zid) { + unsigned int zidlen = _z_id_len(zid); + if (zidlen == 0) { fprintf(stream, "None"); } else { fprintf(stream, "Some("); - for (unsigned int i = 0; i < zid.len; i++) { - fprintf(stream, "%02X", (int)zid.start[i]); + for (unsigned int i = 0; i < zidlen; i++) { + fprintf(stream, "%02X", (int)zid.id[i]); } fprintf(stream, ")"); } diff --git a/examples/windows/z_scout.c b/examples/windows/z_scout.c index 1d1590b02..8d7feea64 100644 --- a/examples/windows/z_scout.c +++ b/examples/windows/z_scout.c @@ -18,13 +18,14 @@ #include #include -void fprintzid(FILE *stream, z_bytes_t zid) { - if (zid.start == NULL) { +void fprintzid(FILE *stream, z_id_t zid) { + unsigned int zidlen = _z_id_len(zid); + if (zidlen == 0) { fprintf(stream, "None"); } else { fprintf(stream, "Some("); - for (unsigned int i = 0; i < zid.len; i++) { - fprintf(stream, "%02X", (int)zid.start[i]); + for (unsigned int i = 0; i < zidlen; i++) { + fprintf(stream, "%02X", (int)zid.id[i]); } fprintf(stream, ")"); } diff --git a/examples/zephyr/z_scout.c b/examples/zephyr/z_scout.c index 1c3e2d101..e9e221194 100644 --- a/examples/zephyr/z_scout.c +++ b/examples/zephyr/z_scout.c @@ -14,13 +14,14 @@ #include #include -void fprintzid(FILE *stream, z_bytes_t zid) { - if (zid.start == NULL) { +void fprintzid(FILE *stream, z_id_t zid) { + unsigned int zidlen = _z_id_len(zid); + if (zidlen == 0) { fprintf(stream, "None"); } else { fprintf(stream, "Some("); - for (unsigned int i = 0; i < zid.len; i++) { - fprintf(stream, "%02X", (int)zid.start[i]); + for (unsigned int i = 0; i < zidlen; i++) { + fprintf(stream, "%02X", (int)zid.id[i]); } fprintf(stream, ")"); } From 8f52d50ad7c781803f6c731189913d0f747f9d12 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 12 Jul 2023 11:37:44 +0200 Subject: [PATCH 124/181] so... C99 is too recent for MSVC... yup... :/ --- include/zenoh-pico/net/session.h | 2 +- include/zenoh-pico/session/resource.h | 2 +- include/zenoh-pico/session/session.h | 6 +++--- src/net/primitives.c | 2 +- src/session/queryable.c | 9 ++++++++- src/session/resource.c | 2 +- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/include/zenoh-pico/net/session.h b/include/zenoh-pico/net/session.h index 64e8d1ba6..51f7d39e2 100644 --- a/include/zenoh-pico/net/session.h +++ b/include/zenoh-pico/net/session.h @@ -51,7 +51,7 @@ typedef struct { // Session counters uint16_t _resource_id; - _z_zint_t _entity_id; + uint32_t _entity_id; _z_zint_t _pull_id; _z_zint_t _query_id; _z_zint_t _interest_id; diff --git a/include/zenoh-pico/session/resource.h b/include/zenoh-pico/session/resource.h index 85b26c2dc..339180ae0 100644 --- a/include/zenoh-pico/session/resource.h +++ b/include/zenoh-pico/session/resource.h @@ -20,7 +20,7 @@ #include "zenoh-pico/net/session.h" /*------------------ Entity ------------------*/ -_z_zint_t _z_get_entity_id(_z_session_t *zn); +uint32_t _z_get_entity_id(_z_session_t *zn); /*------------------ Resource ------------------*/ uint16_t _z_get_resource_id(_z_session_t *zn); diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index 808cb69c5..feb41d49a 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -89,7 +89,7 @@ typedef void (*_z_data_handler_t)(const _z_sample_t *sample, void *arg); typedef struct { _z_keyexpr_t _key; - _z_zint_t _id; + uint32_t _id; _z_data_handler_t _callback; _z_drop_handler_t _dropper; void *_arg; @@ -106,7 +106,7 @@ _Z_LIST_DEFINE(_z_subscription_sptr, _z_subscription_sptr_t) typedef struct { _z_keyexpr_t _key; - _z_zint_t _id; + uint32_t _id; } _z_publication_t; /** @@ -116,7 +116,7 @@ typedef void (*_z_questionable_handler_t)(const z_query_t *query, void *arg); typedef struct { _z_keyexpr_t _key; - _z_zint_t _id; + uint32_t _id; _z_questionable_handler_t _callback; _z_drop_handler_t _dropper; void *_arg; diff --git a/src/net/primitives.c b/src/net/primitives.c index 7b935da6c..cabfca3ca 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -50,7 +50,7 @@ void _z_scout(const z_what_t what, const _z_id_t zid, const char *locator, const /*------------------ Resource Declaration ------------------*/ uint16_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr) { - _z_zint_t ret = Z_RESOURCE_ID_NONE; + 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 diff --git a/src/session/queryable.c b/src/session/queryable.c index 1883e0f23..b8bc3aedd 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -119,7 +119,7 @@ _z_questionable_sptr_list_t *_z_get_questionable_by_key(_z_session_t *zn, const } _z_questionable_sptr_t *_z_register_questionable(_z_session_t *zn, _z_questionable_t *q) { - _Z_DEBUG(">>> Allocating queryable for (%zu:%s)\n", q->_key._id, q->_key._suffix); + _Z_DEBUG(">>> Allocating queryable for (%ju:%s)\n", (uintmax_t)q->_key._id, q->_key._suffix); _z_questionable_sptr_t *ret = NULL; #if Z_MULTI_THREAD == 1 @@ -159,7 +159,11 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons q._zn = zn; q._rid = qid; q._key = key; +#if defined(__STDC_NO_VLA__) || ((__STDC_VERSION__ < 201000L) && (defined(_WIN32) || defined(WIN32))) + char *params = z_malloc(query->_parameters.len + 1); +#else char params[query->_parameters.len + 1]; +#endif memcpy(params, query->_parameters.start, query->_parameters.len); params[query->_parameters.len] = 0; q._parameters = params; @@ -175,6 +179,9 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons _z_keyexpr_clear(&key); _z_questionable_sptr_list_free(&qles); +#if defined(__STDC_NO_VLA__) || ((__STDC_VERSION__ < 201000L) && (defined(_WIN32) || defined(WIN32))) + z_free(params); +#endif // Send the final reply // Create the final reply diff --git a/src/session/resource.c b/src/session/resource.c index 68859175e..df6ef7763 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -37,7 +37,7 @@ void _z_resource_free(_z_resource_t **res) { } /*------------------ Entity ------------------*/ -_z_zint_t _z_get_entity_id(_z_session_t *zn) { return zn->_entity_id++; } +uint32_t _z_get_entity_id(_z_session_t *zn) { return zn->_entity_id++; } uint16_t _z_get_resource_id(_z_session_t *zn) { return zn->_resource_id++; } From 5a33658b34e4a873df41ceba4be89ee6114cbc6f Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 12 Jul 2023 16:16:52 +0200 Subject: [PATCH 125/181] fix more lints --- examples/unix/c11/z_put.c | 14 +++++++------- include/zenoh-pico/net/query.h | 6 ++++-- include/zenoh-pico/net/resource.h | 4 +++- include/zenoh-pico/net/subscribe.h | 4 +++- src/net/resource.c | 12 +++--------- src/protocol/codec.c | 4 ++-- src/protocol/codec/ext.c | 5 ----- src/protocol/codec/message.c | 6 +++--- src/protocol/codec/network.c | 22 +++++++++++++++++----- src/session/subscription.c | 2 +- src/system/windows/system.c | 3 ++- src/utils/uuid.c | 3 ++- tests/z_msgcodec_test.c | 10 ++++++++++ 13 files changed, 57 insertions(+), 38 deletions(-) diff --git a/examples/unix/c11/z_put.c b/examples/unix/c11/z_put.c index 29807cc8d..9a7d70195 100644 --- a/examples/unix/c11/z_put.c +++ b/examples/unix/c11/z_put.c @@ -72,20 +72,20 @@ int main(int argc, char **argv) { } printf("Declaring key expression '%s'...\n", keyexpr); - z_owned_keyexpr_t ke = z_declare_keyexpr(z_loan(s), z_keyexpr(keyexpr)); - if (!z_check(ke)) { - printf("Unable to declare key expression!\n"); - return -1; - } + // z_owned_keyexpr_t ke = z_declare_keyexpr(z_loan(s), z_keyexpr(keyexpr)); + // if (!z_check(ke)) { + // printf("Unable to declare key expression!\n"); + // return -1; + // } printf("Putting Data ('%s': '%s')...\n", keyexpr, value); z_put_options_t options = z_put_options_default(); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); - if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)value, strlen(value), &options) < 0) { + if (z_put(z_loan(s), z_keyexpr(keyexpr), (const uint8_t *)value, strlen(value), &options) < 0) { printf("Oh no! Put has failed...\n"); } - z_undeclare_keyexpr(z_loan(s), z_move(ke)); + // z_undeclare_keyexpr(z_loan(s), z_move(ke)); // Stop read and lease tasks for zenoh-pico zp_stop_read_task(z_loan(s)); diff --git a/include/zenoh-pico/net/query.h b/include/zenoh-pico/net/query.h index 3815860dc..bda52795f 100644 --- a/include/zenoh-pico/net/query.h +++ b/include/zenoh-pico/net/query.h @@ -14,6 +14,8 @@ #ifndef ZENOH_PICO_QUERY_NETAPI_H #define ZENOH_PICO_QUERY_NETAPI_H +#include + #include "zenoh-pico/api/constants.h" #include "zenoh-pico/protocol/core.h" @@ -23,7 +25,7 @@ typedef struct { _z_value_t _value; _z_keyexpr_t _key; - _z_zint_t _rid; + uint32_t _rid; void *_zn; // FIXME: _z_session_t *zn; char *_parameters; _Bool _anyke; @@ -33,7 +35,7 @@ typedef struct { * Return type when declaring a queryable. */ typedef struct { - _z_zint_t _id; + uint32_t _id; void *_zn; // FIXME: _z_session_t *zn; } _z_queryable_t; diff --git a/include/zenoh-pico/net/resource.h b/include/zenoh-pico/net/resource.h index 566c649d4..8636440ab 100644 --- a/include/zenoh-pico/net/resource.h +++ b/include/zenoh-pico/net/resource.h @@ -15,6 +15,8 @@ #ifndef ZENOH_PICO_RESOURCE_NETAPI_H #define ZENOH_PICO_RESOURCE_NETAPI_H +#include + #include "zenoh-pico/collections/string.h" #include "zenoh-pico/protocol/core.h" @@ -39,6 +41,6 @@ _z_keyexpr_t _z_rname(const char *rname); * Returns: * A :c:type:`_z_keyexpr_t` containing a new resource key. */ -_z_keyexpr_t _z_rid_with_suffix(_z_zint_t rid, const char *suffix); +_z_keyexpr_t _z_rid_with_suffix(uint16_t rid, const char *suffix); #endif /* ZENOH_PICO_RESOURCE_NETAPI_H */ diff --git a/include/zenoh-pico/net/subscribe.h b/include/zenoh-pico/net/subscribe.h index 48b0465b5..4e350148b 100644 --- a/include/zenoh-pico/net/subscribe.h +++ b/include/zenoh-pico/net/subscribe.h @@ -15,6 +15,8 @@ #ifndef ZENOH_PICO_SUBSCRIBE_NETAPI_H #define ZENOH_PICO_SUBSCRIBE_NETAPI_H +#include + #include "zenoh-pico/net/session.h" #include "zenoh-pico/protocol/core.h" @@ -22,7 +24,7 @@ * Return type when declaring a subscriber. */ typedef struct { - _z_zint_t _id; + uint32_t _id; _z_session_t *_zn; } _z_subscriber_t; diff --git a/src/net/resource.c b/src/net/resource.c index c0fc38dd0..c6c3bea58 100644 --- a/src/net/resource.c +++ b/src/net/resource.c @@ -15,19 +15,13 @@ #include "zenoh-pico/net/resource.h" #include +#include #include "zenoh-pico/protocol/core.h" -_z_keyexpr_t _z_rname(const char *rname) { - return (_z_keyexpr_t){ - ._id = Z_RESOURCE_ID_NONE, - ._uses_remote_mapping = false, - ._owns_suffix = false, - ._suffix = (char *)rname, - }; -} +_z_keyexpr_t _z_rname(const char *rname) { return _z_rid_with_suffix(0, rname); } -_z_keyexpr_t _z_rid_with_suffix(_z_zint_t rid, const char *suffix) { +_z_keyexpr_t _z_rid_with_suffix(uint16_t rid, const char *suffix) { return (_z_keyexpr_t){ ._id = rid, ._uses_remote_mapping = false, diff --git a/src/protocol/codec.c b/src/protocol/codec.c index f84b722b2..d1123bb16 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -239,7 +239,7 @@ int8_t _z_zint16_decode(uint16_t *zint, _z_zbuf_t *zbf) { _z_zint_t buf; _Z_RETURN_IF_ERR(_z_zint_decode(&buf, zbf)); if (buf <= UINT16_MAX) { - *zint = buf; + *zint = (uint16_t)buf; } else { ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } @@ -250,7 +250,7 @@ int8_t _z_zint32_decode(uint32_t *zint, _z_zbuf_t *zbf) { _z_zint_t buf; _Z_RETURN_IF_ERR(_z_zint_decode(&buf, zbf)); if (buf <= UINT32_MAX) { - *zint = buf; + *zint = (uint32_t)buf; } else { ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } diff --git a/src/protocol/codec/ext.c b/src/protocol/codec/ext.c index a5ce38e3f..62c43ed5f 100644 --- a/src/protocol/codec/ext.c +++ b/src/protocol/codec/ext.c @@ -217,11 +217,6 @@ int8_t _z_msg_ext_decode_iter(_z_zbuf_t *zbf, int8_t (*callback)(_z_msg_ext_t *, _z_msg_ext_t ext = _z_msg_ext_make_unit(0); size_t start = zbf->_ios._r_pos; ret |= _z_msg_ext_decode(&ext, zbf, &has_next); - printf("EXT (%d): ", ret); - for (; start < zbf->_ios._r_pos; start++) { - printf("%02x ", zbf->_ios._buf[start]); - } - printf("\n"); if (ret == _Z_RES_OK) { ret |= callback(&ext, context); _z_msg_ext_clear(&ext); diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 141294d67..811450658 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -205,7 +205,7 @@ int8_t _z_source_info_decode(_z_source_info_t *info, _z_zbuf_t *zbf) { if (ret == _Z_RES_OK) { ret = _z_zint_decode(&intbuf, zbf); if (intbuf <= UINT32_MAX) { - info->_entity_id = intbuf; + info->_entity_id = (uint32_t)intbuf; } else { ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } @@ -213,7 +213,7 @@ int8_t _z_source_info_decode(_z_source_info_t *info, _z_zbuf_t *zbf) { if (ret == _Z_RES_OK) { ret = _z_zint_decode(&intbuf, zbf); if (intbuf <= UINT32_MAX) { - info->_source_sn = intbuf; + info->_source_sn = (uint32_t)intbuf; } else { ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } @@ -620,7 +620,7 @@ int8_t _z_err_decode(_z_msg_err_t *err, _z_zbuf_t *zbf, uint8_t header) { _z_zint_t code; ret = _z_zint_decode(&code, zbf); if (code <= UINT16_MAX) { - err->_code = code; + err->_code = (uint16_t)code; } else { ret = _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index cac062fc5..8efba07b5 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -69,7 +69,10 @@ int8_t _z_push_decode_ext_cb(_z_msg_ext_t *extension, void *ctx) { _z_n_msg_push_t *msg = (_z_n_msg_push_t *)ctx; switch (_Z_EXT_FULL_ID(extension->_header)) { case _Z_MSG_EXT_ENC_ZINT | 0x01: { // QOS ext - msg->_qos = (_z_n_qos_t){._val = extension->_body._zint._val}; + 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}; break; } case _Z_MSG_EXT_ENC_ZBUF | 0x02: { // Timestamp ext @@ -167,7 +170,10 @@ int8_t _z_request_decode_extensions(_z_msg_ext_t *extension, void *ctx) { _z_n_msg_request_t *msg = (_z_n_msg_request_t *)ctx; switch (_Z_EXT_FULL_ID(extension->_header)) { case 0x01 | _Z_MSG_EXT_ENC_ZINT: { // QOS ext - msg->_ext_qos = (_z_n_qos_t){._val = extension->_body._zint._val}; + if (extension->_body._zint._val > UINT8_MAX) { + return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + msg->_ext_qos = (_z_n_qos_t){._val = (uint8_t)extension->_body._zint._val}; break; } case 0x02 | _Z_MSG_EXT_ENC_ZBUF: { // Timestamp ext @@ -176,16 +182,22 @@ int8_t _z_request_decode_extensions(_z_msg_ext_t *extension, void *ctx) { break; } case 0x04 | _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M: { - msg->_ext_target = extension->_body._zint._val; + msg->_ext_target = (uint8_t)extension->_body._zint._val; if (msg->_ext_target > 2) { return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } } break; case 0x05 | _Z_MSG_EXT_ENC_ZINT: { - msg->_ext_budget = extension->_body._zint._val; + if (extension->_body._zint._val > UINT32_MAX) { + return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + msg->_ext_budget = (uint32_t)extension->_body._zint._val; } break; case 0x06 | _Z_MSG_EXT_ENC_ZINT: { - msg->_ext_timeout_ms = extension->_body._zint._val; + if (extension->_body._zint._val > UINT32_MAX) { + return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; + } + msg->_ext_timeout_ms = (uint32_t)extension->_body._zint._val; } break; default: if ((extension->_header & _Z_MSG_EXT_FLAG_M) != 0) { diff --git a/src/session/subscription.c b/src/session/subscription.c index 6d03f2536..4f73eed97 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -122,7 +122,7 @@ _z_subscription_sptr_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8 } _z_subscription_sptr_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *s) { - _Z_DEBUG(">>> Allocating sub decl for (%zu:%s)\n", s->_key._id, s->_key._suffix); + _Z_DEBUG(">>> Allocating sub decl for (%ju:%s)\n", (uintmax_t)s->_key._id, s->_key._suffix); _z_subscription_sptr_t *ret = NULL; #if Z_MULTI_THREAD == 1 diff --git a/src/system/windows/system.c b/src/system/windows/system.c index 6441a3684..09491abda 100644 --- a/src/system/windows/system.c +++ b/src/system/windows/system.c @@ -12,6 +12,7 @@ // ZettaScale Zenoh Team, // +#include #include // The following includes must come after winsock2 #include @@ -46,7 +47,7 @@ uint64_t z_random_u64(void) { return ret; } -void z_random_fill(void *buf, size_t len) { RtlGenRandom(buf, len); } +void z_random_fill(void *buf, size_t len) { RtlGenRandom(buf, (ulong)len); } /*------------------ Memory ------------------*/ // #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) diff --git a/src/utils/uuid.c b/src/utils/uuid.c index e29596c74..7b9984871 100644 --- a/src/utils/uuid.c +++ b/src/utils/uuid.c @@ -14,6 +14,7 @@ #include "zenoh-pico/utils/uuid.h" +#include #include #include "zenoh-pico/utils/pointers.h" @@ -27,7 +28,7 @@ void _z_uuid_to_bytes(uint8_t *bytes, const char *uuid_str) { n_dash += 1; } char val[5] = {'0', 'x', uuid_str[i + n_dash], uuid_str[i + 1 + n_dash], '\0'}; - *bytes = strtol(val, NULL, 0); + *bytes = (uint8_t)strtoul(val, NULL, 0); bytes = _z_ptr_u8_offset(bytes, 1); } } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 3e483eede..b0d3612bb 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -47,6 +47,12 @@ #define _Z_MOCK_EXTENSION_ZINT 0x02 #define _Z_MOCK_EXTENSION_ZBUF 0x03 +#if defined(_WIN32) || defined(WIN32) +#pragma warning(push) +#pragma warning(disable : 4244 4267) // Disable truncation warnings in MSVC, + // as it is used voluntarily in this file when working with RNG +#endif + /*=============================*/ /* Helper functions */ /*=============================*/ @@ -1870,3 +1876,7 @@ int main(void) { return 0; } + +#if defined(_WIN32) || defined(WIN32) +#pragma warning(pop) +#endif From 1266efe337ea87df5b3efa44a866fa500e05da61 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 12 Jul 2023 16:52:47 +0200 Subject: [PATCH 126/181] more format fixes --- src/api/api.c | 11 ++++++----- src/protocol/codec/ext.c | 6 +++--- src/session/query.c | 3 ++- src/session/resource.c | 3 ++- tests/z_peer_multicast_test.c | 7 ++++--- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 673885989..6f2989b84 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -711,7 +712,7 @@ z_owned_keyexpr_t z_declare_keyexpr(z_session_t zs, z_keyexpr_t keyexpr) { key._value = (z_keyexpr_t *)z_malloc(sizeof(z_keyexpr_t)); if (key._value != NULL) { - _z_zint_t id = _z_declare_resource(zs._val, keyexpr); + uint16_t id = _z_declare_resource(zs._val, keyexpr); *key._value = _z_rid_with_suffix(id, NULL); } @@ -742,7 +743,7 @@ z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, con #endif // Z_MULTICAST_TRANSPORT == 1 _z_resource_t *r = _z_get_resource_by_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); if (r == NULL) { - _z_zint_t id = _z_declare_resource(zs._val, keyexpr); + uint16_t id = _z_declare_resource(zs._val, keyexpr); key = _z_rid_with_suffix(id, NULL); } #if Z_MULTICAST_TRANSPORT == 1 @@ -818,7 +819,7 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z #endif // Z_MULTICAST_TRANSPORT == 1 _z_resource_t *r = _z_get_resource_by_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); if (r == NULL) { - _z_zint_t id = _z_declare_resource(zs._val, keyexpr); + uint16_t id = _z_declare_resource(zs._val, keyexpr); key = _z_rid_with_suffix(id, NULL); } #if Z_MULTICAST_TRANSPORT == 1 @@ -845,7 +846,7 @@ z_owned_pull_subscriber_t z_declare_pull_subscriber(z_session_t zs, z_keyexpr_t z_keyexpr_t key = keyexpr; _z_resource_t *r = _z_get_resource_by_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); if (r == NULL) { - _z_zint_t id = _z_declare_resource(zs._val, keyexpr); + uint16_t id = _z_declare_resource(zs._val, keyexpr); key = _z_rid_with_suffix(id, NULL); } @@ -897,7 +898,7 @@ z_owned_queryable_t z_declare_queryable(z_session_t zs, z_keyexpr_t keyexpr, z_o #endif // Z_MULTICAST_TRANSPORT == 1 _z_resource_t *r = _z_get_resource_by_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); if (r == NULL) { - _z_zint_t id = _z_declare_resource(zs._val, keyexpr); + uint16_t id = _z_declare_resource(zs._val, keyexpr); key = _z_rid_with_suffix(id, NULL); } #if Z_MULTICAST_TRANSPORT == 1 diff --git a/src/protocol/codec/ext.c b/src/protocol/codec/ext.c index 62c43ed5f..b87fa5441 100644 --- a/src/protocol/codec/ext.c +++ b/src/protocol/codec/ext.c @@ -177,8 +177,8 @@ int8_t _z_msg_ext_unknown_error(_z_msg_ext_t *extension, uint8_t trace_id) { break; } case _Z_MSG_EXT_ENC_ZINT: { - _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZINT(%02x)\n", ext_id, - trace_id, extension->_body._zint._val); + _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZINT(%02jx)\n", ext_id, + trace_id, (uintmax_t)extension->_body._zint._val); break; } case _Z_MSG_EXT_ENC_ZBUF: { @@ -188,7 +188,7 @@ int8_t _z_msg_ext_unknown_error(_z_msg_ext_t *extension, uint8_t trace_id) { snprintf(hex + 2 * i, 3, "%02x", buf.start[i]); } _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZBUF(%.*s)\n", ext_id, - trace_id, buf.len * 2, hex); + trace_id, (int)buf.len * 2, hex); z_free(hex); break; } diff --git a/src/session/query.c b/src/session/query.c index 34ac9fb1f..92d007211 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -117,7 +117,8 @@ _z_pending_query_t *_z_get_pending_query_by_id(_z_session_t *zn, const _z_zint_t int8_t _z_register_pending_query(_z_session_t *zn, _z_pending_query_t *pen_qry) { int8_t ret = _Z_RES_OK; - _Z_DEBUG(">>> Allocating query for (%zu:%s,%s)\n", pen_qry->_key._id, pen_qry->_key._suffix, pen_qry->_parameters); + _Z_DEBUG(">>> Allocating query for (%ju:%s,%s)\n", (uintmax_t)pen_qry->_key._id, pen_qry->_key._suffix, + pen_qry->_parameters); #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); diff --git a/src/session/resource.c b/src/session/resource.c index df6ef7763..ce73c15ca 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -210,7 +210,8 @@ _z_keyexpr_t _z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t * int8_t _z_register_resource(_z_session_t *zn, uint8_t is_local, _z_resource_t *res) { int8_t ret = _Z_RES_OK; - _Z_DEBUG(">>> Allocating res decl for (%zu,%zu:%s)\n", res->_id, res->_key._id, res->_key._suffix); + _Z_DEBUG(">>> Allocating res decl for (%ju,%u:%s)\n", (uintmax_t)res->_id, (unsigned int)res->_key._id, + res->_key._suffix); #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 2056c1c86..2933d5bc4 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -107,8 +108,8 @@ int main(int argc, char **argv) { z_owned_subscriber_t *sub = (z_owned_subscriber_t *)z_malloc(sizeof(z_owned_subscriber_t)); *sub = z_declare_subscriber(z_loan(s2), z_keyexpr(s1_res), &callback, NULL); assert(z_check(*sub)); - printf("Declared subscription on session 2: %zu %zu %s\n", z_subscriber_loan(sub)._val->_id, (z_zint_t)0, - s1_res); + printf("Declared subscription on session 2: %ju %zu %s\n", (uintmax_t)z_subscriber_loan(sub)._val->_id, + (z_zint_t)0, s1_res); subs2 = _z_list_push(subs2, sub); } @@ -149,7 +150,7 @@ int main(int argc, char **argv) { // Undeclare subscribers and queryables on second session while (subs2) { z_owned_subscriber_t *sub = _z_list_head(subs2); - printf("Undeclared subscriber on session 2: %zu\n", z_subscriber_loan(sub)._val->_id); + printf("Undeclared subscriber on session 2: %ju\n", (uintmax_t)z_subscriber_loan(sub)._val->_id); z_undeclare_subscriber(z_move(*sub)); subs2 = _z_list_pop(subs2, _z_noop_elem_free, NULL); } From 872c3f284baa2a3e6edaf3eda86ebab2b4a0aede Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 12 Jul 2023 16:55:07 +0200 Subject: [PATCH 127/181] again --- tests/z_client_test.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/z_client_test.c b/tests/z_client_test.c index a48d482ea..510571471 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -168,7 +169,7 @@ int main(int argc, char **argv) { z_owned_subscriber_t *sub = (z_owned_subscriber_t *)z_malloc(sizeof(z_owned_subscriber_t)); *sub = z_declare_subscriber(z_loan(s2), z_loan(rids2[i]), &callback, NULL); assert(z_check(*sub)); - printf("Declared subscription on session 2: %zu %u %s\n", z_subscriber_loan(sub)._val->_id, + printf("Declared subscription on session 2: %ju %u %s\n", (uintmax_t)z_subscriber_loan(sub)._val->_id, z_loan(rids2[i])._id, ""); subs2 = _z_list_push(subs2, sub); } @@ -181,7 +182,7 @@ int main(int argc, char **argv) { z_owned_queryable_t *qle = (z_owned_queryable_t *)z_malloc(sizeof(z_owned_queryable_t)); *qle = z_declare_queryable(z_loan(s2), z_keyexpr(s1_res), &callback, NULL); assert(z_check(*qle)); - printf("Declared queryable on session 2: %zu %zu %s\n", qle->_value->_id, (z_zint_t)0, s1_res); + printf("Declared queryable on session 2: %ju %zu %s\n", (uintmax_t)qle->_value->_id, (z_zint_t)0, s1_res); qles2 = _z_list_push(qles2, qle); } @@ -282,7 +283,7 @@ int main(int argc, char **argv) { // Undeclare subscribers and queryables on second session while (subs2) { z_owned_subscriber_t *sub = _z_list_head(subs2); - printf("Undeclared subscriber on session 2: %zu\n", sub->_value->_id); + printf("Undeclared subscriber on session 2: %ju\n", (uintmax_t)sub->_value->_id); z_undeclare_subscriber(z_move(*sub)); subs2 = _z_list_pop(subs2, _z_noop_elem_free, NULL); } @@ -291,7 +292,7 @@ int main(int argc, char **argv) { while (qles2) { z_owned_queryable_t *qle = _z_list_head(qles2); - printf("Undeclared queryable on session 2: %zu\n", qle->_value->_id); + printf("Undeclared queryable on session 2: %ju\n", (uintmax_t)qle->_value->_id); z_undeclare_queryable(z_move(*qle)); qles2 = _z_list_pop(qles2, _z_noop_elem_free, NULL); } From c270dddb9182f83619ed242cd93ff5314611b2d6 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 12 Jul 2023 17:00:16 +0200 Subject: [PATCH 128/181] fix windows --- src/system/windows/system.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/system/windows/system.c b/src/system/windows/system.c index 09491abda..b813d6627 100644 --- a/src/system/windows/system.c +++ b/src/system/windows/system.c @@ -47,7 +47,7 @@ uint64_t z_random_u64(void) { return ret; } -void z_random_fill(void *buf, size_t len) { RtlGenRandom(buf, (ulong)len); } +void z_random_fill(void *buf, size_t len) { RtlGenRandom(buf, (unsigned long)len); } /*------------------ Memory ------------------*/ // #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) From d6fc495e091142fae3b6e0101158b1426c19f258 Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Wed, 26 Jul 2023 09:44:40 +0200 Subject: [PATCH 129/181] Fix integration CI --- tests/routed.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/routed.sh b/tests/routed.sh index 0558efb96..fa49b6e7e 100644 --- a/tests/routed.sh +++ b/tests/routed.sh @@ -34,7 +34,7 @@ if [ ! -f zenohd ]; then if [ -n "$ZENOH_BRANCH" ]; then git switch "$ZENOH_BRANCH" fi - cargo build + cargo build --lib --bin zenohd cp ./target/debug/zenohd "$TESTDIR"/ cd "$TESTDIR" || exit fi From a1d7086b74c0db3d59a6748cd31a97ea6528eb58 Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Wed, 26 Jul 2023 09:53:58 +0200 Subject: [PATCH 130/181] Fix publisher --- include/zenoh-pico/protocol/core.h | 2 +- include/zenoh-pico/protocol/keyexpr.h | 2 +- src/api/api.c | 21 ++------------------- src/net/primitives.c | 25 ++++++++++++++++++++++--- src/net/resource.c | 2 +- src/protocol/codec/declarations.c | 8 ++++---- src/protocol/codec/message.c | 1 - src/protocol/codec/network.c | 12 ++++++------ src/protocol/keyexpr.c | 4 ++-- src/session/resource.c | 4 ++-- tests/z_msgcodec_test.c | 4 ++-- 11 files changed, 43 insertions(+), 42 deletions(-) diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index bbf835d26..565061ed4 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -84,7 +84,7 @@ _Bool _z_timestamp_check(const _z_timestamp_t *stamp); */ typedef struct { uint16_t _id; - _Bool _uses_remote_mapping; + _Bool _sender_mapping; _Bool _owns_suffix; char *_suffix; } _z_keyexpr_t; diff --git a/include/zenoh-pico/protocol/keyexpr.h b/include/zenoh-pico/protocol/keyexpr.h index 49c088a13..4ce168295 100644 --- a/include/zenoh-pico/protocol/keyexpr.h +++ b/include/zenoh-pico/protocol/keyexpr.h @@ -30,7 +30,7 @@ _z_keyexpr_t _z_keyexpr_duplicate(_z_keyexpr_t src); _z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src); _z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src); static inline _z_keyexpr_t _z_keyexpr_null(void) { - return (_z_keyexpr_t){._id = 0, ._uses_remote_mapping = false, ._owns_suffix = false, ._suffix = NULL}; + return (_z_keyexpr_t){._id = 0, ._sender_mapping = false, ._owns_suffix = false, ._suffix = NULL}; } _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp); void _z_timestamp_clear(_z_timestamp_t *tstamp); diff --git a/src/api/api.c b/src/api/api.c index 6f2989b84..457a03e02 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -733,30 +733,13 @@ z_publisher_options_t z_publisher_options_default(void) { } z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, const z_publisher_options_t *options) { - z_keyexpr_t key = keyexpr; - - // TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition - // lacks a way to convey them to later-joining nodes. Thus, in the current version automatic - // resource declarations are only performed on unicast transports. -#if Z_MULTICAST_TRANSPORT == 1 - if (zs._val->_tp._type != _Z_TRANSPORT_MULTICAST_TYPE) { -#endif // Z_MULTICAST_TRANSPORT == 1 - _z_resource_t *r = _z_get_resource_by_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); - if (r == NULL) { - uint16_t id = _z_declare_resource(zs._val, keyexpr); - key = _z_rid_with_suffix(id, NULL); - } -#if Z_MULTICAST_TRANSPORT == 1 - } -#endif // Z_MULTICAST_TRANSPORT == 1 - z_publisher_options_t opt = z_publisher_options_default(); if (options != NULL) { opt.congestion_control = options->congestion_control; opt.priority = options->priority; } - - return (z_owned_publisher_t){._value = _z_declare_publisher(zs._val, key, opt.congestion_control, opt.priority)}; + return (z_owned_publisher_t){._value = + _z_declare_publisher(zs._val, keyexpr, opt.congestion_control, opt.priority)}; } int8_t z_undeclare_publisher(z_owned_publisher_t *pub) { diff --git a/src/net/primitives.c b/src/net/primitives.c index cabfca3ca..a8f75c2c6 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -22,6 +22,7 @@ #include "zenoh-pico/config.h" #include "zenoh-pico/net/logger.h" #include "zenoh-pico/net/memory.h" +#include "zenoh-pico/net/resource.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/definitions/declarations.h" #include "zenoh-pico/protocol/definitions/network.h" @@ -106,13 +107,31 @@ _z_publisher_t *_z_declare_publisher(_z_session_t *zn, _z_keyexpr_t keyexpr, z_c _z_publisher_t *ret = (_z_publisher_t *)z_malloc(sizeof(_z_publisher_t)); if (ret != NULL) { ret->_zn = zn; - ret->_key = _z_keyexpr_duplicate(keyexpr); ret->_id = _z_get_entity_id(zn); ret->_congestion_control = congestion_control; ret->_priority = priority; - // Build the declare message to send on the wire - _z_declare_resource(zn, keyexpr); + // TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition + // lacks a way to convey them to later-joining nodes. Thus, in the current version automatic + // resource declarations are only performed on unicast transports. +#if Z_MULTICAST_TRANSPORT == 1 + if (zn->_tp._type != _Z_TRANSPORT_MULTICAST_TYPE) { +#endif // Z_MULTICAST_TRANSPORT == 1 + + _z_resource_t *rs = _z_get_resource_by_key(zn, _Z_RESOURCE_IS_LOCAL, &keyexpr); + if (rs == NULL) { + uint16_t id = _z_declare_resource(zn, keyexpr); + ret->_key = _z_rid_with_suffix(id, NULL); + } else { + ret->_key = _z_keyexpr_duplicate(rs->_key); + } + +#if Z_MULTICAST_TRANSPORT == 1 + } +#endif // Z_MULTICAST_TRANSPORT == 1 + + // Always apply the sender mapping for publishers + ret->_key._sender_mapping = true; } return ret; diff --git a/src/net/resource.c b/src/net/resource.c index c6c3bea58..120ab3915 100644 --- a/src/net/resource.c +++ b/src/net/resource.c @@ -24,7 +24,7 @@ _z_keyexpr_t _z_rname(const char *rname) { return _z_rid_with_suffix(0, rname); _z_keyexpr_t _z_rid_with_suffix(uint16_t rid, const char *suffix) { return (_z_keyexpr_t){ ._id = rid, - ._uses_remote_mapping = false, + ._sender_mapping = false, ._owns_suffix = false, ._suffix = (char *)suffix, }; diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index cfccd81a5..5dccd122b 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -37,7 +37,7 @@ int8_t _z_decl_ext_keyexpr_encode(_z_wbuf_t *wbf, _z_keyexpr_t ke, _Bool has_nex uint8_t header = _Z_MSG_EXT_ENC_ZBUF | 0x0f | (has_next_ext ? _Z_FLAG_Z_Z : 0); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); uint32_t kelen = _z_keyexpr_has_suffix(ke) ? strlen(ke._suffix) : 0; - header = (ke._uses_remote_mapping ? 2 : 0) | (kelen != 0 ? 1 : 0); + header = (ke._sender_mapping ? 2 : 0) | (kelen != 0 ? 1 : 0); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, 1 + kelen + _z_zint_len(ke._id))); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, ke._id)); @@ -68,7 +68,7 @@ int8_t _z_decl_commons_encode(_z_wbuf_t *wbf, uint8_t header, _Bool has_extensio if (has_kesuffix) { header |= _Z_DECL_SUBSCRIBER_FLAG_N; } - if (!keyexpr._uses_remote_mapping) { + if (keyexpr._sender_mapping) { header |= _Z_DECL_SUBSCRIBER_FLAG_M; } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); @@ -213,7 +213,7 @@ int8_t _z_undecl_decode_extensions(_z_msg_ext_t *extension, void *ctx) { _z_zbuf_t *zbf = &_zbf; uint8_t header; _Z_RETURN_IF_ERR(_z_uint8_decode(&header, zbf)); - ke->_uses_remote_mapping = !_Z_HAS_FLAG(header, 2); + ke->_sender_mapping = _Z_HAS_FLAG(header, 2); _Z_RETURN_IF_ERR(_z_zint16_decode(&ke->_id, zbf)); if (_Z_HAS_FLAG(header, 1)) { size_t len = _z_zbuf_len(zbf); @@ -242,7 +242,7 @@ int8_t _z_undecl_trivial_decode(_z_zbuf_t *zbf, _z_keyexpr_t *_ext_keyexpr, uint } int8_t _z_decl_commons_decode(_z_zbuf_t *zbf, uint8_t header, _Bool *has_extensions, uint32_t *id, _z_keyexpr_t *ke) { *has_extensions = _Z_HAS_FLAG(header, _Z_FLAG_Z_Z); - ke->_uses_remote_mapping = _Z_HAS_FLAG(header, _Z_DECL_SUBSCRIBER_FLAG_M); + ke->_sender_mapping = _Z_HAS_FLAG(header, _Z_DECL_SUBSCRIBER_FLAG_M); _Z_RETURN_IF_ERR(_z_zint32_decode(id, zbf)); _Z_RETURN_IF_ERR(_z_zint16_decode(&ke->_id, zbf)); if (_Z_HAS_FLAG(header, _Z_DECL_SUBSCRIBER_FLAG_N)) { diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 811450658..713d8b8ac 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -59,7 +59,6 @@ int8_t _z_id_encode_as_zbytes(_z_wbuf_t *wbf, const _z_id_t *id) { uint8_t len = _z_id_len(*id); if (len != 0) { - printf("ZIDLEN: %d\n", len); _z_bytes_t buf = _z_bytes_wrap(id->id, len); ret = _z_bytes_encode(wbf, &buf); } else { diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index 8efba07b5..945d92448 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -37,7 +37,7 @@ /*------------------ Push Message ------------------*/ int8_t _z_push_encode(_z_wbuf_t *wbf, const _z_n_msg_push_t *msg) { - uint8_t header = _Z_MID_N_PUSH | (msg->_key._uses_remote_mapping ? _Z_FLAG_N_REQUEST_M : 0); + uint8_t header = _Z_MID_N_PUSH | (msg->_key._sender_mapping ? _Z_FLAG_N_REQUEST_M : 0); _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); _Bool has_timestamp_ext = _z_timestamp_check(&msg->_timestamp); if (has_suffix) { @@ -93,7 +93,7 @@ int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { *msg = (_z_n_msg_push_t){0}; msg->_qos = _Z_N_QOS_DEFAULT; ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N)); - msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_M); + msg->_key._sender_mapping = _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_M); if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_N_Z)) { ret = _z_msg_ext_decode_iter(zbf, _z_push_decode_ext_cb, msg); } @@ -109,7 +109,7 @@ int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { /*------------------ Request Message ------------------*/ int8_t _z_request_encode(_z_wbuf_t *wbf, const _z_n_msg_request_t *msg) { int8_t ret = _Z_RES_OK; - uint8_t header = _Z_MID_N_REQUEST | (msg->_key._uses_remote_mapping ? _Z_FLAG_N_REQUEST_M : 0); + uint8_t header = _Z_MID_N_REQUEST | (msg->_key._sender_mapping ? _Z_FLAG_N_REQUEST_M : 0); _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); if (has_suffix) { header |= _Z_FLAG_N_REQUEST_N; @@ -211,7 +211,7 @@ int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, const uint8_t msg->_ext_qos = _Z_N_QOS_DEFAULT; _Z_RETURN_IF_ERR(_z_zint_decode(&msg->_rid, zbf)); _Z_RETURN_IF_ERR(_z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N))); - msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_M); + msg->_key._sender_mapping = _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_M); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_request_decode_extensions, msg)); } @@ -250,7 +250,7 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { _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); _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); - if (msg->_key._uses_remote_mapping) { + if (msg->_key._sender_mapping) { _Z_SET_FLAG(header, _Z_FLAG_N_RESPONSE_M); } if (has_suffix) { @@ -354,7 +354,7 @@ int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t head *msg = (_z_n_msg_response_t){0}; msg->_ext_qos = _Z_N_QOS_DEFAULT; int8_t ret = _Z_RES_OK; - msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_M); + msg->_key._sender_mapping = _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_M); _Z_RETURN_IF_ERR(_z_zint_decode(&msg->_request_id, zbf)); _Z_RETURN_IF_ERR(_z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N))); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { diff --git a/src/protocol/keyexpr.c b/src/protocol/keyexpr.c index 675a90dd9..76f4b4efb 100644 --- a/src/protocol/keyexpr.c +++ b/src/protocol/keyexpr.c @@ -24,7 +24,7 @@ void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src) { dst->_id = src->_id; dst->_suffix = src->_suffix ? _z_str_clone(src->_suffix) : NULL; - dst->_uses_remote_mapping = src->_uses_remote_mapping; + dst->_sender_mapping = src->_sender_mapping; dst->_owns_suffix = true; } @@ -61,7 +61,7 @@ void _z_keyexpr_free(_z_keyexpr_t **rk) { _z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src) { return (_z_keyexpr_t){ ._id = src._id, - ._uses_remote_mapping = src._uses_remote_mapping, + ._sender_mapping = src._sender_mapping, ._owns_suffix = false, ._suffix = src._suffix, }; diff --git a/src/session/resource.c b/src/session/resource.c index ce73c15ca..bbca0364f 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -152,7 +152,7 @@ _z_resource_t *__unsafe_z_get_resource_by_id(_z_session_t *zn, uint8_t is_local, * - zn->_mutex_inner */ _z_resource_t *__unsafe_z_get_resource_by_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { - _z_resource_list_t *decls = !keyexpr->_uses_remote_mapping ? zn->_local_resources : zn->_remote_resources; + _z_resource_list_t *decls = keyexpr->_sender_mapping ? zn->_local_resources : zn->_remote_resources; return __z_get_resource_by_key(decls, keyexpr); } @@ -162,7 +162,7 @@ _z_resource_t *__unsafe_z_get_resource_by_key(_z_session_t *zn, const _z_keyexpr * - zn->_mutex_inner */ _z_keyexpr_t __unsafe_z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { - _z_resource_list_t *decls = keyexpr->_uses_remote_mapping ? zn->_remote_resources : zn->_local_resources; + _z_resource_list_t *decls = keyexpr->_sender_mapping ? zn->_local_resources : zn->_remote_resources; return __z_get_expanded_key_from_key(decls, keyexpr); } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index b0d3612bb..f57445994 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -612,7 +612,7 @@ _z_keyexpr_t gen_keyexpr(void) { key._suffix = gen_str(gen_zint() % 16); key._owns_suffix = true; } - key._uses_remote_mapping = gen_bool(); + key._sender_mapping = gen_bool(); return key; } @@ -873,7 +873,7 @@ void forget_subscriber_declaration(void) { // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); _z_undecl_subscriber_t d_fsd = { - ._id = 0, ._ext_keyexpr = {._id = 0, ._owns_suffix = false, ._suffix = NULL, ._uses_remote_mapping = false}}; + ._id = 0, ._ext_keyexpr = {._id = 0, ._owns_suffix = false, ._suffix = NULL, ._sender_mapping = false}}; uint8_t e_hdr = 0; _z_uint8_decode(&e_hdr, &zbf); res = _z_undecl_subscriber_decode(&d_fsd, &zbf, e_hdr); From 26f467cdb0d32311b90c0013ed1ace820d130215 Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Wed, 26 Jul 2023 10:04:58 +0200 Subject: [PATCH 131/181] Fix #232 --- tests/routed.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/routed.sh b/tests/routed.sh index fa49b6e7e..72659e075 100644 --- a/tests/routed.sh +++ b/tests/routed.sh @@ -46,7 +46,7 @@ for LOCATOR in $(echo "$LOCATORS" | xargs); do sleep 1 echo "> Running zenohd ... $LOCATOR" - RUST_LOG=debug ./zenohd -l "$LOCATOR" > zenohd."$1".log 2>&1 & + RUST_LOG=debug ./zenohd --plugin-search-dir "$TESTDIR/zenoh-git/target/debug" -l "$LOCATOR" > zenohd."$1".log 2>&1 & ZPID=$! sleep 5 From b9ccf6345dee34422c6d0cee9dd60a42a509c0b0 Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Wed, 26 Jul 2023 10:17:20 +0200 Subject: [PATCH 132/181] Fix API CI --- tests/api.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/api.sh b/tests/api.sh index cd84c0860..7be9183b3 100644 --- a/tests/api.sh +++ b/tests/api.sh @@ -34,7 +34,7 @@ if [ ! -f zenohd ]; then if [ -n "$ZENOH_BRANCH" ]; then git switch "$ZENOH_BRANCH" fi - cargo build + cargo build --lib --bin zenohd cp ./target/debug/zenohd "$TESTDIR"/ cd "$TESTDIR"|| exit fi @@ -46,7 +46,7 @@ for LOCATOR in $(echo "$LOCATORS" | xargs); do sleep 1 echo "> Running zenohd ... $LOCATOR" - RUST_LOG=debug ./zenohd -l "$LOCATOR" > zenohd."$1".log 2>&1 & + RUST_LOG=debug ./zenohd --plugin-search-dir "$TESTDIR/zenoh-git/target/debug" -l "$LOCATOR" > zenohd."$1".log 2>&1 & ZPID=$! sleep 5 From 65d9552859b17e6f0c87f01b81481b6816877605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 26 Jul 2023 11:21:46 +0200 Subject: [PATCH 133/181] Add pc files upon installation (#214) (#222) * Add pc files upon installation * Avoid hardcoded paths --- CMakeLists.txt | 31 +++++++++++++++++++++++++++++-- PackageConfig.cmake.in | 28 ++++++++++++++++++++++++++++ zenohpico.pc.in | 8 ++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 PackageConfig.cmake.in create mode 100644 zenohpico.pc.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 5c1a4c475..88e7dd133 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,9 @@ # cmake_minimum_required(VERSION 3.8) -project(libzenohpico VERSION 0.10.0.0 LANGUAGES C) +project(zenohpico VERSION 0.10.0.0 LANGUAGES C) + +include(CMakePackageConfigHelpers) option(BUILD_SHARED_LIBS "Build shared libraries if ON, otherwise build static libraries" ON) option(ZENOH_DEBUG "Use this to set the ZENOH_DEBUG variable." 0) @@ -217,6 +219,31 @@ install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/zenoh-pico COMPONENT Headers ) +set(CMAKE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/zenohpico") +# Generate Config.cmake +configure_package_config_file( + "PackageConfig.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/zenohpicoConfig.cmake" + INSTALL_DESTINATION "${CMAKE_INSTALL_CMAKEDIR}") + +# Generate Version.cmake +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/zenohpicoConfigVersion.cmake" + VERSION ${PROJECT_VERSION} + COMPATIBILITY SameMajorVersion) + +install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/zenohpicoConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/zenohpicoConfigVersion.cmake" + DESTINATION "${CMAKE_INSTALL_CMAKEDIR}" + CONFIGURATIONS ${configurations} + COMPONENT dev) + +if(UNIX) + configure_file("${CMAKE_SOURCE_DIR}/zenohpico.pc.in" "${CMAKE_SOURCE_DIR}/zenohpico.pc" @ONLY) + install(FILES "${CMAKE_SOURCE_DIR}/zenohpico.pc" CONFIGURATIONS Release RelWithDebInfo DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") +endif() + if(BUILD_EXAMPLES) add_subdirectory(examples) endif() @@ -363,4 +390,4 @@ if(PACKAGING) include(CPack) endif() -endif() +endif() \ No newline at end of file diff --git a/PackageConfig.cmake.in b/PackageConfig.cmake.in new file mode 100644 index 000000000..7ac56c378 --- /dev/null +++ b/PackageConfig.cmake.in @@ -0,0 +1,28 @@ +# +# Copyright (c) 2023 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, +# + +@PACKAGE_INIT@ + +add_library(__zenohpico_static STATIC IMPORTED GLOBAL) +add_library(zenohpico::static ALIAS __zenohpico_static) +set_property(TARGET __zenohpico_static PROPERTY IMPORTED_LOCATION "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@STATICLIB@") + +add_library(__zenohpico_lib SHARED IMPORTED GLOBAL) +add_library(zenohpico::lib ALIAS __zenohpico_lib) +set_target_properties(__zenohpico_lib PROPERTIES IMPORTED_NO_SONAME TRUE) +set_property(TARGET __zenohpico_lib PROPERTY IMPORTED_LOCATION "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@DYLIB@") +if(NOT ("@IMPLIB@" STREQUAL "")) + set_property(TARGET __zenohpico_lib PROPERTY IMPORTED_IMPLIB "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@/@IMPLIB@") +endif() +target_include_directories(__zenohpico_lib INTERFACE "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@") diff --git a/zenohpico.pc.in b/zenohpico.pc.in new file mode 100644 index 000000000..95b80270f --- /dev/null +++ b/zenohpico.pc.in @@ -0,0 +1,8 @@ +prefix=@CMAKE_INSTALL_PREFIX@ + +Name: @PROJECT_NAME@ +Description: @CMAKE_PROJECT_DESCRIPTION@ +URL: @CMAKE_PROJECT_HOMEPAGE_URL@ +Version: @PROJECT_VERSION@ +Cflags: -I${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ +Libs: -L${prefix}/@CMAKE_INSTALL_LIBDIR@ -lzenohpico@LIBNAME_POSTFIX@ \ No newline at end of file From 37397bf16406216784f51991fdc004533a3863fa Mon Sep 17 00:00:00 2001 From: Peter van der Perk <57130844+PetervdPerk-NXP@users.noreply.github.com> Date: Wed, 26 Jul 2023 05:55:34 -0400 Subject: [PATCH 134/181] Add POSIX target for small posix based RTOS's i.e. NuttX (#194) --- CMakeLists.txt | 18 ++++++++++++++---- src/system/unix/network.c | 2 ++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 88e7dd133..332bf0411 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ # Contributors: # ZettaScale Zenoh Team, # -cmake_minimum_required(VERSION 3.8) +cmake_minimum_required(VERSION 3.13) project(zenohpico VERSION 0.10.0.0 LANGUAGES C) @@ -63,8 +63,14 @@ if(NOT CMAKE_BUILD_TYPE) endif() string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE) + +set(CHECK_THREADS "ON") + if(CMAKE_SYSTEM_NAME MATCHES "Linux") add_definition(ZENOH_LINUX) +elseif(POSIX_COMPATIBLE) + add_definition(ZENOH_LINUX) + set(CHECK_THREADS "OFF") elseif(CMAKE_SYSTEM_NAME MATCHES "BSD") add_definition(ZENOH_BSD) elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin") @@ -101,7 +107,9 @@ if(SKBUILD) endif() set(THREADS_PREFER_PTHREAD_FLAG ON) -find_package(Threads REQUIRED) +if(CHECK_THREADS) + find_package(Threads REQUIRED) +endif() if(CMAKE_BUILD_TYPE MATCHES "DEBUG") if(UNIX) @@ -162,7 +170,7 @@ file(GLOB Sources "src/*.c" if(WITH_ZEPHYR) file (GLOB Sources_Zephyr "src/system/zephyr/*.c") list(APPEND Sources ${Sources_Zephyr}) -elseif(CMAKE_SYSTEM_NAME MATCHES "Linux" OR CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "BSD") +elseif(CMAKE_SYSTEM_NAME MATCHES "Linux" OR CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "BSD" OR POSIX_COMPATIBLE) file (GLOB Sources_Unix "src/system/unix/*.c") list(APPEND Sources ${Sources_Unix}) elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten") @@ -178,7 +186,9 @@ link_directories(${LIBRARY_OUTPUT_PATH}) target_sources(${Libname} PRIVATE ${Sources}) -target_link_libraries(${Libname} Threads::Threads) +if(CHECK_THREADS) + target_link_libraries(${Libname} Threads::Threads) +endif() if(CMAKE_SYSTEM_NAME MATCHES "Linux") target_link_libraries(${Libname} rt) diff --git a/src/system/unix/network.c b/src/system/unix/network.c index 8a71d1d82..cde8766e6 100644 --- a/src/system/unix/network.c +++ b/src/system/unix/network.c @@ -316,6 +316,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin ret = _Z_ERR_GENERIC; } +#ifndef UNIX_NO_MULTICAST_IF if (lsockaddr->sa_family == AF_INET) { if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, IPPROTO_IP, IP_MULTICAST_IF, &((struct sockaddr_in *)lsockaddr)->sin_addr, @@ -331,6 +332,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin } else { ret = _Z_ERR_GENERIC; } +#endif // Create lep endpoint if (ret == _Z_RES_OK) { From 87e21ad6005199a213bf0e597d01199b75c43b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 26 Jul 2023 11:56:35 +0200 Subject: [PATCH 135/181] Improve Zephyr version detection (#229) (#230) * Improve Zephyr version detection * Support the new Zephyr multicast API * Reuse ifa variable --- extra_script.py | 2 +- include/zenoh-pico/system/platform/zephyr.h | 7 ++++++- src/system/zephyr/network.c | 22 +++++++++++++++++++-- src/system/zephyr/system.c | 6 +++--- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/extra_script.py b/extra_script.py index c454607cc..391eb8047 100644 --- a/extra_script.py +++ b/extra_script.py @@ -28,7 +28,7 @@ "-", "-", "-"] - CPPDEFINES = ["ZENOH_ZEPHYR", "ZENOH_PIO"] + CPPDEFINES = ["ZENOH_ZEPHYR"] elif FRAMEWORK == 'arduino': PLATFORM = env.get("PIOPLATFORM") diff --git a/include/zenoh-pico/system/platform/zephyr.h b/include/zenoh-pico/system/platform/zephyr.h index ca3924e11..4b318c9d9 100644 --- a/include/zenoh-pico/system/platform/zephyr.h +++ b/include/zenoh-pico/system/platform/zephyr.h @@ -15,9 +15,14 @@ #ifndef ZENOH_PICO_SYSTEM_ZEPHYR_TYPES_H #define ZENOH_PICO_SYSTEM_ZEPHYR_TYPES_H -#if defined(ZENOH_PIO) +#include + +#if KERNEL_VERSION_MAJOR == 2 #include +#elif KERNEL_VERSION_MAJOR == 3 +#include #else +#pragma "This Zephyr version might not be supported." #include #endif diff --git a/src/system/zephyr/network.c b/src/system/zephyr/network.c index 0f34432d8..f0842dc08 100644 --- a/src/system/zephyr/network.c +++ b/src/system/zephyr/network.c @@ -12,7 +12,9 @@ // ZettaScale Zenoh Team, // -#if defined(ZENOH_PIO) +#include + +#if KERNEL_VERSION_MAJOR == 2 #include #else #include @@ -404,14 +406,22 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo if (!mcast) { ret = _Z_ERR_GENERIC; } +#if KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR > 3 + net_if_ipv4_maddr_join(ifa, mcast); +#else net_if_ipv4_maddr_join(mcast); +#endif } else if (rep._iptcp->ai_family == AF_INET6) { struct net_if_mcast_addr *mcast = NULL; mcast = net_if_ipv6_maddr_add(ifa, &((struct sockaddr_in6 *)rep._iptcp->ai_addr)->sin6_addr); if (!mcast) { ret = _Z_ERR_GENERIC; } +#if KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR > 3 + net_if_ipv6_maddr_join(ifa, mcast); +#else net_if_ipv6_maddr_join(mcast); +#endif } else { ret = _Z_ERR_GENERIC; } @@ -439,7 +449,11 @@ void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t * if (rep._iptcp->ai_family == AF_INET) { mcast = net_if_ipv4_maddr_add(ifa, &((struct sockaddr_in *)rep._iptcp->ai_addr)->sin_addr); if (mcast != NULL) { +#if KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR > 3 + net_if_ipv4_maddr_leave(ifa, mcast); +#else net_if_ipv4_maddr_leave(mcast); +#endif net_if_ipv4_maddr_rm(ifa, &((struct sockaddr_in *)rep._iptcp->ai_addr)->sin_addr); } else { // Do nothing. The socket will be closed in any case. @@ -447,7 +461,11 @@ void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t * } else if (rep._iptcp->ai_family == AF_INET6) { mcast = net_if_ipv6_maddr_add(ifa, &((struct sockaddr_in6 *)rep._iptcp->ai_addr)->sin6_addr); if (mcast != NULL) { +#if KERNEL_VERSION_MAJOR == 3 && KERNEL_VERSION_MINOR > 3 + net_if_ipv6_maddr_leave(ifa, mcast); +#else net_if_ipv6_maddr_leave(mcast); +#endif net_if_ipv6_maddr_rm(ifa, &((struct sockaddr_in6 *)rep._iptcp->ai_addr)->sin6_addr); } else { // Do nothing. The socket will be closed in any case. @@ -531,7 +549,7 @@ size_t _z_send_udp_multicast(const _z_sys_net_socket_t sock, const uint8_t *ptr, _z_sys_net_endpoint_t rep) { return sendto(sock._fd, ptr, len, 0, rep._iptcp->ai_addr, rep._iptcp->ai_addrlen); } -#endif +#endif // Z_LINK_UDP_MULTICAST == 1 #if Z_LINK_SERIAL == 1 int8_t _z_open_serial_from_pins(_z_sys_net_socket_t *sock, uint32_t txpin, uint32_t rxpin, uint32_t baudrate) { diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index 37c2333f1..2670ae9cb 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -12,11 +12,11 @@ // ZettaScale Zenoh Team, // -#if defined(ZENOH_PIO) -#include +#include + +#if KERNEL_VERSION_MAJOR == 2 #include #else -#include #include #endif From 2e7a005c8f3cc9e4d83200283a1164331ba329e6 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 28 Jul 2023 10:15:16 +0200 Subject: [PATCH 136/181] Revert "Fix publisher" This reverts commit a1d7086b74c0db3d59a6748cd31a97ea6528eb58. --- include/zenoh-pico/protocol/core.h | 2 +- include/zenoh-pico/protocol/keyexpr.h | 2 +- src/api/api.c | 21 +++++++++++++++++++-- src/net/primitives.c | 25 +++---------------------- src/net/resource.c | 2 +- src/protocol/codec/declarations.c | 8 ++++---- src/protocol/codec/message.c | 1 + src/protocol/codec/network.c | 12 ++++++------ src/protocol/keyexpr.c | 4 ++-- src/session/resource.c | 4 ++-- tests/z_msgcodec_test.c | 4 ++-- 11 files changed, 42 insertions(+), 43 deletions(-) diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 565061ed4..bbf835d26 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -84,7 +84,7 @@ _Bool _z_timestamp_check(const _z_timestamp_t *stamp); */ typedef struct { uint16_t _id; - _Bool _sender_mapping; + _Bool _uses_remote_mapping; _Bool _owns_suffix; char *_suffix; } _z_keyexpr_t; diff --git a/include/zenoh-pico/protocol/keyexpr.h b/include/zenoh-pico/protocol/keyexpr.h index 4ce168295..49c088a13 100644 --- a/include/zenoh-pico/protocol/keyexpr.h +++ b/include/zenoh-pico/protocol/keyexpr.h @@ -30,7 +30,7 @@ _z_keyexpr_t _z_keyexpr_duplicate(_z_keyexpr_t src); _z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src); _z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src); static inline _z_keyexpr_t _z_keyexpr_null(void) { - return (_z_keyexpr_t){._id = 0, ._sender_mapping = false, ._owns_suffix = false, ._suffix = NULL}; + return (_z_keyexpr_t){._id = 0, ._uses_remote_mapping = false, ._owns_suffix = false, ._suffix = NULL}; } _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp); void _z_timestamp_clear(_z_timestamp_t *tstamp); diff --git a/src/api/api.c b/src/api/api.c index 457a03e02..6f2989b84 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -733,13 +733,30 @@ z_publisher_options_t z_publisher_options_default(void) { } z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, const z_publisher_options_t *options) { + z_keyexpr_t key = keyexpr; + + // TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition + // lacks a way to convey them to later-joining nodes. Thus, in the current version automatic + // resource declarations are only performed on unicast transports. +#if Z_MULTICAST_TRANSPORT == 1 + if (zs._val->_tp._type != _Z_TRANSPORT_MULTICAST_TYPE) { +#endif // Z_MULTICAST_TRANSPORT == 1 + _z_resource_t *r = _z_get_resource_by_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); + if (r == NULL) { + uint16_t id = _z_declare_resource(zs._val, keyexpr); + key = _z_rid_with_suffix(id, NULL); + } +#if Z_MULTICAST_TRANSPORT == 1 + } +#endif // Z_MULTICAST_TRANSPORT == 1 + z_publisher_options_t opt = z_publisher_options_default(); if (options != NULL) { opt.congestion_control = options->congestion_control; opt.priority = options->priority; } - return (z_owned_publisher_t){._value = - _z_declare_publisher(zs._val, keyexpr, opt.congestion_control, opt.priority)}; + + return (z_owned_publisher_t){._value = _z_declare_publisher(zs._val, key, opt.congestion_control, opt.priority)}; } int8_t z_undeclare_publisher(z_owned_publisher_t *pub) { diff --git a/src/net/primitives.c b/src/net/primitives.c index a8f75c2c6..cabfca3ca 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -22,7 +22,6 @@ #include "zenoh-pico/config.h" #include "zenoh-pico/net/logger.h" #include "zenoh-pico/net/memory.h" -#include "zenoh-pico/net/resource.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/definitions/declarations.h" #include "zenoh-pico/protocol/definitions/network.h" @@ -107,31 +106,13 @@ _z_publisher_t *_z_declare_publisher(_z_session_t *zn, _z_keyexpr_t keyexpr, z_c _z_publisher_t *ret = (_z_publisher_t *)z_malloc(sizeof(_z_publisher_t)); if (ret != NULL) { ret->_zn = zn; + ret->_key = _z_keyexpr_duplicate(keyexpr); ret->_id = _z_get_entity_id(zn); ret->_congestion_control = congestion_control; ret->_priority = priority; - // TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition - // lacks a way to convey them to later-joining nodes. Thus, in the current version automatic - // resource declarations are only performed on unicast transports. -#if Z_MULTICAST_TRANSPORT == 1 - if (zn->_tp._type != _Z_TRANSPORT_MULTICAST_TYPE) { -#endif // Z_MULTICAST_TRANSPORT == 1 - - _z_resource_t *rs = _z_get_resource_by_key(zn, _Z_RESOURCE_IS_LOCAL, &keyexpr); - if (rs == NULL) { - uint16_t id = _z_declare_resource(zn, keyexpr); - ret->_key = _z_rid_with_suffix(id, NULL); - } else { - ret->_key = _z_keyexpr_duplicate(rs->_key); - } - -#if Z_MULTICAST_TRANSPORT == 1 - } -#endif // Z_MULTICAST_TRANSPORT == 1 - - // Always apply the sender mapping for publishers - ret->_key._sender_mapping = true; + // Build the declare message to send on the wire + _z_declare_resource(zn, keyexpr); } return ret; diff --git a/src/net/resource.c b/src/net/resource.c index 120ab3915..c6c3bea58 100644 --- a/src/net/resource.c +++ b/src/net/resource.c @@ -24,7 +24,7 @@ _z_keyexpr_t _z_rname(const char *rname) { return _z_rid_with_suffix(0, rname); _z_keyexpr_t _z_rid_with_suffix(uint16_t rid, const char *suffix) { return (_z_keyexpr_t){ ._id = rid, - ._sender_mapping = false, + ._uses_remote_mapping = false, ._owns_suffix = false, ._suffix = (char *)suffix, }; diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index 5dccd122b..cfccd81a5 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -37,7 +37,7 @@ int8_t _z_decl_ext_keyexpr_encode(_z_wbuf_t *wbf, _z_keyexpr_t ke, _Bool has_nex uint8_t header = _Z_MSG_EXT_ENC_ZBUF | 0x0f | (has_next_ext ? _Z_FLAG_Z_Z : 0); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); uint32_t kelen = _z_keyexpr_has_suffix(ke) ? strlen(ke._suffix) : 0; - header = (ke._sender_mapping ? 2 : 0) | (kelen != 0 ? 1 : 0); + header = (ke._uses_remote_mapping ? 2 : 0) | (kelen != 0 ? 1 : 0); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, 1 + kelen + _z_zint_len(ke._id))); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, ke._id)); @@ -68,7 +68,7 @@ int8_t _z_decl_commons_encode(_z_wbuf_t *wbf, uint8_t header, _Bool has_extensio if (has_kesuffix) { header |= _Z_DECL_SUBSCRIBER_FLAG_N; } - if (keyexpr._sender_mapping) { + if (!keyexpr._uses_remote_mapping) { header |= _Z_DECL_SUBSCRIBER_FLAG_M; } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); @@ -213,7 +213,7 @@ int8_t _z_undecl_decode_extensions(_z_msg_ext_t *extension, void *ctx) { _z_zbuf_t *zbf = &_zbf; uint8_t header; _Z_RETURN_IF_ERR(_z_uint8_decode(&header, zbf)); - ke->_sender_mapping = _Z_HAS_FLAG(header, 2); + ke->_uses_remote_mapping = !_Z_HAS_FLAG(header, 2); _Z_RETURN_IF_ERR(_z_zint16_decode(&ke->_id, zbf)); if (_Z_HAS_FLAG(header, 1)) { size_t len = _z_zbuf_len(zbf); @@ -242,7 +242,7 @@ int8_t _z_undecl_trivial_decode(_z_zbuf_t *zbf, _z_keyexpr_t *_ext_keyexpr, uint } int8_t _z_decl_commons_decode(_z_zbuf_t *zbf, uint8_t header, _Bool *has_extensions, uint32_t *id, _z_keyexpr_t *ke) { *has_extensions = _Z_HAS_FLAG(header, _Z_FLAG_Z_Z); - ke->_sender_mapping = _Z_HAS_FLAG(header, _Z_DECL_SUBSCRIBER_FLAG_M); + ke->_uses_remote_mapping = _Z_HAS_FLAG(header, _Z_DECL_SUBSCRIBER_FLAG_M); _Z_RETURN_IF_ERR(_z_zint32_decode(id, zbf)); _Z_RETURN_IF_ERR(_z_zint16_decode(&ke->_id, zbf)); if (_Z_HAS_FLAG(header, _Z_DECL_SUBSCRIBER_FLAG_N)) { diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 713d8b8ac..811450658 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -59,6 +59,7 @@ int8_t _z_id_encode_as_zbytes(_z_wbuf_t *wbf, const _z_id_t *id) { uint8_t len = _z_id_len(*id); if (len != 0) { + printf("ZIDLEN: %d\n", len); _z_bytes_t buf = _z_bytes_wrap(id->id, len); ret = _z_bytes_encode(wbf, &buf); } else { diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index 945d92448..8efba07b5 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -37,7 +37,7 @@ /*------------------ Push Message ------------------*/ int8_t _z_push_encode(_z_wbuf_t *wbf, const _z_n_msg_push_t *msg) { - uint8_t header = _Z_MID_N_PUSH | (msg->_key._sender_mapping ? _Z_FLAG_N_REQUEST_M : 0); + uint8_t header = _Z_MID_N_PUSH | (msg->_key._uses_remote_mapping ? _Z_FLAG_N_REQUEST_M : 0); _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); _Bool has_timestamp_ext = _z_timestamp_check(&msg->_timestamp); if (has_suffix) { @@ -93,7 +93,7 @@ int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { *msg = (_z_n_msg_push_t){0}; msg->_qos = _Z_N_QOS_DEFAULT; ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N)); - msg->_key._sender_mapping = _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_M); + msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_M); if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_N_Z)) { ret = _z_msg_ext_decode_iter(zbf, _z_push_decode_ext_cb, msg); } @@ -109,7 +109,7 @@ int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { /*------------------ Request Message ------------------*/ int8_t _z_request_encode(_z_wbuf_t *wbf, const _z_n_msg_request_t *msg) { int8_t ret = _Z_RES_OK; - uint8_t header = _Z_MID_N_REQUEST | (msg->_key._sender_mapping ? _Z_FLAG_N_REQUEST_M : 0); + uint8_t header = _Z_MID_N_REQUEST | (msg->_key._uses_remote_mapping ? _Z_FLAG_N_REQUEST_M : 0); _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); if (has_suffix) { header |= _Z_FLAG_N_REQUEST_N; @@ -211,7 +211,7 @@ int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, const uint8_t msg->_ext_qos = _Z_N_QOS_DEFAULT; _Z_RETURN_IF_ERR(_z_zint_decode(&msg->_rid, zbf)); _Z_RETURN_IF_ERR(_z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N))); - msg->_key._sender_mapping = _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_M); + msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_M); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_request_decode_extensions, msg)); } @@ -250,7 +250,7 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { _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); _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); - if (msg->_key._sender_mapping) { + if (msg->_key._uses_remote_mapping) { _Z_SET_FLAG(header, _Z_FLAG_N_RESPONSE_M); } if (has_suffix) { @@ -354,7 +354,7 @@ int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t head *msg = (_z_n_msg_response_t){0}; msg->_ext_qos = _Z_N_QOS_DEFAULT; int8_t ret = _Z_RES_OK; - msg->_key._sender_mapping = _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_M); + msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_M); _Z_RETURN_IF_ERR(_z_zint_decode(&msg->_request_id, zbf)); _Z_RETURN_IF_ERR(_z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N))); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { diff --git a/src/protocol/keyexpr.c b/src/protocol/keyexpr.c index 76f4b4efb..675a90dd9 100644 --- a/src/protocol/keyexpr.c +++ b/src/protocol/keyexpr.c @@ -24,7 +24,7 @@ void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src) { dst->_id = src->_id; dst->_suffix = src->_suffix ? _z_str_clone(src->_suffix) : NULL; - dst->_sender_mapping = src->_sender_mapping; + dst->_uses_remote_mapping = src->_uses_remote_mapping; dst->_owns_suffix = true; } @@ -61,7 +61,7 @@ void _z_keyexpr_free(_z_keyexpr_t **rk) { _z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src) { return (_z_keyexpr_t){ ._id = src._id, - ._sender_mapping = src._sender_mapping, + ._uses_remote_mapping = src._uses_remote_mapping, ._owns_suffix = false, ._suffix = src._suffix, }; diff --git a/src/session/resource.c b/src/session/resource.c index bbca0364f..ce73c15ca 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -152,7 +152,7 @@ _z_resource_t *__unsafe_z_get_resource_by_id(_z_session_t *zn, uint8_t is_local, * - zn->_mutex_inner */ _z_resource_t *__unsafe_z_get_resource_by_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { - _z_resource_list_t *decls = keyexpr->_sender_mapping ? zn->_local_resources : zn->_remote_resources; + _z_resource_list_t *decls = !keyexpr->_uses_remote_mapping ? zn->_local_resources : zn->_remote_resources; return __z_get_resource_by_key(decls, keyexpr); } @@ -162,7 +162,7 @@ _z_resource_t *__unsafe_z_get_resource_by_key(_z_session_t *zn, const _z_keyexpr * - zn->_mutex_inner */ _z_keyexpr_t __unsafe_z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { - _z_resource_list_t *decls = keyexpr->_sender_mapping ? zn->_local_resources : zn->_remote_resources; + _z_resource_list_t *decls = keyexpr->_uses_remote_mapping ? zn->_remote_resources : zn->_local_resources; return __z_get_expanded_key_from_key(decls, keyexpr); } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index f57445994..b0d3612bb 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -612,7 +612,7 @@ _z_keyexpr_t gen_keyexpr(void) { key._suffix = gen_str(gen_zint() % 16); key._owns_suffix = true; } - key._sender_mapping = gen_bool(); + key._uses_remote_mapping = gen_bool(); return key; } @@ -873,7 +873,7 @@ void forget_subscriber_declaration(void) { // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); _z_undecl_subscriber_t d_fsd = { - ._id = 0, ._ext_keyexpr = {._id = 0, ._owns_suffix = false, ._suffix = NULL, ._sender_mapping = false}}; + ._id = 0, ._ext_keyexpr = {._id = 0, ._owns_suffix = false, ._suffix = NULL, ._uses_remote_mapping = false}}; uint8_t e_hdr = 0; _z_uint8_decode(&e_hdr, &zbf); res = _z_undecl_subscriber_decode(&d_fsd, &zbf, e_hdr); From 6a8139113b28b2cdafebe62775a4fe63289e034e Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 28 Jul 2023 14:52:07 +0200 Subject: [PATCH 137/181] intruduce mappings --- include/zenoh-pico/net/resource.h | 46 --------------------- include/zenoh-pico/protocol/core.h | 59 ++++++++++++++++++++++++++- include/zenoh-pico/protocol/keyexpr.h | 4 +- include/zenoh-pico/session/resource.h | 6 +-- src/api/api.c | 9 ++-- src/net/resource.c | 31 -------------- src/protocol/codec/declarations.c | 17 ++++---- src/protocol/codec/message.c | 4 +- src/protocol/codec/network.c | 15 ++++--- src/protocol/keyexpr.c | 26 ++++++++---- src/session/queryable.c | 1 - src/session/resource.c | 15 +++---- src/session/subscription.c | 1 - tests/z_msgcodec_test.c | 9 ++-- 14 files changed, 117 insertions(+), 126 deletions(-) delete mode 100644 include/zenoh-pico/net/resource.h delete mode 100644 src/net/resource.c diff --git a/include/zenoh-pico/net/resource.h b/include/zenoh-pico/net/resource.h deleted file mode 100644 index 8636440ab..000000000 --- a/include/zenoh-pico/net/resource.h +++ /dev/null @@ -1,46 +0,0 @@ -// -// 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_RESOURCE_NETAPI_H -#define ZENOH_PICO_RESOURCE_NETAPI_H - -#include - -#include "zenoh-pico/collections/string.h" -#include "zenoh-pico/protocol/core.h" - -/** - * Create a resource key from a resource name. - * - * Parameters: - * rname: The resource name. The caller keeps its ownership. - * - * Returns: - * A :c:type:`_z_keyexpr_t` containing a new resource key. - */ -_z_keyexpr_t _z_rname(const char *rname); - -/** - * Create a resource key from a resource id and a suffix. - * - * Parameters: - * id: The resource id. - * suffix: The suffix. - * - * Returns: - * A :c:type:`_z_keyexpr_t` containing a new resource key. - */ -_z_keyexpr_t _z_rid_with_suffix(uint16_t rid, const char *suffix); - -#endif /* ZENOH_PICO_RESOURCE_NETAPI_H */ diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index bbf835d26..d5f160eaf 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -15,6 +15,7 @@ #ifndef ZENOH_PICO_PROTOCOL_CORE_H #define ZENOH_PICO_PROTOCOL_CORE_H +#include #include #include @@ -75,6 +76,20 @@ _z_timestamp_t _z_timestamp_null(void); void _z_timestamp_clear(_z_timestamp_t *tstamp); _Bool _z_timestamp_check(const _z_timestamp_t *stamp); +/** + * The product of: + * - top-most bit: whether or not the keyexpr containing this mapping owns its suffix (1=true) + * - the mapping for the keyexpr prefix: + * - 0: local mapping. + * - 0x7fff (MAX): unknown remote mapping. + * - x: the mapping associated with the x-th peer. + */ +typedef struct { + uint16_t _val; +} _z_mapping_t; +#define _Z_KEYEXPR_MAPPING_LOCAL 0 +#define _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE 0x7fff + /** * A zenoh-net resource key. * @@ -84,13 +99,53 @@ _Bool _z_timestamp_check(const _z_timestamp_t *stamp); */ typedef struct { uint16_t _id; - _Bool _uses_remote_mapping; - _Bool _owns_suffix; + _z_mapping_t _mapping; char *_suffix; } _z_keyexpr_t; +static inline _Bool _z_keyexpr_owns_suffix(const _z_keyexpr_t *key) { return (key->_mapping._val & 0x8000) != 0; } +static inline uint16_t _z_keyexpr_mapping_id(const _z_keyexpr_t *key) { return key->_mapping._val & 0x7fff; } +static inline _Bool _z_keyexpr_is_local(const _z_keyexpr_t *key) { + return (key->_mapping._val & 0x7fff) == _Z_KEYEXPR_MAPPING_LOCAL; +} +static inline _z_mapping_t _z_keyexpr_mapping(uint16_t id, _Bool owns_suffix) { + assert(id <= _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); + return (_z_mapping_t){._val = (owns_suffix ? 0x8000 : 0) | id}; +} +static inline void _z_keyexpr_set_mapping(_z_keyexpr_t *ke, uint16_t id) { + assert(id <= _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); + ke->_mapping._val &= 0x8000; + ke->_mapping._val |= id; +} +static inline void _z_keyexpr_set_owns_suffix(_z_keyexpr_t *ke, _Bool owns_suffix) { + ke->_mapping._val &= 0x7fff; + ke->_mapping._val |= owns_suffix ? 0x8000 : 0; +} static inline _Bool _z_keyexpr_has_suffix(_z_keyexpr_t ke) { return (ke._suffix != NULL) && (ke._suffix[0] != 0); } static inline _Bool _z_keyexpr_check(_z_keyexpr_t ke) { return (ke._id != 0) || _z_keyexpr_has_suffix(ke); } +/** + * Create a resource key from a resource name. + * + * Parameters: + * rname: The resource name. The caller keeps its ownership. + * + * Returns: + * A :c:type:`_z_keyexpr_t` containing a new resource key. + */ +_z_keyexpr_t _z_rname(const char *rname); + +/** + * Create a resource key from a resource id and a suffix. + * + * Parameters: + * id: The resource id. + * suffix: The suffix. + * + * Returns: + * A :c:type:`_z_keyexpr_t` containing a new resource key. + */ +_z_keyexpr_t _z_rid_with_suffix(uint16_t rid, const char *suffix); + /** * A zenoh-net data sample. * diff --git a/include/zenoh-pico/protocol/keyexpr.h b/include/zenoh-pico/protocol/keyexpr.h index 49c088a13..1309e1e3c 100644 --- a/include/zenoh-pico/protocol/keyexpr.h +++ b/include/zenoh-pico/protocol/keyexpr.h @@ -29,9 +29,7 @@ void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src); _z_keyexpr_t _z_keyexpr_duplicate(_z_keyexpr_t src); _z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src); _z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src); -static inline _z_keyexpr_t _z_keyexpr_null(void) { - return (_z_keyexpr_t){._id = 0, ._uses_remote_mapping = false, ._owns_suffix = false, ._suffix = NULL}; -} +static inline _z_keyexpr_t _z_keyexpr_null(void) { return (_z_keyexpr_t){._id = 0, ._mapping = {0}, ._suffix = NULL}; } _z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp); void _z_timestamp_clear(_z_timestamp_t *tstamp); void _z_keyexpr_clear(_z_keyexpr_t *rk); diff --git a/include/zenoh-pico/session/resource.h b/include/zenoh-pico/session/resource.h index 339180ae0..35c6276b2 100644 --- a/include/zenoh-pico/session/resource.h +++ b/include/zenoh-pico/session/resource.h @@ -24,15 +24,15 @@ uint32_t _z_get_entity_id(_z_session_t *zn); /*------------------ Resource ------------------*/ uint16_t _z_get_resource_id(_z_session_t *zn); -_z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint8_t is_local, _z_zint_t rid); -_z_resource_t *_z_get_resource_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr); +_z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint8_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); int8_t _z_register_resource(_z_session_t *zn, uint8_t is_local, _z_resource_t *res); void _z_unregister_resource(_z_session_t *zn, uint8_t is_local, _z_resource_t *res); void _z_flush_resources(_z_session_t *zn); _z_keyexpr_t __unsafe_z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr); -_z_resource_t *__unsafe_z_get_resource_by_id(_z_session_t *zn, uint8_t is_local, _z_zint_t id); +_z_resource_t *__unsafe_z_get_resource_by_id(_z_session_t *zn, uint16_t mapping, _z_zint_t id); _z_resource_t *__unsafe_z_get_resource_matching_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr); #endif /* INCLUDE_ZENOH_PICO_SESSION_RESOURCE_H */ diff --git a/src/api/api.c b/src/api/api.c index 6f2989b84..3cef046c4 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -25,7 +25,6 @@ #include "zenoh-pico/net/logger.h" #include "zenoh-pico/net/memory.h" #include "zenoh-pico/net/primitives.h" -#include "zenoh-pico/net/resource.h" #include "zenoh-pico/net/session.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/keyexpr.h" @@ -741,7 +740,7 @@ z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, con #if Z_MULTICAST_TRANSPORT == 1 if (zs._val->_tp._type != _Z_TRANSPORT_MULTICAST_TYPE) { #endif // Z_MULTICAST_TRANSPORT == 1 - _z_resource_t *r = _z_get_resource_by_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); + _z_resource_t *r = _z_get_resource_by_key(zs._val, &keyexpr); if (r == NULL) { uint16_t id = _z_declare_resource(zs._val, keyexpr); key = _z_rid_with_suffix(id, NULL); @@ -817,7 +816,7 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z #if Z_MULTICAST_TRANSPORT == 1 if (zs._val->_tp._type != _Z_TRANSPORT_MULTICAST_TYPE) { #endif // Z_MULTICAST_TRANSPORT == 1 - _z_resource_t *r = _z_get_resource_by_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); + _z_resource_t *r = _z_get_resource_by_key(zs._val, &keyexpr); if (r == NULL) { uint16_t id = _z_declare_resource(zs._val, keyexpr); key = _z_rid_with_suffix(id, NULL); @@ -844,7 +843,7 @@ z_owned_pull_subscriber_t z_declare_pull_subscriber(z_session_t zs, z_keyexpr_t callback->context = NULL; z_keyexpr_t key = keyexpr; - _z_resource_t *r = _z_get_resource_by_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); + _z_resource_t *r = _z_get_resource_by_key(zs._val, &keyexpr); if (r == NULL) { uint16_t id = _z_declare_resource(zs._val, keyexpr); key = _z_rid_with_suffix(id, NULL); @@ -896,7 +895,7 @@ z_owned_queryable_t z_declare_queryable(z_session_t zs, z_keyexpr_t keyexpr, z_o #if Z_MULTICAST_TRANSPORT == 1 if (zs._val->_tp._type != _Z_TRANSPORT_MULTICAST_TYPE) { #endif // Z_MULTICAST_TRANSPORT == 1 - _z_resource_t *r = _z_get_resource_by_key(zs._val, _Z_RESOURCE_IS_LOCAL, &keyexpr); + _z_resource_t *r = _z_get_resource_by_key(zs._val, &keyexpr); if (r == NULL) { uint16_t id = _z_declare_resource(zs._val, keyexpr); key = _z_rid_with_suffix(id, NULL); diff --git a/src/net/resource.c b/src/net/resource.c deleted file mode 100644 index c6c3bea58..000000000 --- a/src/net/resource.c +++ /dev/null @@ -1,31 +0,0 @@ -// -// 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, -// - -#include "zenoh-pico/net/resource.h" - -#include -#include - -#include "zenoh-pico/protocol/core.h" - -_z_keyexpr_t _z_rname(const char *rname) { return _z_rid_with_suffix(0, rname); } - -_z_keyexpr_t _z_rid_with_suffix(uint16_t rid, const char *suffix) { - return (_z_keyexpr_t){ - ._id = rid, - ._uses_remote_mapping = false, - ._owns_suffix = false, - ._suffix = (char *)suffix, - }; -} diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index cfccd81a5..b24c8bd64 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -37,7 +37,7 @@ int8_t _z_decl_ext_keyexpr_encode(_z_wbuf_t *wbf, _z_keyexpr_t ke, _Bool has_nex uint8_t header = _Z_MSG_EXT_ENC_ZBUF | 0x0f | (has_next_ext ? _Z_FLAG_Z_Z : 0); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); uint32_t kelen = _z_keyexpr_has_suffix(ke) ? strlen(ke._suffix) : 0; - header = (ke._uses_remote_mapping ? 2 : 0) | (kelen != 0 ? 1 : 0); + header = (_z_keyexpr_is_local(&ke) ? 2 : 0) | (kelen != 0 ? 1 : 0); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, 1 + kelen + _z_zint_len(ke._id))); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); _Z_RETURN_IF_ERR(_z_zint_encode(wbf, ke._id)); @@ -68,7 +68,7 @@ int8_t _z_decl_commons_encode(_z_wbuf_t *wbf, uint8_t header, _Bool has_extensio if (has_kesuffix) { header |= _Z_DECL_SUBSCRIBER_FLAG_N; } - if (!keyexpr._uses_remote_mapping) { + if (_z_keyexpr_is_local(&keyexpr)) { header |= _Z_DECL_SUBSCRIBER_FLAG_M; } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); @@ -213,7 +213,7 @@ int8_t _z_undecl_decode_extensions(_z_msg_ext_t *extension, void *ctx) { _z_zbuf_t *zbf = &_zbf; uint8_t header; _Z_RETURN_IF_ERR(_z_uint8_decode(&header, zbf)); - ke->_uses_remote_mapping = !_Z_HAS_FLAG(header, 2); + uint16_t mapping = _Z_HAS_FLAG(header, 2) ? _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE : _Z_KEYEXPR_MAPPING_LOCAL; _Z_RETURN_IF_ERR(_z_zint16_decode(&ke->_id, zbf)); if (_Z_HAS_FLAG(header, 1)) { size_t len = _z_zbuf_len(zbf); @@ -221,9 +221,11 @@ int8_t _z_undecl_decode_extensions(_z_msg_ext_t *extension, void *ctx) { if (!ke->_suffix) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - ke->_owns_suffix = true; + ke->_mapping = _z_keyexpr_mapping(mapping, true); _z_zbuf_read_bytes(zbf, (uint8_t *)ke->_suffix, 0, len); ke->_suffix[len] = 0; + } else { + ke->_mapping = _z_keyexpr_mapping(mapping, false); } } break; default: @@ -242,7 +244,8 @@ int8_t _z_undecl_trivial_decode(_z_zbuf_t *zbf, _z_keyexpr_t *_ext_keyexpr, uint } int8_t _z_decl_commons_decode(_z_zbuf_t *zbf, uint8_t header, _Bool *has_extensions, uint32_t *id, _z_keyexpr_t *ke) { *has_extensions = _Z_HAS_FLAG(header, _Z_FLAG_Z_Z); - ke->_uses_remote_mapping = _Z_HAS_FLAG(header, _Z_DECL_SUBSCRIBER_FLAG_M); + uint16_t mapping = + _Z_HAS_FLAG(header, _Z_DECL_SUBSCRIBER_FLAG_M) ? _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE : _Z_KEYEXPR_MAPPING_LOCAL; _Z_RETURN_IF_ERR(_z_zint32_decode(id, zbf)); _Z_RETURN_IF_ERR(_z_zint16_decode(&ke->_id, zbf)); if (_Z_HAS_FLAG(header, _Z_DECL_SUBSCRIBER_FLAG_N)) { @@ -255,12 +258,12 @@ int8_t _z_decl_commons_decode(_z_zbuf_t *zbf, uint8_t header, _Bool *has_extensi if (ke->_suffix == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - ke->_owns_suffix = true; + ke->_mapping = _z_keyexpr_mapping(mapping, true); _z_zbuf_read_bytes(zbf, (uint8_t *)ke->_suffix, 0, len); ke->_suffix[len] = 0; } else { ke->_suffix = NULL; - ke->_owns_suffix = false; + ke->_mapping = _z_keyexpr_mapping(mapping, false); } return _Z_RES_OK; } diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 811450658..901bf829d 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -125,15 +125,15 @@ int8_t _z_keyexpr_decode(_z_keyexpr_t *ke, _z_zbuf_t *zbf, _Bool has_suffix) { int8_t ret = _Z_RES_OK; ret |= _z_zint16_decode(&ke->_id, zbf); - ke->_owns_suffix = false; if (has_suffix == true) { char *str = NULL; ret |= _z_str_decode(&str, zbf); if (ret == _Z_RES_OK) { ke->_suffix = str; - ke->_owns_suffix = true; + ke->_mapping = _z_keyexpr_mapping(0, true); } else { ke->_suffix = NULL; + ke->_mapping = _z_keyexpr_mapping(0, false); } } else { ke->_suffix = NULL; diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index 8efba07b5..61769bdb0 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -37,7 +37,7 @@ /*------------------ Push Message ------------------*/ int8_t _z_push_encode(_z_wbuf_t *wbf, const _z_n_msg_push_t *msg) { - uint8_t header = _Z_MID_N_PUSH | (msg->_key._uses_remote_mapping ? _Z_FLAG_N_REQUEST_M : 0); + uint8_t header = _Z_MID_N_PUSH | (_z_keyexpr_is_local(&msg->_key) ? _Z_FLAG_N_REQUEST_M : 0); _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); _Bool has_timestamp_ext = _z_timestamp_check(&msg->_timestamp); if (has_suffix) { @@ -93,7 +93,8 @@ int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { *msg = (_z_n_msg_push_t){0}; msg->_qos = _Z_N_QOS_DEFAULT; ret |= _z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_N)); - msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_M); + _z_keyexpr_set_mapping(&msg->_key, _Z_HAS_FLAG(header, _Z_FLAG_N_PUSH_M) ? _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE + : _Z_KEYEXPR_MAPPING_LOCAL); if ((ret == _Z_RES_OK) && _Z_HAS_FLAG(header, _Z_FLAG_N_Z)) { ret = _z_msg_ext_decode_iter(zbf, _z_push_decode_ext_cb, msg); } @@ -109,7 +110,7 @@ int8_t _z_push_decode(_z_n_msg_push_t *msg, _z_zbuf_t *zbf, uint8_t header) { /*------------------ Request Message ------------------*/ int8_t _z_request_encode(_z_wbuf_t *wbf, const _z_n_msg_request_t *msg) { int8_t ret = _Z_RES_OK; - uint8_t header = _Z_MID_N_REQUEST | (msg->_key._uses_remote_mapping ? _Z_FLAG_N_REQUEST_M : 0); + uint8_t header = _Z_MID_N_REQUEST | (_z_keyexpr_is_local(&msg->_key) ? _Z_FLAG_N_REQUEST_M : 0); _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); if (has_suffix) { header |= _Z_FLAG_N_REQUEST_N; @@ -211,7 +212,8 @@ int8_t _z_request_decode(_z_n_msg_request_t *msg, _z_zbuf_t *zbf, const uint8_t msg->_ext_qos = _Z_N_QOS_DEFAULT; _Z_RETURN_IF_ERR(_z_zint_decode(&msg->_rid, zbf)); _Z_RETURN_IF_ERR(_z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_N))); - msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_M); + _z_keyexpr_set_mapping(&msg->_key, _Z_HAS_FLAG(header, _Z_FLAG_N_REQUEST_M) ? _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE + : _Z_KEYEXPR_MAPPING_LOCAL); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_request_decode_extensions, msg)); } @@ -250,7 +252,7 @@ int8_t _z_response_encode(_z_wbuf_t *wbf, const _z_n_msg_response_t *msg) { _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); _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); - if (msg->_key._uses_remote_mapping) { + if (_z_keyexpr_is_local(&msg->_key)) { _Z_SET_FLAG(header, _Z_FLAG_N_RESPONSE_M); } if (has_suffix) { @@ -354,7 +356,8 @@ int8_t _z_response_decode(_z_n_msg_response_t *msg, _z_zbuf_t *zbf, uint8_t head *msg = (_z_n_msg_response_t){0}; msg->_ext_qos = _Z_N_QOS_DEFAULT; int8_t ret = _Z_RES_OK; - msg->_key._uses_remote_mapping = !_Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_M); + _z_keyexpr_set_mapping(&msg->_key, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_M) ? _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE + : _Z_KEYEXPR_MAPPING_LOCAL); _Z_RETURN_IF_ERR(_z_zint_decode(&msg->_request_id, zbf)); _Z_RETURN_IF_ERR(_z_keyexpr_decode(&msg->_key, zbf, _Z_HAS_FLAG(header, _Z_FLAG_N_RESPONSE_N))); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { diff --git a/src/protocol/keyexpr.c b/src/protocol/keyexpr.c index 675a90dd9..b18d974a8 100644 --- a/src/protocol/keyexpr.c +++ b/src/protocol/keyexpr.c @@ -18,14 +18,25 @@ #include #include +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/utils/pointers.h" #include "zenoh-pico/utils/string.h" +_z_keyexpr_t _z_rname(const char *rname) { return _z_rid_with_suffix(0, rname); } + +_z_keyexpr_t _z_rid_with_suffix(uint16_t rid, const char *suffix) { + return (_z_keyexpr_t){ + ._id = rid, + ._mapping = _z_keyexpr_mapping(_Z_KEYEXPR_MAPPING_LOCAL, false), + ._suffix = (char *)suffix, + }; +} + void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src) { dst->_id = src->_id; dst->_suffix = src->_suffix ? _z_str_clone(src->_suffix) : NULL; - dst->_uses_remote_mapping = src->_uses_remote_mapping; - dst->_owns_suffix = true; + dst->_mapping = src->_mapping; + _z_keyexpr_set_owns_suffix(dst, true); } _z_keyexpr_t _z_keyexpr_duplicate(_z_keyexpr_t src) { @@ -42,9 +53,9 @@ _z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src) { void _z_keyexpr_clear(_z_keyexpr_t *rk) { rk->_id = 0; - if (rk->_suffix != NULL && rk->_owns_suffix) { + if (rk->_suffix != NULL && _z_keyexpr_owns_suffix(rk)) { _z_str_clear((char *)rk->_suffix); - rk->_owns_suffix = false; + _z_keyexpr_set_owns_suffix(rk, false); } } @@ -59,12 +70,13 @@ void _z_keyexpr_free(_z_keyexpr_t **rk) { } } _z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src) { - return (_z_keyexpr_t){ + _z_keyexpr_t alias = { ._id = src._id, - ._uses_remote_mapping = src._uses_remote_mapping, - ._owns_suffix = false, + ._mapping = src._mapping, ._suffix = src._suffix, }; + _z_keyexpr_set_owns_suffix(&alias, false); + return alias; } /*------------------ Canonize helpers ------------------*/ diff --git a/src/session/queryable.c b/src/session/queryable.c index b8bc3aedd..078ecdab2 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -17,7 +17,6 @@ #include #include "zenoh-pico/config.h" -#include "zenoh-pico/net/resource.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/definitions/network.h" #include "zenoh-pico/protocol/keyexpr.h" diff --git a/src/session/resource.c b/src/session/resource.c index ce73c15ca..642fa8bfc 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -19,6 +19,7 @@ #include "zenoh-pico/api/types.h" #include "zenoh-pico/config.h" +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/utils/logging.h" _Bool _z_resource_eq(const _z_resource_t *other, const _z_resource_t *this) { return this->_id == other->_id; } @@ -141,8 +142,8 @@ _z_keyexpr_t __z_get_expanded_key_from_key(_z_resource_list_t *xs, const _z_keye * Make sure that the following mutexes are locked before calling this function: * - zn->_mutex_inner */ -_z_resource_t *__unsafe_z_get_resource_by_id(_z_session_t *zn, uint8_t is_local, _z_zint_t id) { - _z_resource_list_t *decls = (is_local == _Z_RESOURCE_IS_LOCAL) ? zn->_local_resources : zn->_remote_resources; +_z_resource_t *__unsafe_z_get_resource_by_id(_z_session_t *zn, uint16_t mapping, _z_zint_t id) { + _z_resource_list_t *decls = (mapping == _Z_KEYEXPR_MAPPING_LOCAL) ? zn->_local_resources : zn->_remote_resources; return __z_get_resource_by_id(decls, id); } @@ -152,7 +153,7 @@ _z_resource_t *__unsafe_z_get_resource_by_id(_z_session_t *zn, uint8_t is_local, * - zn->_mutex_inner */ _z_resource_t *__unsafe_z_get_resource_by_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { - _z_resource_list_t *decls = !keyexpr->_uses_remote_mapping ? zn->_local_resources : zn->_remote_resources; + _z_resource_list_t *decls = _z_keyexpr_is_local(keyexpr) ? zn->_local_resources : zn->_remote_resources; return __z_get_resource_by_key(decls, keyexpr); } @@ -162,16 +163,16 @@ _z_resource_t *__unsafe_z_get_resource_by_key(_z_session_t *zn, const _z_keyexpr * - zn->_mutex_inner */ _z_keyexpr_t __unsafe_z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { - _z_resource_list_t *decls = keyexpr->_uses_remote_mapping ? zn->_remote_resources : zn->_local_resources; + _z_resource_list_t *decls = _z_keyexpr_is_local(keyexpr) ? zn->_local_resources : zn->_remote_resources; return __z_get_expanded_key_from_key(decls, keyexpr); } -_z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint8_t is_local, _z_zint_t rid) { +_z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint8_t mapping, _z_zint_t rid) { #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - _z_resource_t *res = __unsafe_z_get_resource_by_id(zn, is_local, rid); + _z_resource_t *res = __unsafe_z_get_resource_by_id(zn, mapping, rid); #if Z_MULTI_THREAD == 1 _z_mutex_unlock(&zn->_mutex_inner); @@ -180,7 +181,7 @@ _z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint8_t is_local, _z_zint return res; } -_z_resource_t *_z_get_resource_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr) { +_z_resource_t *_z_get_resource_by_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 diff --git a/src/session/subscription.c b/src/session/subscription.c index 4f73eed97..e00d56b1c 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -17,7 +17,6 @@ #include #include "zenoh-pico/config.h" -#include "zenoh-pico/net/resource.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/session/resource.h" #include "zenoh-pico/utils/logging.h" diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index b0d3612bb..dcf051d41 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -604,15 +604,15 @@ void assert_eq_subinfo(_z_subinfo_t *left, _z_subinfo_t *right) { _z_keyexpr_t gen_keyexpr(void) { _z_keyexpr_t key; key._id = gen_zint(); + key._mapping._val = gen_uint8(); _Bool is_numerical = gen_bool(); if (is_numerical == true) { key._suffix = NULL; - key._owns_suffix = false; + _z_keyexpr_set_owns_suffix(&key, false); } else { key._suffix = gen_str(gen_zint() % 16); - key._owns_suffix = true; + _z_keyexpr_set_owns_suffix(&key, true); } - key._uses_remote_mapping = gen_bool(); return key; } @@ -872,8 +872,7 @@ void forget_subscriber_declaration(void) { // Decode _z_zbuf_t zbf = _z_wbuf_to_zbuf(&wbf); - _z_undecl_subscriber_t d_fsd = { - ._id = 0, ._ext_keyexpr = {._id = 0, ._owns_suffix = false, ._suffix = NULL, ._uses_remote_mapping = false}}; + _z_undecl_subscriber_t d_fsd = {._id = 0, ._ext_keyexpr = {0}}; uint8_t e_hdr = 0; _z_uint8_decode(&e_hdr, &zbf); res = _z_undecl_subscriber_decode(&d_fsd, &zbf, e_hdr); From ba5722d77429bed9ee156a52298ba7b7b524908b Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 28 Jul 2023 16:06:45 +0200 Subject: [PATCH 138/181] reference count the resources! --- include/zenoh-pico/session/resource.h | 4 +-- include/zenoh-pico/session/session.h | 1 + src/net/primitives.c | 6 ++-- src/session/resource.c | 42 ++++++++++++++++++++------- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/include/zenoh-pico/session/resource.h b/include/zenoh-pico/session/resource.h index 35c6276b2..05757bfb2 100644 --- a/include/zenoh-pico/session/resource.h +++ b/include/zenoh-pico/session/resource.h @@ -27,8 +27,8 @@ uint16_t _z_get_resource_id(_z_session_t *zn); _z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint8_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); -int8_t _z_register_resource(_z_session_t *zn, uint8_t is_local, _z_resource_t *res); -void _z_unregister_resource(_z_session_t *zn, uint8_t is_local, _z_resource_t *res); +int8_t _z_register_resource(_z_session_t *zn, _z_resource_t *res); +void _z_unregister_resource(_z_session_t *zn, _z_resource_t *res); void _z_flush_resources(_z_session_t *zn); _z_keyexpr_t __unsafe_z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr); diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index feb41d49a..8b394e844 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -73,6 +73,7 @@ void _z_reply_free(_z_reply_t **hello); typedef struct { _z_keyexpr_t _key; uint16_t _id; + uint16_t _refcount; } _z_resource_t; _Bool _z_resource_eq(const _z_resource_t *one, const _z_resource_t *two); diff --git a/src/net/primitives.c b/src/net/primitives.c index cabfca3ca..0510c7512 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -59,7 +59,7 @@ uint16_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr) { if (r != NULL) { r->_id = _z_get_resource_id(zn); r->_key = _z_keyexpr_duplicate(keyexpr); - if (_z_register_resource(zn, _Z_RESOURCE_IS_LOCAL, r) == _Z_RES_OK) { + if (_z_register_resource(zn, r) == _Z_RES_OK) { // Build the declare message to send on the wire _z_keyexpr_t alias = _z_keyexpr_alias(r->_key); _z_declaration_t declaration = _z_make_decl_keyexpr(r->_id, &alias); @@ -67,7 +67,7 @@ uint16_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr) { if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { ret = r->_id; } else { - _z_unregister_resource(zn, _Z_RESOURCE_IS_LOCAL, r); + _z_unregister_resource(zn, r); } _z_n_msg_clear(&n_msg); } else { @@ -88,7 +88,7 @@ int8_t _z_undeclare_resource(_z_session_t *zn, uint16_t rid) { _z_declaration_t declaration = _z_make_undecl_keyexpr(rid); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { - _z_unregister_resource(zn, _Z_RESOURCE_IS_LOCAL, r); // Only if message is send, local resource is removed + _z_unregister_resource(zn, r); // Only if message is send, local resource is removed } else { ret = _Z_ERR_TRANSPORT_TX_FAILED; } diff --git a/src/session/resource.c b/src/session/resource.c index 642fa8bfc..55b48672d 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -20,6 +20,7 @@ #include "zenoh-pico/api/types.h" #include "zenoh-pico/config.h" #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/session/session.h" #include "zenoh-pico/utils/logging.h" _Bool _z_resource_eq(const _z_resource_t *other, const _z_resource_t *this) { return this->_id == other->_id; } @@ -43,13 +44,13 @@ uint32_t _z_get_entity_id(_z_session_t *zn) { return zn->_entity_id++; } uint16_t _z_get_resource_id(_z_session_t *zn) { return zn->_resource_id++; } /*------------------ Resource ------------------*/ -_z_resource_t *__z_get_resource_by_id(_z_resource_list_t *rl, const _z_zint_t id) { +_z_resource_t *__z_get_resource_by_id(_z_resource_list_t *rl, uint16_t mapping, const _z_zint_t id) { _z_resource_t *ret = NULL; _z_resource_list_t *xs = rl; while (xs != NULL) { _z_resource_t *r = _z_resource_list_head(xs); - if (r->_id == id) { + if (r->_id == id && _z_keyexpr_mapping_id(&r->_key) == mapping) { ret = r; break; } @@ -62,11 +63,13 @@ _z_resource_t *__z_get_resource_by_id(_z_resource_list_t *rl, const _z_zint_t id _z_resource_t *__z_get_resource_by_key(_z_resource_list_t *rl, const _z_keyexpr_t *keyexpr) { _z_resource_t *ret = NULL; + uint16_t mapping = _z_keyexpr_mapping_id(keyexpr); _z_resource_list_t *xs = rl; while (xs != NULL) { _z_resource_t *r = _z_resource_list_head(xs); - if ((r->_key._id == keyexpr->_id) && (_z_str_eq(r->_key._suffix, keyexpr->_suffix) == true)) { + if ((r->_key._id == keyexpr->_id) && _z_keyexpr_mapping_id(&r->_key) == mapping && + (_z_str_eq(r->_key._suffix, keyexpr->_suffix) == true)) { ret = r; break; } @@ -95,8 +98,9 @@ _z_keyexpr_t __z_get_expanded_key_from_key(_z_resource_list_t *xs, const _z_keye // Recursevely go through all the RIDs _z_zint_t id = keyexpr->_id; + uint16_t mapping = _z_keyexpr_mapping_id(keyexpr); while (id != Z_RESOURCE_ID_NONE) { - _z_resource_t *res = __z_get_resource_by_id(xs, id); + _z_resource_t *res = __z_get_resource_by_id(xs, mapping, id); if (res == NULL) { len = 0; break; @@ -144,7 +148,7 @@ _z_keyexpr_t __z_get_expanded_key_from_key(_z_resource_list_t *xs, const _z_keye */ _z_resource_t *__unsafe_z_get_resource_by_id(_z_session_t *zn, uint16_t mapping, _z_zint_t id) { _z_resource_list_t *decls = (mapping == _Z_KEYEXPR_MAPPING_LOCAL) ? zn->_local_resources : zn->_remote_resources; - return __z_get_resource_by_id(decls, id); + return __z_get_resource_by_id(decls, mapping, id); } /** @@ -208,26 +212,28 @@ _z_keyexpr_t _z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t * return res; } -int8_t _z_register_resource(_z_session_t *zn, uint8_t is_local, _z_resource_t *res) { +int8_t _z_register_resource(_z_session_t *zn, _z_resource_t *res) { int8_t ret = _Z_RES_OK; _Z_DEBUG(">>> Allocating res decl for (%ju,%u:%s)\n", (uintmax_t)res->_id, (unsigned int)res->_key._id, res->_key._suffix); - + uint16_t mapping = _z_keyexpr_mapping_id(&res->_key); #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 // FIXME: check by keyexpr instead - _z_resource_t *r = __unsafe_z_get_resource_by_id(zn, is_local, res->_id); + _z_resource_t *r = __unsafe_z_get_resource_by_id(zn, mapping, res->_id); if (r == NULL) { + res->_refcount = 1; // Register the resource - if (is_local == _Z_RESOURCE_IS_LOCAL) { + if (mapping == _Z_KEYEXPR_MAPPING_LOCAL) { zn->_local_resources = _z_resource_list_push(zn->_local_resources, res); } else { zn->_remote_resources = _z_resource_list_push(zn->_remote_resources, res); } } else { + r->_refcount++; ret = _Z_ERR_ENTITY_DECLARATION_FAILED; } @@ -238,11 +244,25 @@ int8_t _z_register_resource(_z_session_t *zn, uint8_t is_local, _z_resource_t *r return ret; } -void _z_unregister_resource(_z_session_t *zn, uint8_t is_local, _z_resource_t *res) { +void _z_unregister_resource(_z_session_t *zn, _z_resource_t *res) { + _Bool is_local = _z_keyexpr_is_local(&res->_key); + uint16_t mapping = _z_keyexpr_mapping_id(&res->_key); #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - + _z_resource_list_t *list = is_local ? zn->_local_resources : zn->_remote_resources; + while (list) { + _z_resource_t *head = _z_resource_list_head(list); + if (head && head->_id == res->_id && _z_keyexpr_mapping_id(&head->_key) == mapping) { + res->_refcount--; + if (res->_refcount == 0) { + _z_resource_list_pop(list, &head); + _z_resource_free(&head); + } + break; + } + list = _z_resource_list_tail(list); + } if (is_local == _Z_RESOURCE_IS_LOCAL) { zn->_local_resources = _z_resource_list_drop_filter(zn->_local_resources, _z_resource_eq, res); } else { From 19423da874f6692581c262152fb3db34f109d2ac Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 28 Jul 2023 16:11:45 +0200 Subject: [PATCH 139/181] fix refcounting and add a global unregister for when a peer disconnects --- include/zenoh-pico/protocol/core.h | 6 +++--- include/zenoh-pico/session/resource.h | 3 ++- src/session/resource.c | 23 +++++++++++++++++++---- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index d5f160eaf..6a0425800 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -12,8 +12,8 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_PROTOCOL_CORE_H -#define ZENOH_PICO_PROTOCOL_CORE_H +#ifndef INCLUDE_ZENOH_PICO_PROTOCOL_CORE_H +#define INCLUDE_ZENOH_PICO_PROTOCOL_CORE_H #include #include @@ -244,4 +244,4 @@ typedef struct { uint32_t _entity_id; } _z_reply_context_t; -#endif /* ZENOH_PICO_PROTOCOL_CORE_H */ +#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CORE_H */ diff --git a/include/zenoh-pico/session/resource.h b/include/zenoh-pico/session/resource.h index 05757bfb2..2fc1a8e36 100644 --- a/include/zenoh-pico/session/resource.h +++ b/include/zenoh-pico/session/resource.h @@ -24,11 +24,12 @@ uint32_t _z_get_entity_id(_z_session_t *zn); /*------------------ Resource ------------------*/ uint16_t _z_get_resource_id(_z_session_t *zn); -_z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint8_t mapping, _z_zint_t rid); +_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); int8_t _z_register_resource(_z_session_t *zn, _z_resource_t *res); void _z_unregister_resource(_z_session_t *zn, _z_resource_t *res); +void _z_unregister_resources_for_peer(_z_session_t *zn, uint16_t mapping); void _z_flush_resources(_z_session_t *zn); _z_keyexpr_t __unsafe_z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr); diff --git a/src/session/resource.c b/src/session/resource.c index 55b48672d..6f29b49c0 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -263,10 +263,25 @@ void _z_unregister_resource(_z_session_t *zn, _z_resource_t *res) { } list = _z_resource_list_tail(list); } - if (is_local == _Z_RESOURCE_IS_LOCAL) { - zn->_local_resources = _z_resource_list_drop_filter(zn->_local_resources, _z_resource_eq, res); - } else { - zn->_remote_resources = _z_resource_list_drop_filter(zn->_remote_resources, _z_resource_eq, res); + +#if Z_MULTI_THREAD == 1 + _z_mutex_unlock(&zn->_mutex_inner); +#endif // Z_MULTI_THREAD == 1 +} + +void _z_unregister_resources_for_peer(_z_session_t *zn, uint16_t mapping) { +#if Z_MULTI_THREAD == 1 + _z_mutex_lock(&zn->_mutex_inner); +#endif // Z_MULTI_THREAD == 1 + _z_resource_list_t *list = zn->_remote_resources; + while (list) { + _z_resource_t *head = _z_resource_list_head(list); + if (head && _z_keyexpr_mapping_id(&head->_key) == mapping) { + _z_resource_list_pop(list, &head); + _z_resource_free(&head); + } else { + list = _z_resource_list_tail(list); + } } #if Z_MULTI_THREAD == 1 From 17d87d263dfdd1ca41e23af39827fd7312271b19 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 28 Jul 2023 16:12:45 +0200 Subject: [PATCH 140/181] signature fix --- src/session/resource.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session/resource.c b/src/session/resource.c index 6f29b49c0..1ac35bf3a 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -171,7 +171,7 @@ _z_keyexpr_t __unsafe_z_get_expanded_key_from_key(_z_session_t *zn, const _z_key return __z_get_expanded_key_from_key(decls, keyexpr); } -_z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint8_t mapping, _z_zint_t rid) { +_z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint16_t mapping, _z_zint_t rid) { #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 From 2ae3f64ae0f46311dbada9cb3a431ffdb76d468c Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Tue, 1 Aug 2023 14:31:26 +0200 Subject: [PATCH 141/181] give each multicast peer a u15 ID for KE mappings --- include/zenoh-pico/net/session.h | 11 ------- include/zenoh-pico/transport/transport.h | 15 ++++++--- src/transport/multicast/link/rx.c | 16 ++++----- src/transport/transport.c | 40 ++++++++++++++++++++++ tests/z_data_struct_test.c | 42 ++++++++++++++++++++---- 5 files changed, 93 insertions(+), 31 deletions(-) diff --git a/include/zenoh-pico/net/session.h b/include/zenoh-pico/net/session.h index 51f7d39e2..19c90adee 100644 --- a/include/zenoh-pico/net/session.h +++ b/include/zenoh-pico/net/session.h @@ -24,17 +24,6 @@ #include "zenoh-pico/session/session.h" #include "zenoh-pico/utils/config.h" -typedef struct { - _z_resource_list_t *resources; - _z_subscription_sptr_list_t *subscriptions; -} _z_remote_t; -static void _z_remote_drop(_z_remote_t *remote) { - _z_resource_list_free(&remote->resources); - _z_subscription_sptr_list_free(&remote->subscriptions); -} -_Z_ELEM_DEFINE(_z_remote, _z_remote_t, _z_noop_size, _z_remote_drop, _z_noop_copy) -_Z_LIST_DEFINE(_z_remote, _z_remote_t) - /** * A zenoh-net session. */ diff --git a/include/zenoh-pico/transport/transport.h b/include/zenoh-pico/transport/transport.h index eff07f138..1982b1b92 100644 --- a/include/zenoh-pico/transport/transport.h +++ b/include/zenoh-pico/transport/transport.h @@ -12,8 +12,11 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_TRANSPORT_TYPES_H -#define ZENOH_PICO_TRANSPORT_TYPES_H +#ifndef INCLUDE_ZENOH_PICO_TRANSPORT_TRANSPORT_H +#define INCLUDE_ZENOH_PICO_TRANSPORT_TRANSPORT_H + +#include +#include #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/element.h" @@ -29,14 +32,14 @@ typedef struct { _z_id_t _remote_zid; _z_bytes_t _remote_addr; + _z_conduit_sn_list_t _sn_rx_sns; // SN numbers _z_zint_t _sn_res; volatile _z_zint_t _lease; volatile _z_zint_t _next_lease; - _z_conduit_sn_list_t _sn_rx_sns; - + uint16_t _peer_id; volatile _Bool _received; } _z_transport_peer_entry_t; @@ -47,6 +50,8 @@ _Bool _z_transport_peer_entry_eq(const _z_transport_peer_entry_t *left, const _z _Z_ELEM_DEFINE(_z_transport_peer_entry, _z_transport_peer_entry_t, _z_transport_peer_entry_size, _z_transport_peer_entry_clear, _z_transport_peer_entry_copy) _Z_LIST_DEFINE(_z_transport_peer_entry, _z_transport_peer_entry_t) +_z_transport_peer_entry_list_t *_z_transport_peer_entry_list_insert(_z_transport_peer_entry_list_t *root, + _z_transport_peer_entry_t *entry); typedef struct { // Session associated to the transport @@ -183,4 +188,4 @@ void _z_transport_free(_z_transport_t **zt); void _z_transport_unicast_free(_z_transport_unicast_t **ztu); void _z_transport_multicast_free(_z_transport_multicast_t **ztm); -#endif /* ZENOH_PICO_TRANSPORT_TYPES_H */ +#endif /* INCLUDE_ZENOH_PICO_TRANSPORT_TRANSPORT_H */ diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 676636403..7eeaf8f02 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -61,7 +61,7 @@ int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_me if (_z_zbuf_len(&ztm->_zbuf) < _Z_MSG_LEN_ENC_SIZE) { _z_zbuf_compact(&ztm->_zbuf); ret = _Z_ERR_TRANSPORT_NOT_ENOUGH_BYTES; - continue; + break; } } @@ -75,7 +75,7 @@ int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_me _z_zbuf_set_rpos(&ztm->_zbuf, _z_zbuf_get_rpos(&ztm->_zbuf) - _Z_MSG_LEN_ENC_SIZE); _z_zbuf_compact(&ztm->_zbuf); ret = _Z_ERR_TRANSPORT_NOT_ENOUGH_BYTES; - continue; + break; } } } else { @@ -127,7 +127,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t // Check if the SN is correct if (_Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_FRAME_R) == true) { // @TODO: amend once reliability is in place. For the time being only - // monothonic SNs are ensured + // monotonic SNs are ensured if (_z_sn_precedes(entry->_sn_res, entry->_sn_rx_sns._val._plain._reliable, t_msg->_body._frame._sn) == true) { entry->_sn_rx_sns._val._plain._reliable = t_msg->_body._frame._sn; @@ -169,7 +169,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t _Bool drop = false; if ((_z_wbuf_len(dbuf) + t_msg->_body._fragment._payload.len) > Z_FRAG_MAX_SIZE) { - // Filling the wbuf capacity as a way to signling the last fragment to reset the dbuf + // Filling the wbuf capacity as a way to signaling the last fragment to reset the dbuf // Otherwise, last (smaller) fragments can be understood as a complete message _z_wbuf_write_bytes(dbuf, t_msg->_body._fragment._payload.start, 0, _z_wbuf_space_left(dbuf)); drop = true; @@ -213,12 +213,12 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t } case _Z_MID_T_INIT: { - // Do nothing, multicas transports are not expected to handle INIT messages + // Do nothing, multicast transports are not expected to handle INIT messages break; } case _Z_MID_T_OPEN: { - // Do nothing, multicas transports are not expected to handle OPEN messages + // Do nothing, multicast transports are not expected to handle OPEN messages break; } @@ -261,7 +261,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t entry->_next_lease = entry->_lease; entry->_received = true; - ztm->_peers = _z_transport_peer_entry_list_push(ztm->_peers, entry); + ztm->_peers = _z_transport_peer_entry_list_insert(ztm->_peers, entry); } else { z_free(entry); } @@ -271,7 +271,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t } else { // Existing peer entry->_received = true; - // Check if the representing capapbilities are still the same + // Check if the representing capabilities are still the same if ((t_msg->_body._join._seq_num_res < Z_SN_RESOLUTION) || (t_msg->_body._join._req_id_res < Z_REQ_RESOLUTION) || (t_msg->_body._join._batch_size < Z_BATCH_SIZE)) { diff --git a/src/transport/transport.c b/src/transport/transport.c index cf18fa43c..11ce9ea74 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -501,3 +501,43 @@ void _z_transport_free(_z_transport_t **zt) { *zt = NULL; } } + +/** + * @brief Inserts an entry into `root`, allocating it a `_peer_id` + * + * @param root the insertion root. + * @param entry the entry to be inserted. + * @return _z_transport_peer_entry_list_t* the new root, after inserting the entry. + */ +_z_transport_peer_entry_list_t *_z_transport_peer_entry_list_insert(_z_transport_peer_entry_list_t *root, + _z_transport_peer_entry_t *entry) { + if (root == NULL) { + entry->_peer_id = 1; + root = _z_transport_peer_entry_list_push(root, entry); + } else { + _z_transport_peer_entry_t *head = _z_transport_peer_entry_list_head(root); + if (head->_peer_id + 1 < _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE) { + entry->_peer_id = head->_peer_id + 1; + root = _z_transport_peer_entry_list_push(root, entry); + } else { + _z_transport_peer_entry_list_t *parent = root; + uint16_t target = head->_peer_id - 1; + while (parent->_tail != NULL) { + _z_transport_peer_entry_list_t *list = _z_transport_peer_entry_list_tail(parent); + head = _z_transport_peer_entry_list_head(list); + if (head->_peer_id < target) { + entry->_peer_id = head->_peer_id + 1; + parent->_tail = _z_transport_peer_entry_list_push(list, entry); + return root; + } + parent = list; + target = head->_peer_id - 1; + } + assert(target > 0); + entry->_peer_id = 1; + parent->_tail = _z_transport_peer_entry_list_push(NULL, entry); + parent->_tail->_val = entry; + } + } + return root; +} diff --git a/tests/z_data_struct_test.c b/tests/z_data_struct_test.c index cc88e4a39..8990ef678 100644 --- a/tests/z_data_struct_test.c +++ b/tests/z_data_struct_test.c @@ -18,13 +18,41 @@ #include #include "zenoh-pico/collections/string.h" +#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/system/platform.h" +#include "zenoh-pico/transport/transport.h" + +void entry_list_test(void) { + _z_transport_peer_entry_list_t *root = _z_transport_peer_entry_list_new(); + for (int i = 0; i < 10; i++) { + _z_transport_peer_entry_t *entry = (_z_transport_peer_entry_t *)z_malloc(sizeof(_z_transport_peer_entry_t)); + root = _z_transport_peer_entry_list_insert(root, entry); + } + _z_transport_peer_entry_list_t *list = root; + for (int i = 10; list != NULL; i--, list = _z_transport_peer_entry_list_tail(list)) { + assert(_z_transport_peer_entry_list_head(list)->_peer_id == i); + } + _z_transport_peer_entry_list_head(root)->_peer_id = _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE - 1; + + for (int i = 0; i < 11; i++) { + _z_transport_peer_entry_t *entry = (_z_transport_peer_entry_t *)z_malloc(sizeof(_z_transport_peer_entry_t)); + root = _z_transport_peer_entry_list_insert(root, entry); + } + assert(_z_transport_peer_entry_list_head(root)->_peer_id == _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE - 1); + list = _z_transport_peer_entry_list_tail(root); + for (int i = 20; list != NULL; i--, list = _z_transport_peer_entry_list_tail(list)) { + assert(_z_transport_peer_entry_list_head(list)->_peer_id == i); + } + _z_transport_peer_entry_list_free(&root); +} int main(void) { + entry_list_test(); char *s = (char *)malloc(64); size_t len = 128; // str-vec - printf(">>> str-vec\n"); + printf(">>> str-vec\r\n"); _z_str_vec_t vec = _z_str_vec_make(1); assert(_z_str_vec_is_empty(&vec) == true); @@ -34,12 +62,12 @@ int main(void) { _z_str_vec_append(&vec, _z_str_clone(s)); char *e = _z_str_vec_get(&vec, i); - printf("append(%zu) = %s\n", i, e); + printf("append(%zu) = %s\r\n", i, e); assert(_z_str_eq(s, e) == true); _z_str_vec_set(&vec, i, _z_str_clone(s)); e = _z_str_vec_get(&vec, i); - printf("set(%zu) = %s\n", i, e); + printf("set(%zu) = %s\r\n", i, e); assert(_z_str_eq(s, e) == true); assert(_z_str_vec_len(&vec) == i + 1); @@ -50,7 +78,7 @@ int main(void) { assert(_z_str_vec_is_empty(&vec) == true); // str-list - printf(">>> str-list\n"); + printf(">>> str-list\r\n"); _z_str_list_t *list = _z_str_list_new(); assert(_z_str_list_is_empty(list) == true); @@ -60,7 +88,7 @@ int main(void) { list = _z_str_list_push(list, _z_str_clone(s)); char *e = _z_str_list_head(list); - printf("push(%zu) = %s\n", i, e); + printf("push(%zu) = %s\r\n", i, e); assert(_z_str_eq(s, e) == true); assert(_z_str_list_len(list) == i + 1); @@ -84,7 +112,7 @@ int main(void) { assert(_z_str_list_is_empty(list) == true); // str-intmap - printf(">>> str-intmap\n"); + printf(">>> str-intmap\r\n"); _z_str_intmap_t map = _z_str_intmap_make(); assert(_z_str_intmap_is_empty(&map) == true); @@ -94,7 +122,7 @@ int main(void) { _z_str_intmap_insert(&map, i, _z_str_clone(s)); char *e = _z_str_intmap_get(&map, i); - printf("get(%zu) = %s\n", i, e); + printf("get(%zu) = %s\r\n", i, e); assert(_z_str_eq(s, e) == true); assert(_z_str_intmap_len(&map) == i + 1); From b1825379be48f73e026e364d260b2b1a222f781c Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 2 Aug 2023 09:26:12 +0200 Subject: [PATCH 142/181] add support for remote keyexpr declarations [skip ci] --- include/zenoh-pico/protocol/core.h | 5 +++ .../protocol/definitions/declarations.h | 33 ++++++++++++++ .../zenoh-pico/protocol/definitions/network.h | 18 ++++++++ .../protocol/definitions/transport.h | 2 + include/zenoh-pico/session/resource.h | 2 +- include/zenoh-pico/session/utils.h | 3 +- src/session/resource.c | 11 +++-- src/session/rx.c | 43 ++++++++++++++++++- src/transport/multicast/link/rx.c | 24 +++++++---- src/transport/unicast/link/rx.c | 6 ++- 10 files changed, 128 insertions(+), 19 deletions(-) diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 6a0425800..fabdcb6cd 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -116,6 +116,11 @@ static inline void _z_keyexpr_set_mapping(_z_keyexpr_t *ke, uint16_t id) { ke->_mapping._val &= 0x8000; ke->_mapping._val |= id; } +static inline void _z_keyexpr_fix_mapping(_z_keyexpr_t *ke, uint16_t id) { + if (_z_keyexpr_mapping_id(ke) == _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE) { + _z_keyexpr_set_mapping(ke, id); + } +} static inline void _z_keyexpr_set_owns_suffix(_z_keyexpr_t *ke, _Bool owns_suffix) { ke->_mapping._val &= 0x7fff; ke->_mapping._val |= owns_suffix ? 0x8000 : 0; diff --git a/include/zenoh-pico/protocol/definitions/declarations.h b/include/zenoh-pico/protocol/definitions/declarations.h index 9f81ce9b5..8cd5c8406 100644 --- a/include/zenoh-pico/protocol/definitions/declarations.h +++ b/include/zenoh-pico/protocol/definitions/declarations.h @@ -123,6 +123,39 @@ typedef struct { } _body; } _z_declaration_t; void _z_declaration_clear(_z_declaration_t* decl); +static inline void _z_decl_fix_mapping(_z_declaration_t* msg, uint16_t mapping) { + switch (msg->_tag) { + case _Z_DECL_KEXPR: { + _z_keyexpr_fix_mapping(&msg->_body._decl_kexpr._keyexpr, mapping); + } break; + case _Z_DECL_SUBSCRIBER: { + _z_keyexpr_fix_mapping(&msg->_body._decl_subscriber._keyexpr, mapping); + } break; + case _Z_UNDECL_SUBSCRIBER: { + _z_keyexpr_fix_mapping(&msg->_body._undecl_subscriber._ext_keyexpr, mapping); + } break; + case _Z_DECL_QUERYABLE: { + _z_keyexpr_fix_mapping(&msg->_body._decl_queryable._keyexpr, mapping); + } break; + case _Z_UNDECL_QUERYABLE: { + _z_keyexpr_fix_mapping(&msg->_body._undecl_queryable._ext_keyexpr, mapping); + } break; + case _Z_DECL_TOKEN: { + _z_keyexpr_fix_mapping(&msg->_body._decl_token._keyexpr, mapping); + } break; + case _Z_UNDECL_TOKEN: { + _z_keyexpr_fix_mapping(&msg->_body._undecl_token._ext_keyexpr, mapping); + } break; + case _Z_DECL_INTEREST: { + _z_keyexpr_fix_mapping(&msg->_body._decl_interest._keyexpr, mapping); + } break; + case _Z_UNDECL_INTEREST: { + _z_keyexpr_fix_mapping(&msg->_body._undecl_interest._ext_keyexpr, mapping); + } break; + default: + break; + } +} _z_declaration_t _z_make_decl_keyexpr(uint16_t id, _Z_MOVE(_z_keyexpr_t) key); _z_declaration_t _z_make_undecl_keyexpr(uint16_t id); diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index f6db78d76..bc5927a23 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -227,6 +227,24 @@ inline static void _z_msg_free(_z_zenoh_message_t **msg) { _z_n_msg_free(msg); } _Z_ELEM_DEFINE(_z_network_message, _z_network_message_t, _z_noop_size, _z_n_msg_clear, _z_noop_copy) _Z_VEC_DEFINE(_z_network_message, _z_network_message_t) +void _z_msg_fix_mapping(_z_zenoh_message_t *msg, uint16_t mapping) { + switch (msg->_tag) { + case _Z_N_DECLARE: { + _z_decl_fix_mapping(&msg->_body._declare._decl, mapping); + } break; + case _Z_N_PUSH: { + _z_keyexpr_fix_mapping(&msg->_body._push._key, mapping); + } break; + case _Z_N_REQUEST: { + _z_keyexpr_fix_mapping(&msg->_body._request._key, mapping); + } break; + case _Z_N_RESPONSE: { + _z_keyexpr_fix_mapping(&msg->_body._response._key, mapping); + } break; + default: + break; + } +} _z_network_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id); _z_network_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes_t) parameters, _z_zint_t qid, z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value); diff --git a/include/zenoh-pico/protocol/definitions/transport.h b/include/zenoh-pico/protocol/definitions/transport.h index 80f3683ca..521efd821 100644 --- a/include/zenoh-pico/protocol/definitions/transport.h +++ b/include/zenoh-pico/protocol/definitions/transport.h @@ -16,6 +16,8 @@ #define INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_TRANSPORT_H /* Scouting Messages */ +#include + #include "zenoh-pico/link/endpoint.h" #include "zenoh-pico/protocol/definitions/network.h" diff --git a/include/zenoh-pico/session/resource.h b/include/zenoh-pico/session/resource.h index 2fc1a8e36..aa09410a6 100644 --- a/include/zenoh-pico/session/resource.h +++ b/include/zenoh-pico/session/resource.h @@ -28,7 +28,7 @@ _z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint16_t mapping, _z_zint _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); int8_t _z_register_resource(_z_session_t *zn, _z_resource_t *res); -void _z_unregister_resource(_z_session_t *zn, _z_resource_t *res); +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/session/utils.h b/include/zenoh-pico/session/utils.h index 8cde42aa6..497e0a015 100644 --- a/include/zenoh-pico/session/utils.h +++ b/include/zenoh-pico/session/utils.h @@ -16,6 +16,7 @@ #define INCLUDE_ZENOH_PICO_SESSION_UTILS_H #include +#include #include "zenoh-pico/api/constants.h" #include "zenoh-pico/net/session.h" @@ -30,7 +31,7 @@ int8_t _z_session_close(_z_session_t *zn, uint8_t reason); void _z_session_clear(_z_session_t *zn); void _z_session_free(_z_session_t **zn); -int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *z_msg); +int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *z_msg, uint16_t local_peer_id); int8_t _z_send_n_msg(_z_session_t *zn, _z_network_message_t *n_msg, z_reliability_t reliability, z_congestion_control_t cong_ctrl); diff --git a/src/session/resource.c b/src/session/resource.c index 1ac35bf3a..e3fd4cabe 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -244,18 +244,17 @@ int8_t _z_register_resource(_z_session_t *zn, _z_resource_t *res) { return ret; } -void _z_unregister_resource(_z_session_t *zn, _z_resource_t *res) { - _Bool is_local = _z_keyexpr_is_local(&res->_key); - uint16_t mapping = _z_keyexpr_mapping_id(&res->_key); +void _z_unregister_resource(_z_session_t *zn, uint16_t id, uint16_t mapping) { + _Bool is_local = mapping == _Z_KEYEXPR_MAPPING_LOCAL; #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 _z_resource_list_t *list = is_local ? zn->_local_resources : zn->_remote_resources; while (list) { _z_resource_t *head = _z_resource_list_head(list); - if (head && head->_id == res->_id && _z_keyexpr_mapping_id(&head->_key) == mapping) { - res->_refcount--; - if (res->_refcount == 0) { + if (head && head->_id == id && _z_keyexpr_mapping_id(&head->_key) == mapping) { + head->_refcount--; + if (head->_refcount == 0) { _z_resource_list_pop(list, &head); _z_resource_free(&head); } diff --git a/src/session/rx.c b/src/session/rx.c index b865b1785..ce452c785 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -24,15 +24,56 @@ #include "zenoh-pico/session/query.h" #include "zenoh-pico/session/queryable.h" #include "zenoh-pico/session/resource.h" +#include "zenoh-pico/session/session.h" #include "zenoh-pico/session/subscription.h" #include "zenoh-pico/session/utils.h" #include "zenoh-pico/utils/logging.h" /*------------------ Handle message ------------------*/ -int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg) { +int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16_t local_peer_id) { int8_t ret = _Z_RES_OK; switch (msg->_tag) { case _Z_N_DECLARE: { + _z_n_msg_declare_t decl = msg->_body._declare; + switch (decl._decl._tag) { + case _Z_DECL_KEXPR: { + _z_resource_t *res = (_z_resource_t *)z_malloc(sizeof(_z_resource_t)); + res->_id = decl._decl._body._decl_kexpr._id; + res->_key = decl._decl._body._decl_kexpr._keyexpr; + _z_register_resource(zn, res); + } break; + case _Z_UNDECL_KEXPR: { + _z_resource_t res = {._id = decl._decl._body._undecl_kexpr._id}; + _z_unregister_resource(zn, decl._decl._body._undecl_kexpr._id, local_peer_id); + } break; + case _Z_DECL_SUBSCRIBER: { + // TODO: add support or explicitly discard + } break; + case _Z_UNDECL_SUBSCRIBER: { + // TODO: add support or explicitly discard + } break; + case _Z_DECL_QUERYABLE: { + // TODO: add support or explicitly discard + } break; + case _Z_UNDECL_QUERYABLE: { + // TODO: add support or explicitly discard + } break; + case _Z_DECL_TOKEN: { + // TODO: add support or explicitly discard + } break; + case _Z_UNDECL_TOKEN: { + // TODO: add support or explicitly discard + } break; + case _Z_DECL_INTEREST: { + // TODO: add support or explicitly discard + } break; + case _Z_FINAL_INTEREST: { + // TODO: add support or explicitly discard + } break; + case _Z_UNDECL_INTEREST: { + // TODO: add support or explicitly discard + } break; + } } break; case _Z_N_PUSH: { _z_n_msg_push_t push = msg->_body._push; diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 7eeaf8f02..74486b9c2 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -19,6 +19,8 @@ #include "zenoh-pico/config.h" #include "zenoh-pico/protocol/codec/network.h" #include "zenoh-pico/protocol/codec/transport.h" +#include "zenoh-pico/protocol/definitions/network.h" +#include "zenoh-pico/protocol/definitions/transport.h" #include "zenoh-pico/session/utils.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" @@ -148,9 +150,12 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t } // Handle all the zenoh message, one by one + uint16_t mapping = entry->_peer_id; size_t len = _z_vec_len(&t_msg->_body._frame._messages); for (size_t i = 0; i < len; i++) { - _z_handle_zenoh_message(ztm->_session, _z_network_message_vec_get(&t_msg->_body._frame._messages, i)); + __auto_type zm = _z_network_message_vec_get(&t_msg->_body._frame._messages, i); + _z_msg_fix_mapping(zm, mapping); + _z_handle_zenoh_message(ztm->_session, zm, mapping); } break; @@ -189,7 +194,9 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t _z_zenoh_message_t zm; ret = _z_network_message_decode(&zm, &zbf); if (ret == _Z_RES_OK) { - _z_handle_zenoh_message(ztm->_session, &zm); + uint16_t mapping = entry->_peer_id; + _z_msg_fix_mapping(&zm, mapping); + _z_handle_zenoh_message(ztm->_session, &zm, mapping); _z_msg_clear(&zm); // Clear must be explicitly called for fragmented zenoh messages. Non-fragmented // zenoh messages are released when their transport message is released. } @@ -235,9 +242,9 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t entry->_sn_res = _z_sn_max(t_msg->_body._join._seq_num_res); // If the new node has less representing capabilities then it is incompatible to communication - if ((t_msg->_body._join._seq_num_res < Z_SN_RESOLUTION) || - (t_msg->_body._join._req_id_res < Z_REQ_RESOLUTION) || - (t_msg->_body._join._batch_size < Z_BATCH_SIZE)) { + if ((t_msg->_body._join._seq_num_res != Z_SN_RESOLUTION) || + (t_msg->_body._join._req_id_res != Z_REQ_RESOLUTION) || + (t_msg->_body._join._batch_size != Z_BATCH_SIZE)) { ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; } @@ -272,10 +279,11 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t entry->_received = true; // Check if the representing capabilities are still the same - if ((t_msg->_body._join._seq_num_res < Z_SN_RESOLUTION) || - (t_msg->_body._join._req_id_res < Z_REQ_RESOLUTION) || - (t_msg->_body._join._batch_size < Z_BATCH_SIZE)) { + if ((t_msg->_body._join._seq_num_res != Z_SN_RESOLUTION) || + (t_msg->_body._join._req_id_res != Z_REQ_RESOLUTION) || + (t_msg->_body._join._batch_size != Z_BATCH_SIZE)) { _z_transport_peer_entry_list_drop_filter(ztm->_peers, _z_transport_peer_entry_eq, entry); + // TODO: cleanup here should also be done on mappings/subs/etc... break; } diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index a5db3524d..8fa69a5b4 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -19,6 +19,7 @@ #include "zenoh-pico/config.h" #include "zenoh-pico/protocol/codec/network.h" #include "zenoh-pico/protocol/codec/transport.h" +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/session/utils.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" @@ -120,7 +121,8 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans size_t len = _z_vec_len(&t_msg->_body._frame._messages); for (size_t i = 0; i < len; i++) { _z_handle_zenoh_message(ztu->_session, - (_z_zenoh_message_t *)_z_vec_get(&t_msg->_body._frame._messages, i)); + (_z_zenoh_message_t *)_z_vec_get(&t_msg->_body._frame._messages, i), + _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); } break; @@ -155,7 +157,7 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans _z_zenoh_message_t zm; int8_t ret = _z_network_message_decode(&zm, &zbf); if (ret == _Z_RES_OK) { - _z_handle_zenoh_message(ztu->_session, &zm); + _z_handle_zenoh_message(ztu->_session, &zm, _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); _z_msg_clear(&zm); // Clear must be explicitly called for fragmented zenoh messages. Non-fragmented // zenoh messages are released when their transport message is released. } From 5cbe35a649b4682f2f9f7065bdab0094be059ea0 Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Thu, 3 Aug 2023 18:40:32 +0200 Subject: [PATCH 143/181] "docker rmi " of 'FROM' docker fails now (#235) --- BSDmakefile | 1 - GNUmakefile | 1 - 2 files changed, 2 deletions(-) diff --git a/BSDmakefile b/BSDmakefile index 9339f3620..5dc504035 100644 --- a/BSDmakefile +++ b/BSDmakefile @@ -89,7 +89,6 @@ crossbuild: check-docker cmake $(CMAKE_OPT) -DPACKAGING=DEB,RPM -DDEBARCH=$(DEBARCH) -DRPMARCH=$(RPMARCH) -B$(CROSSBUILD_DIR)/$(CROSSIMG) && \ make VERBOSE=1 -C$(CROSSBUILD_DIR)/$(CROSSIMG) all package" docker rmi $(CROSSIMG_PREFIX)$(CROSSIMG) - docker rmi dockcross/$(CROSSIMG) linux-armv5: CROSSIMG=$@ DEBARCH=arm RPMARCH=arm make crossbuild diff --git a/GNUmakefile b/GNUmakefile index 28f9eb062..6ea060ed9 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -89,7 +89,6 @@ crossbuild: check-docker cmake $(CMAKE_OPT) -DPACKAGING=DEB,RPM -DDEBARCH=$(DEBARCH) -DRPMARCH=$(RPMARCH) -B$(CROSSBUILD_DIR)/$(CROSSIMG) && \ make VERBOSE=1 -C$(CROSSBUILD_DIR)/$(CROSSIMG) all package" docker rmi $(CROSSIMG_PREFIX)$(CROSSIMG) - docker rmi dockcross/$(CROSSIMG) linux-armv5: CROSSIMG=$@ DEBARCH=arm RPMARCH=arm make crossbuild From ba21bd74950e65aa993110988293d65bd013b57f Mon Sep 17 00:00:00 2001 From: ChenYing Kuo Date: Fri, 4 Aug 2023 15:45:49 +0800 Subject: [PATCH 144/181] Use .readthedocs.yaml for building ReadTheDocs's content. (#236) --- .readthedocs.yaml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .readthedocs.yaml diff --git a/.readthedocs.yaml b/.readthedocs.yaml new file mode 100644 index 000000000..f3aa1f5af --- /dev/null +++ b/.readthedocs.yaml @@ -0,0 +1,22 @@ +# .readthedocs.yaml +# Read the Docs configuration file +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the version of Python and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.11" + +# Build documentation in the docs/ directory with Sphinx +sphinx: + configuration: docs/conf.py + +# We recommend specifying your dependencies to enable reproducible builds: +# https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html +# python: +# install: +# - requirements: docs/requirements.txt From b68364fb66dadefa9fd73cea56deb7d09cccdd92 Mon Sep 17 00:00:00 2001 From: ChenYing Kuo Date: Fri, 4 Aug 2023 20:23:42 +0800 Subject: [PATCH 145/181] Fix the ReadTheDocs clang dependency (#237) --- .readthedocs.yaml | 8 +++++--- docs/conf.py | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.readthedocs.yaml b/.readthedocs.yaml index f3aa1f5af..71e28a0e8 100644 --- a/.readthedocs.yaml +++ b/.readthedocs.yaml @@ -10,6 +10,8 @@ build: os: ubuntu-22.04 tools: python: "3.11" + apt_packages: + - libclang-dev # Build documentation in the docs/ directory with Sphinx sphinx: @@ -17,6 +19,6 @@ sphinx: # We recommend specifying your dependencies to enable reproducible builds: # https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html -# python: -# install: -# - requirements: docs/requirements.txt +python: + install: + - requirements: docs/requirements.txt diff --git a/docs/conf.py b/docs/conf.py index 37ff6729d..1d5c7d92c 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -48,4 +48,4 @@ raise ValueError("Windows not supported yet for building docs.") else: - Config.set_library_file('/usr/lib/llvm-6.0/lib/libclang.so.1') # Required for readthedocs + Config.set_library_file('/usr/lib/llvm-14/lib/libclang.so.1') # Required for readthedocs From cea88938c443b7396404a13145e58ab65d79d791 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 7 Aug 2023 11:52:43 +0200 Subject: [PATCH 146/181] actually pass the responses back to the user --- .../zenoh-pico/protocol/definitions/network.h | 19 +--------- include/zenoh-pico/session/query.h | 8 ++-- src/net/primitives.c | 5 ++- src/protocol/definitions/network.c | 18 +++++++++ src/session/query.c | 11 +++--- src/session/rx.c | 37 ++++++++++++++++++- 6 files changed, 66 insertions(+), 32 deletions(-) diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index bc5927a23..e439b276a 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -227,24 +227,7 @@ inline static void _z_msg_free(_z_zenoh_message_t **msg) { _z_n_msg_free(msg); } _Z_ELEM_DEFINE(_z_network_message, _z_network_message_t, _z_noop_size, _z_n_msg_clear, _z_noop_copy) _Z_VEC_DEFINE(_z_network_message, _z_network_message_t) -void _z_msg_fix_mapping(_z_zenoh_message_t *msg, uint16_t mapping) { - switch (msg->_tag) { - case _Z_N_DECLARE: { - _z_decl_fix_mapping(&msg->_body._declare._decl, mapping); - } break; - case _Z_N_PUSH: { - _z_keyexpr_fix_mapping(&msg->_body._push._key, mapping); - } break; - case _Z_N_REQUEST: { - _z_keyexpr_fix_mapping(&msg->_body._request._key, mapping); - } break; - case _Z_N_RESPONSE: { - _z_keyexpr_fix_mapping(&msg->_body._response._key, mapping); - } break; - default: - break; - } -} +void _z_msg_fix_mapping(_z_zenoh_message_t *msg, uint16_t mapping); _z_network_message_t _z_msg_make_pull(_z_keyexpr_t key, _z_zint_t pull_id); _z_network_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes_t) parameters, _z_zint_t qid, z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value); diff --git a/include/zenoh-pico/session/query.h b/include/zenoh-pico/session/query.h index e30f23af9..c5597c3c8 100644 --- a/include/zenoh-pico/session/query.h +++ b/include/zenoh-pico/session/query.h @@ -16,6 +16,7 @@ #define ZENOH_PICO_SESSION_QUERY_H #include "zenoh-pico/net/session.h" +#include "zenoh-pico/protocol/core.h" /*------------------ Query ------------------*/ _z_zint_t _z_get_query_id(_z_session_t *zn); @@ -23,11 +24,10 @@ _z_zint_t _z_get_query_id(_z_session_t *zn); _z_pending_query_t *_z_get_pending_query_by_id(_z_session_t *zn, const _z_zint_t id); int8_t _z_register_pending_query(_z_session_t *zn, _z_pending_query_t *pq); -int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_reply_context_t *reply_context, - const _z_keyexpr_t keyexpr, const _z_bytes_t payload, - const _z_encoding_t encoding, const _z_zint_t kind, +int8_t _z_trigger_query_reply_partial(_z_session_t *zn, _z_zint_t reply_context, 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); -int8_t _z_trigger_query_reply_final(_z_session_t *zn, const _z_reply_context_t *reply_context); +int8_t _z_trigger_query_reply_final(_z_session_t *zn, _z_zint_t id); void _z_unregister_pending_query(_z_session_t *zn, _z_pending_query_t *pq); void _z_flush_pending_queries(_z_session_t *zn); diff --git a/src/net/primitives.c b/src/net/primitives.c index 0510c7512..f84cf73ca 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -67,7 +67,7 @@ uint16_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr) { if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { ret = r->_id; } else { - _z_unregister_resource(zn, r); + _z_unregister_resource(zn, r->_id, _z_keyexpr_mapping_id(&keyexpr)); } _z_n_msg_clear(&n_msg); } else { @@ -88,7 +88,8 @@ int8_t _z_undeclare_resource(_z_session_t *zn, uint16_t rid) { _z_declaration_t declaration = _z_make_undecl_keyexpr(rid); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { - _z_unregister_resource(zn, r); // Only if message is send, local resource is removed + _z_unregister_resource(zn, rid, + _Z_KEYEXPR_MAPPING_LOCAL); // Only if message is send, local resource is removed } else { ret = _Z_ERR_TRANSPORT_TX_FAILED; } diff --git a/src/protocol/definitions/network.c b/src/protocol/definitions/network.c index c161847d0..af852e4e2 100644 --- a/src/protocol/definitions/network.c +++ b/src/protocol/definitions/network.c @@ -238,3 +238,21 @@ _z_network_message_t _z_n_msg_make_ack(_z_zint_t rid, _Z_MOVE(_z_keyexpr_t) key) }, }; } +void _z_msg_fix_mapping(_z_zenoh_message_t *msg, uint16_t mapping) { + switch (msg->_tag) { + case _Z_N_DECLARE: { + _z_decl_fix_mapping(&msg->_body._declare._decl, mapping); + } break; + case _Z_N_PUSH: { + _z_keyexpr_fix_mapping(&msg->_body._push._key, mapping); + } break; + case _Z_N_REQUEST: { + _z_keyexpr_fix_mapping(&msg->_body._request._key, mapping); + } break; + case _Z_N_RESPONSE: { + _z_keyexpr_fix_mapping(&msg->_body._response._key, mapping); + } break; + default: + break; + } +} diff --git a/src/session/query.c b/src/session/query.c index 92d007211..17a8667db 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -138,9 +138,8 @@ int8_t _z_register_pending_query(_z_session_t *zn, _z_pending_query_t *pen_qry) return ret; } -int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_reply_context_t *reply_context, - const _z_keyexpr_t keyexpr, const _z_bytes_t payload, - const _z_encoding_t encoding, const _z_zint_t kind, +int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_zint_t id, 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) { int8_t ret = _Z_RES_OK; @@ -148,7 +147,7 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_reply_context_t _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - _z_pending_query_t *pen_qry = __unsafe__z_get_pending_query_by_id(zn, reply_context->_request_id); + _z_pending_query_t *pen_qry = __unsafe__z_get_pending_query_by_id(zn, id); if ((ret == _Z_RES_OK) && (pen_qry == NULL)) { ret = _Z_ERR_ENTITY_UNKNOWN; } @@ -232,7 +231,7 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_reply_context_t return ret; } -int8_t _z_trigger_query_reply_final(_z_session_t *zn, const _z_reply_context_t *reply_context) { +int8_t _z_trigger_query_reply_final(_z_session_t *zn, _z_zint_t id) { int8_t ret = _Z_RES_OK; #if Z_MULTI_THREAD == 1 @@ -240,7 +239,7 @@ int8_t _z_trigger_query_reply_final(_z_session_t *zn, const _z_reply_context_t * #endif // Z_MULTI_THREAD == 1 // Final reply received for unknown query id - _z_pending_query_t *pen_qry = __unsafe__z_get_pending_query_by_id(zn, reply_context->_request_id); + _z_pending_query_t *pen_qry = __unsafe__z_get_pending_query_by_id(zn, id); if ((ret == _Z_RES_OK) && (pen_qry == NULL)) { ret = _Z_ERR_ENTITY_UNKNOWN; } diff --git a/src/session/rx.c b/src/session/rx.c index ce452c785..c9590c46b 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -17,7 +17,9 @@ #include "zenoh-pico/api/constants.h" #include "zenoh-pico/api/primitives.h" +#include "zenoh-pico/api/types.h" #include "zenoh-pico/collections/bytes.h" +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/definitions/message.h" #include "zenoh-pico/protocol/definitions/network.h" #include "zenoh-pico/protocol/keyexpr.h" @@ -103,7 +105,7 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 case _Z_REQUEST_DEL: { _z_msg_del_t del = req._body._del; int8_t result = _z_trigger_subscriptions(zn, req._key, _z_bytes_empty(), z_encoding_default(), - Z_SAMPLE_KIND_PUT, del._commons._timestamp); + Z_SAMPLE_KIND_DELETE, del._commons._timestamp); if (ret == _Z_RES_OK) { _z_network_message_t ack = _z_n_msg_make_ack(req._rid, &req._key); ret = _z_send_n_msg(zn, &ack, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK); @@ -111,13 +113,44 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 ret |= _z_send_n_msg(zn, &final, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK); } } break; - case _Z_REQUEST_PULL: { // TODO + case _Z_REQUEST_PULL: { + // @TODO: define behaviour } break; } } break; case _Z_N_RESPONSE: { + _z_n_msg_response_t response = msg->_body._response; + switch (response._tag) { + case _Z_RESPONSE_BODY_REPLY: { + _z_msg_reply_t reply = response._body._reply; + _z_trigger_query_reply_partial(zn, response._request_id, response._key, reply._value.payload, + reply._value.encoding, Z_SAMPLE_KIND_PUT, reply._timestamp); + } break; + case _Z_RESPONSE_BODY_ERR: { + // @TODO: expose errors to the user + _z_msg_err_t error = response._body._err; + _z_bytes_t payload = error._ext_value.payload; + _Z_ERROR("Received Err for query %d: code=%d, message=%.*s\n", response._request_id, error.code, + payload.len, payload.start); + } break; + case _Z_RESPONSE_BODY_ACK: { + // @TODO: implement ACKs for puts/dels + } break; + case _Z_RESPONSE_BODY_PUT: { + _z_msg_put_t put = response._body._put; + ret = _z_trigger_subscriptions(zn, response._key, put._payload, put._encoding, Z_SAMPLE_KIND_PUT, + put._commons._timestamp); + } break; + case _Z_RESPONSE_BODY_DEL: { + _z_msg_del_t del = response._body._del; + int8_t result = _z_trigger_subscriptions(zn, response._key, _z_bytes_empty(), z_encoding_default(), + Z_SAMPLE_KIND_DELETE, del._commons._timestamp); + } break; + } } break; case _Z_N_RESPONSE_FINAL: { + _z_zint_t id = msg->_body._response_final._request_id; + _z_trigger_query_reply_final(zn, id); } break; } _z_msg_clear(msg); From 2c3677ab136539b0052272e142080237405d129e Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 7 Aug 2023 12:16:22 +0200 Subject: [PATCH 147/181] fixes --- src/session/rx.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/session/rx.c b/src/session/rx.c index c9590c46b..b99c3d162 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -20,6 +20,7 @@ #include "zenoh-pico/api/types.h" #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/definitions/declarations.h" #include "zenoh-pico/protocol/definitions/message.h" #include "zenoh-pico/protocol/definitions/network.h" #include "zenoh-pico/protocol/keyexpr.h" @@ -130,7 +131,7 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 // @TODO: expose errors to the user _z_msg_err_t error = response._body._err; _z_bytes_t payload = error._ext_value.payload; - _Z_ERROR("Received Err for query %d: code=%d, message=%.*s\n", response._request_id, error.code, + _Z_ERROR("Received Err for query %d: code=%d, message=%.*s\n", response._request_id, error._code, payload.len, payload.start); } break; case _Z_RESPONSE_BODY_ACK: { From e0022436f55d27644246715238a547d9008f2879 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 7 Aug 2023 12:29:09 +0200 Subject: [PATCH 148/181] missed an auto --- src/transport/multicast/link/rx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 74486b9c2..54cbcc214 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -153,7 +153,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t uint16_t mapping = entry->_peer_id; size_t len = _z_vec_len(&t_msg->_body._frame._messages); for (size_t i = 0; i < len; i++) { - __auto_type zm = _z_network_message_vec_get(&t_msg->_body._frame._messages, i); + _z_network_message_t *zm = _z_network_message_vec_get(&t_msg->_body._frame._messages, i); _z_msg_fix_mapping(zm, mapping); _z_handle_zenoh_message(ztm->_session, zm, mapping); } From f10b80670a4c5f80c04b0645175a51d8962eba8a Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 7 Aug 2023 12:43:48 +0200 Subject: [PATCH 149/181] move _z_decl_fix_mapping out of line so bad compilers might understand it better? --- .../protocol/definitions/declarations.h | 34 +------------------ src/net/primitives.c | 5 --- src/protocol/codec/declarations.c | 2 +- src/protocol/codec/ext.c | 2 -- src/protocol/codec/message.c | 1 - src/protocol/definitions/declarations.c | 33 ++++++++++++++++++ 6 files changed, 35 insertions(+), 42 deletions(-) diff --git a/include/zenoh-pico/protocol/definitions/declarations.h b/include/zenoh-pico/protocol/definitions/declarations.h index 8cd5c8406..9cb5e05e5 100644 --- a/include/zenoh-pico/protocol/definitions/declarations.h +++ b/include/zenoh-pico/protocol/definitions/declarations.h @@ -123,39 +123,7 @@ typedef struct { } _body; } _z_declaration_t; void _z_declaration_clear(_z_declaration_t* decl); -static inline void _z_decl_fix_mapping(_z_declaration_t* msg, uint16_t mapping) { - switch (msg->_tag) { - case _Z_DECL_KEXPR: { - _z_keyexpr_fix_mapping(&msg->_body._decl_kexpr._keyexpr, mapping); - } break; - case _Z_DECL_SUBSCRIBER: { - _z_keyexpr_fix_mapping(&msg->_body._decl_subscriber._keyexpr, mapping); - } break; - case _Z_UNDECL_SUBSCRIBER: { - _z_keyexpr_fix_mapping(&msg->_body._undecl_subscriber._ext_keyexpr, mapping); - } break; - case _Z_DECL_QUERYABLE: { - _z_keyexpr_fix_mapping(&msg->_body._decl_queryable._keyexpr, mapping); - } break; - case _Z_UNDECL_QUERYABLE: { - _z_keyexpr_fix_mapping(&msg->_body._undecl_queryable._ext_keyexpr, mapping); - } break; - case _Z_DECL_TOKEN: { - _z_keyexpr_fix_mapping(&msg->_body._decl_token._keyexpr, mapping); - } break; - case _Z_UNDECL_TOKEN: { - _z_keyexpr_fix_mapping(&msg->_body._undecl_token._ext_keyexpr, mapping); - } break; - case _Z_DECL_INTEREST: { - _z_keyexpr_fix_mapping(&msg->_body._decl_interest._keyexpr, mapping); - } break; - case _Z_UNDECL_INTEREST: { - _z_keyexpr_fix_mapping(&msg->_body._undecl_interest._ext_keyexpr, mapping); - } break; - default: - break; - } -} +void _z_decl_fix_mapping(_z_declaration_t* msg, uint16_t mapping); _z_declaration_t _z_make_decl_keyexpr(uint16_t id, _Z_MOVE(_z_keyexpr_t) key); _z_declaration_t _z_make_undecl_keyexpr(uint16_t id); diff --git a/src/net/primitives.c b/src/net/primitives.c index f84cf73ca..5444426a2 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -316,9 +316,6 @@ int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *pay const _z_encoding_t encoding, const z_sample_kind_t kind, const z_congestion_control_t cong_ctrl, z_priority_t priority) { int8_t ret = _Z_RES_OK; - - _z_bytes_t pld = {.len = len, .start = payload}; // Payload - _Bool can_be_dropped = cong_ctrl == Z_CONGESTION_CONTROL_DROP; // Congestion control _z_network_message_t msg; switch (kind) { case Z_SAMPLE_KIND_PUT: @@ -410,8 +407,6 @@ int8_t _z_subscriber_pull(const _z_subscriber_t *sub) { _z_subscription_sptr_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_id); if (s != NULL) { _z_zint_t pull_id = _z_get_pull_id(sub->_zn); - _z_zint_t max_samples = 0; // @TODO: get the correct value for max_sample - _Bool is_final = true; _z_zenoh_message_t z_msg = _z_msg_make_pull(_z_keyexpr_alias(s->ptr->_key), pull_id); if (_z_send_n_msg(sub->_zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { ret = _Z_ERR_TRANSPORT_TX_FAILED; diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index b24c8bd64..9c2805e28 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -147,7 +147,7 @@ int8_t _z_undecl_interest_encode(_z_wbuf_t *wbf, const _z_undecl_interest_t *dec return _z_undecl_encode(wbf, _Z_UNDECL_INTEREST_MID, decl->_id, decl->_ext_keyexpr); } int8_t _z_declaration_encode(_z_wbuf_t *wbf, const _z_declaration_t *decl) { - int8_t ret; + int8_t ret = _Z_RES_OK; switch (decl->_tag) { case _Z_DECL_KEXPR: { ret = _z_decl_kexpr_encode(wbf, &decl->_body._decl_kexpr); diff --git a/src/protocol/codec/ext.c b/src/protocol/codec/ext.c index b87fa5441..2911e2de4 100644 --- a/src/protocol/codec/ext.c +++ b/src/protocol/codec/ext.c @@ -161,7 +161,6 @@ int8_t _z_msg_ext_vec_push_callback(_z_msg_ext_t *extension, _z_msg_ext_vec_t *e return 0; } int8_t _z_msg_ext_vec_decode(_z_msg_ext_vec_t *extensions, _z_zbuf_t *zbf) { - int8_t ret = _Z_RES_OK; _z_msg_ext_vec_reset(extensions); return _z_msg_ext_decode_iter(zbf, (int8_t(*)(_z_msg_ext_t *, void *))_z_msg_ext_vec_push_callback, (void *)extensions); @@ -215,7 +214,6 @@ int8_t _z_msg_ext_decode_iter(_z_zbuf_t *zbf, int8_t (*callback)(_z_msg_ext_t *, _Bool has_next = true; while (has_next && ret == _Z_RES_OK) { _z_msg_ext_t ext = _z_msg_ext_make_unit(0); - size_t start = zbf->_ios._r_pos; ret |= _z_msg_ext_decode(&ext, zbf, &has_next); if (ret == _Z_RES_OK) { ret |= callback(&ext, context); diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 901bf829d..52658cf98 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -92,7 +92,6 @@ 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) { - int8_t ret = _Z_RES_OK; _Z_RETURN_IF_ERR(_z_zint_encode(wbf, _z_zint_len(ts->time) + 1 + _z_id_len(ts->id))); return _z_timestamp_encode(wbf, ts); } diff --git a/src/protocol/definitions/declarations.c b/src/protocol/definitions/declarations.c index ca1572bcd..7572f5a24 100644 --- a/src/protocol/definitions/declarations.c +++ b/src/protocol/definitions/declarations.c @@ -127,3 +127,36 @@ _z_undecl_queryable_t _z_undecl_queryable_null(void) { return (_z_undecl_queryab _z_undecl_token_t _z_undecl_token_null(void) { return (_z_undecl_token_t){0}; } _z_undecl_interest_t _z_undecl_interest_null(void) { return (_z_undecl_interest_t){0}; } _z_final_interest_t _z_final_interest_null(void) { return (_z_final_interest_t){0}; } +void _z_decl_fix_mapping(_z_declaration_t* msg, uint16_t mapping) { + switch (msg->_tag) { + case _Z_DECL_KEXPR: { + _z_keyexpr_fix_mapping(&msg->_body._decl_kexpr._keyexpr, mapping); + } break; + case _Z_DECL_SUBSCRIBER: { + _z_keyexpr_fix_mapping(&msg->_body._decl_subscriber._keyexpr, mapping); + } break; + case _Z_UNDECL_SUBSCRIBER: { + _z_keyexpr_fix_mapping(&msg->_body._undecl_subscriber._ext_keyexpr, mapping); + } break; + case _Z_DECL_QUERYABLE: { + _z_keyexpr_fix_mapping(&msg->_body._decl_queryable._keyexpr, mapping); + } break; + case _Z_UNDECL_QUERYABLE: { + _z_keyexpr_fix_mapping(&msg->_body._undecl_queryable._ext_keyexpr, mapping); + } break; + case _Z_DECL_TOKEN: { + _z_keyexpr_fix_mapping(&msg->_body._decl_token._keyexpr, mapping); + } break; + case _Z_UNDECL_TOKEN: { + _z_keyexpr_fix_mapping(&msg->_body._undecl_token._ext_keyexpr, mapping); + } break; + case _Z_DECL_INTEREST: { + _z_keyexpr_fix_mapping(&msg->_body._decl_interest._keyexpr, mapping); + } break; + case _Z_UNDECL_INTEREST: { + _z_keyexpr_fix_mapping(&msg->_body._undecl_interest._ext_keyexpr, mapping); + } break; + default: + break; + } +} From 7773a433725a89ff0633787dc1305cbacac07692 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 7 Aug 2023 15:19:06 +0200 Subject: [PATCH 150/181] fix lints for embedded compilers --- include/zenoh-pico/protocol/core.h | 2 +- src/protocol/codec/network.c | 1 + src/session/rx.c | 14 +++++++------- src/transport/multicast/link/rx.c | 3 --- src/transport/unicast/link/rx.c | 3 --- src/transport/utils.c | 6 ++++-- 6 files changed, 13 insertions(+), 16 deletions(-) diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index fabdcb6cd..45ac5f731 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -109,7 +109,7 @@ static inline _Bool _z_keyexpr_is_local(const _z_keyexpr_t *key) { } static inline _z_mapping_t _z_keyexpr_mapping(uint16_t id, _Bool owns_suffix) { assert(id <= _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); - return (_z_mapping_t){._val = (owns_suffix ? 0x8000 : 0) | id}; + return (_z_mapping_t){._val = (owns_suffix ? (uint16_t)0x8000 : (uint16_t)0) | id}; } static inline void _z_keyexpr_set_mapping(_z_keyexpr_t *ke, uint16_t id) { assert(id <= _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index 61769bdb0..857e391aa 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -486,6 +486,7 @@ int8_t _z_network_message_encode(_z_wbuf_t *wbf, const _z_network_message_t *msg return _z_response_final_encode(wbf, &msg->_body._response_final); } break; } + return _Z_ERR_GENERIC; } int8_t _z_network_message_decode(_z_network_message_t *msg, _z_zbuf_t *zbf) { uint8_t header; diff --git a/src/session/rx.c b/src/session/rx.c index b99c3d162..1abd30e98 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -43,7 +43,7 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 _z_resource_t *res = (_z_resource_t *)z_malloc(sizeof(_z_resource_t)); res->_id = decl._decl._body._decl_kexpr._id; res->_key = decl._decl._body._decl_kexpr._keyexpr; - _z_register_resource(zn, res); + ret = _z_register_resource(zn, res); } break; case _Z_UNDECL_KEXPR: { _z_resource_t res = {._id = decl._decl._body._undecl_kexpr._id}; @@ -105,8 +105,8 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 } break; case _Z_REQUEST_DEL: { _z_msg_del_t del = req._body._del; - int8_t result = _z_trigger_subscriptions(zn, req._key, _z_bytes_empty(), z_encoding_default(), - Z_SAMPLE_KIND_DELETE, del._commons._timestamp); + ret = _z_trigger_subscriptions(zn, req._key, _z_bytes_empty(), z_encoding_default(), + Z_SAMPLE_KIND_DELETE, del._commons._timestamp); if (ret == _Z_RES_OK) { _z_network_message_t ack = _z_n_msg_make_ack(req._rid, &req._key); ret = _z_send_n_msg(zn, &ack, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK); @@ -124,8 +124,8 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 switch (response._tag) { case _Z_RESPONSE_BODY_REPLY: { _z_msg_reply_t reply = response._body._reply; - _z_trigger_query_reply_partial(zn, response._request_id, response._key, reply._value.payload, - reply._value.encoding, Z_SAMPLE_KIND_PUT, reply._timestamp); + ret = _z_trigger_query_reply_partial(zn, response._request_id, response._key, reply._value.payload, + reply._value.encoding, Z_SAMPLE_KIND_PUT, reply._timestamp); } break; case _Z_RESPONSE_BODY_ERR: { // @TODO: expose errors to the user @@ -144,8 +144,8 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 } break; case _Z_RESPONSE_BODY_DEL: { _z_msg_del_t del = response._body._del; - int8_t result = _z_trigger_subscriptions(zn, response._key, _z_bytes_empty(), z_encoding_default(), - Z_SAMPLE_KIND_DELETE, del._commons._timestamp); + ret = _z_trigger_subscriptions(zn, response._key, _z_bytes_empty(), z_encoding_default(), + Z_SAMPLE_KIND_DELETE, del._commons._timestamp); } break; } } break; diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 54cbcc214..6c90b0b92 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -89,9 +89,6 @@ int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_me } } while (false); // The 1-iteration loop to use continue to break the entire loop on error - // Wrap the main buffer for to_read bytes - _z_zbuf_t zbuf = _z_zbuf_view(&ztm->_zbuf, to_read); - if (ret == _Z_RES_OK) { _Z_DEBUG(">> \t transport_message_decode\n"); ret = _z_transport_message_decode(t_msg, &ztm->_zbuf); diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index 8fa69a5b4..8a58147a0 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -68,9 +68,6 @@ int8_t _z_unicast_recv_t_msg_na(_z_transport_unicast_t *ztu, _z_transport_messag } } while (false); // The 1-iteration loop to use continue to break the entire loop on error - // Wrap the main buffer for to_read bytes - _z_zbuf_t zbuf = _z_zbuf_view(&ztu->_zbuf, to_read); - if (ret == _Z_RES_OK) { _Z_DEBUG(">> \t transport_message_decode\n"); ret = _z_transport_message_decode(t_msg, &ztu->_zbuf); diff --git a/src/transport/utils.c b/src/transport/utils.c index 87d589139..9e3ad968c 100644 --- a/src/transport/utils.c +++ b/src/transport/utils.c @@ -14,6 +14,8 @@ #include "zenoh-pico/transport/utils.h" +#include "zenoh-pico/protocol/core.h" + #define U8_MAX 0xFF #define U16_MAX 0xFFFF #define U32_MAX 0xFFFFFFFF @@ -35,7 +37,7 @@ _z_zint_t _z_sn_max(uint8_t bits) { } break; case 0x03: { - ret = U64_MAX >> 1; + ret = (_z_zint_t)(U64_MAX >> 1); } break; default: { @@ -64,7 +66,7 @@ _z_zint_t _z_sn_modulo_mask(uint8_t bits) { } break; case 0x03: { - ret = U64_MAX >> 1; + ret = (_z_zint_t)(U64_MAX >> 1); } break; default: { From c65ce08b1cc89698cc67f0ccaf4e2eb101c445e5 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 7 Aug 2023 16:53:34 +0200 Subject: [PATCH 151/181] fix builds on embedded platforms --- examples/arduino/z_scout.ino | 13 ++++++++++++- examples/mbed/z_scout.cpp | 14 +++++++++++++- include/zenoh-pico/protocol/core.h | 2 +- src/protocol/codec/ext.c | 2 +- src/session/rx.c | 1 - 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/examples/arduino/z_scout.ino b/examples/arduino/z_scout.ino index c57608159..632b50295 100644 --- a/examples/arduino/z_scout.ino +++ b/examples/arduino/z_scout.ino @@ -20,8 +20,19 @@ #define SSID "SSID" #define PASS "PASS" +uint8_t zid_len(z_id_t id) { + uint8_t len = 16; + while (len > 0) { + --len; + if (id.id[len] != 0) { + ++len; + break; + } + } + return len; +} void fprintzid(z_id_t zid) { - unsigned int zidlen = _z_id_len(zid); + unsigned int zidlen = zid_len(zid); if (zidlen == 0) { Serial.print("None"); } else { diff --git a/examples/mbed/z_scout.cpp b/examples/mbed/z_scout.cpp index 809df76e3..c3cdf5d41 100644 --- a/examples/mbed/z_scout.cpp +++ b/examples/mbed/z_scout.cpp @@ -16,8 +16,20 @@ #include #include +uint8_t zid_len(z_id_t id) { + uint8_t len = 16; + while (len > 0) { + --len; + if (id.id[len] != 0) { + ++len; + break; + } + } + return len; +} + void fprintzid(FILE *stream, z_id_t zid) { - unsigned int zidlen = _z_id_len(zid); + unsigned int zidlen = zid_len(zid); if (zidlen == 0) { fprintf(stream, "None"); } else { diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 45ac5f731..de72c49c7 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -109,7 +109,7 @@ static inline _Bool _z_keyexpr_is_local(const _z_keyexpr_t *key) { } static inline _z_mapping_t _z_keyexpr_mapping(uint16_t id, _Bool owns_suffix) { assert(id <= _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); - return (_z_mapping_t){._val = (owns_suffix ? (uint16_t)0x8000 : (uint16_t)0) | id}; + return (_z_mapping_t){._val = (uint16_t)((owns_suffix ? 0x8000 : 0) | id)}; } static inline void _z_keyexpr_set_mapping(_z_keyexpr_t *ke, uint16_t id) { assert(id <= _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); diff --git a/src/protocol/codec/ext.c b/src/protocol/codec/ext.c index 2911e2de4..fd4f478f2 100644 --- a/src/protocol/codec/ext.c +++ b/src/protocol/codec/ext.c @@ -143,7 +143,7 @@ int8_t _z_msg_ext_vec_encode(_z_wbuf_t *wbf, const _z_msg_ext_vec_t *extensions) int8_t ret = _Z_RES_OK; size_t len = _z_msg_ext_vec_len(extensions); if (len > 0) { - int i; + size_t i; for (i = 0; ret == _Z_RES_OK && i < len - 1; i++) { ret |= _z_msg_ext_encode(wbf, _z_msg_ext_vec_get(extensions, i), true); } diff --git a/src/session/rx.c b/src/session/rx.c index 1abd30e98..2573569a9 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -46,7 +46,6 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 ret = _z_register_resource(zn, res); } break; case _Z_UNDECL_KEXPR: { - _z_resource_t res = {._id = decl._decl._body._undecl_kexpr._id}; _z_unregister_resource(zn, decl._decl._body._undecl_kexpr._id, local_peer_id); } break; case _Z_DECL_SUBSCRIBER: { From 6c3ae1a147464b19bc423578658a2578c71cb8e2 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 9 Aug 2023 09:29:09 +0200 Subject: [PATCH 152/181] fix some constructors [skip ci] --- src/net/session.c | 1 + src/transport/multicast/link/task/join.c | 2 +- src/transport/transport.c | 1 + zenohpico.pc | 2 +- 4 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/net/session.c b/src/net/session.c index cd26c4392..7f60ac90c 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -126,6 +126,7 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { _z_str_array_clear(&locators); } else { _Z_ERROR("A valid config is missing.\n"); + ret = _Z_ERR_GENERIC; } return ret; diff --git a/src/transport/multicast/link/task/join.c b/src/transport/multicast/link/task/join.c index dedc7f49c..345ab689c 100644 --- a/src/transport/multicast/link/task/join.c +++ b/src/transport/multicast/link/task/join.c @@ -21,7 +21,7 @@ int8_t _zp_multicast_send_join(_z_transport_multicast_t *ztm) { _z_conduit_sn_list_t next_sn; - next_sn._is_qos = false; // FIXME: make transport aware of qos configuration + 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; diff --git a/src/transport/transport.c b/src/transport/transport.c index 11ce9ea74..7ee258063 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -382,6 +382,7 @@ int8_t _z_transport_multicast_open_peer(_z_transport_multicast_establish_param_t initial_sn_tx = initial_sn_tx & !_z_sn_modulo_mask(Z_SN_RESOLUTION); _z_conduit_sn_list_t next_sn; + next_sn._is_qos = false; next_sn._val._plain._best_effort = initial_sn_tx; next_sn._val._plain._reliable = initial_sn_tx; diff --git a/zenohpico.pc b/zenohpico.pc index 66eab5547..60d1fd439 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.10.20230807dev +Version: 0.10.20230809dev Cflags: -I${prefix}/ Libs: -L${prefix}/ -lzenohpico From a0f01131188257ca9ea031c754f242f70823ee70 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 11 Aug 2023 12:01:35 +0200 Subject: [PATCH 153/181] re-support multicast and datagram transports --- include/zenoh-pico/config.h | 16 +++++++++---- .../zenoh-pico/protocol/definitions/core.h | 3 ++- .../protocol/definitions/transport.h | 4 ++-- src/net/session.c | 1 - src/protocol/codec/message.c | 2 +- src/protocol/codec/transport.c | 23 ++++--------------- src/protocol/definitions/transport.c | 14 +++++------ src/protocol/iobuf.c | 6 ++++- src/session/scout.c | 4 ++-- src/transport/common/rx.c | 2 +- src/transport/common/tx.c | 2 +- src/transport/multicast/link/rx.c | 4 ++-- src/transport/transport.c | 17 +++++++------- zenohpico.pc | 2 +- 14 files changed, 49 insertions(+), 51 deletions(-) diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index 5ce74f664..23a929993 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -12,8 +12,8 @@ // ZettaScale Zenoh Team, // -#ifndef ZENOH_PICO_CONFIG_H -#define ZENOH_PICO_CONFIG_H +#ifndef INCLUDE_ZENOH_PICO_CONFIG_H +#define INCLUDE_ZENOH_PICO_CONFIG_H /*------------------ Runtime configuration properties ------------------*/ /** @@ -225,8 +225,14 @@ /** * Defaulf maximum batch size possible to be received or sent. */ -#ifndef Z_BATCH_SIZE -#define Z_BATCH_SIZE 65535 +#ifndef Z_BATCH_UNICAST_SIZE +#define Z_BATCH_UNICAST_SIZE 65535 +#endif +/** + * Defaulf maximum batch size possible to be received or sent. + */ +#ifndef Z_BATCH_MULTICAST_SIZE +#define Z_BATCH_MULTICAST_SIZE 8192 #endif /** @@ -265,4 +271,4 @@ #endif #endif -#endif /* ZENOH_PICO_CONFIG_H */ +#endif /* INCLUDE_ZENOH_PICO_CONFIG_H */ diff --git a/include/zenoh-pico/protocol/definitions/core.h b/include/zenoh-pico/protocol/definitions/core.h index 5c97d1427..58d16a430 100644 --- a/include/zenoh-pico/protocol/definitions/core.h +++ b/include/zenoh-pico/protocol/definitions/core.h @@ -17,7 +17,8 @@ #include "zenoh-pico/protocol/core.h" -#define _Z_DEFAULT_BATCH_SIZE 65535 +#define _Z_DEFAULT_UNICAST_BATCH_SIZE 65535 +#define _Z_DEFAULT_MULTICAST_BATCH_SIZE 8192 #define _Z_DEFAULT_RESOLUTION_SIZE 2 #define _Z_DECLARE_CLEAR(layer, name) void _z_##layer##_msg_clear_##name(_z_##name##_t *m, uint8_t header) diff --git a/include/zenoh-pico/protocol/definitions/transport.h b/include/zenoh-pico/protocol/definitions/transport.h index 521efd821..c3f1b3270 100644 --- a/include/zenoh-pico/protocol/definitions/transport.h +++ b/include/zenoh-pico/protocol/definitions/transport.h @@ -53,8 +53,8 @@ // T Lease period if T==1 then the lease period is in seconds else in milliseconds // S Size params if S==1 then size parameters are exchanged // Z Extensions if Z==1 then Zenoh extensions are present -#define _Z_FLAG_T_JOIN_T 0x40 // 1 << 6 -#define _Z_FLAG_T_JOIN_S 0x20 // 1 << 5 +#define _Z_FLAG_T_JOIN_T 0x20 // 1 << 5 +#define _Z_FLAG_T_JOIN_S 0x40 // 1 << 6 // Init message flags: // A Ack if A==1 then the message is an acknowledgment (aka InitAck), otherwise InitSyn diff --git a/src/net/session.c b/src/net/session.c index 7f60ac90c..cf1f73407 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -43,7 +43,6 @@ int8_t __z_open_inner(_z_session_t *zn, char *locator, z_whatami_t mode) { #else ret = _Z_ERR_TRANSPORT_NOT_AVAILABLE; #endif - if (ret == _Z_RES_OK) { ret = _z_session_init(zn, &local_zid); } diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 52658cf98..33de0a169 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -869,7 +869,7 @@ int8_t _z_scouting_message_decode_na(_z_scouting_message_t *msg, _z_zbuf_t *zbf) } break; default: { - _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(%d)\n", mid); + _Z_DEBUG("WARNING: Trying to decode scouting message with unknown ID(0x%x)\n", mid); ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; is_last = true; } break; diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index 9c7281b2f..062277814 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -123,7 +123,7 @@ int8_t _z_join_decode(_z_t_msg_join_t *msg, _z_zbuf_t *zbf, uint8_t header) { } else { msg->_seq_num_res = _Z_DEFAULT_RESOLUTION_SIZE; msg->_req_id_res = _Z_DEFAULT_RESOLUTION_SIZE; - msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; + msg->_batch_size = _Z_DEFAULT_MULTICAST_BATCH_SIZE; } } if (ret == _Z_RES_OK) { @@ -203,7 +203,7 @@ int8_t _z_init_decode(_z_t_msg_init_t *msg, _z_zbuf_t *zbf, uint8_t header) { } else { msg->_seq_num_res = _Z_DEFAULT_RESOLUTION_SIZE; msg->_req_id_res = _Z_DEFAULT_RESOLUTION_SIZE; - msg->_batch_size = _Z_DEFAULT_BATCH_SIZE; + msg->_batch_size = _Z_DEFAULT_UNICAST_BATCH_SIZE; } if ((ret == _Z_RES_OK) && (_Z_HAS_FLAG(header, _Z_FLAG_T_INIT_A) == true)) { @@ -390,7 +390,7 @@ int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragmen int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t header) { int8_t ret = _Z_RES_OK; *msg = (_z_t_msg_fragment_t){0}; - + _Z_DEBUG("Decoding _Z_TRANSPORT_FRAGMENT\n"); ret |= _z_zint_decode(&msg->_sn, zbf); @@ -441,31 +441,24 @@ int8_t _z_transport_message_encode(_z_wbuf_t *wbf, const _z_transport_message_t case _Z_MID_T_FRAME: { ret |= _z_frame_encode(wbf, msg->_header, &msg->_body._frame); } break; - case _Z_MID_T_FRAGMENT: { ret |= _z_fragment_encode(wbf, msg->_header, &msg->_body._fragment); } break; - case _Z_MID_T_KEEP_ALIVE: { ret |= _z_keep_alive_encode(wbf, msg->_header, &msg->_body._keep_alive); } break; - case _Z_MID_T_JOIN: { ret |= _z_join_encode(wbf, msg->_header, &msg->_body._join); } break; - case _Z_MID_T_INIT: { ret |= _z_init_encode(wbf, msg->_header, &msg->_body._init); } break; - case _Z_MID_T_OPEN: { ret |= _z_open_encode(wbf, msg->_header, &msg->_body._open); } break; - case _Z_MID_T_CLOSE: { ret |= _z_close_encode(wbf, msg->_header, &msg->_body._close); } break; - default: { _Z_DEBUG("WARNING: Trying to encode session message with unknown ID(%d)\n", _Z_MID(msg->_header)); ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; @@ -485,33 +478,27 @@ int8_t _z_transport_message_decode(_z_transport_message_t *msg, _z_zbuf_t *zbf) case _Z_MID_T_FRAME: { ret |= _z_frame_decode(&msg->_body._frame, zbf, msg->_header); } break; - case _Z_MID_T_FRAGMENT: { ret |= _z_fragment_decode(&msg->_body._fragment, zbf, msg->_header); } break; - case _Z_MID_T_KEEP_ALIVE: { ret |= _z_keep_alive_decode(&msg->_body._keep_alive, zbf, msg->_header); } break; - case _Z_MID_T_JOIN: { ret |= _z_join_decode(&msg->_body._join, zbf, msg->_header); } break; - case _Z_MID_T_INIT: { ret |= _z_init_decode(&msg->_body._init, zbf, msg->_header); } break; - case _Z_MID_T_OPEN: { ret |= _z_open_decode(&msg->_body._open, zbf, msg->_header); } break; - case _Z_MID_T_CLOSE: { ret |= _z_close_decode(&msg->_body._close, zbf, msg->_header); } break; - default: { - _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(%d)\n", mid); + _Z_DEBUG("WARNING: Trying to decode session message with unknown ID(0x%x) (header=0x%x)\n", mid, + msg->_header); ret |= _Z_ERR_MESSAGE_TRANSPORT_UNKNOWN; } break; } diff --git a/src/protocol/definitions/transport.c b/src/protocol/definitions/transport.c index 2af2f11e0..f8c684c1a 100644 --- a/src/protocol/definitions/transport.c +++ b/src/protocol/definitions/transport.c @@ -71,7 +71,7 @@ _z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, msg._body._join._lease = lease; msg._body._join._seq_num_res = Z_SN_RESOLUTION; msg._body._join._req_id_res = Z_REQ_RESOLUTION; - msg._body._join._batch_size = Z_BATCH_SIZE; + msg._body._join._batch_size = Z_BATCH_MULTICAST_SIZE; msg._body._join._next_sn = next_sn; msg._body._join._zid = zid; @@ -79,8 +79,8 @@ _z_transport_message_t _z_t_msg_make_join(z_whatami_t whatami, _z_zint_t lease, _Z_SET_FLAG(msg._header, _Z_FLAG_T_JOIN_T); } - if ((Z_BATCH_SIZE != _Z_DEFAULT_BATCH_SIZE) || (Z_SN_RESOLUTION != _Z_DEFAULT_RESOLUTION_SIZE) || - (Z_REQ_RESOLUTION != _Z_DEFAULT_RESOLUTION_SIZE)) { + if ((Z_BATCH_MULTICAST_SIZE != _Z_DEFAULT_MULTICAST_BATCH_SIZE) || + (Z_SN_RESOLUTION != _Z_DEFAULT_RESOLUTION_SIZE) || (Z_REQ_RESOLUTION != _Z_DEFAULT_RESOLUTION_SIZE)) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_JOIN_S); } @@ -101,10 +101,10 @@ _z_transport_message_t _z_t_msg_make_init_syn(z_whatami_t whatami, _z_id_t zid) msg._body._init._zid = zid; msg._body._init._seq_num_res = Z_SN_RESOLUTION; msg._body._init._req_id_res = Z_REQ_RESOLUTION; - msg._body._init._batch_size = Z_BATCH_SIZE; + msg._body._init._batch_size = Z_BATCH_UNICAST_SIZE; _z_bytes_reset(&msg._body._init._cookie); - if ((msg._body._init._batch_size != _Z_DEFAULT_BATCH_SIZE) || + if ((msg._body._init._batch_size != _Z_DEFAULT_UNICAST_BATCH_SIZE) || (msg._body._init._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || (msg._body._init._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_S); @@ -123,10 +123,10 @@ _z_transport_message_t _z_t_msg_make_init_ack(z_whatami_t whatami, _z_id_t zid, msg._body._init._zid = zid; msg._body._init._seq_num_res = Z_SN_RESOLUTION; msg._body._init._req_id_res = Z_REQ_RESOLUTION; - msg._body._init._batch_size = Z_BATCH_SIZE; + msg._body._init._batch_size = Z_BATCH_UNICAST_SIZE; msg._body._init._cookie = cookie; - if ((msg._body._init._batch_size != _Z_DEFAULT_BATCH_SIZE) || + if ((msg._body._init._batch_size != _Z_DEFAULT_UNICAST_BATCH_SIZE) || (msg._body._init._seq_num_res != _Z_DEFAULT_RESOLUTION_SIZE) || (msg._body._init._req_id_res != _Z_DEFAULT_RESOLUTION_SIZE)) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_INIT_S); diff --git a/src/protocol/iobuf.c b/src/protocol/iobuf.c index 3af776758..3d44e7b69 100644 --- a/src/protocol/iobuf.c +++ b/src/protocol/iobuf.c @@ -41,6 +41,10 @@ void __z_iosli_init(_z_iosli_t *ios, size_t capacity) { ios->_capacity = capacity; ios->_is_alloc = true; ios->_buf = (uint8_t *)z_malloc(capacity); + if (ios->_buf == NULL) { + ios->_capacity = 0; + ios->_is_alloc = false; + } } _z_iosli_t _z_iosli_make(size_t capacity) { @@ -257,7 +261,7 @@ _z_wbuf_t _z_wbuf_make(size_t capacity, _Bool is_expandable) { // Preallocate 4 slots, this is usually what we expect // when fragmenting a zenoh data message with attachment wbf._ioss = _z_iosli_vec_make(4); - _z_wbuf_add_iosli(&wbf, __z_wbuf_new_iosli(Z_IOSLICE_SIZE)); + _z_wbuf_add_iosli(&wbf, __z_wbuf_new_iosli(capacity)); } else { wbf._ioss = _z_iosli_vec_make(1); _z_wbuf_add_iosli(&wbf, __z_wbuf_new_iosli(capacity)); diff --git a/src/session/scout.c b/src/session/scout.c index d7e34eb7e..7a45a3ecc 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -52,7 +52,7 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig err = _z_link_send_wbuf(&zl, wbf); if (err == _Z_RES_OK) { // The receiving buffer - _z_zbuf_t zbf = _z_zbuf_make(Z_BATCH_SIZE); + _z_zbuf_t zbf = _z_zbuf_make(Z_BATCH_UNICAST_SIZE); z_time_t start = z_time_now(); while (z_time_elapsed_ms(&start) < period) { @@ -132,7 +132,7 @@ _z_hello_list_t *_z_scout_inner(const z_what_t what, _z_id_t zid, const char *lo _z_hello_list_t *ret = NULL; // Create the buffer to serialize the scout message on - _z_wbuf_t wbf = _z_wbuf_make(Z_BATCH_SIZE, false); + _z_wbuf_t wbf = _z_wbuf_make(Z_BATCH_UNICAST_SIZE, false); // Create and encode the scout message _z_scouting_message_t scout = _z_s_msg_make_scout(what, zid); diff --git a/src/transport/common/rx.c b/src/transport/common/rx.c index 32f09902b..5a7d2e45a 100644 --- a/src/transport/common/rx.c +++ b/src/transport/common/rx.c @@ -24,7 +24,7 @@ int8_t _z_link_recv_t_msg(_z_transport_message_t *t_msg, const _z_link_t *zl) { int8_t ret = _Z_RES_OK; // Create and prepare the buffer - _z_zbuf_t zbf = _z_zbuf_make(Z_BATCH_SIZE); + _z_zbuf_t zbf = _z_zbuf_make(Z_BATCH_UNICAST_SIZE); _z_zbuf_reset(&zbf); if (_Z_LINK_IS_STREAMED(zl->_capabilities) == true) { diff --git a/src/transport/common/tx.c b/src/transport/common/tx.c index d0340bdb5..0ac199e80 100644 --- a/src/transport/common/tx.c +++ b/src/transport/common/tx.c @@ -73,7 +73,7 @@ int8_t _z_link_send_t_msg(const _z_link_t *zl, const _z_transport_message_t *t_m int8_t ret = _Z_RES_OK; // Create and prepare the buffer to serialize the message on - uint16_t mtu = (zl->_mtu < Z_BATCH_SIZE) ? zl->_mtu : Z_BATCH_SIZE; + uint16_t mtu = (zl->_mtu < Z_BATCH_UNICAST_SIZE) ? zl->_mtu : Z_BATCH_UNICAST_SIZE; _z_wbuf_t wbf = _z_wbuf_make(mtu, false); if (_Z_LINK_IS_STREAMED(zl->_capabilities) == true) { for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) { diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 6c90b0b92..28dab94e0 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -241,7 +241,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t // If the new node has less representing capabilities then it is incompatible to communication if ((t_msg->_body._join._seq_num_res != Z_SN_RESOLUTION) || (t_msg->_body._join._req_id_res != Z_REQ_RESOLUTION) || - (t_msg->_body._join._batch_size != Z_BATCH_SIZE)) { + (t_msg->_body._join._batch_size != Z_BATCH_MULTICAST_SIZE)) { ret = _Z_ERR_TRANSPORT_OPEN_SN_RESOLUTION; } @@ -278,7 +278,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t // Check if the representing capabilities are still the same if ((t_msg->_body._join._seq_num_res != Z_SN_RESOLUTION) || (t_msg->_body._join._req_id_res != Z_REQ_RESOLUTION) || - (t_msg->_body._join._batch_size != Z_BATCH_SIZE)) { + (t_msg->_body._join._batch_size != Z_BATCH_MULTICAST_SIZE)) { _z_transport_peer_entry_list_drop_filter(ztm->_peers, _z_transport_peer_entry_eq, entry); // TODO: cleanup here should also be done on mappings/subs/etc... break; diff --git a/src/transport/transport.c b/src/transport/transport.c index 7ee258063..897eb1b18 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -19,6 +19,7 @@ #include #include "zenoh-pico/config.h" +#include "zenoh-pico/link/link.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/transport/link/rx.h" #include "zenoh-pico/transport/link/tx.h" @@ -90,8 +91,8 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic // Initialize the read and write buffers if (ret == _Z_RES_OK) { - uint16_t mtu = (zl->_mtu < Z_BATCH_SIZE) ? zl->_mtu : Z_BATCH_SIZE; - _Bool expandable = true; + uint16_t mtu = (zl->_mtu < Z_BATCH_UNICAST_SIZE) ? zl->_mtu : Z_BATCH_UNICAST_SIZE; + _Bool expandable = _Z_LINK_IS_STREAMED(zl->_capabilities); size_t dbuf_size = 0; #if Z_DYNAMIC_MEMORY_ALLOCATION == 0 @@ -99,7 +100,7 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic dbuf_size = Z_FRAG_MAX_SIZE; #endif zt->_transport._unicast._wbuf = _z_wbuf_make(mtu, expandable); - zt->_transport._unicast._zbuf = _z_zbuf_make(Z_BATCH_SIZE); + zt->_transport._unicast._zbuf = _z_zbuf_make(Z_BATCH_UNICAST_SIZE); // Initialize the defragmentation buffers zt->_transport._unicast._dbuf_reliable = _z_wbuf_make(dbuf_size, expandable); @@ -107,7 +108,7 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic // Clean up the buffers if one of them failed to be allocated if ((_z_wbuf_capacity(&zt->_transport._unicast._wbuf) != mtu) || - (_z_zbuf_capacity(&zt->_transport._unicast._zbuf) != Z_BATCH_SIZE) || + (_z_zbuf_capacity(&zt->_transport._unicast._zbuf) != Z_BATCH_UNICAST_SIZE) || #if Z_DYNAMIC_MEMORY_ALLOCATION == 0 (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_reliable) != dbuf_size) || (_z_wbuf_capacity(&zt->_transport._unicast._dbuf_best_effort) != dbuf_size)) { @@ -195,13 +196,13 @@ int8_t _z_transport_multicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_mu // Initialize the read and write buffers if (ret == _Z_RES_OK) { - uint16_t mtu = (zl->_mtu < Z_BATCH_SIZE) ? zl->_mtu : Z_BATCH_SIZE; - zt->_transport._multicast._wbuf = _z_wbuf_make(mtu, true); - zt->_transport._multicast._zbuf = _z_zbuf_make(Z_BATCH_SIZE); + uint16_t mtu = (zl->_mtu < Z_BATCH_MULTICAST_SIZE) ? zl->_mtu : Z_BATCH_MULTICAST_SIZE; + zt->_transport._multicast._wbuf = _z_wbuf_make(mtu, _Z_LINK_IS_STREAMED(zl->_capabilities)); + zt->_transport._multicast._zbuf = _z_zbuf_make(Z_BATCH_MULTICAST_SIZE); // Clean up the buffers if one of them failed to be allocated if ((_z_wbuf_capacity(&zt->_transport._multicast._wbuf) != mtu) || - (_z_zbuf_capacity(&zt->_transport._multicast._zbuf) != Z_BATCH_SIZE)) { + (_z_zbuf_capacity(&zt->_transport._multicast._zbuf) != Z_BATCH_MULTICAST_SIZE)) { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; #if Z_MULTI_THREAD == 1 diff --git a/zenohpico.pc b/zenohpico.pc index 60d1fd439..9be6ba388 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.10.20230809dev +Version: 0.10.20230811dev Cflags: -I${prefix}/ Libs: -L${prefix}/ -lzenohpico From 5238f256f8ccb4f737e82552fb0241f721b8d691 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 11 Aug 2023 17:47:18 +0200 Subject: [PATCH 154/181] fix aliasing issue in resources --- examples/unix/c11/z_pub.c | 21 +++++++++--- include/zenoh-pico/protocol/iobuf.h | 2 +- include/zenoh-pico/transport/link/tx.h | 2 ++ include/zenoh-pico/utils/logging.h | 45 +++++++++++++++----------- src/protocol/codec.c | 5 +-- src/protocol/codec/declarations.c | 5 +-- src/protocol/iobuf.c | 12 +++---- src/session/resource.c | 6 ++-- src/session/rx.c | 10 ++++-- src/session/subscription.c | 5 +++ src/transport/common/tx.c | 30 +++++++++++++++++ src/transport/unicast/link/rx.c | 1 + src/transport/unicast/link/tx.c | 30 ++++++++++++++++- tests/z_iobuf_test.c | 2 +- 14 files changed, 136 insertions(+), 40 deletions(-) diff --git a/examples/unix/c11/z_pub.c b/examples/unix/c11/z_pub.c index add4332f3..ff2330dd1 100644 --- a/examples/unix/c11/z_pub.c +++ b/examples/unix/c11/z_pub.c @@ -16,17 +16,20 @@ #include #include #include +#include #include #include +#include "zenoh-pico/system/platform.h" + int main(int argc, char **argv) { const char *keyexpr = "demo/example/zenoh-pico-pub"; - const char *value = "Pub from Pico!"; + char *value = "Pub from Pico!"; const char *mode = "client"; char *locator = NULL; int opt; - while ((opt = getopt(argc, argv, "k:v:e:m:")) != -1) { + while ((opt = getopt(argc, argv, "k:v:e:m:l:")) != -1) { switch (opt) { case 'k': keyexpr = optarg; @@ -40,6 +43,15 @@ int main(int argc, char **argv) { case 'm': mode = optarg; break; + case 'l': + opt = atoi(optarg); + value = z_malloc(opt + 1); + memset(value, 'A', opt); + value[opt] = 0; + for (int i = opt - 1; opt > 0; i--, opt /= 10) { + value[i] = '0' + (opt % 10); + } + break; case '?': if (optopt == 'k' || optopt == 'v' || optopt == 'e' || optopt == 'm') { fprintf(stderr, "Option -%c requires an argument.\n", optopt); @@ -78,10 +90,11 @@ int main(int argc, char **argv) { return -1; } - char *buf = (char *)malloc(256); + char *buf = value; // (char *)malloc(256); for (int idx = 0; 1; ++idx) { sleep(1); - snprintf(buf, 256, "[%4d] %s", idx, value); + (void)idx; + // snprintf(buf, 256, "[%4d] %s", idx, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); z_publisher_put_options_t options = z_publisher_put_options_default(); diff --git a/include/zenoh-pico/protocol/iobuf.h b/include/zenoh-pico/protocol/iobuf.h index 8707bc35c..b306b9b73 100644 --- a/include/zenoh-pico/protocol/iobuf.h +++ b/include/zenoh-pico/protocol/iobuf.h @@ -97,7 +97,7 @@ typedef struct { size_t _r_idx; size_t _w_idx; size_t _capacity; - _Bool _is_expandable; + size_t _expansion_step; } _z_wbuf_t; _z_wbuf_t _z_wbuf_make(size_t capacity, _Bool is_expandable); diff --git a/include/zenoh-pico/transport/link/tx.h b/include/zenoh-pico/transport/link/tx.h index 2f119dd0b..51aa3412d 100644 --- a/include/zenoh-pico/transport/link/tx.h +++ b/include/zenoh-pico/transport/link/tx.h @@ -21,6 +21,8 @@ void __unsafe_z_prepare_wbuf(_z_wbuf_t *buf, _Bool is_streamed); void __unsafe_z_finalize_wbuf(_z_wbuf_t *buf, _Bool is_streamed); +/*This function is unsafe because it operates in potentially concurrent + data.*Make sure that the following mutexes are locked before calling this function : *-ztu->mutex_tx */ int8_t __unsafe_z_serialize_zenoh_fragment(_z_wbuf_t *dst, _z_wbuf_t *src, z_reliability_t reliability, size_t sn); /*------------------ Transmission and Reception helpers ------------------*/ diff --git a/include/zenoh-pico/utils/logging.h b/include/zenoh-pico/utils/logging.h index 359f70806..3066fcc91 100644 --- a/include/zenoh-pico/utils/logging.h +++ b/include/zenoh-pico/utils/logging.h @@ -22,38 +22,45 @@ static inline void __z_print_timestamp(void) { printf("[%s ", z_time_now_as_str(ret, sizeof(ret))); } +#define _Z_LOG_PREFIX(prefix) \ + __z_print_timestamp(); \ + printf(#prefix " ::%s] ", __func__); + #if (ZENOH_DEBUG == 3) -#define _Z_DEBUG(x, ...) \ - __z_print_timestamp(); \ - printf("DEBUG ::%s] ", __func__); \ +#define _Z_DEBUG(x, ...) \ + _Z_LOG_PREFIX(DEBUG); \ printf(x, ##__VA_ARGS__); -#define _Z_INFO(x, ...) \ - __z_print_timestamp(); \ - printf("INFO ::%s] ", __func__); \ +#define _Z_DEBUG_CONTINUE(x, ...) printf(x, ##__VA_ARGS__); +#define _Z_INFO(x, ...) \ + _Z_LOG_PREFIX(INFO); \ printf(x, ##__VA_ARGS__); -#define _Z_ERROR(x, ...) \ - __z_print_timestamp(); \ - printf("ERROR ::%s] ", __func__); \ +#define _Z_INFO_CONTINUE(x, ...) printf(x, ##__VA_ARGS__); +#define _Z_ERROR(x, ...) \ + _Z_LOG_PREFIX(ERROR); \ printf(x, ##__VA_ARGS__); +#define _Z_ERROR_CONTINUE(x, ...) printf(x, ##__VA_ARGS__); #elif (ZENOH_DEBUG == 2) #define _Z_DEBUG(x, ...) (void)(0) -#define _Z_INFO(x, ...) \ - __z_print_timestamp(); \ - printf("INFO ::%s] ", __func__); \ +#define _Z_DEBUG_CONTINUE(x, ...) (void)(0); +#define _Z_INFO(x, ...) \ + _Z_LOG_PREFIX(INFO); \ printf(x, ##__VA_ARGS__); -#define _Z_ERROR(x, ...) \ - __z_print_timestamp(); \ - printf("ERROR ::%s] ", __func__); \ +#define _Z_INFO_CONTINUE(x, ...) printf(x, ##__VA_ARGS__); +#define _Z_ERROR(x, ...) \ + _Z_LOG_PREFIX(ERROR); \ printf(x, ##__VA_ARGS__); +#define _Z_ERROR_CONTINUE(x, ...) printf(x, ##__VA_ARGS__); #elif (ZENOH_DEBUG == 1) #define _Z_DEBUG(x, ...) (void)(0) -#define _Z_INFO(x, ...) (void)(0) -#define _Z_ERROR(x, ...) \ - __z_print_timestamp(); \ - printf("ERROR ::%s] ", __func__); \ +#define _Z_DEBUG_CONTINUE(x, ...) (void)(0); +#define _Z_INFO(x, ...) (void)(0); +#define _Z_INFO_CONTINUE(x, ...) (void)(0); +#define _Z_ERROR(x, ...) \ + _Z_LOG_PREFIX(ERROR); \ printf(x, ##__VA_ARGS__); +#define _Z_ERROR_CONTINUE(x, ...) printf(x, ##__VA_ARGS__); #elif (ZENOH_DEBUG == 0) #define _Z_DEBUG(x, ...) (void)(0) diff --git a/src/protocol/codec.c b/src/protocol/codec.c index d1123bb16..18b795cfb 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -295,8 +295,9 @@ int8_t _z_zint64_decode(uint64_t *zint, _z_zbuf_t *zbf) { int8_t _z_bytes_val_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { int8_t ret = _Z_RES_OK; - if ((wbf->_is_expandable == true) && (bs->len > Z_TSID_LENGTH)) { - ret |= _z_wbuf_wrap_bytes(wbf, bs->start, 0, bs->len); + if ((wbf->_expansion_step = true) && (bs->len > Z_TSID_LENGTH)) { + // ret |= _z_wbuf_wrap_bytes(wbf, bs->start, 0, bs->len); + ret |= _z_wbuf_write_bytes(wbf, bs->start, 0, bs->len); } else { ret |= _z_wbuf_write_bytes(wbf, bs->start, 0, bs->len); } diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index 9c2805e28..7e474fb6a 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -34,7 +34,7 @@ #include "zenoh-pico/system/platform.h" int8_t _z_decl_ext_keyexpr_encode(_z_wbuf_t *wbf, _z_keyexpr_t ke, _Bool has_next_ext) { - uint8_t header = _Z_MSG_EXT_ENC_ZBUF | 0x0f | (has_next_ext ? _Z_FLAG_Z_Z : 0); + uint8_t header = _Z_MSG_EXT_ENC_ZBUF | _Z_MSG_EXT_FLAG_M | 0x0f | (has_next_ext ? _Z_FLAG_Z_Z : 0); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); uint32_t kelen = _z_keyexpr_has_suffix(ke) ? strlen(ke._suffix) : 0; header = (_z_keyexpr_is_local(&ke) ? 2 : 0) | (kelen != 0 ? 1 : 0); @@ -189,6 +189,7 @@ int8_t _z_decl_kexpr_decode(_z_decl_kexpr_t *decl, _z_zbuf_t *zbf, uint8_t heade *decl = _z_decl_kexpr_null(); _Z_RETURN_IF_ERR(_z_zint16_decode(&decl->_id, zbf)); _Z_RETURN_IF_ERR(_z_keyexpr_decode(&decl->_keyexpr, zbf, _Z_HAS_FLAG(header, _Z_DECL_KEXPR_FLAG_N))); + _z_keyexpr_set_mapping(&decl->_keyexpr, _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_skip_non_mandatories(zbf, 0x15)); @@ -208,7 +209,7 @@ int8_t _z_undecl_kexpr_decode(_z_undecl_kexpr_t *decl, _z_zbuf_t *zbf, uint8_t h int8_t _z_undecl_decode_extensions(_z_msg_ext_t *extension, void *ctx) { _z_keyexpr_t *ke = (_z_keyexpr_t *)ctx; switch (extension->_header) { - case _Z_MSG_EXT_ENC_ZBUF | 0x0f: { + case _Z_MSG_EXT_ENC_ZBUF | _Z_MSG_EXT_FLAG_M | 0x0f: { _z_zbuf_t _zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); _z_zbuf_t *zbf = &_zbf; uint8_t header; diff --git a/src/protocol/iobuf.c b/src/protocol/iobuf.c index 3d44e7b69..d6c653863 100644 --- a/src/protocol/iobuf.c +++ b/src/protocol/iobuf.c @@ -268,7 +268,7 @@ _z_wbuf_t _z_wbuf_make(size_t capacity, _Bool is_expandable) { } wbf._w_idx = 0; // This __must__ come after adding ioslices to reset w_idx wbf._r_idx = 0; - wbf._is_expandable = is_expandable; + wbf._expansion_step = is_expandable ? capacity : 0; wbf._capacity = capacity; return wbf; @@ -361,8 +361,8 @@ int8_t _z_wbuf_write(_z_wbuf_t *wbf, uint8_t b) { size_t writable = _z_iosli_writable(ios); if (writable >= (size_t)1) { _z_iosli_write(ios, b); - } else if (wbf->_is_expandable == true) { - ios = __z_wbuf_new_iosli(Z_IOSLICE_SIZE); + } else if (wbf->_expansion_step != 0) { + ios = __z_wbuf_new_iosli(wbf->_expansion_step); _z_wbuf_add_iosli(wbf, ios); _z_iosli_write(ios, b); } else { @@ -382,12 +382,12 @@ int8_t _z_wbuf_write_bytes(_z_wbuf_t *wbf, const uint8_t *bs, size_t offset, siz size_t writable = _z_iosli_writable(ios); if (writable >= llength) { _z_iosli_write_bytes(ios, bs, loffset, llength); - } else if (wbf->_is_expandable == true) { + } else if (wbf->_expansion_step != 0) { _z_iosli_write_bytes(ios, bs, loffset, writable); llength = llength - writable; loffset = loffset + writable; while (llength > (size_t)0) { - ios = __z_wbuf_new_iosli(Z_IOSLICE_SIZE); + ios = __z_wbuf_new_iosli(wbf->_expansion_step); _z_wbuf_add_iosli(wbf, ios); writable = _z_iosli_writable(ios); @@ -530,7 +530,7 @@ void _z_wbuf_copy(_z_wbuf_t *dst, const _z_wbuf_t *src) { dst->_capacity = src->_capacity; dst->_r_idx = src->_r_idx; dst->_w_idx = src->_w_idx; - dst->_is_expandable = src->_is_expandable; + dst->_expansion_step = src->_expansion_step; _z_iosli_vec_copy(&dst->_ioss, &src->_ioss); } diff --git a/src/session/resource.c b/src/session/resource.c index e3fd4cabe..a17e818bb 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -50,6 +50,8 @@ _z_resource_t *__z_get_resource_by_id(_z_resource_list_t *rl, uint16_t mapping, _z_resource_list_t *xs = rl; while (xs != NULL) { _z_resource_t *r = _z_resource_list_head(xs); + _Z_DEBUG("Checking ressource %d on mapping 0x%x: %d %s\n", r->_id, _z_keyexpr_mapping_id(&r->_key), r->_key._id, + r->_key._suffix); if (r->_id == id && _z_keyexpr_mapping_id(&r->_key) == mapping) { ret = r; break; @@ -215,9 +217,9 @@ _z_keyexpr_t _z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t * int8_t _z_register_resource(_z_session_t *zn, _z_resource_t *res) { int8_t ret = _Z_RES_OK; - _Z_DEBUG(">>> Allocating res decl for (%ju,%u:%s)\n", (uintmax_t)res->_id, (unsigned int)res->_key._id, - res->_key._suffix); uint16_t mapping = _z_keyexpr_mapping_id(&res->_key); + _Z_DEBUG(">>> Allocating res decl for (%ju,%u:%s) on mapping 0x%x\n", (uintmax_t)res->_id, + (unsigned int)res->_key._id, res->_key._suffix, mapping); #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 diff --git a/src/session/rx.c b/src/session/rx.c index 2573569a9..3497b85f5 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -35,14 +35,16 @@ /*------------------ Handle message ------------------*/ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16_t local_peer_id) { int8_t ret = _Z_RES_OK; + switch (msg->_tag) { case _Z_N_DECLARE: { + _Z_DEBUG("Handling _Z_N_DECLARE\n"); _z_n_msg_declare_t decl = msg->_body._declare; switch (decl._decl._tag) { case _Z_DECL_KEXPR: { _z_resource_t *res = (_z_resource_t *)z_malloc(sizeof(_z_resource_t)); res->_id = decl._decl._body._decl_kexpr._id; - res->_key = decl._decl._body._decl_kexpr._keyexpr; + res->_key = _z_keyexpr_duplicate(decl._decl._body._decl_kexpr._keyexpr); ret = _z_register_resource(zn, res); } break; case _Z_UNDECL_KEXPR: { @@ -78,6 +80,7 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 } } break; case _Z_N_PUSH: { + _Z_DEBUG("Handling _Z_N_PUSH\n"); _z_n_msg_push_t push = msg->_body._push; _z_bytes_t payload = push._body._is_put ? push._body._body._put._payload : _z_bytes_empty(); _z_encoding_t encoding = push._body._is_put ? push._body._body._put._encoding : z_encoding_default(); @@ -85,6 +88,7 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 ret = _z_trigger_subscriptions(zn, push._key, payload, encoding, kind, push._timestamp); } break; case _Z_N_REQUEST: { + _Z_DEBUG("Handling _Z_N_REQUEST\n"); _z_n_msg_request_t req = msg->_body._request; switch (req._tag) { case _Z_REQUEST_QUERY: { @@ -119,6 +123,7 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 } } break; case _Z_N_RESPONSE: { + _Z_DEBUG("Handling _Z_N_RESPONSE\n"); _z_n_msg_response_t response = msg->_body._response; switch (response._tag) { case _Z_RESPONSE_BODY_REPLY: { @@ -130,7 +135,7 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 // @TODO: expose errors to the user _z_msg_err_t error = response._body._err; _z_bytes_t payload = error._ext_value.payload; - _Z_ERROR("Received Err for query %d: code=%d, message=%.*s\n", response._request_id, error._code, + _Z_ERROR("Received Err for query %zu: code=%d, message=%.*s\n", response._request_id, error._code, payload.len, payload.start); } break; case _Z_RESPONSE_BODY_ACK: { @@ -149,6 +154,7 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 } } break; case _Z_N_RESPONSE_FINAL: { + _Z_DEBUG("Handling _Z_N_RESPONSE_FINAL\n"); _z_zint_t id = msg->_body._response_final._request_id; _z_trigger_query_reply_final(zn, id); } break; diff --git a/src/session/subscription.c b/src/session/subscription.c index e00d56b1c..504d1da7a 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -17,8 +17,10 @@ #include #include "zenoh-pico/config.h" +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/session/resource.h" +#include "zenoh-pico/session/session.h" #include "zenoh-pico/utils/logging.h" _Bool _z_subscription_eq(const _z_subscription_t *other, const _z_subscription_t *this) { @@ -156,7 +158,9 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 + _Z_DEBUG("Resolving %d - %s on mapping 0x%x\n", keyexpr._id, keyexpr._suffix, _z_keyexpr_mapping_id(&keyexpr)); _z_keyexpr_t key = __unsafe_z_get_expanded_key_from_key(zn, &keyexpr); + _Z_DEBUG("Triggering subs for %d - %s\n", key._id, key._suffix); if (key._suffix != NULL) { _z_subscription_sptr_list_t *subs = __unsafe_z_get_subscriptions_by_key(zn, _Z_RESOURCE_IS_LOCAL, key); @@ -172,6 +176,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co s.kind = kind; s.timestamp = timestamp; _z_subscription_sptr_list_t *xs = subs; + _Z_DEBUG("Triggering %ld subs\n", _z_subscription_sptr_list_len(xs)); while (xs != NULL) { _z_subscription_sptr_t *sub = _z_subscription_sptr_list_head(xs); sub->ptr->_callback(&s, sub->ptr->_arg); diff --git a/src/transport/common/tx.c b/src/transport/common/tx.c index 0ac199e80..ea50773df 100644 --- a/src/transport/common/tx.c +++ b/src/transport/common/tx.c @@ -14,7 +14,9 @@ #include "zenoh-pico/transport/link/tx.h" +#include "zenoh-pico/api/constants.h" #include "zenoh-pico/protocol/codec/transport.h" +#include "zenoh-pico/protocol/definitions/transport.h" #include "zenoh-pico/utils/logging.h" /*------------------ Transmission helper ------------------*/ @@ -101,3 +103,31 @@ int8_t _z_link_send_t_msg(const _z_link_t *zl, const _z_transport_message_t *t_m return ret; } #endif // Z_UNICAST_TRANSPORT == 1 || Z_MULTICAST_TRANSPORT == 1 +int8_t __unsafe_z_serialize_zenoh_fragment(_z_wbuf_t *dst, _z_wbuf_t *src, z_reliability_t reliability, size_t sn) { + int8_t ret = _Z_RES_OK; + + // Assume first that this is not the final fragment + _Bool is_final = false; + do { + size_t w_pos = _z_wbuf_get_wpos(dst); // Mark the buffer for the writing operation + + _z_transport_message_t f_hdr = _z_t_msg_make_frame_header(sn, reliability == Z_RELIABILITY_RELIABLE); + ret = _z_transport_message_encode(dst, &f_hdr); // Encode the frame header + if (ret == _Z_RES_OK) { + size_t space_left = _z_wbuf_space_left(dst); + size_t bytes_left = _z_wbuf_len(src); + + if ((is_final == false) && (bytes_left <= space_left)) { // Check if it is really the final fragment + _z_wbuf_set_wpos(dst, w_pos); // Revert the buffer + is_final = true; // It is really the finally fragment, reserialize the header + continue; + } + + size_t to_copy = (bytes_left <= space_left) ? bytes_left : space_left; // Compute bytes to write + ret = _z_wbuf_siphon(dst, src, to_copy); // Write the fragment + } + break; + } while (1); + + return ret; +} diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index 8a58147a0..e7abc7a42 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -116,6 +116,7 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans // Handle all the zenoh message, one by one size_t len = _z_vec_len(&t_msg->_body._frame._messages); + _Z_INFO("Z_FRAME contained %ld messages\n", len); for (size_t i = 0; i < len; i++) { _z_handle_zenoh_message(ztu->_session, (_z_zenoh_message_t *)_z_vec_get(&t_msg->_body._frame._messages, i), diff --git a/src/transport/unicast/link/tx.c b/src/transport/unicast/link/tx.c index 2482ec2b0..4d697328b 100644 --- a/src/transport/unicast/link/tx.c +++ b/src/transport/unicast/link/tx.c @@ -120,7 +120,35 @@ int8_t _z_unicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg ztu->_transmitted = true; // Mark the session that we have transmitted data } } else { - // TODO[protocol]: Fragmentation goes here + // The message does not fit in the current batch, let's fragment it + // Create an expandable wbuf for fragmentation + _z_wbuf_t fbf = _z_wbuf_make(ztu->_wbuf._expansion_step - 8, true); + + ret = _z_network_message_encode(&fbf, n_msg); // Encode the message on the expandable wbuf + if (ret == _Z_RES_OK) { + _Bool is_first = true; // Fragment and send the message + while (_z_wbuf_len(&fbf) > 0) { + if (is_first == false) { // Get the fragment sequence number + sn = __unsafe_z_unicast_get_sn(ztu, reliability); + } + is_first = false; + + // Clear the buffer for serialization + __unsafe_z_prepare_wbuf(&ztu->_wbuf, _Z_LINK_IS_STREAMED(ztu->_link._capabilities)); + + // Serialize one fragment + ret = __unsafe_z_serialize_zenoh_fragment(&ztu->_wbuf, &fbf, reliability, sn); + if (ret == _Z_RES_OK) { + // Write the message length in the reserved space if needed + __unsafe_z_finalize_wbuf(&ztu->_wbuf, _Z_LINK_IS_STREAMED(ztu->_link._capabilities)); + + ret = _z_link_send_wbuf(&ztu->_link, &ztu->_wbuf); // Send the wbuf on the socket + if (ret == _Z_RES_OK) { + ztu->_transmitted = true; // Mark the session that we have transmitted data + } + } + } + } } } diff --git a/tests/z_iobuf_test.c b/tests/z_iobuf_test.c index f7512e5db..a972aa8e5 100644 --- a/tests/z_iobuf_test.c +++ b/tests/z_iobuf_test.c @@ -27,7 +27,7 @@ void print_zbuf_overview(_z_zbuf_t *zbf) { printf(" ZBuf => Capacity: %zu\n", zbf->_ios._capacity); } void print_wbuf_overview(_z_wbuf_t *wbf) { - printf(" WBuf => Expandable: %u, Capacity: %zu\n", wbf->_is_expandable, wbf->_capacity); + printf(" WBuf => Expandable: %zu, Capacity: %zu\n", wbf->_expansion_step, wbf->_capacity); } void print_iosli(_z_iosli_t *ios) { From 0be6ad05c8540efeba25c290b3fe9c700217bdba Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 18 Aug 2023 17:37:12 +0200 Subject: [PATCH 155/181] implement fragmentation --- include/zenoh-pico/collections/bytes.h | 1 + include/zenoh-pico/protocol/definitions/transport.h | 1 + include/zenoh-pico/protocol/iobuf.h | 1 + src/link/link.c | 3 +-- src/protocol/codec.c | 2 +- src/protocol/codec/transport.c | 6 ++++-- src/protocol/definitions/transport.c | 6 +++++- src/protocol/iobuf.c | 1 + src/transport/common/tx.c | 4 +++- src/transport/transport.c | 4 ++-- src/transport/unicast/link/rx.c | 5 ++++- src/transport/unicast/link/tx.c | 4 +++- zenohpico.pc | 2 +- 13 files changed, 28 insertions(+), 12 deletions(-) diff --git a/include/zenoh-pico/collections/bytes.h b/include/zenoh-pico/collections/bytes.h index acc8503aa..0002bc257 100644 --- a/include/zenoh-pico/collections/bytes.h +++ b/include/zenoh-pico/collections/bytes.h @@ -34,6 +34,7 @@ typedef struct { } _z_bytes_t; _z_bytes_t _z_bytes_empty(void); +inline static _Bool _z_bytes_check(_z_bytes_t value) { return value.start != NULL; } int8_t _z_bytes_init(_z_bytes_t *bs, size_t capacity); _z_bytes_t _z_bytes_make(size_t capacity); _z_bytes_t _z_bytes_wrap(const uint8_t *bs, size_t len); diff --git a/include/zenoh-pico/protocol/definitions/transport.h b/include/zenoh-pico/protocol/definitions/transport.h index c3f1b3270..32b8dd82a 100644 --- a/include/zenoh-pico/protocol/definitions/transport.h +++ b/include/zenoh-pico/protocol/definitions/transport.h @@ -505,6 +505,7 @@ _z_transport_message_t _z_t_msg_make_close(uint8_t reason, _Bool link_only); _z_transport_message_t _z_t_msg_make_keep_alive(void); _z_transport_message_t _z_t_msg_make_frame(_z_zint_t sn, _z_network_message_vec_t messages, _Bool is_reliable); _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliable); +_z_transport_message_t _z_t_msg_make_fragment_header(_z_zint_t sn, _Bool is_reliable, _Bool is_last); _z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_bytes_t messages, _Bool is_reliable, _Bool is_last); /*------------------ Copy ------------------*/ diff --git a/include/zenoh-pico/protocol/iobuf.h b/include/zenoh-pico/protocol/iobuf.h index b306b9b73..90af275fe 100644 --- a/include/zenoh-pico/protocol/iobuf.h +++ b/include/zenoh-pico/protocol/iobuf.h @@ -70,6 +70,7 @@ _z_zbuf_t _z_zbuf_view(_z_zbuf_t *zbf, size_t length); _z_zbuf_t _z_zbytes_as_zbuf(_z_bytes_t slice); size_t _z_zbuf_capacity(const _z_zbuf_t *zbf); +uint8_t const *_z_zbuf_start(const _z_zbuf_t *zbf); size_t _z_zbuf_len(const _z_zbuf_t *zbf); _Bool _z_zbuf_can_read(const _z_zbuf_t *zbf); size_t _z_zbuf_space_left(const _z_zbuf_t *zbf); diff --git a/src/link/link.c b/src/link/link.c index a70f2b9f5..3705ca1a4 100644 --- a/src/link/link.c +++ b/src/link/link.c @@ -148,8 +148,7 @@ size_t _z_link_recv_exact_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, size_t len int8_t _z_link_send_wbuf(const _z_link_t *link, const _z_wbuf_t *wbf) { int8_t ret = _Z_RES_OK; - - for (size_t i = 0; (i < _z_wbuf_len_iosli(wbf)) || (ret == -1); i++) { + for (size_t i = 0; (i < _z_wbuf_len_iosli(wbf)); i++) { _z_bytes_t bs = _z_iosli_to_bytes(_z_wbuf_get_iosli(wbf, i)); size_t n = bs.len; do { diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 18b795cfb..e05d7ba28 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -295,7 +295,7 @@ int8_t _z_zint64_decode(uint64_t *zint, _z_zbuf_t *zbf) { int8_t _z_bytes_val_encode(_z_wbuf_t *wbf, const _z_bytes_t *bs) { int8_t ret = _Z_RES_OK; - if ((wbf->_expansion_step = true) && (bs->len > Z_TSID_LENGTH)) { + if ((wbf->_expansion_step != 0) && (bs->len > Z_TSID_LENGTH)) { // ret |= _z_wbuf_wrap_bytes(wbf, bs->start, 0, bs->len); ret |= _z_wbuf_write_bytes(wbf, bs->start, 0, bs->len); } else { diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index 062277814..408599c61 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -17,6 +17,7 @@ #include #include +#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/protocol/codec/ext.h" #include "zenoh-pico/protocol/codec/network.h" @@ -380,7 +381,7 @@ int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragmen if (_Z_HAS_FLAG(header, _Z_FLAG_T_Z)) { ret = _Z_ERR_MESSAGE_SERIALIZATION_FAILED; } - if (ret == _Z_RES_OK) { + if (ret == _Z_RES_OK && _z_bytes_check(msg->_payload)) { _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_payload)) } @@ -398,7 +399,8 @@ int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t head ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x05); } - ret |= _z_bytes_decode(&msg->_payload, zbf); + __auto_type bytes = _z_bytes_wrap((uint8_t *)_z_zbuf_start(zbf), _z_zbuf_len(zbf)); + _z_bytes_copy(&msg->_payload, &bytes); return ret; } diff --git a/src/protocol/definitions/transport.c b/src/protocol/definitions/transport.c index f8c684c1a..3be09369f 100644 --- a/src/protocol/definitions/transport.c +++ b/src/protocol/definitions/transport.c @@ -1,5 +1,6 @@ #include "zenoh-pico/protocol/definitions/transport.h" +#include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/utils/logging.h" void _z_s_msg_scout_clear(_z_s_msg_scout_t *msg) {} @@ -218,10 +219,13 @@ _z_transport_message_t _z_t_msg_make_frame_header(_z_zint_t sn, _Bool is_reliabl } /*------------------ Fragment Message ------------------*/ +_z_transport_message_t _z_t_msg_make_fragment_header(_z_zint_t sn, _Bool is_reliable, _Bool is_last) { + return _z_t_msg_make_fragment(sn, _z_bytes_empty(), is_reliable, is_last); +} _z_transport_message_t _z_t_msg_make_fragment(_z_zint_t sn, _z_bytes_t payload, _Bool is_reliable, _Bool is_last) { _z_transport_message_t msg; msg._header = _Z_MID_T_FRAGMENT; - if (is_last == true) { + if (is_last == false) { _Z_SET_FLAG(msg._header, _Z_FLAG_T_FRAGMENT_M); } if (is_reliable == true) { diff --git a/src/protocol/iobuf.c b/src/protocol/iobuf.c index d6c653863..1e1025941 100644 --- a/src/protocol/iobuf.c +++ b/src/protocol/iobuf.c @@ -183,6 +183,7 @@ 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); } 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/transport/common/tx.c b/src/transport/common/tx.c index ea50773df..a1144be24 100644 --- a/src/transport/common/tx.c +++ b/src/transport/common/tx.c @@ -15,6 +15,7 @@ #include "zenoh-pico/transport/link/tx.h" #include "zenoh-pico/api/constants.h" +#include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/protocol/definitions/transport.h" #include "zenoh-pico/utils/logging.h" @@ -111,7 +112,8 @@ int8_t __unsafe_z_serialize_zenoh_fragment(_z_wbuf_t *dst, _z_wbuf_t *src, z_rel do { size_t w_pos = _z_wbuf_get_wpos(dst); // Mark the buffer for the writing operation - _z_transport_message_t f_hdr = _z_t_msg_make_frame_header(sn, reliability == Z_RELIABILITY_RELIABLE); + _z_transport_message_t f_hdr = + _z_t_msg_make_fragment_header(sn, reliability == Z_RELIABILITY_RELIABLE, is_final); ret = _z_transport_message_encode(dst, &f_hdr); // Encode the frame header if (ret == _Z_RES_OK) { size_t space_left = _z_wbuf_space_left(dst); diff --git a/src/transport/transport.c b/src/transport/transport.c index 897eb1b18..af71c2016 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -99,7 +99,7 @@ int8_t _z_transport_unicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_unic expandable = false; dbuf_size = Z_FRAG_MAX_SIZE; #endif - zt->_transport._unicast._wbuf = _z_wbuf_make(mtu, expandable); + zt->_transport._unicast._wbuf = _z_wbuf_make(mtu, false); zt->_transport._unicast._zbuf = _z_zbuf_make(Z_BATCH_UNICAST_SIZE); // Initialize the defragmentation buffers @@ -197,7 +197,7 @@ int8_t _z_transport_multicast(_z_transport_t *zt, _z_link_t *zl, _z_transport_mu // Initialize the read and write buffers if (ret == _Z_RES_OK) { uint16_t mtu = (zl->_mtu < Z_BATCH_MULTICAST_SIZE) ? zl->_mtu : Z_BATCH_MULTICAST_SIZE; - zt->_transport._multicast._wbuf = _z_wbuf_make(mtu, _Z_LINK_IS_STREAMED(zl->_capabilities)); + zt->_transport._multicast._wbuf = _z_wbuf_make(mtu, false); zt->_transport._multicast._zbuf = _z_zbuf_make(Z_BATCH_MULTICAST_SIZE); // Clean up the buffers if one of them failed to be allocated diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index e7abc7a42..c8f48c364 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -20,6 +20,7 @@ #include "zenoh-pico/protocol/codec/network.h" #include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/session/utils.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" @@ -127,7 +128,7 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans } case _Z_MID_T_FRAGMENT: { - _Z_INFO("Received Z_FRAGMENT message\n"); + _Z_INFO("Received Z_FRAGMENT message, len: %ld\n", t_msg->_body._fragment._payload.len); _z_wbuf_t *dbuf = _Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_FRAGMENT_R) ? &ztu->_dbuf_reliable @@ -158,6 +159,8 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans _z_handle_zenoh_message(ztu->_session, &zm, _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); _z_msg_clear(&zm); // Clear must be explicitly called for fragmented zenoh messages. Non-fragmented // zenoh messages are released when their transport message is released. + } else { + _Z_DEBUG("Failed to decode defragmented message\n"); } // Free the decoding buffer diff --git a/src/transport/unicast/link/tx.c b/src/transport/unicast/link/tx.c index 4d697328b..3375d08e9 100644 --- a/src/transport/unicast/link/tx.c +++ b/src/transport/unicast/link/tx.c @@ -14,6 +14,8 @@ #include "zenoh-pico/transport/link/tx.h" +#include + #include "zenoh-pico/config.h" #include "zenoh-pico/protocol/codec/network.h" #include "zenoh-pico/protocol/codec/transport.h" @@ -122,7 +124,7 @@ int8_t _z_unicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg } else { // The message does not fit in the current batch, let's fragment it // Create an expandable wbuf for fragmentation - _z_wbuf_t fbf = _z_wbuf_make(ztu->_wbuf._expansion_step - 8, true); + _z_wbuf_t fbf = _z_wbuf_make(ztu->_wbuf._capacity - 12, true); ret = _z_network_message_encode(&fbf, n_msg); // Encode the message on the expandable wbuf if (ret == _Z_RES_OK) { diff --git a/zenohpico.pc b/zenohpico.pc index 9be6ba388..59d1b7971 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.10.20230811dev +Version: 0.10.20230818dev Cflags: -I${prefix}/ Libs: -L${prefix}/ -lzenohpico From 2b87df4c77aea3323930a2dcdfbc315d7ca85677 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 18 Aug 2023 18:50:55 +0200 Subject: [PATCH 156/181] implement fragmentation for multicast --- src/protocol/codec/transport.c | 1 + src/transport/multicast/link/rx.c | 3 ++- src/transport/multicast/link/task/read.c | 1 + src/transport/multicast/link/tx.c | 30 +++++++++++++++++++++++- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index 408599c61..4c6fa2f54 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -401,6 +401,7 @@ int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t head __auto_type bytes = _z_bytes_wrap((uint8_t *)_z_zbuf_start(zbf), _z_zbuf_len(zbf)); _z_bytes_copy(&msg->_payload, &bytes); + zbf->_ios._r_pos = zbf->_ios._w_pos; return ret; } diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 28dab94e0..9211b2e2e 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -21,6 +21,7 @@ #include "zenoh-pico/protocol/codec/transport.h" #include "zenoh-pico/protocol/definitions/network.h" #include "zenoh-pico/protocol/definitions/transport.h" +#include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/session/utils.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" @@ -90,7 +91,7 @@ int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_me } while (false); // The 1-iteration loop to use continue to break the entire loop on error if (ret == _Z_RES_OK) { - _Z_DEBUG(">> \t transport_message_decode\n"); + _Z_DEBUG(">> \t transport_message_decode: %ld\n", _z_zbuf_len(&ztm->_zbuf)); ret = _z_transport_message_decode(t_msg, &ztm->_zbuf); } diff --git a/src/transport/multicast/link/task/read.c b/src/transport/multicast/link/task/read.c index 45753857a..2c4278426 100644 --- a/src/transport/multicast/link/task/read.c +++ b/src/transport/multicast/link/task/read.c @@ -18,6 +18,7 @@ #include "zenoh-pico/config.h" #include "zenoh-pico/protocol/codec/transport.h" +#include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/transport/link/rx.h" #include "zenoh-pico/utils/logging.h" diff --git a/src/transport/multicast/link/tx.c b/src/transport/multicast/link/tx.c index 234929a20..67e4e35bd 100644 --- a/src/transport/multicast/link/tx.c +++ b/src/transport/multicast/link/tx.c @@ -113,7 +113,35 @@ int8_t _z_multicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_m ztm->_transmitted = true; // Mark the session that we have transmitted data } } else { - // TODO[protocol]: Fragmentation goes here + // The message does not fit in the current batch, let's fragment it + // Create an expandable wbuf for fragmentation + _z_wbuf_t fbf = _z_wbuf_make(ztm->_wbuf._capacity - 12, true); + + ret = _z_network_message_encode(&fbf, n_msg); // Encode the message on the expandable wbuf + if (ret == _Z_RES_OK) { + _Bool is_first = true; // Fragment and send the message + while (_z_wbuf_len(&fbf) > 0) { + if (is_first == false) { // Get the fragment sequence number + sn = __unsafe_z_multicast_get_sn(ztm, reliability); + } + is_first = false; + + // Clear the buffer for serialization + __unsafe_z_prepare_wbuf(&ztm->_wbuf, _Z_LINK_IS_STREAMED(ztm->_link._capabilities)); + + // Serialize one fragment + ret = __unsafe_z_serialize_zenoh_fragment(&ztm->_wbuf, &fbf, reliability, sn); + if (ret == _Z_RES_OK) { + // Write the message length in the reserved space if needed + __unsafe_z_finalize_wbuf(&ztm->_wbuf, _Z_LINK_IS_STREAMED(ztm->_link._capabilities)); + + ret = _z_link_send_wbuf(&ztm->_link, &ztm->_wbuf); // Send the wbuf on the socket + if (ret == _Z_RES_OK) { + ztm->_transmitted = true; // Mark the session that we have transmitted data + } + } + } + } } } From a044c9622e72fbfba82723945ca9e3db417c3c89 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 21 Aug 2023 12:17:03 +0200 Subject: [PATCH 157/181] fix memory leaks --- examples/unix/c11/z_pub.c | 20 +++++++++++++------- examples/unix/c11/z_scout.c | 1 + examples/unix/c99/z_scout.c | 1 + src/net/primitives.c | 3 +-- src/protocol/codec/transport.c | 2 +- src/session/resource.c | 2 +- zenohpico.pc | 2 +- 7 files changed, 19 insertions(+), 12 deletions(-) diff --git a/examples/unix/c11/z_pub.c b/examples/unix/c11/z_pub.c index ff2330dd1..3516dfb43 100644 --- a/examples/unix/c11/z_pub.c +++ b/examples/unix/c11/z_pub.c @@ -24,12 +24,14 @@ int main(int argc, char **argv) { const char *keyexpr = "demo/example/zenoh-pico-pub"; - char *value = "Pub from Pico!"; + char *const default_value = "Pub from Pico!"; + char *value = default_value; const char *mode = "client"; char *locator = NULL; + int n = 10; int opt; - while ((opt = getopt(argc, argv, "k:v:e:m:l:")) != -1) { + while ((opt = getopt(argc, argv, "k:v:e:m:l:n:")) != -1) { switch (opt) { case 'k': keyexpr = optarg; @@ -52,6 +54,9 @@ int main(int argc, char **argv) { value[i] = '0' + (opt % 10); } break; + case 'n': + n = atoi(optarg); + break; case '?': if (optopt == 'k' || optopt == 'v' || optopt == 'e' || optopt == 'm') { fprintf(stderr, "Option -%c requires an argument.\n", optopt); @@ -90,16 +95,15 @@ int main(int argc, char **argv) { return -1; } - char *buf = value; // (char *)malloc(256); - for (int idx = 0; 1; ++idx) { + for (int idx = 0; idx < n; ++idx) { sleep(1); (void)idx; // snprintf(buf, 256, "[%4d] %s", idx, value); - printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); + printf("Putting Data ('%s': '%s')...\n", keyexpr, value); z_publisher_put_options_t options = z_publisher_put_options_default(); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); - z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); + z_publisher_put(z_loan(pub), (const uint8_t *)value, strlen(value), &options); } z_undeclare_publisher(z_move(pub)); @@ -110,6 +114,8 @@ int main(int argc, char **argv) { z_close(z_move(s)); - free(buf); + if (value != default_value) { + free(value); + } return 0; } diff --git a/examples/unix/c11/z_scout.c b/examples/unix/c11/z_scout.c index 185211c00..751609db1 100644 --- a/examples/unix/c11/z_scout.c +++ b/examples/unix/c11/z_scout.c @@ -69,6 +69,7 @@ void callback(z_owned_hello_t *hello, void *context) { fprinthello(stdout, z_loan(*hello)); fprintf(stdout, "\n"); (*(int *)context)++; + z_drop(hello); } void drop(void *context) { diff --git a/examples/unix/c99/z_scout.c b/examples/unix/c99/z_scout.c index 3dafdfad4..5b40a408e 100644 --- a/examples/unix/c99/z_scout.c +++ b/examples/unix/c99/z_scout.c @@ -67,6 +67,7 @@ void callback(z_owned_hello_t *hello, void *context) { fprinthello(stdout, z_hello_loan(hello)); fprintf(stdout, "\n"); (*(int *)context)++; + z_drop(hello); } void drop(void *context) { diff --git a/src/net/primitives.c b/src/net/primitives.c index 5444426a2..9c4daadef 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -385,8 +385,7 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, ret = _z_register_pending_query(zn, pq); // Add the pending query to the current session if (ret == _Z_RES_OK) { _z_bytes_t params = _z_bytes_wrap((uint8_t *)pq->_parameters, strlen(pq->_parameters)); - _z_keyexpr_t k = _z_keyexpr_duplicate(keyexpr); - _z_zenoh_message_t z_msg = _z_msg_make_query(&k, ¶ms, pq->_id, pq->_consolidation, &value); + _z_zenoh_message_t z_msg = _z_msg_make_query(&keyexpr, ¶ms, pq->_id, pq->_consolidation, &value); if (_z_send_n_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { _z_unregister_pending_query(zn, pq); diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index 4c6fa2f54..3c41878a6 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -399,7 +399,7 @@ int8_t _z_fragment_decode(_z_t_msg_fragment_t *msg, _z_zbuf_t *zbf, uint8_t head ret |= _z_msg_ext_skip_non_mandatories(zbf, 0x05); } - __auto_type bytes = _z_bytes_wrap((uint8_t *)_z_zbuf_start(zbf), _z_zbuf_len(zbf)); + _z_bytes_t bytes = _z_bytes_wrap((uint8_t *)_z_zbuf_start(zbf), _z_zbuf_len(zbf)); _z_bytes_copy(&msg->_payload, &bytes); zbf->_ios._r_pos = zbf->_ios._w_pos; diff --git a/src/session/resource.c b/src/session/resource.c index a17e818bb..cf5dceb34 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -83,7 +83,7 @@ _z_resource_t *__z_get_resource_by_key(_z_resource_list_t *rl, const _z_keyexpr_ } _z_keyexpr_t __z_get_expanded_key_from_key(_z_resource_list_t *xs, const _z_keyexpr_t *keyexpr) { - _z_keyexpr_t ret = {._id = Z_RESOURCE_ID_NONE, ._suffix = NULL}; + _z_keyexpr_t ret = {._id = Z_RESOURCE_ID_NONE, ._suffix = NULL, ._mapping = _z_keyexpr_mapping(0, true)}; // Need to build the complete resource name, by recursively look at RIDs // Resource names are looked up from right to left diff --git a/zenohpico.pc b/zenohpico.pc index 59d1b7971..d97573d33 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.10.20230818dev +Version: 0.10.20230821dev Cflags: -I${prefix}/ Libs: -L${prefix}/ -lzenohpico From dc5bec927dfff080ab3e4ad0f4cacf8d14e0dbac Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 21 Aug 2023 18:29:46 +0200 Subject: [PATCH 158/181] fix wbuf allocating even if a slice is available after the full one --- .github/workflows/integration.yaml | 46 +++++++++++++++--------------- src/protocol/codec/transport.c | 2 +- src/protocol/iobuf.c | 29 ++++++++++--------- tests/z_iobuf_test.c | 9 ++++-- 4 files changed, 45 insertions(+), 41 deletions(-) diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml index 5015e4757..888b9d72e 100644 --- a/.github/workflows/integration.yaml +++ b/.github/workflows/integration.yaml @@ -32,28 +32,28 @@ jobs: os: [ ubuntu-latest, macOS-latest ] steps: - - uses: actions/checkout@v2 - - - name: Install latest stable - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - override: true - components: rustfmt, clippy + - uses: actions/checkout@v2 - - name: Compile debug - run: make all - env: - BUILD_TYPE: Debug - BUILD_TESTING: OFF - BUILD_MULTICAST: OFF - BUILD_INTEGRATION: ON + - name: Install latest stable + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + components: rustfmt, clippy - - name: Test debug - run: make test - env: - BUILD_TYPE: Debug # Workaround for Windows as it seems the previous step is being ignored - 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: master + - name: Compile debug + run: make all + env: + BUILD_TYPE: Debug + BUILD_TESTING: OFF + BUILD_MULTICAST: OFF + BUILD_INTEGRATION: ON + + - name: Test debug + run: make test + env: + BUILD_TYPE: Debug # Workaround for Windows as it seems the previous step is being ignored + 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: new-protocol diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index 3c41878a6..79fe71b4c 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -382,7 +382,7 @@ int8_t _z_fragment_encode(_z_wbuf_t *wbf, uint8_t header, const _z_t_msg_fragmen ret = _Z_ERR_MESSAGE_SERIALIZATION_FAILED; } if (ret == _Z_RES_OK && _z_bytes_check(msg->_payload)) { - _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_payload)) + _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, msg->_payload.start, 0, msg->_payload.len)); } return ret; diff --git a/src/protocol/iobuf.c b/src/protocol/iobuf.c index 1e1025941..c5700886f 100644 --- a/src/protocol/iobuf.c +++ b/src/protocol/iobuf.c @@ -85,7 +85,7 @@ size_t _z_iosli_writable(const _z_iosli_t *ios) { return ios->_capacity - ios->_ void _z_iosli_write(_z_iosli_t *ios, uint8_t b) { assert(_z_iosli_writable(ios) >= (size_t)1); ios->_buf[ios->_w_pos] = b; - ios->_w_pos = ios->_w_pos + 1; + ios->_w_pos += 1; } void _z_iosli_write_bytes(_z_iosli_t *ios, const uint8_t *bs, size_t offset, size_t length) { @@ -356,21 +356,22 @@ uint8_t _z_wbuf_get(const _z_wbuf_t *wbf, size_t pos) { } int8_t _z_wbuf_write(_z_wbuf_t *wbf, uint8_t b) { - int8_t ret = _Z_RES_OK; - _z_iosli_t *ios = _z_wbuf_get_iosli(wbf, wbf->_w_idx); size_t writable = _z_iosli_writable(ios); - if (writable >= (size_t)1) { - _z_iosli_write(ios, b); - } else if (wbf->_expansion_step != 0) { - ios = __z_wbuf_new_iosli(wbf->_expansion_step); - _z_wbuf_add_iosli(wbf, ios); - _z_iosli_write(ios, b); - } else { - ret = _Z_ERR_TRANSPORT_NO_SPACE; + if (writable == (size_t)0) { + wbf->_w_idx += 1; + if (wbf->_ioss._len <= wbf->_w_idx) { + if (wbf->_expansion_step != 0) { + ios = __z_wbuf_new_iosli(wbf->_expansion_step); + _z_iosli_vec_append(&wbf->_ioss, ios); + } else { + return _Z_ERR_TRANSPORT_NO_SPACE; + } + } + ios = _z_wbuf_get_iosli(wbf, wbf->_w_idx); } - - return ret; + _z_iosli_write(ios, b); + return _Z_RES_OK; } int8_t _z_wbuf_write_bytes(_z_wbuf_t *wbf, const uint8_t *bs, size_t offset, size_t length) { @@ -540,7 +541,7 @@ void _z_wbuf_reset(_z_wbuf_t *wbf) { wbf->_w_idx = 0; // Reset to default iosli allocation - for (size_t i = 0; i < _z_wbuf_len_iosli(wbf); i++) { + for (size_t i = 0; i < _z_iosli_vec_len(&wbf->_ioss); i++) { _z_iosli_t *ios = _z_wbuf_get_iosli(wbf, i); if (ios->_is_alloc == false) { _z_iosli_vec_remove(&wbf->_ioss, i); diff --git a/tests/z_iobuf_test.c b/tests/z_iobuf_test.c index a972aa8e5..4b7d41462 100644 --- a/tests/z_iobuf_test.c +++ b/tests/z_iobuf_test.c @@ -12,6 +12,7 @@ // ZettaScale Zenoh Team, // #include +#include #include #include #include @@ -437,13 +438,15 @@ void wbuf_put_zbuf_get(void) { void wbuf_reusable_write_zbuf_read(void) { _z_wbuf_t wbf = gen_wbuf(128); - for (int i = 0; i < 10; i++) { + for (int i = 1; i <= 10; i++) { size_t len = z_random_u8() % 128; - printf("\n>>> WBuf => Write and Read\n"); + printf("\n>>> WBuf => Write and Read round %d\n", i); print_wbuf_overview(&wbf); printf(" Writing %zu bytes\n", len); for (size_t z = 0; z < len; z++) { - _z_wbuf_write(&wbf, z % (uint8_t)255); + size_t prev_len = _z_wbuf_len(&wbf); + assert(_z_wbuf_write(&wbf, z % (uint8_t)255) == 0); + assert(_z_wbuf_len(&wbf) == prev_len + 1); } printf(" IOSlices: %zu, RIdx: %zu, WIdx: %zu\n", _z_wbuf_len_iosli(&wbf), wbf._r_idx, wbf._w_idx); From 410848a62ebfd8335218df893a6c828ac8bdf7e1 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 21 Aug 2023 18:40:24 +0200 Subject: [PATCH 159/181] fix platformio fetch link --- .github/workflows/arduino_esp32.yaml | 229 +++++++++++++------------- .github/workflows/espidf.yaml | 229 +++++++++++++------------- .github/workflows/mbed.yaml | 229 +++++++++++++------------- .github/workflows/zephyr.yaml | 235 +++++++++++++-------------- 4 files changed, 459 insertions(+), 463 deletions(-) diff --git a/.github/workflows/arduino_esp32.yaml b/.github/workflows/arduino_esp32.yaml index d54477ee0..5b313a503 100644 --- a/.github/workflows/arduino_esp32.yaml +++ b/.github/workflows/arduino_esp32.yaml @@ -15,129 +15,128 @@ name: arduino_esp32 on: push: - branches: [ '**' ] + branches: ["**"] pull_request: - branches: [ '**' ] + branches: ["**"] jobs: build: - name: Build on ${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - os: [ ubuntu-latest ] + os: [ubuntu-latest] steps: - - uses: actions/checkout@v2 - - - name: Install requirements - run: | - echo "No requirements need to be installed" - - - name: Install PlatformIO - run: | - python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)" - . $HOME/.platformio/penv/bin/activate - platformio update - - - name: Set up project - run: | - cd $HOME - export ARDUINO_BASE=$HOME/work/arduino_esp32project/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - mkdir -p $ARDUINO_BASE - cd $ARDUINO_BASE - platformio init -b esp32thing_plus --project-option="build_flags=-DZ_LINK_BLUETOOTH=1 -DZENOH_DEBUG=3" - - cd $ARDUINO_BASE/lib - ln -s $ZENOH_PICO_BASE - - cd $ARDUINO_BASE - - - name: Build z_pub example - run: | - cd $HOME - export ARDUINO_BASE=$HOME/work/arduino_esp32project/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ARDUINO_BASE/src/* - cd $ARDUINO_BASE/src - ln -s $ZENOH_PICO_BASE/examples/arduino/z_pub.ino - - cd $ARDUINO_BASE - platformio run - - - name: Build z_sub example - run: | - cd $HOME - export ARDUINO_BASE=$HOME/work/arduino_esp32project/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ARDUINO_BASE/src/* - cd $ARDUINO_BASE/src - ln -s $ZENOH_PICO_BASE/examples/arduino/z_sub.ino - - cd $ARDUINO_BASE - platformio run - - - name: Build z_pull example - run: | - cd $HOME - export ARDUINO_BASE=$HOME/work/arduino_esp32project/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ARDUINO_BASE/src/* - cd $ARDUINO_BASE/src - ln -s $ZENOH_PICO_BASE/examples/arduino/z_pull.ino - - cd $ARDUINO_BASE - platformio run - - - name: Build z_get example - run: | - cd $HOME - export ARDUINO_BASE=$HOME/work/arduino_esp32project/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ARDUINO_BASE/src/* - cd $ARDUINO_BASE/src - ln -s $ZENOH_PICO_BASE/examples/arduino/z_get.ino - - cd $ARDUINO_BASE - platformio run - - - name: Build z_queryable example - run: | - cd $HOME - export ARDUINO_BASE=$HOME/work/arduino_esp32project/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ARDUINO_BASE/src/* - cd $ARDUINO_BASE/src - ln -s $ZENOH_PICO_BASE/examples/arduino/z_queryable.ino - - cd $ARDUINO_BASE - platformio run - - - name: Build z_scout example - run: | - cd $HOME - export ARDUINO_BASE=$HOME/work/arduino_esp32project/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ARDUINO_BASE/src/* - cd $ARDUINO_BASE/src - ln -s $ZENOH_PICO_BASE/examples/arduino/z_scout.ino - - cd $ARDUINO_BASE - platformio run + - uses: actions/checkout@v2 + + - name: Install requirements + run: | + echo "No requirements need to be installed" + + - name: Install PlatformIO + run: | + python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py)" + . $HOME/.platformio/penv/bin/activate + platformio update + + - name: Set up project + run: | + cd $HOME + export ARDUINO_BASE=$HOME/work/arduino_esp32project/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + mkdir -p $ARDUINO_BASE + cd $ARDUINO_BASE + platformio init -b esp32thing_plus --project-option="build_flags=-DZ_LINK_BLUETOOTH=1 -DZENOH_DEBUG=3" + + cd $ARDUINO_BASE/lib + ln -s $ZENOH_PICO_BASE + + cd $ARDUINO_BASE + + - name: Build z_pub example + run: | + cd $HOME + export ARDUINO_BASE=$HOME/work/arduino_esp32project/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ARDUINO_BASE/src/* + cd $ARDUINO_BASE/src + ln -s $ZENOH_PICO_BASE/examples/arduino/z_pub.ino + + cd $ARDUINO_BASE + platformio run + + - name: Build z_sub example + run: | + cd $HOME + export ARDUINO_BASE=$HOME/work/arduino_esp32project/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ARDUINO_BASE/src/* + cd $ARDUINO_BASE/src + ln -s $ZENOH_PICO_BASE/examples/arduino/z_sub.ino + + cd $ARDUINO_BASE + platformio run + + - name: Build z_pull example + run: | + cd $HOME + export ARDUINO_BASE=$HOME/work/arduino_esp32project/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ARDUINO_BASE/src/* + cd $ARDUINO_BASE/src + ln -s $ZENOH_PICO_BASE/examples/arduino/z_pull.ino + + cd $ARDUINO_BASE + platformio run + + - name: Build z_get example + run: | + cd $HOME + export ARDUINO_BASE=$HOME/work/arduino_esp32project/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ARDUINO_BASE/src/* + cd $ARDUINO_BASE/src + ln -s $ZENOH_PICO_BASE/examples/arduino/z_get.ino + + cd $ARDUINO_BASE + platformio run + + - name: Build z_queryable example + run: | + cd $HOME + export ARDUINO_BASE=$HOME/work/arduino_esp32project/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ARDUINO_BASE/src/* + cd $ARDUINO_BASE/src + ln -s $ZENOH_PICO_BASE/examples/arduino/z_queryable.ino + + cd $ARDUINO_BASE + platformio run + + - name: Build z_scout example + run: | + cd $HOME + export ARDUINO_BASE=$HOME/work/arduino_esp32project/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ARDUINO_BASE/src/* + cd $ARDUINO_BASE/src + ln -s $ZENOH_PICO_BASE/examples/arduino/z_scout.ino + + cd $ARDUINO_BASE + platformio run diff --git a/.github/workflows/espidf.yaml b/.github/workflows/espidf.yaml index 181b60af4..c8b037449 100644 --- a/.github/workflows/espidf.yaml +++ b/.github/workflows/espidf.yaml @@ -15,129 +15,128 @@ name: espidf on: push: - branches: [ '**' ] + branches: ["**"] pull_request: - branches: [ '**' ] + branches: ["**"] jobs: build: - name: Build on ${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - os: [ ubuntu-latest ] + os: [ubuntu-latest] steps: - - uses: actions/checkout@v2 - - - name: Install requirements - run: | - echo "No requirements need to be installed" - - - name: Install PlatformIO - run: | - python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)" - . $HOME/.platformio/penv/bin/activate - platformio update - - - name: Set up project - run: | - cd $HOME - export ESPIDF_BASE=$HOME/work/espidfproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - mkdir -p $ESPIDF_BASE - cd $ESPIDF_BASE - platformio init -b az-delivery-devkit-v4 --project-option="framework=espidf" --project-option="build_flags=-DZENOH_ESPIDF -DZENOH_DEBUG=3" - - cd $ESPIDF_BASE/lib - ln -s $ZENOH_PICO_BASE - - cd $ESPIDF_BASE - - - name: Build z_pub example - run: | - cd $HOME - export ESPIDF_BASE=$HOME/work/espidfproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ESPIDF_BASE/src/* - cd $ESPIDF_BASE/src - ln -s $ZENOH_PICO_BASE/examples/espidf/z_pub.c - - cd $ESPIDF_BASE - platformio run - - - name: Build z_sub example - run: | - cd $HOME - export ESPIDF_BASE=$HOME/work/espidfproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ESPIDF_BASE/src/* - cd $ESPIDF_BASE/src - ln -s $ZENOH_PICO_BASE/examples/espidf/z_sub.c - - cd $ESPIDF_BASE - platformio run - - - name: Build z_pull example - run: | - cd $HOME - export ESPIDF_BASE=$HOME/work/espidfproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ESPIDF_BASE/src/* - cd $ESPIDF_BASE/src - ln -s $ZENOH_PICO_BASE/examples/espidf/z_pull.c - - cd $ESPIDF_BASE - platformio run - - - name: Build z_get example - run: | - cd $HOME - export ESPIDF_BASE=$HOME/work/espidfproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ESPIDF_BASE/src/* - cd $ESPIDF_BASE/src - ln -s $ZENOH_PICO_BASE/examples/espidf/z_get.c - - cd $ESPIDF_BASE - platformio run - - - name: Build z_queryable example - run: | - cd $HOME - export ESPIDF_BASE=$HOME/work/espidfproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ESPIDF_BASE/src/* - cd $ESPIDF_BASE/src - ln -s $ZENOH_PICO_BASE/examples/espidf/z_queryable.c - - cd $ESPIDF_BASE - platformio run - - - name: Build z_scout example - run: | - cd $HOME - export ESPIDF_BASE=$HOME/work/espidfproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ESPIDF_BASE/src/* - cd $ESPIDF_BASE/src - ln -s $ZENOH_PICO_BASE/examples/espidf/z_scout.c - - cd $ESPIDF_BASE - platformio run + - uses: actions/checkout@v2 + + - name: Install requirements + run: | + echo "No requirements need to be installed" + + - name: Install PlatformIO + run: | + python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py)" + . $HOME/.platformio/penv/bin/activate + platformio update + + - name: Set up project + run: | + cd $HOME + export ESPIDF_BASE=$HOME/work/espidfproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + mkdir -p $ESPIDF_BASE + cd $ESPIDF_BASE + platformio init -b az-delivery-devkit-v4 --project-option="framework=espidf" --project-option="build_flags=-DZENOH_ESPIDF -DZENOH_DEBUG=3" + + cd $ESPIDF_BASE/lib + ln -s $ZENOH_PICO_BASE + + cd $ESPIDF_BASE + + - name: Build z_pub example + run: | + cd $HOME + export ESPIDF_BASE=$HOME/work/espidfproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ESPIDF_BASE/src/* + cd $ESPIDF_BASE/src + ln -s $ZENOH_PICO_BASE/examples/espidf/z_pub.c + + cd $ESPIDF_BASE + platformio run + + - name: Build z_sub example + run: | + cd $HOME + export ESPIDF_BASE=$HOME/work/espidfproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ESPIDF_BASE/src/* + cd $ESPIDF_BASE/src + ln -s $ZENOH_PICO_BASE/examples/espidf/z_sub.c + + cd $ESPIDF_BASE + platformio run + + - name: Build z_pull example + run: | + cd $HOME + export ESPIDF_BASE=$HOME/work/espidfproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ESPIDF_BASE/src/* + cd $ESPIDF_BASE/src + ln -s $ZENOH_PICO_BASE/examples/espidf/z_pull.c + + cd $ESPIDF_BASE + platformio run + + - name: Build z_get example + run: | + cd $HOME + export ESPIDF_BASE=$HOME/work/espidfproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ESPIDF_BASE/src/* + cd $ESPIDF_BASE/src + ln -s $ZENOH_PICO_BASE/examples/espidf/z_get.c + + cd $ESPIDF_BASE + platformio run + + - name: Build z_queryable example + run: | + cd $HOME + export ESPIDF_BASE=$HOME/work/espidfproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ESPIDF_BASE/src/* + cd $ESPIDF_BASE/src + ln -s $ZENOH_PICO_BASE/examples/espidf/z_queryable.c + + cd $ESPIDF_BASE + platformio run + + - name: Build z_scout example + run: | + cd $HOME + export ESPIDF_BASE=$HOME/work/espidfproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ESPIDF_BASE/src/* + cd $ESPIDF_BASE/src + ln -s $ZENOH_PICO_BASE/examples/espidf/z_scout.c + + cd $ESPIDF_BASE + platformio run diff --git a/.github/workflows/mbed.yaml b/.github/workflows/mbed.yaml index 3c691cd5e..b4cc500f2 100644 --- a/.github/workflows/mbed.yaml +++ b/.github/workflows/mbed.yaml @@ -15,129 +15,128 @@ name: mbed on: push: - branches: [ '**' ] + branches: ["**"] pull_request: - branches: [ '**' ] + branches: ["**"] jobs: build: - name: Build on ${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - os: [ ubuntu-latest ] + os: [ubuntu-latest] steps: - - uses: actions/checkout@v2 - - - name: Install requirements - run: | - echo "No requirements need to be installed" - - - name: Install PlatformIO - run: | - python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)" - . $HOME/.platformio/penv/bin/activate - platformio update - - - name: Set up project - run: | - cd $HOME - export MBED_BASE=$HOME/work/mbedproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - mkdir -p $MBED_BASE - cd $MBED_BASE - pio init -b nucleo_f767zi --project-option="framework=mbed" --project-option="build_flags=-DZ_LINK_SERIAL=1 -DZENOH_DEBUG=3" - - cd $MBED_BASE/lib - ln -s $ZENOH_PICO_BASE - - cd $MBED_BASE - - - name: Build z_pub example - run: | - cd $HOME - export MBED_BASE=$HOME/work/mbedproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $MBED_BASE/src/* - cd $MBED_BASE/src - ln -s $ZENOH_PICO_BASE/examples/mbed/z_pub.cpp - - cd $MBED_BASE - platformio run - - - name: Build z_sub example - run: | - cd $HOME - export MBED_BASE=$HOME/work/mbedproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $MBED_BASE/src/* - cd $MBED_BASE/src - ln -s $ZENOH_PICO_BASE/examples/mbed/z_sub.cpp - - cd $MBED_BASE - platformio run - - - name: Build z_pull example - run: | - cd $HOME - export MBED_BASE=$HOME/work/mbedproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $MBED_BASE/src/* - cd $MBED_BASE/src - ln -s $ZENOH_PICO_BASE/examples/mbed/z_pull.cpp - - cd $MBED_BASE - platformio run - - - name: Build z_get example - run: | - cd $HOME - export MBED_BASE=$HOME/work/mbedproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $MBED_BASE/src/* - cd $MBED_BASE/src - ln -s $ZENOH_PICO_BASE/examples/mbed/z_get.cpp - - cd $MBED_BASE - platformio run - - - name: Build z_queryable example - run: | - cd $HOME - export MBED_BASE=$HOME/work/mbedproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $MBED_BASE/src/* - cd $MBED_BASE/src - ln -s $ZENOH_PICO_BASE/examples/mbed/z_queryable.cpp - - cd $MBED_BASE - platformio run - - - name: Build z_scout example - run: | - cd $HOME - export MBED_BASE=$HOME/work/mbedproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $MBED_BASE/src/* - cd $MBED_BASE/src - ln -s $ZENOH_PICO_BASE/examples/mbed/z_scout.cpp - - cd $MBED_BASE - platformio run + - uses: actions/checkout@v2 + + - name: Install requirements + run: | + echo "No requirements need to be installed" + + - name: Install PlatformIO + run: | + python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py)" + . $HOME/.platformio/penv/bin/activate + platformio update + + - name: Set up project + run: | + cd $HOME + export MBED_BASE=$HOME/work/mbedproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + mkdir -p $MBED_BASE + cd $MBED_BASE + pio init -b nucleo_f767zi --project-option="framework=mbed" --project-option="build_flags=-DZ_LINK_SERIAL=1 -DZENOH_DEBUG=3" + + cd $MBED_BASE/lib + ln -s $ZENOH_PICO_BASE + + cd $MBED_BASE + + - name: Build z_pub example + run: | + cd $HOME + export MBED_BASE=$HOME/work/mbedproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $MBED_BASE/src/* + cd $MBED_BASE/src + ln -s $ZENOH_PICO_BASE/examples/mbed/z_pub.cpp + + cd $MBED_BASE + platformio run + + - name: Build z_sub example + run: | + cd $HOME + export MBED_BASE=$HOME/work/mbedproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $MBED_BASE/src/* + cd $MBED_BASE/src + ln -s $ZENOH_PICO_BASE/examples/mbed/z_sub.cpp + + cd $MBED_BASE + platformio run + + - name: Build z_pull example + run: | + cd $HOME + export MBED_BASE=$HOME/work/mbedproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $MBED_BASE/src/* + cd $MBED_BASE/src + ln -s $ZENOH_PICO_BASE/examples/mbed/z_pull.cpp + + cd $MBED_BASE + platformio run + + - name: Build z_get example + run: | + cd $HOME + export MBED_BASE=$HOME/work/mbedproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $MBED_BASE/src/* + cd $MBED_BASE/src + ln -s $ZENOH_PICO_BASE/examples/mbed/z_get.cpp + + cd $MBED_BASE + platformio run + + - name: Build z_queryable example + run: | + cd $HOME + export MBED_BASE=$HOME/work/mbedproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $MBED_BASE/src/* + cd $MBED_BASE/src + ln -s $ZENOH_PICO_BASE/examples/mbed/z_queryable.cpp + + cd $MBED_BASE + platformio run + + - name: Build z_scout example + run: | + cd $HOME + export MBED_BASE=$HOME/work/mbedproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $MBED_BASE/src/* + cd $MBED_BASE/src + ln -s $ZENOH_PICO_BASE/examples/mbed/z_scout.cpp + + cd $MBED_BASE + platformio run diff --git a/.github/workflows/zephyr.yaml b/.github/workflows/zephyr.yaml index acc52a51d..32136c638 100644 --- a/.github/workflows/zephyr.yaml +++ b/.github/workflows/zephyr.yaml @@ -15,132 +15,131 @@ name: zephyr on: push: - branches: [ '**' ] + branches: ["**"] pull_request: - branches: [ '**' ] + branches: ["**"] jobs: build: - name: Build on ${{ matrix.os }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - os: [ ubuntu-latest ] + os: [ubuntu-latest] steps: - - uses: actions/checkout@v2 - - - name: Install requirements - run: | - echo "No requirements need to be installed" - - - name: Install PlatformIO - run: | - python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)" - . $HOME/.platformio/penv/bin/activate - platformio update - - - name: Set up project - run: | - cd $HOME - export ZEPHYR_BASE=$HOME/work/zephyrproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - mkdir -p $ZEPHYR_BASE - cd $ZEPHYR_BASE - pio init -b nucleo_f767zi --project-option="framework=zephyr" --project-option="build_flags=-DZ_LINK_SERIAL=1 -DZENOH_DEBUG=3" - - cd $ZEPHYR_BASE/lib - ln -s $ZENOH_PICO_BASE - - mkdir -p $ZEPHYR_BASE/zephyr - cd $ZEPHYR_BASE/zephyr - ln -s $ZENOH_PICO_BASE/docs/zephyr/nucleo_f767zi/prj.conf prj.conf - ln -s $ZENOH_PICO_BASE/docs/zephyr/nucleo_f767zi/CMakeLists.txt CMakeLists.txt - - - name: Build z_pub example - run: | - cd $HOME - export ZEPHYR_BASE=$HOME/work/zephyrproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ZEPHYR_BASE/src/* - cd $ZEPHYR_BASE/src - ln -s $ZENOH_PICO_BASE/examples/zephyr/z_pub.c - - cd $ZEPHYR_BASE - platformio run - - - name: Build z_sub example - run: | - cd $HOME - export ZEPHYR_BASE=$HOME/work/zephyrproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ZEPHYR_BASE/src/* - cd $ZEPHYR_BASE/src - ln -s $ZENOH_PICO_BASE/examples/zephyr/z_sub.c - - cd $ZEPHYR_BASE - platformio run - - - name: Build z_pull example - run: | - cd $HOME - export ZEPHYR_BASE=$HOME/work/zephyrproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ZEPHYR_BASE/src/* - cd $ZEPHYR_BASE/src - ln -s $ZENOH_PICO_BASE/examples/zephyr/z_pull.c - - cd $ZEPHYR_BASE - platformio run - - - name: Build z_get example - run: | - cd $HOME - export ZEPHYR_BASE=$HOME/work/zephyrproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ZEPHYR_BASE/src/* - cd $ZEPHYR_BASE/src - ln -s $ZENOH_PICO_BASE/examples/zephyr/z_get.c - - cd $ZEPHYR_BASE - platformio run - - - name: Build z_queryable example - run: | - cd $HOME - export ZEPHYR_BASE=$HOME/work/zephyrproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ZEPHYR_BASE/src/* - cd $ZEPHYR_BASE/src - ln -s $ZENOH_PICO_BASE/examples/zephyr/z_queryable.c - - cd $ZEPHYR_BASE - platformio run - - - name: Build z_scout example - run: | - cd $HOME - export ZEPHYR_BASE=$HOME/work/zephyrproject/ - export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ - . $HOME/.platformio/penv/bin/activate - - rm -rf $ZEPHYR_BASE/src/* - cd $ZEPHYR_BASE/src - ln -s $ZENOH_PICO_BASE/examples/zephyr/z_scout.c - - cd $ZEPHYR_BASE - platformio run + - uses: actions/checkout@v2 + + - name: Install requirements + run: | + echo "No requirements need to be installed" + + - name: Install PlatformIO + run: | + python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py)" + . $HOME/.platformio/penv/bin/activate + platformio update + + - name: Set up project + run: | + cd $HOME + export ZEPHYR_BASE=$HOME/work/zephyrproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + mkdir -p $ZEPHYR_BASE + cd $ZEPHYR_BASE + pio init -b nucleo_f767zi --project-option="framework=zephyr" --project-option="build_flags=-DZ_LINK_SERIAL=1 -DZENOH_DEBUG=3" + + cd $ZEPHYR_BASE/lib + ln -s $ZENOH_PICO_BASE + + mkdir -p $ZEPHYR_BASE/zephyr + cd $ZEPHYR_BASE/zephyr + ln -s $ZENOH_PICO_BASE/docs/zephyr/nucleo_f767zi/prj.conf prj.conf + ln -s $ZENOH_PICO_BASE/docs/zephyr/nucleo_f767zi/CMakeLists.txt CMakeLists.txt + + - name: Build z_pub example + run: | + cd $HOME + export ZEPHYR_BASE=$HOME/work/zephyrproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ZEPHYR_BASE/src/* + cd $ZEPHYR_BASE/src + ln -s $ZENOH_PICO_BASE/examples/zephyr/z_pub.c + + cd $ZEPHYR_BASE + platformio run + + - name: Build z_sub example + run: | + cd $HOME + export ZEPHYR_BASE=$HOME/work/zephyrproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ZEPHYR_BASE/src/* + cd $ZEPHYR_BASE/src + ln -s $ZENOH_PICO_BASE/examples/zephyr/z_sub.c + + cd $ZEPHYR_BASE + platformio run + + - name: Build z_pull example + run: | + cd $HOME + export ZEPHYR_BASE=$HOME/work/zephyrproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ZEPHYR_BASE/src/* + cd $ZEPHYR_BASE/src + ln -s $ZENOH_PICO_BASE/examples/zephyr/z_pull.c + + cd $ZEPHYR_BASE + platformio run + + - name: Build z_get example + run: | + cd $HOME + export ZEPHYR_BASE=$HOME/work/zephyrproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ZEPHYR_BASE/src/* + cd $ZEPHYR_BASE/src + ln -s $ZENOH_PICO_BASE/examples/zephyr/z_get.c + + cd $ZEPHYR_BASE + platformio run + + - name: Build z_queryable example + run: | + cd $HOME + export ZEPHYR_BASE=$HOME/work/zephyrproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ZEPHYR_BASE/src/* + cd $ZEPHYR_BASE/src + ln -s $ZENOH_PICO_BASE/examples/zephyr/z_queryable.c + + cd $ZEPHYR_BASE + platformio run + + - name: Build z_scout example + run: | + cd $HOME + export ZEPHYR_BASE=$HOME/work/zephyrproject/ + export ZENOH_PICO_BASE=$HOME/work/zenoh-pico/zenoh-pico/ + . $HOME/.platformio/penv/bin/activate + + rm -rf $ZEPHYR_BASE/src/* + cd $ZEPHYR_BASE/src + ln -s $ZENOH_PICO_BASE/examples/zephyr/z_scout.c + + cd $ZEPHYR_BASE + platformio run From 230b3b9b6ed3fb73f6f7d6e0f933915012e7c806 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 21 Aug 2023 18:43:13 +0200 Subject: [PATCH 160/181] fix platformio _again_ --- .github/workflows/arduino_esp32.yaml | 3 ++- .github/workflows/espidf.yaml | 3 ++- .github/workflows/mbed.yaml | 3 ++- .github/workflows/zephyr.yaml | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/workflows/arduino_esp32.yaml b/.github/workflows/arduino_esp32.yaml index 5b313a503..197ce7776 100644 --- a/.github/workflows/arduino_esp32.yaml +++ b/.github/workflows/arduino_esp32.yaml @@ -37,7 +37,8 @@ jobs: - name: Install PlatformIO run: | - python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py)" + curl -fsSL -o get-platformio.py https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py + python3 get-platformio.py . $HOME/.platformio/penv/bin/activate platformio update diff --git a/.github/workflows/espidf.yaml b/.github/workflows/espidf.yaml index c8b037449..6f46eb0e4 100644 --- a/.github/workflows/espidf.yaml +++ b/.github/workflows/espidf.yaml @@ -37,7 +37,8 @@ jobs: - name: Install PlatformIO run: | - python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py)" + curl -fsSL -o get-platformio.py https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py + python3 get-platformio.py . $HOME/.platformio/penv/bin/activate platformio update diff --git a/.github/workflows/mbed.yaml b/.github/workflows/mbed.yaml index b4cc500f2..03386d06e 100644 --- a/.github/workflows/mbed.yaml +++ b/.github/workflows/mbed.yaml @@ -37,7 +37,8 @@ jobs: - name: Install PlatformIO run: | - python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py)" + curl -fsSL -o get-platformio.py https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py + python3 get-platformio.py . $HOME/.platformio/penv/bin/activate platformio update diff --git a/.github/workflows/zephyr.yaml b/.github/workflows/zephyr.yaml index 32136c638..7ec080212 100644 --- a/.github/workflows/zephyr.yaml +++ b/.github/workflows/zephyr.yaml @@ -37,7 +37,8 @@ jobs: - name: Install PlatformIO run: | - python3 -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py)" + curl -fsSL -o get-platformio.py https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py + python3 get-platformio.py . $HOME/.platformio/penv/bin/activate platformio update From 219fd4efd47383edb80188eafe53f32dd7d67946 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 21 Aug 2023 18:47:09 +0200 Subject: [PATCH 161/181] fix warns --- src/session/subscription.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/session/subscription.c b/src/session/subscription.c index 504d1da7a..1d8ef47c2 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -16,6 +16,8 @@ #include +#include + #include "zenoh-pico/config.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/keyexpr.h" @@ -176,7 +178,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co s.kind = kind; s.timestamp = timestamp; _z_subscription_sptr_list_t *xs = subs; - _Z_DEBUG("Triggering %ld subs\n", _z_subscription_sptr_list_len(xs)); + _Z_DEBUG("Triggering %zd subs\n", (uintmax_t)_z_subscription_sptr_list_len(xs)); while (xs != NULL) { _z_subscription_sptr_t *sub = _z_subscription_sptr_list_head(xs); sub->ptr->_callback(&s, sub->ptr->_arg); From 14a9c9b79895ade5365b2f5ce778968053a56152 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 21 Aug 2023 18:51:32 +0200 Subject: [PATCH 162/181] clangd wanted to add a cpp import fo rsome reason --- 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 1d8ef47c2..117a02507 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -15,8 +15,7 @@ #include "zenoh-pico/session/subscription.h" #include - -#include +#include #include "zenoh-pico/config.h" #include "zenoh-pico/protocol/core.h" From c5f879ab15b426e4b4e1e715557dd7e24b4a877c Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 21 Aug 2023 18:55:46 +0200 Subject: [PATCH 163/181] wrong format code --- src/session/subscription.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session/subscription.c b/src/session/subscription.c index 117a02507..fd057aac3 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -177,7 +177,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co s.kind = kind; s.timestamp = timestamp; _z_subscription_sptr_list_t *xs = subs; - _Z_DEBUG("Triggering %zd subs\n", (uintmax_t)_z_subscription_sptr_list_len(xs)); + _Z_DEBUG("Triggering %ju subs\n", (uintmax_t)_z_subscription_sptr_list_len(xs)); while (xs != NULL) { _z_subscription_sptr_t *sub = _z_subscription_sptr_list_head(xs); sub->ptr->_callback(&s, sub->ptr->_arg); From 82f060f4c6d90862ef813a36048bf76a307f3206 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Thu, 24 Aug 2023 11:03:53 +0200 Subject: [PATCH 164/181] fix declarations, including prefixes for subs --- BSDmakefile | 3 +- GNUmakefile | 3 +- include/zenoh-pico/protocol/keyexpr.h | 1 + include/zenoh-pico/session/resource.h | 2 +- src/api/api.c | 13 +++++- src/net/primitives.c | 27 +++++------- src/protocol/keyexpr.c | 4 ++ src/session/resource.c | 60 +++++++++++++++++---------- src/session/rx.c | 10 ++--- src/transport/multicast/link/rx.c | 3 +- tests/z_api_alignment_test.c | 1 + zenohpico.pc | 2 +- 12 files changed, 80 insertions(+), 49 deletions(-) diff --git a/BSDmakefile b/BSDmakefile index 5dc504035..790a7288e 100644 --- a/BSDmakefile +++ b/BSDmakefile @@ -62,8 +62,9 @@ CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DCMAK all: make -$(BUILD_DIR)/Makefile: CMakeLists.txt +$(BUILD_DIR)/Makefile: mkdir -p $(BUILD_DIR) + echo $(CMAKE_OPT) cmake $(CMAKE_OPT) -B$(BUILD_DIR) make: $(BUILD_DIR)/Makefile diff --git a/GNUmakefile b/GNUmakefile index 6ea060ed9..cf4512a1b 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -62,8 +62,9 @@ CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DCMAK all: make -$(BUILD_DIR)/Makefile: CMakeLists.txt +$(BUILD_DIR)/Makefile: mkdir -p $(BUILD_DIR) + echo $(CMAKE_OPT) cmake $(CMAKE_OPT) -B $(BUILD_DIR) make: $(BUILD_DIR)/Makefile diff --git a/include/zenoh-pico/protocol/keyexpr.h b/include/zenoh-pico/protocol/keyexpr.h index 1309e1e3c..05fa45f8d 100644 --- a/include/zenoh-pico/protocol/keyexpr.h +++ b/include/zenoh-pico/protocol/keyexpr.h @@ -27,6 +27,7 @@ _Bool _z_keyexpr_intersects(const char *lstart, const size_t llen, const char *r /*------------------ clone/Copy/Free helpers ------------------*/ void _z_keyexpr_copy(_z_keyexpr_t *dst, const _z_keyexpr_t *src); _z_keyexpr_t _z_keyexpr_duplicate(_z_keyexpr_t src); +_z_keyexpr_t _z_keyexpr_to_owned(_z_keyexpr_t src); _z_keyexpr_t _z_keyexpr_alias(_z_keyexpr_t src); _z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src); static inline _z_keyexpr_t _z_keyexpr_null(void) { return (_z_keyexpr_t){._id = 0, ._mapping = {0}, ._suffix = NULL}; } diff --git a/include/zenoh-pico/session/resource.h b/include/zenoh-pico/session/resource.h index aa09410a6..7c6ef0cc1 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); -int8_t _z_register_resource(_z_session_t *zn, _z_resource_t *res); +int16_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/src/api/api.c b/src/api/api.c index 3cef046c4..3a53f3a34 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -31,6 +31,8 @@ #include "zenoh-pico/session/queryable.h" #include "zenoh-pico/session/resource.h" #include "zenoh-pico/session/utils.h" +#include "zenoh-pico/system/platform.h" +#include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" #include "zenoh-pico/utils/uuid.h" @@ -818,8 +820,17 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z #endif // Z_MULTICAST_TRANSPORT == 1 _z_resource_t *r = _z_get_resource_by_key(zs._val, &keyexpr); if (r == NULL) { + char *wild = strpbrk(keyexpr._suffix, "*$"); + if (wild != NULL) { + size_t len = wild - keyexpr._suffix; + char *suffix = z_malloc(len + 1); + memcpy(suffix, keyexpr._suffix, len); + suffix[len] = 0; + keyexpr._suffix = suffix; + _z_keyexpr_set_owns_suffix(&keyexpr, true); + } uint16_t id = _z_declare_resource(zs._val, keyexpr); - key = _z_rid_with_suffix(id, NULL); + key = _z_rid_with_suffix(id, wild); } #if Z_MULTICAST_TRANSPORT == 1 } diff --git a/src/net/primitives.c b/src/net/primitives.c index 9c4daadef..d3eba83e4 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -54,25 +54,18 @@ uint16_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr) { if (zn->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) { // FIXME: remove when resource declaration is implemented for multicast transport - - _z_resource_t *r = (_z_resource_t *)z_malloc(sizeof(_z_resource_t)); - if (r != NULL) { - r->_id = _z_get_resource_id(zn); - r->_key = _z_keyexpr_duplicate(keyexpr); - if (_z_register_resource(zn, r) == _Z_RES_OK) { - // Build the declare message to send on the wire - _z_keyexpr_t alias = _z_keyexpr_alias(r->_key); - _z_declaration_t declaration = _z_make_decl_keyexpr(r->_id, &alias); - _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); - if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { - ret = r->_id; - } else { - _z_unregister_resource(zn, r->_id, _z_keyexpr_mapping_id(&keyexpr)); - } - _z_n_msg_clear(&n_msg); + 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 + _z_keyexpr_t alias = _z_keyexpr_alias(keyexpr); + _z_declaration_t declaration = _z_make_decl_keyexpr(id, &alias); + _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); + if (_z_send_n_msg(zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { + ret = id; } else { - _z_resource_free(&r); + _z_unregister_resource(zn, id, _Z_KEYEXPR_MAPPING_LOCAL); } + _z_n_msg_clear(&n_msg); } } diff --git a/src/protocol/keyexpr.c b/src/protocol/keyexpr.c index b18d974a8..5d52a4377 100644 --- a/src/protocol/keyexpr.c +++ b/src/protocol/keyexpr.c @@ -45,6 +45,10 @@ _z_keyexpr_t _z_keyexpr_duplicate(_z_keyexpr_t src) { return dst; } +_z_keyexpr_t _z_keyexpr_to_owned(_z_keyexpr_t src) { + return _z_keyexpr_owns_suffix(&src) ? src : _z_keyexpr_duplicate(src); +} + _z_keyexpr_t _z_keyexpr_steal(_Z_MOVE(_z_keyexpr_t) src) { _z_keyexpr_t stolen = *src; *src = _z_keyexpr_null(); diff --git a/src/session/resource.c b/src/session/resource.c index cf5dceb34..d8271fd68 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -20,7 +20,9 @@ #include "zenoh-pico/api/types.h" #include "zenoh-pico/config.h" #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/session/session.h" +#include "zenoh-pico/system/platform.h" #include "zenoh-pico/utils/logging.h" _Bool _z_resource_eq(const _z_resource_t *other, const _z_resource_t *this) { return this->_id == other->_id; } @@ -214,20 +216,31 @@ _z_keyexpr_t _z_get_expanded_key_from_key(_z_session_t *zn, const _z_keyexpr_t * return res; } -int8_t _z_register_resource(_z_session_t *zn, _z_resource_t *res) { - int8_t ret = _Z_RES_OK; +/// 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; + key = _z_keyexpr_alias(key); + uint16_t mapping = register_to_mapping; + uint16_t parent_mapping = _z_keyexpr_mapping_id(&key); - uint16_t mapping = _z_keyexpr_mapping_id(&res->_key); - _Z_DEBUG(">>> Allocating res decl for (%ju,%u:%s) on mapping 0x%x\n", (uintmax_t)res->_id, - (unsigned int)res->_key._id, res->_key._suffix, mapping); #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - // FIXME: check by keyexpr instead - _z_resource_t *r = __unsafe_z_get_resource_by_id(zn, mapping, res->_id); - if (r == NULL) { + if (key._id != Z_RESOURCE_ID_NONE) { + if (parent_mapping == mapping || parent_mapping == _Z_KEYEXPR_MAPPING_LOCAL) { + _z_resource_t *parent = __unsafe_z_get_resource_by_id(zn, mapping, key._id); + parent->_refcount++; + } else { + key = __unsafe_z_get_expanded_key_from_key(zn, &key); + } + } + _z_resource_t *res = z_malloc(sizeof(_z_resource_t)); + if ((key._suffix != NULL || mapping != _Z_KEYEXPR_MAPPING_LOCAL) && res != NULL) { res->_refcount = 1; + res->_key = _z_keyexpr_to_owned(key); + ret = id == Z_RESOURCE_ID_NONE ? _z_get_resource_id(zn) : id; + res->_id = ret; // Register the resource if (mapping == _Z_KEYEXPR_MAPPING_LOCAL) { zn->_local_resources = _z_resource_list_push(zn->_local_resources, res); @@ -235,8 +248,7 @@ int8_t _z_register_resource(_z_session_t *zn, _z_resource_t *res) { zn->_remote_resources = _z_resource_list_push(zn->_remote_resources, res); } } else { - r->_refcount++; - ret = _Z_ERR_ENTITY_DECLARATION_FAILED; + ret = Z_RESOURCE_ID_NONE; } #if Z_MULTI_THREAD == 1 @@ -251,20 +263,26 @@ void _z_unregister_resource(_z_session_t *zn, uint16_t id, uint16_t mapping) { #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - _z_resource_list_t *list = is_local ? zn->_local_resources : zn->_remote_resources; - while (list) { - _z_resource_t *head = _z_resource_list_head(list); - if (head && head->_id == id && _z_keyexpr_mapping_id(&head->_key) == mapping) { - head->_refcount--; - if (head->_refcount == 0) { - _z_resource_list_pop(list, &head); - _z_resource_free(&head); + while (id != 0) { + _z_resource_list_t **list = is_local ? &zn->_local_resources : &zn->_remote_resources; + _z_resource_list_t *tail = *list; + while (list) { + _z_resource_t *head = _z_resource_list_head(tail); + if (head && head->_id == id && _z_keyexpr_mapping_id(&head->_key) == mapping) { + head->_refcount--; + if (head->_refcount == 0) { + *list = _z_resource_list_pop(*list, &head); + id = head->_key._id; + mapping = _z_keyexpr_mapping_id(&head->_key); + _z_resource_free(&head); + } else { + id = 0; + } + break; } - break; + tail = _z_resource_list_tail(tail); } - list = _z_resource_list_tail(list); } - #if Z_MULTI_THREAD == 1 _z_mutex_unlock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 diff --git a/src/session/rx.c b/src/session/rx.c index 3497b85f5..fad9e46d9 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -42,10 +42,10 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 _z_n_msg_declare_t decl = msg->_body._declare; switch (decl._decl._tag) { case _Z_DECL_KEXPR: { - _z_resource_t *res = (_z_resource_t *)z_malloc(sizeof(_z_resource_t)); - res->_id = decl._decl._body._decl_kexpr._id; - res->_key = _z_keyexpr_duplicate(decl._decl._body._decl_kexpr._keyexpr); - ret = _z_register_resource(zn, res); + if (_z_register_resource(zn, decl._decl._body._decl_kexpr._keyexpr, + decl._decl._body._decl_kexpr._id, local_peer_id) == 0) { + ret = _Z_ERR_ENTITY_DECLARATION_FAILED; + } } break; case _Z_UNDECL_KEXPR: { _z_unregister_resource(zn, decl._decl._body._undecl_kexpr._id, local_peer_id); @@ -136,7 +136,7 @@ int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16 _z_msg_err_t error = response._body._err; _z_bytes_t payload = error._ext_value.payload; _Z_ERROR("Received Err for query %zu: code=%d, message=%.*s\n", response._request_id, error._code, - payload.len, payload.start); + (int)payload.len, payload.start); } break; case _Z_RESPONSE_BODY_ACK: { // @TODO: implement ACKs for puts/dels diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 9211b2e2e..17bc9fd9b 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -15,6 +15,7 @@ #include "zenoh-pico/transport/link/rx.h" #include +#include #include "zenoh-pico/config.h" #include "zenoh-pico/protocol/codec/network.h" @@ -91,7 +92,7 @@ int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_me } while (false); // The 1-iteration loop to use continue to break the entire loop on error if (ret == _Z_RES_OK) { - _Z_DEBUG(">> \t transport_message_decode: %ld\n", _z_zbuf_len(&ztm->_zbuf)); + _Z_DEBUG(">> \t transport_message_decode: %ju\n", (uintmax_t)_z_zbuf_len(&ztm->_zbuf)); ret = _z_transport_message_decode(t_msg, &ztm->_zbuf); } diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 1e04dbe38..01119f1eb 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -305,6 +305,7 @@ int main(int argc, char **argv) { printf("Undeclaring Keyexpr..."); _ret_int8 = z_undeclare_keyexpr(z_loan(s1), z_move(_ret_expr)); + printf(" %02x\n", _ret_int8); assert(_ret_int8 == 0); assert(!z_check(_ret_expr)); printf("Ok\n"); diff --git a/zenohpico.pc b/zenohpico.pc index d97573d33..3cb366494 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.10.20230821dev +Version: 0.10.20230824dev Cflags: -I${prefix}/ Libs: -L${prefix}/ -lzenohpico From a46e23622b79068c76249cdab042e53f5878c7c2 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 25 Aug 2023 15:42:50 +0200 Subject: [PATCH 165/181] fix undeclaration error reporting --- src/api/api.c | 3 +- src/net/primitives.c | 12 +++-- src/session/resource.c | 62 +++++++++++---------- tests/api.sh | 2 +- tests/multicast.sh | 2 +- tests/routed.sh | 2 +- tests/z_api_alignment_test.c | 102 +++++++++++++++++++---------------- zenohpico.pc | 2 +- 8 files changed, 103 insertions(+), 84 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 3a53f3a34..261305be1 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -821,7 +821,8 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z _z_resource_t *r = _z_get_resource_by_key(zs._val, &keyexpr); if (r == NULL) { char *wild = strpbrk(keyexpr._suffix, "*$"); - if (wild != NULL) { + if (wild != NULL && wild != keyexpr._suffix) { + wild -= 1; size_t len = wild - keyexpr._suffix; char *suffix = z_malloc(len + 1); memcpy(suffix, keyexpr._suffix, len); diff --git a/src/net/primitives.c b/src/net/primitives.c index d3eba83e4..6397dd588 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -31,6 +31,8 @@ #include "zenoh-pico/session/resource.h" #include "zenoh-pico/session/subscription.h" #include "zenoh-pico/session/utils.h" +#include "zenoh-pico/utils/logging.h" +#include "zenoh-pico/utils/result.h" /*------------------ Scouting ------------------*/ void _z_scout(const z_what_t what, const _z_id_t zid, const char *locator, const uint32_t timeout, @@ -74,8 +76,8 @@ uint16_t _z_declare_resource(_z_session_t *zn, _z_keyexpr_t keyexpr) { int8_t _z_undeclare_resource(_z_session_t *zn, uint16_t rid) { int8_t ret = _Z_RES_OK; - - _z_resource_t *r = _z_get_resource_by_id(zn, _Z_RESOURCE_IS_LOCAL, rid); + _Z_DEBUG("Undeclaring local keyexpr %d\n", rid); + _z_resource_t *r = _z_get_resource_by_id(zn, _Z_KEYEXPR_MAPPING_LOCAL, rid); if (r != NULL) { // Build the declare message to send on the wire _z_declaration_t declaration = _z_make_undecl_keyexpr(rid); @@ -113,7 +115,7 @@ _z_publisher_t *_z_declare_publisher(_z_session_t *zn, _z_keyexpr_t keyexpr, z_c } int8_t _z_undeclare_publisher(_z_publisher_t *pub) { - int8_t ret = _Z_ERR_GENERIC; + int8_t ret = _Z_RES_OK; if (pub != NULL) { // Build the declare message to send on the wire @@ -232,7 +234,7 @@ _z_queryable_t *_z_declare_queryable(_z_session_t *zn, _z_keyexpr_t keyexpr, _Bo } int8_t _z_undeclare_queryable(_z_queryable_t *qle) { - int8_t ret = _Z_ERR_GENERIC; + int8_t ret = _Z_RES_OK; if (qle != NULL) { _z_questionable_sptr_t *q = _z_get_questionable_by_id(qle->_zn, qle->_id); @@ -244,7 +246,7 @@ int8_t _z_undeclare_queryable(_z_queryable_t *qle) { // Only if message is successfully send, local queryable state can be removed _z_unregister_questionable(qle->_zn, q); } else { - ret = _Z_ERR_ENTITY_UNKNOWN; + ret = _Z_ERR_TRANSPORT_TX_FAILED; } _z_n_msg_clear(&n_msg); diff --git a/src/session/resource.c b/src/session/resource.c index d8271fd68..e7c7f4659 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -228,24 +228,29 @@ int16_t _z_register_resource(_z_session_t *zn, _z_keyexpr_t key, uint16_t id, ui #endif // Z_MULTI_THREAD == 1 if (key._id != Z_RESOURCE_ID_NONE) { - if (parent_mapping == mapping || parent_mapping == _Z_KEYEXPR_MAPPING_LOCAL) { - _z_resource_t *parent = __unsafe_z_get_resource_by_id(zn, mapping, key._id); + if (parent_mapping == mapping) { + _z_resource_t *parent = __unsafe_z_get_resource_by_id(zn, parent_mapping, key._id); parent->_refcount++; } else { key = __unsafe_z_get_expanded_key_from_key(zn, &key); } } - _z_resource_t *res = z_malloc(sizeof(_z_resource_t)); - if ((key._suffix != NULL || mapping != _Z_KEYEXPR_MAPPING_LOCAL) && res != NULL) { - res->_refcount = 1; - res->_key = _z_keyexpr_to_owned(key); - ret = id == Z_RESOURCE_ID_NONE ? _z_get_resource_id(zn) : id; - res->_id = ret; - // Register the resource - if (mapping == _Z_KEYEXPR_MAPPING_LOCAL) { - zn->_local_resources = _z_resource_list_push(zn->_local_resources, res); + ret = key._id; + if ((key._suffix != NULL)) { + _z_resource_t *res = z_malloc(sizeof(_z_resource_t)); + if (res == NULL) { + ret = Z_RESOURCE_ID_NONE; } else { - zn->_remote_resources = _z_resource_list_push(zn->_remote_resources, res); + res->_refcount = 1; + res->_key = _z_keyexpr_to_owned(key); + ret = id == Z_RESOURCE_ID_NONE ? _z_get_resource_id(zn) : id; + res->_id = ret; + // Register the resource + if (mapping == _Z_KEYEXPR_MAPPING_LOCAL) { + zn->_local_resources = _z_resource_list_push(zn->_local_resources, res); + } else { + zn->_remote_resources = _z_resource_list_push(zn->_remote_resources, res); + } } } else { ret = Z_RESOURCE_ID_NONE; @@ -260,18 +265,21 @@ int16_t _z_register_resource(_z_session_t *zn, _z_keyexpr_t key, uint16_t id, ui void _z_unregister_resource(_z_session_t *zn, uint16_t id, uint16_t mapping) { _Bool is_local = mapping == _Z_KEYEXPR_MAPPING_LOCAL; + _Z_DEBUG("unregistering: id %d, mapping: %d\n", id, mapping); #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 + _z_resource_list_t **parent_mut = is_local ? &zn->_local_resources : &zn->_remote_resources; while (id != 0) { - _z_resource_list_t **list = is_local ? &zn->_local_resources : &zn->_remote_resources; - _z_resource_list_t *tail = *list; - while (list) { - _z_resource_t *head = _z_resource_list_head(tail); + _z_resource_list_t *parent = *parent_mut; + while (parent != NULL) { + _z_resource_t *head = _z_resource_list_head(parent); + _Z_DEBUG("unreg check: id %d, mapping: %d, refcount: %d\n", head->_id, _z_keyexpr_mapping_id(&head->_key), + head->_refcount); if (head && head->_id == id && _z_keyexpr_mapping_id(&head->_key) == mapping) { head->_refcount--; if (head->_refcount == 0) { - *list = _z_resource_list_pop(*list, &head); + *parent_mut = _z_resource_list_pop(parent, &head); id = head->_key._id; mapping = _z_keyexpr_mapping_id(&head->_key); _z_resource_free(&head); @@ -280,7 +288,8 @@ void _z_unregister_resource(_z_session_t *zn, uint16_t id, uint16_t mapping) { } break; } - tail = _z_resource_list_tail(tail); + parent_mut = &parent->_tail; + parent = *parent_mut; } } #if Z_MULTI_THREAD == 1 @@ -288,20 +297,17 @@ void _z_unregister_resource(_z_session_t *zn, uint16_t id, uint16_t mapping) { #endif // Z_MULTI_THREAD == 1 } +_Bool _z_unregister_resource_for_peer_filter(const _z_resource_t *candidate, const _z_resource_t *ctx) { + uint16_t mapping = ctx->_id; + return _z_keyexpr_mapping_id(&candidate->_key) == mapping; +} void _z_unregister_resources_for_peer(_z_session_t *zn, uint16_t mapping) { #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 - _z_resource_list_t *list = zn->_remote_resources; - while (list) { - _z_resource_t *head = _z_resource_list_head(list); - if (head && _z_keyexpr_mapping_id(&head->_key) == mapping) { - _z_resource_list_pop(list, &head); - _z_resource_free(&head); - } else { - list = _z_resource_list_tail(list); - } - } + _z_resource_t ctx = {._id = mapping, ._refcount = 0, ._key = {0}}; + zn->_remote_resources = + _z_resource_list_drop_filter(zn->_remote_resources, _z_unregister_resource_for_peer_filter, &ctx); #if Z_MULTI_THREAD == 1 _z_mutex_unlock(&zn->_mutex_inner); diff --git a/tests/api.sh b/tests/api.sh index 7be9183b3..a4bf36acc 100644 --- a/tests/api.sh +++ b/tests/api.sh @@ -14,7 +14,7 @@ # TESTBIN="$1" -TESTDIR=$(dirname "$0") +TESTDIR=$(realpath $(dirname "$0")) if [ "$OSTYPE" = "msys" ]; then TESTBIN="$TESTDIR/Debug/$TESTBIN.exe" diff --git a/tests/multicast.sh b/tests/multicast.sh index d03dcb1dc..e6094f52c 100644 --- a/tests/multicast.sh +++ b/tests/multicast.sh @@ -14,7 +14,7 @@ # TESTBIN="$1" -TESTDIR=$(dirname "$0") +TESTDIR=$(realpath $(dirname "$0")) if [ "$OSTYPE" = "msys" ]; then TESTBIN="$TESTDIR/Debug/$TESTBIN.exe" diff --git a/tests/routed.sh b/tests/routed.sh index 72659e075..b1ab0d6d2 100644 --- a/tests/routed.sh +++ b/tests/routed.sh @@ -14,7 +14,7 @@ # TESTBIN="$1" -TESTDIR=$(dirname "$0") +TESTDIR=$(realpath $(dirname "$0")) if [ "$OSTYPE" = "msys" ]; then TESTBIN="$TESTDIR/Debug/$TESTBIN.exe" diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 01119f1eb..96b4cb3e6 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -29,6 +29,16 @@ #define SLEEP 2 #define SCOUTING_TIMEOUT "1000" +#define assert_eq(x, y) \ + { \ + int8_t l = x; \ + int8_t r = y; \ + if (l != r) { \ + printf("assert_eq failed: l=%d, r=%d\n", l, r); \ + assert(false); \ + } \ + } + const char *value = "Test value"; volatile unsigned int zids = 0; @@ -109,7 +119,7 @@ void data_handler(const z_sample_t *sample, void *arg) { } int main(int argc, char **argv) { - assert(argc == 2); + assert_eq(argc, 2); (void)(argc); setvbuf(stdout, NULL, _IOLBF, 1024); @@ -120,25 +130,25 @@ int main(int argc, char **argv) { printf("Testing Keyexpr..."); z_keyexpr_t key = z_keyexpr("demo/example"); _Bool _ret_bool = z_keyexpr_is_initialized(&key); - assert(_ret_bool == true); + assert_eq(_ret_bool, true); int8_t _ret_int8 = z_keyexpr_includes(z_keyexpr("demo/example/**"), z_keyexpr("demo/example/a")); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); #ifdef ZENOH_PICO _ret_int8 = zp_keyexpr_includes_null_terminated("demo/example/**", "demo/example/a"); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); #endif _ret_int8 = z_keyexpr_intersects(z_keyexpr("demo/example/**"), z_keyexpr("demo/example/a")); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); #ifdef ZENOH_PICO _ret_int8 = zp_keyexpr_intersect_null_terminated("demo/example/**", "demo/example/a"); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); #endif _ret_int8 = z_keyexpr_equals(z_keyexpr("demo/example/**"), z_keyexpr("demo/example")); - assert(_ret_int8 == -1); + assert_eq(_ret_int8, -1); #ifdef ZENOH_PICO _ret_int8 = zp_keyexpr_equals_null_terminated("demo/example/**", "demo/example"); - assert(_ret_int8 == -1); + assert_eq(_ret_int8, -1); #endif sleep(SLEEP); @@ -155,12 +165,12 @@ int main(int argc, char **argv) { assert(_ret_int8 < 0); #endif _ret_int8 = z_keyexpr_canonize(keyexpr_str, &keyexpr_len); - assert(_ret_int8 == 0); - assert(strlen("demo/example/**/*") == keyexpr_len); + assert_eq(_ret_int8, 0); + assert_eq(strlen("demo/example/**/*"), keyexpr_len); #ifdef ZENOH_PICO _ret_int8 = zp_keyexpr_canonize_null_terminated(keyexpr_str); - assert(_ret_int8 == 0); - assert(strlen("demo/example/**/*") == keyexpr_len); + assert_eq(_ret_int8, 0); + assert_eq(strlen("demo/example/**/*"), keyexpr_len); #endif printf("Ok\n"); @@ -174,10 +184,10 @@ int main(int argc, char **argv) { assert(z_check(_ret_config)); #ifdef ZENOH_PICO _ret_int8 = zp_config_insert(z_loan(_ret_config), Z_CONFIG_PEER_KEY, z_string_make(argv[1])); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); const char *_ret_cstr = zp_config_get(z_loan(_ret_config), Z_CONFIG_PEER_KEY); - assert(strlen(_ret_cstr) == strlen(argv[1])); - assert(strncmp(_ret_cstr, argv[1], strlen(_ret_cstr)) == 0); + assert_eq(strlen(_ret_cstr), strlen(argv[1])); + assert_eq(strncmp(_ret_cstr, argv[1], strlen(_ret_cstr)), 0); #endif z_owned_scouting_config_t _ret_sconfig = z_scouting_config_default(); @@ -185,10 +195,10 @@ int main(int argc, char **argv) { #ifdef ZENOH_PICO _ret_int8 = zp_scouting_config_insert(z_loan(_ret_sconfig), Z_CONFIG_SCOUTING_TIMEOUT_KEY, z_string_make(SCOUTING_TIMEOUT)); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); _ret_cstr = zp_scouting_config_get(z_loan(_ret_sconfig), Z_CONFIG_SCOUTING_TIMEOUT_KEY); - assert(strlen(_ret_cstr) == strlen(SCOUTING_TIMEOUT)); - assert(strncmp(_ret_cstr, SCOUTING_TIMEOUT, strlen(_ret_cstr)) == 0); + assert_eq(strlen(_ret_cstr), strlen(SCOUTING_TIMEOUT)); + assert_eq(strncmp(_ret_cstr, SCOUTING_TIMEOUT, strlen(_ret_cstr)), 0); #endif z_drop(z_move(_ret_sconfig)); @@ -199,7 +209,7 @@ int main(int argc, char **argv) { _ret_sconfig = z_scouting_config_from(z_loan(_ret_config)); z_owned_closure_hello_t _ret_closure_hello = z_closure(hello_handler, NULL, NULL); _ret_int8 = z_scout(z_move(_ret_sconfig), z_move(_ret_closure_hello)); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); assert(hellos >= 1); uint32_t _scouting_timeout = strtoul(SCOUTING_TIMEOUT, NULL, 10); @@ -218,15 +228,15 @@ int main(int argc, char **argv) { z_owned_closure_zid_t _ret_closure_zid = z_closure(zid_handler, NULL, NULL); _ret_int8 = z_info_peers_zid(z_loan(s1), z_move(_ret_closure_zid)); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); sleep(SLEEP); - assert(zids == 0); + assert_eq(zids, 0); _ret_int8 = z_info_routers_zid(z_loan(s1), z_move(_ret_closure_zid)); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); sleep(SLEEP); - assert(zids == 1); + assert_eq(zids, 1); #ifdef ZENOH_PICO zp_task_read_options_t _ret_read_opt = zp_task_read_options_default(); @@ -240,10 +250,10 @@ int main(int argc, char **argv) { _ret_config = z_config_default(); #ifdef ZENOH_PICO _ret_int8 = zp_config_insert(z_loan(_ret_config), Z_CONFIG_PEER_KEY, z_string_make(argv[1])); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); _ret_cstr = zp_config_get(z_loan(_ret_config), Z_CONFIG_PEER_KEY); - assert(strlen(_ret_cstr) == strlen(argv[1])); - assert(strncmp(_ret_cstr, argv[1], strlen(_ret_cstr)) == 0); + assert_eq(strlen(_ret_cstr), strlen(argv[1])); + assert_eq(strncmp(_ret_cstr, argv[1], strlen(_ret_cstr)), 0); #endif z_owned_session_t s2 = z_open(z_move(_ret_config)); @@ -288,30 +298,30 @@ int main(int argc, char **argv) { _ret_encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); _ret_put_opt.encoding = _ret_encoding; _ret_int8 = z_put(z_loan(s1), z_loan(_ret_expr), (const uint8_t *)value, strlen(value), &_ret_put_opt); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); printf("Ok\n"); sleep(SLEEP); - assert(datas == 1); + assert_eq(datas, 1); printf("Session delete..."); z_delete_options_t _ret_delete_opt = z_delete_options_default(); _ret_int8 = z_delete(z_loan(s1), z_loan(_ret_expr), &_ret_delete_opt); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); printf("Ok\n"); sleep(SLEEP); - assert(datas == 2); + assert_eq(datas, 2); printf("Undeclaring Keyexpr..."); _ret_int8 = z_undeclare_keyexpr(z_loan(s1), z_move(_ret_expr)); printf(" %02x\n", _ret_int8); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); assert(!z_check(_ret_expr)); printf("Ok\n"); _ret_int8 = z_undeclare_subscriber(z_move(_ret_sub)); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); printf("Declaring Pull Subscriber..."); z_owned_closure_sample_t _ret_closure_sample2 = z_closure(data_handler, NULL, &ls1); @@ -333,44 +343,44 @@ int main(int argc, char **argv) { printf("Publisher Put..."); z_publisher_put_options_t _ret_pput_opt = z_publisher_put_options_default(); _ret_int8 = z_publisher_put(z_loan(_ret_pub), (const uint8_t *)value, strlen(value), &_ret_pput_opt); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); printf("Ok\n"); sleep(SLEEP); printf("Pull Subscriber Pulling data..."); _ret_int8 = z_subscriber_pull(z_loan(_ret_psub)); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); printf("Ok\n"); sleep(SLEEP); - assert(datas == 3); + assert_eq(datas, 3); printf("Publisher Delete..."); z_publisher_delete_options_t _ret_pdelete_opt = z_publisher_delete_options_default(); _ret_int8 = z_publisher_delete(z_loan(_ret_pub), &_ret_pdelete_opt); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); printf("Ok\n"); sleep(SLEEP); printf("Pull Subscriber Pulling data..."); _ret_int8 = z_subscriber_pull(z_loan(_ret_psub)); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); printf("Ok\n"); sleep(SLEEP); - assert(datas == 4); + assert_eq(datas, 4); printf("Undeclaring Publisher..."); _ret_int8 = z_undeclare_publisher(z_move(_ret_pub)); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); assert(!z_check(_ret_pub)); printf("Ok\n"); printf("Undeclaring Pull Subscriber..."); _ret_int8 = z_undeclare_pull_subscriber(z_move(_ret_psub)); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); printf("Ok\n"); sleep(SLEEP); @@ -404,16 +414,16 @@ int main(int argc, char **argv) { printf("Testing Get..."); _ret_int8 = z_get(z_loan(s2), z_keyexpr(s1_res), "", z_move(_ret_closure_reply), &_ret_get_opt); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); printf("Ok\n"); sleep(SLEEP); - assert(queries == 1); - assert(replies == 1); + assert_eq(queries, 1); + assert_eq(replies, 1); printf("Undeclaring Queryable..."); _ret_int8 = z_undeclare_queryable(z_move(qle)); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); printf("Ok\n"); #ifdef ZENOH_PICO @@ -423,14 +433,14 @@ int main(int argc, char **argv) { printf("Close sessions..."); _ret_int8 = z_close(z_move(s1)); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); #ifdef ZENOH_PICO zp_stop_read_task(z_loan(s2)); zp_stop_lease_task(z_loan(s2)); #endif _ret_int8 = z_close(z_move(s2)); - assert(_ret_int8 == 0); + assert_eq(_ret_int8, 0); printf("Ok\n"); sleep(SLEEP * 5); diff --git a/zenohpico.pc b/zenohpico.pc index 3cb366494..ff26b6d65 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.10.20230824dev +Version: 0.10.20230825dev Cflags: -I${prefix}/ Libs: -L${prefix}/ -lzenohpico From bcc6563fb3743c69fe6c2cf2e020b1e9bb290b19 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 25 Aug 2023 16:56:17 +0200 Subject: [PATCH 166/181] fix get_resource_by_keyexpr --- src/session/resource.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/session/resource.c b/src/session/resource.c index e7c7f4659..d997b5b4b 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -52,8 +52,6 @@ _z_resource_t *__z_get_resource_by_id(_z_resource_list_t *rl, uint16_t mapping, _z_resource_list_t *xs = rl; while (xs != NULL) { _z_resource_t *r = _z_resource_list_head(xs); - _Z_DEBUG("Checking ressource %d on mapping 0x%x: %d %s\n", r->_id, _z_keyexpr_mapping_id(&r->_key), r->_key._id, - r->_key._suffix); if (r->_id == id && _z_keyexpr_mapping_id(&r->_key) == mapping) { ret = r; break; @@ -190,6 +188,9 @@ _z_resource_t *_z_get_resource_by_id(_z_session_t *zn, uint16_t mapping, _z_zint } _z_resource_t *_z_get_resource_by_key(_z_session_t *zn, const _z_keyexpr_t *keyexpr) { + if (keyexpr->_suffix == NULL) { + return _z_get_resource_by_id(zn, _z_keyexpr_mapping_id(keyexpr), keyexpr->_id); + } #if Z_MULTI_THREAD == 1 _z_mutex_lock(&zn->_mutex_inner); #endif // Z_MULTI_THREAD == 1 @@ -252,8 +253,6 @@ int16_t _z_register_resource(_z_session_t *zn, _z_keyexpr_t key, uint16_t id, ui zn->_remote_resources = _z_resource_list_push(zn->_remote_resources, res); } } - } else { - ret = Z_RESOURCE_ID_NONE; } #if Z_MULTI_THREAD == 1 @@ -274,8 +273,6 @@ void _z_unregister_resource(_z_session_t *zn, uint16_t id, uint16_t mapping) { _z_resource_list_t *parent = *parent_mut; while (parent != NULL) { _z_resource_t *head = _z_resource_list_head(parent); - _Z_DEBUG("unreg check: id %d, mapping: %d, refcount: %d\n", head->_id, _z_keyexpr_mapping_id(&head->_key), - head->_refcount); if (head && head->_id == id && _z_keyexpr_mapping_id(&head->_key) == mapping) { head->_refcount--; if (head->_refcount == 0) { From 385d57e9c48f67b2d5ed5432b6991f978f2e3bdb Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 25 Aug 2023 16:57:50 +0200 Subject: [PATCH 167/181] embedded are still annoying about formating --- src/transport/unicast/link/rx.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index c8f48c364..aabb50d74 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -117,7 +117,6 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans // Handle all the zenoh message, one by one size_t len = _z_vec_len(&t_msg->_body._frame._messages); - _Z_INFO("Z_FRAME contained %ld messages\n", len); for (size_t i = 0; i < len; i++) { _z_handle_zenoh_message(ztu->_session, (_z_zenoh_message_t *)_z_vec_get(&t_msg->_body._frame._messages, i), @@ -128,8 +127,6 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans } case _Z_MID_T_FRAGMENT: { - _Z_INFO("Received Z_FRAGMENT message, len: %ld\n", t_msg->_body._fragment._payload.len); - _z_wbuf_t *dbuf = _Z_HAS_FLAG(t_msg->_header, _Z_FLAG_T_FRAGMENT_R) ? &ztu->_dbuf_reliable : &ztu->_dbuf_best_effort; // Select the right defragmentation buffer From ee98b25e94603db460b12e4ca47b59583999f0d2 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 25 Aug 2023 17:02:06 +0200 Subject: [PATCH 168/181] fix builds of alignment test for windows --- tests/z_api_alignment_test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 96b4cb3e6..a11d3095c 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -31,8 +31,8 @@ #define assert_eq(x, y) \ { \ - int8_t l = x; \ - int8_t r = y; \ + int l = (int)x; \ + int r = (int)y; \ if (l != r) { \ printf("assert_eq failed: l=%d, r=%d\n", l, r); \ assert(false); \ From 4ebace52d1cddde3e3924499de2fd72986442fdc Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 25 Aug 2023 17:58:05 +0200 Subject: [PATCH 169/181] integration tests should pass now --- src/api/api.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/api/api.c b/src/api/api.c index 261305be1..08996bf76 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -940,13 +940,14 @@ z_query_reply_options_t z_query_reply_options_default(void) { int8_t z_query_reply(const z_query_t *query, const z_keyexpr_t keyexpr, const uint8_t *payload, size_t payload_len, const z_query_reply_options_t *options) { + z_query_reply_options_t opts = options == NULL ? z_query_reply_options_default() : *options; _z_value_t value = {.payload = { .start = payload, ._is_alloc = false, .len = payload_len, }, - .encoding = {.prefix = options->encoding.prefix, .suffix = options->encoding.suffix}}; + .encoding = {.prefix = opts.encoding.prefix, .suffix = opts.encoding.suffix}}; return _z_send_reply(query, keyexpr, value); } From dbc382d36ed6f4da0fc0549ffab8b33f9ef8d6f1 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 25 Aug 2023 18:21:26 +0200 Subject: [PATCH 170/181] remove realpath from multicast test --- .github/workflows/multicast.yaml | 34 ++++++++++++++++---------------- tests/multicast.sh | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.github/workflows/multicast.yaml b/.github/workflows/multicast.yaml index 3c3fd8179..63bc774a1 100644 --- a/.github/workflows/multicast.yaml +++ b/.github/workflows/multicast.yaml @@ -15,34 +15,34 @@ name: multicast on: push: - branches: [ '**' ] + branches: ["**"] pull_request: - branches: [ '**' ] + branches: ["**"] schedule: - - cron: '0 6 * * 1-5' + - cron: "0 6 * * 1-5" jobs: build: - name: Build on macOS-latest runs-on: macOS-latest strategy: fail-fast: false matrix: - os: [ macOS-latest ] + os: [macOS-latest] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v2 - - name: Compile debug - run: make all - env: - BUILD_TYPE: Debug - BUILD_TESTING: OFF - BUILD_MULTICAST: ON - BUILD_INTEGRATION: OFF + - name: Compile debug + run: make all + env: + BUILD_TYPE: Debug + BUILD_TESTING: OFF + BUILD_MULTICAST: ON + BUILD_INTEGRATION: OFF + ZENOH_BRANCH: new-protocol - - name: Test debug - run: make test - env: - ARGS: -V + - name: Test debug + run: make test + env: + ARGS: -V diff --git a/tests/multicast.sh b/tests/multicast.sh index e6094f52c..d03dcb1dc 100644 --- a/tests/multicast.sh +++ b/tests/multicast.sh @@ -14,7 +14,7 @@ # TESTBIN="$1" -TESTDIR=$(realpath $(dirname "$0")) +TESTDIR=$(dirname "$0") if [ "$OSTYPE" = "msys" ]; then TESTBIN="$TESTDIR/Debug/$TESTBIN.exe" From ef6ef96dff30f1cfbe2e9e31af23080dbdc07217 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 25 Aug 2023 18:24:49 +0200 Subject: [PATCH 171/181] remove realpath from test scripts, as macos doesn't support it --- tests/api.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/api.sh b/tests/api.sh index a4bf36acc..7be9183b3 100644 --- a/tests/api.sh +++ b/tests/api.sh @@ -14,7 +14,7 @@ # TESTBIN="$1" -TESTDIR=$(realpath $(dirname "$0")) +TESTDIR=$(dirname "$0") if [ "$OSTYPE" = "msys" ]; then TESTBIN="$TESTDIR/Debug/$TESTBIN.exe" From 15e49607115d5a592027a66b93079b8ca90b3522 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Fri, 25 Aug 2023 18:38:29 +0200 Subject: [PATCH 172/181] forgot one of the realpaths, and of course, that broke the macos CI --- tests/routed.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/routed.sh b/tests/routed.sh index b1ab0d6d2..72659e075 100644 --- a/tests/routed.sh +++ b/tests/routed.sh @@ -14,7 +14,7 @@ # TESTBIN="$1" -TESTDIR=$(realpath $(dirname "$0")) +TESTDIR=$(dirname "$0") if [ "$OSTYPE" = "msys" ]; then TESTBIN="$TESTDIR/Debug/$TESTBIN.exe" From a4484bfc34e25b76656e4fe0dd820dc800a9f13d Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 28 Aug 2023 15:39:40 +0200 Subject: [PATCH 173/181] address carlos's comments --- include/zenoh-pico/net/query.h | 4 ++-- include/zenoh-pico/net/subscribe.h | 2 +- include/zenoh-pico/session/utils.h | 2 +- src/link/link.c | 7 ++++++- src/net/primitives.c | 16 ++++++++-------- src/session/queryable.c | 4 ++-- src/session/rx.c | 2 +- src/transport/multicast/link/rx.c | 4 ++-- src/transport/unicast/link/rx.c | 8 ++++---- tests/z_peer_multicast_test.c | 4 ++-- zenohpico.pc | 2 +- 11 files changed, 30 insertions(+), 25 deletions(-) diff --git a/include/zenoh-pico/net/query.h b/include/zenoh-pico/net/query.h index bda52795f..cb59f6dd5 100644 --- a/include/zenoh-pico/net/query.h +++ b/include/zenoh-pico/net/query.h @@ -25,7 +25,7 @@ typedef struct { _z_value_t _value; _z_keyexpr_t _key; - uint32_t _rid; + uint32_t _request_id; void *_zn; // FIXME: _z_session_t *zn; char *_parameters; _Bool _anyke; @@ -35,7 +35,7 @@ typedef struct { * Return type when declaring a queryable. */ typedef struct { - uint32_t _id; + uint32_t _entity_id; void *_zn; // FIXME: _z_session_t *zn; } _z_queryable_t; diff --git a/include/zenoh-pico/net/subscribe.h b/include/zenoh-pico/net/subscribe.h index 4e350148b..2a8a53c1b 100644 --- a/include/zenoh-pico/net/subscribe.h +++ b/include/zenoh-pico/net/subscribe.h @@ -24,7 +24,7 @@ * Return type when declaring a subscriber. */ typedef struct { - uint32_t _id; + uint32_t _entity_id; _z_session_t *_zn; } _z_subscriber_t; diff --git a/include/zenoh-pico/session/utils.h b/include/zenoh-pico/session/utils.h index 497e0a015..39cae73d3 100644 --- a/include/zenoh-pico/session/utils.h +++ b/include/zenoh-pico/session/utils.h @@ -31,7 +31,7 @@ int8_t _z_session_close(_z_session_t *zn, uint8_t reason); void _z_session_clear(_z_session_t *zn); void _z_session_free(_z_session_t **zn); -int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *z_msg, uint16_t local_peer_id); +int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *z_msg, uint16_t local_peer_id); int8_t _z_send_n_msg(_z_session_t *zn, _z_network_message_t *n_msg, z_reliability_t reliability, z_congestion_control_t cong_ctrl); diff --git a/src/link/link.c b/src/link/link.c index 3705ca1a4..5c409e353 100644 --- a/src/link/link.c +++ b/src/link/link.c @@ -148,7 +148,8 @@ size_t _z_link_recv_exact_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, size_t len int8_t _z_link_send_wbuf(const _z_link_t *link, const _z_wbuf_t *wbf) { int8_t ret = _Z_RES_OK; - for (size_t i = 0; (i < _z_wbuf_len_iosli(wbf)); i++) { + _Bool link_is_streamed = _Z_LINK_IS_STREAMED(link->_capabilities); + for (size_t i = 0; (i < _z_wbuf_len_iosli(wbf)) && (ret == _Z_RES_OK); i++) { _z_bytes_t bs = _z_iosli_to_bytes(_z_wbuf_get_iosli(wbf, i)); size_t n = bs.len; do { @@ -157,6 +158,10 @@ int8_t _z_link_send_wbuf(const _z_link_t *link, const _z_wbuf_t *wbf) { ret = _Z_ERR_TRANSPORT_TX_FAILED; break; } + if (link_is_streamed && wb != n) { + ret = _Z_ERR_TRANSPORT_TX_FAILED; + break; + } n = n - wb; bs.start = bs.start + (bs.len - n); } while (n > (size_t)0); diff --git a/src/net/primitives.c b/src/net/primitives.c index 6397dd588..374e52b0d 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -141,7 +141,7 @@ _z_subscriber_t *_z_declare_subscriber(_z_session_t *zn, _z_keyexpr_t keyexpr, _ _z_subscriber_t *ret = (_z_subscriber_t *)z_malloc(sizeof(_z_subscriber_t)); if (ret != NULL) { ret->_zn = zn; - ret->_id = s._id; + ret->_entity_id = s._id; _z_subscription_sptr_t *sp_s = _z_register_subscription( zn, _Z_RESOURCE_IS_LOCAL, &s); // This a pointer to the entry stored at session-level. @@ -171,10 +171,10 @@ int8_t _z_undeclare_subscriber(_z_subscriber_t *sub) { int8_t ret = _Z_ERR_GENERIC; if (sub != NULL) { - _z_subscription_sptr_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_id); + _z_subscription_sptr_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); if (s != NULL) { // Build the declare message to send on the wire - _z_declaration_t declaration = _z_make_undecl_subscriber(sub->_id, &s->ptr->_key); + _z_declaration_t declaration = _z_make_undecl_subscriber(sub->_entity_id, &s->ptr->_key); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); if (_z_send_n_msg(sub->_zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { // Only if message is successfully send, local subscription state can be removed @@ -207,7 +207,7 @@ _z_queryable_t *_z_declare_queryable(_z_session_t *zn, _z_keyexpr_t keyexpr, _Bo _z_queryable_t *ret = (_z_queryable_t *)z_malloc(sizeof(_z_queryable_t)); if (ret != NULL) { ret->_zn = zn; - ret->_id = q._id; + ret->_entity_id = q._id; _z_questionable_sptr_t *sp_q = _z_register_questionable(zn, &q); // This a pointer to the entry stored at session-level. @@ -237,10 +237,10 @@ int8_t _z_undeclare_queryable(_z_queryable_t *qle) { int8_t ret = _Z_RES_OK; if (qle != NULL) { - _z_questionable_sptr_t *q = _z_get_questionable_by_id(qle->_zn, qle->_id); + _z_questionable_sptr_t *q = _z_get_questionable_by_id(qle->_zn, qle->_entity_id); if (q != NULL) { // Build the declare message to send on the wire - _z_declaration_t declaration = _z_make_undecl_queryable(qle->_id, &q->ptr->_key); + _z_declaration_t declaration = _z_make_undecl_queryable(qle->_entity_id, &q->ptr->_key); _z_network_message_t n_msg = _z_n_msg_make_declare(declaration); if (_z_send_n_msg(qle->_zn, &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) == _Z_RES_OK) { // Only if message is successfully send, local queryable state can be removed @@ -283,7 +283,7 @@ int8_t _z_send_reply(const z_query_t *query, _z_keyexpr_t keyexpr, const _z_valu ._tag = _Z_N_RESPONSE, ._body._response = { - ._request_id = query->_rid, + ._request_id = query->_request_id, ._key = ke, ._ext_responder = {._zid = zid, ._eid = 0}, ._ext_qos = _Z_N_QOS_DEFAULT, @@ -398,7 +398,7 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, int8_t _z_subscriber_pull(const _z_subscriber_t *sub) { int8_t ret = _Z_RES_OK; - _z_subscription_sptr_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_id); + _z_subscription_sptr_t *s = _z_get_subscription_by_id(sub->_zn, _Z_RESOURCE_IS_LOCAL, sub->_entity_id); if (s != NULL) { _z_zint_t pull_id = _z_get_pull_id(sub->_zn); _z_zenoh_message_t z_msg = _z_msg_make_pull(_z_keyexpr_alias(s->ptr->_key), pull_id); diff --git a/src/session/queryable.c b/src/session/queryable.c index 078ecdab2..d18c5705e 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -156,7 +156,7 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons // Build the query z_query_t q; q._zn = zn; - q._rid = qid; + q._request_id = qid; q._key = key; #if defined(__STDC_NO_VLA__) || ((__STDC_VERSION__ < 201000L) && (defined(_WIN32) || defined(WIN32))) char *params = z_malloc(query->_parameters.len + 1); @@ -184,7 +184,7 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *query, cons // Send the final reply // Create the final reply - _z_zenoh_message_t z_msg = _z_n_msg_make_response_final(q._rid); + _z_zenoh_message_t z_msg = _z_n_msg_make_response_final(q._request_id); if (_z_send_n_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { ret = _Z_ERR_TRANSPORT_TX_FAILED; } diff --git a/src/session/rx.c b/src/session/rx.c index fad9e46d9..082fe21ee 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -33,7 +33,7 @@ #include "zenoh-pico/utils/logging.h" /*------------------ Handle message ------------------*/ -int8_t _z_handle_zenoh_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16_t local_peer_id) { +int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint16_t local_peer_id) { int8_t ret = _Z_RES_OK; switch (msg->_tag) { diff --git a/src/transport/multicast/link/rx.c b/src/transport/multicast/link/rx.c index 17bc9fd9b..b5d50e030 100644 --- a/src/transport/multicast/link/rx.c +++ b/src/transport/multicast/link/rx.c @@ -154,7 +154,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t for (size_t i = 0; i < len; i++) { _z_network_message_t *zm = _z_network_message_vec_get(&t_msg->_body._frame._messages, i); _z_msg_fix_mapping(zm, mapping); - _z_handle_zenoh_message(ztm->_session, zm, mapping); + _z_handle_network_message(ztm->_session, zm, mapping); } break; @@ -195,7 +195,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t if (ret == _Z_RES_OK) { uint16_t mapping = entry->_peer_id; _z_msg_fix_mapping(&zm, mapping); - _z_handle_zenoh_message(ztm->_session, &zm, mapping); + _z_handle_network_message(ztm->_session, &zm, mapping); _z_msg_clear(&zm); // Clear must be explicitly called for fragmented zenoh messages. Non-fragmented // zenoh messages are released when their transport message is released. } diff --git a/src/transport/unicast/link/rx.c b/src/transport/unicast/link/rx.c index aabb50d74..bac48d82e 100644 --- a/src/transport/unicast/link/rx.c +++ b/src/transport/unicast/link/rx.c @@ -118,9 +118,9 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans // Handle all the zenoh message, one by one size_t len = _z_vec_len(&t_msg->_body._frame._messages); for (size_t i = 0; i < len; i++) { - _z_handle_zenoh_message(ztu->_session, - (_z_zenoh_message_t *)_z_vec_get(&t_msg->_body._frame._messages, i), - _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); + _z_handle_network_message(ztu->_session, + (_z_zenoh_message_t *)_z_vec_get(&t_msg->_body._frame._messages, i), + _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); } break; @@ -153,7 +153,7 @@ int8_t _z_unicast_handle_transport_message(_z_transport_unicast_t *ztu, _z_trans _z_zenoh_message_t zm; int8_t ret = _z_network_message_decode(&zm, &zbf); if (ret == _Z_RES_OK) { - _z_handle_zenoh_message(ztu->_session, &zm, _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE); + _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 // zenoh messages are released when their transport message is released. } else { diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 2933d5bc4..6d803b67b 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -108,7 +108,7 @@ int main(int argc, char **argv) { z_owned_subscriber_t *sub = (z_owned_subscriber_t *)z_malloc(sizeof(z_owned_subscriber_t)); *sub = z_declare_subscriber(z_loan(s2), z_keyexpr(s1_res), &callback, NULL); assert(z_check(*sub)); - printf("Declared subscription on session 2: %ju %zu %s\n", (uintmax_t)z_subscriber_loan(sub)._val->_id, + printf("Declared subscription on session 2: %ju %zu %s\n", (uintmax_t)z_subscriber_loan(sub)._val->_entity_id, (z_zint_t)0, s1_res); subs2 = _z_list_push(subs2, sub); } @@ -150,7 +150,7 @@ int main(int argc, char **argv) { // Undeclare subscribers and queryables on second session while (subs2) { z_owned_subscriber_t *sub = _z_list_head(subs2); - printf("Undeclared subscriber on session 2: %ju\n", (uintmax_t)z_subscriber_loan(sub)._val->_id); + printf("Undeclared subscriber on session 2: %ju\n", (uintmax_t)z_subscriber_loan(sub)._val->_entity_id); z_undeclare_subscriber(z_move(*sub)); subs2 = _z_list_pop(subs2, _z_noop_elem_free, NULL); } diff --git a/zenohpico.pc b/zenohpico.pc index ff26b6d65..2827509d3 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.10.20230825dev +Version: 0.10.20230828dev Cflags: -I${prefix}/ Libs: -L${prefix}/ -lzenohpico From 499219d0113eb480dbceb20eeaa435e04df5fbb0 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 28 Aug 2023 15:59:58 +0200 Subject: [PATCH 174/181] refactoring field name failed for tests --- tests/z_client_test.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 510571471..5c4b1fd1d 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -169,7 +169,7 @@ int main(int argc, char **argv) { z_owned_subscriber_t *sub = (z_owned_subscriber_t *)z_malloc(sizeof(z_owned_subscriber_t)); *sub = z_declare_subscriber(z_loan(s2), z_loan(rids2[i]), &callback, NULL); assert(z_check(*sub)); - printf("Declared subscription on session 2: %ju %u %s\n", (uintmax_t)z_subscriber_loan(sub)._val->_id, + printf("Declared subscription on session 2: %ju %u %s\n", (uintmax_t)z_subscriber_loan(sub)._val->_entity_id, z_loan(rids2[i])._id, ""); subs2 = _z_list_push(subs2, sub); } @@ -182,7 +182,8 @@ int main(int argc, char **argv) { z_owned_queryable_t *qle = (z_owned_queryable_t *)z_malloc(sizeof(z_owned_queryable_t)); *qle = z_declare_queryable(z_loan(s2), z_keyexpr(s1_res), &callback, NULL); assert(z_check(*qle)); - printf("Declared queryable on session 2: %ju %zu %s\n", (uintmax_t)qle->_value->_id, (z_zint_t)0, s1_res); + printf("Declared queryable on session 2: %ju %zu %s\n", (uintmax_t)qle->_value->_entity_id, (z_zint_t)0, + s1_res); qles2 = _z_list_push(qles2, qle); } @@ -283,7 +284,7 @@ int main(int argc, char **argv) { // Undeclare subscribers and queryables on second session while (subs2) { z_owned_subscriber_t *sub = _z_list_head(subs2); - printf("Undeclared subscriber on session 2: %ju\n", (uintmax_t)sub->_value->_id); + printf("Undeclared subscriber on session 2: %ju\n", (uintmax_t)sub->_value->_entity_id); z_undeclare_subscriber(z_move(*sub)); subs2 = _z_list_pop(subs2, _z_noop_elem_free, NULL); } @@ -292,7 +293,7 @@ int main(int argc, char **argv) { while (qles2) { z_owned_queryable_t *qle = _z_list_head(qles2); - printf("Undeclared queryable on session 2: %ju\n", (uintmax_t)qle->_value->_id); + printf("Undeclared queryable on session 2: %ju\n", (uintmax_t)qle->_value->_entity_id); z_undeclare_queryable(z_move(*qle)); qles2 = _z_list_pop(qles2, _z_noop_elem_free, NULL); } From c5ab505484d26df8684dab27e2495a3fceafa4b9 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Tue, 5 Sep 2023 16:22:54 +0200 Subject: [PATCH 175/181] fix query body extension --- src/protocol/codec/message.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 33de0a169..8a529400d 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -404,7 +404,7 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *msg) { _z_bytes_encode_len(&msg->_ext_value.payload))); _Z_RETURN_IF_ERR(_z_encoding_prefix_encode(wbf, msg->_ext_value.encoding.prefix)); _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_ext_value.encoding.suffix)); - _Z_RETURN_IF_ERR(_z_bytes_encode(wbf, &msg->_ext_value.payload)); + _Z_RETURN_IF_ERR(_z_bytes_val_encode(wbf, &msg->_ext_value.payload)); } if (required_exts.consolidation) { uint8_t extheader = _Z_MSG_EXT_ENC_ZINT | _Z_MSG_EXT_FLAG_M | 0x02; @@ -440,7 +440,8 @@ int8_t _z_query_decode_extensions(_z_msg_ext_t *extension, void *ctx) { _z_zbuf_t zbf = _z_zbytes_as_zbuf(extension->_body._zbuf._val); ret = _z_encoding_prefix_decode(&msg->_ext_value.encoding.prefix, &zbf); ret |= _z_bytes_decode(&msg->_ext_value.encoding.suffix, &zbf); - ret |= _z_bytes_decode(&msg->_ext_value.payload, &zbf); + _z_bytes_t bytes = _z_bytes_wrap((uint8_t *)_z_zbuf_start(&zbf), _z_zbuf_len(&zbf)); + _z_bytes_copy(&msg->_ext_value.payload, &bytes); break; } default: From 464025bfd3154c942f8f2a96b24406722a027bd5 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 6 Sep 2023 11:15:34 +0200 Subject: [PATCH 176/181] expose z_publiser/subcriber_keyexpr --- include/zenoh-pico/api/primitives.h | 12 ++++++++++++ src/api/api.c | 28 ++++++++++++++++++++++++++++ zenohpico.pc | 2 +- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 4ac4ebef9..9b0307df3 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -22,6 +22,10 @@ #include "zenoh-pico/net/query.h" #include "zenoh-pico/net/session.h" #include "zenoh-pico/net/subscribe.h" +#include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/keyexpr.h" +#include "zenoh-pico/session/session.h" +#include "zenoh-pico/system/platform.h" #ifdef __cplusplus extern "C" { @@ -944,6 +948,8 @@ z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, con */ int8_t z_undeclare_publisher(z_owned_publisher_t *pub); +z_owned_keyexpr_t z_publisher_keyexpr(z_publisher_t publisher); + /** * Constructs the default values for the put operation via a publisher entity. * @@ -1036,6 +1042,12 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z */ int8_t z_undeclare_subscriber(z_owned_subscriber_t *sub); +/** + * @brief + * + */ +z_owned_keyexpr_t z_subscriber_keyexpr(z_subscriber_t sub); + /** * Constructs the default values for the pull subscriber entity. * diff --git a/src/api/api.c b/src/api/api.c index 08996bf76..c52f95a93 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -1030,3 +1030,31 @@ int8_t zp_send_join(z_session_t zs, const zp_send_join_options_t *options) { (void)(options); return _zp_send_join(zs._val); } +z_owned_keyexpr_t z_publisher_keyexpr(z_publisher_t publisher) { + z_owned_keyexpr_t ret = {._value = z_malloc(sizeof(_z_keyexpr_t))}; + if (ret._value != NULL && publisher._val != NULL) { + *ret._value = _z_keyexpr_duplicate(publisher._val->_key); + } + return ret; +} +z_owned_keyexpr_t z_subscriber_keyexpr(z_subscriber_t sub) { + z_owned_keyexpr_t ret = z_keyexpr_null(); + __auto_type lookup = sub._val->_entity_id; + if (sub._val != NULL) { + _z_subscription_sptr_list_t *tail = sub._val->_zn->_local_subscriptions; + while (tail != NULL && !z_keyexpr_check(&ret)) { + _z_subscription_sptr_t *head = _z_subscription_sptr_list_head(tail); + if (head->ptr->_id == lookup) { + _z_keyexpr_t key = _z_keyexpr_duplicate(head->ptr->_key); + ret = (z_owned_keyexpr_t){._value = z_malloc(sizeof(_z_keyexpr_t))}; + if (ret._value != NULL) { + *ret._value = key; + } else { + _z_keyexpr_clear(&key); + } + } + tail = _z_subscription_sptr_list_tail(tail); + } + } + return ret; +} diff --git a/zenohpico.pc b/zenohpico.pc index 2827509d3..6b54a5398 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.10.20230828dev +Version: 0.10.20230906dev Cflags: -I${prefix}/ Libs: -L${prefix}/ -lzenohpico From faa58050923ec001385caed801f14bf48c951d15 Mon Sep 17 00:00:00 2001 From: OlivierHecart Date: Fri, 8 Sep 2023 17:01:58 +0200 Subject: [PATCH 177/181] CI points to zenoh master --- .github/workflows/integration.yaml | 2 +- .github/workflows/multicast.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml index 888b9d72e..9818f6f94 100644 --- a/.github/workflows/integration.yaml +++ b/.github/workflows/integration.yaml @@ -56,4 +56,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: new-protocol + ZENOH_BRANCH: master diff --git a/.github/workflows/multicast.yaml b/.github/workflows/multicast.yaml index 63bc774a1..1d7af5533 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: new-protocol + ZENOH_BRANCH: master - name: Test debug run: make test From e3a86d9605b24e892fc0b82c44f2e42a984f39d7 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Mon, 11 Sep 2023 10:22:19 +0200 Subject: [PATCH 178/181] fix remaining __auto_type --- src/api/api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/api.c b/src/api/api.c index c52f95a93..db8d725e9 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -1039,7 +1039,7 @@ z_owned_keyexpr_t z_publisher_keyexpr(z_publisher_t publisher) { } z_owned_keyexpr_t z_subscriber_keyexpr(z_subscriber_t sub) { z_owned_keyexpr_t ret = z_keyexpr_null(); - __auto_type lookup = sub._val->_entity_id; + uint32_t lookup = sub._val->_entity_id; if (sub._val != NULL) { _z_subscription_sptr_list_t *tail = sub._val->_zn->_local_subscriptions; while (tail != NULL && !z_keyexpr_check(&ret)) { From bd245f93bc841135b3c2e7ada03e3c34b4ce3223 Mon Sep 17 00:00:00 2001 From: OlivierHecart Date: Mon, 11 Sep 2023 16:40:38 +0200 Subject: [PATCH 179/181] Fix codec bugs (#244) * Fix codec bugs * Ignore response final extensions * Make _z_n_qos_make macro even safer --- include/zenoh-pico/protocol/definitions/network.h | 2 +- src/protocol/codec/network.c | 9 +++++++-- zenohpico.pc | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index e439b276a..69226b6e0 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -63,7 +63,7 @@ typedef struct { } _z_n_qos_t; #define _z_n_qos_make(express, nodrop, priority) \ - (_z_n_qos_t) { ._val = ((express << 4) | (nodrop << 3) | priority) } + (_z_n_qos_t) { ._val = (((express) << 4) | ((nodrop) << 3) | (priority)) } #define _Z_N_QOS_DEFAULT _z_n_qos_make(0, 0, 5) // RESPONSE FINAL message flags: diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index 857e391aa..28abb51d1 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -39,17 +39,18 @@ int8_t _z_push_encode(_z_wbuf_t *wbf, const _z_n_msg_push_t *msg) { uint8_t header = _Z_MID_N_PUSH | (_z_keyexpr_is_local(&msg->_key) ? _Z_FLAG_N_REQUEST_M : 0); _Bool has_suffix = _z_keyexpr_has_suffix(msg->_key); + _Bool has_qos_ext = msg->_qos._val != _Z_N_QOS_DEFAULT._val; _Bool has_timestamp_ext = _z_timestamp_check(&msg->_timestamp); if (has_suffix) { header |= _Z_FLAG_N_REQUEST_N; } - if (has_timestamp_ext) { + if (has_qos_ext || has_timestamp_ext) { header |= _Z_FLAG_N_Z; } _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, header)); _Z_RETURN_IF_ERR(_z_keyexpr_encode(wbf, has_suffix, &msg->_key)); - if (msg->_qos._val != _Z_N_QOS_DEFAULT._val) { + if (has_qos_ext) { _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, _Z_MSG_EXT_ENC_ZINT | 0x01 | (has_timestamp_ext << 7))); _Z_RETURN_IF_ERR(_z_uint8_encode(wbf, msg->_qos._val)); } @@ -411,12 +412,16 @@ int8_t _z_response_final_encode(_z_wbuf_t *wbf, const _z_n_msg_response_final_t return ret; } + int8_t _z_response_final_decode(_z_n_msg_response_final_t *msg, _z_zbuf_t *zbf, uint8_t header) { (void)(header); *msg = (_z_n_msg_response_final_t){0}; int8_t ret = _Z_RES_OK; ret |= _z_zint_decode(&msg->_request_id, zbf); + if (_Z_HAS_FLAG(header, _Z_FLAG_Z_Z)) { + _Z_RETURN_IF_ERR(_z_msg_ext_skip_non_mandatories(zbf, 0x1a)); + } return ret; } diff --git a/zenohpico.pc b/zenohpico.pc index 6b54a5398..5617384c4 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.10.20230906dev +Version: 0.10.20230911dev Cflags: -I${prefix}/ Libs: -L${prefix}/ -lzenohpico From b6ead1f280ba2da19f31a9ff1c26263e103f5e9b Mon Sep 17 00:00:00 2001 From: Luca Cominardi Date: Tue, 12 Sep 2023 15:09:35 +0200 Subject: [PATCH 180/181] Fix #245 (#246) --- examples/arduino/z_get.ino | 8 ++++---- examples/arduino/z_pub.ino | 8 ++++---- examples/arduino/z_pull.ino | 8 ++++---- examples/arduino/z_queryable.ino | 8 ++++---- examples/arduino/z_sub.ino | 8 ++++---- examples/espidf/z_get.c | 8 ++++---- examples/espidf/z_pub.c | 8 ++++---- examples/espidf/z_pull.c | 8 ++++---- examples/espidf/z_queryable.c | 8 ++++---- examples/espidf/z_sub.c | 8 ++++---- examples/mbed/z_get.cpp | 8 ++++---- examples/mbed/z_pub.cpp | 8 ++++---- examples/mbed/z_pull.cpp | 8 ++++---- examples/mbed/z_queryable.cpp | 8 ++++---- examples/mbed/z_sub.cpp | 8 ++++---- examples/unix/c11/z_get.c | 2 +- examples/unix/c11/z_info.c | 2 +- examples/unix/c11/z_pub.c | 2 +- examples/unix/c11/z_pub_st.c | 2 +- examples/unix/c11/z_pull.c | 2 +- examples/unix/c11/z_put.c | 2 +- examples/unix/c11/z_queryable.c | 2 +- examples/unix/c11/z_sub.c | 2 +- examples/unix/c11/z_sub_st.c | 2 +- examples/unix/c99/z_get.c | 2 +- examples/unix/c99/z_info.c | 2 +- examples/unix/c99/z_pub.c | 2 +- examples/unix/c99/z_pub_st.c | 2 +- examples/unix/c99/z_pull.c | 2 +- examples/unix/c99/z_put.c | 2 +- examples/unix/c99/z_queryable.c | 2 +- examples/unix/c99/z_sub.c | 2 +- examples/unix/c99/z_sub_st.c | 2 +- examples/windows/z_get.c | 2 +- examples/windows/z_info.c | 2 +- examples/windows/z_pub.c | 2 +- examples/windows/z_pub_st.c | 2 +- examples/windows/z_pull.c | 2 +- examples/windows/z_put.c | 2 +- examples/windows/z_queryable.c | 2 +- examples/windows/z_sub.c | 2 +- examples/windows/z_sub_st.c | 2 +- examples/zephyr/z_get.c | 8 ++++---- examples/zephyr/z_pub.c | 8 ++++---- examples/zephyr/z_pull.c | 8 ++++---- examples/zephyr/z_queryable.c | 8 ++++---- examples/zephyr/z_sub.c | 8 ++++---- include/zenoh-pico/config.h | 4 ++-- src/net/config.c | 2 +- src/net/session.c | 4 ++-- tests/z_api_alignment_test.c | 8 ++++---- tests/z_client_test.c | 4 ++-- tests/z_peer_multicast_test.c | 4 ++-- 53 files changed, 120 insertions(+), 120 deletions(-) diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index 3cdba9b88..1f92eb3c2 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -23,10 +23,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -79,8 +79,8 @@ void setup() { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/arduino/z_pub.ino b/examples/arduino/z_pub.ino index c56bc0adc..81f3745b2 100644 --- a/examples/arduino/z_pub.ino +++ b/examples/arduino/z_pub.ino @@ -23,10 +23,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -56,8 +56,8 @@ void setup() { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/arduino/z_pull.ino b/examples/arduino/z_pull.ino index 90b6fdf02..5e2952d6c 100644 --- a/examples/arduino/z_pull.ino +++ b/examples/arduino/z_pull.ino @@ -23,10 +23,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -67,8 +67,8 @@ void setup() { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index 732f8c55b..fe1f87b65 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -23,10 +23,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -67,8 +67,8 @@ void setup() { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/arduino/z_sub.ino b/examples/arduino/z_sub.ino index 83719af1c..e9b829245 100644 --- a/examples/arduino/z_sub.ino +++ b/examples/arduino/z_sub.ino @@ -23,10 +23,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -65,8 +65,8 @@ void setup() { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index 891588acd..83f321351 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -37,10 +37,10 @@ static int s_retry_count = 0; #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -133,8 +133,8 @@ void app_main() { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/espidf/z_pub.c b/examples/espidf/z_pub.c index 10e1c9565..112429cbd 100644 --- a/examples/espidf/z_pub.c +++ b/examples/espidf/z_pub.c @@ -37,10 +37,10 @@ static int s_retry_count = 0; #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -120,8 +120,8 @@ void app_main() { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/espidf/z_pull.c b/examples/espidf/z_pull.c index 057f98ebe..976880a79 100644 --- a/examples/espidf/z_pull.c +++ b/examples/espidf/z_pull.c @@ -37,10 +37,10 @@ static int s_retry_count = 0; #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -126,8 +126,8 @@ void app_main() { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index 57e527c14..9f5eb9abe 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -37,10 +37,10 @@ static int s_retry_count = 0; #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -129,8 +129,8 @@ void app_main() { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/espidf/z_sub.c b/examples/espidf/z_sub.c index eb2127767..e09bad00d 100644 --- a/examples/espidf/z_sub.c +++ b/examples/espidf/z_sub.c @@ -37,10 +37,10 @@ static int s_retry_count = 0; #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -126,8 +126,8 @@ void app_main() { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index 693f6214a..a7acba4fd 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -19,10 +19,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -53,8 +53,8 @@ int main(int argc, char **argv) { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/mbed/z_pub.cpp b/examples/mbed/z_pub.cpp index 4b44ae93e..8720faaa3 100644 --- a/examples/mbed/z_pub.cpp +++ b/examples/mbed/z_pub.cpp @@ -19,10 +19,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -40,8 +40,8 @@ int main(int argc, char **argv) { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/mbed/z_pull.cpp b/examples/mbed/z_pull.cpp index 767fa595b..3ca962ed7 100644 --- a/examples/mbed/z_pull.cpp +++ b/examples/mbed/z_pull.cpp @@ -19,10 +19,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -46,8 +46,8 @@ int main(int argc, char **argv) { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index 50c230aef..5f621c74e 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -19,10 +19,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -49,8 +49,8 @@ int main(int argc, char **argv) { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/mbed/z_sub.cpp b/examples/mbed/z_sub.cpp index 903abc816..d8f9329ee 100644 --- a/examples/mbed/z_sub.cpp +++ b/examples/mbed/z_sub.cpp @@ -19,10 +19,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -46,8 +46,8 @@ int main(int argc, char **argv) { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index 632be3719..c1971702e 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -71,7 +71,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c11/z_info.c b/examples/unix/c11/z_info.c index e5ae98dd6..68091cde8 100644 --- a/examples/unix/c11/z_info.c +++ b/examples/unix/c11/z_info.c @@ -55,7 +55,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c11/z_pub.c b/examples/unix/c11/z_pub.c index 3516dfb43..e7b7e64bc 100644 --- a/examples/unix/c11/z_pub.c +++ b/examples/unix/c11/z_pub.c @@ -72,7 +72,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c11/z_pub_st.c b/examples/unix/c11/z_pub_st.c index 5f20feb92..1e27ade12 100644 --- a/examples/unix/c11/z_pub_st.c +++ b/examples/unix/c11/z_pub_st.c @@ -55,7 +55,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c11/z_pull.c b/examples/unix/c11/z_pull.c index 55b5b09da..678641aed 100644 --- a/examples/unix/c11/z_pull.c +++ b/examples/unix/c11/z_pull.c @@ -53,7 +53,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c11/z_put.c b/examples/unix/c11/z_put.c index 9a7d70195..2a403af44 100644 --- a/examples/unix/c11/z_put.c +++ b/examples/unix/c11/z_put.c @@ -55,7 +55,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index d8e067b46..e6f9d8e48 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -70,7 +70,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c11/z_sub.c b/examples/unix/c11/z_sub.c index ee17640da..7cc329416 100644 --- a/examples/unix/c11/z_sub.c +++ b/examples/unix/c11/z_sub.c @@ -59,7 +59,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c11/z_sub_st.c b/examples/unix/c11/z_sub_st.c index a007918a6..7a8b96143 100644 --- a/examples/unix/c11/z_sub_st.c +++ b/examples/unix/c11/z_sub_st.c @@ -59,7 +59,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 1d01b4890..f20104028 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -71,7 +71,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c99/z_info.c b/examples/unix/c99/z_info.c index e434be56f..a06f13ee3 100644 --- a/examples/unix/c99/z_info.c +++ b/examples/unix/c99/z_info.c @@ -55,7 +55,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c99/z_pub.c b/examples/unix/c99/z_pub.c index 51743bdc4..a5839f36f 100644 --- a/examples/unix/c99/z_pub.c +++ b/examples/unix/c99/z_pub.c @@ -55,7 +55,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c99/z_pub_st.c b/examples/unix/c99/z_pub_st.c index 25b1dc88e..1261fcd50 100644 --- a/examples/unix/c99/z_pub_st.c +++ b/examples/unix/c99/z_pub_st.c @@ -55,7 +55,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c99/z_pull.c b/examples/unix/c99/z_pull.c index bb21dd313..e01f43155 100644 --- a/examples/unix/c99/z_pull.c +++ b/examples/unix/c99/z_pull.c @@ -53,7 +53,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); if (locator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c99/z_put.c b/examples/unix/c99/z_put.c index cdeab6652..45ebe29b4 100644 --- a/examples/unix/c99/z_put.c +++ b/examples/unix/c99/z_put.c @@ -55,7 +55,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index 58ddb2b3c..e920b1035 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -66,7 +66,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c99/z_sub.c b/examples/unix/c99/z_sub.c index 01aa4ebb8..db294ee00 100644 --- a/examples/unix/c99/z_sub.c +++ b/examples/unix/c99/z_sub.c @@ -59,7 +59,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/unix/c99/z_sub_st.c b/examples/unix/c99/z_sub_st.c index cdc216623..4a87cce3b 100644 --- a/examples/unix/c99/z_sub_st.c +++ b/examples/unix/c99/z_sub_st.c @@ -59,7 +59,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index 602703050..b2b6aafda 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -43,7 +43,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/windows/z_info.c b/examples/windows/z_info.c index 4df38b1e6..eed2c2952 100644 --- a/examples/windows/z_info.c +++ b/examples/windows/z_info.c @@ -35,7 +35,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/windows/z_pub.c b/examples/windows/z_pub.c index 400872361..31bf582db 100644 --- a/examples/windows/z_pub.c +++ b/examples/windows/z_pub.c @@ -29,7 +29,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/windows/z_pub_st.c b/examples/windows/z_pub_st.c index 65ca61cfd..55475ad22 100644 --- a/examples/windows/z_pub_st.c +++ b/examples/windows/z_pub_st.c @@ -29,7 +29,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/windows/z_pull.c b/examples/windows/z_pull.c index c98ead7fb..52941550e 100644 --- a/examples/windows/z_pull.c +++ b/examples/windows/z_pull.c @@ -33,7 +33,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/windows/z_put.c b/examples/windows/z_put.c index 7f71ae46d..464eab495 100644 --- a/examples/windows/z_put.c +++ b/examples/windows/z_put.c @@ -29,7 +29,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index c9f404540..60b03ab5d 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -42,7 +42,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/windows/z_sub.c b/examples/windows/z_sub.c index 61c41bf57..d48328500 100644 --- a/examples/windows/z_sub.c +++ b/examples/windows/z_sub.c @@ -36,7 +36,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/windows/z_sub_st.c b/examples/windows/z_sub_st.c index 0a2f76692..d9b66fa80 100644 --- a/examples/windows/z_sub_st.c +++ b/examples/windows/z_sub_st.c @@ -36,7 +36,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(locator)); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); } printf("Opening session...\n"); diff --git a/examples/zephyr/z_get.c b/examples/zephyr/z_get.c index a99166284..4fcc47649 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -20,10 +20,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -49,8 +49,8 @@ int main(int argc, char **argv) { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/zephyr/z_pub.c b/examples/zephyr/z_pub.c index 9ed3d29c2..606311860 100644 --- a/examples/zephyr/z_pub.c +++ b/examples/zephyr/z_pub.c @@ -20,10 +20,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -37,8 +37,8 @@ int main(int argc, char **argv) { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/zephyr/z_pull.c b/examples/zephyr/z_pull.c index 6a7209aae..2d4d7c856 100644 --- a/examples/zephyr/z_pull.c +++ b/examples/zephyr/z_pull.c @@ -18,10 +18,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -41,8 +41,8 @@ int main(int argc, char **argv) { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index d26735bee..154075dfa 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -19,10 +19,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -45,8 +45,8 @@ int main(int argc, char **argv) { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/examples/zephyr/z_sub.c b/examples/zephyr/z_sub.c index f385a2962..3860f9e8a 100644 --- a/examples/zephyr/z_sub.c +++ b/examples/zephyr/z_sub.c @@ -18,10 +18,10 @@ #define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode #if CLIENT_OR_PEER == 0 #define MODE "client" -#define PEER "" // If empty, it will scout +#define CONNECT "" // If empty, it will scout #elif CLIENT_OR_PEER == 1 #define MODE "peer" -#define PEER "udp/224.0.0.225:7447#iface=en0" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" #else #error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." #endif @@ -41,8 +41,8 @@ int main(int argc, char **argv) { // Initialize Zenoh Session and other parameters z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); - if (strcmp(PEER, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(PEER)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); } // Open Zenoh session diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index 23a929993..2bbb9abc2 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -32,7 +32,7 @@ * Default value : None. * Multiple values are not accepted in zenoh-pico. */ -#define Z_CONFIG_PEER_KEY 0x41 +#define Z_CONFIG_CONNECT_KEY 0x41 /** * A locator to listen on. @@ -40,7 +40,7 @@ * Default value : None. * Multiple values accepted. */ -#define Z_CONFIG_LISTENER_KEY 0x42 +#define Z_CONFIG_LISTEN_KEY 0x42 /** * The user name to use for authentication. diff --git a/src/net/config.c b/src/net/config.c index ae6ab4c65..7b0457836 100644 --- a/src/net/config.c +++ b/src/net/config.c @@ -33,7 +33,7 @@ _z_config_t *_z_config_client(const char *locator) { _zp_config_insert(ps, Z_CONFIG_MODE_KEY, _z_string_make(Z_CONFIG_MODE_CLIENT)); if (locator != NULL) { // Connect only to the provided locator - _zp_config_insert(ps, Z_CONFIG_PEER_KEY, _z_string_make(locator)); + _zp_config_insert(ps, Z_CONFIG_CONNECT_KEY, _z_string_make(locator)); } else { // The locator is not provided, we should perform scouting _zp_config_insert(ps, Z_CONFIG_MULTICAST_SCOUTING_KEY, _z_string_make(Z_CONFIG_MULTICAST_SCOUTING_DEFAULT)); diff --git a/src/net/session.c b/src/net/session.c index cf1f73407..28ceb6725 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -61,7 +61,7 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { if (config != NULL) { _z_str_array_t locators = _z_str_array_empty(); - if (_z_config_get(config, Z_CONFIG_PEER_KEY) == NULL) { // Scout if peer is not configured + if (_z_config_get(config, Z_CONFIG_CONNECT_KEY) == NULL) { // Scout if peer is not configured opt_as_str = _z_config_get(config, Z_CONFIG_SCOUTING_WHAT_KEY); if (opt_as_str == NULL) { opt_as_str = Z_CONFIG_SCOUTING_WHAT_DEFAULT; @@ -89,7 +89,7 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { _z_hello_list_free(&hellos); } else { locators = _z_str_array_make(1); - locators.val[0] = _z_str_clone(_z_config_get(config, Z_CONFIG_PEER_KEY)); + locators.val[0] = _z_str_clone(_z_config_get(config, Z_CONFIG_CONNECT_KEY)); } ret = _Z_ERR_SCOUT_NO_RESULTS; diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index a11d3095c..a9cadb833 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -183,9 +183,9 @@ int main(int argc, char **argv) { _ret_config = z_config_default(); assert(z_check(_ret_config)); #ifdef ZENOH_PICO - _ret_int8 = zp_config_insert(z_loan(_ret_config), Z_CONFIG_PEER_KEY, z_string_make(argv[1])); + _ret_int8 = zp_config_insert(z_loan(_ret_config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[1])); assert_eq(_ret_int8, 0); - const char *_ret_cstr = zp_config_get(z_loan(_ret_config), Z_CONFIG_PEER_KEY); + const char *_ret_cstr = zp_config_get(z_loan(_ret_config), Z_CONFIG_CONNECT_KEY); assert_eq(strlen(_ret_cstr), strlen(argv[1])); assert_eq(strncmp(_ret_cstr, argv[1], strlen(_ret_cstr)), 0); #endif @@ -249,9 +249,9 @@ int main(int argc, char **argv) { _ret_config = z_config_default(); #ifdef ZENOH_PICO - _ret_int8 = zp_config_insert(z_loan(_ret_config), Z_CONFIG_PEER_KEY, z_string_make(argv[1])); + _ret_int8 = zp_config_insert(z_loan(_ret_config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[1])); assert_eq(_ret_int8, 0); - _ret_cstr = zp_config_get(z_loan(_ret_config), Z_CONFIG_PEER_KEY); + _ret_cstr = zp_config_get(z_loan(_ret_config), Z_CONFIG_CONNECT_KEY); assert_eq(strlen(_ret_cstr), strlen(argv[1])); assert_eq(strncmp(_ret_cstr, argv[1], strlen(_ret_cstr)), 0); #endif diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 5c4b1fd1d..80ca3c2e5 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -112,7 +112,7 @@ int main(int argc, char **argv) { int is_reliable = strncmp(argv[1], "tcp", 3) == 0; z_owned_config_t config = z_config_default(); - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(argv[1])); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[1])); for (unsigned int i = 0; i < SET; i++) idx[i] = i; @@ -129,7 +129,7 @@ int main(int argc, char **argv) { z_sleep_s(SLEEP); config = z_config_default(); - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(argv[1])); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[1])); z_owned_session_t s2 = z_open(z_move(config)); assert(z_check(s2)); diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 6d803b67b..125c90039 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -65,7 +65,7 @@ int main(int argc, char **argv) { z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make("peer")); - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(argv[1])); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[1])); for (unsigned int i = 0; i < SET; i++) idx[i] = i; @@ -84,7 +84,7 @@ int main(int argc, char **argv) { config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make("peer")); - zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(argv[1])); + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[1])); z_owned_session_t s2 = z_open(z_move(config)); assert(z_check(s2)); From 3e366a575e472e4f28b5a97007b2a5c1ed9e17b9 Mon Sep 17 00:00:00 2001 From: Pierre Avital Date: Wed, 13 Sep 2023 15:23:34 +0200 Subject: [PATCH 181/181] remove double keyexpr declaration in publisher (#247) --- src/net/primitives.c | 3 --- zenohpico.pc | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/net/primitives.c b/src/net/primitives.c index 374e52b0d..d536f2e96 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -106,9 +106,6 @@ _z_publisher_t *_z_declare_publisher(_z_session_t *zn, _z_keyexpr_t keyexpr, z_c ret->_id = _z_get_entity_id(zn); ret->_congestion_control = congestion_control; ret->_priority = priority; - - // Build the declare message to send on the wire - _z_declare_resource(zn, keyexpr); } return ret; diff --git a/zenohpico.pc b/zenohpico.pc index 5617384c4..743095b12 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.10.20230911dev +Version: 0.10.20230913dev Cflags: -I${prefix}/ Libs: -L${prefix}/ -lzenohpico