diff --git a/.github/workflows/build-check.yaml b/.github/workflows/build-check.yaml index e67242d4b..88ec7dc3b 100644 --- a/.github/workflows/build-check.yaml +++ b/.github/workflows/build-check.yaml @@ -54,6 +54,18 @@ jobs: sudo apt install -y ninja-build FORCE_C99=ON CMAKE_GENERATOR=Ninja make + raweth_build: + name: Build raweth transport on ubuntu-latest + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Build raweth + run: | + sudo apt install -y ninja-build + Z_FEATURE_RAWETH_TRANSPORT=1 CMAKE_GENERATOR=Ninja make + zenoh_build: name: Build Zenoh from source runs-on: ubuntu-latest @@ -103,7 +115,7 @@ jobs: run: | sudo apt install -y ninja-build CMAKE_GENERATOR=Ninja make - python3 ./build/tests/modularity.py --pub $Z_FEATURE_PUBLICATION --sub $Z_FEATURE_SUBSCRIPTION --queryable $Z_FEATURE_QUERYABLE --query $Z_FEATURE_QUERY --attachment 1 + python3 ./build/tests/modularity.py --pub $Z_FEATURE_PUBLICATION --sub $Z_FEATURE_SUBSCRIPTION --queryable $Z_FEATURE_QUERYABLE --query $Z_FEATURE_QUERY --attachment 0 timeout-minutes: 5 env: Z_FEATURE_PUBLICATION: ${{ matrix.feature_publication }} @@ -115,25 +127,6 @@ jobs: if: always() run: kill ${{ steps.run-zenoh.outputs.zenohd-pid }} - raweth_build: - name: Build and test raweth transport on ubuntu-latest - runs-on: ubuntu-latest - strategy: - matrix: - feature_reth: [1, 0] - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Build project and run test - run: | - sudo apt install -y ninja-build - CMAKE_GENERATOR=Ninja make - python3 ./build/tests/raweth.py --reth $Z_FEATURE_RAWETH_TRANSPORT - timeout-minutes: 5 - env: - Z_FEATURE_RAWETH_TRANSPORT: ${{ matrix.feature_reth }} - st_build: needs: zenoh_build name: Build and test in single thread on ubuntu-latest diff --git a/CMakeLists.txt b/CMakeLists.txt index 6702932fa..80c468f2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -132,8 +132,8 @@ set(Z_FEATURE_SUBSCRIPTION 1 CACHE STRING "Toggle subscription feature") set(Z_FEATURE_QUERY 1 CACHE STRING "Toggle query feature") set(Z_FEATURE_QUERYABLE 1 CACHE STRING "Toggle queryable feature") set(Z_FEATURE_RAWETH_TRANSPORT 0 CACHE STRING "Toggle raw ethernet transport feature") -set(Z_FEATURE_ATTACHMENT 1 CACHE STRING "Toggle attachment feature") -set(Z_FEATURE_INTEREST 1 CACHE STRING "Toggle interest feature") +set(Z_FEATURE_ATTACHMENT 0 CACHE STRING "Toggle attachment feature") +set(Z_FEATURE_INTEREST 0 CACHE STRING "Toggle interest feature") # Toggle to 1 when protocol changes are merged add_definition(Z_FEATURE_MULTI_THREAD=${Z_FEATURE_MULTI_THREAD}) add_definition(Z_FEATURE_PUBLICATION=${Z_FEATURE_PUBLICATION}) add_definition(Z_FEATURE_SUBSCRIPTION=${Z_FEATURE_SUBSCRIPTION}) diff --git a/GNUmakefile b/GNUmakefile index 1b34a6bc9..48cf3c329 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -56,7 +56,7 @@ Z_FEATURE_PUBLICATION?=1 Z_FEATURE_SUBSCRIPTION?=1 Z_FEATURE_QUERY?=1 Z_FEATURE_QUERYABLE?=1 -Z_FEATURE_ATTACHMENT?=1 +Z_FEATURE_ATTACHMENT?=0 Z_FEATURE_INTEREST?=1 Z_FEATURE_RAWETH_TRANSPORT?=0 diff --git a/docs/api.rst b/docs/api.rst index 08aaa7a95..b0591a4b8 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -25,7 +25,7 @@ Enums .. autocenum:: constants.h::z_whatami_t .. autocenum:: constants.h::zp_keyexpr_canon_status_t .. autocenum:: constants.h::z_sample_kind_t -.. autocenum:: constants.h::z_encoding_prefix_t +.. autocenum:: constants.h::z_encoding_id_t .. autocenum:: constants.h::z_consolidation_mode_t .. autocenum:: constants.h::z_reliability_t .. autocenum:: constants.h::z_reply_tag_t @@ -37,17 +37,8 @@ Data Structures ~~~~~~~~~~~~~~~ .. autoctype:: types.h::z_zint_t -.. autoctype:: types.h::z_bytes_t .. autoctype:: types.h::z_id_t -.. autoctype:: types.h::z_string_t -.. autoctype:: types.h::z_keyexpr_t -.. autoctype:: types.h::z_config_t -.. autoctype:: types.h::z_session_t -.. autoctype:: types.h::z_subscriber_t -.. autoctype:: types.h::z_publisher_t -.. autoctype:: types.h::z_queryable_t -.. autoctype:: types.h::z_encoding_t -.. autoctype:: types.h::z_value_t +.. autoctype:: types.h::z_timestamp_t .. autoctype:: types.h::z_subscriber_options_t .. autoctype:: types.h::z_query_consolidation_t .. autoctype:: types.h::z_publisher_options_t @@ -58,78 +49,167 @@ Data Structures .. autoctype:: types.h::z_publisher_put_options_t .. autoctype:: types.h::z_publisher_delete_options_t .. autoctype:: types.h::z_get_options_t -.. autoctype:: types.h::z_sample_t -.. autoctype:: types.h::z_hello_t -.. autoctype:: types.h::z_reply_t -.. autoctype:: types.h::z_reply_data_t .. autoctype:: types.h::zp_task_read_options_t .. autoctype:: types.h::zp_task_lease_options_t .. autoctype:: types.h::zp_read_options_t .. autoctype:: types.h::zp_send_keep_alive_options_t - -Arrays -~~~~~~ -.. c:type:: z_str_array_t - - Represents an array of ``char *``. - - Operations over :c:type:`z_str_array_t` must be done using the provided functions: - - - ``char *z_str_array_get(z_str_array_t *a, size_t k);`` - - ``size_t z_str_array_len(z_str_array_t *a);`` - - ``_Bool z_str_array_array_is_empty(z_str_array_t *a);`` +.. autoctype:: types.h::zp_send_join_options_t +.. autoctype:: types.h::z_qos_t Owned Types ~~~~~~~~~~~ -Like most ``z_owned_X_t`` types, you may obtain an instance of ``z_X_t`` by loaning it using ``z_X_loan(&val)``. -The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, is equivalent to writing ``z_X_loan(&val)``. - -Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said instance, as this implies the instance's inners were moved. -To make this fact more obvious when reading your code, consider using ``z_move(val)`` instead of ``&val`` as the argument. -After a move, ``val`` will still exist, but will no longer be valid. The destructors are double-free-safe, but other functions will still trust that your ``val`` is valid. - -To check if ``val`` is still valid, you may use ``z_X_check(&val)`` or ``z_check(val)`` if your compiler supports ``_Generic``, which will return ``true`` if ``val`` is valid. +TODO: owned type description .. c:type:: z_owned_bytes_t + + Represents an array of bytes. - A zenoh-allocated :c:type:`z_bytes_t`. - -.. c:type:: z_owned_str_t +.. c:type:: z_owned_string_t - A zenoh-allocated :c:type:`char *`. + Represents a string without null-terminator. .. c:type:: z_owned_keyexpr_t - A zenoh-allocated :c:type:`z_keyexpr_t`. + Represents a key expression in Zenoh. .. c:type:: z_owned_config_t - A zenoh-allocated :c:type:`z_config_t`. + Represents a Zenoh configuration, used to configure Zenoh sessions upon opening. + +.. c:type:: z_owned_scouting_config_t + + Represents a scouting configuration, used to configure a scouting procedure. .. c:type:: z_owned_session_t - A zenoh-allocated :c:type:`z_session_t`. + Represents a Zenoh Session. .. c:type:: z_owned_subscriber_t - A zenoh-allocated :c:type:`z_subscriber_t`. + Represents a Zenoh Subscriber entity. .. c:type:: z_owned_publisher_t - A zenoh-allocated :c:type:`z_publisher_t`. + Represents a Zenoh Publisher entity. .. c:type:: z_owned_queryable_t - A zenoh-allocated :c:type:`z_queryable_t`. + Represents a Zenoh Queryable entity. + +.. c:type:: z_owned_query_t + + Represents a Zenoh Query entity, received by Zenoh queryable entities. + +.. c:type:: z_owned_encoding_t + + Represents the encoding of a payload, in a MIME-like format. + +.. c:type:: z_owned_value_t + + Represents a Zenoh value. + +.. c:type:: z_owned_sample_t + + Represents a data sample. + +.. c:type:: z_owned_hello_t + + Represents the content of a `hello` message returned by a zenoh entity as a reply to a `scout` message. .. c:type:: z_owned_reply_t - A zenoh-allocated :c:type:`z_reply_t`. + Represents the reply to a query. + +.. c:type:: z_owned_string_array_t + + Represents an array of non null-terminated string. + +Loaned Types +~~~~~~~~~~~ + +TODO: loaned type description + +.. c:type:: z_loaned_bytes_t + + Represents an array of bytes. + +.. c:type:: z_loaned_string_t + + Represents a string without null-terminator. + +.. c:type:: z_loaned_keyexpr_t + + Represents a key expression in Zenoh. + +.. c:type:: z_loaned_config_t + + Represents a Zenoh configuration, used to configure Zenoh sessions upon opening. + +.. c:type:: z_loaned_scouting_config_t + + Represents a scouting configuration, used to configure a scouting procedure. + +.. c:type:: z_loaned_session_t + + Represents a Zenoh Session. + +.. c:type:: z_loaned_subscriber_t + + Represents a Zenoh Subscriber entity. + +.. c:type:: z_loaned_publisher_t + + Represents a Zenoh Publisher entity. + +.. c:type:: z_loaned_queryable_t + + Represents a Zenoh Queryable entity. + +.. c:type:: z_loaned_query_t -.. c:type:: z_owned_str_array_t + Represents a Zenoh Query entity, received by Zenoh queryable entities. - A zenoh-allocated :c:type:`z_str_array_t`. +.. c:type:: z_loaned_encoding_t + + Represents the encoding of a payload, in a MIME-like format. + +.. c:type:: z_loaned_value_t + + Represents a Zenoh value. + +.. c:type:: z_loaned_sample_t + + Represents a data sample. + +.. c:type:: z_loaned_hello_t + + Represents the content of a `hello` message returned by a zenoh entity as a reply to a `scout` message. + +.. c:type:: z_loaned_reply_t + + Represents the reply to a query. + +.. c:type:: z_loaned_string_array_t + + Represents an array of non null-terminated string. + +View Types +~~~~~~~~~~~ + +TODO: view type description + +.. c:type:: z_view_string_t + + Represents a string without null-terminator. + +.. c:type:: z_view_keyexpr_t + + Represents a key expression in Zenoh. + +.. c:type:: z_view_string_array_t + + Represents an array of non null-terminated string. Closures ~~~~~~~~ @@ -170,7 +250,9 @@ Macros Primitives ~~~~~~~~~~ -.. autocfunction:: primitives.h::z_keyexpr +.. autocfunction:: primitives.h::z_view_str_wrap +.. autocfunction:: primitives.h::z_view_keyexpr_from_string +.. autocfunction:: primitives.h::z_view_keyexpr_from_string_unchecked .. autocfunction:: primitives.h::z_keyexpr_to_string .. autocfunction:: primitives.h::zp_keyexpr_resolve .. autocfunction:: primitives.h::z_keyexpr_is_initialized @@ -192,7 +274,18 @@ Primitives .. autocfunction:: primitives.h::z_scouting_config_from .. autocfunction:: primitives.h::zp_scouting_config_get .. autocfunction:: primitives.h::zp_scouting_config_insert -.. autocfunction:: primitives.h::z_encoding_default +.. autocfunction:: primitives.h::zp_encoding_make +.. autocfunction:: primitives.h::zp_encoding_default +.. autocfunction:: primitives.h::z_encoding_check +.. autocfunction:: primitives.h::z_encoding_drop +.. autocfunction:: primitives.h::z_encoding_loan +.. autocfunction:: primitives.h::z_encoding_move +.. autocfunction:: primitives.h::z_encoding_null +.. autocfunction:: primitives.h::z_value_payload +.. autocfunction:: primitives.h::z_bytes_len +.. autocfunction:: primitives.h::z_bytes_decode_into_string +.. autocfunction:: primitives.h::z_bytes_encode_from_string +.. autocfunction:: primitives.h::z_timestamp_check .. autocfunction:: primitives.h::z_query_target_default .. autocfunction:: primitives.h::z_query_consolidation_auto .. autocfunction:: primitives.h::z_query_consolidation_default @@ -200,28 +293,33 @@ Primitives .. autocfunction:: primitives.h::z_query_consolidation_monotonic .. autocfunction:: primitives.h::z_query_consolidation_none .. autocfunction:: primitives.h::z_query_parameters -.. autocfunction:: primitives.h::z_query_keyexpr .. autocfunction:: primitives.h::z_query_value -.. autocfunction:: primitives.h::z_value_is_initialized +.. autocfunction:: primitives.h::z_query_attachment +.. autocfunction:: primitives.h::z_query_keyexpr .. autocfunction:: primitives.h::z_closure_sample .. autocfunction:: primitives.h::z_closure_query .. autocfunction:: primitives.h::z_closure_reply .. autocfunction:: primitives.h::z_closure_hello .. autocfunction:: primitives.h::z_closure_zid +.. autocfunction:: primitives.h::z_sample_loan +.. autocfunction:: primitives.h::z_string_data .. autocfunction:: primitives.h::z_scout .. autocfunction:: primitives.h::z_open .. autocfunction:: primitives.h::z_close .. autocfunction:: primitives.h::z_info_peers_zid .. autocfunction:: primitives.h::z_info_routers_zid .. autocfunction:: primitives.h::z_info_zid +.. autocfunction:: primitives.h::z_sample_keyexpr +.. autocfunction:: primitives.h::z_sample_payload +.. autocfunction:: primitives.h::z_sample_timestamp +.. autocfunction:: primitives.h::z_sample_encoding +.. autocfunction:: primitives.h::z_sample_kind +.. autocfunction:: primitives.h::z_sample_qos +.. autocfunction:: primitives.h::z_sample_attachment .. autocfunction:: primitives.h::z_put_options_default .. autocfunction:: primitives.h::z_delete_options_default .. autocfunction:: primitives.h::z_put .. autocfunction:: primitives.h::z_delete -.. autocfunction:: primitives.h::z_get_options_default -.. autocfunction:: primitives.h::z_get -.. autocfunction:: primitives.h::z_declare_keyexpr -.. autocfunction:: primitives.h::z_undeclare_keyexpr .. autocfunction:: primitives.h::z_publisher_options_default .. autocfunction:: primitives.h::z_declare_publisher .. autocfunction:: primitives.h::z_undeclare_publisher @@ -229,16 +327,23 @@ Primitives .. autocfunction:: primitives.h::z_publisher_delete_options_default .. autocfunction:: primitives.h::z_publisher_put .. autocfunction:: primitives.h::z_publisher_delete -.. autocfunction:: primitives.h::z_subscriber_options_default -.. autocfunction:: primitives.h::z_declare_subscriber -.. autocfunction:: primitives.h::z_undeclare_subscriber +.. autocfunction:: primitives.h::z_get_options_default +.. autocfunction:: primitives.h::z_get +.. autocfunction:: primitives.h::z_reply_is_ok +.. autocfunction:: primitives.h::z_reply_ok +.. autocfunction:: primitives.h::z_reply_err .. autocfunction:: primitives.h::z_queryable_options_default .. autocfunction:: primitives.h::z_declare_queryable .. autocfunction:: primitives.h::z_undeclare_queryable +.. autocfunction:: primitives.h::z_query_reply_options_default .. autocfunction:: primitives.h::z_query_reply -.. autocfunction:: primitives.h::z_reply_is_ok -.. autocfunction:: primitives.h::z_reply_ok -.. autocfunction:: primitives.h::z_reply_err +.. autocfunction:: primitives.h::z_keyexpr_new +.. autocfunction:: primitives.h::z_declare_keyexpr +.. autocfunction:: primitives.h::z_undeclare_keyexpr +.. autocfunction:: primitives.h::z_subscriber_options_default +.. autocfunction:: primitives.h::z_declare_subscriber +.. autocfunction:: primitives.h::z_undeclare_subscriber +.. autocfunction:: primitives.h::z_subscriber_keyexpr .. autocfunction:: primitives.h::zp_task_read_options_default .. autocfunction:: primitives.h::zp_start_read_task .. autocfunction:: primitives.h::zp_stop_read_task @@ -248,4 +353,6 @@ Primitives .. autocfunction:: primitives.h::zp_read_options_default .. autocfunction:: primitives.h::zp_read .. autocfunction:: primitives.h::zp_send_keep_alive_options_default -.. autocfunction:: primitives.h::zp_send_keep_alive \ No newline at end of file +.. autocfunction:: primitives.h::zp_send_keep_alive +.. autocfunction:: primitives.h::zp_send_join_options_default +.. autocfunction:: primitives.h::zp_send_join diff --git a/docs/conf.py b/docs/conf.py index 3743f81cd..a55ae85c5 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -28,8 +28,13 @@ extensions = ['sphinx_c_autodoc', 'sphinx_c_autodoc.napoleon'] language = 'c' c_autodoc_roots = ['../include/zenoh-pico/api/'] -c_autodoc_compilation_args = ["-DZ_FEATURE_PUBLICATION=1", "-DZ_FEATURE_SUBSCRIPTION=1", - "-DZ_FEATURE_QUERY=1", "-DZ_FEATURE_QUERYABLE=1"] +c_autodoc_compilation_args = [ + "-DZ_FEATURE_PUBLICATION=1", + "-DZ_FEATURE_SUBSCRIPTION=1", + "-DZ_FEATURE_QUERY=1", + "-DZ_FEATURE_QUERYABLE=1", + "-DZ_FEATURE_ATTACHMENT=1", +] # -- Options for HTML output ------------------------------------------------- html_theme = 'sphinx_rtd_theme' @@ -50,4 +55,4 @@ raise ValueError("Windows not supported yet for building docs.") else: - Config.set_library_file('/usr/lib/llvm-14/lib/libclang.so.1') # Required for readthedocs + Config.set_library_file('/usr/lib/llvm-14/lib/libclang.so.1') # Required for readthedocs diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index 754d4a1fa..a4a5a9f2d 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -42,20 +42,23 @@ void reply_dropper(void *ctx) { Serial.println(" >> Received query final notification"); } -void reply_handler(z_owned_reply_t *oreply, void *ctx) { +void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { (void)(ctx); if (z_reply_is_ok(oreply)) { - z_loaned_sample_t sample = z_reply_ok(oreply); - z_owned_str_t keystr = z_keyexpr_to_string(sample.keyexpr); - std::string val((const char *)sample.payload.start, sample.payload.len); + const z_loaned_sample_t *sample = z_reply_ok(oreply); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); Serial.print(" >> [Get listener] Received ("); - Serial.print(z_str_loan(&keystr)); + Serial.print(z_string_data(z_string_loan(&keystr))); Serial.print(", "); - Serial.print(val.c_str()); + Serial.print(z_string_data(z_string_loan(&replystr))); Serial.println(")"); - z_str_drop(z_str_move(&keystr)); + z_string_drop(z_string_move(&keystr)); + z_string_drop(z_string_move(&replystr)); } else { Serial.println(" >> Received an error"); } @@ -78,16 +81,16 @@ void setup() { Serial.println("OK"); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session Serial.print("Opening Zenoh Session..."); - s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + if (z_open(&s, z_config_move(&config)) < 0) { Serial.println("Unable to open session!"); while (1) { ; @@ -96,8 +99,8 @@ void setup() { Serial.println("OK"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_session_loan(&s), NULL); - zp_start_lease_task(z_session_loan(&s), NULL); + zp_start_read_task(z_session_loan_mut(&s), NULL); + zp_start_lease_task(z_session_loan_mut(&s), NULL); Serial.println("Zenoh setup finished!"); @@ -110,12 +113,21 @@ void loop() { Serial.print("Sending Query "); Serial.print(KEYEXPR); Serial.println(" ..."); - z_get_options_t opts = z_get_options_default(); + z_get_options_t opts; + z_get_options_default(&opts); + // Value encoding if (strcmp(VALUE, "") != 0) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + z_view_string_t value_str; + z_view_string_wrap(&value_str, VALUE); + z_owned_bytes_t payload; + z_bytes_encode_from_string(&payload, z_view_string_loan(&value_str)); + opts.payload = &payload; } - 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) { + z_owned_closure_reply_t callback; + z_closure_reply(&callback, reply_handler, reply_dropper, NULL); + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_get(z_session_loan(&s), z_view_keyexpr_loan(&ke), "", z_closure_reply_move(&callback), &opts) < 0) { Serial.println("Unable to send query."); } } diff --git a/examples/arduino/z_pub.ino b/examples/arduino/z_pub.ino index ce0a14621..df03b0bcf 100644 --- a/examples/arduino/z_pub.ino +++ b/examples/arduino/z_pub.ino @@ -55,16 +55,17 @@ void setup() { Serial.println("OK"); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session Serial.print("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { Serial.println("Unable to open session!"); while (1) { ; @@ -73,15 +74,16 @@ void setup() { Serial.println("OK"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_session_loan(&s), NULL); - zp_start_lease_task(z_session_loan(&s), NULL); + zp_start_read_task(z_session_loan_mut(&s), NULL); + zp_start_lease_task(z_session_loan_mut(&s), NULL); // Declare Zenoh publisher Serial.print("Declaring publisher for "); Serial.print(KEYEXPR); Serial.println("..."); - pub = z_declare_publisher(z_session_loan(&s), z_keyexpr(KEYEXPR), NULL); - if (!z_publisher_check(&pub)) { + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_declare_publisher(&pub, z_session_loan(&s), z_view_keyexpr_loan(&ke), NULL) < 0) { Serial.println("Unable to declare publisher for key expression!"); while (1) { ; diff --git a/examples/arduino/z_pull.ino b/examples/arduino/z_pull.ino index 17b7b3722..d785a1faf 100644 --- a/examples/arduino/z_pull.ino +++ b/examples/arduino/z_pull.ino @@ -38,17 +38,18 @@ // z_owned_pull_subscriber_t sub; // @TODO -// void data_handler(const z_sample_t *sample, void *arg) { -// z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); +// void data_handler(const z_loaned_sample_t *sample, void *arg) { +// z_owned_string_t keystr; +// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); // std::string val((const char *)sample->payload.start, sample->payload.len); // Serial.print(" >> [Subscription listener] Received ("); -// Serial.print(z_str_loan(&keystr)); +// Serial.print(z_string_data(z_string_loan(&keystr))); // Serial.print(", "); // Serial.print(val.c_str()); // Serial.println(")"); -// z_str_drop(z_str_move(&keystr)); +// z_string_drop(z_string_move(&keystr)); // } void setup() { @@ -68,16 +69,17 @@ void setup() { Serial.println("OK"); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session Serial.print("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { Serial.println("Unable to open session!"); while (1) { ; @@ -86,18 +88,21 @@ void setup() { Serial.println("OK"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_session_loan(&s), NULL); - zp_start_lease_task(z_session_loan(&s), NULL); + zp_start_read_task(z_session_loan_mut(&s), NULL); + zp_start_lease_task(z_session_loan_mut(&s), NULL); // Declare Zenoh subscriber Serial.print("Declaring Subscriber on "); Serial.print(KEYEXPR); Serial.println(" ..."); // @TODO - // z_owned_closure_sample_t callback = z_closure_sample(data_handler, NULL, NULL); + // z_owned_closure_sample_t callback; + // z_closure_sample(&callback, data_handler, NULL, NULL); // @TODO - // sub = z_declare_pull_subscriber(z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_sample_move(&callback), NULL); - // if (!z_pull_subscriber_check(&sub)) { + // z_view_keyexpr_t ke; + // z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + // sub = z_declare_pull_subscriber(z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_sample_move(&callback), + // NULL); if (!z_pull_subscriber_check(&sub)) { // Serial.println("Unable to declare subscriber."); // while (1) { // ; diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index b6e3c7ed5..da225a613 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -35,18 +35,36 @@ #define KEYEXPR "demo/example/zenoh-pico-queryable" #define VALUE "[ARDUINO]{ESP32} Queryable from Zenoh-Pico!" -void query_handler(const z_query_t *query, void *arg) { - z_owned_str_t keystr = z_keyexpr_to_string(z_query_keyexpr(query)); +void query_handler(const z_loaned_query_t *query, void *arg) { + z_owned_string_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + Serial.print(" >> [Queryable handler] Received Query '"); + Serial.print(z_string_data(z_string_loan(&keystr))); + Serial.println("'"); + + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + Serial.print(" with value '"); + Serial.print(z_string_data(z_string_loan(&payload_string))); + Serial.println("'"); + z_string_drop(z_string_move(&payload_string)); + } + + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); - Serial.print(" >> [Queryable handler] Replying Data ('"); - Serial.print(z_str_loan(&keystr)); - Serial.print("': '"); - Serial.print(VALUE); - Serial.println("')"); + // Reply value encoding + z_view_string_t reply_str; + z_view_string_wrap(&reply_str, VALUE); + z_owned_bytes_t reply_payload; + z_bytes_encode_from_string(&reply_payload, z_view_string_loan(&reply_str)); - z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL); + z_query_reply(query, z_view_keyexpr_loan(&ke), z_bytes_move(&reply_payload), NULL); - z_str_drop(z_str_move(&keystr)); + z_string_drop(z_string_move(&keystr)); } void setup() { @@ -66,16 +84,17 @@ void setup() { Serial.println("OK"); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session Serial.print("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { Serial.println("Unable to open session!"); while (1) { ; @@ -84,17 +103,20 @@ void setup() { Serial.println("OK"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_session_loan(&s), NULL); - zp_start_lease_task(z_session_loan(&s), NULL); + zp_start_read_task(z_session_loan_mut(&s), NULL); + zp_start_lease_task(z_session_loan_mut(&s), NULL); // Declare Zenoh queryable Serial.print("Declaring Queryable on "); Serial.print(KEYEXPR); Serial.println(" ..."); - z_owned_closure_query_t callback = z_closure_query(query_handler, NULL, NULL); - z_owned_queryable_t qable = - z_declare_queryable(z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_query_move(&callback), NULL); - if (!z_queryable_check(&qable)) { + z_owned_closure_query_t callback; + z_closure_query(&callback, query_handler, NULL, NULL); + z_owned_queryable_t qable; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_declare_queryable(&qable, z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_query_move(&callback), + NULL) < 0) { Serial.println("Unable to declare queryable."); while (1) { ; diff --git a/examples/arduino/z_scout.ino b/examples/arduino/z_scout.ino index 632b50295..2d168225c 100644 --- a/examples/arduino/z_scout.ino +++ b/examples/arduino/z_scout.ino @@ -54,12 +54,13 @@ void fprintwhatami(unsigned int whatami) { } } -void fprintlocators(const z_str_array_t *locs) { +void fprintlocators(const z_loaned_string_array_t *locs) { Serial.print("["); - size_t len = z_str_array_len(locs); + size_t len = z_string_array_len(locs); for (unsigned int i = 0; i < len; i++) { Serial.print("'"); - Serial.print(*z_str_array_get(locs, i)); + const z_loaned_string_t *str = z_string_array_get(locs, i); + Serial.print(str->val); Serial.print("'"); if (i < len - 1) { Serial.print(", "); @@ -68,13 +69,13 @@ void fprintlocators(const z_str_array_t *locs) { Serial.print("]"); } -void fprinthello(const z_hello_t hello) { +void fprinthello(const z_loaned_hello_t *hello) { Serial.print(" >> Hello { zid: "); - fprintzid(hello.zid); + fprintzid(hello->zid); Serial.print(", whatami: "); - fprintwhatami(hello.whatami); + fprintwhatami(hello->whatami); Serial.print(", locators: "); - fprintlocators(&hello.locators); + fprintlocators(&hello->locators); Serial.println(" }"); } @@ -114,8 +115,10 @@ void setup() { void loop() { 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_hello(callback, drop, context); + z_owned_scouting_config_t config; + z_scouting_config_default(&config); + z_owned_closure_hello_t closure; + z_closure_hello(&closure, callback, drop, context); printf("Scouting...\n"); z_scout(z_scouting_config_move(&config), z_closure_hello_move(&closure)); } diff --git a/examples/arduino/z_sub.ino b/examples/arduino/z_sub.ino index 3d49f5fca..990ab7580 100644 --- a/examples/arduino/z_sub.ino +++ b/examples/arduino/z_sub.ino @@ -34,19 +34,18 @@ #define KEYEXPR "demo/example/**" -void data_handler(const z_sample_t *sample, void *arg) { - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t keystr = z_keyexpr_to_string(keyexpr); - std::string val((const char *)payload.start, payload.len); +void data_handler(const z_loaned_sample_t *sample, void *arg) { + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + std::string val((const char *)z_sample_payload(sample)->start, z_sample_payload(sample)->len); Serial.print(" >> [Subscription listener] Received ("); - Serial.print(z_str_loan(&keystr)); + Serial.print(z_string_data(z_string_loan(&keystr))); Serial.print(", "); Serial.print(val.c_str()); Serial.println(")"); - z_str_drop(z_str_move(&keystr)); + z_string_drop(z_string_move(&keystr)); } void setup() { @@ -66,16 +65,17 @@ void setup() { Serial.println("OK"); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session Serial.print("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { Serial.println("Unable to open session!"); while (1) { ; @@ -84,17 +84,20 @@ void setup() { Serial.println("OK"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_session_loan(&s), NULL); - zp_start_lease_task(z_session_loan(&s), NULL); + zp_start_read_task(z_session_loan_mut(&s), NULL); + zp_start_lease_task(z_session_loan_mut(&s), NULL); // Declare Zenoh subscriber Serial.print("Declaring Subscriber on "); Serial.print(KEYEXPR); Serial.println(" ..."); - z_owned_closure_sample_t callback = z_closure_sample(data_handler, NULL, NULL); - z_owned_subscriber_t sub = - z_declare_subscriber(z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_sample_move(&callback), NULL); - if (!z_subscriber_check(&sub)) { + z_owned_closure_sample_t callback; + z_closure_sample(&callback, data_handler, NULL, NULL); + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_declare_subscriber(&sub, z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_sample_move(&callback), + NULL) < 0) { Serial.println("Unable to declare subscriber."); while (1) { ; diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index 9c984012f..7c53b86b5 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -103,12 +103,17 @@ void wifi_init_sta(void) { void reply_dropper(void *ctx) { printf(" >> Received query final notification\n"); } -void reply_handler(z_owned_reply_t *oreply, void *ctx) { +void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { if (z_reply_is_ok(oreply)) { - z_loaned_sample_t sample = z_reply_ok(oreply); - 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); + const z_loaned_sample_t *sample = z_reply_ok(oreply); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(" >> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(" >> Received an error\n"); } @@ -132,34 +137,44 @@ void app_main() { printf("OK!\n"); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_loan(s), NULL); - zp_start_lease_task(z_loan(s), NULL); + zp_start_read_task(z_loan_mut(s), NULL); + zp_start_lease_task(z_loan_mut(s), NULL); while (1) { sleep(5); printf("Sending Query '%s'...\n", KEYEXPR); - z_get_options_t opts = z_get_options_default(); + z_get_options_t opts; + z_get_options_default(&opts); + // Value encoding if (strcmp(VALUE, "") != 0) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + z_view_string_t value_str; + z_view_string_wrap(&value_str, VALUE); + z_owned_bytes_t payload; + z_bytes_encode_from_string(&payload, z_loan(value_str)); + opts.payload = &payload; } - 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) { + z_owned_closure_reply_t callback; + z_closure(&callback, reply_handler, reply_dropper); + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_get(z_loan(s), z_loan(ke), "", z_move(callback), &opts) < 0) { printf("Unable to send query.\n"); exit(-1); } @@ -167,8 +182,8 @@ void app_main() { printf("Closing Zenoh Session..."); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); printf("OK!\n"); diff --git a/examples/espidf/z_pub.c b/examples/espidf/z_pub.c index b55336f94..53cb2be99 100644 --- a/examples/espidf/z_pub.c +++ b/examples/espidf/z_pub.c @@ -119,28 +119,31 @@ void app_main() { printf("OK!\n"); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_loan(s), NULL); - zp_start_lease_task(z_loan(s), NULL); + zp_start_read_task(z_loan_mut(s), NULL); + zp_start_lease_task(z_loan_mut(s), NULL); printf("Declaring publisher for '%s'...", KEYEXPR); - z_owned_publisher_t pub = z_declare_publisher(z_loan(s), z_keyexpr(KEYEXPR), NULL); - if (!z_check(pub)) { + z_owned_publisher_t pub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); exit(-1); } @@ -158,8 +161,8 @@ void app_main() { z_undeclare_publisher(z_move(pub)); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); printf("OK!\n"); diff --git a/examples/espidf/z_pull.c b/examples/espidf/z_pull.c index 1f092d8eb..9647f4ef2 100644 --- a/examples/espidf/z_pull.c +++ b/examples/espidf/z_pull.c @@ -101,9 +101,11 @@ void wifi_init_sta(void) { } // @TODO -// void data_handler(const z_sample_t* sample, void* arg) { -// z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); -// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample->payload.len, +// void data_handler(const z_loaned_sample_t* sample, void* arg) { +// z_owned_string_t keystr; +// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), +// (int)sample->payload.len, // sample->payload.start); // z_drop(z_move(keystr)); // } @@ -126,30 +128,34 @@ void app_main() { printf("OK!\n"); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_loan(s), NULL); - zp_start_lease_task(z_loan(s), NULL); + zp_start_read_task(z_loan_mut(s), NULL); + zp_start_lease_task(z_loan_mut(s), NULL); // @TODO - // z_owned_closure_sample_t callback = z_closure(data_handler); + // z_owned_closure_sample_t callback; + // z_closure(&callback, data_handler); printf("Declaring Subscriber on '%s'...", KEYEXPR); // @TODO - // z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL); + // z_view_keyexpr_t ke; + // z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + // z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_loan(ke), z_move(callback), NULL); // if (!z_check(sub)) { // printf("Unable to declare subscriber.\n"); // exit(-1); @@ -167,8 +173,8 @@ void app_main() { printf("Pull Subscriber not supported... exiting\n"); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); printf("OK!\n"); diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index 15b4e9afd..d10e25f57 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -101,12 +101,31 @@ void wifi_init_sta(void) { vEventGroupDelete(s_event_group_handler); } -void query_handler(z_query_t *query, void *ctx) { +void query_handler(const z_loaned_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); - z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL); + z_owned_string_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t params; + z_query_parameters(query, ¶ms); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, + z_loan(params)->val); + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); + } + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + // Reply value encoding + z_view_string_t reply_str; + z_view_string_wrap(&reply_str, VALUE); + z_owned_bytes_t reply_payload; + z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + + z_query_reply(query, z_loan(ke), z_move(reply_payload), NULL); z_drop(z_move(keystr)); } @@ -128,30 +147,34 @@ void app_main() { printf("OK!\n"); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_loan(s), NULL); - zp_start_lease_task(z_loan(s), NULL); + zp_start_read_task(z_loan_mut(s), NULL); + zp_start_lease_task(z_loan_mut(s), NULL); // Declare Zenoh queryable printf("Declaring Queryable on %s...", KEYEXPR); - z_owned_closure_query_t callback = z_closure(query_handler); - z_owned_queryable_t qable = z_declare_queryable(z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL); - if (!z_check(qable)) { + z_owned_closure_query_t callback; + z_closure(&callback, query_handler); + z_owned_queryable_t qable; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to declare queryable.\n"); exit(-1); } @@ -166,8 +189,8 @@ void app_main() { z_undeclare_queryable(z_move(qable)); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); printf("OK!\n"); diff --git a/examples/espidf/z_scout.c b/examples/espidf/z_scout.c index 3b45a5a6a..e569d81ea 100644 --- a/examples/espidf/z_scout.c +++ b/examples/espidf/z_scout.c @@ -109,26 +109,27 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { } } -void fprintlocators(FILE *stream, const z_str_array_t *locs) { +void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); + const z_loaned_string_t *str = z_string_array_get(locs, i); + fprintf(stream, "%.*s", (int)str->len, str->val); fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { + if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); } } fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, "Hello { zid: "); - fprintzid(stream, hello.zid); + fprintzid(stream, hello->zid); fprintf(stream, ", whatami: "); - fprintwhatami(stream, hello.whatami); + fprintwhatami(stream, hello->whatami); fprintf(stream, ", locators: "); - fprintlocators(stream, &hello.locators); + fprintlocators(stream, &hello->locators); fprintf(stream, " }"); } @@ -167,8 +168,10 @@ void app_main() { 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_hello(callback, drop, context); + z_owned_scouting_config_t config; + z_scouting_config_default(&config); + z_owned_closure_hello_t closure; + z_closure_hello(&closure, callback, drop, context); printf("Scouting...\n"); z_scout(z_scouting_config_move(&config), z_closure_hello_move(&closure)); } diff --git a/examples/espidf/z_sub.c b/examples/espidf/z_sub.c index 4863cb853..d496734e6 100644 --- a/examples/espidf/z_sub.c +++ b/examples/espidf/z_sub.c @@ -100,12 +100,13 @@ void wifi_init_sta(void) { vEventGroupDelete(s_event_group_handler); } -void data_handler(const z_sample_t* sample, void* arg) { - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t keystr = z_keyexpr_to_string(keyexpr); - printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)payload.len, payload.start); - z_str_drop(z_str_move(&keystr)); +void data_handler(const z_loaned_sample_t* sample, void* arg) { + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t* payload = z_sample_payload(sample); + printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)), + (int)payload->len, payload->start); + z_string_drop(z_string_move(&keystr)); } void app_main() { @@ -126,29 +127,33 @@ void app_main() { printf("OK!\n"); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_loan(s), NULL); - zp_start_lease_task(z_loan(s), NULL); + zp_start_read_task(z_loan_mut(s), NULL); + zp_start_lease_task(z_loan_mut(s), NULL); printf("Declaring Subscriber on '%s'...", KEYEXPR); - z_owned_closure_sample_t callback = z_closure(data_handler); - z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL); - if (!z_check(sub)) { + z_owned_closure_sample_t callback; + z_closure(&callback, data_handler); + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); exit(-1); } @@ -162,8 +167,8 @@ void app_main() { z_undeclare_subscriber(z_move(sub)); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); printf("OK!\n"); diff --git a/examples/freertos_plus_tcp/z_get.c b/examples/freertos_plus_tcp/z_get.c index 851ffb70a..d91bb6065 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -36,62 +36,75 @@ void reply_dropper(void *ctx) { printf(">> Received query final notification\n"); } -void reply_handler(z_owned_reply_t *reply, void *ctx) { +void reply_handler(const z_loaned_reply_t *reply, void *ctx) { (void)(ctx); if (z_reply_is_ok(reply)) { - z_loaned_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); + const z_loaned_sample_t *sample = z_reply_ok(reply); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); } } void app_main(void) { - z_owned_config_t config = z_config_default(); - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); return; } // 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return; } - z_keyexpr_t ke = z_keyexpr(KEYEXPR); - if (!z_check(ke)) { - printf("%s is not a valid key expression\n", KEYEXPR); - return; + z_view_keyexpr_t ke; + if (z_view_keyexpr_from_string(&ke, KEYEXPR) < 0) { + printf("%s is not a valid key expression", KEYEXPR); + return -1; } while (1) { z_sleep_s(5); printf("Sending Query '%s'...\n", KEYEXPR); - z_get_options_t opts = z_get_options_default(); + z_get_options_t opts; + z_get_options_default(&opts); + // Value encoding if (strcmp(VALUE, "") != 0) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + z_view_string_t value_str; + z_view_string_wrap(&value_str, VALUE); + z_owned_bytes_t payload; + z_bytes_encode_from_string(&payload, z_loan(value_str)); + opts.payload = &payload; } - z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); - if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { + z_owned_closure_reply_t callback; + z_closure(&callback, reply_handler, reply_dropper); + if (z_get(z_loan(s), z_loan(ke), "", z_move(callback), &opts) < 0) { printf("Unable to send query.\n"); return; } } // Stop read and lease tasks for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); } diff --git a/examples/freertos_plus_tcp/z_pub.c b/examples/freertos_plus_tcp/z_pub.c index abb4594f4..f19ddc23e 100644 --- a/examples/freertos_plus_tcp/z_pub.c +++ b/examples/freertos_plus_tcp/z_pub.c @@ -32,15 +32,16 @@ #define VALUE "[FreeRTOS-Plus-TCP] Pub from Zenoh-Pico!" void app_main(void) { - z_owned_config_t config = z_config_default(); - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); return; } @@ -74,15 +75,18 @@ void app_main(void) { zp_task_lease_options_t lease_task_opt = {.task_attributes = &lease_task_attr}; // Start read and lease tasks for zenoh-pico - if (zp_start_read_task(z_loan(s), &read_task_opt) < 0 || zp_start_lease_task(z_loan(s), &lease_task_opt) < 0) { + if (zp_start_read_task(z_loan_mut(s), &read_task_opt) < 0 || + zp_start_lease_task(z_loan_mut(s), &lease_task_opt) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return; } 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)) { + z_owned_publisher_t pub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return; } @@ -93,17 +97,15 @@ void app_main(void) { 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_options_t options; + z_publisher_put_options_default(&options); z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); } + // Clean-up 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)); - + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); } #else diff --git a/examples/freertos_plus_tcp/z_pub_st.c b/examples/freertos_plus_tcp/z_pub_st.c index 5803030c4..bfdd22692 100644 --- a/examples/freertos_plus_tcp/z_pub_st.c +++ b/examples/freertos_plus_tcp/z_pub_st.c @@ -33,22 +33,25 @@ #define N 2147483647 // max int value by default void app_main(void) { - z_owned_config_t config = z_config_default(); - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); return; } 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)) { + z_owned_publisher_t pub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return; } diff --git a/examples/freertos_plus_tcp/z_pull.c b/examples/freertos_plus_tcp/z_pull.c index 2b043e052..8d2ef7f8f 100644 --- a/examples/freertos_plus_tcp/z_pull.c +++ b/examples/freertos_plus_tcp/z_pull.c @@ -29,40 +29,45 @@ #define KEYEXPR "demo/example/**" // @TODO -// void data_handler(const z_sample_t *sample, void *ctx) { +// void data_handler(const z_loaned_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, +// z_owned_string_t keystr; +// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)sample->payload.len, // sample->payload.start); // z_drop(z_move(keystr)); // } void app_main(void) { - z_owned_config_t config = z_config_default(); - zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); return; } // 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return; } // @TODO - // z_owned_closure_sample_t callback = z_closure(data_handler); + // z_owned_closure_sample_t callback; + // z_closure(&callback, data_handler); printf("Declaring Subscriber on '%s'...\n", KEYEXPR); // @TODO - // z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL); + // z_view_keyexpr_t ke; + // z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + // z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_loan(ke), z_move(callback), NULL); // if (!z_check(sub)) { // printf("Unable to declare subscriber.\n"); // return; @@ -78,8 +83,8 @@ void app_main(void) { printf("Pull Subscriber not supported... exiting\n"); // Stop read and lease tasks for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); } diff --git a/examples/freertos_plus_tcp/z_put.c b/examples/freertos_plus_tcp/z_put.c index 1253af540..092ed503b 100644 --- a/examples/freertos_plus_tcp/z_put.c +++ b/examples/freertos_plus_tcp/z_put.c @@ -30,39 +30,42 @@ #define VALUE "[FreeRTOS-Plus-TCP] Pub from Zenoh-Pico!" void app_main(void) { - z_owned_config_t config = z_config_default(); - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); return; } // 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return; } 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)) { + z_owned_keyexpr_t ke; + z_view_keyexpr_t vke; + z_view_keyexpr_from_string_unchecked(&vke, KEYEXPR); + if (z_declare_keyexpr(&ke, z_loan(s), z_loan(vke)) < 0) { printf("Unable to declare key expression!\n"); - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); return; } 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); + z_put_options_t options; + z_put_options_default(&options); if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)VALUE, strlen(VALUE), &options) < 0) { printf("Oh no! Put has failed...\n"); } @@ -73,8 +76,8 @@ void app_main(void) { // Clean up z_undeclare_keyexpr(z_loan(s), z_move(ke)); - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); } #else diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index 6e4a36b30..ca3444034 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -29,52 +29,67 @@ #define KEYEXPR "demo/example/zenoh-pico-queryable" #define VALUE "[FreeRTOS-Plus-TCP] Queryable from Zenoh-Pico!" -void query_handler(const z_query_t *query, void *ctx) { +void query_handler(const z_loaned_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_owned_string_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t params; + z_query_parameters(query, ¶ms); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, + z_loan(params)->val); + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); } - 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_query_reply_options_t options; + z_query_reply_options_default(&options); + // Reply value encoding + z_view_string_t reply_str; + z_view_string_wrap(&reply_str, VALUE); + z_owned_bytes_t reply_payload; + z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + + z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); z_drop(z_move(keystr)); } void app_main(void) { - z_owned_config_t config = z_config_default(); - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); return; } // 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return; } - z_keyexpr_t ke = z_keyexpr(KEYEXPR); - if (!z_check(ke)) { - printf("%s is not a valid key expression\n", KEYEXPR); - return; + z_view_keyexpr_t ke; + if (z_view_keyexpr_from_string(&ke, KEYEXPR) < 0) { + 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)) { + z_owned_closure_query_t callback; + z_closure(&callback, query_handler); + z_owned_queryable_t qable; + if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to create queryable.\n"); return; } @@ -86,8 +101,8 @@ void app_main(void) { 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)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); } diff --git a/examples/freertos_plus_tcp/z_scout.c b/examples/freertos_plus_tcp/z_scout.c index 0f3087eee..4b62c1301 100644 --- a/examples/freertos_plus_tcp/z_scout.c +++ b/examples/freertos_plus_tcp/z_scout.c @@ -43,26 +43,27 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { } } -void fprintlocators(FILE *stream, const z_str_array_t *locs) { +void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); + const z_loaned_string_t *str = z_string_array_get(locs, i); + fprintf(stream, "%.*s", (int)str->len, str->val); fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { + if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); } } fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, "Hello { zid: "); - fprintzid(stream, hello.zid); + fprintzid(stream, hello->zid); fprintf(stream, ", whatami: "); - fprintwhatami(stream, hello.whatami); + fprintwhatami(stream, hello->whatami); fprintf(stream, ", locators: "); - fprintlocators(stream, &hello.locators); + fprintlocators(stream, &hello->locators); fprintf(stream, " }"); } @@ -85,8 +86,10 @@ void drop(void *context) { void app_main(void) { int *context = (int *)pvPortMalloc(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); + z_owned_scouting_config_t config; + z_scouting_config_default(&config); + z_owned_closure_hello_t closure; + z_closure(&closure, callback, drop, context); printf("Scouting...\n"); z_scout(z_move(config), z_move(closure)); } diff --git a/examples/freertos_plus_tcp/z_sub.c b/examples/freertos_plus_tcp/z_sub.c index 1e3f312ce..d0afc19ad 100644 --- a/examples/freertos_plus_tcp/z_sub.c +++ b/examples/freertos_plus_tcp/z_sub.c @@ -28,40 +28,45 @@ #define KEYEXPR "demo/example/**" -void data_handler(const z_sample_t *sample, void *ctx) { +void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t keystr = z_keyexpr_to_string(keyexpr); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)payload.len, payload.start); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)payload->len, + payload->start); z_drop(z_move(keystr)); } void app_main(void) { - z_owned_config_t config = z_config_default(); - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); return; } // 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return; } - z_owned_closure_sample_t callback = z_closure(data_handler); + z_owned_closure_sample_t callback; + z_closure(&callback, 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)) { + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + z_owned_subscriber_t sub; + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); return; } @@ -73,8 +78,8 @@ void app_main(void) { 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)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); } diff --git a/examples/freertos_plus_tcp/z_sub_st.c b/examples/freertos_plus_tcp/z_sub_st.c index 9c9e94707..aa24456ee 100644 --- a/examples/freertos_plus_tcp/z_sub_st.c +++ b/examples/freertos_plus_tcp/z_sub_st.c @@ -31,34 +31,40 @@ int msg_nb = 0; -void data_handler(const z_sample_t *sample, void *ctx) { +void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t keystr = z_keyexpr_to_string(keyexpr); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)payload.len, payload.start); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)payload->len, + payload->start); + z_drop(z_move(keystr)); z_drop(z_move(keystr)); msg_nb++; } void app_main(void) { - z_owned_config_t config = z_config_default(); - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); return; } - z_owned_closure_sample_t callback = z_closure(data_handler); + z_owned_closure_sample_t callback; + z_closure(&callback, 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)) { + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + z_owned_subscriber_t sub; + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); return; } diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index 1784b6d4b..ec2f2cef5 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -33,12 +33,18 @@ void reply_dropper(void *ctx) { printf(" >> Received query final notification\n"); } -void reply_handler(z_owned_reply_t *oreply, void *ctx) { +void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { if (z_reply_is_ok(oreply)) { - z_loaned_sample_t sample = z_reply_ok(oreply); - z_owned_str_t keystr = z_keyexpr_to_string(sample.keyexpr); - printf(" >> Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)sample.payload.len, sample.payload.start); - z_str_drop(z_str_move(&keystr)); + const z_loaned_sample_t *sample = z_reply_ok(oreply); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(" >> Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)), + z_string_data(z_string_loan(&replystr))); + z_string_drop(z_string_move(&keystr)); + z_string_drop(z_string_move(&replystr)); } else { printf(" >> Received an error\n"); } @@ -52,34 +58,44 @@ int main(int argc, char **argv) { net.connect(); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_session_loan(&s), NULL); - zp_start_lease_task(z_session_loan(&s), NULL); + zp_start_read_task(z_session_loan_mut(&s), NULL); + zp_start_lease_task(z_session_loan_mut(&s), NULL); while (1) { z_sleep_s(5); printf("Sending Query '%s'...\n", KEYEXPR); - z_get_options_t opts = z_get_options_default(); + z_get_options_t opts; + z_get_options_default(&opts); + // Value encoding if (strcmp(VALUE, "") != 0) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + z_view_string_t value_str; + z_view_string_wrap(&value_str, VALUE); + z_owned_bytes_t payload; + z_bytes_encode_from_string(&payload, z_view_string_loan(&value_str)); + opts.payload = &payload; } - 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) { + z_owned_closure_reply_t callback; + z_closure_reply(&callback, reply_handler, reply_dropper, NULL); + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_get(z_session_loan(&s), z_view_keyexpr_loan(&ke), "", z_closure_reply_move(&callback), &opts) < 0) { printf("Unable to send query.\n"); exit(-1); } @@ -87,8 +103,8 @@ int main(int argc, char **argv) { printf("Closing Zenoh Session..."); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_session_loan(&s)); - zp_stop_lease_task(z_session_loan(&s)); + zp_stop_read_task(z_session_loan_mut(&s)); + zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); printf("OK!\n"); diff --git a/examples/mbed/z_pub.cpp b/examples/mbed/z_pub.cpp index 0fb939071..08bfd624b 100644 --- a/examples/mbed/z_pub.cpp +++ b/examples/mbed/z_pub.cpp @@ -39,28 +39,31 @@ int main(int argc, char **argv) { net.connect(); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_session_loan(&s), NULL); - zp_start_lease_task(z_session_loan(&s), NULL); + zp_start_read_task(z_session_loan_mut(&s), NULL); + zp_start_lease_task(z_session_loan_mut(&s), NULL); printf("Declaring publisher for '%s'...", KEYEXPR); - z_owned_publisher_t pub = z_declare_publisher(z_session_loan(&s), z_keyexpr(KEYEXPR), NULL); - if (!z_publisher_check(&pub)) { + z_owned_publisher_t pub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_declare_publisher(&pub, z_session_loan(&s), z_view_keyexpr_loan(&ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); exit(-1); } @@ -78,8 +81,8 @@ int main(int argc, char **argv) { z_undeclare_publisher(z_publisher_move(&pub)); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_session_loan(&s)); - zp_stop_lease_task(z_session_loan(&s)); + zp_stop_read_task(z_session_loan_mut(&s)); + zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); printf("OK!\n"); diff --git a/examples/mbed/z_pull.cpp b/examples/mbed/z_pull.cpp index 24acaeaec..52a4f9be0 100644 --- a/examples/mbed/z_pull.cpp +++ b/examples/mbed/z_pull.cpp @@ -31,11 +31,13 @@ #define KEYEXPR "demo/example/**" // @TODO -// void data_handler(const z_sample_t *sample, void *arg) { -// z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); -// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)sample->payload.len, +// void data_handler(const z_loaned_sample_t *sample, void *arg) { +// z_owned_string_t keystr; +// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)), +// (int)sample->payload.len, // sample->payload.start); -// z_str_drop(z_str_move(&keystr)); +// z_string_drop(z_string_move(&keystr)); // } int main(int argc, char **argv) { @@ -46,31 +48,33 @@ int main(int argc, char **argv) { net.connect(); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_session_loan(&s), NULL); - zp_start_lease_task(z_session_loan(&s), NULL); + zp_start_read_task(z_session_loan_mut(&s), NULL); + zp_start_lease_task(z_session_loan_mut(&s), NULL); // @TODO - // z_owned_closure_sample_t callback = z_closure_sample(data_handler, NULL, NULL); + // z_owned_closure_sample_t callback; + // z_closure_sample(&callback, data_handler, NULL, NULL); printf("Declaring Subscriber on '%s'...", KEYEXPR); // @TODO // z_owned_pull_subscriber_t sub = - // z_declare_pull_subscriber(z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_sample_move(&callback), NULL); + // z_declare_pull_subscriber(z_session_loan(&s), z_loan(ke), z_closure_sample_move(&callback), NULL); // if (!z_pull_subscriber_check(&sub)) { // printf("Unable to declare subscriber.\n"); // exit(-1); @@ -88,8 +92,8 @@ int main(int argc, char **argv) { printf("Pull Subscriber not supported... exiting\n"); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_session_loan(&s)); - zp_stop_lease_task(z_session_loan(&s)); + zp_stop_read_task(z_session_loan_mut(&s)); + zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); printf("OK!\n"); diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index b0b9358bf..7be7f79da 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -31,13 +31,30 @@ #define KEYEXPR "demo/example/zenoh-pico-queryable" #define VALUE "[MBedOS]{nucleo-F767ZI} Queryable from Zenoh-Pico!" -void query_handler(const z_query_t *query, void *ctx) { +void query_handler(const z_loaned_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_str_loan(&keystr), (int)pred.len, pred.start); - z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL); - z_str_drop(z_str_move(&keystr)); + z_owned_string_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t pred; + z_query_parameters(query, &pred); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_string_loan(&keystr)), + (int)z_view_string_loan(&pred)->len, z_view_string_loan(&pred)->val); + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_string_data(z_string_loan(&payload_string))); + z_string_drop(z_string_move(&payload_string)); + } + // Reply value encoding + z_view_string_t reply_str; + z_view_string_wrap(&reply_str, VALUE); + z_owned_bytes_t reply_payload; + z_bytes_encode_from_string(&reply_payload, z_view_string_loan(&reply_str)); + + z_query_reply(query, z_query_keyexpr(query), z_bytes_move(&reply_payload), NULL); + z_string_drop(z_string_move(&keystr)); } int main(int argc, char **argv) { @@ -48,31 +65,35 @@ int main(int argc, char **argv) { net.connect(); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_session_loan(&s), NULL); - zp_start_lease_task(z_session_loan(&s), NULL); + zp_start_read_task(z_session_loan_mut(&s), NULL); + zp_start_lease_task(z_session_loan_mut(&s), NULL); // Declare Zenoh queryable printf("Declaring Queryable on %s...", KEYEXPR); - z_owned_closure_query_t callback = z_closure_query(query_handler, NULL, NULL); - z_owned_queryable_t qable = - z_declare_queryable(z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_query_move(&callback), NULL); - if (!z_queryable_check(&qable)) { + z_owned_closure_query_t callback; + z_closure_query(&callback, query_handler, NULL, NULL); + z_owned_queryable_t qable; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_declare_queryable(&qable, z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_query_move(&callback), + NULL) < 0) { printf("Unable to declare queryable.\n"); exit(-1); } @@ -87,8 +108,8 @@ int main(int argc, char **argv) { z_undeclare_queryable(z_queryable_move(&qable)); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_session_loan(&s)); - zp_stop_lease_task(z_session_loan(&s)); + zp_stop_read_task(z_session_loan_mut(&s)); + zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); printf("OK!\n"); diff --git a/examples/mbed/z_scout.cpp b/examples/mbed/z_scout.cpp index c3cdf5d41..9f78c6be4 100644 --- a/examples/mbed/z_scout.cpp +++ b/examples/mbed/z_scout.cpp @@ -51,26 +51,27 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { } } -void fprintlocators(FILE *stream, const z_str_array_t *locs) { +void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); + const z_loaned_string_t *str = z_string_array_get(locs, i); + fprintf(stream, "%.*s", (int)str->len, str->val); fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { + if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); } } fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, "Hello { zid: "); - fprintzid(stream, hello.zid); + fprintzid(stream, hello->zid); fprintf(stream, ", whatami: "); - fprintwhatami(stream, hello.whatami); + fprintwhatami(stream, hello->whatami); fprintf(stream, ", locators: "); - fprintlocators(stream, &hello.locators); + fprintlocators(stream, &hello->locators); fprintf(stream, " }"); } @@ -99,8 +100,10 @@ int main(void) { 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_hello(callback, drop, context); + z_owned_scouting_config_t config; + z_scouting_config_default(&config); + z_owned_closure_hello_t closure; + z_closure_hello(&closure, callback, drop, context); printf("Scouting...\n"); z_scout(z_scouting_config_move(&config), z_closure_hello_move(&closure)); diff --git a/examples/mbed/z_sub.cpp b/examples/mbed/z_sub.cpp index 45d9a1b6d..20ea106d4 100644 --- a/examples/mbed/z_sub.cpp +++ b/examples/mbed/z_sub.cpp @@ -30,13 +30,13 @@ #define KEYEXPR "demo/example/**" -void data_handler(const z_sample_t *sample, void *arg) { - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t keystr = z_keyexpr_to_string(keyexpr); - printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)payload.len, - payload.start); - z_str_drop(z_str_move(&keystr)); +void data_handler(const z_loaned_sample_t *sample, void *arg) { + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)), + (int)payload->len, payload->start); + z_string_drop(z_string_move(&keystr)); } int main(int argc, char **argv) { @@ -47,30 +47,34 @@ int main(int argc, char **argv) { net.connect(); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_session_loan(&s), NULL); - zp_start_lease_task(z_session_loan(&s), NULL); + zp_start_read_task(z_session_loan_mut(&s), NULL); + zp_start_lease_task(z_session_loan_mut(&s), NULL); printf("Declaring Subscriber on '%s'...", KEYEXPR); - z_owned_closure_sample_t callback = z_closure_sample(data_handler, NULL, NULL); - z_owned_subscriber_t sub = - z_declare_subscriber(z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_sample_move(&callback), NULL); - if (!z_subscriber_check(&sub)) { + z_owned_closure_sample_t callback; + z_closure_sample(&callback, data_handler, NULL, NULL); + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_declare_subscriber(&sub, z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_sample_move(&callback), + NULL) < 0) { printf("Unable to declare subscriber.\n"); exit(-1); } @@ -84,8 +88,8 @@ int main(int argc, char **argv) { z_undeclare_subscriber(z_subscriber_move(&sub)); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_session_loan(&s)); - zp_stop_lease_task(z_session_loan(&s)); + zp_stop_read_task(z_session_loan_mut(&s)); + zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); printf("OK!\n"); diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index 7e76e51c7..6066ac2e7 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -37,12 +37,16 @@ int8_t attachment_handler(z_bytes_t key, z_bytes_t att_value, void *ctx) { } #endif -void reply_handler(z_owned_reply_t *reply, void *ctx) { +void reply_handler(const z_loaned_reply_t *reply, void *ctx) { (void)(ctx); if (z_reply_is_ok(reply)) { - z_loaned_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); + const z_loaned_sample_t *sample = z_reply_ok(reply); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr))); #if Z_FEATURE_ATTACHMENT == 1 if (z_attachment_check(&sample.attachment)) { printf("Attachement found\n"); @@ -50,6 +54,7 @@ void reply_handler(z_owned_reply_t *reply, void *ctx) { } #endif z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); } @@ -95,48 +100,57 @@ int main(int argc, char **argv) { z_mutex_init(&mutex); z_condvar_init(&cond); - z_owned_config_t config = z_config_default(); - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } - z_keyexpr_t ke = z_keyexpr(keyexpr); - if (!z_check(ke)) { + z_view_keyexpr_t ke; + if (z_view_keyexpr_from_string(&ke, keyexpr) < 0) { printf("%s is not a valid key expression", keyexpr); return -1; } z_mutex_lock(&mutex); printf("Sending Query '%s'...\n", keyexpr); - z_get_options_t opts = z_get_options_default(); + z_get_options_t opts; + z_get_options_default(&opts); + if (value != NULL) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + z_view_string_t value_str; + z_view_string_wrap(&value_str, value); + z_owned_bytes_t payload; + z_bytes_encode_from_string(&payload, z_loan(value_str)); + opts.payload = &payload; } #if Z_FEATURE_ATTACHMENT == 1 z_owned_bytes_map_t map = z_bytes_map_new(); z_bytes_map_insert_by_alias(&map, z_bytes_from_str("hi"), z_bytes_from_str("there")); opts.attachment = z_bytes_map_as_attachment(&map); #endif - z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); - if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { + + z_owned_closure_reply_t callback; + z_closure(&callback, reply_handler, reply_dropper); + if (z_get(z_loan(s), z_loan(ke), "", z_move(callback), &opts) < 0) { printf("Unable to send query.\n"); return -1; } @@ -144,8 +158,8 @@ int main(int argc, char **argv) { z_mutex_unlock(&mutex); // Stop read and lease tasks for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index d79d53488..e2eef163c 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -57,53 +57,67 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } - z_keyexpr_t ke = z_keyexpr(keyexpr); - if (!z_check(ke)) { + z_view_keyexpr_t ke; + if (z_view_keyexpr_from_string(&ke, keyexpr) < 0) { printf("%s is not a valid key expression", keyexpr); return -1; } printf("Sending Query '%s'...\n", keyexpr); - z_get_options_t opts = z_get_options_default(); + z_get_options_t opts; + z_get_options_default(&opts); + // Value encoding if (value != NULL) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + z_view_string_t value_str; + z_view_string_wrap(&value_str, value); + z_owned_bytes_t payload; + z_bytes_encode_from_string(&payload, z_loan(value_str)); + opts.payload = &payload; } - z_owned_reply_ring_channel_t channel = z_reply_ring_channel_new(1); - if (z_get(z_loan(s), ke, "", z_move(channel.send), &opts) < 0) { + z_owned_reply_ring_channel_t channel; + z_reply_ring_channel_new(&channel, 1); + if (z_get(z_loan(s), z_loan(ke), "", z_move(channel.send), &opts) < 0) { printf("Unable to send query.\n"); return -1; } - z_owned_reply_t reply = z_reply_null(); + z_owned_reply_t reply; + z_null(&reply); for (z_call(channel.recv, &reply); z_check(reply); z_call(channel.recv, &reply)) { - if (z_reply_is_ok(&reply)) { - z_loaned_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); + if (z_reply_is_ok(z_loan(reply))) { + const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply)); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); } @@ -112,8 +126,8 @@ int main(int argc, char **argv) { z_drop(z_move(channel)); // Stop read and lease tasks for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/unix/c11/z_info.c b/examples/unix/c11/z_info.c index 455fe17c7..d70d1c55e 100644 --- a/examples/unix/c11/z_info.c +++ b/examples/unix/c11/z_info.c @@ -56,24 +56,25 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; @@ -84,18 +85,20 @@ int main(int argc, char **argv) { print_zid(&self_id, NULL); printf("Routers IDs:\n"); - z_owned_closure_zid_t callback = z_closure(print_zid); + z_owned_closure_zid_t callback; + z_closure(&callback, 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_owned_closure_zid_t callback2; + z_closure(&callback2, 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)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); } diff --git a/examples/unix/c11/z_ping.c b/examples/unix/c11/z_ping.c index f4da4dd18..fa7744d64 100644 --- a/examples/unix/c11/z_ping.c +++ b/examples/unix/c11/z_ping.c @@ -30,7 +30,7 @@ static z_condvar_t cond; static z_mutex_t mutex; -void callback(const z_sample_t* sample, void* context) { +void callback(const z_loaned_sample_t* sample, void* context) { (void)sample; (void)context; z_condvar_signal(&cond); @@ -63,31 +63,35 @@ 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)); - if (!z_check(session)) { + z_owned_config_t config; + z_config_default(&config); + z_owned_session_t session; + if (z_open(&session, z_move(config)) < 0) { 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) { + if (zp_start_read_task(z_loan_mut(session), NULL) < 0 || zp_start_lease_task(z_loan_mut(session), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&session)); return -1; } - z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); - z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_view_keyexpr_t ping; + z_view_keyexpr_from_string_unchecked(&ping, "test/ping"); + z_view_keyexpr_t pong; + z_view_keyexpr_from_string_unchecked(&pong, "test/pong"); - z_owned_publisher_t pub = z_declare_publisher(z_loan(session), ping, NULL); - if (!z_check(pub)) { + z_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_loan(session), z_loan(ping), NULL) < 0) { 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)) { + z_owned_closure_sample_t respond; + z_closure(&respond, callback, drop, NULL); + z_owned_subscriber_t sub; + if (z_declare_subscriber(&sub, z_loan(session), z_loan(pong), z_move(respond), NULL) < 0) { printf("Unable to declare subscriber for key expression.\n"); return -1; } @@ -123,8 +127,8 @@ int main(int argc, char** argv) { z_drop(z_move(pub)); z_drop(z_move(sub)); - zp_stop_read_task(z_loan(session)); - zp_stop_lease_task(z_loan(session)); + zp_stop_read_task(z_loan_mut(session)); + zp_stop_lease_task(z_loan_mut(session)); z_close(z_move(session)); } diff --git a/examples/unix/c11/z_pong.c b/examples/unix/c11/z_pong.c index b990e21c2..8be8fb256 100644 --- a/examples/unix/c11/z_pong.c +++ b/examples/unix/c11/z_pong.c @@ -16,10 +16,10 @@ #include "zenoh-pico.h" #if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1 -void callback(const z_sample_t* sample, void* context) { - z_publisher_t pub = z_loan(*(z_owned_publisher_t*)context); - z_bytes_t payload = z_sample_payload(sample); - z_publisher_put(pub, payload.start, payload.len, NULL); +void callback(const z_loaned_sample_t* sample, void* context) { + const z_loaned_publisher_t* pub = z_loan(*(z_owned_publisher_t*)context); + const z_loaned_bytes_t* payload = z_sample_payload(sample); + z_publisher_put(pub, payload->start, payload->len, NULL); } void drop(void* context) { z_owned_publisher_t* pub = (z_owned_publisher_t*)context; @@ -33,30 +33,34 @@ void drop(void* context) { 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)); - if (!z_check(session)) { + z_owned_config_t config; + z_config_default(&config); + z_owned_session_t session; + if (z_open(&session, z_move(config)) < 0) { 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) { + if (zp_start_read_task(z_loan_mut(session), NULL) < 0 || zp_start_lease_task(z_loan_mut(session), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&session)); 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)) { + z_view_keyexpr_t pong; + z_view_keyexpr_from_string_unchecked(&pong, "test/pong"); + z_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_loan(session), z_loan(pong), NULL) < 0) { 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)) { + z_view_keyexpr_t ping; + z_view_keyexpr_from_string_unchecked(&ping, "test/ping"); + z_owned_closure_sample_t respond; + z_closure(&respond, callback, drop, (void*)z_move(pub)); + z_owned_subscriber_t sub; + if (z_declare_subscriber(&sub, z_loan(session), z_loan(ping), z_move(respond), NULL) < 0) { printf("Unable to declare subscriber for key expression.\n"); return -1; } @@ -66,8 +70,8 @@ int main(int argc, char** argv) { z_drop(z_move(sub)); - zp_stop_read_task(z_loan(session)); - zp_stop_lease_task(z_loan(session)); + zp_stop_read_task(z_loan_mut(session)); + zp_stop_lease_task(z_loan_mut(session)); z_close(z_move(session)); } diff --git a/examples/unix/c11/z_pub.c b/examples/unix/c11/z_pub.c index cb5dd2ab6..576edf915 100644 --- a/examples/unix/c11/z_pub.c +++ b/examples/unix/c11/z_pub.c @@ -66,26 +66,27 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); - z_close(z_session_move(&s)); + z_close(z_move(s)); return -1; } // Wait for joins in peer mode @@ -93,32 +94,38 @@ int main(int argc, char **argv) { printf("Waiting for joins...\n"); sleep(3); } + // Declare publisher 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)) { + z_owned_publisher_t pub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return -1; } + + // Publish data printf("Press CTRL-C to quit...\n"); char buf[256]; for (int idx = 0; idx < n; ++idx) { - sleep(1); + z_sleep_s(1); sprintf(buf, "[%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); + // Create encoding + z_owned_encoding_t encoding; + zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, "utf8"); + z_publisher_put_options_t options; + z_publisher_put_options_default(&options); + options.encoding = z_move(encoding); + z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); } - + // Clean up 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)); - + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); - return 0; } #else diff --git a/examples/unix/c11/z_pub_st.c b/examples/unix/c11/z_pub_st.c index deb9e1f60..51037f4a4 100644 --- a/examples/unix/c11/z_pub_st.c +++ b/examples/unix/c11/z_pub_st.c @@ -63,25 +63,28 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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)) { + z_owned_publisher_t pub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return -1; } diff --git a/examples/unix/c11/z_pub_thr.c b/examples/unix/c11/z_pub_thr.c index 9711ed46c..228e70f01 100644 --- a/examples/unix/c11/z_pub_thr.c +++ b/examples/unix/c11/z_pub_thr.c @@ -30,28 +30,31 @@ int main(int argc, char **argv) { memset(value, 1, len); // Set config - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); if (argc > 2) { - if (zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[2])) < 0) { + if (zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, argv[2]) < 0) { printf("Couldn't insert locator in config: %s\n", argv[2]); exit(-1); } } // Open session - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); exit(-1); } // Declare publisher - z_owned_publisher_t pub = z_declare_publisher(z_loan(s), z_keyexpr(keyexpr), NULL); - if (!z_check(pub)) { + z_owned_publisher_t pub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); exit(-1); } @@ -62,8 +65,8 @@ int main(int argc, char **argv) { } // Clean up z_undeclare_publisher(z_move(pub)); - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); free(value); exit(0); diff --git a/examples/unix/c11/z_pull.c b/examples/unix/c11/z_pull.c index d64efe68d..6e7506663 100644 --- a/examples/unix/c11/z_pull.c +++ b/examples/unix/c11/z_pull.c @@ -52,42 +52,46 @@ int main(int argc, char **argv) { } } - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, locator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } printf("Declaring Subscriber on '%s'...\n", keyexpr); - z_owned_sample_ring_channel_t channel = z_sample_ring_channel_new(size); - z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(channel.send), NULL); - if (!z_check(sub)) { + z_owned_sample_ring_channel_t channel; + z_sample_ring_channel_new(&channel, size); + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(channel.send), NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } printf("Pulling data every %zu ms... Ring size: %zd\n", interval, size); - z_owned_sample_t sample = z_sample_null(); + z_owned_sample_t sample; + z_null(&sample); while (true) { for (z_call(channel.try_recv, &sample); z_check(sample); z_call(channel.try_recv, &sample)) { - z_sample_t loaned_sample = z_loan(sample); - z_keyexpr_t sample_key = z_sample_keyexpr(&loaned_sample); - z_bytes_t payload = z_sample_payload(&loaned_sample); - z_owned_str_t keystr = z_keyexpr_to_string(sample_key); - printf(">> [Subscriber] Pulled ('%s': '%.*s')\n", z_loan(keystr), (int)payload.len, payload.start); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr); + printf(">> [Subscriber] Pulled ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), + (int)z_sample_payload(z_loan(sample))->len, z_sample_payload(z_loan(sample))->start); z_drop(z_move(keystr)); z_drop(z_move(sample)); } @@ -99,8 +103,8 @@ int main(int argc, char **argv) { z_drop(z_move(channel)); // Stop read and lease tasks for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/unix/c11/z_put.c b/examples/unix/c11/z_put.c index ecf715104..d6ac6cc87 100644 --- a/examples/unix/c11/z_put.c +++ b/examples/unix/c11/z_put.c @@ -61,48 +61,56 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); 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)) { + z_view_keyexpr_t vke; + z_view_keyexpr_from_string(&vke, keyexpr); + z_owned_keyexpr_t ke; + if (z_declare_keyexpr(&ke, z_loan(s), z_loan(vke)) < 0) { printf("Unable to declare key expression!\n"); - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); return -1; } + // Create encoding + z_owned_encoding_t encoding; + zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); + 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); + z_put_options_t options; + z_put_options_default(&options); + options.encoding = z_move(encoding); #if Z_FEATURE_ATTACHMENT == 1 z_owned_bytes_map_t map = z_bytes_map_new(); z_bytes_map_insert_by_alias(&map, z_bytes_from_str("hi"), z_bytes_from_str("there")); options.attachment = z_bytes_map_as_attachment(&map); #endif - if (z_put(z_loan(s), z_keyexpr(keyexpr), (const uint8_t *)value, strlen(value), &options) < 0) { + if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)value, strlen(value), &options) < 0) { printf("Oh no! Put has failed...\n"); } @@ -112,8 +120,8 @@ int main(int argc, char **argv) { // Clean up z_undeclare_keyexpr(z_loan(s), z_move(ke)); - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); return 0; } diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index d9a794438..73ee65683 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -31,14 +31,21 @@ int8_t attachment_handler(z_bytes_t key, z_bytes_t att_value, void *ctx) { } #endif -void query_handler(const z_query_t *query, void *ctx) { +void query_handler(const z_loaned_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_owned_string_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t params; + z_query_parameters(query, ¶ms); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, + z_loan(params)->val); + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); } #if Z_FEATURE_ATTACHMENT == 1 z_attachment_t attachment = z_query_attachment(query); @@ -48,8 +55,13 @@ void query_handler(const z_query_t *query, void *ctx) { } #endif - z_query_reply_options_t options = z_query_reply_options_default(); - options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + // Create encoding + z_owned_encoding_t encoding; + zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); + + z_query_reply_options_t options; + z_query_reply_options_default(&options); + options.encoding = z_move(encoding); #if Z_FEATURE_ATTACHMENT == 1 // Add attachment @@ -58,7 +70,13 @@ void query_handler(const z_query_t *query, void *ctx) { options.attachment = z_bytes_map_as_attachment(&map); #endif - z_query_reply(query, z_keyexpr(keyexpr), (const unsigned char *)value, strlen(value), &options); + // Reply value encoding + z_view_string_t reply_str; + z_view_string_wrap(&reply_str, value); + z_owned_bytes_t reply_payload; + z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + + z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); z_drop(z_move(keystr)); msg_nb++; @@ -107,39 +125,41 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } - z_keyexpr_t ke = z_keyexpr(keyexpr); - if (!z_check(ke)) { + z_view_keyexpr_t ke; + if (z_view_keyexpr_from_string(&ke, keyexpr) < 0) { 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)) { + z_owned_closure_query_t callback; + z_closure(&callback, query_handler); + z_owned_queryable_t qable; + if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to create queryable.\n"); return -1; } @@ -155,8 +175,8 @@ int main(int argc, char **argv) { 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)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/unix/c11/z_queryable_channel.c b/examples/unix/c11/z_queryable_channel.c index cc0637d70..05091d334 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -57,56 +57,71 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } - z_keyexpr_t ke = z_keyexpr(keyexpr); - if (!z_check(ke)) { + z_view_keyexpr_t ke; + if (z_view_keyexpr_from_string(&ke, keyexpr) < 0) { printf("%s is not a valid key expression", keyexpr); return -1; } printf("Creating Queryable on '%s'...\n", keyexpr); - z_owned_query_ring_channel_t channel = z_query_ring_channel_new(10); - z_owned_queryable_t qable = z_declare_queryable(z_loan(s), ke, z_move(channel.send), NULL); - if (!z_check(qable)) { + z_owned_query_ring_channel_t channel; + z_query_ring_channel_new(&channel, 10); + z_owned_queryable_t qable; + if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(channel.send), NULL) < 0) { printf("Unable to create queryable.\n"); return -1; } - z_owned_query_t query = z_query_null(); + z_owned_query_t query; + z_null(&query); for (z_call(channel.recv, &query); z_check(query); z_call(channel.recv, &query)) { - z_query_t q = z_loan(query); - z_owned_str_t keystr = z_keyexpr_to_string(z_query_keyexpr(&q)); - z_bytes_t pred = z_query_parameters(&q); - z_value_t payload_value = z_query_value(&q); - 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); + const z_loaned_query_t *q = z_loan(query); + z_owned_string_t keystr; + z_keyexpr_to_string(z_query_keyexpr(q), &keystr); + z_view_string_t params; + z_query_parameters(q, ¶ms); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), + (int)z_loan(params)->len, z_loan(params)->val); + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(z_loan(query))); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); } - z_query_reply_options_t options = z_query_reply_options_default(); - options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); - z_query_reply(&q, z_keyexpr(keyexpr), (const unsigned char *)value, strlen(value), &options); + z_query_reply_options_t options; + z_query_reply_options_default(&options); + // Reply value encoding + z_view_string_t reply_str; + z_view_string_wrap(&reply_str, value); + z_owned_bytes_t reply_payload; + z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + + z_query_reply(q, z_query_keyexpr(q), z_move(reply_payload), &options); z_drop(z_move(keystr)); z_drop(z_move(query)); } @@ -115,8 +130,8 @@ int main(int argc, char **argv) { 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)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/unix/c11/z_scout.c b/examples/unix/c11/z_scout.c index 3a6950aad..a39bd2226 100644 --- a/examples/unix/c11/z_scout.c +++ b/examples/unix/c11/z_scout.c @@ -41,26 +41,27 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { } } -void fprintlocators(FILE *stream, const z_str_array_t *locs) { +void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); + const z_loaned_string_t *str = z_string_array_get(locs, i); + fprintf(stream, "%.*s", (int)str->len, str->val); fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { + if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); } } fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, "Hello { zid: "); - fprintzid(stream, hello.zid); + fprintzid(stream, hello->zid); fprintf(stream, ", whatami: "); - fprintwhatami(stream, hello.whatami); + fprintwhatami(stream, hello->whatami); fprintf(stream, ", locators: "); - fprintlocators(stream, &hello.locators); + fprintlocators(stream, &hello->locators); fprintf(stream, " }"); } @@ -87,8 +88,10 @@ int main(int argc, char **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); + z_owned_scouting_config_t config; + z_scouting_config_default(&config); + z_owned_closure_hello_t closure; + z_closure(&closure, callback, drop, context); printf("Scouting...\n"); z_scout(z_move(config), z_move(closure)); sleep(1); diff --git a/examples/unix/c11/z_sub.c b/examples/unix/c11/z_sub.c index aaff86582..a91133fa6 100644 --- a/examples/unix/c11/z_sub.c +++ b/examples/unix/c11/z_sub.c @@ -32,12 +32,13 @@ int8_t attachment_handler(z_bytes_t key, z_bytes_t value, void *ctx) { } #endif -void data_handler(const z_sample_t *sample, void *ctx) { +void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t keystr = z_keyexpr_to_string(keyexpr); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)payload.len, payload.start); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)payload->len, + payload->start); #if Z_FEATURE_ATTACHMENT == 1 z_attachment_t attachment = z_sample_attachment(sample); if (z_attachment_check(&attachment)) { @@ -86,33 +87,37 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } - z_owned_closure_sample_t callback = z_closure(data_handler); + z_owned_closure_sample_t callback; + z_closure(&callback, 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)) { + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } @@ -128,8 +133,8 @@ int main(int argc, char **argv) { 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)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/unix/c11/z_sub_channel.c b/examples/unix/c11/z_sub_channel.c index eb2b2f3f9..02a793961 100644 --- a/examples/unix/c11/z_sub_channel.c +++ b/examples/unix/c11/z_sub_channel.c @@ -44,51 +44,55 @@ int main(int argc, char **argv) { } } - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, locator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } printf("Declaring Subscriber on '%s'...\n", keyexpr); - z_owned_sample_fifo_channel_t channel = z_sample_fifo_channel_new(3); - z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(channel.send), NULL); - if (!z_check(sub)) { + z_owned_sample_fifo_channel_t channel; + z_sample_fifo_channel_new(&channel, 3); + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(channel.send), NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } - z_owned_sample_t sample = z_sample_null(); + z_owned_sample_t sample; + z_null(&sample); for (z_call(channel.recv, &sample); z_check(sample); z_call(channel.recv, &sample)) { - z_sample_t loaned_sample = z_loan(sample); - z_keyexpr_t sample_key = z_sample_keyexpr(&loaned_sample); - z_bytes_t payload = z_sample_payload(&loaned_sample); - z_owned_str_t keystr = z_keyexpr_to_string(sample_key); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)payload.len, payload.start); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), + (int)z_sample_payload(z_loan(sample))->len, z_sample_payload(z_loan(sample))->start); z_drop(z_move(keystr)); z_drop(z_move(sample)); - sample = z_sample_null(); + z_null(&sample); } z_undeclare_subscriber(z_move(sub)); z_drop(z_move(channel)); // Stop read and lease tasks for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/unix/c11/z_sub_st.c b/examples/unix/c11/z_sub_st.c index b187f640d..97bcacfc0 100644 --- a/examples/unix/c11/z_sub_st.c +++ b/examples/unix/c11/z_sub_st.c @@ -23,12 +23,13 @@ static int msg_nb = 0; -void data_handler(const z_sample_t *sample, void *ctx) { +void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t keystr = z_keyexpr_to_string(keyexpr); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)payload.len, payload.start); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)payload->len, + payload->start); z_drop(z_move(keystr)); msg_nb++; } @@ -70,26 +71,30 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); return -1; } - z_owned_closure_sample_t callback = z_closure(data_handler); + z_owned_closure_sample_t callback; + z_closure(&callback, 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)) { + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } diff --git a/examples/unix/c11/z_sub_thr.c b/examples/unix/c11/z_sub_thr.c index 788b25b03..0a7829489 100644 --- a/examples/unix/c11/z_sub_thr.c +++ b/examples/unix/c11/z_sub_thr.c @@ -39,7 +39,7 @@ z_stats_t *z_stats_make(void) { return stats; } -void on_sample(const z_sample_t *sample, void *context) { +void on_sample(const z_loaned_sample_t *sample, void *context) { (void)sample; z_stats_t *stats = (z_stats_t *)context; stats->count++; @@ -69,32 +69,36 @@ void drop_stats(void *context) { int main(int argc, char **argv) { char *keyexpr = "test/thr"; - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); // Set config if (argc > 1) { - if (zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[1])) < 0) { + if (zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, argv[1]) < 0) { printf("Failed to insert locator in config: %s\n", argv[1]); exit(-1); } } // Open session - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); exit(-1); } // Declare Subscriber/resource z_stats_t *context = z_stats_make(); - z_owned_closure_sample_t callback = z_closure(on_sample, drop_stats, (void *)context); - z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL); - if (!z_check(sub)) { + z_owned_closure_sample_t callback; + z_closure(&callback, on_sample, drop_stats, (void *)context); + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to create subscriber.\n"); exit(-1); } @@ -106,8 +110,8 @@ int main(int argc, char **argv) { // Clean up z_undeclare_subscriber(z_move(sub)); - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); exit(0); } diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 8731d3903..e11827800 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -29,13 +29,19 @@ void reply_dropper(void *ctx) { z_condvar_free(&cond); } -void reply_handler(z_owned_reply_t *reply, void *ctx) { +void reply_handler(const z_loaned_reply_t *reply, void *ctx) { (void)(ctx); if (z_reply_is_ok(reply)) { - z_loaned_sample_t sample = z_reply_ok(reply); - z_owned_str_t keystr = z_keyexpr_to_string(sample.keyexpr); - printf(">> Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)sample.payload.len, sample.payload.start); - z_str_drop(z_str_move(&keystr)); + const z_loaned_sample_t *sample = z_reply_ok(reply); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)), + z_string_data(z_string_loan(&replystr))); + z_string_drop(z_string_move(&keystr)); + z_string_drop(z_string_move(&replystr)); } else { printf(">> Received an error\n"); } @@ -81,43 +87,51 @@ int main(int argc, char **argv) { z_mutex_init(&mutex); z_condvar_init(&cond); - z_owned_config_t config = z_config_default(); - zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); return -1; } // Start read and lease tasks for zenoh-pico - if (zp_start_read_task(z_session_loan(&s), NULL) < 0 || zp_start_lease_task(z_session_loan(&s), NULL) < 0) { + if (zp_start_read_task(z_session_loan_mut(&s), NULL) < 0 || zp_start_lease_task(z_session_loan_mut(&s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } - z_keyexpr_t ke = z_keyexpr(keyexpr); - if (!z_keyexpr_is_initialized(&ke)) { + z_view_keyexpr_t ke; + if (z_view_keyexpr_from_string(&ke, keyexpr) < 0) { printf("%s is not a valid key expression", keyexpr); return -1; } z_mutex_lock(&mutex); printf("Sending Query '%s'...\n", keyexpr); - z_get_options_t opts = z_get_options_default(); + z_get_options_t opts; + z_get_options_default(&opts); + // Value encoding if (value != NULL) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + z_view_string_t value_str; + z_view_string_wrap(&value_str, value); + z_owned_bytes_t payload; + z_bytes_encode_from_string(&payload, z_view_string_loan(&value_str)); + opts.payload = &payload; } - 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) { + z_owned_closure_reply_t callback; + z_closure_reply(&callback, reply_handler, reply_dropper, NULL); + if (z_get(z_session_loan(&s), z_view_keyexpr_loan(&ke), "", z_closure_reply_move(&callback), &opts) < 0) { printf("Unable to send query.\n"); return -1; } @@ -125,8 +139,8 @@ int main(int argc, char **argv) { z_mutex_unlock(&mutex); // Stop read and lease tasks for zenoh-pico - zp_stop_read_task(z_session_loan(&s)); - zp_stop_lease_task(z_session_loan(&s)); + zp_stop_read_task(z_session_loan_mut(&s)); + zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); diff --git a/examples/unix/c99/z_info.c b/examples/unix/c99/z_info.c index c978af3b4..ba531e63f 100644 --- a/examples/unix/c99/z_info.c +++ b/examples/unix/c99/z_info.c @@ -56,24 +56,25 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); return -1; } // Start read and lease tasks for zenoh-pico - if (zp_start_read_task(z_session_loan(&s), NULL) < 0 || zp_start_lease_task(z_session_loan(&s), NULL) < 0) { + if (zp_start_read_task(z_session_loan_mut(&s), NULL) < 0 || zp_start_lease_task(z_session_loan_mut(&s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; @@ -84,18 +85,20 @@ int main(int argc, char **argv) { print_zid(&self_id, NULL); printf("Routers IDs:\n"); - z_owned_closure_zid_t callback = z_closure_zid(print_zid, NULL, NULL); + z_owned_closure_zid_t callback; + z_closure_zid(&callback, print_zid, NULL, NULL); z_info_routers_zid(z_session_loan(&s), z_closure_zid_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_zid(print_zid, NULL, NULL); + z_owned_closure_zid_t callback2; + z_closure_zid(&callback2, print_zid, NULL, NULL); z_info_peers_zid(z_session_loan(&s), z_closure_zid_move(&callback2)); // Stop read and lease tasks for zenoh-pico - zp_stop_read_task(z_session_loan(&s)); - zp_stop_lease_task(z_session_loan(&s)); + zp_stop_read_task(z_session_loan_mut(&s)); + zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); } diff --git a/examples/unix/c99/z_ping.c b/examples/unix/c99/z_ping.c index 673943898..17fe7833c 100644 --- a/examples/unix/c99/z_ping.c +++ b/examples/unix/c99/z_ping.c @@ -31,7 +31,7 @@ static z_condvar_t cond; static z_mutex_t mutex; -void callback(const z_sample_t* sample, void* context) { +void callback(const z_loaned_sample_t* sample, void* context) { (void)sample; (void)context; z_condvar_signal(&cond); @@ -64,32 +64,37 @@ 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_config_move(&config)); + z_owned_config_t config; + z_config_default(&config); + z_owned_session_t session; + z_open(&session, 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) { + if (zp_start_read_task(z_session_loan_mut(&session), NULL) < 0 || + zp_start_lease_task(z_session_loan_mut(&session), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&session)); return -1; } - z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); - z_owned_publisher_t pub = z_declare_publisher(z_session_loan(&session), ping, NULL); - if (!z_publisher_check(&pub)) { + z_view_keyexpr_t ping; + z_view_keyexpr_from_string_unchecked(&ping, "test/ping"); + z_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_session_loan(&session), z_view_keyexpr_loan(&ping), NULL) < 0) { 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)) { + z_view_keyexpr_t pong; + z_view_keyexpr_from_string_unchecked(&pong, "test/pong"); + z_owned_closure_sample_t respond; + z_closure_sample(&respond, callback, drop, NULL); + z_owned_subscriber_t sub; + if (z_declare_subscriber(&sub, z_session_loan(&session), z_view_keyexpr_loan(&pong), + z_closure_sample_move(&respond), NULL) < 0) { printf("Unable to declare subscriber for key expression.\n"); return -1; } @@ -125,8 +130,8 @@ int main(int argc, char** argv) { 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)); + zp_stop_read_task(z_session_loan_mut(&session)); + zp_stop_lease_task(z_session_loan_mut(&session)); z_close(z_session_move(&session)); } diff --git a/examples/unix/c99/z_pong.c b/examples/unix/c99/z_pong.c index 3b627218b..533dc369a 100644 --- a/examples/unix/c99/z_pong.c +++ b/examples/unix/c99/z_pong.c @@ -17,10 +17,10 @@ #include "zenoh-pico/api/primitives.h" #if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1 -void callback(const z_sample_t* sample, void* context) { - z_publisher_t pub = z_publisher_loan((z_owned_publisher_t*)context); - z_bytes_t payload = z_sample_payload(sample); - z_publisher_put(pub, payload.start, payload.len, NULL); +void callback(const z_loaned_sample_t* sample, void* context) { + const z_loaned_publisher_t* pub = z_publisher_loan((z_owned_publisher_t*)context); + const z_loaned_bytes_t* payload = z_sample_payload(sample); + z_publisher_put(pub, payload->start, payload->len, NULL); } void drop(void* context) { z_owned_publisher_t* pub = (z_owned_publisher_t*)context; @@ -34,32 +34,37 @@ void drop(void* context) { 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_config_move(&config)); + z_owned_config_t config; + z_config_default(&config); + z_owned_session_t session; + z_open(&session, 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) { + if (zp_start_read_task(z_session_loan_mut(&session), NULL) < 0 || + zp_start_lease_task(z_session_loan_mut(&session), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&session)); 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); - if (!z_publisher_check(&pub)) { + z_view_keyexpr_t pong; + z_view_keyexpr_from_string_unchecked(&pong, "test/pong"); + z_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_session_loan(&session), z_view_keyexpr_loan(&pong), NULL) < 0) { 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)) { + z_view_keyexpr_t ping; + z_view_keyexpr_from_string_unchecked(&ping, "test/ping"); + z_owned_closure_sample_t respond; + z_closure_sample(&respond, callback, drop, (void*)z_publisher_move(&pub)); + z_owned_subscriber_t sub; + if (z_declare_subscriber(&sub, z_session_loan(&session), z_view_keyexpr_loan(&ping), + z_closure_sample_move(&respond), NULL) < 0) { printf("Unable to declare subscriber for key expression.\n"); return -1; } @@ -69,8 +74,8 @@ int main(int argc, char** argv) { z_undeclare_subscriber(z_subscriber_move(&sub)); - zp_stop_read_task(z_session_loan(&session)); - zp_stop_lease_task(z_session_loan(&session)); + zp_stop_read_task(z_session_loan_mut(&session)); + zp_stop_lease_task(z_session_loan_mut(&session)); z_close(z_session_move(&session)); } diff --git a/examples/unix/c99/z_pub.c b/examples/unix/c99/z_pub.c index a81065c9e..91e5799c2 100644 --- a/examples/unix/c99/z_pub.c +++ b/examples/unix/c99/z_pub.c @@ -62,32 +62,35 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); return -1; } // Start read and lease tasks for zenoh-pico - if (zp_start_read_task(z_session_loan(&s), NULL) < 0 || zp_start_lease_task(z_session_loan(&s), NULL) < 0) { + if (zp_start_read_task(z_session_loan_mut(&s), NULL) < 0 || zp_start_lease_task(z_session_loan_mut(&s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } printf("Declaring publisher for '%s'...\n", keyexpr); - z_owned_publisher_t pub = z_declare_publisher(z_session_loan(&s), z_keyexpr(keyexpr), NULL); - if (!z_publisher_check(&pub)) { + z_owned_publisher_t pub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_publisher(&pub, z_session_loan(&s), z_view_keyexpr_loan(&ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return -1; } @@ -95,18 +98,23 @@ int main(int argc, char **argv) { printf("Press CTRL-C to quit...\n"); char *buf = (char *)malloc(256); for (int idx = 0; idx < n; ++idx) { - sleep(1); + z_sleep_s(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); + // Create encoding + z_owned_encoding_t encoding; + zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); + z_publisher_put_options_t options; + z_publisher_put_options_default(&options); + options.encoding = z_encoding_move(&encoding); + z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), &options); } // Clean up z_undeclare_publisher(z_publisher_move(&pub)); - zp_stop_read_task(z_session_loan(&s)); - zp_stop_lease_task(z_session_loan(&s)); + zp_stop_read_task(z_session_loan_mut(&s)); + zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); return 0; } diff --git a/examples/unix/c99/z_pub_st.c b/examples/unix/c99/z_pub_st.c index f39b3753b..d5d8220d8 100644 --- a/examples/unix/c99/z_pub_st.c +++ b/examples/unix/c99/z_pub_st.c @@ -62,25 +62,28 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); return -1; } printf("Declaring publisher for '%s'...\n", keyexpr); - z_owned_publisher_t pub = z_declare_publisher(z_session_loan(&s), z_keyexpr(keyexpr), NULL); - if (!z_publisher_check(&pub)) { + z_owned_publisher_t pub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_publisher(&pub, z_session_loan(&s), z_view_keyexpr_loan(&ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return -1; } diff --git a/examples/unix/c99/z_pull.c b/examples/unix/c99/z_pull.c index 72cc35216..460389878 100644 --- a/examples/unix/c99/z_pull.c +++ b/examples/unix/c99/z_pull.c @@ -20,12 +20,14 @@ #if Z_FEATURE_SUBSCRIPTION == 1 // @TODO -// void data_handler(const z_sample_t *sample, void *ctx) { +// void data_handler(const z_loaned_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_str_loan(&keystr), (int)sample->payload.len, +// z_owned_string_t keystr; +// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)), +// (int)sample->payload.len, // sample->payload.start); -// z_str_drop(z_str_move(&keystr)); +// z_string_drop(z_string_move(&keystr)); // } int main(int argc, char **argv) { @@ -53,31 +55,33 @@ int main(int argc, char **argv) { } } - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); if (locator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, locator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); return -1; } // Start read and lease tasks for zenoh-pico - if (zp_start_read_task(z_session_loan(&s), NULL) < 0 || zp_start_lease_task(z_session_loan(&s), NULL) < 0) { + if (zp_start_read_task(z_session_loan_mut(&s), NULL) < 0 || zp_start_lease_task(z_session_loan_mut(&s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } // @TODO - // z_owned_closure_sample_t callback = z_closure_sample(data_handler, NULL, NULL); + // z_owned_closure_sample_t callback; + // z_closure_sample(&callback, data_handler, NULL, NULL); printf("Declaring Subscriber on '%s'...\n", keyexpr); // @TODO // z_owned_pull_subscriber_t sub = - // z_declare_pull_subscriber(z_session_loan(&s), z_keyexpr(keyexpr), z_closure_sample_move(&callback), NULL); + // z_declare_pull_subscriber(z_session_loan(&s), z_loan(ke), z_closure_sample_move(&callback), NULL); // if (!z_pull_subscriber_check(&sub)) { // printf("Unable to declare subscriber.\n"); // return -1; @@ -99,8 +103,8 @@ int main(int argc, char **argv) { printf("Pull Subscriber not supported... exiting\n"); // Stop read and lease tasks for zenoh-pico - zp_stop_read_task(z_session_loan(&s)); - zp_stop_lease_task(z_session_loan(&s)); + zp_stop_read_task(z_session_loan_mut(&s)); + zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); diff --git a/examples/unix/c99/z_put.c b/examples/unix/c99/z_put.c index 453444869..701432fa6 100644 --- a/examples/unix/c99/z_put.c +++ b/examples/unix/c99/z_put.c @@ -57,50 +57,58 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); return -1; } // Start read and lease tasks for zenoh-pico - if (zp_start_read_task(z_session_loan(&s), NULL) < 0 || zp_start_lease_task(z_session_loan(&s), NULL) < 0) { + if (zp_start_read_task(z_session_loan_mut(&s), NULL) < 0 || zp_start_lease_task(z_session_loan_mut(&s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } printf("Declaring key expression '%s'...\n", keyexpr); - z_owned_keyexpr_t ke = z_declare_keyexpr(z_session_loan(&s), z_keyexpr(keyexpr)); - if (!z_keyexpr_check(&ke)) { + z_view_keyexpr_t vke; + z_view_keyexpr_from_string(&vke, keyexpr); + z_owned_keyexpr_t ke; + if (z_declare_keyexpr(&ke, z_session_loan(&s), z_view_keyexpr_loan(&vke)) < 0) { printf("Unable to declare key expression!\n"); - zp_stop_read_task(z_session_loan(&s)); - zp_stop_lease_task(z_session_loan(&s)); + zp_stop_read_task(z_session_loan_mut(&s)); + zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); return -1; } + // Create encoding + z_owned_encoding_t encoding; + zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); + 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); + z_put_options_t options; + z_put_options_default(&options); + options.encoding = z_encoding_move(&encoding); if (z_put(z_session_loan(&s), z_keyexpr_loan(&ke), (const uint8_t *)value, strlen(value), &options) < 0) { printf("Oh no! Put has failed...\n"); } // Clean up z_undeclare_keyexpr(z_session_loan(&s), z_keyexpr_move(&ke)); - zp_stop_read_task(z_session_loan(&s)); - zp_stop_lease_task(z_session_loan(&s)); + zp_stop_read_task(z_session_loan_mut(&s)); + zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); return 0; } diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index c6ccf555c..9bdb4a910 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -22,15 +22,37 @@ 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 query_handler(const z_loaned_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_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_str_drop(z_str_move(&keystr)); + z_owned_string_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t params; + z_query_parameters(query, ¶ms); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_string_loan(&keystr)), + (int)z_view_string_loan(¶ms)->len, z_view_string_loan(¶ms)->val); + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_string_data(z_string_loan(&payload_string))); + z_string_drop(z_string_move(&payload_string)); + } + // Create encoding + z_owned_encoding_t encoding; + zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); + + z_query_reply_options_t options; + z_query_reply_options_default(&options); + options.encoding = z_encoding_move(&encoding); + // Reply value encoding + z_view_string_t reply_str; + z_view_string_wrap(&reply_str, value); + z_owned_bytes_t reply_payload; + z_bytes_encode_from_string(&reply_payload, z_view_string_loan(&reply_str)); + + z_query_reply(query, z_query_keyexpr(query), z_bytes_move(&reply_payload), &options); + z_string_drop(z_string_move(&keystr)); } int main(int argc, char **argv) { @@ -68,39 +90,43 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); return -1; } // Start read and lease tasks for zenoh-pico - if (zp_start_read_task(z_session_loan(&s), NULL) < 0 || zp_start_lease_task(z_session_loan(&s), NULL) < 0) { + if (zp_start_read_task(z_session_loan_mut(&s), NULL) < 0 || zp_start_lease_task(z_session_loan_mut(&s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } - z_keyexpr_t ke = z_keyexpr(keyexpr); - if (!z_keyexpr_is_initialized(&ke)) { + z_view_keyexpr_t ke; + if (z_view_keyexpr_from_string(&ke, keyexpr) < 0) { printf("%s is not a valid key expression", keyexpr); return -1; } + z_owned_closure_query_t callback; + z_closure_query(&callback, query_handler, NULL, NULL); + printf("Creating Queryable on '%s'...\n", keyexpr); - z_owned_closure_query_t callback = z_closure_query(query_handler, NULL, NULL); - z_owned_queryable_t qable = z_declare_queryable(z_session_loan(&s), ke, z_closure_query_move(&callback), NULL); - if (!z_queryable_check(&qable)) { + z_owned_queryable_t qable; + if (z_declare_queryable(&qable, z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_query_move(&callback), + NULL) < 0) { printf("Unable to create queryable.\n"); return -1; } @@ -113,8 +139,8 @@ int main(int argc, char **argv) { z_undeclare_queryable(z_queryable_move(&qable)); // Stop read and lease tasks for zenoh-pico - zp_stop_read_task(z_session_loan(&s)); - zp_stop_lease_task(z_session_loan(&s)); + zp_stop_read_task(z_session_loan_mut(&s)); + zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); diff --git a/examples/unix/c99/z_scout.c b/examples/unix/c99/z_scout.c index e9453bf96..d94c278f4 100644 --- a/examples/unix/c99/z_scout.c +++ b/examples/unix/c99/z_scout.c @@ -40,26 +40,27 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { } } -void fprintlocators(FILE *stream, const z_str_array_t *locs) { +void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); + const z_loaned_string_t *str = z_string_array_get(locs, i); + fprintf(stream, "%.*s", (int)str->len, str->val); fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { + if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); } } fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, "Hello { zid: "); - fprintzid(stream, hello.zid); + fprintzid(stream, hello->zid); fprintf(stream, ", whatami: "); - fprintwhatami(stream, hello.whatami); + fprintwhatami(stream, hello->whatami); fprintf(stream, ", locators: "); - fprintlocators(stream, &hello.locators); + fprintlocators(stream, &hello->locators); fprintf(stream, " }"); } @@ -86,8 +87,10 @@ int main(int argc, char **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_hello(callback, drop, context); + z_owned_scouting_config_t config; + z_scouting_config_default(&config); + z_owned_closure_hello_t closure; + z_closure_hello(&closure, callback, drop, context); printf("Scouting...\n"); z_scout(z_scouting_config_move(&config), z_closure_hello_move(&closure)); sleep(1); diff --git a/examples/unix/c99/z_sub.c b/examples/unix/c99/z_sub.c index b50a58ccc..3495d167a 100644 --- a/examples/unix/c99/z_sub.c +++ b/examples/unix/c99/z_sub.c @@ -20,13 +20,14 @@ #include #if Z_FEATURE_SUBSCRIPTION == 1 -void data_handler(const z_sample_t *sample, void *arg) { +void data_handler(const z_loaned_sample_t *sample, void *arg) { (void)(arg); - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t keystr = z_keyexpr_to_string(keyexpr); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)payload.len, payload.start); - z_str_drop(z_str_move(&keystr)); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)), (int)payload->len, + payload->start); + z_string_drop(z_string_move(&keystr)); } int main(int argc, char **argv) { @@ -62,34 +63,39 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); return -1; } // Start read and lease tasks for zenoh-pico - if (zp_start_read_task(z_session_loan(&s), NULL) < 0 || zp_start_lease_task(z_session_loan(&s), NULL) < 0) { + if (zp_start_read_task(z_session_loan_mut(&s), NULL) < 0 || zp_start_lease_task(z_session_loan_mut(&s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } - z_owned_closure_sample_t callback = z_closure_sample(data_handler, NULL, NULL); + z_owned_closure_sample_t callback; + z_closure_sample(&callback, data_handler, NULL, NULL); + printf("Declaring Subscriber on '%s'...\n", keyexpr); - z_owned_subscriber_t sub = - z_declare_subscriber(z_session_loan(&s), z_keyexpr(keyexpr), z_closure_sample_move(&callback), NULL); - if (!z_subscriber_check(&sub)) { + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_subscriber(&sub, z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_sample_move(&callback), + NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } @@ -102,8 +108,8 @@ int main(int argc, char **argv) { z_undeclare_subscriber(z_subscriber_move(&sub)); // Stop read and lease tasks for zenoh-pico - zp_stop_read_task(z_session_loan(&s)); - zp_stop_lease_task(z_session_loan(&s)); + zp_stop_read_task(z_session_loan_mut(&s)); + zp_stop_lease_task(z_session_loan_mut(&s)); z_close(z_session_move(&s)); diff --git a/examples/unix/c99/z_sub_st.c b/examples/unix/c99/z_sub_st.c index d09033c38..c83d059ba 100644 --- a/examples/unix/c99/z_sub_st.c +++ b/examples/unix/c99/z_sub_st.c @@ -23,13 +23,14 @@ static int msg_nb = 0; -void data_handler(const z_sample_t *sample, void *arg) { +void data_handler(const z_loaned_sample_t *sample, void *arg) { (void)(arg); - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t keystr = z_keyexpr_to_string(keyexpr); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)payload.len, payload.start); - z_str_drop(z_str_move(&keystr)); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)), (int)payload->len, + payload->start); + z_string_drop(z_string_move(&keystr)); msg_nb++; } @@ -70,27 +71,32 @@ 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, mode); if (clocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, clocator); } if (llocator != NULL) { - zp_config_insert(z_config_loan(&config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_LISTEN_KEY, llocator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_config_move(&config)); - if (!z_session_check(&s)) { + z_owned_session_t s; + if (z_open(&s, z_config_move(&config)) < 0) { printf("Unable to open session!\n"); return -1; } - z_owned_closure_sample_t callback = z_closure_sample(data_handler, NULL, NULL); + z_owned_closure_sample_t callback; + z_closure_sample(&callback, data_handler, NULL, NULL); + printf("Declaring Subscriber on '%s'...\n", keyexpr); - z_owned_subscriber_t sub = - z_declare_subscriber(z_session_loan(&s), z_keyexpr(keyexpr), z_closure_sample_move(&callback), NULL); - if (!z_subscriber_check(&sub)) { + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_subscriber(&sub, z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_sample_move(&callback), + NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index 35cadd7be..42fdfea55 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -28,13 +28,18 @@ void reply_dropper(void *ctx) { z_condvar_free(&cond); } -void reply_handler(z_owned_reply_t *reply, void *ctx) { +void reply_handler(const z_loaned_reply_t *reply, void *ctx) { (void)(ctx); if (z_reply_is_ok(reply)) { - z_loaned_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); + const z_loaned_sample_t *sample = z_reply_ok(reply); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); } @@ -50,39 +55,47 @@ int main(int argc, char **argv) { z_mutex_init(&mutex); z_condvar_init(&cond); - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, locator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } - z_keyexpr_t ke = z_keyexpr(keyexpr); - if (!z_check(ke)) { + z_view_keyexpr_t ke; + if (z_view_keyexpr_from_string(&ke, keyexpr) < 0) { printf("%s is not a valid key expression", keyexpr); return -1; } z_mutex_lock(&mutex); printf("Sending Query '%s'...\n", keyexpr); - z_get_options_t opts = z_get_options_default(); + z_get_options_t opts; + z_get_options_default(&opts); + // Value encoding if (value != NULL) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + z_view_string_t value_str; + z_view_string_wrap(&value_str, value); + z_owned_bytes_t payload; + z_bytes_encode_from_string(&payload, z_loan(value_str)); + opts.payload = &payload; } - z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); - if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { + z_owned_closure_reply_t callback; + z_closure(&callback, reply_handler, reply_dropper); + if (z_get(z_loan(s), z_loan(ke), "", z_move(callback), &opts) < 0) { printf("Unable to send query.\n"); return -1; } @@ -90,8 +103,8 @@ int main(int argc, char **argv) { z_mutex_unlock(&mutex); // Stop read and lease tasks for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/windows/z_info.c b/examples/windows/z_info.c index 7967e1aa2..0a8151e62 100644 --- a/examples/windows/z_info.c +++ b/examples/windows/z_info.c @@ -32,21 +32,22 @@ int main(int argc, char **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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, locator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; @@ -57,18 +58,20 @@ int main(int argc, char **argv) { print_zid(&self_id, NULL); printf("Routers IDs:\n"); - z_owned_closure_zid_t callback = z_closure(print_zid); + z_owned_closure_zid_t callback; + z_closure(&callback, 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_owned_closure_zid_t callback2; + z_closure(&callback2, 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)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); } diff --git a/examples/windows/z_ping.c b/examples/windows/z_ping.c index 262e21659..8dfd075ca 100644 --- a/examples/windows/z_ping.c +++ b/examples/windows/z_ping.c @@ -30,7 +30,7 @@ static z_condvar_t cond; static z_mutex_t mutex; -void callback(const z_sample_t* sample, void* context) { +void callback(const z_loaned_sample_t* sample, void* context) { (void)sample; (void)context; z_condvar_signal(&cond); @@ -63,30 +63,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)); - if (!z_check(session)) { + z_owned_config_t config; + z_config_default(&config); + z_owned_session_t session; + if (z_open(&session, z_move(config)) < 0) { 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) { + if (zp_start_read_task(z_loan_mut(session), NULL) < 0 || zp_start_lease_task(z_loan_mut(session), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&session)); return -1; } - z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); - z_owned_publisher_t pub = z_declare_publisher(z_loan(session), ping, NULL); - if (!z_check(pub)) { + z_view_keyexpr_t ping; + z_view_keyexpr_from_string_unchecked(&ping, "test/ping"); + z_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_loan(session), z_loan(ping), NULL) < 0) { 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)) { + z_view_keyexpr_t pong; + z_view_keyexpr_from_string_unchecked(&pong, "test/pong"); + z_owned_closure_sample_t respond; + z_closure(&respond, callback, drop, NULL); + z_owned_subscriber_t sub; + if (z_declare_subscriber(&sub, z_loan(session), z_loan(pong), z_move(respond), NULL) < 0) { printf("Unable to declare subscriber for key expression.\n"); return -1; } @@ -122,8 +126,8 @@ int main(int argc, char** argv) { z_drop(z_move(pub)); z_drop(z_move(sub)); - zp_stop_read_task(z_loan(session)); - zp_stop_lease_task(z_loan(session)); + zp_stop_read_task(z_loan_mut(session)); + zp_stop_lease_task(z_loan_mut(session)); z_close(z_move(session)); } diff --git a/examples/windows/z_pong.c b/examples/windows/z_pong.c index b990e21c2..8be8fb256 100644 --- a/examples/windows/z_pong.c +++ b/examples/windows/z_pong.c @@ -16,10 +16,10 @@ #include "zenoh-pico.h" #if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1 -void callback(const z_sample_t* sample, void* context) { - z_publisher_t pub = z_loan(*(z_owned_publisher_t*)context); - z_bytes_t payload = z_sample_payload(sample); - z_publisher_put(pub, payload.start, payload.len, NULL); +void callback(const z_loaned_sample_t* sample, void* context) { + const z_loaned_publisher_t* pub = z_loan(*(z_owned_publisher_t*)context); + const z_loaned_bytes_t* payload = z_sample_payload(sample); + z_publisher_put(pub, payload->start, payload->len, NULL); } void drop(void* context) { z_owned_publisher_t* pub = (z_owned_publisher_t*)context; @@ -33,30 +33,34 @@ void drop(void* context) { 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)); - if (!z_check(session)) { + z_owned_config_t config; + z_config_default(&config); + z_owned_session_t session; + if (z_open(&session, z_move(config)) < 0) { 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) { + if (zp_start_read_task(z_loan_mut(session), NULL) < 0 || zp_start_lease_task(z_loan_mut(session), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&session)); 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)) { + z_view_keyexpr_t pong; + z_view_keyexpr_from_string_unchecked(&pong, "test/pong"); + z_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_loan(session), z_loan(pong), NULL) < 0) { 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)) { + z_view_keyexpr_t ping; + z_view_keyexpr_from_string_unchecked(&ping, "test/ping"); + z_owned_closure_sample_t respond; + z_closure(&respond, callback, drop, (void*)z_move(pub)); + z_owned_subscriber_t sub; + if (z_declare_subscriber(&sub, z_loan(session), z_loan(ping), z_move(respond), NULL) < 0) { printf("Unable to declare subscriber for key expression.\n"); return -1; } @@ -66,8 +70,8 @@ int main(int argc, char** argv) { z_drop(z_move(sub)); - zp_stop_read_task(z_loan(session)); - zp_stop_lease_task(z_loan(session)); + zp_stop_read_task(z_loan_mut(session)); + zp_stop_lease_task(z_loan_mut(session)); z_close(z_move(session)); } diff --git a/examples/windows/z_pub.c b/examples/windows/z_pub.c index ce62da93d..231edeb66 100644 --- a/examples/windows/z_pub.c +++ b/examples/windows/z_pub.c @@ -27,29 +27,32 @@ int main(int argc, char **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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, locator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); - z_close(z_session_move(&s)); + z_close(z_move(s)); 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)) { + z_owned_publisher_t pub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return -1; } @@ -57,23 +60,25 @@ int main(int argc, char **argv) { printf("Press CTRL-C to quit...\n"); char *buf = (char *)malloc(256); for (int idx = 0; 1; ++idx) { - Sleep(1); + z_sleep_s(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); + // Create encoding + z_owned_encoding_t encoding; + zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); + z_publisher_put_options_t options; + z_publisher_put_options_default(&options); + options.encoding = z_move(encoding); + z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); } + // Clean-up 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)); - + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(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 index 6d6f59d87..959fb2b6f 100644 --- a/examples/windows/z_pub_st.c +++ b/examples/windows/z_pub_st.c @@ -29,22 +29,25 @@ int main(int argc, char **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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, locator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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)) { + z_owned_publisher_t pub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return -1; } diff --git a/examples/windows/z_pull.c b/examples/windows/z_pull.c index 515b5e618..9256cf4d9 100644 --- a/examples/windows/z_pull.c +++ b/examples/windows/z_pull.c @@ -19,10 +19,11 @@ #if Z_FEATURE_SUBSCRIPTION == 1 // @TODO -// void data_handler(const z_sample_t *sample, void *ctx) { +// void data_handler(const z_loaned_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, +// z_owned_string_t keystr; +// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)sample->payload.len, // sample->payload.start); // z_drop(z_move(keystr)); // } @@ -33,30 +34,32 @@ int main(int argc, char **argv) { const char *keyexpr = "demo/example/**"; char *locator = NULL; - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, locator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } // @TODO - // z_owned_closure_sample_t callback = z_closure(data_handler); + // z_owned_closure_sample_t callback; + // z_closure(&callback, data_handler); printf("Declaring Subscriber on '%s'...\n", keyexpr); // @TODO - // z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL); + // z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_loan(ke), z_move(callback), NULL); // if (!z_check(sub)) { // printf("Unable to declare subscriber.\n"); // return -1; @@ -77,8 +80,8 @@ int main(int argc, char **argv) { printf("Pull Subscriber not supported... exiting\n"); // Stop read and lease tasks for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/windows/z_put.c b/examples/windows/z_put.c index 32f7802a7..e3c9b5982 100644 --- a/examples/windows/z_put.c +++ b/examples/windows/z_put.c @@ -27,47 +27,54 @@ int main(int argc, char **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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, locator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); 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)) { + z_view_keyexpr_t vke; + z_view_keyexpr_from_string(&vke, keyexpr); + z_owned_keyexpr_t ke; + if (z_declare_keyexpr(&ke, z_loan(s), z_loan(vke)) < 0) { printf("Unable to declare key expression!\n"); - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); return -1; } + // Create encoding + z_owned_encoding_t encoding; + zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); 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); + z_put_options_t options; + z_put_options_default(&options); + options.encoding = z_move(encoding); if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)value, strlen(value), &options) < 0) { printf("Oh no! Put has failed...\n"); } // Clean up z_undeclare_keyexpr(z_loan(s), z_move(ke)); - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); return 0; } diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index ea504d0e7..a6130c3b7 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -21,18 +21,38 @@ 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 query_handler(const z_loaned_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_owned_string_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + + z_view_string_t params; + z_query_parameters(query, ¶ms); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, + z_loan(params)->val); + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); } - 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); + // Create encoding + z_owned_encoding_t encoding; + zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); + + z_query_reply_options_t options; + z_query_reply_options_default(&options); + options.encoding = z_move(encoding); + // Reply value encoding + z_view_string_t reply_str; + z_view_string_wrap(&reply_str, value); + z_owned_bytes_t reply_payload; + z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + + z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); + z_drop(z_move(keystr)); } @@ -41,35 +61,37 @@ int main(int argc, char **argv) { (void)(argv); char *locator = NULL; - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, locator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } - z_keyexpr_t ke = z_keyexpr(keyexpr); - if (!z_check(ke)) { + z_view_keyexpr_t ke; + if (z_view_keyexpr_from_string(&ke, keyexpr) < 0) { 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)) { + z_owned_closure_query_t callback; + z_closure(&callback, query_handler); + z_owned_queryable_t qable; + if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to create queryable.\n"); return -1; } @@ -82,8 +104,8 @@ int main(int argc, char **argv) { 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)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/windows/z_scout.c b/examples/windows/z_scout.c index 378a61327..dfd993637 100644 --- a/examples/windows/z_scout.c +++ b/examples/windows/z_scout.c @@ -40,26 +40,27 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { } } -void fprintlocators(FILE *stream, const z_str_array_t *locs) { +void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); + const z_loaned_string_t *str = z_string_array_get(locs, i); + fprintf(stream, "%.*s", (int)str->len, str->val); fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { + if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); } } fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, "Hello { zid: "); - fprintzid(stream, hello.zid); + fprintzid(stream, hello->zid); fprintf(stream, ", whatami: "); - fprintwhatami(stream, hello.whatami); + fprintwhatami(stream, hello->whatami); fprintf(stream, ", locators: "); - fprintlocators(stream, &hello.locators); + fprintlocators(stream, &hello->locators); fprintf(stream, " }"); } @@ -85,8 +86,10 @@ int main(int argc, char **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); + z_owned_scouting_config_t config; + z_scouting_config_default(&config); + z_owned_closure_hello_t closure; + z_closure(&closure, callback, drop, context); printf("Scouting...\n"); z_scout(z_move(config), z_move(closure)); Sleep(1); diff --git a/examples/windows/z_sub.c b/examples/windows/z_sub.c index 3c1e154db..5ca826633 100644 --- a/examples/windows/z_sub.c +++ b/examples/windows/z_sub.c @@ -19,12 +19,13 @@ #include #if Z_FEATURE_SUBSCRIPTION == 1 -void data_handler(const z_sample_t *sample, void *ctx) { +void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t keystr = z_keyexpr_to_string(keyexpr); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)payload.len, payload.start); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)payload->len, + payload->start); z_drop(z_move(keystr)); } @@ -35,30 +36,34 @@ int main(int argc, char **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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, locator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } - z_owned_closure_sample_t callback = z_closure(data_handler); + z_owned_closure_sample_t callback; + z_closure(&callback, 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)) { + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } @@ -71,8 +76,8 @@ int main(int argc, char **argv) { 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)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); diff --git a/examples/windows/z_sub_st.c b/examples/windows/z_sub_st.c index 55ee53e09..5aed70bdc 100644 --- a/examples/windows/z_sub_st.c +++ b/examples/windows/z_sub_st.c @@ -22,12 +22,13 @@ int msg_nb = 0; #if Z_FEATURE_SUBSCRIPTION == 1 -void data_handler(const z_sample_t *sample, void *ctx) { +void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t keystr = z_keyexpr_to_string(keyexpr); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)payload.len, payload.start); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)payload->len, + payload->start); z_drop(z_move(keystr)); msg_nb++; @@ -40,23 +41,27 @@ int main(int argc, char **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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); if (locator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, locator); } printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); return -1; } - z_owned_closure_sample_t callback = z_closure(data_handler); + z_owned_closure_sample_t callback; + z_closure(&callback, 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)) { + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } diff --git a/examples/zephyr/z_get.c b/examples/zephyr/z_get.c index 9e37ffdc2..397b3d340 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -30,15 +30,21 @@ #endif #define KEYEXPR "demo/example/**" +#define VALUE "" void reply_dropper(void *ctx) { printf(" >> Received query final notification\n"); } -void reply_handler(z_owned_reply_t *oreply, void *ctx) { +void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { if (z_reply_is_ok(oreply)) { - z_loaned_sample_t sample = z_reply_ok(oreply); - 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); + const z_loaned_sample_t *sample = z_reply_ok(oreply); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(" >> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(" >> Received an error\n"); } @@ -48,32 +54,45 @@ int main(int argc, char **argv) { sleep(5); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_loan(s), NULL); - zp_start_lease_task(z_loan(s), NULL); + zp_start_read_task(z_loan_mut(s), NULL); + zp_start_lease_task(z_loan_mut(s), NULL); while (1) { sleep(5); printf("Sending Query '%s'...\n", KEYEXPR); - z_get_options_t opts = z_get_options_default(); + z_get_options_t opts; + z_get_options_default(&opts); opts.target = Z_QUERY_TARGET_ALL; - 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) { + // Value encoding + if (strcmp(VALUE, "") != 0) { + z_view_string_t value_str; + z_view_string_wrap(&value_str, VALUE); + z_owned_bytes_t payload; + z_bytes_encode_from_string(&payload, z_loan(value_str)); + opts.payload = &payload; + } + z_owned_closure_reply_t callback; + z_closure(&callback, reply_handler, reply_dropper); + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_get(z_loan(s), z_loan(ke), "", z_move(callback), &opts) < 0) { printf("Unable to send query.\n"); exit(-1); } @@ -81,8 +100,8 @@ int main(int argc, char **argv) { printf("Closing Zenoh Session..."); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); printf("OK!\n"); diff --git a/examples/zephyr/z_pub.c b/examples/zephyr/z_pub.c index 1f47be9a0..209f91dc5 100644 --- a/examples/zephyr/z_pub.c +++ b/examples/zephyr/z_pub.c @@ -36,28 +36,31 @@ int main(int argc, char **argv) { sleep(5); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_loan(s), NULL); - zp_start_lease_task(z_loan(s), NULL); + zp_start_read_task(z_loan_mut(s), NULL); + zp_start_lease_task(z_loan_mut(s), NULL); printf("Declaring publisher for '%s'...", KEYEXPR); - z_owned_publisher_t pub = z_declare_publisher(z_loan(s), z_keyexpr(KEYEXPR), NULL); - if (!z_check(pub)) { + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + z_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); exit(-1); } @@ -75,8 +78,8 @@ int main(int argc, char **argv) { z_undeclare_publisher(z_move(pub)); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); printf("OK!\n"); diff --git a/examples/zephyr/z_pull.c b/examples/zephyr/z_pull.c index 20e4109b5..007b20ba5 100644 --- a/examples/zephyr/z_pull.c +++ b/examples/zephyr/z_pull.c @@ -30,9 +30,11 @@ #define KEYEXPR "demo/example/**" // @TODO -// void data_handler(const z_sample_t *sample, void *arg) { -// z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); -// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample->payload.len, +// void data_handler(const z_loaned_sample_t *sample, void *arg) { +// z_owned_string_t keystr; +// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), +// (int)sample->payload.len, // sample->payload.start); // z_drop(z_move(keystr)); // } @@ -41,30 +43,32 @@ int main(int argc, char **argv) { sleep(5); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_loan(s), NULL); - zp_start_lease_task(z_loan(s), NULL); + zp_start_read_task(z_loan_mut(s), NULL); + zp_start_lease_task(z_loan_mut(s), NULL); // @TODO - // z_owned_closure_sample_t callback = z_closure(data_handler); + // z_owned_closure_sample_t callback; + // z_closure(&callback, data_handler); printf("Declaring Subscriber on '%s'...", KEYEXPR); // @TODO - // z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL); + // z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_loan(ke), z_move(callback), NULL); // if (!z_check(sub)) { // printf("Unable to declare subscriber.\n"); // exit(-1); @@ -82,8 +86,8 @@ int main(int argc, char **argv) { printf("Pull Subscriber not supported... exiting\n"); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); printf("OK!\n"); diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index febdaabe2..f210cf1b3 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -31,12 +31,29 @@ #define KEYEXPR "demo/example/zenoh-pico-queryable" #define VALUE "[STSTM32]{nucleo-F767ZI} Queryable from Zenoh-Pico!" -void query_handler(const z_query_t *query, void *ctx) { +void query_handler(const z_loaned_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); - z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL); + z_owned_string_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_string_t params; + z_query_parameters(query, ¶ms); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), z_loan(params)->len, + z_loan(params)->val); + // Process value + const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query)); + if (z_bytes_len(payload) > 0) { + z_owned_string_t payload_string; + z_bytes_decode_into_string(payload, &payload_string); + printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); + } + // Reply value encoding + z_view_string_t reply_str; + z_view_string_wrap(&reply_str, VALUE); + z_owned_bytes_t reply_payload; + z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + + z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); z_drop(z_move(keystr)); } @@ -44,30 +61,34 @@ int main(int argc, char **argv) { sleep(5); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_loan(s), NULL); - zp_start_lease_task(z_loan(s), NULL); + zp_start_read_task(z_loan_mut(s), NULL); + zp_start_lease_task(z_loan_mut(s), NULL); // Declare Zenoh queryable printf("Declaring Queryable on %s...", KEYEXPR); - z_owned_closure_query_t callback = z_closure(query_handler); - z_owned_queryable_t qable = z_declare_queryable(z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL); - if (!z_check(qable)) { + z_owned_closure_query_t callback; + z_closure(&callback, query_handler); + z_owned_queryable_t qable; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to declare queryable.\n"); exit(-1); } @@ -82,8 +103,8 @@ int main(int argc, char **argv) { z_undeclare_queryable(z_move(qable)); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); printf("OK!\n"); diff --git a/examples/zephyr/z_scout.c b/examples/zephyr/z_scout.c index e9e221194..06c8277b5 100644 --- a/examples/zephyr/z_scout.c +++ b/examples/zephyr/z_scout.c @@ -37,26 +37,27 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { } } -void fprintlocators(FILE *stream, const z_str_array_t *locs) { +void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); + const z_loaned_string_t *str = z_string_array_get(locs, i); + fprintf(stream, "%.*s", (int)str->len, str->val); fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { + if (i < z_string_array_len(locs) - 1) { fprintf(stream, ", "); } } fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +void fprinthello(FILE *stream, const z_loaned_hello_t *hello) { fprintf(stream, "Hello { zid: "); - fprintzid(stream, hello.zid); + fprintzid(stream, hello->zid); fprintf(stream, ", whatami: "); - fprintwhatami(stream, hello.whatami); + fprintwhatami(stream, hello->whatami); fprintf(stream, ", locators: "); - fprintlocators(stream, &hello.locators); + fprintlocators(stream, &hello->locators); fprintf(stream, " }"); } @@ -81,8 +82,10 @@ int main(void) { 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_hello(callback, drop, context); + z_owned_scouting_config_t config; + z_scouting_config_default(&config); + z_owned_closure_hello_t closure; + z_closure_hello(&closure, callback, drop, context); printf("Scouting...\n"); z_scout(z_scouting_config_move(&config), z_closure_hello_move(&closure)); diff --git a/examples/zephyr/z_sub.c b/examples/zephyr/z_sub.c index c2e47869f..bda16ff13 100644 --- a/examples/zephyr/z_sub.c +++ b/examples/zephyr/z_sub.c @@ -29,11 +29,12 @@ #define KEYEXPR "demo/example/**" -void data_handler(const z_sample_t *sample, void *arg) { - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t keystr = z_keyexpr_to_string(keyexpr); - printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_loan(keystr), (int)payload.len, payload.start); +void data_handler(const z_loaned_sample_t *sample, void *arg) { + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)payload->len, + payload->start); z_drop(z_move(keystr)); } @@ -41,29 +42,33 @@ int main(int argc, char **argv) { sleep(5); // 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)); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE); if (strcmp(CONNECT, "") != 0) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT); } // Open Zenoh session printf("Opening Zenoh Session..."); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-1); } printf("OK\n"); // Start the receive and the session lease loop for zenoh-pico - zp_start_read_task(z_loan(s), NULL); - zp_start_lease_task(z_loan(s), NULL); + zp_start_read_task(z_loan_mut(s), NULL); + zp_start_lease_task(z_loan_mut(s), NULL); printf("Declaring Subscriber on '%s'...", KEYEXPR); - z_owned_closure_sample_t callback = z_closure(data_handler); - z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL); - if (!z_check(sub)) { + z_owned_closure_sample_t callback; + z_closure(&callback, data_handler); + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + z_owned_subscriber_t sub; + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); exit(-1); } @@ -77,8 +82,8 @@ int main(int argc, char **argv) { z_undeclare_subscriber(z_move(sub)); // Stop the receive and the session lease loop for zenoh-pico - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); printf("OK!\n"); diff --git a/include/zenoh-pico/api/constants.h b/include/zenoh-pico/api/constants.h index a43286225..58fecb460 100644 --- a/include/zenoh-pico/api/constants.h +++ b/include/zenoh-pico/api/constants.h @@ -86,55 +86,79 @@ typedef enum { Z_SAMPLE_KIND_PUT = 0, Z_SAMPLE_KIND_DELETE = 1 } z_sample_kind_t #define Z_SAMPLE_KIND_DEFAULT Z_SAMPLE_KIND_PUT /** - * Zenoh encoding values. - * These values are based on already existing HTTP MIME types and extended with other relevant encodings. + * Default encoding values used by Zenoh. * - * Enumerators: - * Z_ENCODING_PREFIX_EMPTY: Encoding not defined. - * Z_ENCODING_PREFIX_APP_OCTET_STREAM: ``application/octet-stream``. Default value for all other cases. An unknown - * file type should use this type. Z_ENCODING_PREFIX_APP_CUSTOM: Custom application type. Non IANA standard. - * Z_ENCODING_PREFIX_TEXT_PLAIN: ``text/plain``. Default value for textual files. A textual file should be - * human-readable and must not contain binary data. Z_ENCODING_PREFIX_APP_PROPERTIES: Application properties - * type. Non IANA standard. Z_ENCODING_PREFIX_APP_JSON: ``application/json``. JSON format. - * Z_ENCODING_PREFIX_APP_SQL: Application sql type. Non IANA standard. Z_ENCODING_PREFIX_APP_INTEGER: Application - * integer type. Non IANA standard. Z_ENCODING_PREFIX_APP_FLOAT: Application float type. Non IANA standard. - * Z_ENCODING_PREFIX_APP_XML: ``application/xml``. XML. - * Z_ENCODING_PREFIX_APP_XHTML_XML: ``application/xhtml+xml``. XHTML. - * Z_ENCODING_PREFIX_APP_X_WWW_FORM_URLENCODED: ``application/x-www-form-urlencoded``. The keys and values are - * encoded in key-value tuples separated by '&', with a '=' between the key and the value. - * Z_ENCODING_PREFIX_TEXT_JSON: Text JSON. Non IANA standard. Z_ENCODING_PREFIX_TEXT_HTML: ``text/html``. HyperText - * Markup Language (HTML). Z_ENCODING_PREFIX_TEXT_XML: ``text/xml``. `Application/xml` is recommended as of RFC - * 7303 (section 4.1), but `text/xml` is still used sometimes. Z_ENCODING_PREFIX_TEXT_CSS: ``text/css``. - * Cascading Style Sheets (CSS). Z_ENCODING_PREFIX_TEXT_CSV: ``text/csv``. Comma-separated values (CSV). - * Z_ENCODING_PREFIX_TEXT_JAVASCRIPT: ``text/javascript``. JavaScript. - * Z_ENCODING_PREFIX_IMAGE_JPEG: ``image/jpeg``. JPEG images. - * Z_ENCODING_PREFIX_IMAGE_PNG: ``image/png``. Portable Network Graphics. - * Z_ENCODING_PREFIX_IMAGE_GIF: ``image/gif``. Graphics Interchange Format (GIF). + * An encoding has a similar role to Content-type in HTTP: it indicates, when present, how data should be interpreted by + * the application. + * + * Please note the Zenoh protocol does not impose any encoding value nor it operates on it. + * It can be seen as some optional metadata that is carried over by Zenoh in such a way the application may perform + * different operations depending on the encoding value. + * + * A set of associated constants are provided to cover the most common encodings for user convenience. + * This is particularly useful in helping Zenoh to perform additional network optimizations. */ typedef enum { - Z_ENCODING_PREFIX_EMPTY = 0, - Z_ENCODING_PREFIX_APP_OCTET_STREAM = 1, - Z_ENCODING_PREFIX_APP_CUSTOM = 2, // non iana standard - Z_ENCODING_PREFIX_TEXT_PLAIN = 3, - Z_ENCODING_PREFIX_APP_PROPERTIES = 4, // non iana standard - Z_ENCODING_PREFIX_APP_JSON = 5, // if not readable from casual users - Z_ENCODING_PREFIX_APP_SQL = 6, - Z_ENCODING_PREFIX_APP_INTEGER = 7, // non iana standard - Z_ENCODING_PREFIX_APP_FLOAT = 8, // non iana standard - Z_ENCODING_PREFIX_APP_XML = 9, // if not readable from casual users (RFC 3023, section 3) - Z_ENCODING_PREFIX_APP_XHTML_XML = 10, - Z_ENCODING_PREFIX_APP_X_WWW_FORM_URLENCODED = 11, - Z_ENCODING_PREFIX_TEXT_JSON = 12, // non iana standard - if readable from casual users - Z_ENCODING_PREFIX_TEXT_HTML = 13, - Z_ENCODING_PREFIX_TEXT_XML = 14, // if readable from casual users (RFC 3023, section 3) - Z_ENCODING_PREFIX_TEXT_CSS = 15, - Z_ENCODING_PREFIX_TEXT_CSV = 16, - Z_ENCODING_PREFIX_TEXT_JAVASCRIPT = 17, - Z_ENCODING_PREFIX_IMAGE_JPEG = 18, - Z_ENCODING_PREFIX_IMAGE_PNG = 19, - Z_ENCODING_PREFIX_IMAGE_GIF = 20 -} z_encoding_prefix_t; -#define Z_ENCODING_PREFIX_DEFAULT Z_ENCODING_PREFIX_EMPTY + Z_ENCODING_ID_BYTES = 0, // Primitives types supported in all Zenoh bindings, just some bytes. + Z_ENCODING_ID_INT = 1, // A VLE-encoded signed little-endian integer. + Z_ENCODING_ID_UINT = 2, // A VLE-encoded little-endian unsigned integer. + Z_ENCODING_ID_FLOAT = 3, // A VLE-encoded float. + Z_ENCODING_ID_BOOL = 4, // A boolean. + Z_ENCODING_ID_STRING = 5, // A UTF-8 string. + Z_ENCODING_ID_ERROR = 6, // A zenoh error. + Z_ENCODING_ID_APP_OCTET_STREAM = 7, // An application-specific stream of bytes. + Z_ENCODING_ID_TEXT_PLAIN = 8, // A textual file. + Z_ENCODING_ID_APP_JSON = 9, // JSON data intended to be consumed by an application. + Z_ENCODING_ID_TEXT_JSON = 10, // JSON data intended to be human readable. + Z_ENCODING_ID_APP_CDR = 11, // A Common Data Representation (CDR)-encoded data. + Z_ENCODING_ID_APP_CBOR = 12, // A Concise Binary Object Representation (CBOR)-encoded data. + Z_ENCODING_ID_APP_YAML = 13, // YAML data intended to be consumed by an application. + Z_ENCODING_ID_TEXT_YAML = 14, // YAML data intended to be human readable. + Z_ENCODING_ID_TEXT_JSON5 = 15, // JSON5 encoded data that are human readable. + Z_ENCODING_ID_APP_PYTHON_SERIALIZED_OBJECT = 16, // A Python object serialized using pickle. + Z_ENCODING_ID_APP_PROTOBUF = 17, // An application-specific protobuf-encoded data. + Z_ENCODING_ID_APP_JAVA_SERIALIZED_OBJECT = 18, // A Java serialized object. + Z_ENCODING_ID_APP_OPENMETRICS_TEXT = 19, // An openmetrics data. + Z_ENCODING_ID_IMAGE_PNG = 20, // A Portable Network Graphics (PNG) image. + Z_ENCODING_ID_IMAGE_JPEG = 21, // A Joint Photographic Experts Group (JPEG) image. + Z_ENCODING_ID_IMAGE_GIF = 22, // A Graphics Interchange Format (GIF) image. + Z_ENCODING_ID_IMAGE_BMP = 23, // A BitMap (BMP) image. + Z_ENCODING_ID_IMAGE_WEBP = 24, // A Web Portable (WebP) image. + Z_ENCODING_ID_APP_XML = 25, // An XML file intended to be consumed by an application. + Z_ENCODING_ID_APP_X_WWW_FORM_URLENCODED = 26, // An encoded a list of tuples. + Z_ENCODING_ID_TEXT_HTML = 27, // An HTML file. + Z_ENCODING_ID_TEXT_XML = 28, // An XML file that is human readable. + Z_ENCODING_ID_TEXT_CSS = 29, // A CSS file. + Z_ENCODING_ID_TEXT_JAVASCRIPT = 30, // A JavaScript file. + Z_ENCODING_ID_TEXT_MARKDOWN = 31, // A MarkDown file. + Z_ENCODING_ID_TEXT_CSV = 32, // A CSV file. + Z_ENCODING_ID_APP_SQL = 33, // An application-specific SQL query. + Z_ENCODING_ID_APP_COAP_PAYLOAD = 34, // Constrained Application Protocol (CoAP) data. + Z_ENCODING_ID_APP_JSON_PATCH_JSON = 35, // Defines a JSON document structure. + Z_ENCODING_ID_APP_JSON_SEQ = 36, // A JSON text sequence. + Z_ENCODING_ID_APP_JSONPATH = 37, // A JSONPath defines a string syntax. + Z_ENCODING_ID_APP_JWT = 38, // A JSON Web Token (JWT). + Z_ENCODING_ID_APP_MP4 = 39, // An application-specific MPEG-4 encoded data. + Z_ENCODING_ID_APP_SOAP_XML = 40, // A SOAP 1.2 message serialized as XML 1.0. + Z_ENCODING_ID_APP_YANG = 41, // A YANG-encoded data. + Z_ENCODING_ID_AUDIO_AAC = 42, // A MPEG-4 Advanced Audio Coding (AAC) media. + Z_ENCODING_ID_AUDIO_FLAC = 43, // A Free Lossless Audio Codec (FLAC) media. + Z_ENCODING_ID_AUDIO_MP4 = 44, // An audio codec defined in MPEG-4. + Z_ENCODING_ID_AUDIO_OGG = 45, // An Ogg-encapsulated audio stream. + Z_ENCODING_ID_AUDIO_VORBIS = 46, // A Vorbis-encoded audio stream. + Z_ENCODING_ID_VIDEO_H261 = 47, // A h261-encoded video stream. + Z_ENCODING_ID_VIDEO_H263 = 48, // A h263-encoded video stream. + Z_ENCODING_ID_VIDEO_H264 = 49, // A h264-encoded video stream. + Z_ENCODING_ID_VIDEO_H265 = 50, // A h265-encoded video stream. + Z_ENCODING_ID_VIDEO_H266 = 51, // A h266-encoded video stream. + Z_ENCODING_ID_VIDEO_MP4 = 52, // A video codec defined in MPEG-4. + Z_ENCODING_ID_VIDEO_OGG = 53, // An Ogg-encapsulated video stream. + Z_ENCODING_ID_VIDEO_RAW = 54, // An uncompressed, studio-quality video stream. + Z_ENCODING_ID_VIDEO_VP8 = 55, // A VP8-encoded video stream. + Z_ENCODING_ID_VIDEO_VP9 = 56 // A VP9-encoded video stream. +} z_encoding_id_t; + +#define Z_ENCODING_ID_DEFAULT Z_ENCODING_ID_BYTES /** * Consolidation mode values. diff --git a/include/zenoh-pico/api/handlers.h b/include/zenoh-pico/api/handlers.h index 43bd1db21..99d4d15b9 100644 --- a/include/zenoh-pico/api/handlers.h +++ b/include/zenoh-pico/api/handlers.h @@ -16,7 +16,7 @@ #include -#include "zenoh-pico/api/macros.h" +#include "zenoh-pico/api/primitives.h" #include "zenoh-pico/api/types.h" #include "zenoh-pico/collections/element.h" #include "zenoh-pico/collections/fifo_mt.h" @@ -25,15 +25,15 @@ // -- Samples handler void _z_owned_sample_move(z_owned_sample_t *dst, z_owned_sample_t *src); -z_owned_sample_t *_z_sample_to_owned_ptr(const z_sample_t *src); +z_owned_sample_t *_z_sample_to_owned_ptr(const z_loaned_sample_t *src); // -- Queries handler void _z_owned_query_move(z_owned_query_t *dst, z_owned_query_t *src); -z_owned_query_t *_z_query_to_owned_ptr(const z_query_t *src); +z_owned_query_t *_z_query_to_owned_ptr(const z_loaned_query_t *src); // -- Reply handler void _z_owned_reply_move(z_owned_reply_t *dst, z_owned_reply_t *src); -z_owned_reply_t *_z_reply_clone(const z_owned_reply_t *src); +z_owned_reply_t *_z_reply_to_owned_ptr(const z_loaned_reply_t *src); // -- Channel #define _Z_CHANNEL_DEFINE(name, send_closure_name, recv_closure_name, send_type, recv_type, collection_type, \ @@ -77,13 +77,12 @@ z_owned_reply_t *_z_reply_clone(const z_owned_reply_t *src); } \ } \ \ - static inline z_owned_##name##_t z_##name##_new(size_t capacity) { \ - z_owned_##name##_t channel; \ - channel.collection = collection_new_f(capacity); \ - channel.send = z_##send_closure_name(_z_##name##_send, NULL, channel.collection); \ - channel.recv = z_##recv_closure_name(_z_##name##_recv, NULL, channel.collection); \ - channel.try_recv = z_##recv_closure_name(_z_##name##_try_recv, NULL, channel.collection); \ - return channel; \ + static inline int8_t z_##name##_new(z_owned_##name##_t *channel, size_t capacity) { \ + channel->collection = collection_new_f(capacity); \ + z_##send_closure_name(&channel->send, _z_##name##_send, NULL, channel->collection); \ + z_##recv_closure_name(&channel->recv, _z_##name##_recv, NULL, channel->collection); \ + z_##recv_closure_name(&channel->try_recv, _z_##name##_try_recv, NULL, channel->collection); \ + return _Z_RES_OK; \ } \ static inline z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *val) { return val; } \ static inline void z_##name##_drop(z_owned_##name##_t *channel) { \ @@ -93,33 +92,33 @@ z_owned_reply_t *_z_reply_clone(const z_owned_reply_t *src); } // z_owned_sample_ring_channel_t -_Z_CHANNEL_DEFINE(sample_ring_channel, closure_sample, closure_owned_sample, const z_sample_t, z_owned_sample_t, +_Z_CHANNEL_DEFINE(sample_ring_channel, closure_sample, closure_owned_sample, const z_loaned_sample_t, z_owned_sample_t, _z_ring_mt_t, _z_ring_mt_new, _z_ring_mt_free, _z_ring_mt_push, _z_ring_mt_pull, _z_ring_mt_try_pull, _z_owned_sample_move, _z_sample_to_owned_ptr, z_sample_drop) // z_owned_sample_fifo_channel_t -_Z_CHANNEL_DEFINE(sample_fifo_channel, closure_sample, closure_owned_sample, const z_sample_t, z_owned_sample_t, +_Z_CHANNEL_DEFINE(sample_fifo_channel, closure_sample, closure_owned_sample, const z_loaned_sample_t, z_owned_sample_t, _z_fifo_mt_t, _z_fifo_mt_new, _z_fifo_mt_free, _z_fifo_mt_push, _z_fifo_mt_pull, _z_fifo_mt_try_pull, _z_owned_sample_move, _z_sample_to_owned_ptr, z_sample_drop) // z_owned_query_ring_channel_t -_Z_CHANNEL_DEFINE(query_ring_channel, closure_query, closure_owned_query, const z_query_t, z_owned_query_t, +_Z_CHANNEL_DEFINE(query_ring_channel, closure_query, closure_owned_query, const z_loaned_query_t, z_owned_query_t, _z_ring_mt_t, _z_ring_mt_new, _z_ring_mt_free, _z_ring_mt_push, _z_ring_mt_pull, _z_ring_mt_try_pull, _z_owned_query_move, _z_query_to_owned_ptr, z_query_drop) // z_owned_query_fifo_channel_t -_Z_CHANNEL_DEFINE(query_fifo_channel, closure_query, closure_owned_query, const z_query_t, z_owned_query_t, +_Z_CHANNEL_DEFINE(query_fifo_channel, closure_query, closure_owned_query, const z_loaned_query_t, z_owned_query_t, _z_fifo_mt_t, _z_fifo_mt_new, _z_fifo_mt_free, _z_fifo_mt_push, _z_fifo_mt_pull, _z_fifo_mt_try_pull, _z_owned_query_move, _z_query_to_owned_ptr, z_query_drop) // z_owned_reply_ring_channel_t -_Z_CHANNEL_DEFINE(reply_ring_channel, closure_reply, closure_reply, z_owned_reply_t, z_owned_reply_t, _z_ring_mt_t, - _z_ring_mt_new, _z_ring_mt_free, _z_ring_mt_push, _z_ring_mt_pull, _z_ring_mt_try_pull, - _z_owned_reply_move, _z_reply_clone, z_reply_drop) +_Z_CHANNEL_DEFINE(reply_ring_channel, closure_reply, closure_owned_reply, const z_loaned_reply_t, z_owned_reply_t, + _z_ring_mt_t, _z_ring_mt_new, _z_ring_mt_free, _z_ring_mt_push, _z_ring_mt_pull, _z_ring_mt_try_pull, + _z_owned_reply_move, _z_reply_to_owned_ptr, z_reply_drop) // z_owned_reply_fifo_channel_t -_Z_CHANNEL_DEFINE(reply_fifo_channel, closure_reply, closure_reply, z_owned_reply_t, z_owned_reply_t, _z_fifo_mt_t, - _z_fifo_mt_new, _z_fifo_mt_free, _z_fifo_mt_push, _z_fifo_mt_pull, _z_fifo_mt_try_pull, - _z_owned_reply_move, _z_reply_clone, z_reply_drop) +_Z_CHANNEL_DEFINE(reply_fifo_channel, closure_reply, closure_owned_reply, const z_loaned_reply_t, z_owned_reply_t, + _z_fifo_mt_t, _z_fifo_mt_new, _z_fifo_mt_free, _z_fifo_mt_push, _z_fifo_mt_pull, _z_fifo_mt_try_pull, + _z_owned_reply_move, _z_reply_to_owned_ptr, z_reply_drop) #endif // INCLUDE_ZENOH_PICO_API_HANDLERS_H diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 5cbadcb54..58287f5fa 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -35,16 +35,36 @@ #define z_loan(x) _Generic((x), \ z_owned_keyexpr_t : z_keyexpr_loan, \ + z_view_keyexpr_t : z_view_keyexpr_loan, \ z_owned_config_t : z_config_loan, \ z_owned_scouting_config_t : z_scouting_config_loan, \ z_owned_session_t : z_session_loan, \ z_owned_publisher_t : z_publisher_loan, \ z_owned_reply_t : z_reply_loan, \ z_owned_hello_t : z_hello_loan, \ - z_owned_str_t : z_str_loan, \ - z_owned_str_array_t : z_str_array_loan, \ + z_owned_string_t : z_string_loan, \ + z_view_string_t : z_view_string_loan, \ + z_owned_string_array_t : z_string_array_loan, \ z_owned_sample_t : z_sample_loan, \ - z_owned_query_t : z_query_loan \ + z_owned_query_t : z_query_loan, \ + z_owned_bytes_t : z_bytes_loan, \ + z_owned_encoding_t : z_encoding_loan \ + )(&x) + +#define z_loan_mut(x) _Generic((x), \ + z_owned_keyexpr_t : z_keyexpr_loan_mut, \ + z_owned_config_t : z_config_loan_mut, \ + z_owned_scouting_config_t : z_scouting_config_loan_mut, \ + z_owned_session_t : z_session_loan_mut, \ + z_owned_publisher_t : z_publisher_loan_mut, \ + z_owned_reply_t : z_reply_loan_mut, \ + z_owned_hello_t : z_hello_loan_mut, \ + z_owned_string_t : z_string_loan_mut, \ + z_view_string_t : z_view_string_loan_mut, \ + z_owned_string_array_t : z_string_array_loan_mut, \ + z_owned_sample_t : z_sample_loan_mut, \ + z_owned_query_t : z_query_loan_mut, \ + z_owned_bytes_t : z_bytes_loan_mut \ )(&x) /** * Defines a generic function for dropping any of the ``z_owned_X_t`` types. @@ -62,10 +82,11 @@ 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_string_t * : z_string_drop, \ + z_owned_string_array_t * : z_string_array_drop, \ z_owned_sample_t * : z_sample_drop, \ z_owned_query_t * : z_query_drop, \ + z_owned_encoding_t * : z_encoding_drop, \ z_owned_closure_sample_t * : z_closure_sample_drop, \ z_owned_closure_owned_sample_t * : z_closure_owned_sample_drop, \ z_owned_closure_query_t * : z_closure_query_drop, \ @@ -92,8 +113,8 @@ #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_view_keyexpr_t : z_keyexpr_is_initialized, \ + z_owned_value_t : z_value_check, \ z_owned_config_t : z_config_check, \ z_owned_scouting_config_t : z_scouting_config_check, \ z_owned_session_t : z_session_check, \ @@ -102,11 +123,12 @@ 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, \ + z_owned_string_t : z_string_check, \ + z_owned_string_array_t : z_string_array_check, \ + z_owned_bytes_t : z_bytes_check, \ z_owned_sample_t : z_sample_check, \ - z_owned_query_t : z_query_check \ + z_owned_query_t : z_query_check, \ + z_owned_encoding_t : z_encoding_check \ )(&x) /** @@ -122,7 +144,8 @@ z_owned_closure_hello_t : z_closure_hello_call, \ z_owned_closure_zid_t : z_closure_zid_call, \ z_owned_closure_owned_sample_t : z_closure_owned_sample_call, \ - z_owned_closure_owned_query_t : z_closure_owned_query_call \ + z_owned_closure_owned_query_t : z_closure_owned_query_call, \ + z_owned_closure_owned_reply_t : z_closure_owned_reply_call \ ) (&x, __VA_ARGS__) /** @@ -144,8 +167,8 @@ 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_string_t : z_string_move, \ + z_owned_string_array_t : z_string_array_move, \ z_owned_closure_sample_t : z_closure_sample_move, \ z_owned_closure_owned_sample_t : z_closure_owned_sample_move, \ z_owned_closure_query_t : z_closure_query_move, \ @@ -155,12 +178,14 @@ z_owned_closure_zid_t : z_closure_zid_move, \ z_owned_sample_t : z_sample_move, \ z_owned_query_t : z_query_move, \ + z_owned_bytes_t : z_bytes_move, \ + z_owned_encoding_t : z_encoding_move, \ z_owned_sample_ring_channel_t : z_sample_ring_channel_move, \ z_owned_sample_fifo_channel_t : z_sample_fifo_channel_move, \ z_owned_query_ring_channel_t : z_query_ring_channel_move, \ - z_owned_query_fifo_channel_t : z_query_ring_channel_move, \ + z_owned_query_fifo_channel_t : z_query_fifo_channel_move, \ z_owned_reply_ring_channel_t : z_reply_ring_channel_move, \ - z_owned_reply_fifo_channel_t : z_reply_ring_channel_move \ + z_owned_reply_fifo_channel_t : z_reply_fifo_channel_move \ )(&x) /** @@ -181,8 +206,8 @@ 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 \ + z_owned_string_t : z_string_clone, \ + z_owned_string_array_t : z_string_array_clone \ )(&x) /** @@ -191,7 +216,7 @@ * Returns: * Returns the uninitialized instance of `x`. */ -#define z_null(x) (*x = _Generic((x), \ +#define z_null(x) _Generic((x), \ z_owned_session_t * : z_session_null, \ z_owned_publisher_t * : z_publisher_null, \ z_owned_keyexpr_t * : z_keyexpr_null, \ @@ -199,9 +224,10 @@ z_owned_scouting_config_t * : z_scouting_config_null, \ z_owned_subscriber_t * : z_subscriber_null, \ z_owned_queryable_t * : z_queryable_null, \ + z_owned_query_t * : z_query_null, \ z_owned_reply_t * : z_reply_null, \ z_owned_hello_t * : z_hello_null, \ - z_owned_str_t * : z_str_null, \ + z_owned_string_t * : z_string_null, \ z_owned_closure_sample_t * : z_closure_sample_null, \ z_owned_closure_owned_sample_t * : z_closure_owned_sample_null, \ z_owned_closure_query_t * : z_closure_query_null, \ @@ -209,13 +235,18 @@ 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, \ - z_owned_sample_t * : z_sample_null \ - )()) + z_owned_sample_t * : z_sample_null, \ + z_owned_encoding_t * : z_encoding_null \ + )(x) // clang-format on -#define _z_closure_overloader(callback, dropper, ctx, ...) \ - { .call = callback, .drop = dropper, .context = ctx } +#define _z_closure_overloader(closure, callback, dropper, ctx, ...) \ + do { \ + (closure)->call = callback; \ + (closure)->drop = dropper; \ + (closure)->context = ctx; \ + } while (0); /** * Defines a variadic macro to ease the definition of callback closures. @@ -230,25 +261,81 @@ */ #define z_closure(...) _z_closure_overloader(__VA_ARGS__, 0, 0, 0) -#else +#else // __cplusplus // clang-format off + +// z_loan definition + template struct zenoh_loan_type { typedef T type; }; template inline typename zenoh_loan_type::type z_loan(const T&); -template<> struct zenoh_loan_type{ typedef 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_hello_t type; }; -template<> struct zenoh_loan_type{ typedef const char* type; }; +template <> struct zenoh_loan_type { typedef const z_loaned_keyexpr_t* type; }; +template <> struct zenoh_loan_type { typedef const z_loaned_keyexpr_t* type; }; +template <> struct zenoh_loan_type { typedef const z_loaned_config_t* type; }; +template <> struct zenoh_loan_type { const typedef z_loaned_scouting_config_t* type; }; +template <> struct zenoh_loan_type { typedef const z_loaned_session_t* type; }; +template <> struct zenoh_loan_type { typedef const z_loaned_publisher_t* type; }; +template <> struct zenoh_loan_type { typedef const z_loaned_reply_t* type; }; +template <> struct zenoh_loan_type { typedef const z_loaned_hello_t* type; }; +template <> struct zenoh_loan_type { typedef const z_loaned_string_t* type; }; +template <> struct zenoh_loan_type { typedef const z_loaned_string_t* type; }; +template <> struct zenoh_loan_type { typedef const z_loaned_string_array_t* type; }; +template <> struct zenoh_loan_type { typedef const z_loaned_sample_t* type; }; +template <> struct zenoh_loan_type { typedef const z_loaned_query_t* type; }; +template <> struct zenoh_loan_type { typedef const z_loaned_bytes_t* type; }; + +template <> inline const z_loaned_keyexpr_t* z_loan(const z_owned_keyexpr_t& x) { return z_keyexpr_loan(&x); } +template <> inline const z_loaned_keyexpr_t* z_loan(const z_view_keyexpr_t& x) { return z_view_keyexpr_loan(&x); } +template <> inline const z_loaned_config_t* z_loan(const z_owned_config_t& x) { return z_config_loan(&x); } +template <> inline const z_loaned_scouting_config_t* z_loan(const z_owned_scouting_config_t& x) { return z_scouting_config_loan(&x); } +template <> inline const z_loaned_session_t* z_loan(const z_owned_session_t& x) { return z_session_loan(&x); } +template <> inline const z_loaned_publisher_t* z_loan(const z_owned_publisher_t& x) { return z_publisher_loan(&x); } +template <> inline const z_loaned_reply_t* z_loan(const z_owned_reply_t& x) { return z_reply_loan(&x); } +template <> inline const z_loaned_hello_t* z_loan(const z_owned_hello_t& x) { return z_hello_loan(&x); } +template <> inline const z_loaned_string_t* z_loan(const z_owned_string_t& x) { return z_string_loan(&x); } +template <> inline const z_loaned_string_t* z_loan(const z_view_string_t& x) { return z_view_string_loan(&x); } +template <> inline const z_loaned_string_array_t* z_loan(const z_owned_string_array_t& x) { return z_string_array_loan(&x); } +template <> inline const z_loaned_sample_t* z_loan(const z_owned_sample_t& x) { return z_sample_loan(&x); } +template <> inline const z_loaned_query_t* z_loan(const z_owned_query_t& x) { return z_query_loan(&x); } +template <> inline const z_loaned_bytes_t* z_loan(const z_owned_bytes_t& x) { return z_bytes_loan(&x); } + +// z_loan_mut definition + +template struct zenoh_loan_mut_type { typedef T type; }; +template inline typename zenoh_loan_mut_type::type z_loan_mut(T&); -template<> 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_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 <> struct zenoh_loan_mut_type { typedef z_loaned_keyexpr_t* type; }; +template <> struct zenoh_loan_mut_type { typedef z_loaned_keyexpr_t* type; }; +template <> struct zenoh_loan_mut_type { typedef z_loaned_config_t* type; }; +template <> struct zenoh_loan_mut_type { typedef z_loaned_scouting_config_t* type; }; +template <> struct zenoh_loan_mut_type { typedef z_loaned_session_t* type; }; +template <> struct zenoh_loan_mut_type { typedef z_loaned_publisher_t* type; }; +template <> struct zenoh_loan_mut_type { typedef z_loaned_reply_t* type; }; +template <> struct zenoh_loan_mut_type { typedef z_loaned_hello_t* type; }; +template <> struct zenoh_loan_mut_type { typedef z_loaned_string_t* type; }; +template <> struct zenoh_loan_mut_type { typedef z_loaned_string_t* type; }; +template <> struct zenoh_loan_mut_type { typedef z_loaned_string_array_t* type; }; +template <> struct zenoh_loan_mut_type { typedef z_loaned_sample_t* type; }; +template <> struct zenoh_loan_mut_type { typedef z_loaned_query_t* type; }; +template <> struct zenoh_loan_mut_type { typedef z_loaned_bytes_t* type; }; + +template <> inline z_loaned_keyexpr_t* z_loan_mut(z_owned_keyexpr_t& x) { return z_keyexpr_loan_mut(&x); } +template <> inline z_loaned_keyexpr_t* z_loan_mut(z_view_keyexpr_t& x) { return z_view_keyexpr_loan_mut(&x); } +template <> inline z_loaned_config_t* z_loan_mut(z_owned_config_t& x) { return z_config_loan_mut(&x); } +template <> inline z_loaned_scouting_config_t* z_loan_mut(z_owned_scouting_config_t& x) { return z_scouting_config_loan_mut(&x); } +template <> inline z_loaned_session_t* z_loan_mut(z_owned_session_t& x) { return z_session_loan_mut(&x); } +template <> inline z_loaned_publisher_t* z_loan_mut(z_owned_publisher_t& x) { return z_publisher_loan_mut(&x); } +template <> inline z_loaned_reply_t* z_loan_mut(z_owned_reply_t& x) { return z_reply_loan_mut(&x); } +template <> inline z_loaned_hello_t* z_loan_mut(z_owned_hello_t& x) { return z_hello_loan_mut(&x); } +template <> inline z_loaned_string_t* z_loan_mut(z_owned_string_t& x) { return z_string_loan_mut(&x); } +template <> inline z_loaned_string_t* z_loan_mut(z_view_string_t& x) { return z_view_string_loan_mut(&x); } +template <> inline z_loaned_string_array_t* z_loan_mut(z_owned_string_array_t& x) { return z_string_array_loan_mut(&x); } +template <> inline z_loaned_sample_t* z_loan_mut(z_owned_sample_t& x) { return z_sample_loan_mut(&x); } +template <> inline z_loaned_query_t* z_loan_mut(z_owned_query_t& x) { return z_query_loan_mut(&x); } +template <> inline z_loaned_bytes_t* z_loan_mut(z_owned_bytes_t& x) { return z_bytes_loan_mut(&x); } + +// z_drop definition template struct zenoh_drop_type { typedef T type; }; template inline typename zenoh_drop_type::type z_drop(T*); @@ -262,15 +349,18 @@ template<> struct zenoh_drop_type { typedef int8_t type; } template<> struct zenoh_drop_type { typedef int8_t type; }; template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; +template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; -template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; template<> struct zenoh_drop_type { typedef void type; }; +template<> struct zenoh_drop_type { typedef void type; }; +template<> struct zenoh_drop_type { typedef void type; }; +template<> 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); } @@ -281,71 +371,74 @@ template<> inline int8_t z_drop(z_owned_subscriber_t* v) { return z_undeclare_su 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_string_t* v) { z_string_drop(v); } template<> inline void z_drop(z_owned_closure_sample_t* v) { z_closure_sample_drop(v); } -template<> inline void z_drop(z_owned_closure_owned_sample_t* v) { z_closure_owned_sample_drop(v); } template<> inline void z_drop(z_owned_closure_query_t* v) { z_closure_query_drop(v); } template<> inline void z_drop(z_owned_closure_reply_t* v) { z_closure_reply_drop(v); } template<> inline void z_drop(z_owned_closure_hello_t* v) { z_closure_hello_drop(v); } template<> inline void z_drop(z_owned_closure_zid_t* v) { z_closure_zid_drop(v); } -template<> inline void z_drop(z_owned_sample_ring_channel_t* v) { z_owned_sample_ring_channel_drop(v); } -template<> inline void z_drop(z_owned_sample_fifo_channel_t* v) { z_owned_sample_fifo_channel_drop(v); } -template<> inline void z_drop(z_owned_query_ring_channel_t* v) { z_owned_query_ring_channel_drop(v); } -template<> inline void z_drop(z_owned_query_fifo_channel_t* v) { z_owned_query_fifo_channel_drop(v); } -template<> inline void z_drop(z_owned_reply_ring_channel_t* v) { z_owned_reply_ring_channel_drop(v); } -template<> inline void z_drop(z_owned_reply_fifo_channel_t* v) { z_owned_reply_fifo_channel_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_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_clusure_owned_sample_t& v) { v = z_closure_owned_sample_null(); } -inline void z_null(z_owned_closure_query_t& v) { v = z_closure_query_null(); } -inline void z_null(z_owned_clusure_owned_query_t& v) { v = z_closure_owned_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(); } +template<> inline void z_drop(z_owned_sample_ring_channel_t* v) { z_sample_ring_channel_drop(v); } +template<> inline void z_drop(z_owned_sample_fifo_channel_t* v) { z_sample_fifo_channel_drop(v); } +template<> inline void z_drop(z_owned_query_ring_channel_t* v) { z_query_ring_channel_drop(v); } +template<> inline void z_drop(z_owned_query_fifo_channel_t* v) { z_query_fifo_channel_drop(v); } +template<> inline void z_drop(z_owned_reply_ring_channel_t* v) { z_reply_ring_channel_drop(v); } +template<> inline void z_drop(z_owned_reply_fifo_channel_t* v) { z_reply_fifo_channel_drop(v); } + +// z_null definition + +inline void z_null(z_owned_session_t* v) { z_session_null(v); } +inline void z_null(z_owned_publisher_t* v) { z_publisher_null(v); } +inline void z_null(z_owned_keyexpr_t* v) { z_keyexpr_null(v); } +inline void z_null(z_owned_config_t* v) { z_config_null(v); } +inline void z_null(z_owned_scouting_config_t* v) { z_scouting_config_null(v); } +inline void z_null(z_owned_subscriber_t* v) { z_subscriber_null(v); } +inline void z_null(z_owned_queryable_t* v) { z_queryable_null(v); } +inline void z_null(z_owned_reply_t* v) { z_reply_null(v); } +inline void z_null(z_owned_hello_t* v) { z_hello_null(v); } +inline void z_null(z_owned_string_t* v) { z_string_null(v); } +inline void z_null(z_owned_closure_sample_t* v) { z_closure_sample_null(v); } +inline void z_null(z_owned_closure_query_t* v) { z_closure_query_null(v); } +inline void z_null(z_owned_closure_reply_t* v) { z_closure_reply_null(v); } +inline void z_null(z_owned_closure_hello_t* v) { z_closure_hello_null(v); } +inline void z_null(z_owned_closure_zid_t* v) { z_closure_zid_null(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_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 bool z_check(const z_owned_str_t& v) { return z_sample_check(&v); } +inline bool z_check(const z_owned_string_t& v) { return z_string_check(&v); } +inline bool z_check(const z_owned_sample_t& v) { return z_sample_check(&v); } -inline void z_call(const z_owned_closure_sample_t &closure, const z_sample_t *sample) +// z_call definition + +inline void z_call(const z_owned_closure_sample_t &closure, const z_loaned_sample_t *sample) { z_closure_sample_call(&closure, sample); } -inline void z_call(const z_owned_closure_owned_sample_t &closure, const z_sample_t *sample) - { z_closure_owned_sample_call(&closure, sample); } -inline void z_call(const z_owned_closure_query_t &closure, const z_query_t *query) +inline void z_call(const z_owned_closure_query_t &closure, const z_loaned_query_t *query) { z_closure_query_call(&closure, query); } -inline void z_call(const z_owned_closure_owned_query_t &closure, const z_query_t *query) - { z_closure_owned_query_call(&closure, query); } -inline void z_call(const z_owned_closure_reply_t &closure, z_owned_reply_t *reply) +inline void z_call(const z_owned_closure_reply_t &closure, const z_loaned_reply_t *reply) { z_closure_reply_call(&closure, reply); } +inline void z_call(const z_owned_closure_owned_reply_t &closure, z_owned_reply_t *reply) + { z_closure_owned_reply_call(&closure, reply); } inline void z_call(const z_owned_closure_hello_t &closure, z_owned_hello_t *hello) { 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, dropper, ctx, ...) \ - { .context = const_cast(static_cast(ctx)), .call = callback, .drop = dropper } +#define _z_closure_overloader(closure, callback, dropper, ctx, ...) \ + do { \ + (closure)->call = callback; \ + (closure)->drop = dropper; \ + (closure)->context = const_cast(static_cast(ctx)); \ + } while (0); + #define z_closure(...) _z_closure_overloader(__VA_ARGS__, NULL, NULL) + #define z_move(x) (&x) #endif diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 60a676947..0653ffab6 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -35,176 +35,155 @@ extern "C" { #define z_bytes_wrap _z_bytes_wrap /** - * Constructs a :c:type:`z_string_t` departing from a ``const char *``. - * It is a loaned key expression that aliases ``value``. + * Builds a :c:type:`z_view_string_t` by wrapping a ``const char *`` string. * * Parameters: - * value: Pointer to null terminated string. + * value: Pointer to a null terminated string. + * str: Pointer to an uninitialized :c:type:`z_view_string_t`. * - * Returns: - * The :c:type:`z_string_t` corresponding to the given string. + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. */ -z_string_t z_string_make(const char *value); +int8_t z_view_string_wrap(z_view_string_t *str, const char *value); /** - * Constructs a :c:type:`z_keyexpr_t` departing from a string. + * Builds a :c:type:`z_keyexpr_t` from a null-terminated 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. + * keyexpr: Pointer to an uninitialized :c:type:`z_view_keyexpr_t`. * - * Returns: - * The :c:type:`z_keyexpr_t` corresponding to the given string. + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. */ -z_keyexpr_t z_keyexpr(const char *name); +int8_t z_view_keyexpr_from_string(z_view_keyexpr_t *keyexpr, const char *name); /** - * Constructs a :c:type:`z_keyexpr_t` departing from a string. + * Builds a :c:type:`z_keyexpr_t` from a null-terminated 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. + * keyexpr: Pointer to an uninitialized :c:type:`z_view_keyexpr_t`. * - * Returns: - * The :c:type:`z_keyexpr_t` corresponding to the given string. + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. */ -z_keyexpr_t z_keyexpr_unchecked(const char *name); +int8_t z_view_keyexpr_from_string_unchecked(z_view_keyexpr_t *keyexpr, const char *name); /** - * Get null-terminated string departing from a :c:type:`z_keyexpr_t`. + * Gets a null-terminated string from a :c:type:`z_keyexpr_t`. * * If given keyexpr contains a declared keyexpr, the resulting owned string will be uninitialized. * In that case, the user must use :c:func:`zp_keyexpr_resolve` to resolve the nesting declarations * and get its full expanded representation. * * Parameters: - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` + * keyexpr: Pointer to a loaned instance of :c:type:`z_keyexpr_t`. + * str: Pointer to an uninitialized :c:type:`z_owned_string_t`. * - * Returns: - * The :c:type:`z_owned_str_t` containing key expression string representation if it's possible + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. */ -z_owned_str_t z_keyexpr_to_string(z_keyexpr_t keyexpr); +int8_t z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *str); /** - * Returns the key expression's internal string by aliasing it. + * Builds a null-terminated string from a :c:type:`z_loaned_keyexpr_t` for a given + * :c:type:`z_loaned_session_t`. * * Parameters: - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` + * zs: Pointer to a :c:type:`z_loaned_session_t` to resolve the keyexpr. + * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to be resolved. + * str: Pointer to an uninitialized :c:type:`z_owned_string_t`. * - * Returns: - * The :c:type:`z_bytes_t` pointing to key expression string representation if it's possible + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. */ -z_bytes_t z_keyexpr_as_bytes(z_keyexpr_t keyexpr); - -/** - * Indicates if the key expression has been declared but don't guarantee it's still in session. - * - * If given keyexpr was declared, to retrieve the keyexpr string representation the user must use - * :c:func:zp_keyexpr_resolve - * - * Parameters: - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` - * - * Returns: - * Returns ``true`` if the keyexpr was declared or ``false`` otherwise. - */ -_Bool zp_keyexpr_was_declared(const 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 dropping the returned string using ``z_free``. - * - * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` to resolve the keyexpr. - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to be resolved. - * - * Returns: - * The string representation of a keyexpr for a given session. - */ -z_owned_str_t zp_keyexpr_resolve(z_session_t zs, z_keyexpr_t keyexpr); +int8_t zp_keyexpr_resolve(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *str); /** * Checks if a given keyexpr is valid. * * Parameters: - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to be checked. + * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to be checked. * - * Returns: - * Returns ``true`` if the keyexpr is valid, or ``false`` otherwise. + * Return: + * ``true`` if keyexpr is valid, or ``false`` otherwise. */ -_Bool z_keyexpr_is_initialized(const z_keyexpr_t *keyexpr); +_Bool z_keyexpr_is_initialized(const z_loaned_keyexpr_t *keyexpr); /** - * Check if a given keyexpr is valid and in its canonical form. + * Checks if a given keyexpr is valid and in canonical form. * * Parameters: * start: Pointer to the keyexpr in its string representation as a non-null terminated string. * len: Number of characters in ``start``. * - * Returns: - * Returns ``0`` if the passed string is a valid (and canon) key expression, or a ``negative value`` otherwise. + * Return: + * ``0`` if passed string is a valid (and canon) key expression, or a ``negative value`` otherwise. * Error codes are defined in :c:enum:`zp_keyexpr_canon_status_t`. */ int8_t z_keyexpr_is_canon(const char *start, size_t len); /** - * Check if a given keyexpr is valid and in its canonical form. + * Checks if a given keyexpr is valid and in canonical form. * * Parameters: * start: Pointer to the keyexpr in its string representation as a null terminated string. * len: Number of characters in ``start``. * - * Returns: - * Returns ``0`` if the passed string is a valid (and canon) key expression, or a ``negative value`` otherwise. + * Return: + * ``0`` if passed string is a valid (and canon) key expression, or a ``negative value`` otherwise. * Error codes are defined in :c:enum:`zp_keyexpr_canon_status_t`. */ int8_t zp_keyexpr_is_canon_null_terminated(const char *start); /** - * Canonization of a given keyexpr in its its string representation. + * Canonizes of a given keyexpr in string representation. * The canonization is performed over the passed string, possibly shortening it by modifying ``len``. * * Parameters: * start: Pointer to the keyexpr in its string representation as a non-null terminated string. * len: Number of characters in ``start``. * - * Returns: - * Returns ``0`` if the canonization is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if canonization successful, or a ``negative value`` otherwise. * Error codes are defined in :c:enum:`zp_keyexpr_canon_status_t`. */ int8_t z_keyexpr_canonize(char *start, size_t *len); /** - * Canonization of a given keyexpr in its its string representation. + * Canonizes a given keyexpr in string representation. * The canonization is performed over the passed string, possibly shortening it by modifying ``len``. * * Parameters: * start: Pointer to the keyexpr in its string representation as a null terminated string. * len: Number of characters in ``start``. * - * Returns: - * Returns ``0`` if the canonization is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if canonization successful, or a ``negative value`` otherwise. * Error codes are defined in :c:enum:`zp_keyexpr_canon_status_t`. */ int8_t zp_keyexpr_canonize_null_terminated(char *start); /** - * Check if a given keyexpr contains another keyexpr in its set. + * Checks if a given keyexpr contains another keyexpr in its set. * * Parameters: - * l: The first keyexpr. - * r: The second keyexpr. + * l: Pointer to a :c:type:`z_loaned_keyexpr_t`. + * r: Pointer to a :c:type:`z_loaned_keyexpr_t`. * - * 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 ``-1``, or other ``negative value`` for errors. + * Return: + * ``true`` if ``l`` includes ``r``, i.e. the set defined by ``l`` contains every key belonging to the set + * defined by ``r``. Otherwise, returns ``false``. */ -int8_t z_keyexpr_includes(z_keyexpr_t l, z_keyexpr_t r); +_Bool z_keyexpr_includes(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r); /** - * Check if a given keyexpr contains another keyexpr in its set. + * Checks if a given keyexpr contains another keyexpr in its set. * * Parameters: * l: Pointer to the keyexpr in its string representation as a null terminated string. @@ -212,27 +191,27 @@ int8_t z_keyexpr_includes(z_keyexpr_t l, z_keyexpr_t r); * r: Pointer to the keyexpr in its string representation as a null terminated string. * rlen: Number of characters in ``r``. * - * 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 ``-1``, or other ``negative value`` for errors. + * Return: + * ``true`` if ``l`` includes ``r``, i.e. the set defined by ``l`` contains every key belonging to the set + * defined by ``r``. Otherwise, returns ``false``. */ -int8_t zp_keyexpr_includes_null_terminated(const char *l, const char *r); +_Bool zp_keyexpr_includes_null_terminated(const char *l, const char *r); /** - * Check if a given keyexpr intersects with another keyexpr. + * Checks if a given keyexpr intersects with another keyexpr. * * Parameters: - * l: The first keyexpr. - * r: The second keyexpr. + * l: Pointer to a :c:type:`z_loaned_keyexpr_t`. + * r: Pointer to a :c:type:`z_loaned_keyexpr_t`. * - * 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 a ``-1``, or other ``negative value`` for errors. + * Return: + * ``true`` if keyexprs intersect, i.e. there exists at least one key which is contained in both of the + * sets defined by ``l`` and ``r``. Otherwise, returns ``false``. */ -int8_t z_keyexpr_intersects(z_keyexpr_t l, z_keyexpr_t r); +_Bool z_keyexpr_intersects(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r); /** - * Check if a given keyexpr intersects with another keyexpr. + * Checks if a given keyexpr intersects with another keyexpr. * * Parameters: * l: Pointer to the keyexpr in its string representation as a null terminated string. @@ -240,27 +219,26 @@ int8_t z_keyexpr_intersects(z_keyexpr_t l, z_keyexpr_t r); * r: Pointer to the keyexpr in its string representation as a null terminated string. * rlen: Number of characters in ``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 a ``-1``, or other ``negative value`` for errors. + * Return: + * ``true`` if keyexprs intersect, i.e. there exists at least one key which is contained in both of the + * sets defined by ``l`` and ``r``. Otherwise, returns ``false``. */ -int8_t zp_keyexpr_intersect_null_terminated(const char *l, const char *r); +_Bool zp_keyexpr_intersect_null_terminated(const char *l, const char *r); /** - * Check if a two keyexprs are equal. + * Checks if two keyexpr are equal. * * Parameters: - * l: The first keyexpr. - * r: The second keyexpr. + * l: Pointer to a :c:type:`z_loaned_keyexpr_t`. + * r: Pointer to a :c:type:`z_loaned_keyexpr_t`. * - * Returns: - * Returns ``0`` if both ``l`` and ``r`` are equal. Otherwise, it returns a ``-1``, or other ``negative value`` for - * errors. + * Return: + * ``true`` if both ``l`` and ``r`` are equal. Otherwise, returns ``false``. */ -int8_t z_keyexpr_equals(z_keyexpr_t l, z_keyexpr_t r); +_Bool z_keyexpr_equals(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r); /** - * Check if a two keyexprs are equal. + * Checks if two keyexpr as null terminated string are equal. * * Parameters: * l: Pointer to the keyexpr in its string representation as a null terminated string. @@ -268,951 +246,915 @@ int8_t z_keyexpr_equals(z_keyexpr_t l, z_keyexpr_t r); * r: Pointer to the keyexpr in its string representation as a null terminated string. * 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. + * Return: + * ``true`` if both ``l`` and ``r`` are equal. Otherwise, it returns ``false``. */ -int8_t zp_keyexpr_equals_null_terminated(const char *l, const char *r); +_Bool zp_keyexpr_equals_null_terminated(const char *l, const char *r); /** - * Return a new, zenoh-allocated, empty configuration. + * Builds a new, zenoh-allocated, empty configuration. * It consists in an empty set of properties for zenoh session configuration. * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_config_t` by loaning it using - * ``z_config_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, is - * equivalent to writing ``z_config_loan(&val)``. + * Parameters: + * config: Pointer to uninitialized :c:type:`z_owned_config_t`. + */ +void z_config_new(z_owned_config_t *config); + +/** + * Builds a new, zenoh-allocated, default configuration. + * It consists in a default set of properties for zenoh session configuration. * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. + * Parameters: + * config: Pointer to uninitialized :c:type:`z_owned_config_t`. + */ +void z_config_default(z_owned_config_t *config); + +/** + * Gets the property with the given integer key from the configuration. * - * To check if ``val`` is still valid, you may use ``z_config_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. + * Parameters: + * config: Pointer to a :c:type:`z_loaned_config_t` to get the property from. + * key: Integer key of the requested property. * - * Returns: - * Returns a new, zenoh-allocated, empty configuration. + * Return: + * The requested property value. */ -z_owned_config_t z_config_new(void); +const char *zp_config_get(const z_loaned_config_t *config, uint8_t key); /** - * Return a new, zenoh-allocated, default configuration. - * It consists in a default set of properties for zenoh session configuration. + * Inserts or replaces the property with the given integer key in the configuration. * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_config_t` by loaning it using - * ``z_config_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, is - * equivalent to writing ``z_config_loan(&val)``. + * Parameters: + * config: Pointer to a :c:type:`z_loaned_config_t` to modify. + * key: Integer key of the property to be inserted. + * value: Property value to be inserted. * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. + * Return: + * ``0`` if insertion successful, ``negative value`` otherwise. + */ +int8_t zp_config_insert(z_loaned_config_t *config, uint8_t key, const char *value); + +/** + * Builds a new :c:type:`z_owned_scouting_config_t` with a default set of properties for scouting configuration. * - * To check if ``val`` is still valid, you may use ``z_config_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. + * Parameters: + * sc: Pointer to an uninitialized :c:type:`z_owned_scouting_config_t`. + */ +void z_scouting_config_default(z_owned_scouting_config_t *sc); + +/** + * Builds a new :c:type:`z_owned_scouting_config_t` with values from a :c:type:`z_loaned_config_t`. + * + * Parameters: + * config: Pointer to a :c:type:`z_owned_config_t` to get the values from. + * sc: Pointer to an uninitialized :c:type:`z_owned_scouting_config_t`. * - * Returns: - * Returns a new, zenoh-allocated, default configuration. + * Return: + * ``0`` if build successful, ``negative value`` otherwise. */ -z_owned_config_t z_config_default(void); +int8_t z_scouting_config_from(z_owned_scouting_config_t *sc, const z_loaned_config_t *config); /** * Gets the property with the given integer key from the configuration. * * Parameters: - * config: A loaned instance of :c:type:`z_owned_config_t`. + * config: Pointer to a :c:type:`z_loaned_scouting_config_t` to get the property from. * key: Integer key for the requested property. * - * Returns: - * Returns the property with the given integer key from the configuration. + * Return: + * The requested property value. */ -const char *zp_config_get(z_config_t config, uint8_t key); +const char *zp_scouting_config_get(const z_loaned_scouting_config_t *config, uint8_t key); /** - * Inserts or replaces the property with the given integer key in the configuration. + * Inserts or replace the property with the given integer key in the configuration. * * Parameters: - * config: A loaned instance of :c:type:`z_owned_config_t`. + * config: Pointer to a :c:type:`z_loaned_scouting_config_t` to modify. * key: Integer key for the property to be inserted. * value: Property value to be inserted. * - * Returns: - * Returns ``0`` if the insertion is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if insertion successful, ``negative value`` otherwise. */ -int8_t zp_config_insert(z_config_t config, uint8_t key, z_string_t value); +int8_t zp_scouting_config_insert(z_loaned_scouting_config_t *config, uint8_t key, const char *value); /** - * Return a new, zenoh-allocated, default scouting configuration. - * It consists in a default set of properties for scouting configuration. + * Builds a new :c:type:`z_owned_encoding_t`. * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_scouting_config_t` by loaning it - * using - * ``z_scouting_config_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, - * is equivalent to writing ``z_config_loan(&val)``. + * Parameters: + * encoding: Pointer to an uninitialized :c:type:`z_owned_encoding_t`. + * id: A known :c:type:`z_encoding_id_t` value. + * schema: Pointer to a custom schema string value. * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. + */ +int8_t zp_encoding_make(z_owned_encoding_t *encoding, z_encoding_id_t id, const char *schema); + +/** + * Builds a new a :c:type:`z_owned_encoding_t` with default value. * - * To check if ``val`` is still valid, you may use ``z_scouting_config_check(&val)`` or ``z_check(val)`` if your - * compiler supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. + * Parameters: + * encoding: Pointer to an uninitialized :c:type:`z_owned_encoding_t`. * - * Returns: - * Returns a new, zenoh-allocated, default scouting configuration. + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. */ -z_owned_scouting_config_t z_scouting_config_default(void); +int8_t zp_encoding_default(z_owned_encoding_t *encoding); /** - * Return a new, zenoh-allocated, scouting configuration extracted from a :c:type:`z_owned_config_t`. - * It consists in a default set of properties for scouting configuration. + * Checks if a :c:type:`z_owned_encoding_t` has non-default values. * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_scouting_config_t` by loaning it - * using - * ``z_scouting_config_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, - * is equivalent to writing ``z_config_loan(&val)``. + * Return: + * ``true`` if encoding is in non-default state, ``false`` otherwise. + */ +_Bool z_encoding_check(const z_owned_encoding_t *encoding); + +/** + * Free the memory of a :c:type:`z_owned_encoding_t`. + */ +void z_encoding_drop(z_owned_encoding_t *encoding); + +/** + * Gets a loaned version of a :c:type:`z_owned_encoding_t`. * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. + * Parameters: + * encoding: Pointer to a :c:type:`z_owned_encoding_t` to loan. * - * To check if ``val`` is still valid, you may use ``z_scouting_config_check(&val)`` or ``z_check(val)`` if your - * compiler supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. + * Return: + * Pointer to the loaned version. + */ +const z_loaned_encoding_t *z_encoding_loan(const z_owned_encoding_t *encoding); + +/** + * Gets a moved version of a :c:type:`z_owned_encoding_t`. * * Parameters: - * config: A loaned instance of :c:type:`z_owned_config_t`. + * encoding: Pointer to a :c:type:`z_owned_encoding_t` to move. * - * Returns: - * Returns a new, zenoh-allocated, default scouting configuration. + * Return: + * Pointer to the moved version. */ -z_owned_scouting_config_t z_scouting_config_from(z_config_t config); +z_owned_encoding_t *z_encoding_move(z_owned_encoding_t *encoding); /** - * Gets the property with the given integer key from the configuration. + * Builds a :c:type:`z_owned_encoding_t` with default value. * * Parameters: - * config: A loaned instance of :c:type:`z_owned_scouting_config_t`. - * key: Integer key for the requested property. + * encoding: Pointer to an uninitialized :c:type:`z_owned_encoding_t`. * - * Returns: - * Returns the property with the given integer key from the configuration. + * Return: + * ``0`` if creation successful,``negative value`` otherwise. */ -const char *zp_scouting_config_get(z_scouting_config_t config, uint8_t key); +int8_t z_encoding_null(z_owned_encoding_t *encoding); /** - * Inserts or replaces the property with the given integer key in the configuration. + * Gets the bytes data from a value payload by aliasing it. * * Parameters: - * config: A loaned instance of :c:type:`z_owned_scouting_config_t`. - * key: Integer key for the property to be inserted. - * value: Property value to be inserted. + * value: Pointer to a :c:type:`z_loaned_value_t` to get data from. + * + * Return: + * Pointer to the data as a :c:type:`z_loaned_bytes_t`. + */ +const z_loaned_bytes_t *z_value_payload(const z_loaned_value_t *value); + +/** + * Gets total number of bytes in a bytes array. * - * Returns: - * Returns ``0`` if the insertion is successful, or a ``negative value`` otherwise. + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to get length from. + * + * Return: + * The number of bytes. */ -int8_t zp_scouting_config_insert(z_scouting_config_t config, uint8_t key, z_string_t value); +size_t z_bytes_len(const z_loaned_bytes_t *bytes); /** - * Constructs a :c:type:`z_encoding_t`. + * Decodes data into a :c:type:`z_owned_string_t` * * Parameters: - * prefix: A known :c:type:`z_encoding_prefix_t`. - * suffix: A custom suffix to be appended to the prefix. + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode. + * str: Pointer to an uninitialized :c:type:`z_owned_string_t` to contain the decoded string. * - * Returns: - * Returns the constructed :c:type:`z_encoding_t`. + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. */ -z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix); +int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *str); /** - * Constructs a default encoding. + * Encodes a string into a :c:type:`z_owned_bytes_t` * - * Returns: - * Returns the constructed :c:type:`z_encoding_t`. + * Parameters: + * buffer: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded string. + * str: Pointer to the string to encode. + * + * Return: + * ``0`` if encode successful, ``negative value`` otherwise. */ -z_encoding_t z_encoding_default(void); +int8_t z_bytes_encode_from_string(z_owned_bytes_t *buffer, const z_loaned_string_t *str); /** + * Checks validity of a timestamp * - * Checks validity of the timestamp + * Parameters: + * ts: Timestamp value to check validity of. * + * Return: + * ``true`` if timestamp is valid, ``false`` otherwise. */ _Bool z_timestamp_check(z_timestamp_t ts); /** - * Constructs a default query target. + * Builds a default query target. * - * Returns: - * Returns the constructed :c:type:`z_query_target_t`. + * Return: + * The constructed :c:type:`z_query_target_t`. */ z_query_target_t z_query_target_default(void); /** - * Automatic query consolidation strategy selection. + * Builds an automatic query consolidation :c:type:`z_query_consolidation_t`. * - * A query consolidation strategy will automatically be selected depending the query selector. - * If the selector contains time range properties, no consolidation is performed. + * A query consolidation strategy will automatically be selected depending on the query selector. + * If selector contains time range properties, no consolidation is performed. * Otherwise the :c:func:`z_query_consolidation_latest` strategy is used. * - * Returns: - * Returns the constructed :c:type:`z_query_consolidation_t`. + * Return: + * The constructed :c:type:`z_query_consolidation_t`. */ z_query_consolidation_t z_query_consolidation_auto(void); /** - * Constructs a default :c:type:`z_query_consolidation_t`. + * Builds a default :c:type:`z_query_consolidation_t`. * - * Returns: - * Returns the constructed :c:type:`z_query_consolidation_t`. + * Return: + * The constructed :c:type:`z_query_consolidation_t`. */ z_query_consolidation_t z_query_consolidation_default(void); /** - * Latest consolidation. + * Builds a latest query consolidation :c:type:`z_query_consolidation_t`. * * This strategy optimizes bandwidth on all links in the system but will provide a very poor latency. * - * Returns: - * Returns the constructed :c:type:`z_query_consolidation_t`. + * Return: + * The constructed :c:type:`z_query_consolidation_t`. */ z_query_consolidation_t z_query_consolidation_latest(void); /** - * Monotonic consolidation. + * Builds a monotonic query consolidation :c:type:`z_query_consolidation_t`. * * This strategy offers the best latency. Replies are directly transmitted to the application when received * without needing to wait for all replies. This mode does not guarantee that there will be no duplicates. * - * Returns: - * Returns the constructed :c:type:`z_query_consolidation_t`. + * Return: + * The constructed :c:type:`z_query_consolidation_t`. */ z_query_consolidation_t z_query_consolidation_monotonic(void); /** - * No consolidation. + * Builds a no query consolidation :c:type:`z_query_consolidation_t`. * * This strategy is useful when querying timeseries data bases or when using quorums. * - * Returns: - * Returns the constructed :c:type:`z_query_consolidation_t`. + * Return: + * The constructed :c:type:`z_query_consolidation_t`. */ z_query_consolidation_t z_query_consolidation_none(void); /** - * Get a query's value selector by aliasing it. + * Gets a query parameters field. * * Parameters: - * query: Pointer to the query to get the value selector from. - * - * Returns: - * Returns the value selector wrapped as a :c:type:`z_bytes_t`, since value selector is a user-defined representation. + * query: Pointer to the :c:type:`z_loaned_query_t` to get the parameters from. + * parameters: Pointer to an uninitialized :c:type:`z_view_string_t` to contain the parameters. */ -z_bytes_t z_query_parameters(const z_query_t *query); +void z_query_parameters(const z_loaned_query_t *query, z_view_string_t *parameters); /** - * Get a query's payload value by aliasing it. - * Note: This API has been marked as unstable: it works as advertised, but we may change it in a future release. + * Gets a query value payload by aliasing it. * * Parameters: - * query: Pointer to the query to get the payload from. + * query: Pointer to the :c:type:`z_loaned_query_t` to get the value from. * - * Returns: - * Returns the payload wrapped as a :c:type:`z_value_t`, since payload value is a user-defined representation. + * Return: + * Pointer to the value payload as a :c:type:`z_loaned_value_t`. */ -z_value_t z_query_value(const z_query_t *query); +const z_loaned_value_t *z_query_value(const z_loaned_query_t *query); #if Z_FEATURE_ATTACHMENT == 1 /** - * Get a query's attachment value by aliasing it. - * Note: This API has been marked as unstable: it works as advertised, but we may change it in a future release. + * Gets a query attachment value by aliasing it. * * Parameters: - * query: Pointer to the query to get the attachment from. + * query: Pointer to the :c:type:`z_loaned_query_t` to get the attachment from. * - * Returns: - * Returns the attachment wrapped as a :c:type:`z_attachment_t`, since attachment is a user-defined representation. + * Return: + * The attachment value wrapped as a :c:type:`z_attachment_t`. */ -z_attachment_t z_query_attachment(const z_query_t *query); +z_attachment_t z_query_attachment(const z_loaned_query_t *query); #endif /** - * Get a query's key by aliasing it. + * Gets a query keyexpr by aliasing it. * * Parameters: - * query: Pointer to the query to get keyexpr from. + * query: Pointer to the :c:type:`z_loaned_query_t` to get the keyexpr from. * - * Returns: - * Returns the :c:type:`z_keyexpr_t` associated to the query. + * Return: + * The keyexpr wrapped as a:c:type:`z_keyexpr_t`. */ -z_keyexpr_t z_query_keyexpr(const z_query_t *query); +const z_loaned_keyexpr_t *z_query_keyexpr(const z_loaned_query_t *query); /** - * Return a new sample closure. + * Builds a new sample closure. * It consists on a structure that contains all the elements for stateful, memory-leak-free callbacks. * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_closure_sample_t` by loaning it using - * ``z_closure_sample_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, - * is equivalent to writing ``z_closure_sample_loan(&val)``. - * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. - * - * To check if ``val`` is still valid, you may use ``z_closure_sample_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. - * * Parameters: - * call: the typical callback function. ``context`` will be passed as its last argument. - * drop: allows the callback's state to be freed. ``context`` will be passed as its last argument. - * context: a pointer to an arbitrary state. + * call: Pointer to the callback function. ``context`` will be passed as its last argument. + * drop: Pointer to the function that will free the callback state. ``context`` will be passed as its last argument. + * context: Pointer to an arbitrary state. * - * Returns: - * Returns a new sample closure. + * Return: + * The sample closure. */ -z_owned_closure_sample_t z_closure_sample(_z_data_handler_t call, _z_dropper_handler_t drop, void *context); +int8_t z_closure_sample(z_owned_closure_sample_t *closure, z_data_handler_t call, z_dropper_handler_t drop, + void *context); /** - * Return a new sample closure. + * Builds a new sample closure. * It consists on a structure that contains all the elements for stateful, memory-leak-free callbacks. * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. - * - * To check if ``val`` is still valid, you may use ``z_closure_owned_sample_check(&val)`` or ``z_check(val)`` if your - * compiler supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. - * * Parameters: - * call: the typical callback function. ``context`` will be passed as its last argument. - * drop: allows the callback's state to be freed. ``context`` will be passed as its last argument. - * context: a pointer to an arbitrary state. + * call: Pointer to the callback function. ``context`` will be passed as its last argument. + * drop: Pointer to the function that will free the callback state. ``context`` will be passed as its last argument. + * context: Pointer to an arbitrary state. * - * Returns: - * Returns a new sample closure. + * Return: + * The sample closure. */ -z_owned_closure_owned_sample_t z_closure_owned_sample(_z_owned_sample_handler_t call, _z_dropper_handler_t drop, - void *context); +int8_t z_closure_owned_sample(z_owned_closure_owned_sample_t *closure, z_owned_sample_handler_t call, + z_dropper_handler_t drop, void *context); /** - * Return a new query closure. + * Builds a new query closure. * It consists on a structure that contains all the elements for stateful, memory-leak-free callbacks. * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_closure_query_t` by loaning it using - * ``z_closure_query_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, is - * equivalent to writing ``z_closure_query_loan(&val)``. - * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. - * - * To check if ``val`` is still valid, you may use ``z_closure_query_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. - * * Parameters: - * call: the typical callback function. ``context`` will be passed as its last argument. - * drop: allows the callback's state to be freed. ``context`` will be passed as its last argument. - * context: a pointer to an arbitrary state. + * call: Pointer to the callback function. ``context`` will be passed as its last argument. + * drop: Pointer to the function that will free the callback state. ``context`` will be passed as its last argument. + * context: Pointer to an arbitrary state. * - * Returns: - * Returns a new query closure. + * Return: + * The query closure. */ -z_owned_closure_query_t z_closure_query(_z_queryable_handler_t call, _z_dropper_handler_t drop, void *context); +int8_t z_closure_query(z_owned_closure_query_t *closure, z_queryable_handler_t call, z_dropper_handler_t drop, + void *context); /** - * Return a new query closure. + * Builds a new query closure. * It consists on a structure that contains all the elements for stateful, memory-leak-free callbacks. * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. - * - * To check if ``val`` is still valid, you may use ``z_closure_owned_query_check(&val)`` or ``z_check(val)`` if your - * compiler supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. - * * Parameters: - * call: the typical callback function. ``context`` will be passed as its last argument. - * drop: allows the callback's state to be freed. ``context`` will be passed as its last argument. - * context: a pointer to an arbitrary state. + * call: Pointer to the callback function. ``context`` will be passed as its last argument. + * drop: Pointer to the function that will free the callback state. ``context`` will be passed as its last argument. + * context: Pointer to an arbitrary state. * - * Returns: - * Returns a new query closure. + * Return: + * The query closure. */ -z_owned_closure_owned_query_t z_closure_owned_query(_z_owned_query_handler_t call, _z_dropper_handler_t drop, - void *context); +int8_t z_closure_owned_query(z_owned_closure_owned_query_t *closure, z_owned_query_handler_t call, + z_dropper_handler_t drop, void *context); /** - * Return a new reply closure. + * Builds a new reply closure. * It consists on a structure that contains all the elements for stateful, memory-leak-free callbacks. * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_closure_reply_t` by loaning it using - * ``z_closure_reply_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, is - * equivalent to writing ``z_closure_reply_loan(&val)``. - * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. - * - * To check if ``val`` is still valid, you may use ``z_closure_reply_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. - * * Parameters: - * call: the typical callback function. ``context`` will be passed as its last argument. - * drop: allows the callback's state to be freed. ``context`` will be passed as its last argument. - * context: a pointer to an arbitrary state. + * call: Pointer to the callback function. ``context`` will be passed as its last argument. + * drop: Pointer to the function that will free the callback state. ``context`` will be passed as its last argument. + * context: Pointer to an arbitrary state. * - * Returns: - * Returns a new reply closure. + * Return: + * The reply closure. */ -z_owned_closure_reply_t z_closure_reply(z_owned_reply_handler_t call, _z_dropper_handler_t drop, void *context); +int8_t z_closure_reply(z_owned_closure_reply_t *closure, z_reply_handler_t call, z_dropper_handler_t drop, + void *context); /** - * Return a new hello closure. + * Builds a new reply closure. * It consists on a structure that contains all the elements for stateful, memory-leak-free callbacks. * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_closure_hello_t` by loaning it using - * ``z_closure_hello_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, - * is equivalent to writing ``z_closure_hello_loan(&val)``. - * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. - * - * To check if ``val`` is still valid, you may use ``z_closure_hello_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. - * * Parameters: - * call: the typical callback function. ``context`` will be passed as its last argument. - * drop: allows the callback's state to be freed. ``context`` will be passed as its last argument. - * context: a pointer to an arbitrary state. + * call: Pointer to the callback function. ``context`` will be passed as its last argument. + * drop: Pointer to the function that will free the callback state. ``context`` will be passed as its last argument. + * context: Pointer to an arbitrary state. * - * Returns: - * Returns a new hello closure. + * Return: + * The reply closure. */ -z_owned_closure_hello_t z_closure_hello(z_owned_hello_handler_t call, _z_dropper_handler_t drop, void *context); +int8_t z_closure_owned_reply(z_owned_closure_owned_reply_t *closure, z_owned_reply_handler_t call, + z_dropper_handler_t drop, void *context); /** - * Return a new zid closure. + * Builds a new hello closure. * It consists on a structure that contains all the elements for stateful, memory-leak-free callbacks. * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_closure_zid_t` by loaning it using - * ``z_closure_zid_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, is - * equivalent to writing ``z_closure_zid_loan(&val)``. - * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. + * Parameters: + * call: Pointer to the callback function. ``context`` will be passed as its last argument. + * drop: Pointer to the function that will free the callback state. ``context`` will be passed as its last argument. + * context: Pointer to an arbitrary state. * - * To check if ``val`` is still valid, you may use ``z_closure_zid_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. + * Return: + * The hello closure. + */ +int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_owned_hello_handler_t call, z_dropper_handler_t drop, + void *context); + +/** + * Builds a new zid closure. + * It consists on a structure that contains all the elements for stateful, memory-leak-free callbacks. * * Parameters: - * call: the typical callback function. ``context`` will be passed as its last argument. - * drop: allows the callback's state to be freed. ``context`` will be passed as its last argument. - * context: a pointer to an arbitrary state. + * call: Pointer to the callback function. ``context`` will be passed as its last argument. + * drop: Pointer to the function that will free the callback state. ``context`` will be passed as its last argument. + * context: Pointer to an arbitrary state. * - * Returns: - * Returns a new zid closure. + * Return: + * The hello closure. */ -z_owned_closure_zid_t z_closure_zid(z_id_handler_t call, _z_dropper_handler_t drop, void *context); +int8_t z_closure_zid(z_owned_closure_zid_t *closure, z_id_handler_t call, z_dropper_handler_t drop, void *context); /**************** Loans ****************/ -#define _OWNED_FUNCTIONS(type, ownedtype, name) \ - _Bool z_##name##_check(const ownedtype *name); \ - 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); \ - 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) -_OWNED_FUNCTIONS(z_config_t, z_owned_config_t, config) -_OWNED_FUNCTIONS(z_scouting_config_t, z_owned_scouting_config_t, scouting_config) -_OWNED_FUNCTIONS(z_session_t, z_owned_session_t, session) -_OWNED_FUNCTIONS(z_subscriber_t, z_owned_subscriber_t, subscriber) -_OWNED_FUNCTIONS(z_publisher_t, z_owned_publisher_t, publisher) -_OWNED_FUNCTIONS(z_queryable_t, z_owned_queryable_t, queryable) -_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) -_OWNED_FUNCTIONS(z_sample_t, z_owned_sample_t, sample) -_OWNED_FUNCTIONS(z_query_t, z_owned_query_t, query) +#define _OWNED_FUNCTIONS(loanedtype, ownedtype, name) \ + _Bool z_##name##_check(const ownedtype *obj); \ + const loanedtype *z_##name##_loan(const ownedtype *obj); \ + loanedtype *z_##name##_loan_mut(ownedtype *obj); \ + ownedtype *z_##name##_move(ownedtype *obj); \ + int8_t z_##name##_clone(ownedtype *obj, const loanedtype *src); \ + void z_##name##_drop(ownedtype *obj); \ + void z_##name##_null(ownedtype *obj); + +_OWNED_FUNCTIONS(z_loaned_string_t, z_owned_string_t, string) +_OWNED_FUNCTIONS(z_loaned_keyexpr_t, z_owned_keyexpr_t, keyexpr) +_OWNED_FUNCTIONS(z_loaned_config_t, z_owned_config_t, config) +_OWNED_FUNCTIONS(z_loaned_scouting_config_t, z_owned_scouting_config_t, scouting_config) +_OWNED_FUNCTIONS(z_loaned_session_t, z_owned_session_t, session) +_OWNED_FUNCTIONS(z_loaned_subscriber_t, z_owned_subscriber_t, subscriber) +_OWNED_FUNCTIONS(z_loaned_publisher_t, z_owned_publisher_t, publisher) +_OWNED_FUNCTIONS(z_loaned_queryable_t, z_owned_queryable_t, queryable) +_OWNED_FUNCTIONS(z_loaned_hello_t, z_owned_hello_t, hello) +_OWNED_FUNCTIONS(z_loaned_reply_t, z_owned_reply_t, reply) +_OWNED_FUNCTIONS(z_loaned_string_array_t, z_owned_string_array_t, string_array) +_OWNED_FUNCTIONS(z_loaned_sample_t, z_owned_sample_t, sample) +_OWNED_FUNCTIONS(z_loaned_query_t, z_owned_query_t, query) +_OWNED_FUNCTIONS(z_loaned_bytes_t, z_owned_bytes_t, bytes) +_OWNED_FUNCTIONS(z_loaned_value_t, z_owned_value_t, value) #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); + void z_##name##_null(ownedtype *name); _OWNED_FUNCTIONS_CLOSURE(z_owned_closure_sample_t, closure_sample) _OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_sample_t, closure_owned_sample) _OWNED_FUNCTIONS_CLOSURE(z_owned_closure_query_t, closure_query) _OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_query_t, closure_owned_query) _OWNED_FUNCTIONS_CLOSURE(z_owned_closure_reply_t, closure_reply) +_OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_reply_t, closure_owned_reply) _OWNED_FUNCTIONS_CLOSURE(z_owned_closure_hello_t, closure_hello) _OWNED_FUNCTIONS_CLOSURE(z_owned_closure_zid_t, closure_zid) -/************* Primitives **************/ +#define _VIEW_FUNCTIONS(loanedtype, viewtype, name) \ + const loanedtype *z_view_##name##_loan(const viewtype *name); \ + loanedtype *z_view_##name##_loan_mut(viewtype *name); \ + void z_view_##name##_null(viewtype *name); + +_VIEW_FUNCTIONS(z_loaned_keyexpr_t, z_view_keyexpr_t, keyexpr) +_VIEW_FUNCTIONS(z_loaned_string_t, z_view_string_t, string) + +// Gets internal value from refcounted type (e.g. z_loaned_session_t, z_query_t) +#define _Z_RC_IN_VAL(arg) ((arg)->in->val) + +// Gets internal value from refcounted owned type (e.g. z_owned_session_t, z_owned_query_t) +#define _Z_OWNED_RC_IN_VAL(arg) ((arg)->_rc.in->val) + /** - * Looks for other Zenoh-enabled entities like routers and/or peers. + * Loans a :c:type:`z_owned_sample_t`. * * Parameters: - * config: A moved instance of :c:type:`z_owned_scouting_config_t` containing the set properties to configure the - * scouting. callback: A moved instance of :c:type:`z_owned_closure_hello_t` containing the callbacks to be called. + * sample: Pointer to a :c:type:`z_owned_sample_t` to loan. * - * Returns: - * Returns ``0`` if the scouting is successful triggered, or a ``negative value`` otherwise. + * Return: + * Pointer to the loaned sample as a :c:type:`z_loaned_sample_t`. */ -int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callback); +const z_loaned_sample_t *z_sample_loan(const z_owned_sample_t *sample); /** - * Opens a Zenoh session. + * Gets data from a :c:type:`z_loaned_string_t`. * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_session_t` by loaning it using - * ``z_session_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, is - * equivalent to writing ``z_session_loan(&val)``. + * Parameters: + * str: Pointer to a :c:type:`z_loaned_string_t` to get data from. * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. + * Return: + * Pointer to the string data. + */ +const char *z_string_data(const z_loaned_string_t *str); + +/************* Primitives **************/ +/** + * Scouts for other Zenoh entities like routers and/or peers. * - * To check if ``val`` is still valid, you may use ``z_session_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. + * Parameters: + * config: Pointer to a moved :c:type:`z_owned_scouting_config_t` to configure the scouting with. + * callback: Pointer to a moved :c:type:`z_owned_closure_hello_t` callback. + * + * Return: + * ``0`` if scouting successfully triggered, ``negative value`` otherwise. + */ +int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callback); + +/** + * Opens a Zenoh session. * * Parameters: - * config: A moved instance of :c:type:`z_owned_config_t` containing the set properties to configure the session. + * zs: Pointer to an uninitialized :c:type:`z_owned_session_t` to store the session info. + * config: Pointer to a moved :c:type:`z_owned_config_t` to configure the session with. * - * Returns: - * A :c:type:`z_owned_session_t` with either a valid open session or a failing session. - * Should the session opening fail, ``z_check(val)`` ing the returned value will return ``false``. + * Return: + * ``0`` if open successful, ``negative value`` otherwise. */ -z_owned_session_t z_open(z_owned_config_t *config); +int8_t z_open(z_owned_session_t *zs, z_owned_config_t *config); /** * Closes a Zenoh session. * * Parameters: - * zs: A moved instance of the the :c:type:`z_owned_session_t` to close. + * zs: Pointer to a moved :c:type:`z_owned_session_t` to close. * - * Returns: - * Returns ``0`` if the session is successful closed, or a ``negative value`` otherwise. + * Return: + * ``0`` if close successful, ``negative value`` otherwise. */ int8_t z_close(z_owned_session_t *zs); /** - * Fetches the Zenoh IDs of all connected peers. + * Fetches Zenoh IDs of all connected peers. * - * :c:var:`callback` will be called once for each ID. It is guaranteed to never be called concurrently, + * The callback will be called once for each ID. It is guaranteed to never be called concurrently, * and to be dropped before this function exits. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` to inquiry. - * callback: A moved instance of :c:type:`z_owned_closure_zid_t` containing the callbacks to be called. + * zs: Pointer to :c:type:`z_loaned_session_t` to fetch peer id from. + * callback: Pointer to a moved :c:type:`z_owned_closure_zid_t` callback. * - * Returns: - * Returns ``0`` if the info is successful triggered, or a ``negative value`` otherwise. + * Return: + * ``0`` if operation successfully triggered, ``negative value`` otherwise. */ -int8_t z_info_peers_zid(const z_session_t zs, z_owned_closure_zid_t *callback); +int8_t z_info_peers_zid(const z_loaned_session_t *zs, z_owned_closure_zid_t *callback); /** - * Fetches the Zenoh IDs of all connected routers. + * Fetches Zenoh IDs of all connected routers. * - * :c:var:`callback` will be called once for each ID. It is guaranteed to never be called concurrently, + * The callback will be called once for each ID. It is guaranteed to never be called concurrently, * and to be dropped before this function exits. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` to inquiry. - * callback: A moved instance of :c:type:`z_owned_closure_zid_t` containing the callbacks to be called. + * zs: Pointer to a :c:type:`z_loaned_session_t` to fetch router id from. + * callback: Pointer to a moved :c:type:`z_owned_closure_zid_t` callback. * - * Returns: - * Returns ``0`` if the info is successful triggered, or a ``negative value`` otherwise. + * Return: + * ``0`` if operation successfully triggered, ``negative value`` otherwise. */ -int8_t z_info_routers_zid(const z_session_t zs, z_owned_closure_zid_t *callback); +int8_t z_info_routers_zid(const z_loaned_session_t *zs, z_owned_closure_zid_t *callback); /** - * Get the local Zenoh ID associated to a given Zenoh session. + * Gets the local Zenoh ID associated to a given Zenoh session. * - * Unless the :c:type:`z_session_t` is invalid, that ID is guaranteed to be non-zero. - * In other words, this function returning an array of 16 zeros means you failed to pass it a valid session. + * If this function returns an array of 16 zeros, this means the session is invalid. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` to inquiry. + * zs: Pointer to a :c:type:`z_loaned_session_t` to get the id from. * - * Returns: - * Returns the local Zenoh ID of the given :c:type:`z_session_t`. + * Return: + * The local Zenoh ID of the session as :c:type:`z_id_t`. */ -z_id_t z_info_zid(const z_session_t zs); +z_id_t z_info_zid(const z_loaned_session_t *zs); /** - * Get a sample's keyexpr value by aliasing it. - * Note: This API has been marked as unstable: it works as advertised, but we may change it in a future release. + * Gets the keyexpr from a sample by aliasing it. * * Parameters: - * sample: Pointer to the sample to get the keyexpr from. + * sample: Pointer to a :c:type:`z_loaned_sample_t` to get the keyexpr from. * - * Returns: - * Returns the keyexpr wrapped as a :c:type:`z_keyexpr_t`. + * Return: + * The keyexpr wrapped as a :c:type:`z_loaned_keyexpr_t`. */ -z_keyexpr_t z_sample_keyexpr(const z_sample_t *sample); +const z_loaned_keyexpr_t *z_sample_keyexpr(const z_loaned_sample_t *sample); /** - * Get a sample's payload value by aliasing it. - * Note: This API has been marked as unstable: it works as advertised, but we may change it in a future release. + * Gets the payload of a sample by aliasing it. * * Parameters: - * sample: Pointer to the sample to get the payload from. + * sample: Pointer to a :c:type:`z_loaned_sample_t` to get the payload from. * - * Returns: - * Returns the payload wrapped as a :c:type:`z_bytes_t`. + * Return: + * The payload wrapped as a :c:type:`z_loaned_bytes_t`. */ -z_bytes_t z_sample_payload(const z_sample_t *sample); +const z_loaned_bytes_t *z_sample_payload(const z_loaned_sample_t *sample); /** - * Get a sample's timestamp value by aliasing it. - * Note: This API has been marked as unstable: it works as advertised, but we may change it in a future release. + * Gets the timestamp of a sample by aliasing it. * * Parameters: - * sample: Pointer to the sample to get the timestamp from. + * sample: Pointer to a :c:type:`z_loaned_sample_t` to get the timestamp from. * - * Returns: - * Returns the timestamp wrapped as a :c:type:`z_timestamp_t`. + * Return: + * The timestamp wrapped as a :c:type:`z_timestamp_t`. */ -z_timestamp_t z_sample_timestamp(const z_sample_t *sample); +z_timestamp_t z_sample_timestamp(const z_loaned_sample_t *sample); /** - * Get a sample's encoding value by aliasing it. - * Note: This API has been marked as unstable: it works as advertised, but we may change it in a future release. + * Gets the encoding of a sample by aliasing it. * * Parameters: - * sample: Pointer to the sample to get the encoding from. + * sample: Pointer to a :c:type:`z_loaned_sample_t` to get the encoding from. * - * Returns: - * Returns the encoding wrapped as a :c:type:`z_encoding_t`. + * Return: + * The encoding wrapped as a :c:type:`z_loaned_encoding_t*`. */ -z_encoding_t z_sample_encoding(const z_sample_t *sample); +const z_loaned_encoding_t *z_sample_encoding(const z_loaned_sample_t *sample); /** - * Get a sample's kind by aliasing it. - * Note: This API has been marked as unstable: it works as advertised, but we may change it in a future release. + * Gets the kind of a sample by aliasing it. * * Parameters: - * sample: Pointer to the sample to get the sample kind from. + * sample: Pointer to a :c:type:`z_loaned_sample_t` to get the kind from. * - * Returns: - * Returns the sample kind wrapped as a :c:type:`z_sample_kind_t`. + * Return: + * The sample kind wrapped as a :c:type:`z_sample_kind_t`. */ -z_sample_kind_t z_sample_kind(const z_sample_t *sample); +z_sample_kind_t z_sample_kind(const z_loaned_sample_t *sample); /** - * Get a sample's qos value by aliasing it. - * Note: This API has been marked as unstable: it works as advertised, but we may change it in a future release. + * Gets the qos value of a sample by aliasing it. * * Parameters: - * sample: Pointer to the sample to get the qos from. + * sample: Pointer to a :c:type:`z_loaned_sample_t` to get the qos from. * - * Returns: - * Returns the qos wrapped as a :c:type:`z_qos_t`. + * Return: + * The qos wrapped as a :c:type:`z_qos_t`. */ -z_qos_t z_sample_qos(const z_sample_t *sample); +z_qos_t z_sample_qos(const z_loaned_sample_t *sample); #if Z_FEATURE_ATTACHMENT == 1 /** - * Get a sample's attachment value by aliasing it. - * Note: This API has been marked as unstable: it works as advertised, but we may change it in a future release. + * Gets the attachment of a value by aliasing it. * * Parameters: - * sample: Pointer to the sample to get the attachment from. + * sample: Pointer to a :c:type:`z_loaned_sample_t` to get the attachment from. * - * Returns: - * Returns the attachment wrapped as a :c:type:`z_attachment_t`. + * Return: + * The attachment wrapped as a :c:type:`z_attachment_t`. */ -z_attachment_t z_sample_attachment(const z_sample_t *sample); +z_attachment_t z_sample_attachment(const z_loaned_sample_t *sample); #endif #if Z_FEATURE_PUBLICATION == 1 /** - * Constructs the default values for the put operation. + * Builds a :c:type:`z_put_options_t` with default values. * - * Returns: - * Returns the constructed :c:type:`z_put_options_t`. + * Parameters: + * Pointer to an uninitialized :c:type:`z_put_options_t`. */ -z_put_options_t z_put_options_default(void); + +void z_put_options_default(z_put_options_t *options); /** - * Constructs the default values for the delete operation. + * Builds a :c:type:`z_delete_options_t` with default values. * - * Returns: - * Returns the constructed :c:type:`z_delete_options_t`. + * Parameters: + * Pointer to an uninitialized :c:type:`z_delete_options_t`. */ -z_delete_options_t z_delete_options_default(void); +void z_delete_options_default(z_delete_options_t *options); /** * Puts data for a given keyexpr. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` through where data will be put. - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to put. + * zs: Pointer to a :c:type:`z_loaned_session_t` to put the data through. + * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to put the data for. * payload: Pointer to the data to put. * payload_len: The length of the ``payload``. - * options: The put options to be applied in the put operation. + * options: Pointer to a :c:type:`z_put_options_t` to configure the operation. * - * Returns: - * Returns ``0`` if the put operation is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if put operation successful, ``negative value`` otherwise. */ -int8_t z_put(z_session_t zs, z_keyexpr_t keyexpr, const uint8_t *payload, z_zint_t payload_len, - const z_put_options_t *options); +int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, const uint8_t *payload, + z_zint_t payload_len, const z_put_options_t *options); /** - * Deletes data from a given keyexpr. + * Deletes data for a given keyexpr. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` through where data will be put. - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to put. - * options: The delete options to be applied in the delete operation. + * zs: Pointer to a :c:type:`z_loaned_session_t` to delete the data through. + * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to delete the data for. + * options: Pointer to a :c:type:`z_delete_options_t` to configure the operation. * - * Returns: - * Returns ``0`` if the delete operation is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if delete operation successful, ``negative value`` otherwise. */ -int8_t z_delete(z_session_t zs, z_keyexpr_t keyexpr, const z_delete_options_t *options); +int8_t z_delete(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, const z_delete_options_t *options); /** - * Constructs the default values for the publisher entity. + * Builds a :c:type:`z_publisher_options_t` with default values. * - * Returns: - * Returns the constructed :c:type:`z_publisher_options_t`. + * Parameters: + * Pointer to an uninitialized :c:type:`z_delete_options_t`. */ -z_publisher_options_t z_publisher_options_default(void); +void z_publisher_options_default(z_publisher_options_t *options); /** - * Declares a publisher for the given keyexpr. + * Declares a publisher for a given keyexpr. * * Data can be put and deleted with this publisher with the help of the * :c:func:`z_publisher_put` and :c:func:`z_publisher_delete` functions. * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_publisher_t` by loaning it using - * ``z_publisher_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, is - * equivalent to writing ``z_publisher_loan(&val)``. - * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. - * - * To check if ``val`` is still valid, you may use ``z_publisher_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. - * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where to declare the publisher. - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to associate with the publisher. - * options: The options to apply to the publisher. If ``NULL`` is passed, the default options will be applied. + * zs: Pointer to a :c:type:`z_loaned_session_t` to declare the publisher through. + * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to bind the publisher with. + * options: Pointer to a :c:type:`z_publisher_options_t` to configure the operation. * - * Returns: - * 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``. + * Return: + * ``0`` if declare successful, ``negative value`` otherwise. */ -z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, const z_publisher_options_t *options); +int8_t z_declare_publisher(z_owned_publisher_t *pub, const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, + const z_publisher_options_t *options); /** - * Undeclare the publisher generated by a call to :c:func:`z_declare_publisher`. + * Undeclares a publisher. * * Parameters: - * pub: A moved instance of :c:type:`z_owned_publisher_t` to undeclare. + * pub: Pointer to a moved :c:type:`z_owned_publisher_t` to undeclare. * - * Returns: - * Returns ``0`` if the undeclare publisher operation is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if undeclare successful, ``negative value`` otherwise. */ int8_t z_undeclare_publisher(z_owned_publisher_t *pub); -z_owned_keyexpr_t z_publisher_keyexpr(z_publisher_t publisher); +z_owned_keyexpr_t z_publisher_keyexpr(z_loaned_publisher_t *publisher); /** - * Constructs the default values for the put operation via a publisher entity. + * Builds a :c:type:`z_publisher_put_options_t` with default values. * - * Returns: - * Returns the constructed :c:type:`z_publisher_put_options_t`. + * Parameters: + * options: Pointer to an uninitialized :c:type:`z_publisher_put_options_t`. */ -z_publisher_put_options_t z_publisher_put_options_default(void); +void z_publisher_put_options_default(z_publisher_put_options_t *options); /** - * Constructs the default values for the delete operation via a publisher entity. + * Builds a :c:type:`z_publisher_delete_options_t` with default values. * - * Returns: - * Returns the constructed :c:type:`z_publisher_delete_options_t`. + * Parameters: + * options: Pointer to an uninitialized :c:type:`z_publisher_delete_options_t`. */ -z_publisher_delete_options_t z_publisher_delete_options_default(void); +void z_publisher_delete_options_default(z_publisher_delete_options_t *options); /** - * Puts data for the keyexpr associated to the given publisher. + * Puts data for the keyexpr bound to the given publisher. * * Parameters: - * pub: A loaned instance of :c:type:`z_publisher_t` from where to put the data. - * options: The options to apply to the put operation. If ``NULL`` is passed, the default options will be applied. + * pub: Pointer to a :c:type:`z_loaned_publisher_t` from where to put the data. + * options: Pointer to a :c:type:`z_publisher_put_options_t` to configure the operation. * - * Returns: - * Returns ``0`` if the put operation is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if put operation successful, ``negative value`` otherwise. */ -int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t len, +int8_t z_publisher_put(const z_loaned_publisher_t *pub, const uint8_t *payload, size_t len, const z_publisher_put_options_t *options); /** - * Deletes data from the keyexpr associated to the given publisher. + * Deletes data from the keyexpr bound to the given publisher. * * Parameters: - * pub: A loaned instance of :c:type:`z_publisher_t` from where to delete the data. - * options: The options to apply to the delete operation. If ``NULL`` is passed, the default options will be applied. + * pub: Pointer to a :c:type:`z_loaned_publisher_t` from where to delete the data. + * options: Pointer to a :c:type:`z_publisher_delete_options_t` to configure the delete operation. * - * Returns: - * Returns ``0`` if the delete operation is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if delete operation successful, ``negative value`` otherwise. */ -int8_t z_publisher_delete(const z_publisher_t pub, const z_publisher_delete_options_t *options); +int8_t z_publisher_delete(const z_loaned_publisher_t *pub, const z_publisher_delete_options_t *options); #endif #if Z_FEATURE_QUERY == 1 /** - * Constructs the default values for the get operation. + * Builds a :c:type:`z_get_options_t` with default values. * - * Returns: - * Returns the constructed :c:type:`z_get_options_t`. + * Parameters: + * options: Pointer to an uninitialized :c:type:`z_get_options_t`. */ -z_get_options_t z_get_options_default(void); +void z_get_options_default(z_get_options_t *options); /** - * Issues a distributed query for a given keyexpr. + * Sends a distributed query for a given keyexpr. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` through where data will be put. - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to put. + * zs: Pointer to a :c:type:`z_loaned_session_t` to send the query through. + * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to send the query for. * parameters: Pointer to the parameters as a null-terminated string. - * callback: A moved instance of :c:type:`z_owned_closure_reply_t` containing the callbacks to be called. - * options: The get options to be applied in the distributed query. + * callback: Pointer to a :c:type:`z_owned_closure_reply_t` callback. + * options: Pointer to a :c:type:`z_get_options_t` to configure the operation. * - * Returns: - * Returns ``0`` if the put operation is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if put operation successful, ``negative value`` otherwise. */ -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); +int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, const char *parameters, + z_owned_closure_reply_t *callback, z_get_options_t *options); /** - * Checks if the queryable answered with an OK, which allows this value to be treated as a sample. - * - * If this returns ``false``, you should use ``z_check`` before trying to use :c:func:`z_reply_err` if you want to - * process the error that may be here. + * Checks if queryable answered with an OK, which allows this value to be treated as a sample. * * Parameters: - * reply: Pointer to the received query reply. + * reply: Pointer to a :c:type:`z_loaned_reply_t` to check. * - * Returns: - * Returns ``true`` if the queryable answered with an OK, which allows this value to be treated as a sample, or - * ``false`` otherwise. + * Return: + * ``true`` if queryable answered with an OK, ``false`` otherwise. */ -_Bool z_reply_is_ok(const z_owned_reply_t *reply); +_Bool z_reply_is_ok(const z_loaned_reply_t *reply); /** - * Yields the contents of the reply by asserting it indicates a success. + * Gets the content of an OK reply. * * You should always make sure that :c:func:`z_reply_is_ok` returns ``true`` before calling this function. * * Parameters: - * reply: Pointer to the received query reply. + * reply: Pointer to a :c:type:`z_loaned_reply_t` to get content from. * - * Returns: - * Returns the :c:type:`z_loaned_sample_t` wrapped in the query reply. + * Return: + * The OK reply content wrapped as a :c:type:`z_loaned_sample_t`. */ -z_loaned_sample_t z_reply_ok(const z_owned_reply_t *reply); +const z_loaned_sample_t *z_reply_ok(const z_loaned_reply_t *reply); /** - * Yields the contents of the reply by asserting it indicates a failure. + * Gets the contents of an error reply. * * You should always make sure that :c:func:`z_reply_is_ok` returns ``false`` before calling this function. * * Parameters: - * reply: Pointer to the received query reply. + * reply: Pointer to a :c:type:`z_loaned_reply_t` to get content from. * - * Returns: - * Returns the :c:type:`z_value_t` wrapped in the query reply. + * Return: + * The error reply content wrapped as a :c:type:`z_loaned_value_t`. */ -z_value_t z_reply_err(const z_owned_reply_t *reply); +const z_loaned_value_t *z_reply_err(const z_loaned_reply_t *reply); #endif #if Z_FEATURE_QUERYABLE == 1 /** - * Constructs the default values for the queryable entity. + * Builds a :c:type:`z_queryable_options_t` with default values. * - * Returns: - * Returns the constructed :c:type:`z_queryable_options_t`. + * Parameters: + * options: Pointer to an uninitialized :c:type:`z_queryable_options_t`. */ -z_queryable_options_t z_queryable_options_default(void); +void z_queryable_options_default(z_queryable_options_t *options); /** - * Declares a queryable for the given keyexpr. - * - * Received queries are processed by means of callbacks. - * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_queryable_t` by loaning it using - * ``z_queryable_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, is - * equivalent to writing ``z_queryable_loan(&val)``. - * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. - * - * To check if ``val`` is still valid, you may use ``z_queryable_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. + * Declares a queryable for a given keyexpr. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where to declare the subscriber. - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to associate with the subscriber. - * callback: A moved instance of :c:type:`z_owned_closure_query_t` containing the callbacks to be called and the - * context to pass to them. options: The options to apply to the queryable. If ``NULL`` is passed, the default options - * will be applied. + * queryable: Pointer to an uninitialized :c:type:`z_owned_queryable_t` to contain the queryable. + * zs: Pointer to a :c:type:`z_loaned_session_t` to declare the subscriber through. + * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to bind the subscriber with. + * callback: Pointer to a :c:type:`z_owned_closure_query_t` callback. + * options: Pointer to a :c:type:`z_queryable_options_t` to configure the declare. * - * Returns: - * A :c:type:`z_owned_queryable_t` with either a valid queryable or a failing queryable. - * Should the queryable be invalid, ``z_check(val)`` ing the returned value will return ``false``. + * Return: + * ``0`` if declare operation successful, ``negative value`` otherwise. */ -z_owned_queryable_t z_declare_queryable(z_session_t zs, z_keyexpr_t keyexpr, z_owned_closure_query_t *callback, - const z_queryable_options_t *options); +int8_t z_declare_queryable(z_owned_queryable_t *queryable, const z_loaned_session_t *zs, + const z_loaned_keyexpr_t *keyexpr, z_owned_closure_query_t *callback, + const z_queryable_options_t *options); /** - * Undeclares the queryable generated by a call to :c:func:`z_declare_queryable`. + * Undeclares a queryable. * * Parameters: - * queryable: A moved instance of :c:type:`z_owned_queryable_t` to undeclare. + * queryable: Pointer to a :c:type:`z_owned_queryable_t` to undeclare. * - * Returns: - * Returns ``0`` if the undeclare queryable operation is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if undeclare operation successful, ``negative value`` otherwise. */ int8_t z_undeclare_queryable(z_owned_queryable_t *queryable); /** - * Constructs the default values for the query reply operation. + * Builds a :c:type:`z_query_reply_options_t` with default values. * - * Returns: - * Returns the constructed :c:type:`z_query_reply_options_t`. + * Parameters: + * options: Pointer to an uninitialized :c:type:`z_query_reply_options_t`. */ -z_query_reply_options_t z_query_reply_options_default(void); +void z_query_reply_options_default(z_query_reply_options_t *options); /** * Sends a reply to a query. @@ -1223,278 +1165,242 @@ z_query_reply_options_t z_query_reply_options_default(void); * returns. * * Parameters: - * query: Pointer to the received query. - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to associate with the subscriber. - * payload: Pointer to the data to put. - * payload_len: The length of the ``payload``. - * options: The options to apply to the send query reply operation. If ``NULL`` is passed, the default options will be - * applied. + * query: Pointer to a :c:type:`z_loaned_query_t` to reply. + * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to bind the reply with. + * payload: Pointer to the reply data. + * payload_len: The length of the payload. + * options: Pointer to a :c:type:`z_query_reply_options_t` to configure the reply. * - * Returns: - * Returns ``0`` if the send query reply operation is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if reply operation successful, ``negative value`` otherwise. */ -int8_t z_query_reply(const z_query_t *query, const z_keyexpr_t keyexpr, const uint8_t *payload, size_t payload_len, +int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, z_owned_bytes_t *payload, const z_query_reply_options_t *options); #endif /** - * Creates keyexpr owning string passed to it + * Builds a new keyexpr. + * + * Parameters: + * keyexpr: Pointer to an uninitialized :c:type:`z_owned_keyexpr_t` to store the keyexpr. + * name: Pointer to the null-terminated string of the keyexpr. + * + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. */ -z_owned_keyexpr_t z_keyexpr_new(const char *name); +int8_t z_keyexpr_new(z_owned_keyexpr_t *keyexpr, const char *name); /** - * Declares a keyexpr, so that it is internally mapped into into a numerical id. + * Declares a keyexpr, so that it is mapped on a numerical id. * * This numerical id is used on the network to save bandwidth and ease the retrieval of the concerned resource * in the routing tables. * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_keyexpr_t` by loaning it using - * ``z_keyexpr_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, is - * equivalent to writing ``z_keyexpr_loan(&val)``. - * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. - * - * To check if ``val`` is still valid, you may use ``z_keyexpr_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. - * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where to declare the keyexpr. - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to declare. + * ke: Pointer to an uninitialized :c:type:`z_owned_keyexpr_t` to contain the declared keyexpr. + * zs: Pointer to a :c:type:`z_loaned_session_t` to declare the keyexpr through. + * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to bind the keyexpr with. * - * Returns: - * A :c:type:`z_owned_keyexpr_t` with either a valid or invalid keyexpr. - * Should the keyexpr be invalid, ``z_check(val)`` ing the returned value will return ``false``. + * Return: + * ``0`` if declare successful, ``negative value`` otherwise. */ -z_owned_keyexpr_t z_declare_keyexpr(z_session_t zs, z_keyexpr_t keyexpr); +int8_t z_declare_keyexpr(z_owned_keyexpr_t *ke, const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr); /** - * Undeclares the keyexpr generated by a call to :c:func:`z_declare_keyexpr`. + * Undeclares a keyexpr. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` through where data will be put. - * keyexpr: A moved instance of :c:type:`z_owned_keyexpr_t` to undeclare. + * zs: Pointer to a :c:type:`z_loaned_session_t` to undeclare the data through. + * keyexpr: Pointer to a moved :c:type:`z_owned_keyexpr_t` to undeclare. * - * Returns: - * Returns ``0`` if the undeclare keyexpr operation is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if undeclare successful, ``negative value`` otherwise. */ -int8_t z_undeclare_keyexpr(z_session_t zs, z_owned_keyexpr_t *keyexpr); +// TODO(sashacmc): change parameters order? +int8_t z_undeclare_keyexpr(const z_loaned_session_t *zs, z_owned_keyexpr_t *keyexpr); #if Z_FEATURE_SUBSCRIPTION == 1 /** - * Constructs the default values for the subscriber entity. + * Builds a :c:type:`z_subscriber_options_t` with default values. * - * Returns: - * Returns the constructed :c:type:`z_subscriber_options_t`. + * Parameters: + * options: Pointer to an uninitialized :c:type:`z_subscriber_options_t`. */ -z_subscriber_options_t z_subscriber_options_default(void); +void z_subscriber_options_default(z_subscriber_options_t *options); /** - * Declares a (push) subscriber for the given keyexpr. - * - * Received data is processed by means of callbacks. - * - * Like most ``z_owned_X_t`` types, you may obtain an instance of :c:type:`z_owned_subscriber_t` by loaning it using - * ``z_subscriber_loan(&val)``. The ``z_loan(val)`` macro, available if your compiler supports C11's ``_Generic``, is - * equivalent to writing ``z_subscriber_loan(&val)``. - * - * Like all ``z_owned_X_t``, an instance will be destroyed by any function which takes a mutable pointer to said - * instance, as this implies the instance's inners were moved. To make this fact more obvious when reading your code, - * consider using ``z_move(val)`` instead of ``&val`` as the argument. After a ``z_move``, ``val`` will still exist, but - * will no longer be valid. The destructors are double-drop-safe, but other functions will still trust that your ``val`` - * is valid. - * - * To check if ``val`` is still valid, you may use ``z_subscriber_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. + * Declares a subscriber for a given keyexpr. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where to declare the subscriber. - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to associate with the subscriber. - * callback: A moved instance of :c:type:`z_owned_closure_sample_t` containg the callbacks to be called and the - * context to pass to them. options: The options to apply to the subscriber. If ``NULL`` is passed, the default options - * will be applied. + * sub: Pointer to a :c:type:`z_owned_subscriber_t` to contain the subscriber. + * zs: Pointer to a :c:type:`z_loaned_session_t` to declare the subscriber through. + * keyexpr: Pointer to a :c:type:`z_loaned_keyexpr_t` to bind the subscriber with. + * callback: Pointer to a`z_owned_closure_sample_t` callback. + * options: Pointer to a :c:type:`z_subscriber_options_t` to configure the operation * - * Returns: - * A :c:type:`z_owned_subscriber_t` with either a valid subscriber or a failing subscriber. - * Should the subscriber be invalid, ``z_check(val)`` ing the returned value will return ``false``. + * Return: + * ``0`` if declare successful, ``negative value`` otherwise. */ -z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z_owned_closure_sample_t *callback, - const z_subscriber_options_t *options); +int8_t z_declare_subscriber(z_owned_subscriber_t *sub, const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, + z_owned_closure_sample_t *callback, const z_subscriber_options_t *options); /** - * Undeclares the (push) subscriber generated by a call to :c:func:`z_declare_subscriber`. + * Undeclares the subscriber. * * Parameters: - * sub: A moved instance of :c:type:`z_owned_subscriber_t` to undeclare. + * sub: Pointer to a :c:type:`z_owned_subscriber_t` to undeclare. * - * Returns: - * Returns ``0`` if the undeclare (push) subscriber operation is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if undeclare successful, ``negative value`` otherwise. */ int8_t z_undeclare_subscriber(z_owned_subscriber_t *sub); /** - * @brief - * - */ -z_owned_keyexpr_t z_subscriber_keyexpr(z_subscriber_t sub); -#endif - -/** - * Checks if a given value is valid. + * Copies the keyexpr of a subscriber * * Parameters: - * value: A loaned instance of :c:type:`z_value_t` to be checked. + * keyexpr: Pointer to an uninitialized :c:type:`z_owned_keyexpr_t` to contain the keyexpr. + * sub: Pointer to a :c:type:`z_loaned_subscriber_t` to copy the keyexpr from. * - * Returns: - * Returns ``true`` if the value is valid, or ``false`` otherwise. + * Return: + * ``0`` if copy successful, ``negative value`` otherwise. */ -_Bool z_value_is_initialized(z_value_t *value); +int8_t z_subscriber_keyexpr(z_owned_keyexpr_t *keyexpr, z_loaned_subscriber_t *sub); +#endif /************* Multi Thread Tasks helpers **************/ /** - * Constructs the default values for the session read task. + * Builds a :c:type:`zp_task_read_options_t` with default value. * - * Returns: - * Returns the constructed :c:type:`zp_task_read_options_t`. + * Parameters: + * options: Pointer to an uninitialized :c:type:`zp_task_read_options_t`. */ -zp_task_read_options_t zp_task_read_options_default(void); +void zp_task_read_options_default(zp_task_read_options_t *options); /** - * Start a separate task to read from the network and process the messages as soon as they are received. + * Starts a task to read from the network and process the received messages. * * Note that the task can be implemented in form of thread, process, etc. and its implementation is platform-dependent. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where to start the read task. - * options: The options to apply when starting the read task. If ``NULL`` is passed, the default options will be - * applied. + * zs: Pointer to a :c:type:`z_loaned_session_t` to start the task from. + * options: Pointer to a :c:type:`zp_task_read_options_t` to configure the task. * - * Returns: - * Returns ``0`` if the read task started successfully, or a ``negative value`` otherwise. + * Return: + * ``0`` if task started successfully, ``negative value`` otherwise. */ -int8_t zp_start_read_task(z_session_t zs, const zp_task_read_options_t *options); +int8_t zp_start_read_task(z_loaned_session_t *zs, const zp_task_read_options_t *options); /** - * Stop the read task. + * Stops the read task. * * This may result in stopping a thread or a process depending on the target platform. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where to stop the read task. + * zs: Pointer to a :c:type:`z_loaned_session_t` to stop the task from. * - * Returns: - * Returns ``0`` if the read task stopped successfully, or a ``negative value`` otherwise. + * Return: + * ``0`` if task stopped successfully, ``negative value`` otherwise. */ -int8_t zp_stop_read_task(z_session_t zs); +int8_t zp_stop_read_task(z_loaned_session_t *zs); /** - * Constructs the default values for the session lease task. + * Builds a :c:type:`zp_task_lease_options_t` with default value. * - * Returns: - * Returns the constructed :c:type:`zp_task_lease_options_t`. + * Parameters: + * options: Pointer to an uninitialized :c:type:`zp_task_lease_options_t`. */ -zp_task_lease_options_t zp_task_lease_options_default(void); +void zp_task_lease_options_default(zp_task_lease_options_t *options); /** - * Start a separate task to handle the session lease. + * Starts a task to handle the session lease. * * This task will send ``KeepAlive`` messages when needed and will close the session when the lease is expired. * When operating over a multicast transport, it also periodically sends the ``Join`` messages. * Note that the task can be implemented in form of thread, process, etc. and its implementation is platform-dependent. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where to start the lease task. - * options: The options to apply when starting the lease task. If ``NULL`` is passed, the default options will be - * applied. + * zs: Pointer to a :c:type:`z_loaned_session_t` to start the task from. + * options: Pointer to a :c:type:`zp_task_lease_options_t` to configure the task. * - * Returns: - * Returns ``0`` if the lease task started successfully, or a ``negative value`` otherwise. + * Return: + * ``0`` if task started successfully, ``negative value`` otherwise. */ -int8_t zp_start_lease_task(z_session_t zs, const zp_task_lease_options_t *options); +int8_t zp_start_lease_task(z_loaned_session_t *zs, const zp_task_lease_options_t *options); /** - * Stop the lease task. + * Stops the lease task. * * This may result in stopping a thread or a process depending on the target platform. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where to stop the lease task. + * zs: Pointer to a :c:type:`z_loaned_session_t` to stop the task from. * - * Returns: - * Returns ``0`` if the lease task stopped successfully, or a ``negative value`` otherwise. + * Return: + * ``0`` if task stopped successfully, ``negative value`` otherwise. */ -int8_t zp_stop_lease_task(z_session_t zs); +int8_t zp_stop_lease_task(z_loaned_session_t *zs); /************* Single Thread helpers **************/ /** - * Constructs the default values for the reading procedure. + * Builds a :c:type:`zp_read_options_t` with default value. * - * Returns: - * Returns the constructed :c:type:`zp_read_options_t`. + * Parameters: + * options: Pointer to an uninitialized :c:type:`zp_read_options_t`. */ -zp_read_options_t zp_read_options_default(void); +void zp_read_options_default(zp_read_options_t *options); /** - * Triggers a single execution of reading procedure from the network and processes of any received the message. + * Executes a single read from the network and process received messages. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where trigger the reading procedure. - * options: The options to apply to the read. If ``NULL`` is passed, the default options will be - * applied. + * zs: Pointer to a :c:type:`z_loaned_session_t` to execute the read for. + * options: Pointer to a :c:type:`zp_read_options_t` to configure the operation. * - * Returns: - * Returns ``0`` if the reading procedure was executed successfully, or a ``negative value`` otherwise. + * Return: + * ``0`` if execution was successful, ``negative value`` otherwise. */ -int8_t zp_read(z_session_t zs, const zp_read_options_t *options); +int8_t zp_read(const z_loaned_session_t *zs, const zp_read_options_t *options); /** - * Constructs the default values for sending the keep alive. + * Builds a :c:type:`zp_send_keep_alive_options_t` with default value. * - * Returns: - * Returns the constructed :c:type:`zp_send_keep_alive_options_t`. + * Parameters: + * options: Pointer to an uninitialized :c:type:`zp_send_keep_alive_options_t`. */ -zp_send_keep_alive_options_t zp_send_keep_alive_options_default(void); +void zp_send_keep_alive_options_default(zp_send_keep_alive_options_t *options); /** - * Triggers a single execution of keep alive procedure. - * - * It will send ``KeepAlive`` messages when needed and will close the session when the lease is expired. + * Executes a single send keep alive procedure. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where trigger the leasing procedure. - * options: The options to apply to the send of a ``KeepAlive`` messages. If ``NULL`` is passed, the default options - * will be applied. + * zs: Pointer to a :c:type:`z_loaned_session_t` to execute the send for. + * options: Pointer to a :c:type:`zp_send_keep_alive_options_t` to configure the operation. * - * Returns: - * Returns ``0`` if the leasing procedure was executed successfully, or a ``negative value`` otherwise. + * Return: + * ``0`` if execution was successful, ``negative value`` otherwise. */ -int8_t zp_send_keep_alive(z_session_t zs, const zp_send_keep_alive_options_t *options); +int8_t zp_send_keep_alive(const z_loaned_session_t *zs, const zp_send_keep_alive_options_t *options); /** - * Constructs the default values for sending the join. + * Builds a :c:type:`zp_send_join_options_t` with default value. * - * Returns: - * Returns the constructed :c:type:`zp_send_join_options_t`. + * Parameters: + * options: Pointer to an uninitialized :c:type:`zp_send_join_options_t`. */ -zp_send_join_options_t zp_send_join_options_default(void); +void zp_send_join_options_default(zp_send_join_options_t *options); /** - * Triggers a single execution of join procedure. - * - * It will send ``Join`` messages. + * Executes a single send join procedure. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where trigger the leasing procedure. - * options: The options to apply to the send of a ``Join`` messages. If ``NULL`` is passed, the default options will - * be applied. + * zs: Pointer to a :c:type:`z_loaned_session_t` to execute the send for. + * options: Pointer to a :c:type:`zp_send_keep_alive_options_t` to configure the operation. * - * Returns: - * Returns ``0`` if the leasing procedure was executed successfully, or a ``negative value`` otherwise. + * Return: + * ``0`` if execution was successful, ``negative value`` otherwise. */ -int8_t zp_send_join(z_session_t zs, const zp_send_join_options_t *options); +int8_t zp_send_join(const z_loaned_session_t *zs, const zp_send_join_options_t *options); #ifdef __cplusplus } diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index bad8daefa..70c19885e 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -18,6 +18,8 @@ #include "zenoh-pico/collections/bytes.h" #include "zenoh-pico/collections/element.h" #include "zenoh-pico/collections/list.h" +#include "zenoh-pico/collections/string.h" +#include "zenoh-pico/net/encoding.h" #include "zenoh-pico/net/publish.h" #include "zenoh-pico/net/query.h" #include "zenoh-pico/net/reply.h" @@ -30,17 +32,27 @@ extern "C" { #endif -/* Owned types */ +/* Owned/Loaned types */ + +// For pointer types #define _OWNED_TYPE_PTR(type, name) \ typedef struct { \ - type *_value; \ + type *_val; \ } z_owned_##name##_t; -#define _OWNED_TYPE_STR(type, name) \ - typedef struct { \ - type _value; \ +// For refcounted types +#define _OWNED_TYPE_RC(type, name) \ + typedef struct { \ + type _rc; \ } z_owned_##name##_t; +#define _LOANED_TYPE(type, name) typedef type z_loaned_##name##_t; + +#define _VIEW_TYPE(type, name) \ + typedef struct { \ + type _val; \ + } z_view_##name##_t; + /** * Represents a variable-length encoding unsigned integer. * @@ -48,16 +60,6 @@ extern "C" { */ typedef _z_zint_t z_zint_t; -/** - * Represents an array of bytes. - * - * Members: - * size_t len: The length of the bytes array. - * 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. * @@ -68,6 +70,21 @@ _Bool z_bytes_check(const z_bytes_t *v); */ typedef _z_id_t z_id_t; +/* + * Represents timestamp value in Zenoh + */ +typedef _z_timestamp_t z_timestamp_t; + +/** + * Represents an array of bytes. + * + * Members: + * size_t len: The length of the bytes array. + * uint8_t *start: A pointer to the bytes array. + */ +_OWNED_TYPE_PTR(_z_bytes_t, bytes) +_LOANED_TYPE(_z_bytes_t, bytes) + /** * Represents a string without null-terminator. * @@ -75,165 +92,140 @@ typedef _z_id_t z_id_t; * size_t len: The length of the string. * const char *val: A pointer to the string. */ -typedef _z_string_t z_string_t; - -typedef _z_str_t z_str_t; -_OWNED_TYPE_STR(z_str_t, str) +_OWNED_TYPE_PTR(_z_string_t, string) +_LOANED_TYPE(_z_string_t, string) +_VIEW_TYPE(_z_string_t, string) /** * Represents a key expression in Zenoh. * - * Operations over :c:type:`z_keyexpr_t` must be done using the provided functions: + * Members are private and operations must be done using the provided functions: * - * - :c:func:`z_keyexpr` + * - :c:func:`z_keyexpr_new` * - :c:func:`z_keyexpr_is_initialized` * - :c:func:`z_keyexpr_to_string` * - :c:func:`zp_keyexpr_resolve` */ -typedef _z_keyexpr_t z_keyexpr_t; -_OWNED_TYPE_PTR(z_keyexpr_t, keyexpr) +_OWNED_TYPE_PTR(_z_keyexpr_t, keyexpr) +_LOANED_TYPE(_z_keyexpr_t, keyexpr) +_VIEW_TYPE(_z_keyexpr_t, keyexpr) /** - * Represents a Zenoh configuration. - * - * Configurations are usually used to set the parameters of a Zenoh session upon its opening. + * Represents a Zenoh configuration, used to configure Zenoh sessions upon opening. * - * Operations over :c:type:`z_config_t` must be done using the provided functions: + * Members are private and operations must be done using the provided functions: * * - :c:func:`z_config_new` * - :c:func:`z_config_default` * - :c:func:`zp_config_get` * - :c:func:`zp_config_insert` */ -typedef struct { - _z_config_t *_val; -} z_config_t; _OWNED_TYPE_PTR(_z_config_t, config) +_LOANED_TYPE(_z_config_t, config) /** - * Represents a scouting configuration. + * Represents a scouting configuration, used to configure a scouting procedure. * - * Configurations are usually used to set the parameters of the scouting procedure. - * - * Operations over :c:type:`z_scouting_config_t` must be done using the provided functions: + * Members are private and operations must be done using the provided functions: * * - :c:func:`z_scouting_config_default` * - :c:func:`z_scouting_config_from` * - :c:func:`zp_scouting_config_get` * - :c:func:`zp_scouting_config_insert` */ -typedef struct { - _z_scouting_config_t *_val; -} z_scouting_config_t; _OWNED_TYPE_PTR(_z_scouting_config_t, scouting_config) +_LOANED_TYPE(_z_scouting_config_t, scouting_config) /** - * Represents a Zenoh session. + * Represents a Zenoh Session. */ -typedef struct { - _z_session_rc_t _val; -} z_session_t; - -typedef struct { - _z_session_rc_t _value; -} z_owned_session_t; +_OWNED_TYPE_RC(_z_session_rc_t, session) +_LOANED_TYPE(_z_session_rc_t, session) /** - * Represents a Zenoh (push) Subscriber entity. + * Represents a Zenoh Subscriber entity. * - * Operations over :c:type:`z_subscriber_t` must be done using the provided functions: + * Members are private and operations must be done using the provided functions: * * - :c:func:`z_declare_subscriber` * - :c:func:`z_undeclare_subscriber` */ -typedef struct { - _z_subscriber_t *_val; -} z_subscriber_t; _OWNED_TYPE_PTR(_z_subscriber_t, subscriber) +_LOANED_TYPE(_z_subscriber_t, subscriber) /** * Represents a Zenoh Publisher entity. * - * Operations over :c:type:`z_publisher_t` must be done using the provided functions: + * Members are private and operations must be done using the provided functions: * * - :c:func:`z_declare_publisher` * - :c:func:`z_undeclare_publisher` * - :c:func:`z_publisher_put` * - :c:func:`z_publisher_delete` */ -typedef struct { - _z_publisher_t *_val; -} z_publisher_t; _OWNED_TYPE_PTR(_z_publisher_t, publisher) +_LOANED_TYPE(_z_publisher_t, publisher) /** * Represents a Zenoh Queryable entity. * - * Operations over :c:type:`z_queryable_t` must be done using the provided functions: + * Members are private and operations must be done using the provided functions: * * - :c:func:`z_declare_queryable` * - :c:func:`z_undeclare_queryable` */ -typedef struct { - _z_queryable_t *_val; -} z_queryable_t; _OWNED_TYPE_PTR(_z_queryable_t, queryable) +_LOANED_TYPE(_z_queryable_t, queryable) /** - * Represents a Zenoh query entity, received by Zenoh Queryable entities. + * Represents a Zenoh Query entity, received by Zenoh Queryable entities. * */ -typedef struct z_query_t { - z_owned_query_t _val; -} z_query_t; +_OWNED_TYPE_RC(_z_query_rc_t, query) +_LOANED_TYPE(_z_query_rc_t, query) /** * Represents the encoding of a payload, in a MIME-like format. * * Members: - * z_encoding_prefix_t prefix: The integer prefix of this encoding. - * z_bytes_t suffix: The suffix of this encoding. It MUST be a valid UTF-8 string. - */ -typedef _z_encoding_t z_encoding_t; - -/* - * Represents timestamp value in Zenoh + * z_encoding_id_t prefix: The integer prefix of this encoding. + * z_loaned_bytes_t* suffix: The suffix of this encoding. It MUST be a valid UTF-8 string. */ -typedef _z_timestamp_t z_timestamp_t; +_OWNED_TYPE_PTR(_z_encoding_t, encoding) +_LOANED_TYPE(_z_encoding_t, encoding) /** * Represents a Zenoh value. * * Members: - * z_encoding_t encoding: The encoding of the `payload`. - * z_bytes_t payload: The payload of this zenoh value. + * z_loaned_encoding_t encoding: The encoding of the `payload`. + * z_loaned_bytes_t* payload: The payload of this zenoh value. */ -typedef _z_value_t z_value_t; +_OWNED_TYPE_PTR(_z_value_t, value) +_LOANED_TYPE(_z_value_t, value) /** - * Represents the set of options that can be applied to a (push) subscriber, - * upon its declaration via :c:func:`z_declare_subscriber`. + * Represents the configuration used to configure a subscriber upon declaration :c:func:`z_declare_subscriber`. * * Members: - * z_reliability_t reliability: The subscription reliability. + * z_reliability_t reliability: The subscription reliability value. */ typedef struct { z_reliability_t reliability; } z_subscriber_options_t; /** - * Represents the replies consolidation to apply on replies to a :c:func:`z_get`. + * Represents the reply consolidation mode to apply on replies to a :c:func:`z_get`. * * Members: - * z_consolidation_mode_t mode: Defines the consolidation mode to apply to the replies. + * z_consolidation_mode_t mode: the consolidation mode, see :c:type:`z_consolidation_mode_t` */ typedef struct { z_consolidation_mode_t mode; } z_query_consolidation_t; /** - * Represents the set of options that can be applied to a publisher, - * upon its declaration via :c:func:`z_declare_publisher`. + * Represents the configuration used to configure a publisher upon declaration with :c:func:`z_declare_publisher`. * * Members: * z_congestion_control_t congestion_control: The congestion control to apply when routing messages from this @@ -246,8 +238,7 @@ typedef struct { } z_publisher_options_t; /** - * Represents the set of options that can be applied to a queryable, - * upon its declaration via :c:func:`z_declare_queryable`. + * Represents the configuration used to configure a queryable upon declaration :c:func:`z_declare_queryable`. * * Members: * _Bool complete: The completeness of the queryable. @@ -257,29 +248,27 @@ typedef struct { } z_queryable_options_t; /** - * Represents the set of options that can be applied to a query reply, - * sent via :c:func:`z_query_reply`. + * Represents the configuration used to configure a query reply sent via :c:func:`z_query_reply. * * Members: - * z_encoding_t encoding: The encoding of the payload. + * z_owned_encoding_t *encoding: The encoding of the payload. * z_attachment_t attachment: an attachment to the response. */ typedef struct { - z_encoding_t encoding; + z_owned_encoding_t *encoding; z_attachment_t attachment; } z_query_reply_options_t; /** - * Represents the set of options that can be applied to the put operation, - * whenever issued via :c:func:`z_put`. + * Represents the configuration used to configure a put operation sent via via :c:func:`z_put`. * * Members: - * z_encoding_t encoding: The encoding of the payload. + * z_owned_encoding_t *encoding: The encoding of the payload. * z_congestion_control_t congestion_control: The congestion control to apply when routing this message. * z_priority_t priority: The priority of this message when routed. */ typedef struct { - z_encoding_t encoding; + z_owned_encoding_t *encoding; z_congestion_control_t congestion_control; z_priority_t priority; #if Z_FEATURE_ATTACHMENT == 1 @@ -288,8 +277,7 @@ typedef struct { } z_put_options_t; /** - * Represents the set of options that can be applied to the delete operation, - * whenever issued via :c:func:`z_delete`. + * Represents the configuration used to configure a delete operation sent via :c:func:`z_delete`. * * Members: * z_congestion_control_t congestion_control: The congestion control to apply when routing this message. @@ -301,38 +289,39 @@ typedef struct { } z_delete_options_t; /** - * Represents the set of options that can be applied to the put operation by a previously declared publisher, - * whenever issued via :c:func:`z_publisher_put`. + * Represents the configuration used to configure a put operation by a previously declared publisher, + * sent via :c:func:`z_publisher_put`. * * Members: - * z_encoding_t encoding: The encoding of the payload. + * z_owned_encoding_t *encoding: The encoding of the payload. */ typedef struct { - z_encoding_t encoding; + z_owned_encoding_t *encoding; #if Z_FEATURE_ATTACHMENT == 1 z_attachment_t attachment; #endif } z_publisher_put_options_t; /** - * Represents the set of options that can be applied to the delete operation by a previously declared publisher, - * whenever issued via :c:func:`z_publisher_delete`. + * Represents the configuration used to configure a delete operation by a previously declared publisher, + * sent via :c:func:`z_publisher_delete`. */ typedef struct { uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior } z_publisher_delete_options_t; /** - * Represents the set of options that can be applied to the get operation, - * whenever issued via :c:func:`z_get`. + * Represents the configuration used to configure a get operation sent via :c:func:`z_get`. * * 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 value: The payload to include in the query. + * z_owned_bytes_t payload: The payload to include in the query. + * z_owned_encoding_t *encoding: Payload encoding. */ typedef struct { - z_value_t value; + z_owned_bytes_t *payload; + z_owned_encoding_t *encoding; z_query_consolidation_t consolidation; z_query_target_t target; uint32_t timeout_ms; @@ -342,8 +331,7 @@ typedef struct { } z_get_options_t; /** - * Represents the set of options that can be applied to the read task, - * whenever issued via :c:func:`zp_start_read_task`. + * Represents the configuration used to configure a read task started via :c:func:`zp_start_read_task`. */ typedef struct { #if Z_FEATURE_MULTI_THREAD == 1 @@ -354,8 +342,7 @@ typedef struct { } zp_task_read_options_t; /** - * Represents the set of options that can be applied to the lease task, - * whenever issued via :c:func:`zp_start_lease_task`. + * Represents the configuration used to configure a lease task started via :c:func:`zp_start_lease_task`. */ typedef struct { #if Z_FEATURE_MULTI_THREAD == 1 @@ -366,33 +353,31 @@ typedef struct { } zp_task_lease_options_t; /** - * Represents the set of options that can be applied to the read operation, - * whenever issued via :c:func:`zp_read`. + * Represents the configuration used to configure a read operation started via :c:func:`zp_read`. */ typedef struct { uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior } zp_read_options_t; /** - * Represents the set of options that can be applied to the keep alive send, - * whenever issued via :c:func:`zp_send_keep_alive`. + * Represents the configuration used to configure a send keep alive operation started via :c:func:`zp_send_keep_alive`. */ typedef struct { uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior } zp_send_keep_alive_options_t; /** - * Represents the set of options that can be applied to the join send, - * whenever issued via :c:func:`zp_send_join`. + * Represents the configuration used to configure a send join operation started via :c:func:`zp_send_join`. */ typedef struct { uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior } zp_send_join_options_t; /** - * QoS settings of zenoh message. + * QoS settings of a zenoh message. */ typedef _z_qos_t z_qos_t; + /** * Returns message priority. */ @@ -400,16 +385,19 @@ static inline z_priority_t z_qos_get_priority(z_qos_t qos) { z_priority_t ret = _z_n_qos_get_priority(qos); return ret == _Z_PRIORITY_CONTROL ? Z_PRIORITY_DEFAULT : ret; } + /** * Returns message congestion control. */ static inline z_congestion_control_t z_qos_get_congestion_control(z_qos_t qos) { return _z_n_qos_get_congestion_control(qos); } + /** * Returns message express flag. If set to true, the message is not batched to reduce the latency. */ static inline _Bool z_qos_get_express(z_qos_t qos) { return _z_n_qos_get_express(qos); } + /** * Returns default qos settings. */ @@ -418,69 +406,56 @@ static inline z_qos_t z_qos_default(void) { return _Z_N_QOS_DEFAULT; } /** * Represents a data sample. * - * A sample is the value associated to a given :c:type:`z_keyexpr_t` at a given point in time. + * A sample is the value associated to a given key-expression at a given point in time. * * Members: * z_keyexpr_t keyexpr: The keyexpr of this data sample. - * z_bytes_t payload: The value of this data sample. - * z_encoding_t encoding: The encoding of the value of this data sample. + * z_loaned_bytes_t* payload: The value of this data sample. + * z_loaned_encoding_t encoding: The encoding of the value of this data sample. * z_sample_kind_t kind: The kind of this data sample (PUT or DELETE). * z_timestamp_t timestamp: The timestamp of this data sample. * z_qos_t qos: Quality of service settings used to deliver this sample. */ -typedef struct z_sample_t { - _z_sample_rc_t _rc; -} z_sample_t; -_OWNED_TYPE_PTR(z_sample_t, sample) -typedef _z_sample_t z_loaned_sample_t; +_OWNED_TYPE_RC(_z_sample_rc_t, sample) +_LOANED_TYPE(_z_sample_rc_t, sample) /** * Represents the content of a `hello` message returned by a zenoh entity as a reply to a `scout` message. * * Members: * 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. + * z_loaned_bytes_t* zid: The Zenoh ID of the scouted entity (empty if absent). + * z_loaned_string_array_t locators: The locators of the scouted entity. */ -typedef _z_hello_t z_hello_t; -_OWNED_TYPE_PTR(z_hello_t, hello) - -/** - * Represents the content of a reply to a query. - * - * Members: - * z_sample_t sample: The :c:type:`_z_sample_t` containing the key and value of the reply. - * z_bytes_t replier_id: The id of the replier that sent this reply. - */ -typedef _z_reply_data_t z_reply_data_t; +_OWNED_TYPE_PTR(_z_hello_t, hello) +_LOANED_TYPE(_z_hello_t, hello) /** * Represents the reply to a query. - * - * Members: - * z_reply_data_t data: the content of the reply. */ -typedef _z_reply_t z_reply_t; -_OWNED_TYPE_PTR(z_reply_t, reply) +_OWNED_TYPE_RC(_z_reply_rc_t, reply) +_LOANED_TYPE(_z_reply_rc_t, reply) /** - * Represents an array of ``z_str_t``. + * Represents an array of non null-terminated string. * - * Operations over :c:type:`z_str_array_t` must be done using the provided functions: + * Operations over :c:type:`z_loaned_string_array_t` must be done using the provided functions: * - * - ``char *z_str_array_get(z_str_array_t *a, size_t k);`` - * - ``size_t z_str_array_len(z_str_array_t *a);`` - * - ``_Bool z_str_array_array_is_empty(z_str_array_t *a);`` + * - :c:func:`z_string_array_get` + * - :c:func:`z_string_array_len` + * - :c:func:`z_str_array_array_is_empty` */ -typedef _z_str_array_t z_str_array_t; +_OWNED_TYPE_PTR(_z_string_vec_t, string_array) +_LOANED_TYPE(_z_string_vec_t, string_array) +_VIEW_TYPE(_z_string_vec_t, string_array) -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) +const z_loaned_string_t *z_string_array_get(const z_loaned_string_array_t *a, size_t k); +size_t z_string_array_len(const z_loaned_string_array_t *a); +_Bool z_string_array_is_empty(const z_loaned_string_array_t *a); -typedef void (*_z_dropper_handler_t)(void *arg); -typedef void (*_z_owned_sample_handler_t)(z_owned_sample_t *sample, void *arg); +typedef void (*z_dropper_handler_t)(void *arg); +typedef void (*z_owned_sample_handler_t)(z_owned_sample_t *sample, void *arg); +typedef _z_data_handler_t z_data_handler_t; /** * Represents the sample closure. @@ -488,17 +463,17 @@ typedef void (*_z_owned_sample_handler_t)(z_owned_sample_t *sample, void *arg); * A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks. * * Members: - * _z_data_handler_t call: `void *call(const struct z_sample_t*, const void *context)` is the callback function. - * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. + * z_data_handler_t call: `void *call(const struct z_sample_t*, const void *context)` is the callback function. + * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. */ typedef struct { void *context; - _z_data_handler_t call; - _z_dropper_handler_t drop; + z_data_handler_t call; + z_dropper_handler_t drop; } z_owned_closure_sample_t; -void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_sample_t *sample); +void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_loaned_sample_t *sample); /** * Represents the owned sample closure. @@ -506,39 +481,43 @@ void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_samp * A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks. * * Members: - * _z_owned_sample_handler_t call: `void *call(const struct z_owned_sample_t*, const void *context)` is the callback + * void *context: a pointer to an arbitrary state. + * z_owned_sample_handler_t call: `void *call(const struct z_owned_sample_t*, const void *context)` is the callback * function. - * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. void *context: a + * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. void *context: a * pointer to an arbitrary state. */ typedef struct { void *context; - _z_owned_sample_handler_t call; - _z_dropper_handler_t drop; + z_owned_sample_handler_t call; + z_dropper_handler_t drop; } z_owned_closure_owned_sample_t; void z_closure_owned_sample_call(const z_owned_closure_owned_sample_t *closure, z_owned_sample_t *sample); +typedef _z_queryable_handler_t z_queryable_handler_t; + /** * Represents the query callback closure. * * A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks. * * Members: + * void *context: a pointer to an arbitrary state. * _z_queryable_handler_t call: `void (*_z_queryable_handler_t)(z_query_t *query, void *arg)` is the * callback function. - * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. + * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ typedef struct { void *context; - _z_queryable_handler_t call; - _z_dropper_handler_t drop; + z_queryable_handler_t call; + z_dropper_handler_t drop; } z_owned_closure_query_t; -void z_closure_query_call(const z_owned_closure_query_t *closure, const z_query_t *query); +void z_closure_query_call(const z_owned_closure_query_t *closure, const z_loaned_query_t *query); -typedef void (*_z_owned_query_handler_t)(z_owned_query_t *query, void *arg); +typedef void (*z_owned_query_handler_t)(z_owned_query_t *query, void *arg); /** * Represents the owned query closure. @@ -546,20 +525,22 @@ typedef void (*_z_owned_query_handler_t)(z_owned_query_t *query, void *arg); * A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks. * * Members: - * _z_owned_query_handler_t call: `void *call(const struct z_owned_query_t*, const void *context)` is the callback + * void *context: a pointer to an arbitrary state. + * z_owned_query_handler_t call: `void *call(const struct z_owned_query_t*, const void *context)` is the callback * function. - * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. void *context: a + * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. void *context: a * pointer to an arbitrary state. */ typedef struct { void *context; - _z_owned_query_handler_t call; - _z_dropper_handler_t drop; + z_owned_query_handler_t call; + z_dropper_handler_t drop; } z_owned_closure_owned_query_t; void z_closure_owned_query_call(const z_owned_closure_owned_query_t *closure, z_owned_query_t *query); typedef void (*z_owned_reply_handler_t)(z_owned_reply_t *reply, void *arg); +typedef _z_reply_handler_t z_reply_handler_t; /** * Represents the query reply callback closure. @@ -567,18 +548,39 @@ typedef void (*z_owned_reply_handler_t)(z_owned_reply_t *reply, void *arg); * A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks. * * Members: + * void *context: a pointer to an arbitrary state. + * z_reply_handler_t call: `void (*_z_reply_handler_t)(_z_reply_t *reply, void *arg)` is the + * callback function. + * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. + * void *context: a pointer to an arbitrary state. + */ +typedef struct { + void *context; + z_reply_handler_t call; + z_dropper_handler_t drop; +} z_owned_closure_reply_t; + +void z_closure_reply_call(const z_owned_closure_reply_t *closure, const z_loaned_reply_t *reply); + +/** + * Represents the owned query reply callback closure. + * + * A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks. + * + * Members: + * void *context: a pointer to an arbitrary state. * z_owned_reply_handler_t call: `void (*z_owned_reply_handler_t)(const z_owned_reply_t *reply, void *arg)` is the * callback function. - * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. + * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ typedef struct { void *context; z_owned_reply_handler_t call; - _z_dropper_handler_t drop; -} z_owned_closure_reply_t; + z_dropper_handler_t drop; +} z_owned_closure_owned_reply_t; -void z_closure_reply_call(const z_owned_closure_reply_t *closure, z_owned_reply_t *reply); +void z_closure_owned_reply_call(const z_owned_closure_owned_reply_t *closure, z_owned_reply_t *reply); typedef void (*z_owned_hello_handler_t)(z_owned_hello_t *hello, void *arg); @@ -588,15 +590,16 @@ typedef void (*z_owned_hello_handler_t)(z_owned_hello_t *hello, void *arg); * A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks. * * Members: + * void *context: a pointer to an arbitrary state. * z_owned_hello_handler_t call: `void (*z_owned_hello_handler_t)(const z_owned_hello_t *hello, void *arg)` is the * callback function. - * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. + * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ typedef struct { void *context; z_owned_hello_handler_t call; - _z_dropper_handler_t drop; + z_dropper_handler_t drop; } z_owned_closure_hello_t; void z_closure_hello_call(const z_owned_closure_hello_t *closure, z_owned_hello_t *hello); @@ -609,21 +612,22 @@ typedef void (*z_id_handler_t)(const z_id_t *id, void *arg); * A closure is a structure that contains all the elements for stateful, memory-leak-free callbacks. * * Members: + * void *context: a pointer to an arbitrary state. * z_id_handler_t call: `void (*z_id_handler_t)(const z_id_t *id, void *arg)` is the callback function. - * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. + * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ typedef struct { void *context; z_id_handler_t call; - _z_dropper_handler_t drop; + z_dropper_handler_t drop; } z_owned_closure_zid_t; void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id); #if Z_FEATURE_ATTACHMENT == 1 struct _z_bytes_pair_t { - _z_bytes_t key; - _z_bytes_t value; + _z_bytes_t *key; + _z_bytes_t *value; }; void _z_bytes_pair_clear(struct _z_bytes_pair_t *this_); @@ -634,6 +638,7 @@ _Z_LIST_DEFINE(_z_bytes_pair, struct _z_bytes_pair_t) /** * A map of maybe-owned vector of bytes to maybe-owned vector of bytes. */ +// TODO(sashacmc): z_owned_bytes_map_t for attachment typedef struct z_owned_bytes_map_t { _z_bytes_pair_list_t *_inner; } z_owned_bytes_map_t; @@ -642,36 +647,42 @@ typedef struct z_owned_bytes_map_t { * Aliases `this` into a generic `z_attachment_t`, allowing it to be passed to corresponding APIs. */ z_attachment_t z_bytes_map_as_attachment(const z_owned_bytes_map_t *this_); + /** * Returns `true` if the map is not in its gravestone state */ bool z_bytes_map_check(const z_owned_bytes_map_t *this_); + /** - * Destroys the map, resetting `this` to its gravestone value. + * Deletes the map, resetting `this` to its gravestone value. * * This function is double-free safe, passing a pointer to the gravestone value will have no effect. */ void z_bytes_map_drop(z_owned_bytes_map_t *this_); + /** - * Constructs a map from the provided attachment, copying keys and values. + * Builds a map from the provided attachment, copying keys and values. * * If `this` is at gravestone value, the returned value will also be at gravestone value. */ z_owned_bytes_map_t z_bytes_map_from_attachment(z_attachment_t this_); + /** - * Constructs a map from the provided attachment, aliasing the attachment's keys and values. + * Builds a map from the provided attachment, aliasing the attachment's keys and values. * * If `this` is at gravestone value, the returned value will also be at gravestone value. */ z_owned_bytes_map_t z_bytes_map_from_attachment_aliasing(z_attachment_t this_); + /** * Returns the value associated with `key`, returning a gravestone value if: * - `this` or `key` is in gravestone state. * - `this` has no value associated to `key` */ -z_bytes_t z_bytes_map_get(const z_owned_bytes_map_t *this_, z_bytes_t key); +z_loaned_bytes_t *z_bytes_map_get(const z_owned_bytes_map_t *this_, z_loaned_bytes_t *key); + /** * Associates `value` to `key` in the map, aliasing them. * @@ -681,7 +692,8 @@ z_bytes_t z_bytes_map_get(const z_owned_bytes_map_t *this_, z_bytes_t key); * Calling this with `NULL` or the gravestone value is undefined behaviour. */ -void z_bytes_map_insert_by_alias(const z_owned_bytes_map_t *this_, z_bytes_t key, z_bytes_t value); +void z_bytes_map_insert_by_alias(const z_owned_bytes_map_t *this_, z_loaned_bytes_t *key, z_loaned_bytes_t *value); + /** * Associates `value` to `key` in the map, copying them to obtain ownership: `key` and `value` are not aliased past the * function's return. @@ -689,7 +701,8 @@ void z_bytes_map_insert_by_alias(const z_owned_bytes_map_t *this_, z_bytes_t key * Calling this with `NULL` or the gravestone value is undefined behaviour. */ -void z_bytes_map_insert_by_copy(const z_owned_bytes_map_t *this_, z_bytes_t key, z_bytes_t value); +void z_bytes_map_insert_by_copy(const z_owned_bytes_map_t *this_, z_loaned_bytes_t *key, z_loaned_bytes_t *value); + /** * Iterates over the key-value pairs in the map. * @@ -704,13 +717,17 @@ void z_bytes_map_insert_by_copy(const z_owned_bytes_map_t *this_, z_bytes_t key, */ int8_t z_bytes_map_iter(const z_owned_bytes_map_t *this_, z_attachment_iter_body_t body, void *ctx); + /** - * Constructs a new map. + * Builds a new map. */ +// TODO(sashacmc): z_bytes_map_new for attachment z_owned_bytes_map_t z_bytes_map_new(void); + /** - * Constructs the gravestone value for `z_owned_bytes_map_t` + * Initializes a `z_owned_bytes_map_t` */ +// TODO(sashacmc): z_bytes_map_null for attachment z_owned_bytes_map_t z_bytes_map_null(void); #endif @@ -719,11 +736,7 @@ z_owned_bytes_map_t z_bytes_map_null(void); * * `str == NULL` will cause this to return `z_bytes_null()` */ -z_bytes_t z_bytes_from_str(const char *str); -/** - * Returns the gravestone value for `z_bytes_t` - */ -z_bytes_t z_bytes_null(void); +int8_t z_bytes_from_str(z_owned_bytes_t *bytes, const char *str); #ifdef __cplusplus } diff --git a/include/zenoh-pico/collections/refcount.h b/include/zenoh-pico/collections/refcount.h index 73d5f4807..428b18378 100644 --- a/include/zenoh-pico/collections/refcount.h +++ b/include/zenoh-pico/collections/refcount.h @@ -113,13 +113,13 @@ } \ return p; \ } \ - static inline name##_rc_t name##_rc_clone(name##_rc_t *p) { \ + static inline name##_rc_t name##_rc_clone(const name##_rc_t *p) { \ name##_rc_t c; \ c.in = p->in; \ _ZP_RC_OP_INCR_CNT \ return c; \ } \ - static inline name##_rc_t *name##_rc_clone_as_ptr(name##_rc_t *p) { \ + static inline name##_rc_t *name##_rc_clone_as_ptr(const name##_rc_t *p) { \ name##_rc_t *c = (name##_rc_t *)z_malloc(sizeof(name##_rc_t)); \ if (c != NULL) { \ c->in = p->in; \ diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index cfbc33978..2c7f95bdd 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -34,9 +34,6 @@ void _z_str_copy(char *dst, const char *src); void _z_str_n_copy(char *dst, const char *src, size_t size); _Z_ELEM_DEFINE(_z_str, char, _z_str_size, _z_noop_clear, _z_str_copy) -// This is here for reference on why -// the _z_str_array_t was not defined using this macro -// but instead manually as find below _Z_VEC_DEFINE(_z_str, char) _Z_LIST_DEFINE(_z_str, char) _Z_INT_MAP_DEFINE(_z_str, char) @@ -72,7 +69,10 @@ typedef struct { char *val; } _z_string_t; +_z_string_t _z_string_null(void); _z_string_t _z_string_make(const char *value); +_z_string_t _z_string_wrap(char *value); +_z_string_t *_z_string_make_as_ptr(const char *value); size_t _z_string_size(const _z_string_t *s); void _z_string_copy(_z_string_t *dst, const _z_string_t *src); @@ -81,32 +81,13 @@ void _z_string_move_str(_z_string_t *dst, char *src); void _z_string_clear(_z_string_t *s); void _z_string_free(_z_string_t **s); void _z_string_reset(_z_string_t *s); +_z_string_t _z_string_convert_bytes(const _z_bytes_t *bs); _z_string_t _z_string_from_bytes(const _z_bytes_t *bs); _Z_ELEM_DEFINE(_z_string, _z_string_t, _z_string_size, _z_string_clear, _z_string_copy) -/*-------- str_array --------*/ -/** - * An array of NULL terminated strings. - * - * Members: - * size_t len: The length of the array. - * char **_val: A pointer to the array. - */ -typedef struct { - size_t len; - 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); -size_t _z_str_array_len(const _z_str_array_t *sa); -_Bool _z_str_array_is_empty(const _z_str_array_t *sa); -void _z_str_array_copy(_z_str_array_t *dst, const _z_str_array_t *src); -void _z_str_array_move(_z_str_array_t *dst, _z_str_array_t *src); -void _z_str_array_clear(_z_str_array_t *sa); -void _z_str_array_free(_z_str_array_t **sa); +_Z_VEC_DEFINE(_z_string, _z_string_t) +_Z_LIST_DEFINE(_z_string, _z_string_t) +_Z_INT_MAP_DEFINE(_z_string, _z_string_t) #endif /* ZENOH_PICO_COLLECTIONS_STRING_H */ diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index c61f8ebb6..8b5788e23 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -236,7 +236,7 @@ * Enable attachments. */ #ifndef Z_FEATURE_ATTACHMENT -#define Z_FEATURE_ATTACHMENT 1 +#define Z_FEATURE_ATTACHMENT 0 #endif /** diff --git a/include/zenoh-pico/link/endpoint.h b/include/zenoh-pico/link/endpoint.h index bd7c8cbc3..9512e32e6 100644 --- a/include/zenoh-pico/link/endpoint.h +++ b/include/zenoh-pico/link/endpoint.h @@ -49,7 +49,7 @@ typedef struct { _Bool _z_locator_eq(const _z_locator_t *left, const _z_locator_t *right); void _z_locator_init(_z_locator_t *locator); -char *_z_locator_to_str(const _z_locator_t *l); +_z_string_t *_z_locator_to_string(const _z_locator_t *loc); int8_t _z_locator_from_str(_z_locator_t *lc, const char *s); size_t _z_locator_size(_z_locator_t *lc); diff --git a/include/zenoh-pico/net/encoding.h b/include/zenoh-pico/net/encoding.h new file mode 100644 index 000000000..eb71cc4aa --- /dev/null +++ b/include/zenoh-pico/net/encoding.h @@ -0,0 +1,36 @@ +// +// Copyright (c) 2024 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, + +#ifndef ZENOH_PICO_ENCODING_NETAPI_H +#define ZENOH_PICO_ENCODING_NETAPI_H + +#include "zenoh-pico/api/constants.h" +#include "zenoh-pico/collections/bytes.h" + +/** + * A zenoh encoding. + */ +typedef struct _z_encoding_t { + _z_bytes_t schema; + uint16_t id; +} _z_encoding_t; + +int8_t _z_encoding_make(_z_encoding_t *encoding, z_encoding_id_t id, const char *schema); +_z_encoding_t _z_encoding_wrap(z_encoding_id_t id, const char *schema); +_z_encoding_t _z_encoding_null(void); +void _z_encoding_clear(_z_encoding_t *encoding); +_Bool _z_encoding_check(const _z_encoding_t *encoding); +void _z_encoding_copy(_z_encoding_t *dst, const _z_encoding_t *src); +void _z_encoding_move(_z_encoding_t *dst, _z_encoding_t *src); + +#endif /* ZENOH_PICO_ENCODING_NETAPI_H */ diff --git a/include/zenoh-pico/net/primitives.h b/include/zenoh-pico/net/primitives.h index 2aa839398..09bed035d 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -18,6 +18,7 @@ #include "zenoh-pico/api/constants.h" #include "zenoh-pico/collections/string.h" +#include "zenoh-pico/net/encoding.h" #include "zenoh-pico/net/publish.h" #include "zenoh-pico/net/query.h" #include "zenoh-pico/net/session.h" @@ -86,7 +87,7 @@ int8_t _z_undeclare_resource(_z_session_t *zn, uint16_t rid); * Returns: * The created :c:type:`_z_publisher_t` or null if the declaration failed. */ -_z_publisher_t *_z_declare_publisher(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, +_z_publisher_t *_z_declare_publisher(const _z_session_rc_t *zn, _z_keyexpr_t keyexpr, z_congestion_control_t congestion_control, z_priority_t priority); /** @@ -143,7 +144,7 @@ int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *pay * Returns: * The created :c:type:`_z_subscriber_t` or null if the declaration failed. */ -_z_subscriber_t *_z_declare_subscriber(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, _z_subinfo_t sub_info, +_z_subscriber_t *_z_declare_subscriber(const _z_session_rc_t *zn, _z_keyexpr_t keyexpr, _z_subinfo_t sub_info, _z_data_handler_t callback, _z_drop_handler_t dropper, void *arg); /** @@ -173,7 +174,7 @@ int8_t _z_undeclare_subscriber(_z_subscriber_t *sub); * Returns: * The created :c:type:`_z_queryable_t` or null if the declaration failed. */ -_z_queryable_t *_z_declare_queryable(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, _Bool complete, +_z_queryable_t *_z_declare_queryable(const _z_session_rc_t *zn, _z_keyexpr_t keyexpr, _Bool complete, _z_queryable_handler_t callback, _z_drop_handler_t dropper, void *arg); /** @@ -225,7 +226,7 @@ int8_t _z_send_reply(const _z_query_t *query, const _z_keyexpr_t keyexpr, const */ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, const z_query_target_t target, const z_consolidation_mode_t consolidation, const _z_value_t value, _z_reply_handler_t callback, - void *arg_call, _z_drop_handler_t dropper, void *arg_drop, uint32_t timeout_ms + _z_drop_handler_t dropper, void *arg, uint32_t timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t attachment diff --git a/include/zenoh-pico/net/query.h b/include/zenoh-pico/net/query.h index 8a6da3fa2..bd62ea5b3 100644 --- a/include/zenoh-pico/net/query.h +++ b/include/zenoh-pico/net/query.h @@ -38,13 +38,6 @@ typedef struct _z_query_t { void _z_query_clear(_z_query_t *q); _Z_REFCOUNT_DEFINE(_z_query, _z_query) -/** - * Container for an owned query rc - */ -typedef struct { - _z_query_rc_t _rc; -} z_owned_query_t; - /** * Return type when declaring a queryable. */ diff --git a/include/zenoh-pico/net/reply.h b/include/zenoh-pico/net/reply.h index 2f6425775..78904dbfd 100644 --- a/include/zenoh-pico/net/reply.h +++ b/include/zenoh-pico/net/reply.h @@ -18,6 +18,7 @@ #include "zenoh-pico/collections/list.h" #include "zenoh-pico/collections/refcount.h" #include "zenoh-pico/collections/string.h" +#include "zenoh-pico/net/encoding.h" #include "zenoh-pico/net/sample.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/session/session.h" @@ -31,13 +32,13 @@ * */ typedef struct _z_reply_data_t { - _z_sample_t sample; + _z_sample_rc_t sample; _z_id_t replier_id; } _z_reply_data_t; void _z_reply_data_clear(_z_reply_data_t *rd); void _z_reply_data_copy(_z_reply_data_t *dst, _z_reply_data_t *src); -_z_reply_t *_z_reply_alloc_and_move(_z_reply_t *_reply); +_z_reply_t _z_reply_move(_z_reply_t *src_reply); _Z_ELEM_DEFINE(_z_reply_data, _z_reply_data_t, _z_noop_size, _z_reply_data_clear, _z_noop_copy) _Z_LIST_DEFINE(_z_reply_data, _z_reply_data_t) @@ -55,9 +56,16 @@ typedef struct _z_reply_t { _z_reply_data_t data; z_reply_tag_t _tag; } _z_reply_t; + +_z_reply_t _z_reply_null(void); void _z_reply_clear(_z_reply_t *src); void _z_reply_free(_z_reply_t **hello); void _z_reply_copy(_z_reply_t *dst, _z_reply_t *src); +_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_bytes_t *payload, + const _z_timestamp_t *timestamp, _z_encoding_t encoding, z_sample_kind_t kind, + z_attachment_t att); + +_Z_REFCOUNT_DEFINE(_z_reply, _z_reply) typedef struct _z_pending_reply_t { _z_reply_t _reply; diff --git a/include/zenoh-pico/net/sample.h b/include/zenoh-pico/net/sample.h index 1d0a50677..36d9c54a5 100644 --- a/include/zenoh-pico/net/sample.h +++ b/include/zenoh-pico/net/sample.h @@ -14,6 +14,7 @@ #ifndef ZENOH_PICO_SAMPLE_NETAPI_H #define ZENOH_PICO_SAMPLE_NETAPI_H +#include "zenoh-pico/net/encoding.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/session/session.h" diff --git a/include/zenoh-pico/protocol/codec/core.h b/include/zenoh-pico/protocol/codec/core.h index 93010a9ba..e2c1c3946 100644 --- a/include/zenoh-pico/protocol/codec/core.h +++ b/include/zenoh-pico/protocol/codec/core.h @@ -18,6 +18,7 @@ #include #include +#include "zenoh-pico/net/encoding.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/utils/config.h" @@ -72,6 +73,7 @@ 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); int8_t _z_str_encode(_z_wbuf_t *buf, const char *s); +int8_t _z_zstr_encode(_z_wbuf_t *wbf, const _z_string_t *s); int8_t _z_str_decode(char **str, _z_zbuf_t *buf); size_t _z_encoding_len(const _z_encoding_t *en); diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 4d9453fc2..1b5119948 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -25,6 +25,7 @@ #include "zenoh-pico/collections/refcount.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" +#include "zenoh-pico/net/encoding.h" #include "zenoh-pico/system/platform.h" #define _Z_OPTIONAL @@ -56,14 +57,6 @@ uint8_t _z_id_len(_z_id_t id); _Bool _z_id_check(_z_id_t id); _z_id_t _z_id_empty(void); -/** - * A zenoh encoding. - */ -typedef struct { - _z_bytes_t schema; - uint16_t id; -} _z_encoding_t; - /** * A zenoh timestamp. */ @@ -248,12 +241,12 @@ void _z_value_free(_z_value_t **hello); * * 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. + * _z_string_vec_t locators: The locators of the scouted entity. * z_whatami_t whatami: The kind of zenoh entity. */ typedef struct { _z_id_t zid; - _z_str_array_t locators; + _z_string_vec_t locators; z_whatami_t whatami; uint8_t version; } _z_hello_t; diff --git a/include/zenoh-pico/protocol/definitions/message.h b/include/zenoh-pico/protocol/definitions/message.h index 4ff9faaca..700c63967 100644 --- a/include/zenoh-pico/protocol/definitions/message.h +++ b/include/zenoh-pico/protocol/definitions/message.h @@ -15,6 +15,7 @@ #ifndef INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_MESSAGE_H #define INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_MESSAGE_H +#include "zenoh-pico/net/encoding.h" #include "zenoh-pico/protocol/core.h" #include "zenoh-pico/protocol/definitions/core.h" /* Zenoh Messages */ diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index 34ff93139..fbfdb5a62 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -49,12 +49,12 @@ _Z_ELEM_DEFINE(_z_resource, _z_resource_t, _z_noop_size, _z_resource_clear, _z_r _Z_LIST_DEFINE(_z_resource, _z_resource_t) // Forward declaration to avoid cyclical include -typedef struct z_sample_t z_sample_t; +typedef struct _z_sample_rc_t z_loaned_sample_t; /** * The callback signature of the functions handling data messages. */ -typedef void (*_z_data_handler_t)(const z_sample_t *sample, void *arg); +typedef void (*_z_data_handler_t)(const z_loaned_sample_t *sample, void *arg); typedef struct { _z_keyexpr_t _key; @@ -80,11 +80,13 @@ typedef struct { uint32_t _id; } _z_publication_t; -typedef struct z_query_t z_query_t; // Forward type declaration to avoid cyclical include +// Forward type declaration to avoid cyclical include +typedef struct _z_query_rc_t z_loaned_query_t; + /** * The callback signature of the functions handling query messages. */ -typedef void (*_z_queryable_handler_t)(const z_query_t *query, void *arg); +typedef void (*_z_queryable_handler_t)(const z_loaned_query_t *query, void *arg); typedef struct { _z_keyexpr_t _key; @@ -108,19 +110,19 @@ _Z_LIST_DEFINE(_z_session_queryable_rc, _z_session_queryable_rc_t) typedef struct _z_reply_t _z_reply_t; typedef _z_list_t _z_reply_data_list_t; typedef _z_list_t _z_pending_reply_list_t; -struct __z_reply_handler_wrapper_t; +typedef struct _z_reply_rc_t _z_reply_rc_t; +typedef _z_reply_rc_t z_loaned_reply_t; /** * The callback signature of the functions handling query replies. */ -typedef void (*_z_reply_handler_t)(_z_reply_t *reply, struct __z_reply_handler_wrapper_t *arg); +typedef void (*_z_reply_handler_t)(const z_loaned_reply_t *reply, void *arg); typedef struct { _z_keyexpr_t _key; _z_zint_t _id; _z_reply_handler_t _callback; _z_drop_handler_t _dropper; - void *_call_arg; // TODO[API-NET]: These two can be merged into one, when API and NET are a single layer - void *_drop_arg; // TODO[API-NET]: These two can be merged into one, when API and NET are a single layer + void *_arg; char *_parameters; _z_pending_reply_list_t *_pending_replies; z_query_target_t _target; diff --git a/include/zenoh-pico/session/subscription.h b/include/zenoh-pico/session/subscription.h index 56ec11733..d15a60cf0 100644 --- a/include/zenoh-pico/session/subscription.h +++ b/include/zenoh-pico/session/subscription.h @@ -15,6 +15,7 @@ #ifndef INCLUDE_ZENOH_PICO_SESSION_SUBSCRIPTION_H #define INCLUDE_ZENOH_PICO_SESSION_SUBSCRIPTION_H +#include "zenoh-pico/net/encoding.h" #include "zenoh-pico/net/session.h" /*------------------ Subscription ------------------*/ diff --git a/include/zenoh-pico/utils/config.h b/include/zenoh-pico/utils/config.h index e6ec4e934..c7d49188a 100644 --- a/include/zenoh-pico/utils/config.h +++ b/include/zenoh-pico/utils/config.h @@ -46,7 +46,7 @@ int8_t _z_config_init(_z_config_t *ps); * key: The key of the property to add. * value: The value of the property to add. */ -int8_t _zp_config_insert(_z_config_t *ps, uint8_t key, _z_string_t value); +int8_t _zp_config_insert(_z_config_t *ps, uint8_t key, const char *value); /** * Get the property with the given key from a properties map. diff --git a/include/zenoh-pico/utils/logging.h b/include/zenoh-pico/utils/logging.h index f6e3304ef..c440d1774 100644 --- a/include/zenoh-pico/utils/logging.h +++ b/include/zenoh-pico/utils/logging.h @@ -17,6 +17,8 @@ #include +#include "zenoh-pico/system/platform-common.h" + // Logging values #define _Z_LOG_LVL_ERROR 1 #define _Z_LOG_LVL_INFO 2 diff --git a/src/api/api.c b/src/api/api.c index d08d6c579..f2bed7e36 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -42,62 +42,56 @@ #include "zenoh-pico/utils/uuid.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); } -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); } +int8_t z_view_string_wrap(z_view_string_t *str, const char *value) { + str->_val = _z_string_wrap((char *)value); + return _Z_RES_OK; +} -z_keyexpr_t z_keyexpr_unchecked(const char *name) { return _z_rname(name); } +const z_loaned_string_t *z_string_array_get(const z_loaned_string_array_t *a, size_t k) { + return _z_string_vec_get(a, k); +} -z_owned_str_t z_keyexpr_to_string(z_keyexpr_t keyexpr) { - z_owned_str_t ret = {._value = NULL}; +size_t z_string_array_len(const z_loaned_string_array_t *a) { return _z_string_vec_len(a); } - if (keyexpr._id == Z_RESOURCE_ID_NONE) { - size_t ke_len = _z_str_size(keyexpr._suffix); +_Bool z_string_array_is_empty(const z_loaned_string_array_t *a) { return _z_string_vec_is_empty(a); } - ret._value = (char *)z_malloc(ke_len); - if (ret._value != NULL) { - _z_str_n_copy(ret._value, keyexpr._suffix, ke_len); - } - } +int8_t z_view_keyexpr_from_string(z_view_keyexpr_t *keyexpr, const char *name) { + keyexpr->_val = _z_rname(name); + return _Z_RES_OK; +} - return ret; +int8_t z_view_keyexpr_from_string_unchecked(z_view_keyexpr_t *keyexpr, const char *name) { + keyexpr->_val = _z_rname(name); + return _Z_RES_OK; } -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; +int8_t z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *s) { + int8_t ret = _Z_RES_OK; + if (keyexpr->_id == Z_RESOURCE_ID_NONE) { + s->_val = (_z_string_t *)z_malloc(sizeof(_z_string_t)); + if (s->_val != NULL) { + s->_val->val = _z_str_clone(keyexpr->_suffix); + s->_val->len = strlen(keyexpr->_suffix); + } else { + ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } } else { - z_bytes_t ret = {.start = NULL, .len = 0, ._is_alloc = false}; - return ret; + ret = _Z_ERR_GENERIC; } -} -_Bool zp_keyexpr_was_declared(const z_keyexpr_t *keyexpr) { - _Bool ret = false; - if (keyexpr->_id != Z_RESOURCE_ID_NONE) { - ret = true; - } return ret; } -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.in->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 - return ret; +int8_t zp_keyexpr_resolve(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *str) { + _z_keyexpr_t ekey = _z_get_expanded_key_from_key(&_Z_RC_IN_VAL(zs), keyexpr); + *str->_val = _z_string_make((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 + return _Z_RES_OK; } -_Bool z_keyexpr_is_initialized(const z_keyexpr_t *keyexpr) { +_Bool z_keyexpr_is_initialized(const z_loaned_keyexpr_t *keyexpr) { _Bool ret = false; if ((keyexpr->_id != Z_RESOURCE_ID_NONE) || (keyexpr->_suffix != NULL)) { @@ -126,150 +120,174 @@ int8_t zp_keyexpr_canonize_null_terminated(char *start) { return ret; } -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 = zp_keyexpr_includes_null_terminated(l._suffix, r._suffix); - } else { - ret = _Z_ERR_GENERIC; +_Bool z_keyexpr_includes(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r) { + if ((l->_id == Z_RESOURCE_ID_NONE) && (r->_id == Z_RESOURCE_ID_NONE)) { + return zp_keyexpr_includes_null_terminated(l->_suffix, r->_suffix); } - - return ret; + return false; } -int8_t zp_keyexpr_includes_null_terminated(const char *l, const char *r) { - int8_t ret = 0; - +_Bool zp_keyexpr_includes_null_terminated(const char *l, const char *r) { if (l != NULL && r != NULL) { - ret = _z_keyexpr_includes(l, strlen(l), r, strlen(r)) == true ? 0 : -1; - } else { - ret = _Z_ERR_GENERIC; + return _z_keyexpr_includes(l, strlen(l), r, strlen(r)); } - - return ret; + return false; } -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 = zp_keyexpr_intersect_null_terminated(l._suffix, r._suffix); - } else { - ret = _Z_ERR_GENERIC; +_Bool z_keyexpr_intersects(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r) { + if ((l->_id == Z_RESOURCE_ID_NONE) && (r->_id == Z_RESOURCE_ID_NONE)) { + return zp_keyexpr_intersect_null_terminated(l->_suffix, r->_suffix); } - - return ret; + return false; } -int8_t zp_keyexpr_intersect_null_terminated(const char *l, const char *r) { - int8_t ret = -1; - +_Bool zp_keyexpr_intersect_null_terminated(const char *l, const char *r) { if (l != NULL && r != NULL) { - ret = (_z_keyexpr_intersects(l, strlen(l), r, strlen(r)) == true) ? 0 : -1; - } else { - ret = _Z_ERR_GENERIC; + return _z_keyexpr_intersects(l, strlen(l), r, strlen(r)); } - - return ret; + return false; } -int8_t z_keyexpr_equals(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 = zp_keyexpr_equals_null_terminated(l._suffix, r._suffix); - } else { - ret = _Z_ERR_GENERIC; +_Bool z_keyexpr_equals(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r) { + if ((l->_id == Z_RESOURCE_ID_NONE) && (r->_id == Z_RESOURCE_ID_NONE)) { + return zp_keyexpr_equals_null_terminated(l->_suffix, r->_suffix); } - - return ret; + return false; } -int8_t zp_keyexpr_equals_null_terminated(const char *l, const char *r) { - int8_t ret = -1; - +_Bool zp_keyexpr_equals_null_terminated(const char *l, const char *r) { if (l != NULL && r != NULL) { size_t llen = strlen(l); if (llen == strlen(r)) { if (strncmp(l, r, llen) == 0) { - ret = 0; + return true; } } - } else { - ret = _Z_ERR_GENERIC; } - - return ret; + return false; } -z_owned_config_t z_config_new(void) { return (z_owned_config_t){._value = _z_config_empty()}; } +void z_config_new(z_owned_config_t *config) { config->_val = _z_config_empty(); } -z_owned_config_t z_config_default(void) { return (z_owned_config_t){._value = _z_config_default()}; } +void z_config_default(z_owned_config_t *config) { config->_val = _z_config_default(); } -const char *zp_config_get(z_config_t config, uint8_t key) { return _z_config_get(config._val, key); } +const char *zp_config_get(const z_loaned_config_t *config, uint8_t key) { return _z_config_get(config, key); } -int8_t zp_config_insert(z_config_t config, uint8_t key, z_string_t value) { - return _zp_config_insert(config._val, key, value); +int8_t zp_config_insert(z_loaned_config_t *config, uint8_t key, const char *value) { + return _zp_config_insert(config, key, 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)); +void z_scouting_config_default(z_owned_scouting_config_t *sc) { + sc->_val = _z_config_empty(); - return (z_owned_scouting_config_t){._value = sc}; + _zp_config_insert(sc->_val, Z_CONFIG_MULTICAST_LOCATOR_KEY, Z_CONFIG_MULTICAST_LOCATOR_DEFAULT); + _zp_config_insert(sc->_val, Z_CONFIG_SCOUTING_TIMEOUT_KEY, Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT); + _zp_config_insert(sc->_val, Z_CONFIG_SCOUTING_WHAT_KEY, Z_CONFIG_SCOUTING_WHAT_DEFAULT); } -z_owned_scouting_config_t z_scouting_config_from(z_config_t c) { - _z_scouting_config_t *sc = _z_config_empty(); +int8_t z_scouting_config_from(z_owned_scouting_config_t *sc, const z_loaned_config_t *c) { + sc->_val = _z_config_empty(); char *opt; - opt = _z_config_get(c._val, Z_CONFIG_MULTICAST_LOCATOR_KEY); + opt = _z_config_get(c, 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->_val, Z_CONFIG_MULTICAST_LOCATOR_KEY, Z_CONFIG_MULTICAST_LOCATOR_DEFAULT); } else { - _zp_config_insert(sc, Z_CONFIG_MULTICAST_LOCATOR_KEY, _z_string_make(opt)); + _zp_config_insert(sc->_val, Z_CONFIG_MULTICAST_LOCATOR_KEY, opt); } - opt = _z_config_get(c._val, Z_CONFIG_SCOUTING_TIMEOUT_KEY); + opt = _z_config_get(c, 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->_val, Z_CONFIG_SCOUTING_TIMEOUT_KEY, Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT); } else { - _zp_config_insert(sc, Z_CONFIG_SCOUTING_TIMEOUT_KEY, _z_string_make(opt)); + _zp_config_insert(sc->_val, Z_CONFIG_SCOUTING_TIMEOUT_KEY, opt); } - opt = _z_config_get(c._val, Z_CONFIG_SCOUTING_WHAT_KEY); + opt = _z_config_get(c, 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->_val, Z_CONFIG_SCOUTING_WHAT_KEY, Z_CONFIG_SCOUTING_WHAT_DEFAULT); } else { - _zp_config_insert(sc, Z_CONFIG_SCOUTING_WHAT_KEY, _z_string_make(opt)); + _zp_config_insert(sc->_val, Z_CONFIG_SCOUTING_WHAT_KEY, opt); } - return (z_owned_scouting_config_t){._value = sc}; + return _Z_RES_OK; } -const char *zp_scouting_config_get(z_scouting_config_t sc, uint8_t key) { return _z_config_get(sc._val, key); } +const char *zp_scouting_config_get(const z_loaned_scouting_config_t *sc, uint8_t key) { return _z_config_get(sc, key); } -int8_t zp_scouting_config_insert(z_scouting_config_t sc, uint8_t key, z_string_t value) { - return _zp_config_insert(sc._val, key, value); +int8_t zp_scouting_config_insert(z_loaned_scouting_config_t *sc, uint8_t key, const char *value) { + return _zp_config_insert(sc, key, value); } -z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix) { - return (_z_encoding_t){ - .id = prefix, .schema = _z_bytes_wrap((const uint8_t *)suffix, (suffix == NULL) ? (size_t)0 : strlen(suffix))}; +// TODO: STRING BASED ENCODING FEATURE +int8_t zp_encoding_make(z_owned_encoding_t *encoding, z_encoding_id_t id, const char *schema) { + // Init encoding + encoding->_val = (_z_encoding_t *)z_malloc(sizeof(_z_encoding_t)); + if (encoding->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + return _z_encoding_make(encoding->_val, id, schema); } -z_encoding_t z_encoding_default(void) { return z_encoding(Z_ENCODING_PREFIX_DEFAULT, NULL); } +z_owned_encoding_t *z_encoding_move(z_owned_encoding_t *encoding) { return encoding; } -_Bool z_timestamp_check(z_timestamp_t ts) { return _z_timestamp_check(&ts); } +int8_t z_encoding_null(z_owned_encoding_t *encoding) { return zp_encoding_make(encoding, Z_ENCODING_ID_DEFAULT, NULL); } + +_Bool z_encoding_check(const z_owned_encoding_t *encoding) { return _z_encoding_check(encoding->_val); } + +void z_encoding_drop(z_owned_encoding_t *encoding) { + if (encoding == NULL) { + return; + } + if (!_z_bytes_is_empty(&encoding->_val->schema)) { + _z_bytes_clear(&encoding->_val->schema); + } + z_free(encoding->_val); +} + +const z_loaned_encoding_t *z_encoding_loan(const z_owned_encoding_t *encoding) { return encoding->_val; } + +// Convert a user owned encoding to an internal encoding, return default encoding if value invalid +static _z_encoding_t _z_encoding_from_owned(const z_owned_encoding_t *encoding) { + if (encoding == NULL) { + return _z_encoding_null(); + } + if (encoding->_val == NULL) { + return _z_encoding_null(); + } + return *encoding->_val; +} + +const z_loaned_bytes_t *z_value_payload(const z_loaned_value_t *value) { return &value->payload; } + +size_t z_bytes_len(const z_loaned_bytes_t *bytes) { return bytes->len; } + +int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *s) { + // Init owned string + z_string_null(s); + s->_val = (_z_string_t *)z_malloc(sizeof(_z_string_t)); + if (s->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Convert bytes to string + *s->_val = _z_string_from_bytes(bytes); + return _Z_RES_OK; +} -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}; +int8_t z_bytes_encode_from_string(z_owned_bytes_t *buffer, const z_loaned_string_t *s) { + // Init owned bytes + z_bytes_null(buffer); + buffer->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t)); + if (buffer->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Encode data + *buffer->_val = _z_bytes_wrap((uint8_t *)s->val, s->len); + return _Z_RES_OK; } +_Bool z_timestamp_check(z_timestamp_t ts) { return _z_timestamp_check(&ts); } + z_query_target_t z_query_target_default(void) { return Z_QUERY_TARGET_DEFAULT; } z_query_consolidation_t z_query_consolidation_auto(void) { @@ -290,31 +308,20 @@ z_query_consolidation_t z_query_consolidation_none(void) { z_query_consolidation_t z_query_consolidation_default(void) { return z_query_consolidation_auto(); } -z_bytes_t z_query_parameters(const z_query_t *query) { - z_bytes_t parameters = - _z_bytes_wrap((uint8_t *)query->_val._rc.in->val._parameters, strlen(query->_val._rc.in->val._parameters)); - return parameters; +void z_query_parameters(const z_loaned_query_t *query, z_view_string_t *parameters) { + parameters->_val.val = query->in->val._parameters; + parameters->_val.len = strlen(query->in->val._parameters); } -z_value_t z_query_value(const z_query_t *query) { return query->_val._rc.in->val._value; } +const z_loaned_value_t *z_query_value(const z_loaned_query_t *query) { return &query->in->val._value; } #if Z_FEATURE_ATTACHMENT == 1 -z_attachment_t z_query_attachment(const z_query_t *query) { return query->_val._rc.in->val.attachment; } +z_attachment_t z_query_attachment(const z_loaned_query_t *query) { return query->in->val.attachment; } #endif -z_keyexpr_t z_query_keyexpr(const z_query_t *query) { return query->_val._rc.in->val._key; } +const z_loaned_keyexpr_t *z_query_keyexpr(const z_loaned_query_t *query) { return &query->in->val._key; } -_Bool z_value_is_initialized(z_value_t *value) { - _Bool ret = false; - - if ((value->payload.start != NULL)) { - ret = true; - } - - return ret; -} - -void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_sample_t *sample) { +void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_loaned_sample_t *sample) { if (closure->call != NULL) { (closure->call)(sample, closure->context); } @@ -326,7 +333,7 @@ void z_closure_owned_sample_call(const z_owned_closure_owned_sample_t *closure, } } -void z_closure_query_call(const z_owned_closure_query_t *closure, const z_query_t *query) { +void z_closure_query_call(const z_owned_closure_query_t *closure, const z_loaned_query_t *query) { if (closure->call != NULL) { (closure->call)(query, closure->context); } @@ -338,7 +345,13 @@ void z_closure_owned_query_call(const z_owned_closure_owned_query_t *closure, z_ } } -void z_closure_reply_call(const z_owned_closure_reply_t *closure, z_owned_reply_t *reply) { +void z_closure_reply_call(const z_owned_closure_reply_t *closure, const z_loaned_reply_t *reply) { + if (closure->call != NULL) { + (closure->call)(reply, closure->context); + } +} + +void z_closure_owned_reply_call(const z_owned_closure_owned_reply_t *closure, z_owned_reply_t *reply) { if (closure->call != NULL) { (closure->call)(reply, closure->context); } @@ -356,180 +369,119 @@ void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id) } } -/**************** Loans ****************/ -#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; \ - 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); \ - } \ - } - -#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) \ - 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) \ - _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; \ - size_t size = _z_str_size(val->_value); \ - ret._value = (_##type)z_malloc(size); \ - if (ret._value != NULL) { \ - f_copy(ret._value, val->_value, size); \ - } \ - return ret; \ - } \ - void z_##name##_drop(ownedtype *val) { \ - if (val->_value != NULL) { \ - f_free(&val->_value); \ - } \ - } - -#define OWNED_FUNCTIONS_PTR_RC(type, ownedtype, name) \ - _Bool z_##name##_check(const ownedtype *val) { return val->_rc.in != NULL; } \ - type z_##name##_loan(const ownedtype *val) { return (type){._val = (ownedtype){._rc.in = val->_rc.in}}; } \ - ownedtype z_##name##_null(void) { return (ownedtype){._rc.in = NULL}; } \ - ownedtype *z_##name##_move(ownedtype *val) { return val; } \ - ownedtype z_##name##_clone(ownedtype *val) { return (ownedtype){._rc = _z_##name##_rc_clone(&val->_rc)}; } \ - void z_##name##_drop(ownedtype *val) { _z_##name##_rc_drop(&val->_rc); } +#define OWNED_FUNCTIONS_PTR(type, name, f_copy, f_free) \ + _Bool z_##name##_check(const z_owned_##name##_t *obj) { return obj->_val != NULL; } \ + const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *obj) { return obj->_val; } \ + z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *obj) { return obj->_val; } \ + void z_##name##_null(z_owned_##name##_t *obj) { obj->_val = NULL; } \ + z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *obj) { return obj; } \ + int8_t z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src) { \ + int8_t ret = _Z_RES_OK; \ + obj->_val = (type *)z_malloc(sizeof(type)); \ + if (obj->_val != NULL) { \ + f_copy(obj->_val, src); \ + } else { \ + ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; \ + } \ + return ret; \ + } \ + void z_##name##_drop(z_owned_##name##_t *obj) { \ + if (obj->_val != NULL) { \ + f_free(&obj->_val); \ + } \ + } + +#define OWNED_FUNCTIONS_RC(name) \ + _Bool z_##name##_check(const z_owned_##name##_t *val) { return val->_rc.in != NULL; } \ + const z_loaned_##name##_t *z_##name##_loan(const z_owned_##name##_t *val) { return &val->_rc; } \ + z_loaned_##name##_t *z_##name##_loan_mut(z_owned_##name##_t *val) { return &val->_rc; } \ + void z_##name##_null(z_owned_##name##_t *val) { val->_rc.in = NULL; } \ + z_owned_##name##_t *z_##name##_move(z_owned_##name##_t *val) { return val; } \ + int8_t z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src) { \ + int8_t ret = _Z_RES_OK; \ + obj->_rc = _z_##name##_rc_clone((z_loaned_##name##_t *)src); \ + if (obj->_rc.in == NULL) { \ + ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; \ + } \ + return ret; \ + } \ + void z_##name##_drop(z_owned_##name##_t *val) { \ + if (val->_rc.in != NULL) { \ + if (_z_##name##_rc_drop(&val->_rc)) { \ + val->_rc.in = NULL; \ + } \ + } \ + } + +#define VIEW_FUNCTIONS_PTR(type, name) \ + const z_loaned_##name##_t *z_view_##name##_loan(const z_view_##name##_t *obj) { return &obj->_val; } \ + z_loaned_##name##_t *z_view_##name##_loan_mut(z_view_##name##_t *obj) { return &obj->_val; } static inline void _z_owner_noop_copy(void *dst, const void *src) { (void)(dst); (void)(src); } -OWNED_FUNCTIONS_STR(z_str_t, z_owned_str_t, str, _z_str_free, _z_str_n_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_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) -OWNED_FUNCTIONS_PTR_INTERNAL(z_str_array_t, z_owned_str_array_t, str_array, _z_str_array_free, _z_owner_noop_copy) - -// Owned sample functions -_Bool z_sample_check(const z_owned_sample_t *val) { return val->_value != ((void *)0); } -z_sample_t z_sample_loan(const z_owned_sample_t *val) { return *val->_value; } -z_owned_sample_t z_sample_null(void) { return (z_owned_sample_t){._value = ((void *)0)}; } -z_owned_sample_t *z_sample_move(z_owned_sample_t *val) { return val; } -z_owned_sample_t z_sample_clone(z_owned_sample_t *val) { - z_owned_sample_t ret; - ret._value = (z_sample_t *)z_malloc(sizeof(z_sample_t)); - if (ret._value != ((void *)0)) { - ret._value->_rc = _z_sample_rc_clone(&val->_value->_rc); - } - return ret; -} -void z_sample_drop(z_owned_sample_t *val) { - if (val->_value != ((void *)0)) { - _z_sample_rc_drop(&val->_value->_rc); - z_free(val->_value); - } -} - -_Bool z_session_check(const z_owned_session_t *val) { return val->_value.in != NULL; } -z_session_t z_session_loan(const z_owned_session_t *val) { return (z_session_t){._val = val->_value}; } -z_owned_session_t z_session_null(void) { return (z_owned_session_t){._value = {.in = NULL}}; } -z_owned_session_t *z_session_move(z_owned_session_t *val) { return val; } -z_owned_session_t z_session_clone(z_owned_session_t *val) { - z_owned_session_t ret; - ret._value = _z_session_rc_clone(&val->_value); - return ret; -} -void z_session_drop(z_owned_session_t *val) { z_close(val); } - -#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}; -} - -z_owned_closure_owned_sample_t z_closure_owned_sample(_z_owned_sample_handler_t call, _z_dropper_handler_t drop, - void *context) { - return (z_owned_closure_owned_sample_t){.call = call, .drop = drop, .context = context}; -} - -z_owned_closure_query_t z_closure_query(_z_queryable_handler_t call, _z_dropper_handler_t drop, void *context) { - return (z_owned_closure_query_t){.call = call, .drop = drop, .context = context}; -} - -z_owned_closure_owned_query_t z_closure_owned_query(_z_owned_query_handler_t call, _z_dropper_handler_t drop, - void *context) { - return (z_owned_closure_owned_query_t){.call = call, .drop = drop, .context = context}; -} - -z_owned_closure_reply_t z_closure_reply(z_owned_reply_handler_t call, _z_dropper_handler_t drop, void *context) { - return (z_owned_closure_reply_t){.call = call, .drop = drop, .context = context}; -} - -z_owned_closure_hello_t z_closure_hello(z_owned_hello_handler_t call, _z_dropper_handler_t drop, void *context) { - return (z_owned_closure_hello_t){.call = call, .drop = drop, .context = context}; -} - -z_owned_closure_zid_t z_closure_zid(z_id_handler_t call, _z_dropper_handler_t drop, void *context) { - return (z_owned_closure_zid_t){.call = call, .drop = drop, .context = context}; -} - -OWNED_FUNCTIONS_CLOSURE(z_owned_closure_sample_t, closure_sample) -OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_sample_t, closure_owned_sample) -OWNED_FUNCTIONS_CLOSURE(z_owned_closure_query_t, closure_query) -OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_query_t, closure_owned_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) +OWNED_FUNCTIONS_PTR(_z_config_t, config, _z_owner_noop_copy, _z_config_free) +OWNED_FUNCTIONS_PTR(_z_scouting_config_t, scouting_config, _z_owner_noop_copy, _z_scouting_config_free) +OWNED_FUNCTIONS_PTR(_z_string_t, string, _z_string_copy, _z_string_free) +OWNED_FUNCTIONS_PTR(_z_value_t, value, _z_value_copy, _z_value_free) + +OWNED_FUNCTIONS_PTR(_z_keyexpr_t, keyexpr, _z_keyexpr_copy, _z_keyexpr_free) +VIEW_FUNCTIONS_PTR(_z_keyexpr_t, keyexpr) +VIEW_FUNCTIONS_PTR(_z_string_t, string) + +OWNED_FUNCTIONS_PTR(_z_hello_t, hello, _z_owner_noop_copy, _z_hello_free) +OWNED_FUNCTIONS_PTR(_z_string_vec_t, string_array, _z_owner_noop_copy, _z_string_vec_free) +VIEW_FUNCTIONS_PTR(_z_string_vec_t, string_array) +OWNED_FUNCTIONS_PTR(_z_bytes_t, bytes, _z_bytes_copy, _z_bytes_free) + +static _z_bytes_t _z_bytes_from_owned_bytes(z_owned_bytes_t *bytes) { + _z_bytes_t b = _z_bytes_empty(); + if ((bytes != NULL) && (bytes->_val != NULL)) { + b = _z_bytes_wrap(bytes->_val->start, bytes->_val->len); + } + return b; +} + +OWNED_FUNCTIONS_RC(sample) +OWNED_FUNCTIONS_RC(session) + +#define OWNED_FUNCTIONS_CLOSURE(ownedtype, name, f_call, f_drop) \ + _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; \ + } \ + void z_##name##_null(ownedtype *val) { \ + val->call = NULL; \ + val->drop = NULL; \ + val->context = NULL; \ + } \ + int8_t z_##name(ownedtype *closure, f_call call, f_drop drop, void *context) { \ + closure->call = call; \ + closure->drop = drop; \ + closure->context = context; \ + \ + return _Z_RES_OK; \ + } + +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_sample_t, closure_sample, _z_data_handler_t, z_dropper_handler_t) +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_sample_t, closure_owned_sample, z_owned_sample_handler_t, + z_dropper_handler_t) +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_query_t, closure_query, _z_queryable_handler_t, z_dropper_handler_t) +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_query_t, closure_owned_query, z_owned_query_handler_t, + z_dropper_handler_t) +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_reply_t, closure_reply, _z_reply_handler_t, z_dropper_handler_t) +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_reply_t, closure_owned_reply, z_owned_reply_handler_t, + z_dropper_handler_t) +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_hello_t, closure_hello, z_owned_hello_handler_t, z_dropper_handler_t) +OWNED_FUNCTIONS_CLOSURE(z_owned_closure_zid_t, closure_zid, z_id_handler_t, z_dropper_handler_t) /************* Primitives **************/ typedef struct __z_hello_handler_wrapper_t { @@ -538,7 +490,7 @@ typedef struct __z_hello_handler_wrapper_t { } __z_hello_handler_wrapper_t; void __z_hello_handler(_z_hello_t *hello, __z_hello_handler_wrapper_t *wrapped_ctx) { - z_owned_hello_t ohello = {._value = hello}; + z_owned_hello_t ohello = {._val = hello}; wrapped_ctx->user_call(&ohello, wrapped_ctx->ctx); } @@ -556,26 +508,26 @@ 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 *opt_as_str = _z_config_get(config->_value, Z_CONFIG_SCOUTING_WHAT_KEY); + char *opt_as_str = _z_config_get(config->_val, Z_CONFIG_SCOUTING_WHAT_KEY); if (opt_as_str == NULL) { opt_as_str = (char *)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); + opt_as_str = _z_config_get(config->_val, Z_CONFIG_MULTICAST_LOCATOR_KEY); if (opt_as_str == NULL) { opt_as_str = (char *)Z_CONFIG_MULTICAST_LOCATOR_DEFAULT; } char *mcast_locator = opt_as_str; - opt_as_str = _z_config_get(config->_value, Z_CONFIG_SCOUTING_TIMEOUT_KEY); + opt_as_str = _z_config_get(config->_val, Z_CONFIG_SCOUTING_TIMEOUT_KEY); if (opt_as_str == NULL) { opt_as_str = (char *)Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT; } uint32_t timeout = strtoul(opt_as_str, NULL, 10); _z_id_t zid = _z_id_empty(); - char *zid_str = _z_config_get(config->_value, Z_CONFIG_SESSION_ZID_KEY); + char *zid_str = _z_config_get(config->_val, Z_CONFIG_SESSION_ZID_KEY); if (zid_str != NULL) { _z_uuid_to_bytes(zid.id, zid_str); } @@ -591,43 +543,43 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb return ret; } -z_owned_session_t z_open(z_owned_config_t *config) { - z_owned_session_t zs = {._value = {.in = NULL}}; +int8_t z_open(z_owned_session_t *zs, z_owned_config_t *config) { + z_session_null(zs); // Create rc _z_session_rc_t zsrc = _z_session_rc_new(); if (zsrc.in == NULL) { z_config_drop(config); - return zs; + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Open session - if (_z_open(&zsrc.in->val, config->_value) != _Z_RES_OK) { + int8_t ret = _z_open(&zsrc.in->val, config->_val); + if (ret != _Z_RES_OK) { _z_session_rc_drop(&zsrc); z_config_drop(config); - return zs; + return ret; } // Store rc in session - zs._value = zsrc; + zs->_rc = zsrc; z_config_drop(config); - return zs; + return _Z_RES_OK; } int8_t z_close(z_owned_session_t *zs) { - if ((zs == NULL) || (zs->_value.in == NULL)) { + if (zs == NULL || !z_session_check(zs)) { return _Z_RES_OK; } - _z_close(&zs->_value.in->val); - _z_session_rc_drop(&zs->_value); - zs->_value.in = NULL; + _z_close(&_Z_OWNED_RC_IN_VAL(zs)); + z_session_drop(zs); return _Z_RES_OK; } -int8_t z_info_peers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) { +int8_t z_info_peers_zid(const z_loaned_session_t *zs, z_owned_closure_zid_t *callback) { // Call transport function - switch (zs._val.in->val._tp._type) { + switch (_Z_RC_IN_VAL(zs)._tp._type) { case _Z_TRANSPORT_MULTICAST_TYPE: case _Z_TRANSPORT_RAWETH_TYPE: - _zp_multicast_fetch_zid(&zs._val.in->val._tp, callback); + _zp_multicast_fetch_zid(&(_Z_RC_IN_VAL(zs)._tp), callback); break; default: break; @@ -642,11 +594,11 @@ int8_t z_info_peers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) { return 0; } -int8_t z_info_routers_zid(const z_session_t zs, z_owned_closure_zid_t *callback) { +int8_t z_info_routers_zid(const z_loaned_session_t *zs, z_owned_closure_zid_t *callback) { // Call transport function - switch (zs._val.in->val._tp._type) { + switch (_Z_RC_IN_VAL(zs)._tp._type) { case _Z_TRANSPORT_UNICAST_TYPE: - _zp_unicast_fetch_zid(&zs._val.in->val._tp, callback); + _zp_unicast_fetch_zid(&(_Z_RC_IN_VAL(zs)._tp), callback); break; default: break; @@ -661,42 +613,52 @@ 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) { return zs._val.in->val._local_zid; } +z_id_t z_info_zid(const z_loaned_session_t *zs) { return _Z_RC_IN_VAL(zs)._local_zid; } -z_keyexpr_t z_sample_keyexpr(const z_sample_t *sample) { return sample->_rc.in->val.keyexpr; } -z_bytes_t z_sample_payload(const z_sample_t *sample) { return sample->_rc.in->val.payload; } -z_timestamp_t z_sample_timestamp(const z_sample_t *sample) { return sample->_rc.in->val.timestamp; } -z_encoding_t z_sample_encoding(const z_sample_t *sample) { return sample->_rc.in->val.encoding; } -z_sample_kind_t z_sample_kind(const z_sample_t *sample) { return sample->_rc.in->val.kind; } -z_qos_t z_sample_qos(const z_sample_t *sample) { return sample->_rc.in->val.qos; } +const z_loaned_keyexpr_t *z_sample_keyexpr(const z_loaned_sample_t *sample) { return &_Z_RC_IN_VAL(sample).keyexpr; } +z_sample_kind_t z_sample_kind(const z_loaned_sample_t *sample) { return _Z_RC_IN_VAL(sample).kind; } +const z_loaned_bytes_t *z_sample_payload(const z_loaned_sample_t *sample) { return &_Z_RC_IN_VAL(sample).payload; } +z_timestamp_t z_sample_timestamp(const z_loaned_sample_t *sample) { return _Z_RC_IN_VAL(sample).timestamp; } +const z_loaned_encoding_t *z_sample_encoding(const z_loaned_sample_t *sample) { return &_Z_RC_IN_VAL(sample).encoding; } +z_qos_t z_sample_qos(const z_loaned_sample_t *sample) { return _Z_RC_IN_VAL(sample).qos; } #if Z_FEATURE_ATTACHMENT == 1 -z_attachment_t z_sample_attachment(const z_sample_t *sample) { return sample->_rc.in->val.attachment; } +z_attachment_t z_sample_attachment(const z_loaned_sample_t *sample) { return _Z_RC_IN_VAL(sample).attachment; } #endif +const char *z_string_data(const z_loaned_string_t *str) { return str->val; } + #if Z_FEATURE_PUBLICATION == 1 -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); } - -z_put_options_t z_put_options_default(void) { - return (z_put_options_t) { - .encoding = z_encoding_default(), .congestion_control = Z_CONGESTION_CONTROL_DEFAULT, - .priority = Z_PRIORITY_DEFAULT, +int8_t _z_publisher_drop(_z_publisher_t **pub) { + int8_t ret = _Z_RES_OK; + + ret = _z_undeclare_publisher(*pub); + _z_publisher_free(pub); + + return ret; +} + +OWNED_FUNCTIONS_PTR(_z_publisher_t, publisher, _z_owner_noop_copy, _z_publisher_drop) + +void z_put_options_default(z_put_options_t *options) { + options->congestion_control = Z_CONGESTION_CONTROL_DEFAULT; + options->priority = Z_PRIORITY_DEFAULT; + options->encoding = NULL; #if Z_FEATURE_ATTACHMENT == 1 - .attachment = z_attachment_null() + options->attachment = z_attachment_null(); #endif - }; } -z_delete_options_t z_delete_options_default(void) { - return (z_delete_options_t){.congestion_control = Z_CONGESTION_CONTROL_DEFAULT, .priority = Z_PRIORITY_DEFAULT}; +void z_delete_options_default(z_delete_options_t *options) { + options->congestion_control = Z_CONGESTION_CONTROL_DEFAULT; + options->priority = Z_PRIORITY_DEFAULT; } -int8_t z_put(z_session_t zs, z_keyexpr_t keyexpr, const uint8_t *payload, z_zint_t payload_len, - const z_put_options_t *options) { +int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, const uint8_t *payload, + z_zint_t payload_len, const z_put_options_t *options) { int8_t ret = 0; - z_put_options_t opt = z_put_options_default(); + z_put_options_t opt; + z_put_options_default(&opt); if (options != NULL) { opt.congestion_control = options->congestion_control; opt.encoding = options->encoding; @@ -705,8 +667,8 @@ int8_t z_put(z_session_t zs, z_keyexpr_t keyexpr, const uint8_t *payload, z_zint opt.attachment = options->attachment; #endif } - ret = _z_write(&zs._val.in->val, keyexpr, (const uint8_t *)payload, payload_len, opt.encoding, Z_SAMPLE_KIND_PUT, - opt.congestion_control, opt.priority + ret = _z_write(&_Z_RC_IN_VAL(zs), *keyexpr, (const uint8_t *)payload, payload_len, + _z_encoding_from_owned(opt.encoding), Z_SAMPLE_KIND_PUT, opt.congestion_control, opt.priority #if Z_FEATURE_ATTACHMENT == 1 , opt.attachment @@ -719,24 +681,26 @@ int8_t z_put(z_session_t zs, z_keyexpr_t keyexpr, const uint8_t *payload, z_zint #else z_attachment_t att = z_attachment_null(); #endif - _z_trigger_local_subscriptions(&zs._val.in->val, keyexpr, payload, payload_len, + _z_trigger_local_subscriptions(&_Z_RC_IN_VAL(zs), *keyexpr, payload, payload_len, _z_n_qos_make(0, opt.congestion_control == Z_CONGESTION_CONTROL_BLOCK, opt.priority), att ); - + // Clean-up + z_encoding_drop(opt.encoding); return ret; } -int8_t z_delete(z_session_t zs, z_keyexpr_t keyexpr, const z_delete_options_t *options) { +int8_t z_delete(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, const z_delete_options_t *options) { int8_t ret = 0; - z_delete_options_t opt = z_delete_options_default(); + z_delete_options_t opt; + z_delete_options_default(&opt); if (options != NULL) { opt.congestion_control = options->congestion_control; opt.priority = options->priority; } - ret = _z_write(&zs._val.in->val, keyexpr, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, + ret = _z_write(&_Z_RC_IN_VAL(zs), *keyexpr, NULL, 0, _z_encoding_null(), Z_SAMPLE_KIND_DELETE, opt.congestion_control, opt.priority #if Z_FEATURE_ATTACHMENT == 1 , @@ -747,74 +711,70 @@ int8_t z_delete(z_session_t zs, z_keyexpr_t keyexpr, const z_delete_options_t *o return ret; } -z_publisher_options_t z_publisher_options_default(void) { - return (z_publisher_options_t){.congestion_control = Z_CONGESTION_CONTROL_DEFAULT, .priority = Z_PRIORITY_DEFAULT}; +void z_publisher_options_default(z_publisher_options_t *options) { + options->congestion_control = Z_CONGESTION_CONTROL_DEFAULT; + options->priority = Z_PRIORITY_DEFAULT; } -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; +int8_t z_declare_publisher(z_owned_publisher_t *pub, const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, + const z_publisher_options_t *options) { + _z_keyexpr_t key = *keyexpr; + pub->_val = 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 (zs._val.in->val._tp._type == _Z_TRANSPORT_UNICAST_TYPE) { - _z_resource_t *r = _z_get_resource_by_key(&zs._val.in->val, &keyexpr); + if (_Z_RC_IN_VAL(zs)._tp._type == _Z_TRANSPORT_UNICAST_TYPE) { + _z_resource_t *r = _z_get_resource_by_key(&_Z_RC_IN_VAL(zs), keyexpr); if (r == NULL) { - uint16_t id = _z_declare_resource(&zs._val.in->val, keyexpr); + uint16_t id = _z_declare_resource(&_Z_RC_IN_VAL(zs), *keyexpr); key = _z_rid_with_suffix(id, NULL); } } // Set options - z_publisher_options_t opt = z_publisher_options_default(); + z_publisher_options_t opt; + z_publisher_options_default(&opt); if (options != NULL) { opt.congestion_control = options->congestion_control; opt.priority = options->priority; } // Set publisher - _z_publisher_t *pub = _z_declare_publisher(&zs._val, key, opt.congestion_control, opt.priority); - if (pub == NULL) { + _z_publisher_t *int_pub = _z_declare_publisher(zs, key, opt.congestion_control, opt.priority); + if (int_pub == NULL) { if (key._id != Z_RESOURCE_ID_NONE) { - _z_undeclare_resource(&zs._val.in->val, key._id); + _z_undeclare_resource(&_Z_RC_IN_VAL(zs), key._id); } - return (z_owned_publisher_t){._value = NULL}; + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Create write filter - if (_z_write_filter_create(pub) != _Z_RES_OK) { + int8_t res = _z_write_filter_create(int_pub); + if (res != _Z_RES_OK) { if (key._id != Z_RESOURCE_ID_NONE) { - _z_undeclare_resource(&zs._val.in->val, key._id); + _z_undeclare_resource(&_Z_RC_IN_VAL(zs), key._id); } - return (z_owned_publisher_t){._value = NULL}; + return res; } - return (z_owned_publisher_t){._value = pub}; + pub->_val = int_pub; + return _Z_RES_OK; } -int8_t z_undeclare_publisher(z_owned_publisher_t *pub) { - int8_t ret = _Z_RES_OK; - - ret = _z_undeclare_publisher(pub->_value); - _z_publisher_free(&pub->_value); - - return ret; -} +int8_t z_undeclare_publisher(z_owned_publisher_t *pub) { return _z_publisher_drop(&pub->_val); } -z_publisher_put_options_t z_publisher_put_options_default(void) { - return (z_publisher_put_options_t) { - .encoding = z_encoding_default(), +void z_publisher_put_options_default(z_publisher_put_options_t *options) { + options->encoding = NULL; #if Z_FEATURE_ATTACHMENT == 1 - .attachment = z_attachment_null() + options->attachment = z_attachment_null(); #endif - }; } -z_publisher_delete_options_t z_publisher_delete_options_default(void) { - return (z_publisher_delete_options_t){.__dummy = 0}; -} +void z_publisher_delete_options_default(z_publisher_delete_options_t *options) { options->__dummy = 0; } -int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t len, +int8_t z_publisher_put(const z_loaned_publisher_t *pub, const uint8_t *payload, size_t len, const z_publisher_put_options_t *options) { int8_t ret = 0; // Build options - z_publisher_put_options_t opt = z_publisher_put_options_default(); + z_publisher_put_options_t opt; + z_publisher_put_options_default(&opt); if (options != NULL) { opt.encoding = options->encoding; #if Z_FEATURE_ATTACHMENT == 1 @@ -822,31 +782,34 @@ int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t l #endif } // Check if write filter is active before writing - if (!_z_write_filter_active(pub._val)) { + if (!_z_write_filter_active(pub)) { // Write value - ret = _z_write(&pub._val->_zn.in->val, pub._val->_key, payload, len, opt.encoding, Z_SAMPLE_KIND_PUT, - pub._val->_congestion_control, pub._val->_priority + ret = _z_write(&pub->_zn.in->val, pub->_key, payload, len, _z_encoding_from_owned(opt.encoding), + Z_SAMPLE_KIND_PUT, pub->_congestion_control, pub->_priority #if Z_FEATURE_ATTACHMENT == 1 , opt.attachment #endif ); } + #if Z_FEATURE_ATTACHMENT == 1 z_attachment_t att = opt.attachment; #else z_attachment_t att = z_attachment_null(); #endif - // Trigger local subscriptions - _z_trigger_local_subscriptions(&pub._val->_zn.in->val, pub._val->_key, payload, len, _Z_N_QOS_DEFAULT, att); + _z_trigger_local_subscriptions(&pub->_zn.in->val, pub->_key, payload, len, _Z_N_QOS_DEFAULT, att); + + // Clean-up + z_encoding_drop(opt.encoding); return ret; } -int8_t z_publisher_delete(const z_publisher_t pub, const z_publisher_delete_options_t *options) { +int8_t z_publisher_delete(const z_loaned_publisher_t *pub, const z_publisher_delete_options_t *options) { (void)(options); - return _z_write(&pub._val->_zn.in->val, pub._val->_key, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, - pub._val->_congestion_control, pub._val->_priority + return _z_write(&pub->_zn.in->val, pub->_key, NULL, 0, _z_encoding_null(), Z_SAMPLE_KIND_DELETE, + pub->_congestion_control, pub->_priority #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_null() @@ -854,53 +817,43 @@ int8_t z_publisher_delete(const z_publisher_t pub, const z_publisher_delete_opti ); } -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); +z_owned_keyexpr_t z_publisher_keyexpr(z_loaned_publisher_t *publisher) { + z_owned_keyexpr_t ret = {._val = z_malloc(sizeof(_z_keyexpr_t))}; + if (ret._val != NULL && publisher != NULL) { + *ret._val = _z_keyexpr_duplicate(publisher->_key); } return ret; } #endif #if Z_FEATURE_QUERY == 1 -OWNED_FUNCTIONS_PTR_INTERNAL(z_reply_t, z_owned_reply_t, reply, _z_reply_free, _z_owner_noop_copy) +OWNED_FUNCTIONS_RC(reply) -z_get_options_t z_get_options_default(void) { - return (z_get_options_t) { - .target = z_query_target_default(), .consolidation = z_query_consolidation_default(), - .value = {.encoding = z_encoding_default(), .payload = _z_bytes_empty()}, +void z_get_options_default(z_get_options_t *options) { + options->target = z_query_target_default(); + options->consolidation = z_query_consolidation_default(); + options->encoding = NULL; + options->payload = NULL; #if Z_FEATURE_ATTACHMENT == 1 - .attachment = z_attachment_null(), + options->attachment = z_attachment_null(); #endif - .timeout_ms = Z_GET_TIMEOUT_DEFAULT - }; + options->timeout_ms = Z_GET_TIMEOUT_DEFAULT; } -typedef struct __z_reply_handler_wrapper_t { - z_owned_reply_handler_t user_call; - void *ctx; -} __z_reply_handler_wrapper_t; - -void __z_reply_handler(_z_reply_t *reply, __z_reply_handler_wrapper_t *wrapped_ctx) { - 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, - const z_get_options_t *options) { +int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, const char *parameters, + z_owned_closure_reply_t *callback, z_get_options_t *options) { int8_t ret = _Z_RES_OK; void *ctx = callback->context; callback->context = NULL; - z_get_options_t opt = z_get_options_default(); + z_get_options_t opt; + z_get_options_default(&opt); if (options != NULL) { opt.consolidation = options->consolidation; opt.target = options->target; - opt.value = options->value; + opt.encoding = options->encoding; + opt.payload = z_bytes_move(options->payload); #if Z_FEATURE_ATTACHMENT == 1 opt.attachment = options->attachment; #endif @@ -914,178 +867,194 @@ int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owne opt.consolidation.mode = Z_CONSOLIDATION_MODE_LATEST; } } + // Set value + _z_value_t value = {.payload = _z_bytes_from_owned_bytes(opt.payload), + .encoding = _z_encoding_from_owned(opt.encoding)}; - // TODO[API-NET]: When API and NET are a single layer, there is no wrap the user callback and args - // to enclose the z_reply_t into a z_owned_reply_t. - __z_reply_handler_wrapper_t *wrapped_ctx = - (__z_reply_handler_wrapper_t *)z_malloc(sizeof(__z_reply_handler_wrapper_t)); - if (wrapped_ctx != NULL) { - wrapped_ctx->user_call = callback->call; - wrapped_ctx->ctx = ctx; - } - - ret = _z_query(&zs._val.in->val, keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, - __z_reply_handler, wrapped_ctx, callback->drop, ctx, opt.timeout_ms + ret = _z_query(&_Z_RC_IN_VAL(zs), *keyexpr, parameters, opt.target, opt.consolidation.mode, value, callback->call, + callback->drop, ctx, opt.timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , opt.attachment #endif ); + if (opt.payload != NULL) { + z_bytes_drop(opt.payload); + } + // Clean-up + z_encoding_drop(opt.encoding); return ret; } -_Bool z_reply_is_ok(const z_owned_reply_t *reply) { - (void)(reply); +_Bool z_reply_is_ok(const z_loaned_reply_t *reply) { + _ZP_UNUSED(reply); // For the moment always return TRUE. // The support for reply errors will come in the next release. return true; } -z_loaned_sample_t z_reply_ok(const z_owned_reply_t *reply) { return reply->_value->data.sample; } +const z_loaned_sample_t *z_reply_ok(const z_loaned_reply_t *reply) { return &reply->in->val.data.sample; } -z_value_t z_reply_err(const z_owned_reply_t *reply) { - (void)(reply); - return (z_value_t){.payload = _z_bytes_empty(), .encoding = z_encoding_default()}; +const z_loaned_value_t *z_reply_err(const z_loaned_reply_t *reply) { + _ZP_UNUSED(reply); + return NULL; } #endif #if Z_FEATURE_QUERYABLE == 1 -OWNED_FUNCTIONS_PTR_RC(z_query_t, z_owned_query_t, query) -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); } +int8_t _z_queryable_drop(_z_queryable_t **queryable) { + int8_t ret = _Z_RES_OK; -z_queryable_options_t z_queryable_options_default(void) { - return (z_queryable_options_t){.complete = _Z_QUERYABLE_COMPLETE_DEFAULT}; + ret = _z_undeclare_queryable(*queryable); + _z_queryable_free(queryable); + return ret; } -z_owned_queryable_t z_declare_queryable(z_session_t zs, z_keyexpr_t keyexpr, z_owned_closure_query_t *callback, - const z_queryable_options_t *options) { +OWNED_FUNCTIONS_RC(query) +OWNED_FUNCTIONS_PTR(_z_queryable_t, queryable, _z_owner_noop_copy, _z_queryable_drop) + +void z_queryable_options_default(z_queryable_options_t *options) { options->complete = _Z_QUERYABLE_COMPLETE_DEFAULT; } + +int8_t z_declare_queryable(z_owned_queryable_t *queryable, const z_loaned_session_t *zs, + const z_loaned_keyexpr_t *keyexpr, z_owned_closure_query_t *callback, + const z_queryable_options_t *options) { void *ctx = callback->context; callback->context = NULL; - z_keyexpr_t key = keyexpr; + _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 (zs._val.in->val._tp._type == _Z_TRANSPORT_UNICAST_TYPE) { - _z_resource_t *r = _z_get_resource_by_key(&zs._val.in->val, &keyexpr); + if (_Z_RC_IN_VAL(zs)._tp._type == _Z_TRANSPORT_UNICAST_TYPE) { + _z_resource_t *r = _z_get_resource_by_key(&_Z_RC_IN_VAL(zs), keyexpr); if (r == NULL) { - uint16_t id = _z_declare_resource(&zs._val.in->val, keyexpr); + uint16_t id = _z_declare_resource(&_Z_RC_IN_VAL(zs), *keyexpr); key = _z_rid_with_suffix(id, NULL); } } - z_queryable_options_t opt = z_queryable_options_default(); + z_queryable_options_t opt; + z_queryable_options_default(&opt); if (options != NULL) { opt.complete = options->complete; } - return (z_owned_queryable_t){ - ._value = _z_declare_queryable(&zs._val, key, opt.complete, callback->call, callback->drop, ctx)}; -} - -int8_t z_undeclare_queryable(z_owned_queryable_t *queryable) { - int8_t ret = _Z_RES_OK; + queryable->_val = _z_declare_queryable(zs, key, opt.complete, callback->call, callback->drop, ctx); - ret = _z_undeclare_queryable(queryable->_value); - _z_queryable_free(&queryable->_value); - return ret; + return _Z_RES_OK; } -z_query_reply_options_t z_query_reply_options_default(void) { - return (z_query_reply_options_t){.encoding = z_encoding_default()}; -} +int8_t z_undeclare_queryable(z_owned_queryable_t *queryable) { return _z_queryable_drop(&queryable->_val); } + +void z_query_reply_options_default(z_query_reply_options_t *options) { options->encoding = NULL; } -int8_t z_query_reply(const z_query_t *query, const z_keyexpr_t keyexpr, const uint8_t *payload, size_t payload_len, +int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, z_owned_bytes_t *payload, const z_query_reply_options_t *options) { - z_query_reply_options_t opts = options == NULL ? z_query_reply_options_default() : *options; - _z_value_t value = {.payload = - { - .start = payload, - ._is_alloc = false, - .len = payload_len, - }, - .encoding = {.id = opts.encoding.id, .schema = opts.encoding.schema}}; - return _z_send_reply(&query->_val._rc.in->val, keyexpr, value, Z_SAMPLE_KIND_PUT, opts.attachment); - return _Z_ERR_GENERIC; + z_query_reply_options_t opts; + if (options == NULL) { + z_query_reply_options_default(&opts); + } else { + opts = *options; + } + // Set value + _z_value_t value = {.payload = _z_bytes_from_owned_bytes(payload), + .encoding = _z_encoding_from_owned(opts.encoding)}; + + int8_t ret = _z_send_reply(&query->in->val, *keyexpr, value, Z_SAMPLE_KIND_PUT, opts.attachment); + if (payload != NULL) { + z_bytes_drop(payload); + } + // Clean-up + z_encoding_drop(opts.encoding); + return ret; } #endif -z_owned_keyexpr_t z_keyexpr_new(const char *name) { - z_owned_keyexpr_t key; +int8_t z_keyexpr_new(z_owned_keyexpr_t *key, const char *name) { + int8_t ret = _Z_RES_OK; - 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); + key->_val = name ? (_z_keyexpr_t *)z_malloc(sizeof(_z_keyexpr_t)) : NULL; + if (key->_val != NULL) { + *key->_val = _z_rid_with_suffix(Z_RESOURCE_ID_NONE, name); + } else { + ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - return key; + return ret; } -z_owned_keyexpr_t z_declare_keyexpr(z_session_t zs, z_keyexpr_t keyexpr) { - z_owned_keyexpr_t key; +int8_t z_declare_keyexpr(z_owned_keyexpr_t *key, const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr) { + int8_t ret = _Z_RES_OK; - key._value = (z_keyexpr_t *)z_malloc(sizeof(z_keyexpr_t)); - if (key._value != NULL) { - uint16_t id = _z_declare_resource(&zs._val.in->val, keyexpr); - *key._value = _z_rid_with_suffix(id, NULL); + key->_val = (_z_keyexpr_t *)z_malloc(sizeof(_z_keyexpr_t)); + if (key->_val != NULL) { + uint16_t id = _z_declare_resource(&_Z_RC_IN_VAL(zs), *keyexpr); + *key->_val = _z_rid_with_suffix(id, NULL); + } else { + ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - return key; + return ret; } -int8_t z_undeclare_keyexpr(z_session_t zs, z_owned_keyexpr_t *keyexpr) { +int8_t z_undeclare_keyexpr(const z_loaned_session_t *zs, z_owned_keyexpr_t *keyexpr) { int8_t ret = _Z_RES_OK; - ret = _z_undeclare_resource(&zs._val.in->val, keyexpr->_value->_id); + ret = _z_undeclare_resource(&_Z_RC_IN_VAL(zs), keyexpr->_val->_id); z_keyexpr_drop(keyexpr); return ret; } #if Z_FEATURE_SUBSCRIPTION == 1 -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); } +int8_t _z_subscriber_drop(_z_subscriber_t **sub) { + int8_t ret = _Z_RES_OK; + + ret = _z_undeclare_subscriber(*sub); + _z_subscriber_free(sub); -z_subscriber_options_t z_subscriber_options_default(void) { - return (z_subscriber_options_t){.reliability = Z_RELIABILITY_DEFAULT}; + return ret; } -z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z_owned_closure_sample_t *callback, - const z_subscriber_options_t *options) { +OWNED_FUNCTIONS_PTR(_z_subscriber_t, subscriber, _z_owner_noop_copy, _z_subscriber_drop) + +void z_subscriber_options_default(z_subscriber_options_t *options) { options->reliability = Z_RELIABILITY_DEFAULT; } + +int8_t z_declare_subscriber(z_owned_subscriber_t *sub, const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, + z_owned_closure_sample_t *callback, const z_subscriber_options_t *options) { void *ctx = callback->context; callback->context = NULL; char *suffix = NULL; - z_keyexpr_t key = keyexpr; + _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 (zs._val.in->val._tp._type == _Z_TRANSPORT_UNICAST_TYPE) { - _z_resource_t *r = _z_get_resource_by_key(&zs._val.in->val, &keyexpr); + if (_Z_RC_IN_VAL(zs)._tp._type == _Z_TRANSPORT_UNICAST_TYPE) { + _z_resource_t *r = _z_get_resource_by_key(&_Z_RC_IN_VAL(zs), keyexpr); if (r == NULL) { - char *wild = strpbrk(keyexpr._suffix, "*$"); + char *wild = strpbrk(keyexpr->_suffix, "*$"); _Bool do_keydecl = true; - if (wild != NULL && wild != keyexpr._suffix) { + _z_keyexpr_t resource_key = *keyexpr; + if (wild != NULL && wild != resource_key._suffix) { wild -= 1; - size_t len = wild - keyexpr._suffix; + size_t len = wild - resource_key._suffix; suffix = z_malloc(len + 1); if (suffix != NULL) { - memcpy(suffix, keyexpr._suffix, len); + memcpy(suffix, resource_key._suffix, len); suffix[len] = 0; - keyexpr._suffix = suffix; - _z_keyexpr_set_owns_suffix(&keyexpr, false); + resource_key._suffix = suffix; + _z_keyexpr_set_owns_suffix(&resource_key, false); } else { do_keydecl = false; } } if (do_keydecl) { - uint16_t id = _z_declare_resource(&zs._val.in->val, keyexpr); + uint16_t id = _z_declare_resource(&_Z_RC_IN_VAL(zs), resource_key); key = _z_rid_with_suffix(id, wild); } + _z_keyexpr_clear(&resource_key); } } @@ -1093,134 +1062,130 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z if (options != NULL) { subinfo.reliability = options->reliability; } - _z_subscriber_t *sub = _z_declare_subscriber(&zs._val, key, subinfo, callback->call, callback->drop, ctx); + _z_subscriber_t *int_sub = _z_declare_subscriber(zs, key, subinfo, callback->call, callback->drop, ctx); if (suffix != NULL) { z_free(suffix); } - return (z_owned_subscriber_t){._value = sub}; -} - -int8_t z_undeclare_subscriber(z_owned_subscriber_t *sub) { - int8_t ret = _Z_RES_OK; + sub->_val = int_sub; - _z_undeclare_subscriber(sub->_value); - _z_subscriber_free(&sub->_value); - - return ret; + if (int_sub == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } else { + return _Z_RES_OK; + } } -z_owned_keyexpr_t z_subscriber_keyexpr(z_subscriber_t sub) { - z_owned_keyexpr_t ret = z_keyexpr_null(); - uint32_t lookup = sub._val->_entity_id; - if (sub._val != NULL) { - _z_subscription_rc_list_t *tail = sub._val->_zn.in->val._local_subscriptions; - while (tail != NULL && !z_keyexpr_check(&ret)) { - _z_subscription_rc_t *head = _z_subscription_rc_list_head(tail); - if (head->in->val._id == lookup) { - _z_keyexpr_t key = _z_keyexpr_duplicate(head->in->val._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); - } +int8_t z_undeclare_subscriber(z_owned_subscriber_t *sub) { return _z_subscriber_drop(&sub->_val); } + +int8_t z_subscriber_keyexpr(z_owned_keyexpr_t *keyexpr, z_loaned_subscriber_t *sub) { + // Init keyexpr + z_keyexpr_null(keyexpr); + if ((keyexpr == NULL) || (sub == NULL)) { + return _Z_ERR_GENERIC; + } + uint32_t lookup = sub->_entity_id; + _z_subscription_rc_list_t *tail = sub->_zn.in->val._local_subscriptions; + while (tail != NULL && !z_keyexpr_check(keyexpr)) { + _z_subscription_rc_t *head = _z_subscription_rc_list_head(tail); + if (head->in->val._id == lookup) { + // Allocate keyexpr + keyexpr->_val = (_z_keyexpr_t *)z_malloc(sizeof(_z_keyexpr_t)); + if (keyexpr->_val == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } - tail = _z_subscription_rc_list_tail(tail); + _z_keyexpr_copy(keyexpr->_val, &head->in->val._key); } + tail = _z_subscription_rc_list_tail(tail); } - return ret; + return _Z_RES_OK; } #endif /**************** Tasks ****************/ -zp_task_read_options_t zp_task_read_options_default(void) { - return (zp_task_read_options_t) { +void zp_task_read_options_default(zp_task_read_options_t *options) { #if Z_FEATURE_MULTI_THREAD == 1 - .task_attributes = NULL + options->task_attributes = NULL; #else - .__dummy = 0 + options->__dummy = 0; #endif - }; } -int8_t zp_start_read_task(z_session_t zs, const zp_task_read_options_t *options) { +int8_t zp_start_read_task(z_loaned_session_t *zs, const zp_task_read_options_t *options) { (void)(options); #if Z_FEATURE_MULTI_THREAD == 1 - zp_task_read_options_t opt = zp_task_read_options_default(); + zp_task_read_options_t opt; + zp_task_read_options_default(&opt); if (options != NULL) { opt.task_attributes = options->task_attributes; } - return _zp_start_read_task(&zs._val.in->val, opt.task_attributes); + return _zp_start_read_task(&_Z_RC_IN_VAL(zs), opt.task_attributes); #else (void)(zs); return -1; #endif } -int8_t zp_stop_read_task(z_session_t zs) { +int8_t zp_stop_read_task(z_loaned_session_t *zs) { #if Z_FEATURE_MULTI_THREAD == 1 - return _zp_stop_read_task(&zs._val.in->val); + return _zp_stop_read_task(&_Z_RC_IN_VAL(zs)); #else (void)(zs); return -1; #endif } -zp_task_lease_options_t zp_task_lease_options_default(void) { - return (zp_task_lease_options_t) { +void zp_task_lease_options_default(zp_task_lease_options_t *options) { #if Z_FEATURE_MULTI_THREAD == 1 - .task_attributes = NULL + options->task_attributes = NULL; #else - .__dummy = 0 + options->__dummy = 0; #endif - }; } -int8_t zp_start_lease_task(z_session_t zs, const zp_task_lease_options_t *options) { +int8_t zp_start_lease_task(z_loaned_session_t *zs, const zp_task_lease_options_t *options) { (void)(options); #if Z_FEATURE_MULTI_THREAD == 1 - zp_task_lease_options_t opt = zp_task_lease_options_default(); + zp_task_lease_options_t opt; + zp_task_lease_options_default(&opt); if (options != NULL) { opt.task_attributes = options->task_attributes; } - return _zp_start_lease_task(&zs._val.in->val, opt.task_attributes); + return _zp_start_lease_task(&_Z_RC_IN_VAL(zs), opt.task_attributes); #else (void)(zs); return -1; #endif } -int8_t zp_stop_lease_task(z_session_t zs) { +int8_t zp_stop_lease_task(z_loaned_session_t *zs) { #if Z_FEATURE_MULTI_THREAD == 1 - return _zp_stop_lease_task(&zs._val.in->val); + return _zp_stop_lease_task(&_Z_RC_IN_VAL(zs)); #else (void)(zs); return -1; #endif } -zp_read_options_t zp_read_options_default(void) { return (zp_read_options_t){.__dummy = 0}; } +void zp_read_options_default(zp_read_options_t *options) { options->__dummy = 0; } -int8_t zp_read(z_session_t zs, const zp_read_options_t *options) { +int8_t zp_read(const z_loaned_session_t *zs, const zp_read_options_t *options) { (void)(options); - return _zp_read(&zs._val.in->val); + return _zp_read(&_Z_RC_IN_VAL(zs)); } -zp_send_keep_alive_options_t zp_send_keep_alive_options_default(void) { - return (zp_send_keep_alive_options_t){.__dummy = 0}; -} +void zp_send_keep_alive_options_default(zp_send_keep_alive_options_t *options) { options->__dummy = 0; } -int8_t zp_send_keep_alive(z_session_t zs, const zp_send_keep_alive_options_t *options) { +int8_t zp_send_keep_alive(const z_loaned_session_t *zs, const zp_send_keep_alive_options_t *options) { (void)(options); - return _zp_send_keep_alive(&zs._val.in->val); + return _zp_send_keep_alive(&_Z_RC_IN_VAL(zs)); } -zp_send_join_options_t zp_send_join_options_default(void) { return (zp_send_join_options_t){.__dummy = 0}; } +void zp_send_join_options_default(zp_send_join_options_t *options) { options->__dummy = 0; } -int8_t zp_send_join(z_session_t zs, const zp_send_join_options_t *options) { +int8_t zp_send_join(const z_loaned_session_t *zs, const zp_send_join_options_t *options) { (void)(options); - return _zp_send_join(&zs._val.in->val); + return _zp_send_join(&_Z_RC_IN_VAL(zs)); } #if Z_FEATURE_ATTACHMENT == 1 void _z_bytes_pair_clear(struct _z_bytes_pair_t *this_) { @@ -1237,11 +1202,11 @@ z_attachment_t z_bytes_map_as_attachment(const z_owned_bytes_map_t *this_) { bool z_bytes_map_check(const z_owned_bytes_map_t *this_) { return this_->_inner != NULL; } void z_bytes_map_drop(z_owned_bytes_map_t *this_) { _z_bytes_pair_list_free(&this_->_inner); } -int8_t _z_bytes_map_insert_by_alias(z_bytes_t key, z_bytes_t value, void *this_) { +int8_t _z_bytes_map_insert_by_alias(z_loaned_bytes_t *key, z_loaned_bytes_t *value, void *this_) { z_bytes_map_insert_by_alias((z_owned_bytes_map_t *)this_, key, value); return 0; } -int8_t _z_bytes_map_insert_by_copy(z_bytes_t key, z_bytes_t value, void *this_) { +int8_t _z_bytes_map_insert_by_copy(z_loaned_bytes_t *key, z_loaned_bytes_t *value, void *this_) { z_bytes_map_insert_by_copy((z_owned_bytes_map_t *)this_, key, value); return 0; } @@ -1261,7 +1226,7 @@ z_owned_bytes_map_t z_bytes_map_from_attachment_aliasing(z_attachment_t this_) { z_attachment_iterate(this_, _z_bytes_map_insert_by_alias, &map); return map; } -z_bytes_t z_bytes_map_get(const z_owned_bytes_map_t *this_, z_bytes_t key) { +z_loaned_bytes_t *z_bytes_map_get(const z_owned_bytes_map_t *this_, z_loaned_bytes_t *key) { _z_bytes_pair_list_t *current = this_->_inner; while (current) { struct _z_bytes_pair_t *head = _z_bytes_pair_list_head(current); @@ -1271,7 +1236,7 @@ z_bytes_t z_bytes_map_get(const z_owned_bytes_map_t *this_, z_bytes_t key) { } return z_bytes_null(); } -void z_bytes_map_insert_by_alias(const z_owned_bytes_map_t *this_, z_bytes_t key, z_bytes_t value) { +void z_bytes_map_insert_by_alias(const z_owned_bytes_map_t *this_, z_loaned_bytes_t *key, z_loaned_bytes_t *value) { _z_bytes_pair_list_t *current = this_->_inner; while (current) { struct _z_bytes_pair_t *head = _z_bytes_pair_list_head(current); @@ -1292,7 +1257,7 @@ void z_bytes_map_insert_by_alias(const z_owned_bytes_map_t *this_, z_bytes_t key ((z_owned_bytes_map_t *)this_)->_inner = _z_bytes_pair_list_push(this_->_inner, insert); } } -void z_bytes_map_insert_by_copy(const z_owned_bytes_map_t *this_, z_bytes_t key, z_bytes_t value) { +void z_bytes_map_insert_by_copy(const z_owned_bytes_map_t *this_, z_loaned_bytes_t *key, z_loaned_bytes_t *value) { _z_bytes_pair_list_t *current = this_->_inner; while (current) { struct _z_bytes_pair_t *head = _z_bytes_pair_list_head(current); @@ -1330,6 +1295,9 @@ int8_t z_bytes_map_iter(const z_owned_bytes_map_t *this_, z_attachment_iter_body } z_owned_bytes_map_t z_bytes_map_new(void) { return (z_owned_bytes_map_t){._inner = _z_bytes_pair_list_new()}; } z_owned_bytes_map_t z_bytes_map_null(void) { return (z_owned_bytes_map_t){._inner = NULL}; } -z_bytes_t z_bytes_from_str(const char *str) { return z_bytes_wrap((const uint8_t *)str, strlen(str)); } -z_bytes_t z_bytes_null(void) { return (z_bytes_t){.len = 0, ._is_alloc = false, .start = NULL}; } + +int8_t z_bytes_from_str(z_owned_bytes_t *bytes const char *str) { + bytes->_val = z_bytes_wrap((const uint8_t *)str, strlen(str)); + return _Z_RES_OK; +} #endif diff --git a/src/api/handlers.c b/src/api/handlers.c index 14b3f6960..c136e97b0 100644 --- a/src/api/handlers.c +++ b/src/api/handlers.c @@ -23,19 +23,15 @@ void _z_owned_sample_move(z_owned_sample_t *dst, z_owned_sample_t *src) { zp_free(src); } -z_owned_sample_t *_z_sample_to_owned_ptr(const z_sample_t *src) { +z_owned_sample_t *_z_sample_to_owned_ptr(const z_loaned_sample_t *src) { z_owned_sample_t *dst = (z_owned_sample_t *)zp_malloc(sizeof(z_owned_sample_t)); if (dst == NULL) { return NULL; } - if (src != NULL) { - dst->_value = (z_sample_t *)zp_malloc(sizeof(z_sample_t)); - if (dst->_value == NULL) { - return NULL; - } - _z_sample_rc_copy(&dst->_value->_rc, &src->_rc); + if (src == NULL) { + dst->_rc.in = NULL; } else { - dst->_value = NULL; + _z_sample_rc_copy(&dst->_rc, src); } return dst; } @@ -47,9 +43,16 @@ void _z_owned_query_move(z_owned_query_t *dst, z_owned_query_t *src) { zp_free(src); } -z_owned_query_t *_z_query_to_owned_ptr(const z_query_t *src) { +z_owned_query_t *_z_query_to_owned_ptr(const z_loaned_query_t *src) { z_owned_query_t *dst = (z_owned_query_t *)zp_malloc(sizeof(z_owned_query_t)); - _z_query_rc_copy(&dst->_rc, &src->_val._rc); + if (dst == NULL) { + return NULL; + } + if (src == NULL) { + dst->_rc.in = NULL; + } else { + _z_query_rc_copy(&dst->_rc, src); + } return dst; } #endif // Z_FEATURE_QUERYABLE @@ -61,16 +64,15 @@ void _z_owned_reply_move(z_owned_reply_t *dst, z_owned_reply_t *src) { zp_free(src); } -z_owned_reply_t *_z_reply_clone(const z_owned_reply_t *src) { +z_owned_reply_t *_z_reply_to_owned_ptr(const z_loaned_reply_t *src) { z_owned_reply_t *dst = (z_owned_reply_t *)zp_malloc(sizeof(z_owned_reply_t)); if (dst == NULL) { return NULL; } - if (src != NULL && src->_value) { - dst->_value = (_z_reply_t *)zp_malloc(sizeof(_z_reply_t)); - _z_reply_copy(dst->_value, src->_value); + if (src == NULL) { + dst->_rc.in = NULL; } else { - dst->_value = NULL; + _z_reply_rc_copy(&dst->_rc, src); } return dst; } diff --git a/src/collections/string.c b/src/collections/string.c index cd55c8546..1f25c985a 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -18,6 +18,11 @@ #include /*-------- string --------*/ +_z_string_t _z_string_null(void) { + _z_string_t s = {.len = 0, .val = NULL}; + return s; +} + _z_string_t _z_string_make(const char *value) { _z_string_t s; s.val = _z_str_clone(value); @@ -25,6 +30,20 @@ _z_string_t _z_string_make(const char *value) { return s; } +_z_string_t _z_string_wrap(char *value) { + _z_string_t s; + s.val = value; + s.len = strlen(value); + return s; +} + +_z_string_t *_z_string_make_as_ptr(const char *value) { + _z_string_t *s = (_z_string_t *)z_malloc(sizeof(_z_string_t)); + s->val = _z_str_clone(value); + s->len = strlen(value); + return s; +} + size_t _z_string_size(const _z_string_t *s) { return s->len; } void _z_string_copy(_z_string_t *dst, const _z_string_t *src) { @@ -69,8 +88,8 @@ void _z_string_free(_z_string_t **str) { } } -_z_string_t _z_string_from_bytes(const _z_bytes_t *bs) { - _z_string_t s; +_z_string_t _z_string_convert_bytes(const _z_bytes_t *bs) { + _z_string_t s = _z_string_null(); size_t len = bs->len * (size_t)2; char *s_val = (char *)z_malloc((len + (size_t)1) * sizeof(char)); @@ -91,6 +110,22 @@ _z_string_t _z_string_from_bytes(const _z_bytes_t *bs) { return s; } +_z_string_t _z_string_from_bytes(const _z_bytes_t *bs) { + _z_string_t s = _z_string_null(); + // Allocate string + s.len = bs->len + (size_t)1; // bytes data + null terminator + char *str_val = (char *)z_malloc(s.len * sizeof(char)); + if (str_val == NULL) { + return s; + } + // Recopy data + s.val = str_val; + memcpy(s.val, bs->start, bs->len); + // Set null terminator + s.val[bs->len] = '\0'; + return s; +} + /*-------- str --------*/ size_t _z_str_size(const char *src) { return strlen(src) + (size_t)1; } @@ -127,59 +162,3 @@ char *_z_str_clone(const char *src) { } _Bool _z_str_eq(const char *left, const char *right) { return strcmp(left, right) == 0; } - -/*-------- str_array --------*/ -void _z_str_array_init(_z_str_array_t *sa, size_t len) { - char **val = (char **)&sa->val; - *val = (char *)z_malloc(len * sizeof(char *)); - if (*val != NULL) { - sa->len = 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); - return sa; -} - -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; } - -_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]); - } - z_free(sa->val); -} - -void _z_str_array_free(_z_str_array_t **sa) { - _z_str_array_t *ptr = *sa; - if (ptr != NULL) { - _z_str_array_clear(ptr); - - z_free(ptr); - *sa = NULL; - } -} - -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]); - } - 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; - - src->val = NULL; - src->len = 0; -} diff --git a/src/collections/vec.c b/src/collections/vec.c index 20afff6ca..8da49dd72 100644 --- a/src/collections/vec.c +++ b/src/collections/vec.c @@ -36,7 +36,7 @@ void _z_vec_copy(_z_vec_t *dst, const _z_vec_t *src, z_element_clone_f d_f) { dst->_val = (void **)z_malloc(sizeof(void *) * src->_capacity); if (dst->_val != NULL) { for (size_t i = 0; i < src->_len; i++) { - _z_vec_append(dst, d_f(src->_val[i])); + dst->_val[i] = d_f(src->_val[i]); } } } diff --git a/src/link/endpoint.c b/src/link/endpoint.c index 035e646a6..b2464ba10 100644 --- a/src/link/endpoint.c +++ b/src/link/endpoint.c @@ -246,21 +246,23 @@ void __z_locator_onto_str(char *dst, size_t dst_len, const _z_locator_t *loc) { } /** - * Converts a :c:type:`_z_locator_t` into its string format. + * Converts a :c:type:`_z_locator_t` into its _z_string format. * * Parameters: - * loc: :c:type:`_z_locator_t` to be converted into its string format. + * loc: :c:type:`_z_locator_t` to be converted into its _z_string format. * * Returns: - * The pointer to the stringified :c:type:`_z_locator_t`. + * The pointer to the z_stringified :c:type:`_z_locator_t`. */ -char *_z_locator_to_str(const _z_locator_t *l) { - size_t len = _z_locator_strlen(l) + (size_t)1; - char *dst = (char *)z_malloc(len); - if (dst != NULL) { - __z_locator_onto_str(dst, len, l); +_z_string_t *_z_locator_to_string(const _z_locator_t *loc) { + _z_string_t *s = (_z_string_t *)z_malloc(sizeof(_z_string_t)); + s->len = _z_locator_strlen(loc) + (size_t)1; + s->val = (char *)z_malloc(s->len); + if (s->val == NULL) { + return NULL; } - return dst; + __z_locator_onto_str(s->val, s->len, loc); + return s; } /*------------------ Endpoint ------------------*/ @@ -417,37 +419,37 @@ int8_t _z_endpoint_from_str(_z_endpoint_t *ep, const char *str) { char *_z_endpoint_to_str(const _z_endpoint_t *endpoint) { char *ret = NULL; - - char *locator = _z_locator_to_str(&endpoint->_locator); - if (locator != NULL) { - size_t len = 1; // Start with space for the null-terminator - len = len + strlen(locator); - - char *config = _z_endpoint_config_to_str(&endpoint->_config, endpoint->_locator._protocol); - if (config != NULL) { - len = len + (size_t)1; // Config separator - len = len + strlen(config); // Config content - } - - // Reconstruct the endpoint as a string - ret = (char *)z_malloc(len); - if (ret != NULL) { - ret[0] = '\0'; - len = len - (size_t)1; - - if (len > (size_t)0) { - (void)strncat(ret, locator, len); - len = len - strlen(locator); - } - - if (config != NULL) { - if (len > (size_t)0) { - (void)strncat(ret, config, len); - len = len - strlen(config); - } - } + // Retrieve locator + _z_string_t *locator = _z_locator_to_string(&endpoint->_locator); + if (locator == NULL) { + return NULL; + } + size_t curr_len = locator->len; + // Retrieve config + char *config = _z_endpoint_config_to_str(&endpoint->_config, endpoint->_locator._protocol); + if (config != NULL) { + curr_len += strlen(config) + (size_t)1; // Content + separator; + } + // Reconstruct the endpoint as a string + ret = (char *)z_malloc(curr_len); + if (ret == NULL) { + return NULL; + } + ret[0] = '\0'; + curr_len -= (size_t)1; + // Copy locator + if (curr_len > (size_t)0) { + (void)strncat(ret, locator->val, curr_len); + curr_len -= locator->len; + } + // Copy config + if (config != NULL) { + if (curr_len > (size_t)0) { + (void)strncat(ret, config, curr_len); + curr_len -= strlen(config); } } - + // Clean up + _z_string_clear(locator); return ret; } diff --git a/src/net/config.c b/src/net/config.c index 7b0457836..d8ab54459 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_CONFIG_MODE_CLIENT); if (locator != NULL) { // Connect only to the provided locator - _zp_config_insert(ps, Z_CONFIG_CONNECT_KEY, _z_string_make(locator)); + _zp_config_insert(ps, Z_CONFIG_CONNECT_KEY, 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_CONFIG_MULTICAST_SCOUTING_DEFAULT); + _zp_config_insert(ps, Z_CONFIG_MULTICAST_LOCATOR_KEY, Z_CONFIG_MULTICAST_LOCATOR_DEFAULT); + _zp_config_insert(ps, Z_CONFIG_SCOUTING_TIMEOUT_KEY, Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT); } } return ps; diff --git a/src/net/encoding.c b/src/net/encoding.c new file mode 100644 index 000000000..c41e15bd6 --- /dev/null +++ b/src/net/encoding.c @@ -0,0 +1,59 @@ +// +// Copyright (c) 2024 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, + +#include "zenoh-pico/net/encoding.h" + +#include + +#include "zenoh-pico/api/constants.h" +#include "zenoh-pico/utils/logging.h" +#include "zenoh-pico/utils/result.h" + +int8_t _z_encoding_make(_z_encoding_t *encoding, z_encoding_id_t id, const char *schema) { + encoding->id = id; + // Clone schema + if (schema != NULL) { + encoding->schema = _z_bytes_make(strlen(schema) + 1); + if (encoding->schema.start == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + strcpy((char *)encoding->schema.start, schema); + } else { + encoding->schema = _z_bytes_empty(); + } + return _Z_RES_OK; +} + +_z_encoding_t _z_encoding_wrap(z_encoding_id_t id, const char *schema) { + return (_z_encoding_t){ + .id = id, .schema = _z_bytes_wrap((const uint8_t *)schema, (schema == NULL) ? (size_t)0 : strlen(schema))}; +} + +_z_encoding_t _z_encoding_null(void) { return _z_encoding_wrap(Z_ENCODING_ID_DEFAULT, NULL); } + +void _z_encoding_clear(_z_encoding_t *encoding) { _z_bytes_clear(&encoding->schema); }; + +_Bool _z_encoding_check(const _z_encoding_t *encoding) { + return ((encoding->id != Z_ENCODING_ID_DEFAULT) || _z_bytes_check(encoding->schema)); +} + +void _z_encoding_copy(_z_encoding_t *dst, const _z_encoding_t *src) { + dst->id = src->id; + _z_bytes_copy(&dst->schema, &src->schema); +} + +void _z_encoding_move(_z_encoding_t *dst, _z_encoding_t *src) { + dst->id = src->id; + src->id = Z_ENCODING_ID_DEFAULT; + _z_bytes_move(&dst->schema, &src->schema); +} diff --git a/src/net/memory.c b/src/net/memory.c index dcf095b27..1d60c338f 100644 --- a/src/net/memory.c +++ b/src/net/memory.c @@ -17,8 +17,8 @@ #include "zenoh-pico/protocol/core.h" void _z_hello_clear(_z_hello_t *hello) { - if (hello->locators.len > 0) { - _z_str_array_clear(&hello->locators); + if (!_z_string_vec_is_empty(&hello->locators)) { + _z_string_vec_clear(&hello->locators); } } @@ -34,7 +34,7 @@ void _z_hello_free(_z_hello_t **hello) { } void _z_value_clear(_z_value_t *value) { - _z_bytes_clear(&value->encoding.schema); + _z_encoding_clear(&value->encoding); _z_bytes_clear(&value->payload); } diff --git a/src/net/primitives.c b/src/net/primitives.c index c4cc5d34d..43bc39ab9 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -50,6 +50,7 @@ void _z_scout(const z_what_t what, const _z_id_t zid, const char *locator, const if (dropper != NULL) { (*dropper)(arg_drop); } + _z_hello_list_free(&hellos); } /*------------------ Resource Declaration ------------------*/ @@ -100,7 +101,7 @@ int8_t _z_undeclare_resource(_z_session_t *zn, uint16_t rid) { #if Z_FEATURE_PUBLICATION == 1 /*------------------ Publisher Declaration ------------------*/ -_z_publisher_t *_z_declare_publisher(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, +_z_publisher_t *_z_declare_publisher(const _z_session_rc_t *zn, _z_keyexpr_t keyexpr, z_congestion_control_t congestion_control, z_priority_t priority) { // Allocate publisher _z_publisher_t *ret = (_z_publisher_t *)z_malloc(sizeof(_z_publisher_t)); @@ -188,7 +189,7 @@ int8_t _z_write(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *pay #if Z_FEATURE_SUBSCRIPTION == 1 /*------------------ Subscriber Declaration ------------------*/ -_z_subscriber_t *_z_declare_subscriber(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, _z_subinfo_t sub_info, +_z_subscriber_t *_z_declare_subscriber(const _z_session_rc_t *zn, _z_keyexpr_t keyexpr, _z_subinfo_t sub_info, _z_data_handler_t callback, _z_drop_handler_t dropper, void *arg) { _z_subscription_t s; s._id = _z_get_entity_id(&zn->in->val); @@ -258,7 +259,7 @@ int8_t _z_undeclare_subscriber(_z_subscriber_t *sub) { #if Z_FEATURE_QUERYABLE == 1 /*------------------ Queryable Declaration ------------------*/ -_z_queryable_t *_z_declare_queryable(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, _Bool complete, +_z_queryable_t *_z_declare_queryable(const _z_session_rc_t *zn, _z_keyexpr_t keyexpr, _Bool complete, _z_queryable_handler_t callback, _z_drop_handler_t dropper, void *arg) { _z_session_queryable_t q; q._id = _z_get_entity_id(&zn->in->val); @@ -383,7 +384,7 @@ int8_t _z_send_reply(const _z_query_t *query, _z_keyexpr_t keyexpr, const _z_val /*------------------ Query ------------------*/ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, const z_query_target_t target, const z_consolidation_mode_t consolidation, _z_value_t value, _z_reply_handler_t callback, - void *arg_call, _z_drop_handler_t dropper, void *arg_drop, uint32_t timeout_ms + _z_drop_handler_t dropper, void *arg, uint32_t timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t attachment @@ -403,8 +404,7 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, pq->_callback = callback; pq->_dropper = dropper; pq->_pending_replies = NULL; - pq->_call_arg = arg_call; - pq->_drop_arg = arg_drop; + pq->_arg = arg; ret = _z_register_pending_query(zn, pq); // Add the pending query to the current session if (ret == _Z_RES_OK) { @@ -474,4 +474,4 @@ int8_t _z_remove_interest(_z_session_t *zn, uint32_t interest_id) { _z_unregister_interest(zn, sintr); return _Z_RES_OK; } -#endif \ No newline at end of file +#endif diff --git a/src/net/reply.c b/src/net/reply.c index 809d8cb1c..2ad0b3701 100644 --- a/src/net/reply.c +++ b/src/net/reply.c @@ -13,12 +13,22 @@ #include "zenoh-pico/net/reply.h" +#include "zenoh-pico/net/sample.h" #include "zenoh-pico/session/utils.h" #include "zenoh-pico/utils/logging.h" +_z_reply_t _z_reply_null(void) { + _z_reply_t r = {._tag = Z_REPLY_TAG_DATA, + .data = { + .replier_id = {.id = {0}}, + .sample = {.in = NULL}, + }}; + return r; +} + #if Z_FEATURE_QUERY == 1 void _z_reply_data_clear(_z_reply_data_t *reply_data) { - _z_sample_clear(&reply_data->sample); + _z_sample_rc_drop(&reply_data->sample); reply_data->replier_id = _z_id_empty(); } @@ -34,16 +44,13 @@ void _z_reply_data_free(_z_reply_data_t **reply_data) { } void _z_reply_data_copy(_z_reply_data_t *dst, _z_reply_data_t *src) { - _z_sample_copy(&dst->sample, &src->sample); + _z_sample_rc_copy(&dst->sample, &src->sample); dst->replier_id = src->replier_id; } -_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)); - } +_z_reply_t _z_reply_move(_z_reply_t *src_reply) { + _z_reply_t reply = *src_reply; + *src_reply = _z_reply_null(); return reply; } @@ -76,4 +83,41 @@ void _z_pending_reply_clear(_z_pending_reply_t *pr) { // Free the timestamp _z_timestamp_clear(&pr->_tstamp); } + +_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_bytes_t *payload, + const _z_timestamp_t *timestamp, _z_encoding_t encoding, z_sample_kind_t kind, + z_attachment_t att) { + _z_reply_t reply = _z_reply_null(); + reply._tag = tag; + if (tag == Z_REPLY_TAG_DATA) { + reply.data.replier_id = id; + // Create sample + _z_sample_t sample = _z_sample_null(); + sample.keyexpr = keyexpr; // FIXME: call z_keyexpr_move or copy + sample.encoding = encoding; // FIXME: call z_encoding_move or copy + _z_bytes_copy(&sample.payload, payload); + sample.kind = kind; + sample.timestamp = _z_timestamp_duplicate(timestamp); +#if Z_FEATURE_ATTACHMENT == 1 + sample.attachment = att; // FIXME: call z_attachment_move or copy +#endif + // Create sample rc from value + reply.data.sample = _z_sample_rc_new_from_val(sample); + } + return reply; +} +#else +_z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, const _z_bytes_t *payload, + const _z_timestamp_t *timestamp, _z_encoding_t encoding, z_sample_kind_t kind, + z_attachment_t att) { + _ZP_UNUSED(keyexpr); + _ZP_UNUSED(tag); + _ZP_UNUSED(id); + _ZP_UNUSED(payload); + _ZP_UNUSED(timestamp); + _ZP_UNUSED(encoding); + _ZP_UNUSED(kind); + _ZP_UNUSED(att); + return _z_reply_null(); +} #endif diff --git a/src/net/sample.c b/src/net/sample.c index 781008f64..c27bb8775 100644 --- a/src/net/sample.c +++ b/src/net/sample.c @@ -20,8 +20,7 @@ _z_sample_t _z_sample_null(void) { _z_sample_t s = { .keyexpr = _z_keyexpr_null(), .payload = _z_bytes_empty(), - .encoding = {.id = Z_ENCODING_PREFIX_DEFAULT, - .schema = _z_bytes_wrap(NULL, (size_t)0)}, // FIXME: call _z_encoding_null + .encoding = _z_encoding_null(), .timestamp = _z_timestamp_null(), .kind = 0, .qos = {0}, @@ -42,9 +41,7 @@ void _z_sample_move(_z_sample_t *dst, _z_sample_t *src) { src->keyexpr._suffix = NULL; // FIXME: call the z_keyexpr_move _z_bytes_move(&dst->payload, &src->payload); - - dst->encoding.id = src->encoding.id; // FIXME: call the z_encoding_move - _z_bytes_move(&dst->encoding.schema, &src->encoding.schema); // FIXME: call the z_encoding_move + _z_encoding_move(&dst->encoding, &src->encoding); dst->timestamp.time = src->timestamp.time; // FIXME: call the z_timestamp_move dst->timestamp.id = src->timestamp.id; // FIXME: call the z_timestamp_move @@ -53,7 +50,7 @@ void _z_sample_move(_z_sample_t *dst, _z_sample_t *src) { void _z_sample_clear(_z_sample_t *sample) { _z_keyexpr_clear(&sample->keyexpr); _z_bytes_clear(&sample->payload); - _z_bytes_clear(&sample->encoding.schema); // FIXME: call the z_encoding_clear + _z_encoding_clear(&sample->encoding); _z_timestamp_clear(&sample->timestamp); #if Z_FEATURE_ATTACHMENT == 1 _z_attachment_drop(&sample->attachment); @@ -73,10 +70,7 @@ void _z_sample_copy(_z_sample_t *dst, const _z_sample_t *src) { dst->keyexpr = _z_keyexpr_duplicate(src->keyexpr); dst->payload = _z_bytes_duplicate(&src->payload); dst->timestamp = _z_timestamp_duplicate(&src->timestamp); - - // TODO(sashacmc): should be changed after encoding rework - dst->encoding.id = src->encoding.id; - _z_bytes_copy(&dst->encoding.schema, &src->encoding.schema); + _z_encoding_copy(&dst->encoding, &src->encoding); dst->kind = src->kind; #if Z_FEATURE_ATTACHMENT == 1 @@ -97,7 +91,7 @@ _z_sample_t _z_sample_create(const _z_keyexpr_t *key, const _z_bytes_t *payload, _z_sample_t s = _z_sample_null(); _z_keyexpr_copy(&s.keyexpr, key); _z_bytes_copy(&s.payload, payload); - s.encoding = encoding; // FIXME: call z_encoding_move or copy + _z_encoding_copy(&s.encoding, &encoding); s.kind = kind; s.timestamp = timestamp; s.qos = qos; diff --git a/src/net/session.c b/src/net/session.c index 3b2af1097..509cb294b 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -65,7 +65,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(); + _z_string_vec_t locators = _z_string_vec_make(0); char *connect = _z_config_get(config, Z_CONFIG_CONNECT_KEY); char *listen = _z_config_get(config, Z_CONFIG_LISTEN_KEY); if (connect == NULL && listen == NULL) { // Scout if peer is not configured @@ -91,7 +91,7 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { _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); + _z_string_vec_copy(&locators, &hello->locators); } _z_hello_list_free(&hellos); } else { @@ -99,20 +99,21 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { if (listen != NULL) { if (connect == NULL) { key = Z_CONFIG_LISTEN_KEY; - _zp_config_insert(config, Z_CONFIG_MODE_KEY, _z_string_make(Z_CONFIG_MODE_PEER)); + _zp_config_insert(config, Z_CONFIG_MODE_KEY, Z_CONFIG_MODE_PEER); } else { return _Z_ERR_GENERIC; } } - locators = _z_str_array_make(1); - locators.val[0] = _z_str_clone(_z_config_get(config, key)); + locators = _z_string_vec_make(1); + _z_string_vec_append(&locators, _z_string_make_as_ptr(_z_config_get(config, key))); } ret = _Z_ERR_SCOUT_NO_RESULTS; - for (size_t i = 0; i < locators.len; i++) { + size_t len = _z_string_vec_len(&locators); + for (size_t i = 0; i < len; i++) { ret = _Z_RES_OK; - char *locator = locators.val[i]; + _z_string_t *locator = _z_string_vec_get(&locators, i); // @TODO: check invalid configurations // For example, client mode in multicast links @@ -130,7 +131,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->val, mode); if (ret == _Z_RES_OK) { break; } @@ -138,7 +139,7 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { _Z_ERROR("Trying to configure an invalid mode."); } } - _z_str_array_clear(&locators); + _z_string_vec_clear(&locators); } else { _Z_ERROR("A valid config is missing."); ret = _Z_ERR_GENERIC; @@ -154,7 +155,9 @@ _z_config_t *_z_info(const _z_session_t *zn) { if (ps != NULL) { _z_config_init(ps); _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)); + // TODO(sasahcmc): is it zero terminated??? + // rework it!!! + _zp_config_insert(ps, Z_INFO_PID_KEY, _z_string_convert_bytes(&local_zid).val); switch (zn->_tp._type) { case _Z_TRANSPORT_UNICAST_TYPE: diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 923ec1f27..0afe9bf32 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -259,6 +259,13 @@ int8_t _z_str_encode(_z_wbuf_t *wbf, const char *s) { return _z_wbuf_write_bytes(wbf, (const uint8_t *)s, 0, len); } +int8_t _z_zstr_encode(_z_wbuf_t *wbf, const _z_string_t *s) { + size_t len = s->len - (size_t)1; + _Z_RETURN_IF_ERR(_z_zsize_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->val, 0, len); +} + int8_t _z_str_decode(char **str, _z_zbuf_t *zbf) { int8_t ret = _Z_RES_OK; diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 025c97fd4..8c4c27cc7 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -148,8 +148,8 @@ int8_t _z_locators_encode(_z_wbuf_t *wbf, const _z_locator_array_t *la) { _Z_DEBUG("Encoding _LOCATORS"); _Z_RETURN_IF_ERR(_z_zsize_encode(wbf, la->_len)) for (size_t i = 0; i < la->_len; i++) { - char *s = _z_locator_to_str(&la->_val[i]); - _Z_RETURN_IF_ERR(_z_str_encode(wbf, s)) + _z_string_t *s = _z_locator_to_string(&la->_val[i]); + _Z_RETURN_IF_ERR(_z_zstr_encode(wbf, s)) z_free(s); } @@ -279,8 +279,7 @@ 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->_body._put._encoding.id != Z_ENCODING_PREFIX_EMPTY || - !_z_bytes_is_empty(&pshb->_body._put._encoding.schema); + has_encoding = _z_encoding_check(&pshb->_body._put._encoding); if (has_encoding) { header |= _Z_FLAG_Z_P_E; } @@ -410,7 +409,7 @@ int8_t _z_query_encode(_z_wbuf_t *wbf, const _z_msg_query_t *msg) { int8_t ret = _Z_RES_OK; uint8_t header = _Z_MID_Z_QUERY; - _Bool has_params = z_bytes_check(&msg->_parameters); + _Bool has_params = _z_bytes_check(msg->_parameters); if (has_params) { _Z_SET_FLAG(header, _Z_FLAG_Z_Q_P); } @@ -556,7 +555,7 @@ int8_t _z_err_encode(_z_wbuf_t *wbf, const _z_msg_err_t *err) { uint8_t header = _Z_MID_Z_ERR; // Encode header - _Bool has_encoding = err->encoding.id != Z_ENCODING_PREFIX_EMPTY; + _Bool has_encoding = _z_encoding_check(&err->encoding); if (has_encoding) { _Z_SET_FLAG(header, _Z_FLAG_Z_E_E); } diff --git a/src/protocol/config.c b/src/protocol/config.c index 3c70a27e6..ad4440fc2 100644 --- a/src/protocol/config.c +++ b/src/protocol/config.c @@ -25,11 +25,11 @@ int8_t _z_config_init(_z_config_t *ps) { return 0; } -int8_t _zp_config_insert(_z_config_t *ps, uint8_t key, _z_string_t value) { +int8_t _zp_config_insert(_z_config_t *ps, uint8_t key, const char *value) { int8_t ret = _Z_RES_OK; - char *res = _z_str_intmap_insert(ps, key, value.val); - if (res != value.val) { + char *res = _z_str_intmap_insert(ps, key, _z_str_clone(value)); + if (strcmp(res, value) != 0) { ret = _Z_ERR_CONFIG_FAILED_INSERT; } diff --git a/src/protocol/core.c b/src/protocol/core.c index ada2c0c38..6989f45c9 100644 --- a/src/protocol/core.c +++ b/src/protocol/core.c @@ -66,15 +66,14 @@ _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_null(void) { return (_z_value_t){.payload = _z_bytes_empty(), .encoding = _z_encoding_null()}; } _z_value_t _z_value_steal(_z_value_t *value) { _z_value_t ret = *value; *value = _z_value_null(); return ret; } void _z_value_copy(_z_value_t *dst, const _z_value_t *src) { - dst->encoding.id = src->encoding.id; - _z_bytes_copy(&dst->encoding.schema, &src->encoding.schema); + _z_encoding_copy(&dst->encoding, &src->encoding); _z_bytes_copy(&dst->payload, &src->payload); } diff --git a/src/protocol/definitions/message.c b/src/protocol/definitions/message.c index 40f0dc347..8daa82210 100644 --- a/src/protocol/definitions/message.c +++ b/src/protocol/definitions/message.c @@ -23,7 +23,7 @@ void _z_msg_reply_clear(_z_msg_reply_t *msg) { _z_push_body_clear(&msg->_body); } void _z_msg_put_clear(_z_msg_put_t *msg) { - _z_bytes_clear(&msg->_encoding.schema); + _z_encoding_clear(&msg->_encoding); _z_bytes_clear(&msg->_payload); _z_timestamp_clear(&msg->_commons._timestamp); } @@ -33,8 +33,7 @@ _z_msg_query_reqexts_t _z_msg_query_required_extensions(const _z_msg_query_t *ms z_attachment_t att = _z_encoded_as_attachment(&msg->_ext_attachment); #endif return (_z_msg_query_reqexts_t) { - .body = msg->_ext_value.payload.start != NULL || msg->_ext_value.encoding.id != 0 || - !_z_bytes_is_empty(&msg->_ext_value.encoding.schema), + .body = msg->_ext_value.payload.start != NULL || _z_encoding_check(&msg->_ext_value.encoding), .info = _z_id_check(msg->_ext_info._id) || msg->_ext_info._entity_id != 0 || msg->_ext_info._source_sn != 0, #if Z_FEATURE_ATTACHMENT == 1 .attachment = z_attachment_check(&att) @@ -49,6 +48,6 @@ void _z_msg_query_clear(_z_msg_query_t *msg) { _z_value_clear(&msg->_ext_value); } void _z_msg_err_clear(_z_msg_err_t *err) { - _z_bytes_clear(&err->encoding.schema); + _z_encoding_clear(&err->encoding); _z_bytes_clear(&err->_payload); } diff --git a/src/session/push.c b/src/session/push.c index 9b676668a..1d791cb83 100644 --- a/src/session/push.c +++ b/src/session/push.c @@ -26,7 +26,7 @@ int8_t _z_trigger_push(_z_session_t *zn, _z_n_msg_push_t *push) { // TODO check body to know where to dispatch _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(); + _z_encoding_t encoding = push->_body._is_put ? push->_body._body._put._encoding : _z_encoding_null(); int kind = push->_body._is_put ? Z_SAMPLE_KIND_PUT : Z_SAMPLE_KIND_DELETE; #if Z_FEATURE_SUBSCRIPTION == 1 #if Z_FEATURE_ATTACHMENT == 1 diff --git a/src/session/query.c b/src/session/query.c index a84cf7290..17f909788 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -27,14 +27,10 @@ #if Z_FEATURE_QUERY == 1 void _z_pending_query_clear(_z_pending_query_t *pen_qry) { if (pen_qry->_dropper != NULL) { - pen_qry->_dropper(pen_qry->_drop_arg); + pen_qry->_dropper(pen_qry->_arg); } - - z_free(pen_qry->_call_arg); - _z_keyexpr_clear(&pen_qry->_key); _z_str_clear(pen_qry->_parameters); - _z_pending_reply_list_free(&pen_qry->_pending_replies); } @@ -117,19 +113,15 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_zint_t id, cons ret = _Z_ERR_QUERY_NOT_MATCH; } - // Build the reply - _z_reply_t reply; - reply._tag = Z_REPLY_TAG_DATA; - reply.data.replier_id = zn->_local_zid; - reply.data.sample.keyexpr = expanded_ke; - _z_bytes_copy(&reply.data.sample.payload, &msg->_payload); - reply.data.sample.encoding.id = msg->_encoding.id; - _z_bytes_copy(&reply.data.sample.encoding.schema, &msg->_encoding.schema); - reply.data.sample.kind = Z_SAMPLE_KIND_PUT; - reply.data.sample.timestamp = _z_timestamp_duplicate(&msg->_commons._timestamp); + // Retrieve attachment #if Z_FEATURE_ATTACHMENT == 1 - reply.data.sample.attachment = _z_encoded_as_attachment(&msg->_attachment); + z_attachment_t att = _z_encoded_as_attachment(&msg->_attachment); +#else + z_attachment_t att = z_attachment_null(); #endif + // Build the reply + _z_reply_t reply = _z_reply_create(expanded_ke, Z_REPLY_TAG_DATA, zn->_local_zid, &msg->_payload, + &msg->_commons._timestamp, msg->_encoding, Z_SAMPLE_KIND_PUT, att); // Verify if this is a newer reply, free the old one in case it is if ((ret == _Z_RES_OK) && ((pen_qry->_consolidation == Z_CONSOLIDATION_MODE_LATEST) || @@ -141,7 +133,8 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_zint_t id, cons pen_rep = _z_pending_reply_list_head(pen_rps); // 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 (_z_str_eq(pen_rep->_reply.data.sample.in->val.keyexpr._suffix, + reply.data.sample.in->val.keyexpr._suffix) == true) { if (msg->_commons._timestamp.time <= pen_rep->_tstamp.time) { drop = true; } else { @@ -150,7 +143,6 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_zint_t id, cons } break; } - pen_rps = _z_pending_reply_list_tail(pen_rps); } @@ -163,7 +155,7 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_zint_t id, cons _z_reply_t partial_reply; (void)memset(&partial_reply, 0, sizeof(_z_reply_t)); // Avoid warnings on uninitialized values on the reply - partial_reply.data.sample.keyexpr = _z_keyexpr_duplicate(reply.data.sample.keyexpr); + partial_reply.data.sample.in->val.keyexpr = _z_keyexpr_duplicate(reply.data.sample.in->val.keyexpr); pen_rep->_reply = partial_reply; } else { pen_rep->_reply = reply; // Store the whole reply in the latest mode @@ -180,7 +172,10 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_zint_t id, cons // Trigger the user callback if ((ret == _Z_RES_OK) && (pen_qry->_consolidation != Z_CONSOLIDATION_MODE_LATEST)) { - pen_qry->_callback(_z_reply_alloc_and_move(&reply), pen_qry->_call_arg); + _z_reply_rc_t cb_reply = _z_reply_rc_new(); + cb_reply.in->val = _z_reply_move(&reply); + pen_qry->_callback(&cb_reply, pen_qry->_arg); + _z_reply_rc_drop(&cb_reply); } if (ret != _Z_RES_OK) { @@ -207,8 +202,11 @@ int8_t _z_trigger_query_reply_final(_z_session_t *zn, _z_zint_t id) { _z_pending_reply_t *pen_rep = _z_pending_reply_list_head(pen_qry->_pending_replies); // Trigger the query handler - pen_qry->_callback(_z_reply_alloc_and_move(&pen_rep->_reply), pen_qry->_call_arg); + _z_reply_rc_t cb_reply = _z_reply_rc_new(); + cb_reply.in->val = _z_reply_move(&pen_rep->_reply); + pen_qry->_callback(&cb_reply, pen_qry->_arg); pen_qry->_pending_replies = _z_pending_reply_list_pop(pen_qry->_pending_replies, NULL); + _z_reply_rc_drop(&cb_reply); } } diff --git a/src/session/queryable.c b/src/session/queryable.c index 771dcfa3c..0e5b207ca 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -145,8 +145,8 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *msgq, const _zp_session_unlock_mutex(zn); // Build the z_query - z_query_t query = {._val = {._rc = _z_query_rc_new()}}; - query._val._rc.in->val = _z_query_create(&msgq->_ext_value, &key, &msgq->_parameters, zn, qid, att); + _z_query_rc_t query = _z_query_rc_new(); + query.in->val = _z_query_create(&msgq->_ext_value, &key, &msgq->_parameters, zn, qid, att); // Parse session_queryable list _z_session_queryable_rc_list_t *xs = qles; while (xs != NULL) { @@ -155,7 +155,7 @@ int8_t _z_trigger_queryables(_z_session_t *zn, const _z_msg_query_t *msgq, const xs = _z_session_queryable_rc_list_tail(xs); } // Clean up - _z_query_rc_drop(&query._val._rc); + _z_query_rc_drop(&query); _z_keyexpr_clear(&key); _z_session_queryable_rc_list_free(&qles); } else { diff --git a/src/session/rx.c b/src/session/rx.c index ba303c0f5..f70f33b2f 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -116,7 +116,7 @@ int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint case _Z_REQUEST_DEL: { _z_msg_del_t del = req._body._del; #if Z_FEATURE_SUBSCRIPTION == 1 - ret = _z_trigger_subscriptions(zn, req._key, _z_bytes_empty(), z_encoding_default(), + ret = _z_trigger_subscriptions(zn, req._key, _z_bytes_empty(), _z_encoding_null(), Z_SAMPLE_KIND_DELETE, del._commons._timestamp, req._ext_qos, z_attachment_null()); #endif diff --git a/src/session/scout.c b/src/session/scout.c index b3a8dffb8..956163be8 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -84,15 +84,15 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig 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); + hello->locators = _z_string_vec_make(n_loc); for (size_t i = 0; i < n_loc; i++) { - hello->locators.val[i] = - _z_locator_to_str(&s_msg._body._hello._locators._val[i]); + _z_string_vec_append( + &hello->locators, + _z_locator_to_string(&s_msg._body._hello._locators._val[i])); } } else { // @TODO: construct the locator departing from the sock address - hello->locators.len = 0; - hello->locators.val = NULL; + _z_string_vec_clear(&hello->locators); } ret = _z_hello_list_push(ret, hello); diff --git a/src/session/subscription.c b/src/session/subscription.c index 3d1ec99fe..31e28b0e2 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -140,7 +140,7 @@ _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_loca void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *payload, _z_zint_t payload_len, const _z_n_qos_t qos, const z_attachment_t att) { - _z_encoding_t encoding = {.id = Z_ENCODING_PREFIX_DEFAULT, .schema = _z_bytes_wrap(NULL, 0)}; + _z_encoding_t encoding = _z_encoding_null(); int8_t ret = _z_trigger_subscriptions(zn, keyexpr, _z_bytes_wrap(payload, payload_len), encoding, Z_SAMPLE_KIND_PUT, _z_timestamp_null(), qos, att); (void)ret; @@ -162,8 +162,8 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co _zp_session_unlock_mutex(zn); // Build the sample - z_sample_t sample = {._rc = _z_sample_rc_new()}; - sample._rc.in->val = _z_sample_create(&key, &payload, timestamp, encoding, kind, qos, att); + _z_sample_rc_t sample = _z_sample_rc_new(); + sample.in->val = _z_sample_create(&key, &payload, timestamp, encoding, kind, qos, att); // Parse subscription list _z_subscription_rc_list_t *xs = subs; _Z_DEBUG("Triggering %ju subs", (uintmax_t)_z_subscription_rc_list_len(xs)); @@ -173,7 +173,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co xs = _z_subscription_rc_list_tail(xs); } // Clean up - _z_sample_rc_drop(&sample._rc); + _z_sample_rc_drop(&sample); _z_keyexpr_clear(&key); _z_subscription_rc_list_free(&subs); } else { @@ -209,12 +209,7 @@ void _z_flush_subscriptions(_z_session_t *zn) { #else // Z_FEATURE_SUBSCRIPTION == 0 void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *payload, - _z_zint_t payload_len, _z_n_qos_t qos -#if Z_FEATURE_ATTACHMENT == 1 - , - z_attachment_t att -#endif -) { + _z_zint_t payload_len, _z_n_qos_t qos, z_attachment_t att) { _ZP_UNUSED(zn); _ZP_UNUSED(keyexpr); _ZP_UNUSED(payload); diff --git a/src/transport/multicast.c b/src/transport/multicast.c index d8613dc89..db76bb01a 100644 --- a/src/transport/multicast.c +++ b/src/transport/multicast.c @@ -46,7 +46,9 @@ void _zp_multicast_info_session(const _z_transport_t *zt, _z_config_t *ps) { while (xs != NULL) { _z_transport_peer_entry_t *peer = _z_transport_peer_entry_list_head(xs); _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)); + _z_string_t remote_zid_str = _z_string_convert_bytes(&remote_zid); + _zp_config_insert(ps, Z_INFO_PEER_PID_KEY, remote_zid_str.val); + _z_string_clear(&remote_zid_str); xs = _z_transport_peer_entry_list_tail(xs); } diff --git a/src/transport/raweth/tx.c b/src/transport/raweth/tx.c index a3cf15a8d..faeada46e 100644 --- a/src/transport/raweth/tx.c +++ b/src/transport/raweth/tx.c @@ -241,7 +241,7 @@ int8_t _z_raweth_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg, _ZP_UNUSED(cong_ctrl); #endif // Z_FEATURE_MULTI_THREAD == 1 - const z_keyexpr_t *keyexpr = NULL; + const _z_keyexpr_t *keyexpr = NULL; switch (n_msg->_tag) { case _Z_N_PUSH: keyexpr = &n_msg->_body._push._key; diff --git a/src/transport/unicast.c b/src/transport/unicast.c index 02ddc9e5c..650a99615 100644 --- a/src/transport/unicast.c +++ b/src/transport/unicast.c @@ -39,8 +39,10 @@ void _zp_unicast_fetch_zid(const _z_transport_t *zt, z_owned_closure_zid_t *call void _zp_unicast_info_session(const _z_transport_t *zt, _z_config_t *ps) { _z_id_t remote_zid = zt->_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)); + _z_bytes_t remote_zid_bytes = _z_bytes_wrap(remote_zid.id, _z_id_len(remote_zid)); + _z_string_t remote_zid_str = _z_string_convert_bytes(&remote_zid_bytes); + _zp_config_insert(ps, Z_INFO_ROUTER_PID_KEY, remote_zid_str.val); + _z_string_clear(&remote_zid_str); } #else diff --git a/tests/modularity.py b/tests/modularity.py index 0567ee5c6..c7603c412 100644 --- a/tests/modularity.py +++ b/tests/modularity.py @@ -162,14 +162,14 @@ def query_and_queryable(args): if args.attachment == 1: z_query_expected_output = """Opening session... Sending Query 'demo/example/**'... ->> Received ('demo/example/zenoh-pico-queryable': 'Queryable from Pico!') +>> Received ('demo/example/**': 'Queryable from Pico!') Attachement found >>> hello: world >> Received query final notification""" else: z_query_expected_output = """Opening session... Sending Query 'demo/example/**'... ->> Received ('demo/example/zenoh-pico-queryable': 'Queryable from Pico!') +>> Received ('demo/example/**': 'Queryable from Pico!') >> Received query final notification""" else: z_query_expected_output = """Opening session... @@ -189,7 +189,7 @@ def query_and_queryable(args): z_queryable_expected_output = """Opening session... Creating Queryable on 'demo/example/zenoh-pico-queryable'... Press CTRL-C to quit... ->> [Queryable handler] Received Query 'demo/example/**?' + >> [Queryable handler] Received Query 'demo/example/**' Attachement found >>> hi: there """ @@ -197,7 +197,7 @@ def query_and_queryable(args): z_queryable_expected_output = """Opening session... Creating Queryable on 'demo/example/zenoh-pico-queryable'... Press CTRL-C to quit... ->> [Queryable handler] Received Query 'demo/example/**?' + >> [Queryable handler] Received Query 'demo/example/**' """ else: z_queryable_expected_output = """Opening session... diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 9ac4b220c..1204092f4 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -19,6 +19,7 @@ #undef NDEBUG #include +#define URI "demo/example/**/*" #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) #include #define sleep(x) Sleep(x * 1000) @@ -56,66 +57,74 @@ 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 + z_drop(hello); } volatile unsigned int queries = 0; -void query_handler(const z_query_t *query, void *arg) { +void query_handler(const z_loaned_query_t *query, void *arg) { printf("%s\n", __func__); queries++; - z_owned_str_t k_str = z_keyexpr_to_string(z_query_keyexpr(query)); + const z_loaned_keyexpr_t *query_ke = z_query_keyexpr(query); + z_owned_string_t k_str; + z_keyexpr_to_string(query_ke, &k_str); #ifdef ZENOH_PICO if (z_check(k_str) == false) { - k_str = zp_keyexpr_resolve(*(z_session_t *)arg, z_query_keyexpr(query)); + zp_keyexpr_resolve(*(const z_loaned_session_t **)arg, z_query_keyexpr(query), &k_str); } #endif - z_bytes_t pred = z_query_parameters(query); + z_view_string_t pred; + z_query_parameters(query, &pred); (void)(pred); - z_value_t payload_value = z_query_value(query); + const z_loaned_value_t *payload_value = z_query_value(query); (void)(payload_value); - z_query_reply_options_t _ret_qreply_opt = z_query_reply_options_default(); - z_query_reply(query, z_keyexpr(z_loan(k_str)), (const uint8_t *)value, strlen(value), &_ret_qreply_opt); + z_query_reply_options_t _ret_qreply_opt; + z_query_reply_options_default(&_ret_qreply_opt); + + // Reply value encoding + z_view_string_t reply_str; + z_view_string_wrap(&reply_str, value); + z_owned_bytes_t reply_payload; + z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + + z_query_reply(query, query_ke, z_move(reply_payload), &_ret_qreply_opt); z_drop(z_move(k_str)); } volatile unsigned int replies = 0; -void reply_handler(z_owned_reply_t *reply, void *arg) { +void reply_handler(const z_loaned_reply_t *reply, void *arg) { printf("%s\n", __func__); replies++; if (z_reply_is_ok(reply)) { - z_loaned_sample_t sample = z_reply_ok(reply); + const z_loaned_sample_t *sample = z_reply_ok(reply); - z_owned_str_t k_str = z_keyexpr_to_string(sample.keyexpr); + z_owned_string_t k_str; + z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str); #ifdef ZENOH_PICO if (z_check(k_str) == false) { - k_str = zp_keyexpr_resolve(*(z_session_t *)arg, sample.keyexpr); + zp_keyexpr_resolve(*(const z_loaned_session_t **)arg, z_sample_keyexpr(sample), &k_str); } #endif z_drop(z_move(k_str)); } else { - z_value_t _ret_zvalue = z_reply_err(reply); + const z_loaned_value_t *_ret_zvalue = z_reply_err(reply); (void)(_ret_zvalue); } - - 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; -void data_handler(const z_sample_t *sample, void *arg) { +void data_handler(const z_loaned_sample_t *sample, void *arg) { printf("%s\n", __func__); datas++; - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_owned_str_t k_str = z_keyexpr_to_string(keyexpr); + z_owned_string_t k_str; + z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str); #ifdef ZENOH_PICO if (z_check(k_str) == false) { - k_str = zp_keyexpr_resolve(*(z_session_t *)arg, keyexpr); + zp_keyexpr_resolve(*(const z_loaned_session_t **)arg, z_sample_keyexpr(sample), &k_str); } #endif z_drop(z_move(k_str)); @@ -130,37 +139,37 @@ 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_eq(_ret_bool, true); + z_view_keyexpr_t key_demo_example, key_demo_example_a, key_demo_example_starstar; + z_view_keyexpr_from_string(&key_demo_example, "demo/example"); + z_view_keyexpr_from_string(&key_demo_example_a, "demo/example/a"); + z_view_keyexpr_from_string(&key_demo_example_starstar, "demo/example/**"); - int8_t _ret_int8 = z_keyexpr_includes(z_keyexpr("demo/example/**"), z_keyexpr("demo/example/a")); - assert_eq(_ret_int8, 0); + _Bool _ret_bool = z_keyexpr_includes(z_loan(key_demo_example_starstar), z_loan(key_demo_example_a)); + assert(_ret_bool); #ifdef ZENOH_PICO - _ret_int8 = zp_keyexpr_includes_null_terminated("demo/example/**", "demo/example/a"); - assert_eq(_ret_int8, 0); + _ret_bool = zp_keyexpr_includes_null_terminated("demo/example/**", "demo/example/a"); + assert(_ret_bool); #endif - _ret_int8 = z_keyexpr_intersects(z_keyexpr("demo/example/**"), z_keyexpr("demo/example/a")); - assert_eq(_ret_int8, 0); + _ret_bool = z_keyexpr_intersects(z_loan(key_demo_example_starstar), z_loan(key_demo_example_a)); + assert(_ret_bool); #ifdef ZENOH_PICO - _ret_int8 = zp_keyexpr_intersect_null_terminated("demo/example/**", "demo/example/a"); - assert_eq(_ret_int8, 0); + _ret_bool = zp_keyexpr_intersect_null_terminated("demo/example/**", "demo/example/a"); + assert(_ret_bool); #endif - _ret_int8 = z_keyexpr_equals(z_keyexpr("demo/example/**"), z_keyexpr("demo/example")); - assert_eq(_ret_int8, -1); + _ret_bool = z_keyexpr_equals(z_loan(key_demo_example_starstar), z_loan(key_demo_example)); + assert(!_ret_bool); #ifdef ZENOH_PICO - _ret_int8 = zp_keyexpr_equals_null_terminated("demo/example/**", "demo/example"); - assert_eq(_ret_int8, -1); + _ret_bool = zp_keyexpr_equals_null_terminated("demo/example/**", "demo/example"); + assert(!_ret_bool); #endif sleep(SLEEP); - size_t keyexpr_len = strlen("demo/example/**/*"); - char *keyexpr_str = (char *)malloc(keyexpr_len + 1); - memcpy(keyexpr_str, "demo/example/**/*", keyexpr_len); + size_t keyexpr_len = strlen(URI); + char *keyexpr_str = (char *)z_malloc(keyexpr_len + 1); + memcpy(keyexpr_str, URI, keyexpr_len); keyexpr_str[keyexpr_len] = '\0'; - _ret_int8 = z_keyexpr_is_canon(keyexpr_str, keyexpr_len); + int8_t _ret_int8 = z_keyexpr_is_canon(keyexpr_str, keyexpr_len); assert(_ret_int8 < 0); #ifdef ZENOH_PICO @@ -169,35 +178,36 @@ int main(int argc, char **argv) { #endif _ret_int8 = z_keyexpr_canonize(keyexpr_str, &keyexpr_len); assert_eq(_ret_int8, 0); - assert_eq(strlen("demo/example/**/*"), keyexpr_len); + assert_eq(strlen(URI), keyexpr_len); #ifdef ZENOH_PICO _ret_int8 = zp_keyexpr_canonize_null_terminated(keyexpr_str); assert_eq(_ret_int8, 0); - assert_eq(strlen("demo/example/**/*"), keyexpr_len); + assert_eq(strlen(URI), keyexpr_len); #endif printf("Ok\n"); sleep(SLEEP); printf("Testing Configs..."); - z_owned_config_t _ret_config = z_config_new(); + z_owned_config_t _ret_config; + z_config_new(&_ret_config); assert(z_check(_ret_config)); z_drop(z_move(_ret_config)); - _ret_config = z_config_default(); + z_config_default(&_ret_config); assert(z_check(_ret_config)); #ifdef ZENOH_PICO - _ret_int8 = zp_config_insert(z_loan(_ret_config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[1])); + _ret_int8 = zp_config_insert(z_loan_mut(_ret_config), Z_CONFIG_CONNECT_KEY, argv[1]); assert_eq(_ret_int8, 0); 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 - z_owned_scouting_config_t _ret_sconfig = z_scouting_config_default(); + z_owned_scouting_config_t _ret_sconfig; + z_scouting_config_default(&_ret_sconfig); assert(z_check(_ret_sconfig)); #ifdef ZENOH_PICO - _ret_int8 = - zp_scouting_config_insert(z_loan(_ret_sconfig), Z_CONFIG_SCOUTING_TIMEOUT_KEY, z_string_make(SCOUTING_TIMEOUT)); + _ret_int8 = zp_scouting_config_insert(z_loan_mut(_ret_sconfig), Z_CONFIG_SCOUTING_TIMEOUT_KEY, SCOUTING_TIMEOUT); assert_eq(_ret_int8, 0); _ret_cstr = zp_scouting_config_get(z_loan(_ret_sconfig), Z_CONFIG_SCOUTING_TIMEOUT_KEY); assert_eq(strlen(_ret_cstr), strlen(SCOUTING_TIMEOUT)); @@ -209,8 +219,9 @@ int main(int argc, char **argv) { 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); + z_scouting_config_from(&_ret_sconfig, z_loan(_ret_config)); + z_owned_closure_hello_t _ret_closure_hello; + z_closure(&_ret_closure_hello, hello_handler, NULL, NULL); _ret_int8 = z_scout(z_move(_ret_sconfig), z_move(_ret_closure_hello)); assert_eq(_ret_int8, 0); assert(hellos >= 1); @@ -220,7 +231,8 @@ int main(int argc, char **argv) { printf("Ok\n"); z_sleep_s(SLEEP); - z_owned_session_t s1 = z_open(z_move(_ret_config)); + z_owned_session_t s1; + z_open(&s1, z_move(_ret_config)); assert(z_check(s1)); z_id_t _ret_zid = z_info_zid(z_loan(s1)); printf("Session 1 with PID: 0x"); @@ -229,7 +241,8 @@ int main(int argc, char **argv) { } printf("\n"); - z_owned_closure_zid_t _ret_closure_zid = z_closure(zid_handler, NULL, NULL); + z_owned_closure_zid_t _ret_closure_zid; + z_closure(&_ret_closure_zid, zid_handler, NULL, NULL); _ret_int8 = z_info_peers_zid(z_loan(s1), z_move(_ret_closure_zid)); assert_eq(_ret_int8, 0); sleep(SLEEP); @@ -242,24 +255,27 @@ int main(int argc, char **argv) { assert_eq(zids, 1); #ifdef ZENOH_PICO - zp_task_read_options_t _ret_read_opt = zp_task_read_options_default(); - zp_start_read_task(z_loan(s1), &_ret_read_opt); - zp_task_lease_options_t _ret_lease_opt = zp_task_lease_options_default(); - zp_start_lease_task(z_loan(s1), &_ret_lease_opt); + zp_task_read_options_t _ret_read_opt; + zp_task_read_options_default(&_ret_read_opt); + zp_start_read_task(z_loan_mut(s1), &_ret_read_opt); + zp_task_lease_options_t _ret_lease_opt; + zp_task_lease_options_default(&_ret_lease_opt); + zp_start_lease_task(z_loan_mut(s1), &_ret_lease_opt); #endif sleep(SLEEP); - _ret_config = z_config_default(); + z_config_default(&_ret_config); #ifdef ZENOH_PICO - _ret_int8 = zp_config_insert(z_loan(_ret_config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[1])); + _ret_int8 = zp_config_insert(z_loan_mut(_ret_config), Z_CONFIG_CONNECT_KEY, argv[1]); assert_eq(_ret_int8, 0); _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 - z_owned_session_t s2 = z_open(z_move(_ret_config)); + z_owned_session_t s2; + z_open(&s2, z_move(_ret_config)); assert(z_check(s2)); _ret_zid = z_info_zid(z_loan(s2)); printf("Session 2 with PID: 0x"); @@ -269,37 +285,44 @@ int main(int argc, char **argv) { printf("\n"); #ifdef ZENOH_PICO - zp_start_read_task(z_loan(s2), NULL); - zp_start_lease_task(z_loan(s2), NULL); + zp_start_read_task(z_loan_mut(s2), NULL); + zp_start_lease_task(z_loan_mut(s2), NULL); #endif sleep(SLEEP); - z_session_t ls1 = z_loan(s1); + const z_loaned_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)); + z_owned_closure_sample_t _ret_closure_sample; + z_closure(&_ret_closure_sample, data_handler, NULL, &ls1); + z_subscriber_options_t _ret_sub_opt; + z_subscriber_options_default(&_ret_sub_opt); + + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr_str); + z_owned_subscriber_t _ret_sub; + _ret_int8 = z_declare_subscriber(&_ret_sub, z_loan(s2), z_loan(ke), z_move(_ret_closure_sample), &_ret_sub_opt); + assert(_ret_int8 == _Z_RES_OK); 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)); + char s1_res[64]; + sprintf(s1_res, "%s/chunk/%d", keyexpr_str, 1); + z_view_keyexpr_t s1_key; + z_view_keyexpr_from_string(&s1_key, s1_res); + z_owned_keyexpr_t _ret_expr; + z_declare_keyexpr(&_ret_expr, z_loan(s1), z_loan(s1_key)); assert(z_check(_ret_expr)); printf("Ok\n"); printf("Session Put..."); - z_put_options_t _ret_put_opt = z_put_options_default(); + z_put_options_t _ret_put_opt; + z_put_options_default(&_ret_put_opt); _ret_put_opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; - z_encoding_t _ret_encoding = z_encoding_default(); - (void)(_ret_encoding); - _ret_encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); - _ret_put_opt.encoding = _ret_encoding; + z_owned_encoding_t _ret_encoding; + zp_encoding_make(&_ret_encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); + _ret_put_opt.encoding = z_move(_ret_encoding); _ret_int8 = z_put(z_loan(s1), z_loan(_ret_expr), (const uint8_t *)value, strlen(value), &_ret_put_opt); assert_eq(_ret_int8, 0); printf("Ok\n"); @@ -308,7 +331,8 @@ int main(int argc, char **argv) { assert_eq(datas, 1); printf("Session delete..."); - z_delete_options_t _ret_delete_opt = z_delete_options_default(); + z_delete_options_t _ret_delete_opt; + z_delete_options_default(&_ret_delete_opt); _ret_delete_opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; _ret_int8 = z_delete(z_loan(s1), z_loan(_ret_expr), &_ret_delete_opt); assert_eq(_ret_int8, 0); @@ -325,16 +349,19 @@ int main(int argc, char **argv) { printf("Ok\n"); printf("Declaring Publisher..."); - z_publisher_options_t _ret_pub_opt = z_publisher_options_default(); + z_publisher_options_t _ret_pub_opt; + z_publisher_options_default(&_ret_pub_opt); _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)); + z_owned_publisher_t _ret_pub; + _ret_int8 = z_declare_publisher(&_ret_pub, z_loan(s1), z_loan(s1_key), &_ret_pub_opt); + assert(_ret_int8 == _Z_RES_OK); printf("Ok\n"); sleep(SLEEP); printf("Publisher Put..."); - z_publisher_put_options_t _ret_pput_opt = z_publisher_put_options_default(); + z_publisher_put_options_t _ret_pput_opt; + z_publisher_put_options_default(&_ret_pput_opt); _ret_int8 = z_publisher_put(z_loan(_ret_pub), (const uint8_t *)value, strlen(value), &_ret_pput_opt); assert_eq(_ret_int8, 0); printf("Ok\n"); @@ -343,7 +370,8 @@ int main(int argc, char **argv) { assert_eq(datas, 3); printf("Publisher Delete..."); - z_publisher_delete_options_t _ret_pdelete_opt = z_publisher_delete_options_default(); + z_publisher_delete_options_t _ret_pdelete_opt; + z_publisher_delete_options_default(&_ret_pdelete_opt); _ret_int8 = z_publisher_delete(z_loan(_ret_pub), &_ret_pdelete_opt); assert_eq(_ret_int8, 0); printf("Ok\n"); @@ -368,19 +396,23 @@ int main(int argc, char **argv) { 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)); + z_owned_closure_query_t _ret_closure_query; + z_closure(&_ret_closure_query, query_handler, NULL, &ls1); + z_queryable_options_t _ret_qle_opt; + z_queryable_options_default(&_ret_qle_opt); + z_owned_queryable_t qle; + assert(z_declare_queryable(&qle, z_loan(s1), z_loan(s1_key), z_move(_ret_closure_query), &_ret_qle_opt) == + _Z_RES_OK); 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(); + const z_loaned_session_t *ls2 = z_loan(s2); + z_owned_closure_reply_t _ret_closure_reply; + z_closure(&_ret_closure_reply, reply_handler, NULL, &ls2); + z_get_options_t _ret_get_opt; + z_get_options_default(&_ret_get_opt); _ret_get_opt.target = z_query_target_default(); _ret_get_opt.consolidation = z_query_consolidation_auto(); (void)(_ret_get_opt.consolidation); @@ -395,7 +427,7 @@ int main(int argc, char **argv) { 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); + _ret_int8 = z_get(z_loan(s2), z_loan(s1_key), "", z_move(_ret_closure_reply), &_ret_get_opt); assert_eq(_ret_int8, 0); printf("Ok\n"); @@ -409,8 +441,8 @@ int main(int argc, char **argv) { printf("Ok\n"); #ifdef ZENOH_PICO - zp_stop_read_task(z_loan(s1)); - zp_stop_lease_task(z_loan(s1)); + zp_stop_read_task(z_loan_mut(s1)); + zp_stop_lease_task(z_loan_mut(s1)); #endif printf("Close sessions..."); @@ -418,8 +450,8 @@ int main(int argc, char **argv) { assert_eq(_ret_int8, 0); #ifdef ZENOH_PICO - zp_stop_read_task(z_loan(s2)); - zp_stop_lease_task(z_loan(s2)); + zp_stop_read_task(z_loan_mut(s2)); + zp_stop_lease_task(z_loan_mut(s2)); #endif _ret_int8 = z_close(z_move(s2)); assert_eq(_ret_int8, 0); @@ -427,7 +459,6 @@ int main(int argc, char **argv) { sleep(SLEEP * 5); - free(s1_res); free(keyexpr_str); return 0; diff --git a/tests/z_api_double_drop_test.c b/tests/z_api_double_drop_test.c index 776848b2a..48cd41e4b 100644 --- a/tests/z_api_double_drop_test.c +++ b/tests/z_api_double_drop_test.c @@ -23,7 +23,8 @@ #define URL "demo/example" void test_keyexpr(void) { - z_owned_keyexpr_t keyexpr = z_keyexpr_new(URL); + z_owned_keyexpr_t keyexpr; + z_keyexpr_new(&keyexpr, URL); assert(z_check(keyexpr)); z_drop(z_move(keyexpr)); assert(!z_check(keyexpr)); @@ -32,7 +33,8 @@ void test_keyexpr(void) { } void test_config(void) { - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); assert(z_check(config)); z_drop(z_move(config)); assert(!z_check(config)); @@ -41,7 +43,8 @@ void test_config(void) { } void test_scouting_config(void) { - z_owned_scouting_config_t config = z_scouting_config_default(); + z_owned_scouting_config_t config; + z_scouting_config_default(&config); assert(z_check(config)); z_drop(z_move(config)); assert(!z_check(config)); diff --git a/tests/z_api_null_drop_test.c b/tests/z_api_null_drop_test.c index 51d65328d..5494e283f 100644 --- a/tests/z_api_null_drop_test.c +++ b/tests/z_api_null_drop_test.c @@ -24,17 +24,28 @@ int main(void) { // // Check that all null functions exists // - z_owned_session_t session_null_1 = z_session_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_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(); + z_owned_session_t session_null_1; + z_session_null(&session_null_1); + z_owned_keyexpr_t keyexpr_null_1; + z_keyexpr_null(&keyexpr_null_1); + z_owned_config_t config_null_1; + z_config_null(&config_null_1); + z_owned_scouting_config_t scouting_config_null_1; + z_scouting_config_null(&scouting_config_null_1); + z_owned_hello_t hello_null_1; + z_hello_null(&hello_null_1); + z_owned_closure_sample_t closure_sample_null_1; + z_closure_sample_null(&closure_sample_null_1); + z_owned_closure_query_t closure_query_null_1; + z_closure_query_null(&closure_query_null_1); + z_owned_closure_reply_t closure_reply_null_1; + z_closure_reply_null(&closure_reply_null_1); + z_owned_closure_hello_t closure_hello_null_1; + z_closure_hello_null(&closure_hello_null_1); + z_owned_closure_zid_t closure_zid_null_1; + z_closure_zid_null(&closure_zid_null_1); + z_owned_string_t str_null_1; + z_string_null(&str_null_1); // // Test that they actually make invalid value (where applicable) @@ -59,7 +70,7 @@ int main(void) { 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_owned_string_t str_null_2; z_null(&session_null_2); z_null(&keyexpr_null_2); @@ -74,28 +85,32 @@ int main(void) { z_null(&str_null_2); #if Z_FEATURE_PUBLICATION == 1 - z_owned_publisher_t publisher_null_1 = z_publisher_null(); + z_owned_publisher_t publisher_null_1; + z_publisher_null(&publisher_null_1); assert(!z_check(publisher_null_1)); z_owned_publisher_t publisher_null_2; z_null(&publisher_null_2); assert(!z_check(publisher_null_2)); #endif #if Z_FEATURE_SUBSCRIPTION == 1 - z_owned_subscriber_t subscriber_null_1 = z_subscriber_null(); + z_owned_subscriber_t subscriber_null_1; + z_subscriber_null(&subscriber_null_1); assert(!z_check(subscriber_null_1)); z_owned_subscriber_t subscriber_null_2; z_null(&subscriber_null_2); assert(!z_check(subscriber_null_2)); #endif #if Z_FEATURE_QUERYABLE == 1 - z_owned_queryable_t queryable_null_1 = z_queryable_null(); + z_owned_queryable_t queryable_null_1; + z_queryable_null(&queryable_null_1); assert(!z_check(queryable_null_1)); z_owned_queryable_t queryable_null_2; z_null(&queryable_null_2); assert(!z_check(queryable_null_2)); #endif #if Z_FEATURE_QUERY == 1 - z_owned_reply_t reply_null_1 = z_reply_null(); + z_owned_reply_t reply_null_1; + z_reply_null(&reply_null_1); assert(!z_check(reply_null_1)); z_owned_reply_t reply_null_2; z_null(&reply_null_2); diff --git a/tests/z_channels_test.c b/tests/z_channels_test.c index 1751f8675..7ca5d3358 100644 --- a/tests/z_channels_test.c +++ b/tests/z_channels_test.c @@ -17,6 +17,7 @@ #include #include "zenoh-pico/api/handlers.h" +#include "zenoh-pico/api/macros.h" #include "zenoh-pico/net/sample.h" #undef NDEBUG @@ -27,33 +28,34 @@ _z_sample_t s = {.keyexpr = _z_rname("key"), \ .payload = {.start = (const uint8_t *)v, .len = strlen(v)}, \ .timestamp = _z_timestamp_null(), \ - .encoding = z_encoding_default(), \ + .encoding = _z_encoding_null(), \ .kind = 0, \ .qos = {0}}; \ - z_sample_t sample = {._rc = _z_sample_rc_new_from_val(s)}; \ + z_loaned_sample_t sample = _z_sample_rc_new_from_val(s); \ z_call(channel.send, &sample); \ } while (0); -#define _RECV(channel, method, buf) \ - do { \ - z_owned_sample_t sample = z_sample_null(); \ - z_call(channel.method, &sample); \ - if (z_check(sample)) { \ - z_sample_t loaned_sample = z_loan(sample); \ - z_bytes_t payload = z_sample_payload(&loaned_sample); \ - strncpy(buf, (const char *)payload.start, (size_t)payload.len); \ - buf[payload.len] = '\0'; \ - z_drop(z_move(sample)); \ - } else { \ - buf[0] = '\0'; \ - } \ +#define _RECV(channel, method, buf) \ + do { \ + z_owned_sample_t sample; \ + z_sample_null(&sample); \ + z_call(channel.method, &sample); \ + if (z_check(sample)) { \ + const z_loaned_bytes_t *payload = z_sample_payload(z_loan(sample)); \ + strncpy(buf, (const char *)payload->start, (size_t)payload->len); \ + buf[payload->len] = '\0'; \ + z_drop(z_move(sample)); \ + } else { \ + buf[0] = '\0'; \ + } \ } while (0); #define RECV(channel, buf) _RECV(channel, recv, buf) #define TRY_RECV(channel, buf) _RECV(channel, try_recv, buf) void sample_fifo_channel_test(void) { - z_owned_sample_fifo_channel_t channel = z_sample_fifo_channel_new(10); + z_owned_sample_fifo_channel_t channel; + z_sample_fifo_channel_new(&channel, 10); SEND(channel, "v1") SEND(channel, "v22") @@ -75,7 +77,8 @@ void sample_fifo_channel_test(void) { } void sample_fifo_channel_test_try_recv(void) { - z_owned_sample_fifo_channel_t channel = z_sample_fifo_channel_new(10); + z_owned_sample_fifo_channel_t channel; + z_sample_fifo_channel_new(&channel, 10); char buf[100]; @@ -102,7 +105,8 @@ void sample_fifo_channel_test_try_recv(void) { } void sample_ring_channel_test_in_size(void) { - z_owned_sample_ring_channel_t channel = z_sample_ring_channel_new(10); + z_owned_sample_ring_channel_t channel; + z_sample_ring_channel_new(&channel, 10); char buf[100]; @@ -129,7 +133,8 @@ void sample_ring_channel_test_in_size(void) { } void sample_ring_channel_test_over_size(void) { - z_owned_sample_ring_channel_t channel = z_sample_ring_channel_new(3); + z_owned_sample_ring_channel_t channel; + z_sample_ring_channel_new(&channel, 3); char buf[100]; TRY_RECV(channel, buf) diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 3564ec198..087ff18f7 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -47,37 +47,46 @@ z_owned_keyexpr_t rids2[SET]; volatile unsigned int total = 0; volatile unsigned int queries = 0; -void query_handler(const z_query_t *query, void *arg) { +void query_handler(const z_loaned_query_t *query, void *arg) { char *res = (char *)malloc(64); snprintf(res, 64, "%s%u", uri, *(unsigned int *)arg); printf(">> Received query: %s\t(%u/%u)\n", res, queries, total); - z_owned_str_t k_str = z_keyexpr_to_string(z_query_keyexpr(query)); - assert(_z_str_eq(z_loan(k_str), res) == true); + z_owned_string_t k_str; + z_keyexpr_to_string(z_query_keyexpr(query), &k_str); + assert(_z_str_eq(z_loan(k_str)->val, res) == true); - z_bytes_t pred = z_query_parameters(query); - assert(pred.len == strlen("")); - assert(strncmp((const char *)pred.start, "", strlen("")) == 0); + z_view_string_t pred; + z_query_parameters(query, &pred); + assert(z_loan(pred)->len == strlen("")); + assert(strncmp((const char *)z_loan(pred)->val, "", strlen("")) == 0); - z_query_reply(query, z_keyexpr(res), (const uint8_t *)res, strlen(res), NULL); + // Reply value encoding + z_view_string_t reply_str; + z_view_string_wrap(&reply_str, res); + z_owned_bytes_t reply_payload; + z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); + z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); queries++; z_drop(z_move(k_str)); free(res); } volatile unsigned int replies = 0; -void reply_handler(z_owned_reply_t *reply, void *arg) { +void reply_handler(const z_loaned_reply_t *reply, void *arg) { char *res = (char *)malloc(64); snprintf(res, 64, "%s%u", uri, *(unsigned int *)arg); if (z_reply_is_ok(reply)) { - z_loaned_sample_t sample = z_reply_ok(reply); + const z_loaned_sample_t *sample = z_reply_ok(reply); printf(">> Received reply data: %s\t(%u/%u)\n", res, replies, total); - z_owned_str_t k_str = z_keyexpr_to_string(sample.keyexpr); - assert(sample.payload.len == strlen(res)); - assert(strncmp(res, (const char *)sample.payload.start, strlen(res)) == 0); - assert(_z_str_eq(z_loan(k_str), res) == true); + z_owned_string_t k_str; + z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + assert(payload->len == strlen(res)); + assert(strncmp(res, (const char *)payload->start, strlen(res)) == 0); + assert(_z_str_eq(z_loan(k_str)->val, res) == true); replies++; z_drop(z_move(k_str)); @@ -88,25 +97,25 @@ 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) { +void data_handler(const z_loaned_sample_t *sample, void *arg) { char *res = (char *)malloc(64); snprintf(res, 64, "%s%u", uri, *(unsigned int *)arg); printf(">> Received data: %s\t(%u/%u)\n", res, datas, total); - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t k_str = z_keyexpr_to_string(keyexpr); - assert((payload.len == MSG_LEN) || (payload.len == FRAGMENT_MSG_LEN)); - assert(_z_str_eq(z_loan(k_str), res) == true); + z_owned_string_t k_str; + z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + assert((payload->len == MSG_LEN) || (payload->len == FRAGMENT_MSG_LEN)); + assert(_z_str_eq(z_loan(k_str)->val, res) == true); datas++; z_drop(z_move(k_str)); free(res); } -z_string_t format_id(const z_id_t *id) { +_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); + return _z_string_convert_bytes(&id_as_bytes); } int main(int argc, char **argv) { @@ -117,35 +126,38 @@ 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_CONNECT_KEY, z_string_make(argv[1])); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, argv[1]); for (unsigned int i = 0; i < SET; i++) idx[i] = i; - z_owned_session_t s1 = z_open(z_move(config)); + z_owned_session_t s1; + z_open(&s1, z_move(config)); assert(z_check(s1)); - z_string_t zid1 = format_id(&z_loan(s1)._val.in->val._local_zid); + _z_string_t zid1 = format_id(&(_Z_RC_IN_VAL(z_loan(s1))._local_zid)); printf("Session 1 with PID: %s\n", zid1.val); _z_string_clear(&zid1); // Start the read session session lease loops - zp_start_read_task(z_loan(s1), NULL); - zp_start_lease_task(z_loan(s1), NULL); + zp_start_read_task(z_loan_mut(s1), NULL); + zp_start_lease_task(z_loan_mut(s1), NULL); z_sleep_s(SLEEP); - config = z_config_default(); - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[1])); + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, argv[1]); - z_owned_session_t s2 = z_open(z_move(config)); + z_owned_session_t s2; + z_open(&s2, z_move(config)); assert(z_check(s2)); - z_string_t zid2 = format_id(&z_loan(s2)._val.in->val._local_zid); + _z_string_t zid2 = format_id(&(_Z_RC_IN_VAL(z_loan(s2))._local_zid)); printf("Session 2 with PID: %s\n", zid2.val); _z_string_clear(&zid2); // Start the read session session lease loops - zp_start_read_task(z_loan(s2), NULL); - zp_start_lease_task(z_loan(s2), NULL); + zp_start_read_task(z_loan_mut(s2), NULL); + zp_start_lease_task(z_loan_mut(s2), NULL); z_sleep_s(SLEEP); @@ -153,8 +165,11 @@ int main(int argc, char **argv) { char *s1_res = (char *)malloc(64); 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: %u %s\n", z_loan(expr)._id, z_loan(expr)._suffix); + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, s1_res); + z_owned_keyexpr_t expr; + z_declare_keyexpr(&expr, z_loan(s1), z_loan(ke)); + printf("Declared resource on session 1: %u %s\n", z_loan(expr)->_id, z_loan(expr)->_suffix); rids1[i] = expr; } @@ -162,8 +177,11 @@ 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: %u %s\n", z_loan(expr)._id, z_loan(expr)._suffix); + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, s1_res); + z_owned_keyexpr_t expr; + z_declare_keyexpr(&expr, z_loan(s2), z_loan(ke)); + printf("Declared resource on session 2: %u %s\n", z_loan(expr)->_id, z_loan(expr)->_suffix); rids2[i] = expr; } @@ -171,12 +189,13 @@ int main(int argc, char **argv) { // Declare subscribers and queryabales on second session for (unsigned int i = 0; i < SET; i++) { - z_owned_closure_sample_t callback = z_closure(data_handler, NULL, &idx[i]); + z_owned_closure_sample_t callback; + z_closure(&callback, data_handler, NULL, &idx[i]); 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->_entity_id, - z_loan(rids2[i])._id, ""); + int8_t res = z_declare_subscriber(sub, z_loan(s2), z_loan(rids2[i]), &callback, NULL); + assert(res == _Z_RES_OK); + printf("Declared subscription on session 2: %ju %u %s\n", (uintmax_t)z_subscriber_loan(sub)->_entity_id, + z_loan(rids2[i])->_id, ""); subs2 = _z_list_push(subs2, sub); } @@ -184,12 +203,13 @@ int main(int argc, char **argv) { for (unsigned int i = 0; i < SET; i++) { snprintf(s1_res, 64, "%s%u", uri, i); - z_owned_closure_query_t callback = z_closure(query_handler, NULL, &idx[i]); + z_owned_closure_query_t callback; + z_closure(&callback, query_handler, NULL, &idx[i]); 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->_entity_id, (z_zint_t)0, - s1_res); + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, s1_res); + assert(z_declare_queryable(qle, z_loan(s2), z_loan(ke), &callback, NULL) == _Z_RES_OK); + printf("Declared queryable on session 2: %ju %zu %s\n", (uintmax_t)qle->_val->_entity_id, (z_zint_t)0, s1_res); qles2 = _z_list_push(qles2, qle); } @@ -198,8 +218,9 @@ int main(int argc, char **argv) { // Declare publisher on first session for (unsigned int i = 0; i < SET; i++) { z_owned_publisher_t *pub = (z_owned_publisher_t *)z_malloc(sizeof(z_owned_publisher_t)); - *pub = z_declare_publisher(z_loan(s1), z_loan(rids1[i]), NULL); - if (!z_check(*pub)) printf("Declared publisher on session 1: %zu\n", z_loan(*pub)._val->_id); + if (z_declare_publisher(pub, z_loan(s1), z_loan(rids1[i]), NULL) < 0) { + printf("Declared publisher on session 1: %zu\n", z_loan(*pub)->_id); + } pubs1 = _z_list_push(pubs1, pub); } @@ -213,10 +234,11 @@ int main(int argc, char **argv) { total = MSG * SET; for (unsigned int n = 0; n < MSG; n++) { for (unsigned int i = 0; i < SET; i++) { - z_put_options_t opt = z_put_options_default(); + z_put_options_t opt; + z_put_options_default(&opt); opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; z_put(z_loan(s1), z_loan(rids1[i]), (const uint8_t *)payload, len, &opt); - printf("Wrote data from session 1: %u %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); } } @@ -247,10 +269,11 @@ int main(int argc, char **argv) { total = FRAGMENT_MSG_NB * SET; for (unsigned int n = 0; n < FRAGMENT_MSG_NB; n++) { for (unsigned int i = 0; i < SET; i++) { - z_put_options_t opt = z_put_options_default(); + z_put_options_t opt; + z_put_options_default(&opt); opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; z_put(z_loan(s1), z_loan(rids1[i]), (const uint8_t *)payload, len, &opt); - printf("Wrote fragment data from session 1: %u %zu b\t(%u/%u)\n", z_loan(rids1[i])._id, len, + printf("Wrote fragment data from session 1: %u %zu b\t(%u/%u)\n", z_loan(rids1[i])->_id, len, n * SET + (i + 1), total); } } @@ -273,8 +296,11 @@ int main(int argc, char **argv) { for (unsigned int n = 0; n < QRY; n++) { for (unsigned int i = 0; i < SET; i++) { 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); + z_owned_closure_reply_t callback; + z_closure(&callback, reply_handler, NULL, &idx[i]); + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, s1_res); + z_get(z_loan(s1), z_loan(ke), "", &callback, NULL); printf("Queried data from session 1: %zu %s\n", (z_zint_t)0, s1_res); } } @@ -311,7 +337,7 @@ int main(int argc, char **argv) { // Undeclare publishers on first session while (pubs1) { z_owned_publisher_t *pub = _z_list_head(pubs1); - printf("Undeclared publisher on session 2: %zu\n", z_loan(*pub)._val->_id); + printf("Undeclared publisher on session 2: %zu\n", z_loan(*pub)->_id); z_undeclare_publisher(z_move(*pub)); pubs1 = _z_list_pop(pubs1, _z_noop_elem_free, NULL); } @@ -321,7 +347,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->_entity_id); + printf("Undeclared subscriber on session 2: %ju\n", (uintmax_t)sub->_val->_entity_id); z_undeclare_subscriber(z_move(*sub)); subs2 = _z_list_pop(subs2, _z_noop_elem_free, NULL); } @@ -330,7 +356,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->_entity_id); + printf("Undeclared queryable on session 2: %ju\n", (uintmax_t)qle->_val->_entity_id); z_undeclare_queryable(z_move(*qle)); qles2 = _z_list_pop(qles2, _z_noop_elem_free, NULL); } @@ -339,14 +365,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: %u\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: %u\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])); } @@ -354,12 +380,12 @@ int main(int argc, char **argv) { // Stop both sessions printf("Stopping threads on session 1\n"); - zp_stop_read_task(z_loan(s1)); - zp_stop_lease_task(z_loan(s1)); + zp_stop_read_task(z_loan_mut(s1)); + zp_stop_lease_task(z_loan_mut(s1)); printf("Stopping threads on session 2\n"); - zp_stop_read_task(z_loan(s2)); - zp_stop_lease_task(z_loan(s2)); + zp_stop_read_task(z_loan_mut(s2)); + zp_stop_lease_task(z_loan_mut(s2)); // Close both sessions printf("Closing session 1\n"); diff --git a/tests/z_keyexpr_test.c b/tests/z_keyexpr_test.c index 1539b75a5..2d6aa68fd 100644 --- a/tests/z_keyexpr_test.c +++ b/tests/z_keyexpr_test.c @@ -105,100 +105,100 @@ 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") == 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(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", "@ab") == 0)); - assert(!(zp_keyexpr_intersect_null_terminated("@a", "@a/b") == 0)); - assert(!(zp_keyexpr_intersect_null_terminated("@a", "@a/*") == 0)); - assert(!(zp_keyexpr_intersect_null_terminated("@a", "@a/*/**") == 0)); - assert(!(zp_keyexpr_intersect_null_terminated("@a", "@a$*/**") == 0)); - assert((zp_keyexpr_intersect_null_terminated("@a", "@a/**") == 0)); - assert(!(zp_keyexpr_intersect_null_terminated("**/xyz$*xyz", "@a/b/xyzdefxyz") == 0)); - 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/@b/b/b/c/d/d/d/e") == 0)); - 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/**/e", "@a/b/b/d/d/d/e") == 0)); - assert((zp_keyexpr_intersect_null_terminated("@a/**/e", "@a/b/b/b/d/d/d/e") == 0)); - assert((zp_keyexpr_intersect_null_terminated("@a/**/e", "@a/b/b/c/d/d/d/e") == 0)); - assert(!(zp_keyexpr_intersect_null_terminated("@a/**/e", "@a/b/b/@c/b/d/d/d/e") == 0)); - assert(!(zp_keyexpr_intersect_null_terminated("@a/*", "@a/@b") == 0)); - assert(!(zp_keyexpr_intersect_null_terminated("@a/**", "@a/@b") == 0)); - assert((zp_keyexpr_intersect_null_terminated("@a/**/@b", "@a/@b") == 0)); - assert(!(zp_keyexpr_intersect_null_terminated("@a/**/@b", "@a/**/@c/**/@b") == 0)); - assert((zp_keyexpr_intersect_null_terminated("@a/@b/**", "@a/@b") == 0)); - assert((zp_keyexpr_intersect_null_terminated("@a/**/@c/@b", "@a/**/@c/**/@b") == 0)); - assert((zp_keyexpr_intersect_null_terminated("@a/**/@c/**/@b", "@a/**/@c/@b") == 0)); + assert((zp_keyexpr_intersect_null_terminated("@a", "@a"))); + assert(!zp_keyexpr_intersect_null_terminated("@a", "@ab")); + assert(!zp_keyexpr_intersect_null_terminated("@a", "@a/b")); + assert(!zp_keyexpr_intersect_null_terminated("@a", "@a/*")); + assert(!zp_keyexpr_intersect_null_terminated("@a", "@a/*/**")); + assert(!zp_keyexpr_intersect_null_terminated("@a", "@a$*/**")); + assert((zp_keyexpr_intersect_null_terminated("@a", "@a/**"))); + assert(!zp_keyexpr_intersect_null_terminated("**/xyz$*xyz", "@a/b/xyzdefxyz")); + 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/@b/b/b/c/d/d/d/e")); + 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/**/e", "@a/b/b/d/d/d/e"))); + assert((zp_keyexpr_intersect_null_terminated("@a/**/e", "@a/b/b/b/d/d/d/e"))); + assert((zp_keyexpr_intersect_null_terminated("@a/**/e", "@a/b/b/c/d/d/d/e"))); + assert(!zp_keyexpr_intersect_null_terminated("@a/**/e", "@a/b/b/@c/b/d/d/d/e")); + assert(!zp_keyexpr_intersect_null_terminated("@a/*", "@a/@b")); + 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/**/@c/**/@b")); + assert((zp_keyexpr_intersect_null_terminated("@a/@b/**", "@a/@b"))); + assert((zp_keyexpr_intersect_null_terminated("@a/**/@c/@b", "@a/**/@c/**/@b"))); + assert((zp_keyexpr_intersect_null_terminated("@a/**/@c/**/@b", "@a/**/@c/@b"))); assert(_z_keyexpr_includes("a", strlen("a"), "a", strlen("a"))); assert(_z_keyexpr_includes("a/b", strlen("a/b"), "a/b", strlen("a/b"))); @@ -234,54 +234,54 @@ 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") == 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); + 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", "@ab") == 0)); - assert(!(zp_keyexpr_includes_null_terminated("@a", "@a/b") == 0)); - assert(!(zp_keyexpr_includes_null_terminated("@a", "@a/*") == 0)); - assert(!(zp_keyexpr_includes_null_terminated("@a", "@a/*/**") == 0)); - assert(!(zp_keyexpr_includes_null_terminated("@a$*/**", "@a") == 0)); - assert(!(zp_keyexpr_includes_null_terminated("@a", "@a/**") == 0)); - assert((zp_keyexpr_includes_null_terminated("@a/**", "@a") == 0)); - assert(!(zp_keyexpr_includes_null_terminated("**/xyz$*xyz", "@a/b/xyzdefxyz") == 0)); - assert((zp_keyexpr_includes_null_terminated("@a/**/c/**/e", "@a/b/b/b/c/d/d/d/e") == 0)); - assert(!(zp_keyexpr_includes_null_terminated("@a/*", "@a/@b") == 0)); - assert(!(zp_keyexpr_includes_null_terminated("@a/**", "@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", "@a"))); + assert(!zp_keyexpr_includes_null_terminated("@a", "@ab")); + assert(!zp_keyexpr_includes_null_terminated("@a", "@a/b")); + assert(!zp_keyexpr_includes_null_terminated("@a", "@a/*")); + assert(!zp_keyexpr_includes_null_terminated("@a", "@a/*/**")); + assert(!zp_keyexpr_includes_null_terminated("@a$*/**", "@a")); + assert(!zp_keyexpr_includes_null_terminated("@a", "@a/**")); + assert((zp_keyexpr_includes_null_terminated("@a/**", "@a"))); + assert(!zp_keyexpr_includes_null_terminated("**/xyz$*xyz", "@a/b/xyzdefxyz")); + assert((zp_keyexpr_includes_null_terminated("@a/**/c/**/e", "@a/b/b/b/c/d/d/d/e"))); + assert(!zp_keyexpr_includes_null_terminated("@a/*", "@a/@b")); + 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"))); // clang-format off @@ -415,9 +415,9 @@ int main(void) { printf("\n"); } - 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); + assert(!zp_keyexpr_equals_null_terminated("a/**/$*b", "a/cb")); + assert(!zp_keyexpr_equals_null_terminated("a/bc", "a/cb")); + assert(zp_keyexpr_equals_null_terminated("greetings/hello/there", "greetings/hello/there")); return 0; } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 463adc5e0..7f515198d 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -219,9 +219,11 @@ char *gen_str(size_t size) { return str; } -_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); +_z_string_vec_t gen_str_array(size_t size) { + _z_string_vec_t sa = _z_string_vec_make(size); + for (size_t i = 0; i < size; i++) { + _z_string_vec_append(&sa, _z_string_make_as_ptr(gen_str(16))); + } return sa; } @@ -301,18 +303,18 @@ void assert_eq_uint8_array(const _z_bytes_t *left, const _z_bytes_t *right) { printf(")"); } -void assert_eq_str_array(_z_str_array_t *left, _z_str_array_t *right) { +void assert_eq_str_array(_z_string_vec_t *left, _z_string_vec_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); } @@ -329,10 +331,10 @@ void assert_eq_locator_array(const _z_locator_array_t *left, const _z_locator_ar const _z_locator_t *l = &left->_val[i]; const _z_locator_t *r = &right->_val[i]; - char *ls = _z_locator_to_str(l); - char *rs = _z_locator_to_str(r); + _z_string_t *ls = _z_locator_to_string(l); + _z_string_t *rs = _z_locator_to_string(r); - printf("%s:%s", ls, rs); + printf("%s:%s", ls->val, rs->val); if (i < left->_len - 1) printf(" "); z_free(ls); diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 5965d5ab1..898eeb508 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -41,17 +41,17 @@ _z_list_t *subs2 = NULL; volatile unsigned int total = 0; volatile unsigned int datas = 0; -void data_handler(const z_sample_t *sample, void *arg) { +void data_handler(const z_loaned_sample_t *sample, void *arg) { char *res = (char *)malloc(64); snprintf(res, 64, "%s%u", uri, *(unsigned int *)arg); printf(">> Received data: %s\t(%u/%u)\n", res, datas, total); - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_bytes_t payload = z_sample_payload(sample); - z_owned_str_t k_str = z_keyexpr_to_string(keyexpr); - assert(payload.len == MSG_LEN); - assert(strlen(z_loan(k_str)) == strlen(res)); - assert(strncmp(res, z_loan(k_str), strlen(res)) == 0); + z_owned_string_t k_str; + z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + assert(payload->len == MSG_LEN); + assert(z_loan(k_str)->len == strlen(res)); + assert(strncmp(res, z_loan(k_str)->val, strlen(res)) == 0); (void)(sample); datas++; @@ -67,40 +67,43 @@ int main(int argc, char **argv) { _Bool is_reliable = strncmp(argv[1], "tcp", 3) == 0; - 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_LISTEN_KEY, z_string_make(argv[1])); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, "peer"); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, argv[1]); for (unsigned int i = 0; i < SET; i++) idx[i] = i; - z_owned_session_t s1 = z_open(z_move(config)); + z_owned_session_t s1; + z_open(&s1, z_move(config)); assert(z_check(s1)); _z_bytes_t id_as_bytes = - _z_bytes_wrap(z_loan(s1)._val.in->val._local_zid.id, _z_id_len(z_loan(s1)._val.in->val._local_zid)); - z_string_t zid1 = _z_string_from_bytes(&id_as_bytes); + _z_bytes_wrap(_Z_RC_IN_VAL(z_loan(s1))._local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s1))._local_zid)); + _z_string_t zid1 = _z_string_convert_bytes(&id_as_bytes); printf("Session 1 with PID: %s\n", zid1.val); _z_string_clear(&zid1); // Start the read session session lease loops - zp_start_read_task(z_loan(s1), NULL); - zp_start_lease_task(z_loan(s1), NULL); + zp_start_read_task(z_loan_mut(s1), NULL); + zp_start_lease_task(z_loan_mut(s1), NULL); z_sleep_s(SLEEP); - 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_CONNECT_KEY, z_string_make(argv[1])); + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, "peer"); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, argv[1]); - z_owned_session_t s2 = z_open(z_move(config)); + z_owned_session_t s2; + z_open(&s2, z_move(config)); assert(z_check(s2)); - id_as_bytes = _z_bytes_wrap(z_loan(s2)._val.in->val._local_zid.id, _z_id_len(z_loan(s2)._val.in->val._local_zid)); - z_string_t zid2 = _z_string_from_bytes(&id_as_bytes); + id_as_bytes = _z_bytes_wrap(_Z_RC_IN_VAL(z_loan(s2))._local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s2))._local_zid)); + _z_string_t zid2 = _z_string_convert_bytes(&id_as_bytes); printf("Session 2 with PID: %s\n", zid2.val); _z_string_clear(&zid2); // Start the read session session lease loops - zp_start_read_task(z_loan(s2), NULL); - zp_start_lease_task(z_loan(s2), NULL); + zp_start_read_task(z_loan_mut(s2), NULL); + zp_start_lease_task(z_loan_mut(s2), NULL); z_sleep_s(SLEEP * 5); @@ -109,11 +112,14 @@ int main(int argc, char **argv) { for (unsigned int i = 0; i < SET; i++) { memset(s1_res, 0, 64); snprintf(s1_res, 64, "%s%u", uri, i); - z_owned_closure_sample_t callback = z_closure(data_handler, NULL, &idx[i]); + z_owned_closure_sample_t callback; + z_closure(&callback, data_handler, NULL, &idx[i]); 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->_entity_id, + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, s1_res); + int8_t res = z_declare_subscriber(sub, z_loan(s2), z_loan(ke), &callback, NULL); + assert(res == _Z_RES_OK); + printf("Declared subscription on session 2: %ju %zu %s\n", (uintmax_t)z_subscriber_loan(sub)->_entity_id, (z_zint_t)0, s1_res); subs2 = _z_list_push(subs2, sub); } @@ -128,9 +134,12 @@ int main(int argc, char **argv) { for (unsigned int i = 0; i < SET; i++) { memset(s1_res, 0, 64); snprintf(s1_res, 64, "%s%u", uri, i); - z_put_options_t opt = z_put_options_default(); + z_put_options_t opt; + z_put_options_default(&opt); opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; - z_put(z_loan(s1), z_keyexpr(s1_res), (const uint8_t *)payload, len, &opt); + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, s1_res); + z_put(z_loan(s1), z_loan(ke), (const uint8_t *)payload, len, &opt); printf("Wrote data from session 1: %s %zu b\t(%u/%u)\n", s1_res, len, n * SET + (i + 1), total); } } @@ -155,7 +164,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->_entity_id); + printf("Undeclared subscriber on session 2: %ju\n", (uintmax_t)z_subscriber_loan(sub)->_entity_id); z_undeclare_subscriber(z_move(*sub)); subs2 = _z_list_pop(subs2, _z_noop_elem_free, NULL); } @@ -164,12 +173,12 @@ int main(int argc, char **argv) { // Stop both sessions printf("Stopping threads on session 1\n"); - zp_stop_read_task(z_loan(s1)); - zp_stop_lease_task(z_loan(s1)); + zp_stop_read_task(z_loan_mut(s1)); + zp_stop_lease_task(z_loan_mut(s1)); printf("Stopping threads on session 2\n"); - zp_stop_read_task(z_loan(s2)); - zp_stop_lease_task(z_loan(s2)); + zp_stop_read_task(z_loan_mut(s2)); + zp_stop_lease_task(z_loan_mut(s2)); // Close both sessions printf("Closing session 1\n"); diff --git a/tests/z_perf_rx.c b/tests/z_perf_rx.c index b6ef2d291..331bebb1a 100644 --- a/tests/z_perf_rx.c +++ b/tests/z_perf_rx.c @@ -39,16 +39,16 @@ void z_stats_stop(z_stats_t *stats) { stats->count = 0; } -void on_sample(const z_sample_t *sample, void *context) { +void on_sample(const z_loaned_sample_t *sample, void *context) { z_stats_t *stats = (z_stats_t *)context; - z_bytes_t payload = z_sample_payload(sample); + const z_loaned_bytes_t *payload = z_sample_payload(sample); - if (stats->curr_len != payload.len) { + if (stats->curr_len != payload->len) { // End previous measurement z_stats_stop(stats); // Check for end packet - stats->curr_len = (unsigned long)payload.len; - if (payload.len == 1) { + stats->curr_len = (unsigned long)payload->len; + if (payload->len == 1) { test_end = true; return; } @@ -75,32 +75,36 @@ int main(int argc, char **argv) { clocator = "tcp/127.0.0.1:7447"; } // Set config - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); if (mode != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } // Open session - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); exit(-1); } // Declare Subscriber/resource - z_owned_closure_sample_t callback = z_closure(on_sample, NULL, (void *)&test_stats); - z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL); - if (!z_check(sub)) { + z_owned_closure_sample_t callback; + z_closure(&callback, on_sample, NULL, (void *)&test_stats); + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to create subscriber.\n"); exit(-1); } @@ -113,13 +117,14 @@ int main(int argc, char **argv) { z_sleep_s(1); // Clean up z_undeclare_subscriber(z_move(sub)); - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); exit(0); } #else int main(void) { + (void)test_stats; printf("ERROR: Zenoh pico was compiled without Z_FEATURE_SUBSCRIPTION but this test requires it.\n"); return -2; } diff --git a/tests/z_perf_tx.c b/tests/z_perf_tx.c index 1852ecf37..be324d3b7 100644 --- a/tests/z_perf_tx.c +++ b/tests/z_perf_tx.c @@ -52,31 +52,34 @@ int main(int argc, char **argv) { clocator = "tcp/127.0.0.1:7447"; } // Set config - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); if (mode != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } // Open session - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { printf("Unable to open session!\n"); exit(-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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); exit(-1); } // Declare publisher - z_owned_publisher_t pub = z_declare_publisher(z_loan(s), z_keyexpr(keyexpr), NULL); - if (!z_check(pub)) { + z_owned_publisher_t pub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); exit(-1); } @@ -97,8 +100,8 @@ int main(int argc, char **argv) { z_publisher_put(z_loan(pub), (const uint8_t *)value, 1, NULL); // Clean up z_undeclare_publisher(z_move(pub)); - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); free(value); exit(0); diff --git a/tests/z_session_test.c b/tests/z_session_test.c index 40058e102..dcb55e097 100644 --- a/tests/z_session_test.c +++ b/tests/z_session_test.c @@ -22,18 +22,19 @@ #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")); + z_owned_config_t config; + z_config_default(&config); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, "client"); printf("Opening session...\n"); - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; @@ -43,8 +44,8 @@ int main(void) { // 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)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); // Immediately close the session z_close(&s); diff --git a/tests/z_test_fragment_rx.c b/tests/z_test_fragment_rx.c index dac7f0883..8f1c8b95f 100644 --- a/tests/z_test_fragment_rx.c +++ b/tests/z_test_fragment_rx.c @@ -18,20 +18,21 @@ #include #if Z_FEATURE_SUBSCRIPTION == 1 -void data_handler(const z_sample_t *sample, void *ctx) { +void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_keyexpr_t keyexpr = z_sample_keyexpr(sample); - z_owned_str_t keystr = z_keyexpr_to_string(keyexpr); + z_owned_string_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); bool is_valid = true; - z_bytes_t payload = z_sample_payload(sample); - const uint8_t *data = payload.start; - for (size_t i = 0; i < payload.len; i++) { + const z_loaned_bytes_t *payload = z_sample_payload(sample); + const uint8_t *data = payload->start; + for (size_t i = 0; i < payload->len; i++) { if (data[i] != (uint8_t)i) { is_valid = false; break; } } - printf("[rx]: Received packet on %s, len: %d, validity: %d\n", z_loan(keystr), (int)payload.len, is_valid); + printf("[rx]: Received packet on %s, len: %d, validity: %d\n", z_string_data(z_loan(keystr)), + (int)z_sample_payload(sample)->len, is_valid); z_drop(z_move(keystr)); } @@ -51,32 +52,36 @@ int main(int argc, char **argv) { clocator = "tcp/127.0.0.1:7447"; } // Set config - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); if (mode != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } // Open session - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; } // Declare subscriber - z_owned_closure_sample_t callback = z_closure(data_handler); - z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL); - if (!z_check(sub)) { + z_owned_closure_sample_t callback; + z_closure(&callback, data_handler); + z_owned_subscriber_t sub; + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } @@ -89,8 +94,8 @@ int main(int argc, char **argv) { } // Clean up z_undeclare_subscriber(z_move(sub)); - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); return 0; } diff --git a/tests/z_test_fragment_tx.c b/tests/z_test_fragment_tx.c index f964797f9..0e6658565 100644 --- a/tests/z_test_fragment_tx.c +++ b/tests/z_test_fragment_tx.c @@ -46,24 +46,25 @@ int main(int argc, char **argv) { clocator = "tcp/127.0.0.1:7447"; } // Set config - z_owned_config_t config = z_config_default(); + z_owned_config_t config; + z_config_default(&config); if (mode != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); } if (llocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); } if (clocator != NULL) { - zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); } // Open session - z_owned_session_t s = z_open(z_move(config)); - if (!z_check(s)) { + z_owned_session_t s; + if (z_open(&s, z_move(config)) < 0) { 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_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { printf("Unable to start read and lease tasks\n"); z_close(z_session_move(&s)); return -1; @@ -73,16 +74,18 @@ int main(int argc, char **argv) { z_sleep_s(3); } // Put data + z_view_keyexpr_t ke; + z_view_keyexpr_from_string(&ke, keyexpr); for (int i = 0; i < 5; i++) { printf("[tx]: Sending packet on %s, len: %d\n", keyexpr, (int)size); - if (z_put(z_loan(s), z_keyexpr(keyexpr), (const uint8_t *)value, size, NULL) < 0) { + if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)value, size, NULL) < 0) { printf("Oh no! Put has failed...\n"); } z_sleep_s(1); } // Clean up - zp_stop_read_task(z_loan(s)); - zp_stop_lease_task(z_loan(s)); + zp_stop_read_task(z_loan_mut(s)); + zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); free(value); return 0; diff --git a/zenohpico.pc b/zenohpico.pc index 17aab2980..642e15247 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.11.20240326dev +Version: 0.11.20240517dev Cflags: -I${prefix}/include Libs: -L${prefix}/lib -lzenohpico