From d226db9ffe51a5c0c57e71a6b720560910daa969 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Thu, 16 May 2024 12:42:56 +0200 Subject: [PATCH 01/43] API rework --- .github/workflows/build-check.yaml | 2 +- CMakeLists.txt | 4 +- GNUmakefile | 2 +- examples/arduino/z_get.ino | 27 +- examples/arduino/z_pub.ino | 18 +- examples/arduino/z_pull.ino | 23 +- examples/arduino/z_queryable.ino | 29 +- examples/arduino/z_scout.ino | 6 +- examples/arduino/z_sub.ino | 33 +- examples/espidf/z_get.c | 34 +- examples/espidf/z_pub.c | 23 +- examples/espidf/z_pull.c | 30 +- examples/espidf/z_queryable.c | 31 +- examples/espidf/z_scout.c | 6 +- examples/espidf/z_sub.c | 37 +- examples/freertos_plus_tcp/z_get.c | 31 +- examples/freertos_plus_tcp/z_pub.c | 25 +- examples/freertos_plus_tcp/z_pub_st.c | 15 +- examples/freertos_plus_tcp/z_pull.c | 27 +- examples/freertos_plus_tcp/z_put.c | 28 +- examples/freertos_plus_tcp/z_queryable.c | 34 +- examples/freertos_plus_tcp/z_scout.c | 6 +- examples/freertos_plus_tcp/z_sub.c | 34 +- examples/freertos_plus_tcp/z_sub_st.c | 29 +- examples/mbed/z_get.cpp | 33 +- examples/mbed/z_pub.cpp | 23 +- examples/mbed/z_pull.cpp | 30 +- examples/mbed/z_queryable.cpp | 39 +- examples/mbed/z_scout.cpp | 6 +- examples/mbed/z_sub.cpp | 40 +- examples/unix/c11/z_get.c | 47 +- examples/unix/c11/z_get_channel.c | 49 +- examples/unix/c11/z_info.c | 25 +- examples/unix/c11/z_ping.c | 31 +- examples/unix/c11/z_pong.c | 38 +- examples/unix/c11/z_pub.c | 26 +- examples/unix/c11/z_pub_st.c | 17 +- examples/unix/c11/z_pub_thr.c | 19 +- examples/unix/c11/z_pull.c | 34 +- examples/unix/c11/z_put.c | 30 +- examples/unix/c11/z_queryable.c | 58 +- examples/unix/c11/z_queryable_channel.c | 63 +- examples/unix/c11/z_scout.c | 32 +- examples/unix/c11/z_sub.c | 36 +- examples/unix/c11/z_sub_channel.c | 36 +- examples/unix/c11/z_sub_st.c | 30 +- examples/unix/c11/z_sub_thr.c | 24 +- examples/unix/c99/z_get.c | 37 +- examples/unix/c99/z_info.c | 25 +- examples/unix/c99/z_ping.c | 35 +- examples/unix/c99/z_pong.c | 39 +- examples/unix/c99/z_pub.c | 26 +- examples/unix/c99/z_pub_st.c | 17 +- examples/unix/c99/z_pull.c | 25 +- examples/unix/c99/z_put.c | 30 +- examples/unix/c99/z_queryable.c | 45 +- examples/unix/c99/z_scout.c | 32 +- examples/unix/c99/z_sub.c | 40 +- examples/unix/c99/z_sub_st.c | 34 +- examples/windows/z_get.c | 29 +- examples/windows/z_info.c | 23 +- examples/windows/z_ping.c | 24 +- examples/windows/z_pong.c | 24 +- examples/windows/z_pub.c | 24 +- examples/windows/z_pub_st.c | 15 +- examples/windows/z_pull.c | 25 +- examples/windows/z_put.c | 28 +- examples/windows/z_queryable.c | 32 +- examples/windows/z_scout.c | 6 +- examples/windows/z_sub.c | 34 +- examples/windows/z_sub_st.c | 28 +- examples/zephyr/z_get.c | 33 +- examples/zephyr/z_pub.c | 23 +- examples/zephyr/z_pull.c | 30 +- examples/zephyr/z_queryable.c | 33 +- examples/zephyr/z_scout.c | 6 +- examples/zephyr/z_sub.c | 37 +- include/zenoh-pico/api/handlers.h | 25 +- include/zenoh-pico/api/macros.h | 82 +- include/zenoh-pico/api/primitives.h | 309 ++++---- include/zenoh-pico/api/types.h | 134 ++-- include/zenoh-pico/collections/refcount.h | 4 +- include/zenoh-pico/config.h | 2 +- include/zenoh-pico/net/primitives.h | 6 +- include/zenoh-pico/net/query.h | 7 - include/zenoh-pico/net/reply.h | 2 +- include/zenoh-pico/session/session.h | 10 +- include/zenoh-pico/utils/config.h | 2 +- src/api/api.c | 891 ++++++++++------------ src/api/handlers.c | 22 +- src/net/config.c | 10 +- src/net/primitives.c | 8 +- src/net/reply.c | 4 +- src/net/session.c | 6 +- src/protocol/codec/message.c | 2 +- src/protocol/config.c | 6 +- src/session/query.c | 19 +- src/session/queryable.c | 6 +- src/session/subscription.c | 6 +- src/transport/multicast.c | 3 +- src/transport/unicast.c | 3 +- tests/z_api_alignment_test.c | 151 ++-- tests/z_api_double_drop_test.c | 9 +- tests/z_api_null_drop_test.c | 45 +- tests/z_channels_test.c | 40 +- tests/z_client_test.c | 133 ++-- tests/z_peer_multicast_test.c | 71 +- tests/z_perf_rx.c | 36 +- tests/z_perf_tx.c | 23 +- tests/z_session_test.c | 15 +- tests/z_test_fragment_rx.c | 41 +- tests/z_test_fragment_tx.c | 19 +- zenohpico.pc | 2 +- 113 files changed, 2298 insertions(+), 1995 deletions(-) diff --git a/.github/workflows/build-check.yaml b/.github/workflows/build-check.yaml index e67242d4b..b90e5feaf 100644 --- a/.github/workflows/build-check.yaml +++ b/.github/workflows/build-check.yaml @@ -103,7 +103,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 }} 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/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index 754d4a1fa..b0e78e654 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -45,9 +45,10 @@ void reply_dropper(void *ctx) { void reply_handler(z_owned_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_str_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(" >> [Get listener] Received ("); Serial.print(z_str_loan(&keystr)); @@ -78,16 +79,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 +97,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,11 +111,13 @@ 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); if (strcmp(VALUE, "") != 0) { opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); } - z_owned_closure_reply_t callback = z_closure_reply(reply_handler, reply_dropper, NULL); + z_owned_closure_reply_t callback; + z_closure_reply(&callback, reply_handler, reply_dropper, NULL); if (z_get(z_session_loan(&s), z_keyexpr(KEYEXPR), "", 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..6c06e9a8c 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,14 @@ 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)) { + if (z_declare_publisher(&pub, z_session_loan(&s), z_keyexpr(KEYEXPR), 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..867d84bdc 100644 --- a/examples/arduino/z_pull.ino +++ b/examples/arduino/z_pull.ino @@ -38,8 +38,9 @@ // 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_str_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 ("); @@ -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,15 +88,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 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)) { diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index b6e3c7ed5..b07ecc17b 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -35,8 +35,9 @@ #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_str_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); Serial.print(" >> [Queryable handler] Replying Data ('"); Serial.print(z_str_loan(&keystr)); @@ -66,16 +67,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 +86,18 @@ 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; + if (z_declare_queryable(&qable, z_session_loan(&s), z_keyexpr(KEYEXPR), 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..05b1770a1 100644 --- a/examples/arduino/z_scout.ino +++ b/examples/arduino/z_scout.ino @@ -114,8 +114,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..c5a79299e 100644 --- a/examples/arduino/z_sub.ino +++ b/examples/arduino/z_sub.ino @@ -34,11 +34,10 @@ #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_str_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)); @@ -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,18 @@ 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; + if (z_declare_subscriber(&sub, z_session_loan(&s), z_keyexpr(KEYEXPR), 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..f2db48257 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -105,9 +105,12 @@ void reply_dropper(void *ctx) { printf(" >> Received query final notification\n" void reply_handler(z_owned_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_str_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + + printf(" >> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len, + z_sample_payload(sample)->start); z_drop(z_move(keystr)); } else { printf(" >> Received an error\n"); @@ -132,33 +135,36 @@ 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); if (strcmp(VALUE, "") != 0) { opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); } - z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); + z_owned_closure_reply_t callback; + z_closure(&callback, reply_handler, reply_dropper); if (z_get(z_loan(s), z_keyexpr(KEYEXPR), "", z_move(callback), &opts) < 0) { printf("Unable to send query.\n"); exit(-1); @@ -167,8 +173,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..6aad2f139 100644 --- a/examples/espidf/z_pub.c +++ b/examples/espidf/z_pub.c @@ -119,28 +119,29 @@ 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; + if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(KEYEXPR), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); exit(-1); } @@ -158,8 +159,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..aef49a4b7 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_str_t keystr; +// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), +// (int)sample->payload.len, // sample->payload.start); // z_drop(z_move(keystr)); // } @@ -126,27 +128,29 @@ 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); @@ -167,8 +171,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..dd642a62a 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -103,9 +103,10 @@ void wifi_init_sta(void) { void query_handler(z_query_t *query, void *ctx) { (void)(ctx); - z_owned_str_t keystr = z_keyexpr_to_string(z_query_keyexpr(query)); + z_owned_str_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); z_bytes_t pred = z_query_parameters(query); - printf(">> [Queryable handler] Received Query '%s%.*s'\n", z_loan(keystr), (int)pred.len, pred.start); + printf(">> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)pred.len, pred.start); z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL); z_drop(z_move(keystr)); } @@ -128,30 +129,32 @@ 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; + if (z_declare_queryable(&qable, z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL) < 0) { printf("Unable to declare queryable.\n"); exit(-1); } @@ -166,8 +169,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..ed2eb44f7 100644 --- a/examples/espidf/z_scout.c +++ b/examples/espidf/z_scout.c @@ -167,8 +167,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..000bf238e 100644 --- a/examples/espidf/z_sub.c +++ b/examples/espidf/z_sub.c @@ -100,11 +100,12 @@ 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); +void data_handler(const z_loaned_sample_t* sample, void* arg) { + z_owned_str_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_payload_t* payload = z_sample_payload(sample); + printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len, + payload->start); z_str_drop(z_str_move(&keystr)); } @@ -126,29 +127,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 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; + if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); exit(-1); } @@ -162,8 +165,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..bc27664cd 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -39,9 +39,11 @@ void reply_dropper(void *ctx) { void reply_handler(z_owned_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_str_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); z_drop(z_move(keystr)); } else { printf(">> Received an error\n"); @@ -49,21 +51,22 @@ void reply_handler(z_owned_reply_t *reply, void *ctx) { } 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; @@ -78,11 +81,13 @@ void app_main(void) { 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); if (strcmp(VALUE, "") != 0) { opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); } - z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); + z_owned_closure_reply_t callback; + z_closure(&callback, reply_handler, reply_dropper); if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { printf("Unable to send query.\n"); return; @@ -90,8 +95,8 @@ void app_main(void) { } // 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..73a675427 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,16 @@ 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; + if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(KEYEXPR), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return; } @@ -93,7 +95,8 @@ 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(); + z_publisher_put_options_t options; + z_publisher_put_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); } @@ -101,8 +104,8 @@ void app_main(void) { 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)); } diff --git a/examples/freertos_plus_tcp/z_pub_st.c b/examples/freertos_plus_tcp/z_pub_st.c index 5803030c4..0686bf584 100644 --- a/examples/freertos_plus_tcp/z_pub_st.c +++ b/examples/freertos_plus_tcp/z_pub_st.c @@ -33,22 +33,23 @@ #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; + if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(KEYEXPR), 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..1f2797a31 100644 --- a/examples/freertos_plus_tcp/z_pull.c +++ b/examples/freertos_plus_tcp/z_pull.c @@ -29,37 +29,40 @@ #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_str_t keystr; +// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_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); @@ -78,8 +81,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..4459d04a0 100644 --- a/examples/freertos_plus_tcp/z_put.c +++ b/examples/freertos_plus_tcp/z_put.c @@ -30,38 +30,40 @@ #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; + if (z_declare_keyexpr(&ke, z_loan(s), z_keyexpr(KEYEXPR)) < 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(); + z_put_options_t options; + z_put_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)VALUE, strlen(VALUE), &options) < 0) { printf("Oh no! Put has failed...\n"); @@ -73,8 +75,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..40a730aff 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -29,37 +29,40 @@ #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_owned_str_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); 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); + printf(" >> [Queryable handler] Received Query '%s?%.*s'\n", z_str_data(z_loan(keystr)), (int)pred.len, pred.start); if (payload_value.payload.len > 0) { printf(" with value '%.*s'\n", (int)payload_value.payload.len, payload_value.payload.start); } - z_query_reply_options_t options = z_query_reply_options_default(); + z_query_reply_options_t options; + z_query_reply_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), &options); z_drop(z_move(keystr)); } 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; @@ -72,9 +75,10 @@ void app_main(void) { } 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), ke, z_move(callback), NULL) < 0) { printf("Unable to create queryable.\n"); return; } @@ -86,8 +90,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..702021b99 100644 --- a/examples/freertos_plus_tcp/z_scout.c +++ b/examples/freertos_plus_tcp/z_scout.c @@ -85,8 +85,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..fd674d66c 100644 --- a/examples/freertos_plus_tcp/z_sub.c +++ b/examples/freertos_plus_tcp/z_sub.c @@ -28,40 +28,42 @@ #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_str_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_str_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_owned_subscriber_t sub; + if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); return; } @@ -73,8 +75,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..1f89f1518 100644 --- a/examples/freertos_plus_tcp/z_sub_st.c +++ b/examples/freertos_plus_tcp/z_sub_st.c @@ -31,34 +31,37 @@ 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_str_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_str_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_owned_subscriber_t sub; + if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(KEYEXPR), 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..36052b73c 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -35,9 +35,11 @@ void reply_dropper(void *ctx) { printf(" >> Received query final notification\n" void reply_handler(z_owned_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); + const z_loaned_sample_t *sample = z_reply_ok(oreply); + z_owned_str_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(" >> Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len, payload->start); z_str_drop(z_str_move(&keystr)); } else { printf(" >> Received an error\n"); @@ -52,33 +54,36 @@ 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); if (strcmp(VALUE, "") != 0) { opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); } - z_owned_closure_reply_t callback = z_closure_reply(reply_handler, reply_dropper, NULL); + z_owned_closure_reply_t callback; + z_closure_reply(&callback, reply_handler, reply_dropper, NULL); if (z_get(z_session_loan(&s), z_keyexpr(KEYEXPR), "", z_closure_reply_move(&callback), &opts) < 0) { printf("Unable to send query.\n"); exit(-1); @@ -87,8 +92,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..4395edcdb 100644 --- a/examples/mbed/z_pub.cpp +++ b/examples/mbed/z_pub.cpp @@ -39,28 +39,29 @@ 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; + if (z_declare_publisher(&pub, z_session_loan(&s), z_keyexpr(KEYEXPR), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); exit(-1); } @@ -78,8 +79,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..8beda1521 100644 --- a/examples/mbed/z_pull.cpp +++ b/examples/mbed/z_pull.cpp @@ -31,9 +31,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_str_loan(&keystr), (int)sample->payload.len, +// void data_handler(const z_loaned_sample_t *sample, void *arg) { +// z_owned_str_t keystr; +// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), +// (int)sample->payload.len, // sample->payload.start); // z_str_drop(z_str_move(&keystr)); // } @@ -46,27 +48,29 @@ 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 = @@ -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..dd9be6976 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -31,11 +31,14 @@ #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_owned_str_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_str_t pred; + z_query_parameters(query, &pred); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_str_loan(&keystr)), + (int)z_view_str_loan(&pred)->len, z_view_str_loan(&pred)->val); z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL); z_str_drop(z_str_move(&keystr)); } @@ -48,31 +51,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); // 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; + if (z_declare_queryable(&qable, z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_query_move(&callback), NULL) < + 0) { printf("Unable to declare queryable.\n"); exit(-1); } @@ -87,8 +92,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..0eeb27084 100644 --- a/examples/mbed/z_scout.cpp +++ b/examples/mbed/z_scout.cpp @@ -99,8 +99,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..3c750f1c3 100644 --- a/examples/mbed/z_sub.cpp +++ b/examples/mbed/z_sub.cpp @@ -30,12 +30,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_str_loan(&keystr), (int)payload.len, - payload.start); +void data_handler(const z_loaned_sample_t *sample, void *arg) { + z_owned_str_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_str_data(z_str_loan(&keystr)), (int)payload->len, + payload->start); z_str_drop(z_str_move(&keystr)); } @@ -47,30 +47,32 @@ 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; + if (z_declare_subscriber(&sub, z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_sample_move(&callback), NULL) < + 0) { printf("Unable to declare subscriber.\n"); exit(-1); } @@ -84,8 +86,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..c93a8fb66 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -40,9 +40,11 @@ int8_t attachment_handler(z_bytes_t key, z_bytes_t att_value, void *ctx) { void reply_handler(z_owned_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_str_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len, + z_sample_payload(sample)->start); #if Z_FEATURE_ATTACHMENT == 1 if (z_attachment_check(&sample.attachment)) { printf("Attachement found\n"); @@ -95,38 +97,41 @@ 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)) { - printf("%s is not a valid key expression", keyexpr); - return -1; - } + // TODO(sashacmc): + // z_keyexpr_t ke = z_keyexpr(keyexpr); + // if (!z_check(ke)) { + // 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)); } @@ -135,8 +140,10 @@ int main(int argc, char **argv) { 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_keyexpr(keyexpr), "", z_move(callback), &opts) < 0) { printf("Unable to send query.\n"); return -1; } @@ -144,8 +151,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..92a029574 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -57,52 +57,59 @@ 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)) { - printf("%s is not a valid key expression", keyexpr); - return -1; - } + // TODO(sashacmc): + // z_keyexpr_t ke = z_keyexpr(keyexpr); + // if (!z_check(ke)) { + // 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); if (value != NULL) { opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); } - 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_keyexpr(keyexpr), "", 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); + const z_loaned_sample_t *sample = z_reply_ok(&reply); + z_owned_str_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len, + z_sample_payload(sample)->start); z_drop(z_move(keystr)); } else { printf(">> Received an error\n"); @@ -112,8 +119,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..8bb0078d6 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,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_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + // TODO(sashacmc): + // z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + // z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); - z_owned_publisher_t pub = z_declare_publisher(z_loan(session), ping, NULL); - if (!z_check(pub)) { + z_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_loan(session), z_keyexpr("test/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_keyexpr("test/pong"), z_move(respond), NULL) < 0) { printf("Unable to declare subscriber for key expression.\n"); return -1; } @@ -123,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/unix/c11/z_pong.c b/examples/unix/c11/z_pong.c index b990e21c2..0cc873643 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)) { + // TODO(sashacmc): + // z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_loan(session), z_keyexpr("test/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)) { + // TODO(sashacmc): + // z_keyexpr_t ping = z_keyexpr_unchecked("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_keyexpr("test/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..98d63fad6 100644 --- a/examples/unix/c11/z_pub.c +++ b/examples/unix/c11/z_pub.c @@ -66,24 +66,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; @@ -94,8 +95,8 @@ int main(int argc, char **argv) { sleep(3); } 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; + if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(keyexpr), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return -1; } @@ -106,7 +107,8 @@ int main(int argc, char **argv) { 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(); + z_publisher_put_options_t options; + z_publisher_put_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); } @@ -114,8 +116,8 @@ int main(int argc, char **argv) { 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)); diff --git a/examples/unix/c11/z_pub_st.c b/examples/unix/c11/z_pub_st.c index deb9e1f60..aeebe6c95 100644 --- a/examples/unix/c11/z_pub_st.c +++ b/examples/unix/c11/z_pub_st.c @@ -63,25 +63,26 @@ 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; + if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(keyexpr), 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..ed25fe188 100644 --- a/examples/unix/c11/z_pub_thr.c +++ b/examples/unix/c11/z_pub_thr.c @@ -30,28 +30,29 @@ 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; + if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(keyexpr), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); exit(-1); } @@ -62,8 +63,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..79d85ac6f 100644 --- a/examples/unix/c11/z_pull.c +++ b/examples/unix/c11/z_pull.c @@ -52,42 +52,44 @@ 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; + if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), 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_str_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr); + printf(">> [Subscriber] Pulled ('%s': '%.*s')\n", z_str_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 +101,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..fe5dc6023 100644 --- a/examples/unix/c11/z_put.c +++ b/examples/unix/c11/z_put.c @@ -61,41 +61,43 @@ 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_owned_keyexpr_t ke; + if (z_declare_keyexpr(&ke, z_loan(s), z_keyexpr(keyexpr)) < 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; } printf("Putting Data ('%s': '%s')...\n", keyexpr, value); - z_put_options_t options = z_put_options_default(); + z_put_options_t options; + z_put_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); #if Z_FEATURE_ATTACHMENT == 1 z_owned_bytes_map_t map = z_bytes_map_new(); @@ -112,8 +114,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..6fff4a6f5 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -31,15 +31,17 @@ 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_str_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + // TODO(sashacmc): + // 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_str_data(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); + // } #if Z_FEATURE_ATTACHMENT == 1 z_attachment_t attachment = z_query_attachment(query); if (z_attachment_check(&attachment)) { @@ -48,7 +50,8 @@ void query_handler(const z_query_t *query, void *ctx) { } #endif - z_query_reply_options_t options = z_query_reply_options_default(); + z_query_reply_options_t options; + z_query_reply_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); #if Z_FEATURE_ATTACHMENT == 1 @@ -107,39 +110,42 @@ 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)) { - printf("%s is not a valid key expression", keyexpr); - return -1; - } + // TODO(sashacmc): + // z_keyexpr_t ke = z_keyexpr(keyexpr); + // if (!z_check(ke)) { + // printf("%s is not a valid key expression", keyexpr); + // return -1; + // } printf("Creating Queryable on '%s'...\n", keyexpr); - z_owned_closure_query_t callback = z_closure(query_handler); - z_owned_queryable_t qable = z_declare_queryable(z_loan(s), ke, z_move(callback), NULL); - if (!z_check(qable)) { + 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_keyexpr(keyexpr), z_move(callback), NULL) < 0) { printf("Unable to create queryable.\n"); return -1; } @@ -155,8 +161,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..8c0e220d6 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -57,56 +57,63 @@ 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)) { - printf("%s is not a valid key expression", keyexpr); - return -1; - } + // TODO(sashacmc): + // z_keyexpr_t ke = z_keyexpr(keyexpr); + // if (!z_check(ke)) { + // printf("%s is not a valid key expression", keyexpr); + // return -1; + // } printf("Creating Queryable on '%s'...\n", keyexpr); - z_owned_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_keyexpr(keyexpr), 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); - } - z_query_reply_options_t options = z_query_reply_options_default(); + const z_loaned_query_t *q = z_loan(query); + z_owned_str_t keystr; + z_keyexpr_to_string(z_query_keyexpr(q), &keystr); + // TODO(sashacmc): + // 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_str_data(z_loan(keystr)), (int)pred.len, + // pred.start); if (payload_value.payload.len > 0) { + // printf(" with value '%.*s'\n", (int)payload_value.payload.len, payload_value.payload.start); + //} + z_query_reply_options_t options; + z_query_reply_options_default(&options); 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(q, z_keyexpr(keyexpr), (const unsigned char *)value, strlen(value), &options); z_drop(z_move(keystr)); z_drop(z_move(query)); } @@ -115,8 +122,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..beba299ee 100644 --- a/examples/unix/c11/z_scout.c +++ b/examples/unix/c11/z_scout.c @@ -43,24 +43,26 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { void fprintlocators(FILE *stream, const z_str_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); - fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { - fprintf(stream, ", "); - } - } + (void)locs; + // TODO(sashacmc): + // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + // fprintf(stream, "\""); + // fprintf(stream, "%s", *z_str_array_get(locs, i)); + // fprintf(stream, "\""); + // if (i < z_str_array_len(locs) - 1) { + // fprintf(stream, ", "); + // } + //} fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +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 +89,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..a79a05583 100644 --- a/examples/unix/c11/z_sub.c +++ b/examples/unix/c11/z_sub.c @@ -32,12 +32,12 @@ 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_str_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_str_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 +86,35 @@ 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; + if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } @@ -128,8 +130,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..032598777 100644 --- a/examples/unix/c11/z_sub_channel.c +++ b/examples/unix/c11/z_sub_channel.c @@ -44,51 +44,53 @@ 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; + if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), 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_str_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_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..3a67c2805 100644 --- a/examples/unix/c11/z_sub_st.c +++ b/examples/unix/c11/z_sub_st.c @@ -23,12 +23,12 @@ 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_str_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_str_data(z_loan(keystr)), (int)payload->len, payload->start); z_drop(z_move(keystr)); msg_nb++; } @@ -70,26 +70,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; } - 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; + if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), 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..5a1c8a685 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,34 @@ 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; + if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL) < 0) { printf("Unable to create subscriber.\n"); exit(-1); } @@ -106,8 +108,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..2d0d307ce 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -32,9 +32,11 @@ void reply_dropper(void *ctx) { void reply_handler(z_owned_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); + const z_loaned_sample_t *sample = z_reply_ok(reply); + z_owned_str_t keystr; + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(">> Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len, payload->start); z_str_drop(z_str_move(&keystr)); } else { printf(">> Received an error\n"); @@ -81,42 +83,45 @@ 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)) { + const z_loaned_keyexpr_t *ke = z_keyexpr(keyexpr); + if (!z_keyexpr_is_initialized(ke)) { 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_owned_closure_reply_t callback = z_closure_reply(reply_handler, reply_dropper, NULL); + z_owned_closure_reply_t callback; + z_closure_reply(&callback, reply_handler, reply_dropper, NULL); if (z_get(z_session_loan(&s), ke, "", z_closure_reply_move(&callback), &opts) < 0) { printf("Unable to send query.\n"); return -1; @@ -125,8 +130,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..52b966795 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)) { + // TODO(sashacmc): + // z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + z_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_session_loan(&session), z_keyexpr("test/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)) { + // TODO(sashacmc): + // z_keyexpr_t pong = z_keyexpr_unchecked("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_keyexpr("test/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..97a86c3ed 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,35 @@ 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)) { + // TODO(sashacmc): z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_session_loan(&session), z_keyexpr("test/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)) { + // TODO(sashacmc): z_keyexpr_t ping = z_keyexpr_unchecked("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_keyexpr("test/ping"), z_closure_sample_move(&respond), + NULL) < 0) { printf("Unable to declare subscriber for key expression.\n"); return -1; } @@ -69,8 +72,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..74376cd2f 100644 --- a/examples/unix/c99/z_pub.c +++ b/examples/unix/c99/z_pub.c @@ -62,32 +62,33 @@ 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; + if (z_declare_publisher(&pub, z_session_loan(&s), z_keyexpr(keyexpr), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return -1; } @@ -99,14 +100,15 @@ int main(int argc, char **argv) { 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(); + z_publisher_put_options_t options; + z_publisher_put_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); 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..f576c8b07 100644 --- a/examples/unix/c99/z_pub_st.c +++ b/examples/unix/c99/z_pub_st.c @@ -62,25 +62,26 @@ 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; + if (z_declare_publisher(&pub, z_session_loan(&s), z_keyexpr(keyexpr), 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..77301516a 100644 --- a/examples/unix/c99/z_pull.c +++ b/examples/unix/c99/z_pull.c @@ -20,10 +20,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_str_loan(&keystr), (int)sample->payload.len, +// z_owned_str_t keystr; +// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)sample->payload.len, // sample->payload.start); // z_str_drop(z_str_move(&keystr)); // } @@ -53,27 +54,29 @@ 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 = @@ -99,8 +102,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..009803d8d 100644 --- a/examples/unix/c99/z_put.c +++ b/examples/unix/c99/z_put.c @@ -57,41 +57,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; } 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_owned_keyexpr_t ke; + if (z_declare_keyexpr(&ke, z_session_loan(&s), z_keyexpr(keyexpr)) < 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; } printf("Putting Data ('%s': '%s')...\n", keyexpr, value); - z_put_options_t options = z_put_options_default(); + z_put_options_t options; + z_put_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); 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"); @@ -99,8 +101,8 @@ int main(int argc, char **argv) { // 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..ee38ab56a 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -22,12 +22,16 @@ 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(); + z_owned_str_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + z_view_str_t pred; + z_query_parameters(query, &pred); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_str_loan(&keystr)), + (int)z_view_str_loan(&pred)->len, z_view_str_loan(&pred)->val); + z_query_reply_options_t options; + z_query_reply_options_default(&options); 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)); @@ -68,39 +72,42 @@ 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)) { + const z_loaned_keyexpr_t *ke = z_keyexpr(keyexpr); + if (!z_keyexpr_is_initialized(ke)) { 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), ke, z_closure_query_move(&callback), NULL) < 0) { printf("Unable to create queryable.\n"); return -1; } @@ -113,8 +120,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..61206287f 100644 --- a/examples/unix/c99/z_scout.c +++ b/examples/unix/c99/z_scout.c @@ -42,24 +42,26 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { void fprintlocators(FILE *stream, const z_str_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); - fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { - fprintf(stream, ", "); - } - } + (void)locs; + // TODO(sashacmc): + // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + // fprintf(stream, "\""); + // fprintf(stream, "%s", *z_str_array_get(locs, i)); + // fprintf(stream, "\""); + // if (i < z_str_array_len(locs) - 1) { + // fprintf(stream, ", "); + // } + //} fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +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 +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_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..37b0699e0 100644 --- a/examples/unix/c99/z_sub.c +++ b/examples/unix/c99/z_sub.c @@ -20,12 +20,13 @@ #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_owned_str_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_str_data(z_str_loan(&keystr)), (int)payload->len, + payload->start); z_str_drop(z_str_move(&keystr)); } @@ -62,34 +63,37 @@ 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; + if (z_declare_subscriber(&sub, z_session_loan(&s), z_keyexpr(keyexpr), z_closure_sample_move(&callback), NULL) < + 0) { printf("Unable to declare subscriber.\n"); return -1; } @@ -102,8 +106,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..03f1cbf35 100644 --- a/examples/unix/c99/z_sub_st.c +++ b/examples/unix/c99/z_sub_st.c @@ -23,12 +23,13 @@ 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_owned_str_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_str_data(z_str_loan(&keystr)), (int)payload->len, + payload->start); z_str_drop(z_str_move(&keystr)); msg_nb++; } @@ -70,27 +71,30 @@ 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; + if (z_declare_subscriber(&sub, z_session_loan(&s), z_keyexpr(keyexpr), 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..a20d4a4bd 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -31,9 +31,11 @@ void reply_dropper(void *ctx) { void reply_handler(z_owned_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_str_t keystr; + z_keyexpr_to_string(sample.keyexpr, &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); z_drop(z_move(keystr)); } else { printf(">> Received an error\n"); @@ -50,20 +52,21 @@ 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; @@ -77,11 +80,13 @@ int main(int argc, char **argv) { 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_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); + z_owned_closure_reply_t callback; + z_closure(&callback, reply_handler, reply_dropper); if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { printf("Unable to send query.\n"); return -1; @@ -90,8 +95,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..850cea78a 100644 --- a/examples/windows/z_ping.c +++ b/examples/windows/z_ping.c @@ -63,30 +63,32 @@ 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_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_loan(session), 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_owned_closure_sample_t respond; + z_closure(&respond, callback, drop, NULL); + z_owned_subscriber_t sub; + if (z_declare_subscriber(&sub, z_loan(session), pong, z_move(respond), NULL) < 0) { printf("Unable to declare subscriber for key expression.\n"); return -1; } @@ -122,8 +124,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..240f03da6 100644 --- a/examples/windows/z_pong.c +++ b/examples/windows/z_pong.c @@ -33,30 +33,32 @@ 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_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_loan(session), 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_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), ping, z_move(respond), NULL) < 0) { printf("Unable to declare subscriber for key expression.\n"); return -1; } @@ -66,8 +68,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..29f32efad 100644 --- a/examples/windows/z_pub.c +++ b/examples/windows/z_pub.c @@ -27,29 +27,30 @@ 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 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; + if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(keyexpr), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return -1; } @@ -61,7 +62,8 @@ int main(int argc, char **argv) { 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(); + z_publisher_put_options_t options; + z_publisher_put_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); } @@ -69,8 +71,8 @@ int main(int argc, char **argv) { 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)); diff --git a/examples/windows/z_pub_st.c b/examples/windows/z_pub_st.c index 6d6f59d87..2b7c2d76e 100644 --- a/examples/windows/z_pub_st.c +++ b/examples/windows/z_pub_st.c @@ -29,22 +29,23 @@ 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; + if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(keyexpr), 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..c8beb623a 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_str_t keystr; +// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)sample->payload.len, // sample->payload.start); // z_drop(z_move(keystr)); // } @@ -33,27 +34,29 @@ 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); @@ -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..a9596286a 100644 --- a/examples/windows/z_put.c +++ b/examples/windows/z_put.c @@ -27,38 +27,40 @@ 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_owned_keyexpr_t ke; + if (z_declare_keyexpr(&ke, z_loan(s), z_keyexpr(keyexpr)) < 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; } printf("Putting Data ('%s': '%s')...\n", keyexpr, value); - z_put_options_t options = z_put_options_default(); + z_put_options_t options; + z_put_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)value, strlen(value), &options) < 0) { printf("Oh no! Put has failed...\n"); @@ -66,8 +68,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/windows/z_queryable.c b/examples/windows/z_queryable.c index ea504d0e7..1fde0df81 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -21,16 +21,18 @@ 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_owned_str_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); 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); + printf(" >> [Queryable handler] Received Query '%s?%.*s'\n", z_str_data(z_loan(keystr)), (int)pred.len, pred.start); if (payload_value.payload.len > 0) { printf(" with value '%.*s'\n", (int)payload_value.payload.len, payload_value.payload.start); } - z_query_reply_options_t options = z_query_reply_options_default(); + z_query_reply_options_t options; + z_query_reply_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); z_query_reply(query, z_keyexpr(keyexpr), (const unsigned char *)value, strlen(value), &options); z_drop(z_move(keystr)); @@ -41,20 +43,21 @@ 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; @@ -67,9 +70,10 @@ int main(int argc, char **argv) { } 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), ke, z_move(callback), NULL) < 0) { printf("Unable to create queryable.\n"); return -1; } @@ -82,8 +86,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..ea6a69130 100644 --- a/examples/windows/z_scout.c +++ b/examples/windows/z_scout.c @@ -85,8 +85,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..4b1441228 100644 --- a/examples/windows/z_sub.c +++ b/examples/windows/z_sub.c @@ -19,12 +19,12 @@ #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_str_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_str_data(z_loan(keystr)), (int)payload->len, payload->start); z_drop(z_move(keystr)); } @@ -35,30 +35,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)); 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; + if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } @@ -71,8 +73,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..04ed79fe2 100644 --- a/examples/windows/z_sub_st.c +++ b/examples/windows/z_sub_st.c @@ -22,12 +22,12 @@ 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_str_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_str_data(z_loan(keystr)), (int)payload->len, payload->start); z_drop(z_move(keystr)); msg_nb++; @@ -40,23 +40,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; } - 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; + if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), 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..2dbe27234 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -35,9 +35,11 @@ void reply_dropper(void *ctx) { printf(" >> Received query final notification\n" void reply_handler(z_owned_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_str_t keystr; + z_keyexpr_to_string(sample.keyexpr, &keystr); + const z_loaned_bytes_t *payload = z_sample_payload(sample); + printf(" >> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); z_drop(z_move(keystr)); } else { printf(" >> Received an error\n"); @@ -48,31 +50,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); 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); + z_owned_closure_reply_t callback; + z_closure(&callback, reply_handler, reply_dropper); if (z_get(z_loan(s), z_keyexpr(KEYEXPR), "", z_move(callback), &opts) < 0) { printf("Unable to send query.\n"); exit(-1); @@ -81,8 +86,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..b96e7ba74 100644 --- a/examples/zephyr/z_pub.c +++ b/examples/zephyr/z_pub.c @@ -36,28 +36,29 @@ 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_owned_publisher_t pub; + if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(KEYEXPR), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); exit(-1); } @@ -75,8 +76,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..f739edd26 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_str_t keystr; +// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); +// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), +// (int)sample->payload.len, // sample->payload.start); // z_drop(z_move(keystr)); // } @@ -41,27 +43,29 @@ 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); @@ -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..5455aef8f 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -31,11 +31,12 @@ #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_owned_str_t keystr; + z_keyexpr_to_string(z_query_keyexpr(query), &keystr); z_bytes_t pred = z_query_parameters(query); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_loan(keystr), (int)pred.len, pred.start); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)pred.len, pred.start); z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL); z_drop(z_move(keystr)); } @@ -44,30 +45,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); // 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; + if (z_declare_queryable(&qable, z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL) < 0) { printf("Unable to declare queryable.\n"); exit(-1); } @@ -82,8 +85,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..718d6675a 100644 --- a/examples/zephyr/z_scout.c +++ b/examples/zephyr/z_scout.c @@ -81,8 +81,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..eddd62b14 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_str_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_str_data(z_loan(keystr)), (int)payload->len, + payload->start); z_drop(z_move(keystr)); } @@ -41,29 +42,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 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; + if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); exit(-1); } @@ -77,8 +80,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/handlers.h b/include/zenoh-pico/api/handlers.h index 43bd1db21..b955d2a4e 100644 --- a/include/zenoh-pico/api/handlers.h +++ b/include/zenoh-pico/api/handlers.h @@ -25,11 +25,11 @@ // -- 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); @@ -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,22 +92,22 @@ 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) diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 5cbadcb54..35d13ed14 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -42,10 +42,25 @@ z_owned_reply_t : z_reply_loan, \ z_owned_hello_t : z_hello_loan, \ z_owned_str_t : z_str_loan, \ + z_view_str_t : z_view_str_loan, \ z_owned_str_array_t : z_str_array_loan, \ z_owned_sample_t : z_sample_loan, \ z_owned_query_t : z_query_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_str_t : z_str_loan_mut, \ + z_owned_str_array_t : z_str_array_loan_mut, \ + z_owned_sample_t : z_sample_loan_mut, \ + z_owned_query_t : z_query_loan_mut \ + )(&x) /** * Defines a generic function for dropping any of the ``z_owned_X_t`` types. * @@ -92,7 +107,7 @@ #define z_check(x) _Generic((x), \ z_owned_keyexpr_t : z_keyexpr_check, \ - z_keyexpr_t : z_keyexpr_is_initialized, \ + z_view_keyexpr_t : z_keyexpr_is_initialized, \ z_value_t : z_value_is_initialized, \ z_owned_config_t : z_config_check, \ z_owned_scouting_config_t : z_scouting_config_check, \ @@ -104,7 +119,7 @@ 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_bytes_t : z_bytes_check, \ z_owned_sample_t : z_sample_check, \ z_owned_query_t : z_query_check \ )(&x) @@ -158,9 +173,9 @@ 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) /** @@ -191,7 +206,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,6 +214,7 @@ 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, \ @@ -210,12 +226,16 @@ 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 \ - )()) + )(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. @@ -232,6 +252,8 @@ #else +// TODO(sashacmc): z_loan_mut + // clang-format off template struct zenoh_loan_type { typedef T type; }; template inline typename zenoh_loan_type::type z_loan(const T&); @@ -295,23 +317,23 @@ template<> inline void z_drop(z_owned_query_fifo_channel_t* v) { z_owned_query_f 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(); } +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_str_t* v) { z_str_null(v); } +inline void z_null(z_owned_closure_sample_t* v) { z_closure_sample_null(v); } +inline void z_null(z_owned_clusure_owned_sample_t* v) { z_closure_owned_sample_null(v); } +inline void z_null(z_owned_closure_query_t* v) { z_closure_query_null(v); } +inline void z_null(z_owned_clusure_owned_query_t* v) { z_closure_owned_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); } @@ -343,9 +365,15 @@ 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..5cfb25aaf 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -34,6 +34,7 @@ extern "C" { /********* Data Types Handlers *********/ #define z_bytes_wrap _z_bytes_wrap +// TODO(sashacmc): docs /** * Constructs a :c:type:`z_string_t` departing from a ``const char *``. * It is a loaned key expression that aliases ``value``. @@ -44,7 +45,7 @@ extern "C" { * Returns: * The :c:type:`z_string_t` corresponding to the given string. */ -z_string_t z_string_make(const char *value); +int8_t z_view_str_wrap(z_view_str_t *str, const char *value); /** * Constructs a :c:type:`z_keyexpr_t` departing from a string. @@ -57,7 +58,7 @@ z_string_t z_string_make(const char *value); * Returns: * The :c:type:`z_keyexpr_t` corresponding to the given string. */ -z_keyexpr_t z_keyexpr(const char *name); +int8_t z_view_keyexpr_new(z_view_keyexpr_t *keyexpr, const char *name); /** * Constructs a :c:type:`z_keyexpr_t` departing from a string. @@ -70,7 +71,10 @@ z_keyexpr_t z_keyexpr(const char *name); * Returns: * The :c:type:`z_keyexpr_t` corresponding to the given string. */ -z_keyexpr_t z_keyexpr_unchecked(const char *name); +// TODO(sashacmc): +z_owned_keyexpr_t z_keyexpr_unchecked(const char *name); + +int8_t z_view_keyexpr_unchecked(z_view_keyexpr_t *keyexpr, const char *name); /** * Get null-terminated string departing from a :c:type:`z_keyexpr_t`. @@ -85,7 +89,7 @@ z_keyexpr_t z_keyexpr_unchecked(const char *name); * Returns: * The :c:type:`z_owned_str_t` containing key expression string representation if it's possible */ -z_owned_str_t z_keyexpr_to_string(z_keyexpr_t keyexpr); +void z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_str_t *s); /** * Returns the key expression's internal string by aliasing it. @@ -94,9 +98,9 @@ z_owned_str_t z_keyexpr_to_string(z_keyexpr_t keyexpr); * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` * * Returns: - * The :c:type:`z_bytes_t` pointing to key expression string representation if it's possible + * The :c:type:`z_loaned_bytes_t*` pointing to key expression string representation if it's possible */ -z_bytes_t z_keyexpr_as_bytes(z_keyexpr_t keyexpr); +z_loaned_bytes_t *z_keyexpr_as_bytes(z_loaned_keyexpr_t *keyexpr); /** * Indicates if the key expression has been declared but don't guarantee it's still in session. @@ -110,20 +114,20 @@ z_bytes_t z_keyexpr_as_bytes(z_keyexpr_t keyexpr); * Returns: * Returns ``true`` if the keyexpr was declared or ``false`` otherwise. */ -_Bool zp_keyexpr_was_declared(const z_keyexpr_t *keyexpr); +_Bool zp_keyexpr_was_declared(const z_loaned_keyexpr_t *keyexpr); /** - * Constructs a null-terminated string departing from a :c:type:`z_keyexpr_t` for a given :c:type:`z_session_t`. + * Constructs a null-terminated string departing from a :c:type:`z_keyexpr_t` for a given :c:type:`z_loaned_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. + * zs: A loaned instance of the the :c:type:`z_loaned_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_str_t *str); /** * Checks if a given keyexpr is valid. @@ -134,7 +138,7 @@ z_owned_str_t zp_keyexpr_resolve(z_session_t zs, z_keyexpr_t keyexpr); * Returns: * Returns ``true`` if the keyexpr is valid, or ``false`` otherwise. */ -_Bool z_keyexpr_is_initialized(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. @@ -201,7 +205,7 @@ int8_t zp_keyexpr_canonize_null_terminated(char *start); * 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. */ -int8_t z_keyexpr_includes(z_keyexpr_t l, z_keyexpr_t r); +int8_t 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. @@ -229,7 +233,7 @@ int8_t zp_keyexpr_includes_null_terminated(const char *l, const char *r); * 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. */ -int8_t z_keyexpr_intersects(z_keyexpr_t l, z_keyexpr_t r); +int8_t z_keyexpr_intersects(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r); /** * Check if a given keyexpr intersects with another keyexpr. @@ -257,7 +261,7 @@ int8_t zp_keyexpr_intersect_null_terminated(const char *l, const char *r); * Returns ``0`` if both ``l`` and ``r`` are equal. Otherwise, it returns a ``-1``, or other ``negative value`` for * errors. */ -int8_t z_keyexpr_equals(z_keyexpr_t l, z_keyexpr_t r); +int8_t z_keyexpr_equals(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r); /** * Check if a two keyexprs are equal. @@ -274,6 +278,7 @@ int8_t z_keyexpr_equals(z_keyexpr_t l, z_keyexpr_t r); */ int8_t zp_keyexpr_equals_null_terminated(const char *l, const char *r); +// TODO(sashacmc): update comment /** * Return a new, zenoh-allocated, empty configuration. * It consists in an empty set of properties for zenoh session configuration. @@ -294,8 +299,9 @@ int8_t zp_keyexpr_equals_null_terminated(const char *l, const char *r); * Returns: * Returns a new, zenoh-allocated, empty configuration. */ -z_owned_config_t z_config_new(void); +void z_config_new(z_owned_config_t *config); +// TODO(sashacmc): update comment /** * Return a new, zenoh-allocated, default configuration. * It consists in a default set of properties for zenoh session configuration. @@ -316,7 +322,7 @@ z_owned_config_t z_config_new(void); * Returns: * Returns a new, zenoh-allocated, default configuration. */ -z_owned_config_t z_config_default(void); +void z_config_default(z_owned_config_t *config); /** * Gets the property with the given integer key from the configuration. @@ -328,7 +334,7 @@ z_owned_config_t z_config_default(void); * Returns: * Returns the property with the given integer key from the configuration. */ -const char *zp_config_get(z_config_t config, uint8_t key); +const char *zp_config_get(const z_loaned_config_t *config, uint8_t key); /** * Inserts or replaces the property with the given integer key in the configuration. @@ -341,7 +347,7 @@ const char *zp_config_get(z_config_t config, uint8_t key); * Returns: * Returns ``0`` if the insertion is successful, or a ``negative value`` otherwise. */ -int8_t zp_config_insert(z_config_t config, uint8_t key, z_string_t value); +int8_t zp_config_insert(z_loaned_config_t *config, uint8_t key, const char *value); /** * Return a new, zenoh-allocated, default scouting configuration. @@ -364,7 +370,7 @@ int8_t zp_config_insert(z_config_t config, uint8_t key, z_string_t value); * Returns: * Returns a new, zenoh-allocated, default scouting configuration. */ -z_owned_scouting_config_t z_scouting_config_default(void); +void z_scouting_config_default(z_owned_scouting_config_t *sc); /** * Return a new, zenoh-allocated, scouting configuration extracted from a :c:type:`z_owned_config_t`. @@ -390,7 +396,7 @@ z_owned_scouting_config_t z_scouting_config_default(void); * Returns: * Returns a new, zenoh-allocated, default scouting configuration. */ -z_owned_scouting_config_t z_scouting_config_from(z_config_t config); +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. @@ -402,7 +408,7 @@ z_owned_scouting_config_t z_scouting_config_from(z_config_t config); * Returns: * Returns the property with the given integer key from the configuration. */ -const char *zp_scouting_config_get(z_scouting_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. @@ -415,7 +421,7 @@ const char *zp_scouting_config_get(z_scouting_config_t config, uint8_t key); * Returns: * Returns ``0`` if the insertion is successful, or a ``negative value`` otherwise. */ -int8_t zp_scouting_config_insert(z_scouting_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); /** * Constructs a :c:type:`z_encoding_t`. @@ -510,9 +516,10 @@ z_query_consolidation_t z_query_consolidation_none(void); * 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. + * Returns the value selector wrapped as a :c:type:`z_loaned_bytes_t*`, since value selector is a user-defined + * representation. */ -z_bytes_t z_query_parameters(const z_query_t *query); +void z_query_parameters(const z_loaned_query_t *query, z_view_str_t *parameters); /** * Get a query's payload value by aliasing it. @@ -524,7 +531,7 @@ z_bytes_t z_query_parameters(const z_query_t *query); * Returns: * Returns the payload wrapped as a :c:type:`z_value_t`, since payload value is a user-defined representation. */ -z_value_t z_query_value(const z_query_t *query); +z_value_t z_query_value(const z_loaned_query_t *query); #if Z_FEATURE_ATTACHMENT == 1 /** @@ -537,7 +544,7 @@ z_value_t z_query_value(const z_query_t *query); * Returns: * Returns the attachment wrapped as a :c:type:`z_attachment_t`, since attachment is a user-defined representation. */ -z_attachment_t z_query_attachment(const z_query_t *query); +z_attachment_t z_query_attachment(const z_loaned_query_t *query); #endif /** @@ -549,8 +556,9 @@ z_attachment_t z_query_attachment(const z_query_t *query); * Returns: * Returns the :c:type:`z_keyexpr_t` associated to the query. */ -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); +// TODO(sashacmc): update comment for all "owned" /** * Return a new sample closure. * It consists on a structure that contains all the elements for stateful, memory-leak-free callbacks. @@ -576,7 +584,8 @@ z_keyexpr_t z_query_keyexpr(const z_query_t *query); * Returns: * Returns a new 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. @@ -599,8 +608,8 @@ z_owned_closure_sample_t z_closure_sample(_z_data_handler_t call, _z_dropper_han * Returns: * Returns a new 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. @@ -627,7 +636,8 @@ z_owned_closure_owned_sample_t z_closure_owned_sample(_z_owned_sample_handler_t * Returns: * Returns a new 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. @@ -650,8 +660,8 @@ z_owned_closure_query_t z_closure_query(_z_queryable_handler_t call, _z_dropper_ * Returns: * Returns a new 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. @@ -678,7 +688,8 @@ z_owned_closure_owned_query_t z_closure_owned_query(_z_owned_query_handler_t cal * Returns: * Returns a new 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_owned_reply_handler_t call, _z_dropper_handler_t drop, + void *context); /** * Return a new hello closure. @@ -705,7 +716,8 @@ z_owned_closure_reply_t z_closure_reply(z_owned_reply_handler_t call, _z_dropper * Returns: * Returns a new hello 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_hello(z_owned_closure_hello_t *closure, z_owned_hello_handler_t call, _z_dropper_handler_t drop, + void *context); /** * Return a new zid closure. @@ -732,36 +744,38 @@ z_owned_closure_hello_t z_closure_hello(z_owned_hello_handler_t call, _z_dropper * Returns: * Returns a new zid 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); \ + void z_##name##_clone(ownedtype *obj, const loanedtype *src); \ + void z_##name##_drop(ownedtype *obj); \ + void z_##name##_null(ownedtype *obj); + +_OWNED_FUNCTIONS(z_loaned_str_t, z_owned_str_t, str) +_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_str_array_t, z_owned_str_array_t, str_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) #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) @@ -771,6 +785,27 @@ _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) +#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_str_t, z_view_str_t, str) + +// Gets internal value from refcountered type (e.g. z_loaned_session_t, z_query_t) +#define _Z_RC_IN_VAL(arg) ((arg)->in->val) + +// Gets internal value from refcountered owned type (e.g. z_owned_session_t, z_owned_query_t) +#define _Z_OWNED_RC_IN_VAL(arg) ((arg)->_rc.in->val) + +// TODO(sashacmc): remove this VERY BAD workaround (it is only to allow compilaton)!!! +z_loaned_keyexpr_t *z_keyexpr(const char *name); + +// TODO(sashacmc): comments, docs, etc. +const z_loaned_sample_t *z_sample_loan(const z_owned_sample_t *sample); +const char *z_str_data(const z_loaned_str_t *str); + /************* Primitives **************/ /** * Looks for other Zenoh-enabled entities like routers and/or peers. @@ -807,7 +842,7 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb * 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``. */ -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. @@ -827,13 +862,13 @@ int8_t z_close(z_owned_session_t *zs); * and to be dropped before this function exits. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` to inquiry. + * zs: A loaned instance of the the :c:type:`z_loaned_session_t` to inquiry. * callback: A moved instance of :c:type:`z_owned_closure_zid_t` containing the callbacks to be called. * * Returns: * Returns ``0`` if the info is successful triggered, or a ``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. @@ -842,27 +877,27 @@ int8_t z_info_peers_zid(const z_session_t zs, z_owned_closure_zid_t *callback); * and to be dropped before this function exits. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` to inquiry. + * zs: A loaned instance of the the :c:type:`z_loaned_session_t` to inquiry. * callback: A moved instance of :c:type:`z_owned_closure_zid_t` containing the callbacks to be called. * * Returns: * Returns ``0`` if the info is successful triggered, or a ``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. * - * Unless the :c:type:`z_session_t` is invalid, that ID is guaranteed to be non-zero. + * Unless the :c:type:`z_loaned_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. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` to inquiry. + * zs: A loaned instance of the the :c:type:`z_loaned_session_t` to inquiry. * * Returns: - * Returns the local Zenoh ID of the given :c:type:`z_session_t`. + * Returns the local Zenoh ID of the given :c:type:`z_loaned_session_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. @@ -872,9 +907,9 @@ z_id_t z_info_zid(const z_session_t zs); * sample: Pointer to the sample to get the keyexpr from. * * Returns: - * Returns the keyexpr wrapped as a :c:type:`z_keyexpr_t`. + * Returns 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. @@ -884,9 +919,9 @@ z_keyexpr_t z_sample_keyexpr(const z_sample_t *sample); * sample: Pointer to the sample to get the payload from. * * Returns: - * Returns the payload wrapped as a :c:type:`z_bytes_t`. + * Returns 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. @@ -898,7 +933,7 @@ z_bytes_t z_sample_payload(const z_sample_t *sample); * Returns: * Returns 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. @@ -910,7 +945,7 @@ z_timestamp_t z_sample_timestamp(const z_sample_t *sample); * Returns: * Returns the encoding wrapped as a :c:type:`z_encoding_t`. */ -z_encoding_t z_sample_encoding(const z_sample_t *sample); +z_encoding_t z_sample_encoding(const z_loaned_sample_t *sample); /** * Get a sample's kind by aliasing it. @@ -922,7 +957,7 @@ z_encoding_t z_sample_encoding(const z_sample_t *sample); * Returns: * Returns 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. @@ -934,7 +969,7 @@ z_sample_kind_t z_sample_kind(const z_sample_t *sample); * Returns: * Returns 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 /** @@ -947,7 +982,7 @@ z_qos_t z_sample_qos(const z_sample_t *sample); * Returns: * Returns 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 /** @@ -956,7 +991,8 @@ z_attachment_t z_sample_attachment(const z_sample_t *sample); * Returns: * Returns the constructed :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. @@ -964,13 +1000,13 @@ z_put_options_t z_put_options_default(void); * Returns: * Returns the constructed :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. + * zs: A loaned instance of the the :c:type:`z_loaned_session_t` through where data will be put. * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to put. * payload: Pointer to the data to put. * payload_len: The length of the ``payload``. @@ -979,21 +1015,21 @@ z_delete_options_t z_delete_options_default(void); * Returns: * Returns ``0`` if the put operation is successful, or a ``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. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` through where data will be put. + * zs: A loaned instance of the the :c:type:`z_loaned_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. * * Returns: * Returns ``0`` if the delete operation is successful, or a ``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. @@ -1001,7 +1037,7 @@ int8_t z_delete(z_session_t zs, z_keyexpr_t keyexpr, const z_delete_options_t *o * Returns: * Returns the constructed :c:type:`z_publisher_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. @@ -1023,7 +1059,7 @@ z_publisher_options_t z_publisher_options_default(void); * 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. + * zs: A loaned instance of the the :c:type:`z_loaned_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. * @@ -1031,7 +1067,8 @@ z_publisher_options_t z_publisher_options_default(void); * A :c:type:`z_owned_publisher_t` with either a valid publisher or a failing publisher. * Should the publisher be invalid, ``z_check(val)`` ing the returned value will return ``false``. */ -z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, 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`. @@ -1044,7 +1081,7 @@ z_owned_publisher_t z_declare_publisher(z_session_t zs, z_keyexpr_t keyexpr, con */ int8_t z_undeclare_publisher(z_owned_publisher_t *pub); -z_owned_keyexpr_t z_publisher_keyexpr(z_publisher_t publisher); +z_owned_keyexpr_t z_publisher_keyexpr(z_loaned_publisher_t *publisher); /** * Constructs the default values for the put operation via a publisher entity. @@ -1052,7 +1089,7 @@ z_owned_keyexpr_t z_publisher_keyexpr(z_publisher_t publisher); * Returns: * Returns the constructed :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. @@ -1060,32 +1097,32 @@ z_publisher_put_options_t z_publisher_put_options_default(void); * Returns: * Returns the constructed :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. * * Parameters: - * pub: A loaned instance of :c:type:`z_publisher_t` from where to put the data. + * pub: A loaned instance of :c:type:`z_loaned_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. * * Returns: * Returns ``0`` if the put operation is successful, or a ``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. * * Parameters: - * pub: A loaned instance of :c:type:`z_publisher_t` from where to delete the data. + * pub: A loaned instance of :c:type:`z_loaned_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. * * Returns: * Returns ``0`` if the delete operation is successful, or a ``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 @@ -1095,13 +1132,13 @@ int8_t z_publisher_delete(const z_publisher_t pub, const z_publisher_delete_opti * Returns: * Returns the constructed :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. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` through where data will be put. + * zs: A loaned instance of the the :c:type:`z_loaned_session_t` through where data will be put. * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to put. * 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. @@ -1110,8 +1147,8 @@ z_get_options_t z_get_options_default(void); * Returns: * Returns ``0`` if the put operation is successful, or a ``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, z_loaned_keyexpr_t *keyexpr, const char *parameters, + z_owned_closure_reply_t *callback, const z_get_options_t *options); /** * Checks if the queryable answered with an OK, which allows this value to be treated as a sample. * @@ -1138,7 +1175,7 @@ _Bool z_reply_is_ok(const z_owned_reply_t *reply); * Returns: * Returns the :c:type:`z_loaned_sample_t` wrapped in the query reply. */ -z_loaned_sample_t z_reply_ok(const z_owned_reply_t *reply); +const z_loaned_sample_t *z_reply_ok(const z_owned_reply_t *reply); /** * Yields the contents of the reply by asserting it indicates a failure. @@ -1161,7 +1198,7 @@ z_value_t z_reply_err(const z_owned_reply_t *reply); * Returns: * Returns the constructed :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. @@ -1182,7 +1219,7 @@ z_queryable_options_t z_queryable_options_default(void); * 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 subscriber. + * zs: A loaned instance of the the :c:type:`z_loaned_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 @@ -1190,10 +1227,11 @@ z_queryable_options_t z_queryable_options_default(void); * * 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``. + * Should the queryable be invalid,_loaned ``z_check(val)`` ing the returned value will return ``false``. */ -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`. @@ -1212,7 +1250,7 @@ int8_t z_undeclare_queryable(z_owned_queryable_t *queryable); * Returns: * Returns the constructed :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. @@ -1233,14 +1271,14 @@ z_query_reply_options_t z_query_reply_options_default(void); * Returns: * Returns ``0`` if the send query reply operation is successful, or a ``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, - const z_query_reply_options_t *options); +int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, const uint8_t *payload, + size_t payload_len, const z_query_reply_options_t *options); #endif /** * Creates keyexpr owning string passed to it */ -z_owned_keyexpr_t z_keyexpr_new(const char *name); +int8_t z_keyexpr_new(z_owned_keyexpr_t *key, const char *name); /** * Declares a keyexpr, so that it is internally mapped into into a numerical id. @@ -1262,26 +1300,27 @@ z_owned_keyexpr_t z_keyexpr_new(const char *name); * 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. + * zs: A loaned instance of the the :c:type:`z_loaned_session_t` where to declare the keyexpr. * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to declare. * * 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``. */ -z_owned_keyexpr_t z_declare_keyexpr(z_session_t zs, z_keyexpr_t keyexpr); +int8_t z_declare_keyexpr(z_owned_keyexpr_t *key, 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`. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` through where data will be put. + * zs: A loaned instance of the the :c:type:`z_loaned_session_t` through where data will be put. * keyexpr: A moved instance of :c:type:`z_owned_keyexpr_t` to undeclare. * * Returns: * Returns ``0`` if the undeclare keyexpr operation is successful, or a ``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 /** @@ -1290,7 +1329,7 @@ int8_t z_undeclare_keyexpr(z_session_t zs, z_owned_keyexpr_t *keyexpr); * Returns: * Returns the constructed :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. @@ -1311,7 +1350,7 @@ z_subscriber_options_t z_subscriber_options_default(void); * 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 subscriber. + * zs: A loaned instance of the the :c:type:`z_loaned_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 @@ -1321,8 +1360,8 @@ z_subscriber_options_t z_subscriber_options_default(void); * 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``. */ -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`. @@ -1339,7 +1378,7 @@ int8_t z_undeclare_subscriber(z_owned_subscriber_t *sub); * @brief * */ -z_owned_keyexpr_t z_subscriber_keyexpr(z_subscriber_t sub); +z_owned_keyexpr_t z_subscriber_keyexpr(z_loaned_subscriber_t *sub); #endif /** @@ -1360,7 +1399,7 @@ _Bool z_value_is_initialized(z_value_t *value); * Returns: * Returns the constructed :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. @@ -1368,14 +1407,14 @@ zp_task_read_options_t zp_task_read_options_default(void); * 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. + * zs: A loaned instance of the the :c:type:`z_loaned_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. * * Returns: * Returns ``0`` if the read task started successfully, or a ``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. @@ -1383,12 +1422,12 @@ int8_t zp_start_read_task(z_session_t zs, const zp_task_read_options_t *options) * 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: A loaned instance of the the :c:type:`z_loaned_session_t` where to stop the read task. * * Returns: * Returns ``0`` if the read task stopped successfully, or a ``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. @@ -1396,7 +1435,7 @@ int8_t zp_stop_read_task(z_session_t zs); * Returns: * Returns the constructed :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. @@ -1406,14 +1445,14 @@ zp_task_lease_options_t zp_task_lease_options_default(void); * 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. + * zs: A loaned instance of the the :c:type:`z_loaned_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. * * Returns: * Returns ``0`` if the lease task started successfully, or a ``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. @@ -1421,12 +1460,12 @@ int8_t zp_start_lease_task(z_session_t zs, const zp_task_lease_options_t *option * 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: A loaned instance of the the :c:type:`z_loaned_session_t` where to stop the lease task. * * Returns: * Returns ``0`` if the lease task stopped successfully, or a ``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 **************/ /** @@ -1435,20 +1474,20 @@ int8_t zp_stop_lease_task(z_session_t zs); * Returns: * Returns the constructed :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. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where trigger the reading procedure. + * zs: A loaned instance of the the :c:type:`z_loaned_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. * * Returns: * Returns ``0`` if the reading procedure was executed successfully, or a ``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. @@ -1456,7 +1495,7 @@ int8_t zp_read(z_session_t zs, const zp_read_options_t *options); * Returns: * Returns the constructed :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. @@ -1464,14 +1503,14 @@ zp_send_keep_alive_options_t zp_send_keep_alive_options_default(void); * It will send ``KeepAlive`` messages when needed and will close the session when the lease is expired. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where trigger the leasing procedure. + * zs: A loaned instance of the the :c:type:`z_loaned_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. * * Returns: * Returns ``0`` if the leasing procedure was executed successfully, or a ``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. @@ -1479,7 +1518,7 @@ int8_t zp_send_keep_alive(z_session_t zs, const zp_send_keep_alive_options_t *op * Returns: * Returns the constructed :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. @@ -1487,14 +1526,14 @@ zp_send_join_options_t zp_send_join_options_default(void); * It will send ``Join`` messages. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_session_t` where trigger the leasing procedure. + * zs: A loaned instance of the the :c:type:`z_loaned_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. * * Returns: * Returns ``0`` if the leasing procedure was executed successfully, or a ``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..556b94352 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -30,17 +30,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. * @@ -55,8 +65,9 @@ typedef _z_zint_t z_zint_t; * 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); + +_OWNED_TYPE_PTR(_z_bytes_t, bytes) +_LOANED_TYPE(_z_bytes_t, bytes) /** * Represents a Zenoh ID. @@ -75,10 +86,10 @@ 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, str) +_LOANED_TYPE(_z_string_t, str) +_VIEW_TYPE(_z_string_t, str) /** * Represents a key expression in Zenoh. @@ -90,8 +101,9 @@ _OWNED_TYPE_STR(z_str_t, str) * - :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. @@ -105,10 +117,8 @@ _OWNED_TYPE_PTR(z_keyexpr_t, keyexpr) * - :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. @@ -122,21 +132,15 @@ _OWNED_TYPE_PTR(_z_config_t, config) * - :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. */ -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. @@ -146,10 +150,8 @@ typedef struct { * - :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. @@ -161,10 +163,8 @@ _OWNED_TYPE_PTR(_z_subscriber_t, subscriber) * - :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. @@ -174,31 +174,31 @@ _OWNED_TYPE_PTR(_z_publisher_t, publisher) * - :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. * */ -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. + * z_loaned_bytes_t* suffix: The suffix of this encoding. It MUST be a valid UTF-8 string. */ +// TODO(sashacmc): typedef _z_encoding_t z_encoding_t; /* * Represents timestamp value in Zenoh */ +// TODO(sashacmc): typedef _z_timestamp_t z_timestamp_t; /** @@ -206,8 +206,9 @@ typedef _z_timestamp_t z_timestamp_t; * * Members: * z_encoding_t encoding: The encoding of the `payload`. - * z_bytes_t payload: The payload of this zenoh value. + * z_loaned_bytes_t* payload: The payload of this zenoh value. */ +// TODO(sashacmc): typedef _z_value_t z_value_t; /** @@ -422,36 +423,34 @@ static inline z_qos_t z_qos_default(void) { return _Z_N_QOS_DEFAULT; } * * Members: * z_keyexpr_t keyexpr: The keyexpr of this data sample. - * z_bytes_t payload: The value of this data sample. + * z_loaned_bytes_t* payload: The value of this data sample. * z_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_loaned_bytes_t* zid: The Zenoh ID of the scouted entity (empty if absent). * z_str_array_t locators: The locators of the scouted entity. */ -typedef _z_hello_t z_hello_t; -_OWNED_TYPE_PTR(z_hello_t, hello) +_OWNED_TYPE_PTR(_z_hello_t, hello) +_LOANED_TYPE(_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. + * z_loaned_bytes_t* replier_id: The id of the replier that sent this reply. */ +// TODO(sashacmc): typedef _z_reply_data_t z_reply_data_t; /** @@ -460,8 +459,8 @@ typedef _z_reply_data_t z_reply_data_t; * 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_PTR(_z_reply_t, reply) +_LOANED_TYPE(_z_reply_t, reply) /** * Represents an array of ``z_str_t``. @@ -472,12 +471,15 @@ _OWNED_TYPE_PTR(z_reply_t, reply) * - ``size_t z_str_array_len(z_str_array_t *a);`` * - ``_Bool z_str_array_array_is_empty(z_str_array_t *a);`` */ +// TODO(sashacmc): typedef _z_str_array_t z_str_array_t; -z_str_t *z_str_array_get(const z_str_array_t *a, size_t k); +z_owned_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) +_LOANED_TYPE(z_str_array_t, str_array) typedef void (*_z_dropper_handler_t)(void *arg); typedef void (*_z_owned_sample_handler_t)(z_owned_sample_t *sample, void *arg); @@ -492,13 +494,14 @@ typedef void (*_z_owned_sample_handler_t)(z_owned_sample_t *sample, void *arg); * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ +// TODO(sashacmc): typedef struct { void *context; _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. @@ -511,6 +514,7 @@ void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_samp * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. void *context: a * pointer to an arbitrary state. */ +// TODO(sashacmc): typedef struct { void *context; _z_owned_sample_handler_t call; @@ -530,13 +534,14 @@ void z_closure_owned_sample_call(const z_owned_closure_owned_sample_t *closure, * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ +// TODO(sashacmc): typedef struct { void *context; _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); @@ -551,6 +556,7 @@ typedef void (*_z_owned_query_handler_t)(z_owned_query_t *query, void *arg); * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. void *context: a * pointer to an arbitrary state. */ +// TODO(sashacmc): typedef struct { void *context; _z_owned_query_handler_t call; @@ -572,6 +578,7 @@ typedef void (*z_owned_reply_handler_t)(z_owned_reply_t *reply, void *arg); * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ +// TODO(sashacmc): typedef struct { void *context; z_owned_reply_handler_t call; @@ -593,6 +600,7 @@ typedef void (*z_owned_hello_handler_t)(z_owned_hello_t *hello, void *arg); * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ +// TODO(sashacmc): typedef struct { void *context; z_owned_hello_handler_t call; @@ -613,6 +621,7 @@ typedef void (*z_id_handler_t)(const z_id_t *id, void *arg); * _z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ +// TODO(sashacmc): typedef struct { void *context; z_id_handler_t call; @@ -622,8 +631,8 @@ typedef struct { 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 +643,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): typedef struct z_owned_bytes_map_t { _z_bytes_pair_list_t *_inner; } z_owned_bytes_map_t; @@ -671,7 +681,7 @@ z_owned_bytes_map_t z_bytes_map_from_attachment_aliasing(z_attachment_t this_); * - `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 +691,7 @@ 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 +699,7 @@ 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. * @@ -707,10 +717,12 @@ int8_t z_bytes_map_iter(const z_owned_bytes_map_t *this_, z_attachment_iter_body /** * Constructs a new map. */ +// TODO(sashacmc): z_owned_bytes_map_t z_bytes_map_new(void); /** * Constructs the gravestone value for `z_owned_bytes_map_t` */ +// TODO(sashacmc): z_owned_bytes_map_t z_bytes_map_null(void); #endif @@ -719,11 +731,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/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/net/primitives.h b/include/zenoh-pico/net/primitives.h index 2aa839398..8fa286f82 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -86,7 +86,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 +143,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 +173,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); /** 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..ad8e7de35 100644 --- a/include/zenoh-pico/net/reply.h +++ b/include/zenoh-pico/net/reply.h @@ -31,7 +31,7 @@ * */ typedef struct _z_reply_data_t { - _z_sample_t sample; + _z_sample_rc_t sample; _z_id_t replier_id; } _z_reply_data_t; diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index 34ff93139..17c1157ae 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 +// TODO(sashacmc): make in proper way +typedef struct _z_query_rc_t _z_query_rc_t; +typedef _z_query_rc_t z_loaned_query_t; // Forward type declaration to avoid cyclical include /** * 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; 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/src/api/api.c b/src/api/api.c index d08d6c579..a18bab192 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -41,41 +41,51 @@ #include "zenoh-pico/utils/result.h" #include "zenoh-pico/utils/uuid.h" -/********* Data Types Handlers *********/ -_Bool z_bytes_check(const z_bytes_t *v) { return v->start != NULL; } - -z_string_t z_string_make(const char *value) { return _z_string_make(value); } +// TODO(sashacmc): remove this VERY BAD workaround (it is only to allow compilaton)!!! +z_loaned_keyexpr_t *z_keyexpr(const char *name) { + static z_view_keyexpr_t keyexpr; + z_view_keyexpr_new(&keyexpr, name); + return (z_loaned_keyexpr_t *)z_view_keyexpr_loan(&keyexpr); +} -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); } +/********* Data Types Handlers *********/ -z_keyexpr_t z_keyexpr(const char *name) { return _z_rname(name); } +int8_t z_view_str_wrap(z_view_str_t *str, const char *value) { + str->_val = _z_string_make(value); + return _Z_RES_OK; +} -z_keyexpr_t z_keyexpr_unchecked(const char *name) { return _z_rname(name); } +// TODO(sashacmc): to z_slice_array_t ??? +// 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_owned_str_t z_keyexpr_to_string(z_keyexpr_t keyexpr) { - z_owned_str_t ret = {._value = NULL}; +int8_t z_view_keyexpr_new(z_view_keyexpr_t *keyexpr, const char *name) { + keyexpr->_val = _z_rname(name); + return _Z_RES_OK; +} - if (keyexpr._id == Z_RESOURCE_ID_NONE) { - size_t ke_len = _z_str_size(keyexpr._suffix); +int8_t z_view_keyexpr_unchecked(z_view_keyexpr_t *keyexpr, const char *name) { + keyexpr->_val = _z_rname(name); + return _Z_RES_OK; +} - ret._value = (char *)z_malloc(ke_len); - if (ret._value != NULL) { - _z_str_n_copy(ret._value, keyexpr._suffix, ke_len); +void z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_str_t *s) { + 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); } } - - return ret; } -z_bytes_t z_keyexpr_as_bytes(z_keyexpr_t keyexpr) { +/* +TODO(sashacmc) +z_loaned_bytes_t* z_keyexpr_as_bytes(z_loaned_keyexpr_t *keyexpr) { if (keyexpr._id == Z_RESOURCE_ID_NONE) { - z_bytes_t ret = {.start = (const uint8_t *)keyexpr._suffix, .len = strlen(keyexpr._suffix), ._is_alloc = false}; - return ret; - } else { - z_bytes_t ret = {.start = NULL, .len = 0, ._is_alloc = false}; - return ret; + z_loaned_bytes_t* ret = {.start = (const uint8_t *)keyexpr._suffix, .len = strlen(keyexpr._suffix), ._is_alloc = +false}; return ret; } else { z_loaned_bytes_t* ret = {.start = NULL, .len = 0, ._is_alloc = false}; return ret; } } @@ -86,18 +96,16 @@ _Bool zp_keyexpr_was_declared(const z_keyexpr_t *keyexpr) { } 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_str_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,11 +134,11 @@ 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 z_keyexpr_includes(const z_loaned_keyexpr_t *l, const z_loaned_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); + 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; } @@ -150,11 +158,11 @@ int8_t zp_keyexpr_includes_null_terminated(const char *l, const char *r) { return ret; } -int8_t z_keyexpr_intersects(z_keyexpr_t l, z_keyexpr_t r) { +int8_t z_keyexpr_intersects(const z_loaned_keyexpr_t *l, const z_loaned_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); + 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; } @@ -174,11 +182,11 @@ int8_t zp_keyexpr_intersect_null_terminated(const char *l, const char *r) { return ret; } -int8_t z_keyexpr_equals(z_keyexpr_t l, z_keyexpr_t r) { +int8_t z_keyexpr_equals(const z_loaned_keyexpr_t *l, const z_loaned_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); + 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; } @@ -203,58 +211,56 @@ int8_t zp_keyexpr_equals_null_terminated(const char *l, const char *r) { return ret; } -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) { @@ -290,19 +296,18 @@ 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_str_t *parameters) { + // TODO(sashacmc) + // *parameters = _z_bytes_wrap((uint8_t *)query->in->val._parameters, strlen(query->in->val._parameters)); } -z_value_t z_query_value(const z_query_t *query) { return query->_val._rc.in->val._value; } +z_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; @@ -314,7 +319,7 @@ _Bool z_value_is_initialized(z_value_t *value) { return ret; } -void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_sample_t *sample) { +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 +331,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); } @@ -356,180 +361,98 @@ 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); } +// TODO(sashacmc): return value for drop? (queryable, etc) +#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; } \ + void z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src) { \ + obj->_val = (type *)z_malloc(sizeof(type)); \ + if (obj->_val != NULL) { \ + f_copy(obj->_val, src); \ + } \ + } \ + void z_##name##_drop(z_owned_##name##_t *obj) { \ + if (obj->_val != NULL) { \ + f_free(&obj->_val); \ + } \ + } + +// TODO(sashacmc): Sould drop clean pinter to allow double call ? +#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; } \ + void z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src) { \ + obj->_rc = _z_##name##_rc_clone((z_loaned_##name##_t *)src); \ + } \ + void z_##name##_drop(z_owned_##name##_t *val) { _z_##name##_rc_drop(&val->_rc); } + +#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) +// TODO(sashacmc): remove +// OWNED_FUNCTIONS_STR(z_str_t, z_owned_str_t, str, _z_str_free, _z_str_n_copy) + +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, str, _z_string_copy, _z_string_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, str) + +OWNED_FUNCTIONS_PTR(_z_hello_t, hello, _z_owner_noop_copy, _z_hello_free) +OWNED_FUNCTIONS_PTR(_z_str_array_t, str_array, _z_owner_noop_copy, _z_str_array_free) +OWNED_FUNCTIONS_PTR(_z_bytes_t, bytes, _z_bytes_copy, _z_bytes_free) + +OWNED_FUNCTIONS_RC(sample) +// TODO(sashacmc): drop != close +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_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 +461,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 +479,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 +514,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 +565,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 +584,51 @@ 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; } +z_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_str_data(const z_loaned_str_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; #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,7 +637,7 @@ 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, + ret = _z_write(&_Z_RC_IN_VAL(zs), *keyexpr, (const uint8_t *)payload, payload_len, opt.encoding, Z_SAMPLE_KIND_PUT, opt.congestion_control, opt.priority #if Z_FEATURE_ATTACHMENT == 1 , @@ -719,7 +651,7 @@ 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 @@ -728,15 +660,16 @@ int8_t z_put(z_session_t zs, z_keyexpr_t keyexpr, const uint8_t *payload, z_zint 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_default(), Z_SAMPLE_KIND_DELETE, opt.congestion_control, opt.priority #if Z_FEATURE_ATTACHMENT == 1 , @@ -747,74 +680,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 = z_encoding_default(); #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 +751,31 @@ 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, 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); 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_default(), Z_SAMPLE_KIND_DELETE, + pub->_congestion_control, pub->_priority #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_null() @@ -854,27 +783,27 @@ 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_PTR(_z_reply_t, reply, _z_owner_noop_copy, _z_reply_free) -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->value.encoding = z_encoding_default(); + options->value.payload = _z_bytes_empty(); #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 { @@ -883,20 +812,21 @@ typedef struct __z_reply_handler_wrapper_t { } __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}; + z_owned_reply_t oreply = {._val = reply}; wrapped_ctx->user_call(&oreply, wrapped_ctx->ctx); - z_reply_drop(&oreply); // user_call is allowed to take ownership of the reply by setting oreply._value to NULL + z_reply_drop(&oreply); // user_call is allowed to take ownership of the reply by setting oreply._val 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, z_loaned_keyexpr_t *keyexpr, const char *parameters, + z_owned_closure_reply_t *callback, const 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; @@ -924,7 +854,7 @@ int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owne wrapped_ctx->ctx = ctx; } - ret = _z_query(&zs._val.in->val, keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, + ret = _z_query(&_Z_RC_IN_VAL(zs), *keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, __z_reply_handler, wrapped_ctx, callback->drop, ctx, opt.timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , @@ -941,7 +871,7 @@ _Bool z_reply_is_ok(const z_owned_reply_t *reply) { 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_owned_reply_t *reply) { return &reply->_val->data.sample; } z_value_t z_reply_err(const z_owned_reply_t *reply) { (void)(reply); @@ -950,57 +880,61 @@ z_value_t z_reply_err(const z_owned_reply_t *reply) { #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 = z_encoding_default(); } -int8_t z_query_reply(const z_query_t *query, const z_keyexpr_t keyexpr, const uint8_t *payload, size_t payload_len, - const z_query_reply_options_t *options) { - z_query_reply_options_t opts = options == NULL ? z_query_reply_options_default() : *options; +int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, const uint8_t *payload, + size_t payload_len, const z_query_reply_options_t *options) { + z_query_reply_options_t opts; + if (options == NULL) { + z_query_reply_options_default(&opts); + } else { + opts = *options; + } _z_value_t value = {.payload = { .start = payload, @@ -1008,82 +942,84 @@ int8_t z_query_reply(const z_query_t *query, const z_keyexpr_t keyexpr, const ui .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_send_reply(&query->in->val, *keyexpr, value, Z_SAMPLE_KIND_PUT, opts.attachment); return _Z_ERR_GENERIC; } #endif -z_owned_keyexpr_t z_keyexpr_new(const char *name) { - z_owned_keyexpr_t key; - - key._value = name ? (z_keyexpr_t *)z_malloc(sizeof(z_keyexpr_t)) : NULL; - if (key._value != NULL) { - *key._value = _z_rid_with_suffix(Z_RESOURCE_ID_NONE, name); +int8_t z_keyexpr_new(z_owned_keyexpr_t *key, const char *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); } - return key; + return _Z_RES_OK; } -z_owned_keyexpr_t z_declare_keyexpr(z_session_t zs, z_keyexpr_t keyexpr) { - z_owned_keyexpr_t key; - - 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); +int8_t z_declare_keyexpr(z_owned_keyexpr_t *key, const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr) { + 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); } - return key; + return _Z_RES_OK; } -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) { + if (wild != NULL && wild != keyexpr->_suffix) { wild -= 1; - size_t len = wild - keyexpr._suffix; + size_t len = wild - keyexpr->_suffix; suffix = z_malloc(len + 1); if (suffix != NULL) { - memcpy(suffix, keyexpr._suffix, len); + memcpy(suffix, keyexpr->_suffix, len); suffix[len] = 0; - keyexpr._suffix = suffix; - _z_keyexpr_set_owns_suffix(&keyexpr, false); + // TODO(sashacmc): Why we modify it? Rework to remove cast + ((z_loaned_keyexpr_t *)keyexpr)->_suffix = suffix; + _z_keyexpr_set_owns_suffix((z_loaned_keyexpr_t *)keyexpr, 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), *keyexpr); key = _z_rid_with_suffix(id, wild); } } @@ -1093,35 +1029,35 @@ 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; - - _z_undeclare_subscriber(sub->_value); - _z_subscriber_free(&sub->_value); + sub->_val = int_sub; - return ret; + if (int_sub == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; // TODO(sashacmc): shoud we introduce sperate error? + } 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; +int8_t z_undeclare_subscriber(z_owned_subscriber_t *sub) { return _z_subscriber_drop(&sub->_val); } + +z_owned_keyexpr_t z_subscriber_keyexpr(z_loaned_subscriber_t *sub) { + z_owned_keyexpr_t ret; + z_keyexpr_null(&ret); + if (sub != NULL) { + uint32_t lookup = sub->_entity_id; + _z_subscription_rc_list_t *tail = sub->_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; + ret = (z_owned_keyexpr_t){._val = z_malloc(sizeof(_z_keyexpr_t))}; + if (ret._val != NULL) { + *ret._val = key; } else { _z_keyexpr_clear(&key); } @@ -1134,93 +1070,89 @@ z_owned_keyexpr_t z_subscriber_keyexpr(z_subscriber_t sub) { #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 +1169,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 +1193,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 +1203,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 +1224,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 +1262,11 @@ 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; +} +// TODO(sashacmc): +// z_loaned_bytes_t *z_bytes_null(void) { return (z_loaned_bytes_t *){.len = 0, ._is_alloc = false, .start = NULL}; } #endif diff --git a/src/api/handlers.c b/src/api/handlers.c index 14b3f6960..e133a5835 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); + dst->_rc = _z_sample_rc_clone(src); } else { - dst->_value = NULL; + dst->_rc.in = NULL; } return dst; } @@ -47,9 +43,9 @@ 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); + _z_query_rc_copy(&dst->_rc, src); return dst; } #endif // Z_FEATURE_QUERYABLE @@ -66,11 +62,11 @@ z_owned_reply_t *_z_reply_clone(const z_owned_reply_t *src) { 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 && src->_val) { + dst->_val = (_z_reply_t *)zp_malloc(sizeof(_z_reply_t)); + _z_reply_copy(dst->_val, src->_val); } else { - dst->_value = NULL; + dst->_val = NULL; } return dst; } 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/primitives.c b/src/net/primitives.c index c4cc5d34d..84e767b68 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -100,7 +100,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 +188,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 +258,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); @@ -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..f9ece5032 100644 --- a/src/net/reply.c +++ b/src/net/reply.c @@ -18,7 +18,7 @@ #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,7 +34,7 @@ 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; } diff --git a/src/net/session.c b/src/net/session.c index 3b2af1097..6c708cd38 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -99,7 +99,7 @@ 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; } @@ -154,7 +154,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_from_bytes(&local_zid).val); switch (zn->_tp._type) { case _Z_TRANSPORT_UNICAST_TYPE: diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 025c97fd4..32fd16299 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -410,7 +410,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); } diff --git a/src/protocol/config.c b/src/protocol/config.c index 3c70a27e6..40c895c96 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 (res != value) { ret = _Z_ERR_CONFIG_FAILED_INSERT; } diff --git a/src/session/query.c b/src/session/query.c index a84cf7290..27dbceabf 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -121,14 +121,14 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_zint_t id, cons _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); + reply.data.sample.in->val.keyexpr = expanded_ke; + _z_bytes_copy(&reply.data.sample.in->val.payload, &msg->_payload); + reply.data.sample.in->val.encoding.id = msg->_encoding.id; + _z_bytes_copy(&reply.data.sample.in->val.encoding.schema, &msg->_encoding.schema); + reply.data.sample.in->val.kind = Z_SAMPLE_KIND_PUT; + reply.data.sample.in->val.timestamp = _z_timestamp_duplicate(&msg->_commons._timestamp); #if Z_FEATURE_ATTACHMENT == 1 - reply.data.sample.attachment = _z_encoded_as_attachment(&msg->_attachment); + reply.data.sample.in->val.attachment = _z_encoded_as_attachment(&msg->_attachment); #endif // Verify if this is a newer reply, free the old one in case it is @@ -141,7 +141,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 { @@ -163,7 +164,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 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/subscription.c b/src/session/subscription.c index 3d1ec99fe..48397577e 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -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 { diff --git a/src/transport/multicast.c b/src/transport/multicast.c index d8613dc89..6b2b83feb 100644 --- a/src/transport/multicast.c +++ b/src/transport/multicast.c @@ -46,7 +46,8 @@ 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)); + // TODO(sashacmc): double check! + _zp_config_insert(ps, Z_INFO_PEER_PID_KEY, _z_string_from_bytes(&remote_zid).val); xs = _z_transport_peer_entry_list_tail(xs); } diff --git a/src/transport/unicast.c b/src/transport/unicast.c index 02ddc9e5c..75e29a56b 100644 --- a/src/transport/unicast.c +++ b/src/transport/unicast.c @@ -40,7 +40,8 @@ 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)); + // TODO(sashacmc): double check! + _zp_config_insert(ps, Z_INFO_ROUTER_PID_KEY, _z_string_from_bytes(&remote_zidbytes).val); } #else diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 9ac4b220c..3f4a4fc14 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -56,28 +56,31 @@ void hello_handler(z_owned_hello_t *hello, void *arg) { (void)(arg); printf("%s\n", __func__); hellos++; - z_hello_null(); + z_null(hello); z_drop(hello); // validate double-drop safety: caller drops hello if it's not dropped by the handler } 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)); + z_owned_str_t k_str; + z_keyexpr_to_string(z_query_keyexpr(query), &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_str_t pred; + z_query_parameters(query, &pred); (void)(pred); z_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); + z_query_reply(query, z_keyexpr(z_loan(k_str)->val), (const uint8_t *)value, strlen(value), &_ret_qreply_opt); z_drop(z_move(k_str)); } @@ -88,12 +91,13 @@ void reply_handler(z_owned_reply_t *reply, void *arg) { 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_str_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)); @@ -102,20 +106,20 @@ void reply_handler(z_owned_reply_t *reply, void *arg) { (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 + z_null(reply); + 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_str_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)); @@ -131,8 +135,8 @@ int main(int argc, char **argv) { #endif printf("Testing Keyexpr..."); - z_keyexpr_t key = z_keyexpr("demo/example"); - _Bool _ret_bool = z_keyexpr_is_initialized(&key); + const z_loaned_keyexpr_t *key = z_keyexpr("demo/example"); + _Bool _ret_bool = z_keyexpr_is_initialized(key); assert_eq(_ret_bool, true); int8_t _ret_int8 = z_keyexpr_includes(z_keyexpr("demo/example/**"), z_keyexpr("demo/example/a")); @@ -180,24 +184,25 @@ int main(int argc, char **argv) { 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 +214,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 +226,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 +236,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 +250,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,19 +280,22 @@ 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_owned_subscriber_t _ret_sub; + _ret_int8 = + z_declare_subscriber(&_ret_sub, z_loan(s2), z_keyexpr(keyexpr_str), z_move(_ret_closure_sample), &_ret_sub_opt); + assert(_ret_int8 == _Z_RES_OK); printf("Ok\n"); sleep(SLEEP); @@ -289,12 +303,14 @@ int main(int argc, char **argv) { 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)); + z_owned_keyexpr_t _ret_expr; + z_declare_keyexpr(&_ret_expr, z_loan(s1), z_keyexpr(s1_res)); assert(z_check(_ret_expr)); printf("Ok\n"); printf("Session Put..."); - z_put_options_t _ret_put_opt = z_put_options_default(); + 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); @@ -308,7 +324,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 +342,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_keyexpr(keyexpr_str), &_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 +363,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 +389,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_keyexpr(s1_res), 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); @@ -409,8 +434,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 +443,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); 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..c958deb4c 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_str_t str_null_1; + z_str_null(&str_null_1); // // Test that they actually make invalid value (where applicable) @@ -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..9c151dcab 100644 --- a/tests/z_channels_test.c +++ b/tests/z_channels_test.c @@ -30,30 +30,31 @@ .encoding = z_encoding_default(), \ .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 +76,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 +104,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 +132,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..b4cbdd2a3 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -47,17 +47,19 @@ 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_str_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_str_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); @@ -71,13 +73,15 @@ void reply_handler(z_owned_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_str_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,23 +92,23 @@ 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_str_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); } @@ -117,35 +121,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 +160,9 @@ 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_owned_keyexpr_t expr; + z_declare_keyexpr(&expr, z_loan(s1), z_keyexpr(s1_res)); + printf("Declared resource on session 1: %u %s\n", z_loan(expr)->_id, z_loan(expr)->_suffix); rids1[i] = expr; } @@ -162,8 +170,9 @@ 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_owned_keyexpr_t expr; + z_declare_keyexpr(&expr, z_loan(s2), z_keyexpr(s1_res)); + printf("Declared resource on session 2: %u %s\n", z_loan(expr)->_id, z_loan(expr)->_suffix); rids2[i] = expr; } @@ -171,12 +180,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 +194,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_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); + assert(z_declare_queryable(qle, z_loan(s2), z_keyexpr(s1_res), &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 +207,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 +223,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 +258,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,7 +285,8 @@ 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_owned_closure_reply_t callback; + z_closure(&callback, reply_handler, NULL, &idx[i]); z_get(z_loan(s1), z_keyexpr(s1_res), "", &callback, NULL); printf("Queried data from session 1: %zu %s\n", (z_zint_t)0, s1_res); } @@ -311,7 +324,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 +334,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 +343,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 +352,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 +367,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_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 5965d5ab1..2e7df7cc1 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_str_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_from_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_from_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,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_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, + int8_t res = z_declare_subscriber(sub, z_loan(s2), z_keyexpr(s1_res), &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,7 +132,8 @@ 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); printf("Wrote data from session 1: %s %zu b\t(%u/%u)\n", s1_res, len, n * SET + (i + 1), total); @@ -155,7 +160,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 +169,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..88e106786 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,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 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; + if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL) < 0) { printf("Unable to create subscriber.\n"); exit(-1); } @@ -113,8 +115,8 @@ 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); } diff --git a/tests/z_perf_tx.c b/tests/z_perf_tx.c index 1852ecf37..cac34429d 100644 --- a/tests/z_perf_tx.c +++ b/tests/z_perf_tx.c @@ -52,31 +52,32 @@ 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; + if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(keyexpr), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); exit(-1); } @@ -97,8 +98,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..368f976be 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_str_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_str_data(z_loan(keystr)), + (int)z_sample_payload(sample)->len, is_valid); z_drop(z_move(keystr)); } @@ -51,32 +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"); 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; + if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); return -1; } @@ -89,8 +92,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..356c9453f 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; @@ -81,8 +82,8 @@ int main(int argc, char **argv) { 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..26ddb1280 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.20240503dev Cflags: -I${prefix}/include Libs: -L${prefix}/lib -lzenohpico From eb952304349cbe74c5c600d8cc19c8dc39b8123f Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Fri, 17 May 2024 21:15:29 +0200 Subject: [PATCH 02/43] Keyexpr rework --- examples/arduino/z_get.ino | 6 +- examples/arduino/z_pub.ino | 4 +- examples/arduino/z_pull.ino | 8 ++- examples/arduino/z_queryable.ino | 12 ++-- examples/arduino/z_scout.ino | 28 ++++----- examples/arduino/z_sub.ino | 8 ++- examples/espidf/z_get.c | 4 +- examples/espidf/z_pub.c | 4 +- examples/espidf/z_pull.c | 4 +- examples/espidf/z_queryable.c | 18 ++++-- examples/espidf/z_scout.c | 26 +++++---- examples/espidf/z_sub.c | 6 +- examples/freertos_plus_tcp/z_get.c | 10 ++-- examples/freertos_plus_tcp/z_pub.c | 4 +- examples/freertos_plus_tcp/z_pub_st.c | 4 +- examples/freertos_plus_tcp/z_pull.c | 4 +- examples/freertos_plus_tcp/z_put.c | 4 +- examples/freertos_plus_tcp/z_queryable.c | 25 ++++---- examples/freertos_plus_tcp/z_scout.c | 26 +++++---- examples/freertos_plus_tcp/z_sub.c | 4 +- examples/freertos_plus_tcp/z_sub_st.c | 4 +- examples/mbed/z_get.cpp | 4 +- examples/mbed/z_pub.cpp | 4 +- examples/mbed/z_pull.cpp | 2 +- examples/mbed/z_queryable.cpp | 8 ++- examples/mbed/z_scout.cpp | 26 +++++---- examples/mbed/z_sub.cpp | 6 +- examples/unix/c11/z_get.c | 13 ++--- examples/unix/c11/z_get_channel.c | 13 ++--- examples/unix/c11/z_ping.c | 11 ++-- examples/unix/c11/z_pong.c | 12 ++-- examples/unix/c11/z_pub.c | 4 +- examples/unix/c11/z_pub_st.c | 4 +- examples/unix/c11/z_pub_thr.c | 4 +- examples/unix/c11/z_pull.c | 4 +- examples/unix/c11/z_put.c | 6 +- examples/unix/c11/z_queryable.c | 17 +++--- examples/unix/c11/z_queryable_channel.c | 15 +++-- examples/unix/c11/z_sub.c | 4 +- examples/unix/c11/z_sub_channel.c | 4 +- examples/unix/c11/z_sub_st.c | 4 +- examples/unix/c11/z_sub_thr.c | 4 +- examples/unix/c99/z_get.c | 6 +- examples/unix/c99/z_ping.c | 14 ++--- examples/unix/c99/z_pong.c | 12 ++-- examples/unix/c99/z_pub.c | 4 +- examples/unix/c99/z_pub_st.c | 4 +- examples/unix/c99/z_pull.c | 2 +- examples/unix/c99/z_put.c | 4 +- examples/unix/c99/z_queryable.c | 9 +-- examples/unix/c99/z_scout.c | 2 +- examples/unix/c99/z_sub.c | 6 +- examples/unix/c99/z_sub_st.c | 6 +- examples/windows/z_get.c | 8 +-- examples/windows/z_ping.c | 12 ++-- examples/windows/z_pong.c | 18 +++--- examples/windows/z_pub.c | 4 +- examples/windows/z_pub_st.c | 4 +- examples/windows/z_pull.c | 2 +- examples/windows/z_put.c | 4 +- examples/windows/z_queryable.c | 22 ++++---- examples/windows/z_scout.c | 26 +++++---- examples/windows/z_sub.c | 4 +- examples/windows/z_sub_st.c | 4 +- examples/zephyr/z_get.c | 6 +- examples/zephyr/z_pub.c | 4 +- examples/zephyr/z_pull.c | 2 +- examples/zephyr/z_queryable.c | 12 ++-- examples/zephyr/z_scout.c | 26 +++++---- examples/zephyr/z_sub.c | 4 +- include/zenoh-pico/api/macros.h | 1 + include/zenoh-pico/api/primitives.h | 9 +-- src/api/api.c | 13 +---- src/session/subscription.c | 7 +-- src/transport/raweth/tx.c | 2 +- tests/z_api_alignment_test.c | 72 +++++++++++++----------- tests/z_client_test.c | 18 ++++-- tests/z_peer_multicast_test.c | 8 ++- tests/z_perf_rx.c | 4 +- tests/z_perf_tx.c | 4 +- tests/z_test_fragment_rx.c | 4 +- tests/z_test_fragment_tx.c | 4 +- 82 files changed, 441 insertions(+), 318 deletions(-) diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index b0e78e654..5f86ee450 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -51,7 +51,7 @@ void reply_handler(z_owned_reply_t *oreply, void *ctx) { std::string val((const char *)z_sample_payload(sample)->start, z_sample_payload(sample)->len); Serial.print(" >> [Get listener] Received ("); - Serial.print(z_str_loan(&keystr)); + Serial.print(z_str_data(z_str_loan(&keystr))); Serial.print(", "); Serial.print(val.c_str()); Serial.println(")"); @@ -118,7 +118,9 @@ void loop() { } z_owned_closure_reply_t callback; z_closure_reply(&callback, reply_handler, reply_dropper, NULL); - if (z_get(z_session_loan(&s), z_keyexpr(KEYEXPR), "", z_closure_reply_move(&callback), &opts) < 0) { + 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 6c06e9a8c..df03b0bcf 100644 --- a/examples/arduino/z_pub.ino +++ b/examples/arduino/z_pub.ino @@ -81,7 +81,9 @@ void setup() { Serial.print("Declaring publisher for "); Serial.print(KEYEXPR); Serial.println("..."); - if (z_declare_publisher(&pub, z_session_loan(&s), z_keyexpr(KEYEXPR), NULL) < 0) { + 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 867d84bdc..bf75d2412 100644 --- a/examples/arduino/z_pull.ino +++ b/examples/arduino/z_pull.ino @@ -44,7 +44,7 @@ // 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_str_data(z_str_loan(&keystr))); // Serial.print(", "); // Serial.print(val.c_str()); // Serial.println(")"); @@ -99,8 +99,10 @@ void setup() { // 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 b07ecc17b..1463e4af7 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -40,12 +40,14 @@ void query_handler(const z_loaned_query_t *query, void *arg) { z_keyexpr_to_string(z_query_keyexpr(query), &keystr); Serial.print(" >> [Queryable handler] Replying Data ('"); - Serial.print(z_str_loan(&keystr)); + Serial.print(z_str_data(z_str_loan(&keystr))); Serial.print("': '"); Serial.print(VALUE); Serial.println("')"); - z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL); + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + z_query_reply(query, z_view_keyexpr_loan(&ke), (const unsigned char *)VALUE, strlen(VALUE), NULL); z_str_drop(z_str_move(&keystr)); } @@ -96,8 +98,10 @@ void setup() { z_owned_closure_query_t callback; z_closure_query(&callback, query_handler, NULL, NULL); z_owned_queryable_t qable; - if (z_declare_queryable(&qable, z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_query_move(&callback), NULL) < - 0) { + 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 05b1770a1..a3b78bb46 100644 --- a/examples/arduino/z_scout.ino +++ b/examples/arduino/z_scout.ino @@ -56,25 +56,27 @@ void fprintwhatami(unsigned int whatami) { void fprintlocators(const z_str_array_t *locs) { Serial.print("["); - size_t len = z_str_array_len(locs); - for (unsigned int i = 0; i < len; i++) { - Serial.print("'"); - Serial.print(*z_str_array_get(locs, i)); - Serial.print("'"); - if (i < len - 1) { - Serial.print(", "); - } - } + (void)locs; + // TODO(sashacmc): z_str_array_t + // size_t len = z_str_array_len(locs); + // for (unsigned int i = 0; i < len; i++) { + // Serial.print("'"); + // Serial.print(*z_str_array_get(locs, i)); + // Serial.print("'"); + // if (i < len - 1) { + // Serial.print(", "); + // } + //} 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(" }"); } diff --git a/examples/arduino/z_sub.ino b/examples/arduino/z_sub.ino index c5a79299e..683e6acf5 100644 --- a/examples/arduino/z_sub.ino +++ b/examples/arduino/z_sub.ino @@ -40,7 +40,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { 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_str_data(z_str_loan(&keystr))); Serial.print(", "); Serial.print(val.c_str()); Serial.println(")"); @@ -94,8 +94,10 @@ void setup() { z_owned_closure_sample_t callback; z_closure_sample(&callback, data_handler, NULL, NULL); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_sample_move(&callback), NULL) < - 0) { + 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 f2db48257..d45afa073 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -165,7 +165,9 @@ void app_main() { } z_owned_closure_reply_t callback; z_closure(&callback, reply_handler, reply_dropper); - if (z_get(z_loan(s), z_keyexpr(KEYEXPR), "", z_move(callback), &opts) < 0) { + 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); } diff --git a/examples/espidf/z_pub.c b/examples/espidf/z_pub.c index 6aad2f139..53cb2be99 100644 --- a/examples/espidf/z_pub.c +++ b/examples/espidf/z_pub.c @@ -141,7 +141,9 @@ void app_main() { printf("Declaring publisher for '%s'...", KEYEXPR); z_owned_publisher_t pub; - if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(KEYEXPR), NULL) < 0) { + 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); } diff --git a/examples/espidf/z_pull.c b/examples/espidf/z_pull.c index aef49a4b7..9f6a2d11f 100644 --- a/examples/espidf/z_pull.c +++ b/examples/espidf/z_pull.c @@ -153,7 +153,9 @@ void app_main() { // 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); diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index dd642a62a..6bd3d88b7 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -101,13 +101,17 @@ 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), &keystr); - z_bytes_t pred = z_query_parameters(query); - printf(">> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)pred.len, pred.start); - z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL); + // TODO(sashacmc): z_query_parameters + // z_keyexpr_to_string(z_query_keyexpr(query), &keystr); + // z_bytes_t pred = z_query_parameters(query); + // printf(">> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)pred.len, + // pred.start); + z_view_keyexpr_t ke; + z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + z_query_reply(query, z_loan(ke), (const unsigned char *)VALUE, strlen(VALUE), NULL); z_drop(z_move(keystr)); } @@ -154,7 +158,9 @@ void app_main() { 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_keyexpr(KEYEXPR), z_move(callback), NULL) < 0) { + 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); } diff --git a/examples/espidf/z_scout.c b/examples/espidf/z_scout.c index ed2eb44f7..99ca0b026 100644 --- a/examples/espidf/z_scout.c +++ b/examples/espidf/z_scout.c @@ -111,24 +111,26 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { void fprintlocators(FILE *stream, const z_str_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); - fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { - fprintf(stream, ", "); - } - } + (void)locs; + // TODO(sashacmc): z_str_array missed + // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + // fprintf(stream, "\""); + // fprintf(stream, "%s", *z_str_array_get(locs, i)); + // fprintf(stream, "\""); + // if (i < z_str_array_len(locs) - 1) { + // fprintf(stream, ", "); + // } + //} fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +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, " }"); } diff --git a/examples/espidf/z_sub.c b/examples/espidf/z_sub.c index 000bf238e..76e93927f 100644 --- a/examples/espidf/z_sub.c +++ b/examples/espidf/z_sub.c @@ -103,7 +103,7 @@ void wifi_init_sta(void) { void data_handler(const z_loaned_sample_t* sample, void* arg) { z_owned_str_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); - const z_loaned_payload_t* payload = z_sample_payload(sample); + const z_loaned_bytes_t* payload = z_sample_payload(sample); printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len, payload->start); z_str_drop(z_str_move(&keystr)); @@ -151,7 +151,9 @@ void app_main() { z_owned_closure_sample_t callback; z_closure(&callback, data_handler); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL) < 0) { + 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); } diff --git a/examples/freertos_plus_tcp/z_get.c b/examples/freertos_plus_tcp/z_get.c index bc27664cd..c4177132b 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -72,10 +72,10 @@ void app_main(void) { 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) { @@ -88,7 +88,7 @@ void app_main(void) { } z_owned_closure_reply_t callback; z_closure(&callback, reply_handler, reply_dropper); - if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { + if (z_get(z_loan(s), z_loan(ke), "", z_move(callback), &opts) < 0) { printf("Unable to send query.\n"); return; } diff --git a/examples/freertos_plus_tcp/z_pub.c b/examples/freertos_plus_tcp/z_pub.c index 73a675427..ab13b8796 100644 --- a/examples/freertos_plus_tcp/z_pub.c +++ b/examples/freertos_plus_tcp/z_pub.c @@ -84,7 +84,9 @@ void app_main(void) { printf("Declaring publisher for '%s'...\n", KEYEXPR); z_owned_publisher_t pub; - if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(KEYEXPR), NULL) < 0) { + 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_pub_st.c b/examples/freertos_plus_tcp/z_pub_st.c index 0686bf584..bfdd22692 100644 --- a/examples/freertos_plus_tcp/z_pub_st.c +++ b/examples/freertos_plus_tcp/z_pub_st.c @@ -49,7 +49,9 @@ void app_main(void) { printf("Declaring publisher for '%s'...\n", KEYEXPR); z_owned_publisher_t pub; - if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(KEYEXPR), NULL) < 0) { + 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 1f2797a31..2c6ed1014 100644 --- a/examples/freertos_plus_tcp/z_pull.c +++ b/examples/freertos_plus_tcp/z_pull.c @@ -65,7 +65,9 @@ void app_main(void) { // 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; diff --git a/examples/freertos_plus_tcp/z_put.c b/examples/freertos_plus_tcp/z_put.c index 4459d04a0..b057bb88f 100644 --- a/examples/freertos_plus_tcp/z_put.c +++ b/examples/freertos_plus_tcp/z_put.c @@ -53,7 +53,9 @@ void app_main(void) { printf("Declaring key expression '%s'...\n", KEYEXPR); z_owned_keyexpr_t ke; - if (z_declare_keyexpr(&ke, z_loan(s), z_keyexpr(KEYEXPR)) < 0) { + 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_mut(s)); zp_stop_lease_task(z_loan_mut(s)); diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index 40a730aff..02251adc3 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -33,16 +33,17 @@ 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), &keystr); - 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_str_data(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); - } + // TODO(sashacmc): z_query_parameters + // 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_str_data(z_loan(keystr)), (int)pred.len, + // pred.start); if (payload_value.payload.len > 0) { + // printf(" with value '%.*s'\n", (int)payload_value.payload.len, payload_value.payload.start); + // } z_query_reply_options_t options; z_query_reply_options_default(&options); 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(query, z_query_keyexpr(query), (const unsigned char *)VALUE, strlen(VALUE), &options); z_drop(z_move(keystr)); } @@ -68,17 +69,17 @@ void app_main(void) { 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(&callback, query_handler); z_owned_queryable_t qable; - if (z_declare_queryable(&qable, z_loan(s), ke, z_move(callback), NULL) < 0) { + if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to create queryable.\n"); return; } diff --git a/examples/freertos_plus_tcp/z_scout.c b/examples/freertos_plus_tcp/z_scout.c index 702021b99..777ef2b2c 100644 --- a/examples/freertos_plus_tcp/z_scout.c +++ b/examples/freertos_plus_tcp/z_scout.c @@ -45,24 +45,26 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { void fprintlocators(FILE *stream, const z_str_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); - fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { - fprintf(stream, ", "); - } - } + (void)locs; + // TODO(sashacmc): z_str_array missed + // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + // fprintf(stream, "\""); + // fprintf(stream, "%s", *z_str_array_get(locs, i)); + // fprintf(stream, "\""); + // if (i < z_str_array_len(locs) - 1) { + // fprintf(stream, ", "); + // } + //} fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +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, " }"); } diff --git a/examples/freertos_plus_tcp/z_sub.c b/examples/freertos_plus_tcp/z_sub.c index fd674d66c..b59d85972 100644 --- a/examples/freertos_plus_tcp/z_sub.c +++ b/examples/freertos_plus_tcp/z_sub.c @@ -62,8 +62,10 @@ void app_main(void) { z_owned_closure_sample_t callback; z_closure(&callback, data_handler); printf("Declaring Subscriber on '%s'...\n", KEYEXPR); + 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_keyexpr(KEYEXPR), z_move(callback), NULL) < 0) { + 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/freertos_plus_tcp/z_sub_st.c b/examples/freertos_plus_tcp/z_sub_st.c index 1f89f1518..53f81f2a8 100644 --- a/examples/freertos_plus_tcp/z_sub_st.c +++ b/examples/freertos_plus_tcp/z_sub_st.c @@ -60,8 +60,10 @@ void app_main(void) { z_owned_closure_sample_t callback; z_closure(&callback, data_handler); printf("Declaring Subscriber on '%s'...\n", KEYEXPR); + 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_keyexpr(KEYEXPR), z_move(callback), NULL) < 0) { + 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 36052b73c..4c0f47c23 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -84,7 +84,9 @@ int main(int argc, char **argv) { } z_owned_closure_reply_t callback; z_closure_reply(&callback, reply_handler, reply_dropper, NULL); - if (z_get(z_session_loan(&s), z_keyexpr(KEYEXPR), "", z_closure_reply_move(&callback), &opts) < 0) { + 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); } diff --git a/examples/mbed/z_pub.cpp b/examples/mbed/z_pub.cpp index 4395edcdb..08bfd624b 100644 --- a/examples/mbed/z_pub.cpp +++ b/examples/mbed/z_pub.cpp @@ -61,7 +61,9 @@ int main(int argc, char **argv) { printf("Declaring publisher for '%s'...", KEYEXPR); z_owned_publisher_t pub; - if (z_declare_publisher(&pub, z_session_loan(&s), z_keyexpr(KEYEXPR), NULL) < 0) { + 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); } diff --git a/examples/mbed/z_pull.cpp b/examples/mbed/z_pull.cpp index 8beda1521..f011a4643 100644 --- a/examples/mbed/z_pull.cpp +++ b/examples/mbed/z_pull.cpp @@ -74,7 +74,7 @@ int main(int argc, char **argv) { 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); diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index dd9be6976..505da93e5 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -39,7 +39,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, &pred); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_str_loan(&keystr)), (int)z_view_str_loan(&pred)->len, z_view_str_loan(&pred)->val); - z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL); + z_query_reply(query, z_query_keyexpr(query), (const unsigned char *)VALUE, strlen(VALUE), NULL); z_str_drop(z_str_move(&keystr)); } @@ -76,8 +76,10 @@ int main(int argc, char **argv) { z_owned_closure_query_t callback; z_closure_query(&callback, query_handler, NULL, NULL); z_owned_queryable_t qable; - if (z_declare_queryable(&qable, z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_query_move(&callback), NULL) < - 0) { + 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); } diff --git a/examples/mbed/z_scout.cpp b/examples/mbed/z_scout.cpp index 0eeb27084..6940f284b 100644 --- a/examples/mbed/z_scout.cpp +++ b/examples/mbed/z_scout.cpp @@ -53,24 +53,26 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { void fprintlocators(FILE *stream, const z_str_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); - fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { - fprintf(stream, ", "); - } - } + (void)locs; + // TODO(sashacmc): z_str_array_t + // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + // fprintf(stream, "\""); + // fprintf(stream, "%s", *z_str_array_get(locs, i)); + // fprintf(stream, "\""); + // if (i < z_str_array_len(locs) - 1) { + // fprintf(stream, ", "); + // } + //} fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +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, " }"); } diff --git a/examples/mbed/z_sub.cpp b/examples/mbed/z_sub.cpp index 3c750f1c3..5b8a56716 100644 --- a/examples/mbed/z_sub.cpp +++ b/examples/mbed/z_sub.cpp @@ -71,8 +71,10 @@ int main(int argc, char **argv) { z_owned_closure_sample_t callback; z_closure_sample(&callback, data_handler, NULL, NULL); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_sample_move(&callback), NULL) < - 0) { + 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); } diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index c93a8fb66..16e7a8a2d 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -121,12 +121,11 @@ int main(int argc, char **argv) { return -1; } - // TODO(sashacmc): - // z_keyexpr_t ke = z_keyexpr(keyexpr); - // if (!z_check(ke)) { - // printf("%s is not a valid key expression", keyexpr); - // return -1; - // } + 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); @@ -143,7 +142,7 @@ int main(int argc, char **argv) { z_owned_closure_reply_t callback; z_closure(&callback, reply_handler, reply_dropper); - if (z_get(z_loan(s), z_keyexpr(keyexpr), "", z_move(callback), &opts) < 0) { + if (z_get(z_loan(s), z_loan(ke), "", z_move(callback), &opts) < 0) { printf("Unable to send query.\n"); return -1; } diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index 92a029574..4b85fb55d 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -81,12 +81,11 @@ int main(int argc, char **argv) { return -1; } - // TODO(sashacmc): - // z_keyexpr_t ke = z_keyexpr(keyexpr); - // if (!z_check(ke)) { - // printf("%s is not a valid key expression", keyexpr); - // return -1; - // } + 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; @@ -96,7 +95,7 @@ int main(int argc, char **argv) { } z_owned_reply_ring_channel_t channel; z_reply_ring_channel_new(&channel, 1); - if (z_get(z_loan(s), z_keyexpr(keyexpr), "", z_move(channel.send), &opts) < 0) { + if (z_get(z_loan(s), z_loan(ke), "", z_move(channel.send), &opts) < 0) { printf("Unable to send query.\n"); return -1; } diff --git a/examples/unix/c11/z_ping.c b/examples/unix/c11/z_ping.c index 8bb0078d6..fa7744d64 100644 --- a/examples/unix/c11/z_ping.c +++ b/examples/unix/c11/z_ping.c @@ -77,12 +77,13 @@ int main(int argc, char** argv) { return -1; } - // TODO(sashacmc): - // 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; - if (z_declare_publisher(&pub, z_loan(session), z_keyexpr("test/ping"), NULL) < 0) { + if (z_declare_publisher(&pub, z_loan(session), z_loan(ping), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return -1; } @@ -90,7 +91,7 @@ int main(int argc, char** argv) { 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_keyexpr("test/pong"), z_move(respond), NULL) < 0) { + 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; } diff --git a/examples/unix/c11/z_pong.c b/examples/unix/c11/z_pong.c index 0cc873643..8be8fb256 100644 --- a/examples/unix/c11/z_pong.c +++ b/examples/unix/c11/z_pong.c @@ -47,20 +47,20 @@ int main(int argc, char** argv) { return -1; } - // TODO(sashacmc): - // z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + 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_keyexpr("test/pong"), NULL) < 0) { + if (z_declare_publisher(&pub, z_loan(session), z_loan(pong), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); return -1; } - // TODO(sashacmc): - // z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + 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_keyexpr("test/ping"), z_move(respond), NULL) < 0) { + 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; } diff --git a/examples/unix/c11/z_pub.c b/examples/unix/c11/z_pub.c index 98d63fad6..8c78dc328 100644 --- a/examples/unix/c11/z_pub.c +++ b/examples/unix/c11/z_pub.c @@ -96,7 +96,9 @@ int main(int argc, char **argv) { } printf("Declaring publisher for '%s'...\n", keyexpr); z_owned_publisher_t pub; - if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(keyexpr), NULL) < 0) { + 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_st.c b/examples/unix/c11/z_pub_st.c index aeebe6c95..51037f4a4 100644 --- a/examples/unix/c11/z_pub_st.c +++ b/examples/unix/c11/z_pub_st.c @@ -82,7 +82,9 @@ int main(int argc, char **argv) { printf("Declaring publisher for '%s'...\n", keyexpr); z_owned_publisher_t pub; - if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(keyexpr), NULL) < 0) { + 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 ed25fe188..228e70f01 100644 --- a/examples/unix/c11/z_pub_thr.c +++ b/examples/unix/c11/z_pub_thr.c @@ -52,7 +52,9 @@ int main(int argc, char **argv) { } // Declare publisher z_owned_publisher_t pub; - if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(keyexpr), NULL) < 0) { + 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); } diff --git a/examples/unix/c11/z_pull.c b/examples/unix/c11/z_pull.c index 79d85ac6f..61a0f283a 100644 --- a/examples/unix/c11/z_pull.c +++ b/examples/unix/c11/z_pull.c @@ -76,7 +76,9 @@ int main(int argc, char **argv) { z_owned_sample_ring_channel_t channel; z_sample_ring_channel_new(&channel, size); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(channel.send), NULL) < 0) { + 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; } diff --git a/examples/unix/c11/z_put.c b/examples/unix/c11/z_put.c index fe5dc6023..2f13733b3 100644 --- a/examples/unix/c11/z_put.c +++ b/examples/unix/c11/z_put.c @@ -86,8 +86,10 @@ int main(int argc, char **argv) { } printf("Declaring key expression '%s'...\n", keyexpr); + 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_keyexpr(keyexpr)) < 0) { + 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_mut(s)); zp_stop_lease_task(z_loan_mut(s)); @@ -104,7 +106,7 @@ int main(int argc, char **argv) { 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"); } diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index 6fff4a6f5..27d1c69d4 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -35,7 +35,7 @@ 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), &keystr); - // TODO(sashacmc): + // TODO(sashacmc): z_query_parameters // 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_str_data(z_loan(keystr)), (int)pred.len, @@ -61,7 +61,7 @@ void query_handler(const z_loaned_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); + z_query_reply(query, z_query_keyexpr(query), (const unsigned char *)value, strlen(value), &options); z_drop(z_move(keystr)); msg_nb++; @@ -134,18 +134,17 @@ int main(int argc, char **argv) { return -1; } - // TODO(sashacmc): - // z_keyexpr_t ke = z_keyexpr(keyexpr); - // if (!z_check(ke)) { - // printf("%s is not a valid key expression", keyexpr); - // return -1; - // } + 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(&callback, query_handler); z_owned_queryable_t qable; - if (z_declare_queryable(&qable, z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL) < 0) { + if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to create queryable.\n"); return -1; } diff --git a/examples/unix/c11/z_queryable_channel.c b/examples/unix/c11/z_queryable_channel.c index 8c0e220d6..8487afab6 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -81,18 +81,17 @@ int main(int argc, char **argv) { return -1; } - // TODO(sashacmc): - // z_keyexpr_t ke = z_keyexpr(keyexpr); - // if (!z_check(ke)) { - // printf("%s is not a valid key expression", keyexpr); - // return -1; - // } + 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(&channel, 10); z_owned_queryable_t qable; - if (z_declare_queryable(&qable, z_loan(s), z_keyexpr(keyexpr), z_move(channel.send), NULL) < 0) { + 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; } @@ -113,7 +112,7 @@ int main(int argc, char **argv) { z_query_reply_options_t options; z_query_reply_options_default(&options); 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(q, z_loan(ke), (const unsigned char *)value, strlen(value), &options); z_drop(z_move(keystr)); z_drop(z_move(query)); } diff --git a/examples/unix/c11/z_sub.c b/examples/unix/c11/z_sub.c index a79a05583..3b1e0865d 100644 --- a/examples/unix/c11/z_sub.c +++ b/examples/unix/c11/z_sub.c @@ -114,7 +114,9 @@ int main(int argc, char **argv) { z_closure(&callback, data_handler); printf("Declaring Subscriber on '%s'...\n", keyexpr); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL) < 0) { + 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_channel.c b/examples/unix/c11/z_sub_channel.c index 032598777..83985a7c6 100644 --- a/examples/unix/c11/z_sub_channel.c +++ b/examples/unix/c11/z_sub_channel.c @@ -68,7 +68,9 @@ int main(int argc, char **argv) { z_owned_sample_fifo_channel_t channel; z_sample_fifo_channel_new(&channel, 3); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(channel.send), NULL) < 0) { + 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; } diff --git a/examples/unix/c11/z_sub_st.c b/examples/unix/c11/z_sub_st.c index 3a67c2805..0deee874f 100644 --- a/examples/unix/c11/z_sub_st.c +++ b/examples/unix/c11/z_sub_st.c @@ -91,7 +91,9 @@ int main(int argc, char **argv) { z_closure(&callback, data_handler); printf("Declaring Subscriber on '%s'...\n", keyexpr); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL) < 0) { + 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 5a1c8a685..0a7829489 100644 --- a/examples/unix/c11/z_sub_thr.c +++ b/examples/unix/c11/z_sub_thr.c @@ -96,7 +96,9 @@ int main(int argc, char **argv) { z_owned_closure_sample_t callback; z_closure(&callback, on_sample, drop_stats, (void *)context); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL) < 0) { + 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); } diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 2d0d307ce..dff38043f 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -107,8 +107,8 @@ int main(int argc, char **argv) { return -1; } - const z_loaned_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; } @@ -122,7 +122,7 @@ int main(int argc, char **argv) { } z_owned_closure_reply_t callback; z_closure_reply(&callback, reply_handler, reply_dropper, NULL); - if (z_get(z_session_loan(&s), ke, "", z_closure_reply_move(&callback), &opts) < 0) { + 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; } diff --git a/examples/unix/c99/z_ping.c b/examples/unix/c99/z_ping.c index 52b966795..17fe7833c 100644 --- a/examples/unix/c99/z_ping.c +++ b/examples/unix/c99/z_ping.c @@ -80,21 +80,21 @@ int main(int argc, char** argv) { return -1; } - // TODO(sashacmc): - // z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + 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_keyexpr("test/ping"), NULL) < 0) { + 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; } - // TODO(sashacmc): - // z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + 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_keyexpr("test/pong"), z_closure_sample_move(&respond), - NULL) < 0) { + 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; } diff --git a/examples/unix/c99/z_pong.c b/examples/unix/c99/z_pong.c index 97a86c3ed..533dc369a 100644 --- a/examples/unix/c99/z_pong.c +++ b/examples/unix/c99/z_pong.c @@ -50,19 +50,21 @@ int main(int argc, char** argv) { return -1; } - // TODO(sashacmc): z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + 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_keyexpr("test/pong"), NULL) < 0) { + 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; } - // TODO(sashacmc): z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + 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_keyexpr("test/ping"), z_closure_sample_move(&respond), - NULL) < 0) { + 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; } diff --git a/examples/unix/c99/z_pub.c b/examples/unix/c99/z_pub.c index 74376cd2f..6714a6e06 100644 --- a/examples/unix/c99/z_pub.c +++ b/examples/unix/c99/z_pub.c @@ -88,7 +88,9 @@ int main(int argc, char **argv) { printf("Declaring publisher for '%s'...\n", keyexpr); z_owned_publisher_t pub; - if (z_declare_publisher(&pub, z_session_loan(&s), z_keyexpr(keyexpr), NULL) < 0) { + 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_pub_st.c b/examples/unix/c99/z_pub_st.c index f576c8b07..d5d8220d8 100644 --- a/examples/unix/c99/z_pub_st.c +++ b/examples/unix/c99/z_pub_st.c @@ -81,7 +81,9 @@ int main(int argc, char **argv) { printf("Declaring publisher for '%s'...\n", keyexpr); z_owned_publisher_t pub; - if (z_declare_publisher(&pub, z_session_loan(&s), z_keyexpr(keyexpr), NULL) < 0) { + 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 77301516a..e93bd9db6 100644 --- a/examples/unix/c99/z_pull.c +++ b/examples/unix/c99/z_pull.c @@ -80,7 +80,7 @@ int main(int argc, char **argv) { 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; diff --git a/examples/unix/c99/z_put.c b/examples/unix/c99/z_put.c index 009803d8d..eb9e6ebf3 100644 --- a/examples/unix/c99/z_put.c +++ b/examples/unix/c99/z_put.c @@ -82,8 +82,10 @@ int main(int argc, char **argv) { } printf("Declaring key expression '%s'...\n", keyexpr); + 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_keyexpr(keyexpr)) < 0) { + 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_mut(&s)); zp_stop_lease_task(z_session_loan_mut(&s)); diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index ee38ab56a..7f2b4adc9 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -33,7 +33,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_reply_options_t options; z_query_reply_options_default(&options); 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(query, z_query_keyexpr(query), (const unsigned char *)value, strlen(value), &options); z_str_drop(z_str_move(&keystr)); } @@ -96,8 +96,8 @@ int main(int argc, char **argv) { return -1; } - const z_loaned_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; } @@ -107,7 +107,8 @@ int main(int argc, char **argv) { printf("Creating Queryable on '%s'...\n", keyexpr); z_owned_queryable_t qable; - if (z_declare_queryable(&qable, z_session_loan(&s), ke, z_closure_query_move(&callback), NULL) < 0) { + 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; } diff --git a/examples/unix/c99/z_scout.c b/examples/unix/c99/z_scout.c index 61206287f..cd07d9778 100644 --- a/examples/unix/c99/z_scout.c +++ b/examples/unix/c99/z_scout.c @@ -43,7 +43,7 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { void fprintlocators(FILE *stream, const z_str_array_t *locs) { fprintf(stream, "["); (void)locs; - // TODO(sashacmc): + // TODO(sashacmc): z_str_array_t // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { // fprintf(stream, "\""); // fprintf(stream, "%s", *z_str_array_get(locs, i)); diff --git a/examples/unix/c99/z_sub.c b/examples/unix/c99/z_sub.c index 37b0699e0..4276819ea 100644 --- a/examples/unix/c99/z_sub.c +++ b/examples/unix/c99/z_sub.c @@ -92,8 +92,10 @@ int main(int argc, char **argv) { printf("Declaring Subscriber on '%s'...\n", keyexpr); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_session_loan(&s), z_keyexpr(keyexpr), z_closure_sample_move(&callback), NULL) < - 0) { + 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/unix/c99/z_sub_st.c b/examples/unix/c99/z_sub_st.c index 03f1cbf35..5a3e83e02 100644 --- a/examples/unix/c99/z_sub_st.c +++ b/examples/unix/c99/z_sub_st.c @@ -93,8 +93,10 @@ int main(int argc, char **argv) { printf("Declaring Subscriber on '%s'...\n", keyexpr); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_session_loan(&s), z_keyexpr(keyexpr), z_closure_sample_move(&callback), NULL) < - 0) { + 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 a20d4a4bd..042909497 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -33,7 +33,7 @@ void reply_handler(z_owned_reply_t *reply, void *ctx) { if (z_reply_is_ok(reply)) { const z_loaned_sample_t *sample = z_reply_ok(reply); z_owned_str_t keystr; - z_keyexpr_to_string(sample.keyexpr, &keystr); + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); const z_loaned_bytes_t *payload = z_sample_payload(sample); printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); z_drop(z_move(keystr)); @@ -72,8 +72,8 @@ int main(int argc, char **argv) { 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; } @@ -87,7 +87,7 @@ int main(int argc, char **argv) { } z_owned_closure_reply_t callback; z_closure(&callback, reply_handler, reply_dropper); - if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { + if (z_get(z_loan(s), z_loan(ke), "", z_move(callback), &opts) < 0) { printf("Unable to send query.\n"); return -1; } diff --git a/examples/windows/z_ping.c b/examples/windows/z_ping.c index 850cea78a..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); @@ -77,18 +77,20 @@ int main(int argc, char** argv) { return -1; } - z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + 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), ping, NULL) < 0) { + 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_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), pong, z_move(respond), NULL) < 0) { + 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; } diff --git a/examples/windows/z_pong.c b/examples/windows/z_pong.c index 240f03da6..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; @@ -47,18 +47,20 @@ int main(int argc, char** argv) { return -1; } - z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + 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), pong, NULL) < 0) { + 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_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), ping, z_move(respond), NULL) < 0) { + 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; } diff --git a/examples/windows/z_pub.c b/examples/windows/z_pub.c index 29f32efad..5ef1da47a 100644 --- a/examples/windows/z_pub.c +++ b/examples/windows/z_pub.c @@ -50,7 +50,9 @@ int main(int argc, char **argv) { printf("Declaring publisher for '%s'...\n", keyexpr); z_owned_publisher_t pub; - if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(keyexpr), NULL) < 0) { + 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_pub_st.c b/examples/windows/z_pub_st.c index 2b7c2d76e..959fb2b6f 100644 --- a/examples/windows/z_pub_st.c +++ b/examples/windows/z_pub_st.c @@ -45,7 +45,9 @@ int main(int argc, char **argv) { printf("Declaring publisher for '%s'...\n", keyexpr); z_owned_publisher_t pub; - if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(keyexpr), NULL) < 0) { + 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 c8beb623a..5cc5be9ce 100644 --- a/examples/windows/z_pull.c +++ b/examples/windows/z_pull.c @@ -59,7 +59,7 @@ int main(int argc, char **argv) { // 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; diff --git a/examples/windows/z_put.c b/examples/windows/z_put.c index a9596286a..d009968a3 100644 --- a/examples/windows/z_put.c +++ b/examples/windows/z_put.c @@ -49,8 +49,10 @@ int main(int argc, char **argv) { } printf("Declaring key expression '%s'...\n", keyexpr); + 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_keyexpr(keyexpr)) < 0) { + 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_mut(s)); zp_stop_lease_task(z_loan_mut(s)); diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index 1fde0df81..0c409b597 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -25,16 +25,18 @@ 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), &keystr); - 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_str_data(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); - } + // TODO(sashacmc): + // 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_str_data(z_loan(keystr)), (int)pred.len, + // pred.start); + // if (payload_value.payload.len > 0) { + // printf(" with value '%.*s'\n", (int)payload_value.payload.len, payload_value.payload.start); + // } z_query_reply_options_t options; z_query_reply_options_default(&options); 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(query, z_query_keyexpr(query), (const unsigned char *)value, strlen(value), &options); z_drop(z_move(keystr)); } @@ -63,8 +65,8 @@ int main(int argc, char **argv) { 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; } @@ -73,7 +75,7 @@ int main(int argc, char **argv) { z_owned_closure_query_t callback; z_closure(&callback, query_handler); z_owned_queryable_t qable; - if (z_declare_queryable(&qable, z_loan(s), ke, z_move(callback), NULL) < 0) { + if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to create queryable.\n"); return -1; } diff --git a/examples/windows/z_scout.c b/examples/windows/z_scout.c index ea6a69130..a89ddd038 100644 --- a/examples/windows/z_scout.c +++ b/examples/windows/z_scout.c @@ -42,24 +42,26 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { void fprintlocators(FILE *stream, const z_str_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); - fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { - fprintf(stream, ", "); - } - } + (void)locs; + // TODO(sashacmc): z_str_array missed + // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + // fprintf(stream, "\""); + // fprintf(stream, "%s", *z_str_array_get(locs, i)); + // fprintf(stream, "\""); + // if (i < z_str_array_len(locs) - 1) { + // fprintf(stream, ", "); + // } + //} fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +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, " }"); } diff --git a/examples/windows/z_sub.c b/examples/windows/z_sub.c index 4b1441228..298d54b4f 100644 --- a/examples/windows/z_sub.c +++ b/examples/windows/z_sub.c @@ -60,7 +60,9 @@ int main(int argc, char **argv) { z_closure(&callback, data_handler); printf("Declaring Subscriber on '%s'...\n", keyexpr); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL) < 0) { + 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/windows/z_sub_st.c b/examples/windows/z_sub_st.c index 04ed79fe2..4276a4ecb 100644 --- a/examples/windows/z_sub_st.c +++ b/examples/windows/z_sub_st.c @@ -58,7 +58,9 @@ int main(int argc, char **argv) { z_closure(&callback, data_handler); printf("Declaring Subscriber on '%s'...\n", keyexpr); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL) < 0) { + 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 2dbe27234..658dfffa0 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -37,7 +37,7 @@ void reply_handler(z_owned_reply_t *oreply, void *ctx) { if (z_reply_is_ok(oreply)) { const z_loaned_sample_t *sample = z_reply_ok(oreply); z_owned_str_t keystr; - z_keyexpr_to_string(sample.keyexpr, &keystr); + z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); const z_loaned_bytes_t *payload = z_sample_payload(sample); printf(" >> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); z_drop(z_move(keystr)); @@ -78,7 +78,9 @@ int main(int argc, char **argv) { opts.target = Z_QUERY_TARGET_ALL; z_owned_closure_reply_t callback; z_closure(&callback, reply_handler, reply_dropper); - if (z_get(z_loan(s), z_keyexpr(KEYEXPR), "", z_move(callback), &opts) < 0) { + 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); } diff --git a/examples/zephyr/z_pub.c b/examples/zephyr/z_pub.c index b96e7ba74..209f91dc5 100644 --- a/examples/zephyr/z_pub.c +++ b/examples/zephyr/z_pub.c @@ -57,8 +57,10 @@ int main(int argc, char **argv) { zp_start_lease_task(z_loan_mut(s), NULL); printf("Declaring publisher for '%s'...", KEYEXPR); + 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_keyexpr(KEYEXPR), NULL) < 0) { + if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) { printf("Unable to declare publisher for key expression!\n"); exit(-1); } diff --git a/examples/zephyr/z_pull.c b/examples/zephyr/z_pull.c index f739edd26..cdab228bf 100644 --- a/examples/zephyr/z_pull.c +++ b/examples/zephyr/z_pull.c @@ -68,7 +68,7 @@ int main(int argc, char **argv) { // 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); diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index 5455aef8f..3950bc1b8 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -35,9 +35,11 @@ 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), &keystr); - z_bytes_t pred = z_query_parameters(query); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)pred.len, pred.start); - z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL); + // TODO(sashacmc): z_query_parameters + // z_bytes_t pred = z_query_parameters(query); + // printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)pred.len, + // pred.start); + z_query_reply(query, z_query_keyexpr(query), (const unsigned char *)VALUE, strlen(VALUE), NULL); z_drop(z_move(keystr)); } @@ -70,7 +72,9 @@ int main(int argc, char **argv) { 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_keyexpr(KEYEXPR), z_move(callback), NULL) < 0) { + 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); } diff --git a/examples/zephyr/z_scout.c b/examples/zephyr/z_scout.c index 718d6675a..3f1c9c93e 100644 --- a/examples/zephyr/z_scout.c +++ b/examples/zephyr/z_scout.c @@ -39,24 +39,26 @@ void fprintwhatami(FILE *stream, unsigned int whatami) { void fprintlocators(FILE *stream, const z_str_array_t *locs) { fprintf(stream, "["); - for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - fprintf(stream, "\""); - fprintf(stream, "%s", *z_str_array_get(locs, i)); - fprintf(stream, "\""); - if (i < z_str_array_len(locs) - 1) { - fprintf(stream, ", "); - } - } + (void)locs; + // TODO(sashacmc): z_str_array missed + // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { + // fprintf(stream, "\""); + // fprintf(stream, "%s", *z_str_array_get(locs, i)); + // fprintf(stream, "\""); + // if (i < z_str_array_len(locs) - 1) { + // fprintf(stream, ", "); + // } + //} fprintf(stream, "]"); } -void fprinthello(FILE *stream, const z_hello_t hello) { +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, " }"); } diff --git a/examples/zephyr/z_sub.c b/examples/zephyr/z_sub.c index eddd62b14..362b249a2 100644 --- a/examples/zephyr/z_sub.c +++ b/examples/zephyr/z_sub.c @@ -65,8 +65,10 @@ int main(int argc, char **argv) { printf("Declaring Subscriber on '%s'...", KEYEXPR); 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_keyexpr(KEYEXPR), z_move(callback), NULL) < 0) { + if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) { printf("Unable to declare subscriber.\n"); exit(-1); } diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 35d13ed14..e58db7b68 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -35,6 +35,7 @@ #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, \ diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 5cfb25aaf..f2d135868 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -58,7 +58,7 @@ int8_t z_view_str_wrap(z_view_str_t *str, const char *value); * Returns: * The :c:type:`z_keyexpr_t` corresponding to the given string. */ -int8_t z_view_keyexpr_new(z_view_keyexpr_t *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. @@ -74,7 +74,7 @@ int8_t z_view_keyexpr_new(z_view_keyexpr_t *keyexpr, const char *name); // TODO(sashacmc): z_owned_keyexpr_t z_keyexpr_unchecked(const char *name); -int8_t z_view_keyexpr_unchecked(z_view_keyexpr_t *keyexpr, 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`. @@ -799,9 +799,6 @@ _VIEW_FUNCTIONS(z_loaned_str_t, z_view_str_t, str) // Gets internal value from refcountered owned type (e.g. z_owned_session_t, z_owned_query_t) #define _Z_OWNED_RC_IN_VAL(arg) ((arg)->_rc.in->val) -// TODO(sashacmc): remove this VERY BAD workaround (it is only to allow compilaton)!!! -z_loaned_keyexpr_t *z_keyexpr(const char *name); - // TODO(sashacmc): comments, docs, etc. const z_loaned_sample_t *z_sample_loan(const z_owned_sample_t *sample); const char *z_str_data(const z_loaned_str_t *str); @@ -1147,7 +1144,7 @@ void z_get_options_default(z_get_options_t *options); * Returns: * Returns ``0`` if the put operation is successful, or a ``negative value`` otherwise. */ -int8_t z_get(const z_loaned_session_t *zs, z_loaned_keyexpr_t *keyexpr, const char *parameters, +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, const z_get_options_t *options); /** * Checks if the queryable answered with an OK, which allows this value to be treated as a sample. diff --git a/src/api/api.c b/src/api/api.c index a18bab192..cfbdf0d2b 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -41,13 +41,6 @@ #include "zenoh-pico/utils/result.h" #include "zenoh-pico/utils/uuid.h" -// TODO(sashacmc): remove this VERY BAD workaround (it is only to allow compilaton)!!! -z_loaned_keyexpr_t *z_keyexpr(const char *name) { - static z_view_keyexpr_t keyexpr; - z_view_keyexpr_new(&keyexpr, name); - return (z_loaned_keyexpr_t *)z_view_keyexpr_loan(&keyexpr); -} - /********* Data Types Handlers *********/ int8_t z_view_str_wrap(z_view_str_t *str, const char *value) { @@ -60,12 +53,12 @@ int8_t z_view_str_wrap(z_view_str_t *str, const char *value) { // 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); } -int8_t z_view_keyexpr_new(z_view_keyexpr_t *keyexpr, const char *name) { +int8_t z_view_keyexpr_from_string(z_view_keyexpr_t *keyexpr, const char *name) { keyexpr->_val = _z_rname(name); return _Z_RES_OK; } -int8_t z_view_keyexpr_unchecked(z_view_keyexpr_t *keyexpr, const char *name) { +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; } @@ -818,7 +811,7 @@ void __z_reply_handler(_z_reply_t *reply, __z_reply_handler_wrapper_t *wrapped_c z_reply_drop(&oreply); // user_call is allowed to take ownership of the reply by setting oreply._val to NULL } -int8_t z_get(const z_loaned_session_t *zs, z_loaned_keyexpr_t *keyexpr, const char *parameters, +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, const z_get_options_t *options) { int8_t ret = _Z_RES_OK; diff --git a/src/session/subscription.c b/src/session/subscription.c index 48397577e..c76bb6b87 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -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/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/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 3f4a4fc14..6c8d46060 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) @@ -65,8 +66,9 @@ void query_handler(const z_loaned_query_t *query, void *arg) { printf("%s\n", __func__); queries++; + const z_loaned_keyexpr_t *query_ke = z_query_keyexpr(query); z_owned_str_t k_str; - z_keyexpr_to_string(z_query_keyexpr(query), &k_str); + z_keyexpr_to_string(query_ke, &k_str); #ifdef ZENOH_PICO if (z_check(k_str) == false) { zp_keyexpr_resolve(*(const z_loaned_session_t **)arg, z_query_keyexpr(query), &k_str); @@ -80,7 +82,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { (void)(payload_value); z_query_reply_options_t _ret_qreply_opt; z_query_reply_options_default(&_ret_qreply_opt); - z_query_reply(query, z_keyexpr(z_loan(k_str)->val), (const uint8_t *)value, strlen(value), &_ret_qreply_opt); + z_query_reply(query, query_ke, (const uint8_t *)value, strlen(value), &_ret_qreply_opt); z_drop(z_move(k_str)); } @@ -134,37 +136,37 @@ int main(int argc, char **argv) { zc_init_logger(); #endif - printf("Testing Keyexpr..."); - const z_loaned_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 @@ -173,11 +175,11 @@ 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"); @@ -292,19 +294,22 @@ int main(int argc, char **argv) { 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_keyexpr(keyexpr_str), z_move(_ret_closure_sample), &_ret_sub_opt); + _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); + 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_keyexpr(s1_res)); + z_declare_keyexpr(&_ret_expr, z_loan(s1), z_loan(s1_key)); assert(z_check(_ret_expr)); printf("Ok\n"); @@ -346,7 +351,7 @@ int main(int argc, char **argv) { z_publisher_options_default(&_ret_pub_opt); _ret_pub_opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; z_owned_publisher_t _ret_pub; - _ret_int8 = z_declare_publisher(&_ret_pub, z_loan(s1), z_keyexpr(keyexpr_str), &_ret_pub_opt); + _ret_int8 = z_declare_publisher(&_ret_pub, z_loan(s1), z_loan(ke), &_ret_pub_opt); assert(_ret_int8 == _Z_RES_OK); printf("Ok\n"); @@ -394,7 +399,7 @@ int main(int argc, char **argv) { 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_keyexpr(s1_res), z_move(_ret_closure_query), &_ret_qle_opt) == + 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"); @@ -420,7 +425,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"); @@ -452,7 +457,6 @@ int main(int argc, char **argv) { sleep(SLEEP * 5); - free(s1_res); free(keyexpr_str); return 0; diff --git a/tests/z_client_test.c b/tests/z_client_test.c index b4cbdd2a3..d1a60de03 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -61,7 +61,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { 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); + z_query_reply(query, z_query_keyexpr(query), (const uint8_t *)res, strlen(res), NULL); queries++; z_drop(z_move(k_str)); @@ -160,8 +160,10 @@ 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_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_keyexpr(s1_res)); + 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; } @@ -170,8 +172,10 @@ int main(int argc, char **argv) { for (unsigned int i = 0; i < SET; i++) { snprintf(s1_res, 64, "%s%u", uri, i); + 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_keyexpr(s1_res)); + 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; } @@ -197,7 +201,9 @@ int main(int argc, char **argv) { 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)); - assert(z_declare_queryable(qle, z_loan(s2), z_keyexpr(s1_res), &callback, NULL) == _Z_RES_OK); + 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); } @@ -287,7 +293,9 @@ int main(int argc, char **argv) { snprintf(s1_res, 64, "%s%u", uri, i); z_owned_closure_reply_t callback; z_closure(&callback, reply_handler, NULL, &idx[i]); - z_get(z_loan(s1), z_keyexpr(s1_res), "", &callback, NULL); + 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); } } diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 2e7df7cc1..01906c594 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -115,7 +115,9 @@ int main(int argc, char **argv) { 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)); - int8_t res = z_declare_subscriber(sub, z_loan(s2), z_keyexpr(s1_res), &callback, NULL); + 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); @@ -135,7 +137,9 @@ int main(int argc, char **argv) { z_put_options_t opt; z_put_options_default(&opt); opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; - z_put(z_loan(s1), z_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); } } diff --git a/tests/z_perf_rx.c b/tests/z_perf_rx.c index 88e106786..e3324f01d 100644 --- a/tests/z_perf_rx.c +++ b/tests/z_perf_rx.c @@ -102,7 +102,9 @@ int main(int argc, char **argv) { z_owned_closure_sample_t callback; z_closure(&callback, on_sample, NULL, (void *)&test_stats); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL) < 0) { + 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); } diff --git a/tests/z_perf_tx.c b/tests/z_perf_tx.c index cac34429d..be324d3b7 100644 --- a/tests/z_perf_tx.c +++ b/tests/z_perf_tx.c @@ -77,7 +77,9 @@ int main(int argc, char **argv) { } // Declare publisher z_owned_publisher_t pub; - if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(keyexpr), NULL) < 0) { + 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); } diff --git a/tests/z_test_fragment_rx.c b/tests/z_test_fragment_rx.c index 368f976be..81d6b27c2 100644 --- a/tests/z_test_fragment_rx.c +++ b/tests/z_test_fragment_rx.c @@ -79,7 +79,9 @@ int main(int argc, char **argv) { z_owned_closure_sample_t callback; z_closure(&callback, data_handler); z_owned_subscriber_t sub; - if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL) < 0) { + 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/tests/z_test_fragment_tx.c b/tests/z_test_fragment_tx.c index 356c9453f..0e6658565 100644 --- a/tests/z_test_fragment_tx.c +++ b/tests/z_test_fragment_tx.c @@ -74,9 +74,11 @@ 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); From 5163aaa4bda3528b75eaae7946305fd7047f9abd Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Thu, 16 May 2024 09:31:24 +0200 Subject: [PATCH 03/43] feat: remove reply callback indirection --- include/zenoh-pico/net/primitives.h | 2 +- include/zenoh-pico/session/session.h | 6 ++---- src/api/api.c | 26 ++------------------------ src/net/primitives.c | 5 ++--- src/session/query.c | 10 +++------- 5 files changed, 10 insertions(+), 39 deletions(-) diff --git a/include/zenoh-pico/net/primitives.h b/include/zenoh-pico/net/primitives.h index 8fa286f82..9812db2a7 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -225,7 +225,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/session/session.h b/include/zenoh-pico/session/session.h index 17c1157ae..d142da94f 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -110,19 +110,17 @@ _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; /** * 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)(_z_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/src/api/api.c b/src/api/api.c index cfbdf0d2b..dedc1c7a7 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -799,19 +799,7 @@ void z_get_options_default(z_get_options_t *options) { 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 = {._val = 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._val to NULL -} - -int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, const char *parameters, +int8_t z_get(const z_loaned_session_t *zs, z_loaned_keyexpr_t *keyexpr, const char *parameters, z_owned_closure_reply_t *callback, const z_get_options_t *options) { int8_t ret = _Z_RES_OK; @@ -837,18 +825,8 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co opt.consolidation.mode = Z_CONSOLIDATION_MODE_LATEST; } } - - // 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(&_Z_RC_IN_VAL(zs), *keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, - __z_reply_handler, wrapped_ctx, callback->drop, ctx, opt.timeout_ms + callback->call, callback->drop, ctx, opt.timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , opt.attachment diff --git a/src/net/primitives.c b/src/net/primitives.c index 84e767b68..8724f25a8 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -383,7 +383,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 +403,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) { diff --git a/src/session/query.c b/src/session/query.c index 27dbceabf..4d049e698 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); } @@ -181,7 +177,7 @@ 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); + pen_qry->_callback(_z_reply_alloc_and_move(&reply), pen_qry->_arg); } if (ret != _Z_RES_OK) { @@ -208,7 +204,7 @@ 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); + pen_qry->_callback(_z_reply_alloc_and_move(&pen_rep->_reply), pen_qry->_arg); pen_qry->_pending_replies = _z_pending_reply_list_pop(pen_qry->_pending_replies, NULL); } } From 7fb26902b21968c974c0a6ff912d81e9c99f86ca Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Thu, 16 May 2024 15:22:34 +0200 Subject: [PATCH 04/43] feat: add owned_reply and loaned_reply --- include/zenoh-pico/api/handlers.h | 14 +++++------ include/zenoh-pico/api/macros.h | 11 +++++---- include/zenoh-pico/api/primitives.h | 35 +++++++++++++++++++++++++--- include/zenoh-pico/api/types.h | 29 +++++++++++++++++++---- include/zenoh-pico/session/session.h | 4 +++- src/api/api.c | 18 ++++++++++---- src/api/handlers.c | 12 ++++------ 7 files changed, 91 insertions(+), 32 deletions(-) diff --git a/include/zenoh-pico/api/handlers.h b/include/zenoh-pico/api/handlers.h index b955d2a4e..ae1c8cf6d 100644 --- a/include/zenoh-pico/api/handlers.h +++ b/include/zenoh-pico/api/handlers.h @@ -33,7 +33,7 @@ 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, \ @@ -112,13 +112,13 @@ _Z_CHANNEL_DEFINE(query_fifo_channel, closure_query, closure_owned_query, const _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 e58db7b68..942c1622b 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -46,7 +46,7 @@ z_view_str_t : z_view_str_loan, \ z_owned_str_array_t : z_str_array_loan, \ z_owned_sample_t : z_sample_loan, \ - z_owned_query_t : z_query_loan \ + z_owned_query_t : z_query_loan \ )(&x) #define z_loan_mut(x) _Generic((x), \ @@ -120,7 +120,7 @@ z_owned_hello_t : z_hello_check, \ z_owned_str_t : z_str_check, \ z_owned_str_array_t : z_str_array_check, \ - z_owned_bytes_t : z_bytes_check, \ + z_owned_bytes_t : z_bytes_check, \ z_owned_sample_t : z_sample_check, \ z_owned_query_t : z_query_check \ )(&x) @@ -138,7 +138,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__) /** @@ -358,8 +359,10 @@ inline void z_call(const z_owned_closure_query_t &closure, const z_query_t *quer { 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_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) diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index f2d135868..f6a3ebc8f 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -688,9 +688,37 @@ int8_t z_closure_owned_query(z_owned_closure_owned_query_t *closure, _z_owned_qu * Returns: * Returns a new reply closure. */ -int8_t z_closure_reply(z_owned_closure_reply_t *closure, z_owned_reply_handler_t call, _z_dropper_handler_t drop, +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 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. + * + * Returns: + * Returns a new reply closure. + */ +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 hello closure. * It consists on a structure that contains all the elements for stateful, memory-leak-free callbacks. @@ -782,6 +810,7 @@ _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) @@ -1159,7 +1188,7 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co * Returns ``true`` if the queryable answered with an OK, which allows this value to be treated as a sample, or * ``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. @@ -1172,7 +1201,7 @@ _Bool z_reply_is_ok(const z_owned_reply_t *reply); * Returns: * Returns the :c:type:`z_loaned_sample_t` wrapped in the query reply. */ -const 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. diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 556b94352..424b50c66 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -181,7 +181,6 @@ _LOANED_TYPE(_z_queryable_t, queryable) * Represents a Zenoh query entity, received by Zenoh Queryable entities. * */ - _OWNED_TYPE_RC(_z_query_rc_t, query) _LOANED_TYPE(_z_query_rc_t, query) @@ -459,8 +458,8 @@ typedef _z_reply_data_t z_reply_data_t; * Members: * z_reply_data_t data: the content of the reply. */ -_OWNED_TYPE_PTR(_z_reply_t, reply) -_LOANED_TYPE(_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``. @@ -573,6 +572,26 @@ 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: + * _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. + */ +// TODO(sashacmc): +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: * 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. @@ -583,9 +602,9 @@ typedef struct { void *context; z_owned_reply_handler_t call; _z_dropper_handler_t drop; -} z_owned_closure_reply_t; +} 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); diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index d142da94f..21169f1b5 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -110,10 +110,12 @@ _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; +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, void *arg); +typedef void (*_z_reply_handler_t)(const z_loaned_reply_t *reply, void *arg); typedef struct { _z_keyexpr_t _key; diff --git a/src/api/api.c b/src/api/api.c index dedc1c7a7..462dd21fb 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -336,7 +336,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); } @@ -443,7 +449,9 @@ OWNED_FUNCTIONS_CLOSURE(z_owned_closure_owned_sample_t, closure_owned_sample, _z 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_owned_reply_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) @@ -786,7 +794,7 @@ z_owned_keyexpr_t z_publisher_keyexpr(z_loaned_publisher_t *publisher) { #endif #if Z_FEATURE_QUERY == 1 -OWNED_FUNCTIONS_PTR(_z_reply_t, reply, _z_owner_noop_copy, _z_reply_free) +OWNED_FUNCTIONS_RC(reply) void z_get_options_default(z_get_options_t *options) { options->target = z_query_target_default(); @@ -835,14 +843,14 @@ int8_t z_get(const z_loaned_session_t *zs, z_loaned_keyexpr_t *keyexpr, const ch return ret; } -_Bool z_reply_is_ok(const z_owned_reply_t *reply) { +_Bool z_reply_is_ok(const z_loaned_reply_t *reply) { (void)(reply); // For the moment always return TRUE. // The support for reply errors will come in the next release. return true; } -const z_loaned_sample_t *z_reply_ok(const z_owned_reply_t *reply) { return &reply->_val->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); diff --git a/src/api/handlers.c b/src/api/handlers.c index e133a5835..90c8cacf4 100644 --- a/src/api/handlers.c +++ b/src/api/handlers.c @@ -45,6 +45,9 @@ void _z_owned_query_move(z_owned_query_t *dst, z_owned_query_t *src) { z_owned_query_t *_z_query_to_owned_ptr(const z_loaned_query_t *src) { z_owned_query_t *dst = (z_owned_query_t *)zp_malloc(sizeof(z_owned_query_t)); + if (dst == NULL) { + return NULL; + } _z_query_rc_copy(&dst->_rc, src); return dst; } @@ -57,17 +60,12 @@ 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->_val) { - dst->_val = (_z_reply_t *)zp_malloc(sizeof(_z_reply_t)); - _z_reply_copy(dst->_val, src->_val); - } else { - dst->_val = NULL; - } + _z_reply_rc_copy(&dst->_rc, src); return dst; } #endif // Z_FEATURE_QUERY From a3cc873c572402a7a3ccdb85d5fdc8f2fdd8e611 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Thu, 16 May 2024 15:23:13 +0200 Subject: [PATCH 05/43] feat: implement reply as rc for query callbacks --- include/zenoh-pico/net/reply.h | 9 +++++- src/net/reply.c | 56 ++++++++++++++++++++++++++++++---- src/session/query.c | 29 +++++++++--------- 3 files changed, 73 insertions(+), 21 deletions(-) diff --git a/include/zenoh-pico/net/reply.h b/include/zenoh-pico/net/reply.h index ad8e7de35..a01ecf9ad 100644 --- a/include/zenoh-pico/net/reply.h +++ b/include/zenoh-pico/net/reply.h @@ -37,7 +37,7 @@ typedef struct _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 +55,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/src/net/reply.c b/src/net/reply.c index f9ece5032..756ea7b69 100644 --- a/src/net/reply.c +++ b/src/net/reply.c @@ -13,9 +13,19 @@ #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 = 0, + .sample = {.in = NULL}, + }}; + return r; +} + #if Z_FEATURE_QUERY == 1 void _z_reply_data_clear(_z_reply_data_t *reply_data) { _z_sample_rc_drop(&reply_data->sample); @@ -38,12 +48,9 @@ void _z_reply_data_copy(_z_reply_data_t *dst, _z_reply_data_t *src) { 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(const _z_keyexpr_t *keyexpr, z_reply_tag_t tag, z_id_t id, const _z_bytes_t *payload, + const _z_timestamp_t *timestamp, const _z_encoding_t encoding, const z_sample_kind_t kind, + const 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/session/query.c b/src/session/query.c index 4d049e698..17f909788 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -113,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.in->val.keyexpr = expanded_ke; - _z_bytes_copy(&reply.data.sample.in->val.payload, &msg->_payload); - reply.data.sample.in->val.encoding.id = msg->_encoding.id; - _z_bytes_copy(&reply.data.sample.in->val.encoding.schema, &msg->_encoding.schema); - reply.data.sample.in->val.kind = Z_SAMPLE_KIND_PUT; - reply.data.sample.in->val.timestamp = _z_timestamp_duplicate(&msg->_commons._timestamp); + // Retrieve attachment #if Z_FEATURE_ATTACHMENT == 1 - reply.data.sample.in->val.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) || @@ -147,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); } @@ -177,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->_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) { @@ -204,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->_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); } } From 4da9aef23c324f576b69a769f24cb20557c04432 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Thu, 16 May 2024 16:18:08 +0200 Subject: [PATCH 06/43] fix: change z_reply_err args --- include/zenoh-pico/api/primitives.h | 2 +- src/api/api.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index f6a3ebc8f..418e8e054 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -1214,7 +1214,7 @@ const z_loaned_sample_t *z_reply_ok(const z_loaned_reply_t *reply); * Returns: * Returns the :c:type:`z_value_t` wrapped in the query reply. */ -z_value_t z_reply_err(const z_owned_reply_t *reply); +z_value_t z_reply_err(const z_loaned_reply_t *reply); #endif #if Z_FEATURE_QUERYABLE == 1 diff --git a/src/api/api.c b/src/api/api.c index 462dd21fb..c5f2060d2 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -844,7 +844,7 @@ int8_t z_get(const z_loaned_session_t *zs, z_loaned_keyexpr_t *keyexpr, const ch } _Bool z_reply_is_ok(const z_loaned_reply_t *reply) { - (void)(reply); + _ZP_UNUSED(reply); // For the moment always return TRUE. // The support for reply errors will come in the next release. return true; @@ -852,8 +852,8 @@ _Bool z_reply_is_ok(const z_loaned_reply_t *reply) { const z_loaned_sample_t *z_reply_ok(const z_loaned_reply_t *reply) { return &reply->in->val.data.sample; } -z_value_t z_reply_err(const z_owned_reply_t *reply) { - (void)(reply); +z_value_t z_reply_err(const z_loaned_reply_t *reply) { + _ZP_UNUSED(reply); return (z_value_t){.payload = _z_bytes_empty(), .encoding = z_encoding_default()}; } #endif From 0e4f783c69aad264976e8e8d8cd64b8487eb908f Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Thu, 16 May 2024 16:18:25 +0200 Subject: [PATCH 07/43] fix: update tests with new reply rc --- tests/z_api_alignment_test.c | 5 +---- tests/z_client_test.c | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 6c8d46060..b95d1b8cc 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -88,7 +88,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { } 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++; @@ -107,9 +107,6 @@ void reply_handler(z_owned_reply_t *reply, void *arg) { z_value_t _ret_zvalue = z_reply_err(reply); (void)(_ret_zvalue); } - - z_null(reply); - z_drop(reply); // validate double-drop safety: caller drops reply if it's not dropped by the handler } volatile unsigned int datas = 0; diff --git a/tests/z_client_test.c b/tests/z_client_test.c index d1a60de03..4015f778a 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -69,7 +69,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { } 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)) { From 539b1563a31da161379ed087ffedc2c31233e2f2 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Thu, 16 May 2024 16:18:45 +0200 Subject: [PATCH 08/43] fix: update examples with new reply rc --- examples/arduino/z_get.ino | 2 +- examples/espidf/z_get.c | 2 +- examples/freertos_plus_tcp/z_get.c | 2 +- examples/mbed/z_get.cpp | 2 +- examples/unix/c11/z_get.c | 2 +- examples/unix/c11/z_get_channel.c | 4 ++-- examples/unix/c99/z_get.c | 2 +- examples/windows/z_get.c | 2 +- examples/zephyr/z_get.c | 2 +- 9 files changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index 5f86ee450..73773cd75 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -42,7 +42,7 @@ 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)) { const z_loaned_sample_t *sample = z_reply_ok(oreply); diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index d45afa073..1dae905c0 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -103,7 +103,7 @@ 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)) { const z_loaned_sample_t *sample = z_reply_ok(oreply); z_owned_str_t keystr; diff --git a/examples/freertos_plus_tcp/z_get.c b/examples/freertos_plus_tcp/z_get.c index c4177132b..6e3145748 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -36,7 +36,7 @@ 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)) { const z_loaned_sample_t *sample = z_reply_ok(reply); diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index 4c0f47c23..e16387948 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -33,7 +33,7 @@ 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)) { const z_loaned_sample_t *sample = z_reply_ok(oreply); z_owned_str_t keystr; diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index 16e7a8a2d..f810adfbf 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -37,7 +37,7 @@ 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)) { const z_loaned_sample_t *sample = z_reply_ok(reply); diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index 4b85fb55d..ed96149fc 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -103,8 +103,8 @@ int main(int argc, char **argv) { 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)) { - const z_loaned_sample_t *sample = z_reply_ok(&reply); + if (z_reply_is_ok(z_loan(reply))) { + const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply)); z_owned_str_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len, diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index dff38043f..457b5ecc4 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -29,7 +29,7 @@ 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)) { const z_loaned_sample_t *sample = z_reply_ok(reply); diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index 042909497..e614f4f77 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -28,7 +28,7 @@ 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)) { const z_loaned_sample_t *sample = z_reply_ok(reply); diff --git a/examples/zephyr/z_get.c b/examples/zephyr/z_get.c index 658dfffa0..0a0d8cf1b 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -33,7 +33,7 @@ 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)) { const z_loaned_sample_t *sample = z_reply_ok(oreply); z_owned_str_t keystr; From 9b743904c85004f84fe1ce8919c5ad2bec7cad38 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 17 May 2024 09:25:49 +0200 Subject: [PATCH 09/43] fix: unify owned_to_ptr functions --- src/api/handlers.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/api/handlers.c b/src/api/handlers.c index 90c8cacf4..c136e97b0 100644 --- a/src/api/handlers.c +++ b/src/api/handlers.c @@ -28,10 +28,10 @@ z_owned_sample_t *_z_sample_to_owned_ptr(const z_loaned_sample_t *src) { if (dst == NULL) { return NULL; } - if (src != NULL) { - dst->_rc = _z_sample_rc_clone(src); - } else { + if (src == NULL) { dst->_rc.in = NULL; + } else { + _z_sample_rc_copy(&dst->_rc, src); } return dst; } @@ -48,7 +48,11 @@ z_owned_query_t *_z_query_to_owned_ptr(const z_loaned_query_t *src) { if (dst == NULL) { return NULL; } - _z_query_rc_copy(&dst->_rc, src); + if (src == NULL) { + dst->_rc.in = NULL; + } else { + _z_query_rc_copy(&dst->_rc, src); + } return dst; } #endif // Z_FEATURE_QUERYABLE @@ -65,7 +69,11 @@ z_owned_reply_t *_z_reply_to_owned_ptr(const z_loaned_reply_t *src) { if (dst == NULL) { return NULL; } - _z_reply_rc_copy(&dst->_rc, src); + if (src == NULL) { + dst->_rc.in = NULL; + } else { + _z_reply_rc_copy(&dst->_rc, src); + } return dst; } #endif // Z_FEATURE_QUERY From a8c5dde5616dda34ca17d73cedf746e25236325a Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 17 May 2024 09:26:47 +0200 Subject: [PATCH 10/43] fix: dummy z_reply_create function args --- src/net/reply.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/net/reply.c b/src/net/reply.c index 756ea7b69..c87c65e64 100644 --- a/src/net/reply.c +++ b/src/net/reply.c @@ -107,9 +107,9 @@ _z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id, return reply; } #else -_z_reply_t _z_reply_create(const _z_keyexpr_t *keyexpr, z_reply_tag_t tag, z_id_t id, const _z_bytes_t *payload, - const _z_timestamp_t *timestamp, const _z_encoding_t encoding, const z_sample_kind_t kind, - const z_attachment_t att) { +_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); From 827c321bc5968766244265c0bd0ad9af5af2a1e3 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 17 May 2024 11:49:54 +0200 Subject: [PATCH 11/43] style: missing space on macros.h --- include/zenoh-pico/api/macros.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 942c1622b..17a19f2f3 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -46,7 +46,7 @@ z_view_str_t : z_view_str_loan, \ z_owned_str_array_t : z_str_array_loan, \ z_owned_sample_t : z_sample_loan, \ - z_owned_query_t : z_query_loan \ + z_owned_query_t : z_query_loan \ )(&x) #define z_loan_mut(x) _Generic((x), \ From 9315dba70a29772581622eafa2d3b72881cb3a58 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Tue, 21 May 2024 09:47:08 +0200 Subject: [PATCH 12/43] fix: updating exposed types --- include/zenoh-pico/api/macros.h | 10 ++--- include/zenoh-pico/api/primitives.h | 20 +++++----- include/zenoh-pico/api/types.h | 60 +++++++++++++++-------------- src/api/api.c | 22 +++++------ src/net/reply.c | 2 +- tests/z_perf_rx.c | 1 + 6 files changed, 60 insertions(+), 55 deletions(-) diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 17a19f2f3..18ea8acc4 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -351,15 +351,15 @@ 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 void z_call(const z_owned_closure_sample_t &closure, const z_sample_t *sample) +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) +inline void z_call(const z_owned_closure_owned_sample_t &closure, const z_owned_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) +inline void z_call(const z_owned_closure_owned_query_t &closure, const z_owned_query_t *query) { z_closure_owned_query_call(&closure, query); } -inline void z_call(const z_owned_closure_reply_t &closure, const z_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); } diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 418e8e054..622274f5c 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -584,7 +584,7 @@ const z_loaned_keyexpr_t *z_query_keyexpr(const z_loaned_query_t *query); * Returns: * Returns a new sample closure. */ -int8_t z_closure_sample(z_owned_closure_sample_t *closure, _z_data_handler_t call, _z_dropper_handler_t drop, +int8_t z_closure_sample(z_owned_closure_sample_t *closure, z_data_handler_t call, z_dropper_handler_t drop, void *context); /** @@ -608,8 +608,8 @@ int8_t z_closure_sample(z_owned_closure_sample_t *closure, _z_data_handler_t cal * Returns: * Returns a new sample closure. */ -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); +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. @@ -636,7 +636,7 @@ int8_t z_closure_owned_sample(z_owned_closure_owned_sample_t *closure, _z_owned_ * Returns: * Returns a new query closure. */ -int8_t z_closure_query(z_owned_closure_query_t *closure, _z_queryable_handler_t call, _z_dropper_handler_t drop, +int8_t z_closure_query(z_owned_closure_query_t *closure, z_queryable_handler_t call, z_dropper_handler_t drop, void *context); /** @@ -660,8 +660,8 @@ int8_t z_closure_query(z_owned_closure_query_t *closure, _z_queryable_handler_t * Returns: * Returns a new query closure. */ -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); +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. @@ -688,7 +688,7 @@ int8_t z_closure_owned_query(z_owned_closure_owned_query_t *closure, _z_owned_qu * Returns: * Returns a new reply closure. */ -int8_t z_closure_reply(z_owned_closure_reply_t *closure, _z_reply_handler_t call, _z_dropper_handler_t drop, +int8_t z_closure_reply(z_owned_closure_reply_t *closure, z_reply_handler_t call, z_dropper_handler_t drop, void *context); /** @@ -717,7 +717,7 @@ int8_t z_closure_reply(z_owned_closure_reply_t *closure, _z_reply_handler_t call * Returns a new reply closure. */ 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); + z_dropper_handler_t drop, void *context); /** * Return a new hello closure. @@ -744,7 +744,7 @@ int8_t z_closure_owned_reply(z_owned_closure_owned_reply_t *closure, z_owned_rep * Returns: * Returns a new hello closure. */ -int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_owned_hello_handler_t call, _z_dropper_handler_t drop, +int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_owned_hello_handler_t call, z_dropper_handler_t drop, void *context); /** @@ -772,7 +772,7 @@ int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_owned_hello_handler_t * Returns: * Returns a new zid closure. */ -int8_t z_closure_zid(z_owned_closure_zid_t *closure, 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(loanedtype, ownedtype, name) \ diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 424b50c66..a9f4b86de 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -480,8 +480,9 @@ _Bool z_str_array_is_empty(const z_str_array_t *a); _OWNED_TYPE_PTR(z_str_array_t, str_array) _LOANED_TYPE(z_str_array_t, str_array) -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. @@ -489,15 +490,15 @@ 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. + * 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. */ // TODO(sashacmc): 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_loaned_sample_t *sample); @@ -508,20 +509,22 @@ void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_loan * 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 + * 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. */ // TODO(sashacmc): 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. * @@ -530,19 +533,19 @@ void z_closure_owned_sample_call(const z_owned_closure_owned_sample_t *closure, * Members: * _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. */ // TODO(sashacmc): 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_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. @@ -550,21 +553,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 + * 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. */ // TODO(sashacmc): 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. @@ -572,16 +576,16 @@ 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: - * _z_reply_handler_t call: `void (*_z_reply_handler_t)(_z_reply_t *reply, void *arg)` is the + * 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. + * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ // TODO(sashacmc): typedef struct { void *context; - _z_reply_handler_t call; - _z_dropper_handler_t drop; + 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); @@ -594,14 +598,14 @@ void z_closure_reply_call(const z_owned_closure_reply_t *closure, const z_loaned * Members: * 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. */ // TODO(sashacmc): typedef struct { void *context; z_owned_reply_handler_t call; - _z_dropper_handler_t drop; + z_dropper_handler_t drop; } z_owned_closure_owned_reply_t; void z_closure_owned_reply_call(const z_owned_closure_owned_reply_t *closure, z_owned_reply_t *reply); @@ -616,14 +620,14 @@ typedef void (*z_owned_hello_handler_t)(z_owned_hello_t *hello, void *arg); * Members: * 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. */ // TODO(sashacmc): 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); @@ -637,14 +641,14 @@ typedef void (*z_id_handler_t)(const z_id_t *id, void *arg); * * Members: * 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. */ // TODO(sashacmc): 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); diff --git a/src/api/api.c b/src/api/api.c index c5f2060d2..d0177b867 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -443,17 +443,17 @@ OWNED_FUNCTIONS_RC(session) 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_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) + 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 { @@ -807,7 +807,7 @@ void z_get_options_default(z_get_options_t *options) { options->timeout_ms = Z_GET_TIMEOUT_DEFAULT; } -int8_t z_get(const z_loaned_session_t *zs, z_loaned_keyexpr_t *keyexpr, const char *parameters, +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, const z_get_options_t *options) { int8_t ret = _Z_RES_OK; diff --git a/src/net/reply.c b/src/net/reply.c index c87c65e64..2ad0b3701 100644 --- a/src/net/reply.c +++ b/src/net/reply.c @@ -20,7 +20,7 @@ _z_reply_t _z_reply_null(void) { _z_reply_t r = {._tag = Z_REPLY_TAG_DATA, .data = { - .replier_id = 0, + .replier_id = {.id = {0}}, .sample = {.in = NULL}, }}; return r; diff --git a/tests/z_perf_rx.c b/tests/z_perf_rx.c index e3324f01d..331bebb1a 100644 --- a/tests/z_perf_rx.c +++ b/tests/z_perf_rx.c @@ -124,6 +124,7 @@ int main(int argc, char **argv) { } #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; } From 61593058023dbe391bdb1a135d46e1c8e5fc8228 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 22 May 2024 14:55:45 +0200 Subject: [PATCH 13/43] Strings rework --- examples/arduino/z_get.ino | 9 +-- examples/arduino/z_pull.ino | 6 +- examples/arduino/z_queryable.ino | 10 +-- examples/arduino/z_scout.ino | 23 +++--- examples/arduino/z_sub.ino | 6 +- examples/espidf/z_get.c | 5 +- examples/espidf/z_pull.c | 2 +- examples/espidf/z_queryable.c | 16 +++-- examples/espidf/z_scout.c | 21 +++--- examples/espidf/z_sub.c | 6 +- examples/freertos_plus_tcp/z_get.c | 5 +- examples/freertos_plus_tcp/z_pull.c | 2 +- examples/freertos_plus_tcp/z_queryable.c | 22 +++--- examples/freertos_plus_tcp/z_scout.c | 21 +++--- examples/freertos_plus_tcp/z_sub.c | 2 +- examples/freertos_plus_tcp/z_sub_st.c | 2 +- examples/mbed/z_get.cpp | 9 +-- examples/mbed/z_pull.cpp | 6 +- examples/mbed/z_queryable.cpp | 15 ++-- examples/mbed/z_scout.cpp | 21 +++--- examples/mbed/z_sub.cpp | 6 +- examples/unix/c11/z_get.c | 5 +- examples/unix/c11/z_get_channel.c | 5 +- examples/unix/c11/z_pull.c | 2 +- examples/unix/c11/z_queryable.c | 21 +++--- examples/unix/c11/z_queryable_channel.c | 22 +++--- examples/unix/c11/z_scout.c | 21 +++--- examples/unix/c11/z_sub.c | 2 +- examples/unix/c11/z_sub_channel.c | 2 +- examples/unix/c11/z_sub_st.c | 2 +- examples/unix/c99/z_get.c | 9 +-- examples/unix/c99/z_pull.c | 6 +- examples/unix/c99/z_queryable.c | 16 +++-- examples/unix/c99/z_scout.c | 21 +++--- examples/unix/c99/z_sub.c | 6 +- examples/unix/c99/z_sub_st.c | 6 +- examples/windows/z_get.c | 5 +- examples/windows/z_pull.c | 2 +- examples/windows/z_queryable.c | 26 ++++--- examples/windows/z_scout.c | 21 +++--- examples/windows/z_sub.c | 2 +- examples/windows/z_sub_st.c | 2 +- examples/zephyr/z_get.c | 2 +- examples/zephyr/z_pull.c | 2 +- examples/zephyr/z_queryable.c | 15 ++-- examples/zephyr/z_scout.c | 21 +++--- examples/zephyr/z_sub.c | 2 +- include/zenoh-pico/api/macros.h | 52 +++++++------- include/zenoh-pico/api/primitives.h | 43 ++++-------- include/zenoh-pico/api/types.h | 41 ++++++----- include/zenoh-pico/collections/string.h | 30 ++------ include/zenoh-pico/protocol/core.h | 4 +- src/api/api.c | 89 +++++++++++------------- src/collections/string.c | 63 ++--------------- src/net/memory.c | 4 +- src/net/session.c | 17 ++--- src/session/scout.c | 10 +-- tests/z_api_alignment_test.c | 16 +++-- tests/z_api_null_drop_test.c | 6 +- tests/z_client_test.c | 12 ++-- tests/z_msgcodec_test.c | 22 +++--- tests/z_peer_multicast_test.c | 2 +- tests/z_test_fragment_rx.c | 2 +- zenohpico.pc | 2 +- 64 files changed, 411 insertions(+), 465 deletions(-) diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index 73773cd75..f5853253e 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -46,17 +46,17 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { (void)(ctx); if (z_reply_is_ok(oreply)) { const z_loaned_sample_t *sample = z_reply_ok(oreply); - z_owned_str_t keystr; + 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(" >> [Get listener] Received ("); - Serial.print(z_str_data(z_str_loan(&keystr))); + Serial.print(z_str_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)); } else { Serial.println(" >> Received an error"); } @@ -114,7 +114,8 @@ void loop() { z_get_options_t opts; z_get_options_default(&opts); if (strcmp(VALUE, "") != 0) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + // TODO(sashacmc): encoding + // opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); } z_owned_closure_reply_t callback; z_closure_reply(&callback, reply_handler, reply_dropper, NULL); diff --git a/examples/arduino/z_pull.ino b/examples/arduino/z_pull.ino index bf75d2412..dd8a271db 100644 --- a/examples/arduino/z_pull.ino +++ b/examples/arduino/z_pull.ino @@ -39,17 +39,17 @@ // @TODO // void data_handler(const z_loaned_sample_t *sample, void *arg) { -// z_owned_str_t keystr; +// 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_data(z_str_loan(&keystr))); +// Serial.print(z_str_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() { diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index 1463e4af7..c3c022ce6 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -36,20 +36,22 @@ #define VALUE "[ARDUINO]{ESP32} Queryable from Zenoh-Pico!" void query_handler(const z_loaned_query_t *query, void *arg) { - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_query_keyexpr(query), &keystr); Serial.print(" >> [Queryable handler] Replying Data ('"); - Serial.print(z_str_data(z_str_loan(&keystr))); + Serial.print(z_str_data(z_string_loan(&keystr))); Serial.print("': '"); Serial.print(VALUE); Serial.println("')"); z_view_keyexpr_t ke; z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); - z_query_reply(query, z_view_keyexpr_loan(&ke), (const unsigned char *)VALUE, strlen(VALUE), NULL); + z_owned_bytes_t reply_payload; + // TODO(sashacmc): VALUE encoding + 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() { diff --git a/examples/arduino/z_scout.ino b/examples/arduino/z_scout.ino index a3b78bb46..2d168225c 100644 --- a/examples/arduino/z_scout.ino +++ b/examples/arduino/z_scout.ino @@ -54,19 +54,18 @@ void fprintwhatami(unsigned int whatami) { } } -void fprintlocators(const z_str_array_t *locs) { +void fprintlocators(const z_loaned_string_array_t *locs) { Serial.print("["); - (void)locs; - // TODO(sashacmc): z_str_array_t - // size_t len = z_str_array_len(locs); - // for (unsigned int i = 0; i < len; i++) { - // Serial.print("'"); - // Serial.print(*z_str_array_get(locs, i)); - // Serial.print("'"); - // if (i < len - 1) { - // Serial.print(", "); - // } - //} + size_t len = z_string_array_len(locs); + for (unsigned int i = 0; i < len; i++) { + Serial.print("'"); + const z_loaned_string_t *str = z_string_array_get(locs, i); + Serial.print(str->val); + Serial.print("'"); + if (i < len - 1) { + Serial.print(", "); + } + } Serial.print("]"); } diff --git a/examples/arduino/z_sub.ino b/examples/arduino/z_sub.ino index 683e6acf5..5336932e1 100644 --- a/examples/arduino/z_sub.ino +++ b/examples/arduino/z_sub.ino @@ -35,17 +35,17 @@ #define KEYEXPR "demo/example/**" void data_handler(const z_loaned_sample_t *sample, void *arg) { - z_owned_str_t keystr; + 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_data(z_str_loan(&keystr))); + Serial.print(z_str_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() { diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index 1dae905c0..d42474e8d 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -106,7 +106,7 @@ void reply_dropper(void *ctx) { printf(" >> Received query final notification\n" void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { if (z_reply_is_ok(oreply)) { const z_loaned_sample_t *sample = z_reply_ok(oreply); - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); printf(" >> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len, @@ -161,7 +161,8 @@ void app_main() { z_get_options_t opts; z_get_options_default(&opts); if (strcmp(VALUE, "") != 0) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + // TODO(sashacmc): encoding + // opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); } z_owned_closure_reply_t callback; z_closure(&callback, reply_handler, reply_dropper); diff --git a/examples/espidf/z_pull.c b/examples/espidf/z_pull.c index 9f6a2d11f..8efd04a29 100644 --- a/examples/espidf/z_pull.c +++ b/examples/espidf/z_pull.c @@ -102,7 +102,7 @@ void wifi_init_sta(void) { // @TODO // void data_handler(const z_loaned_sample_t* sample, void* arg) { -// z_owned_str_t keystr; +// z_owned_string_t keystr; // z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); // printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), // (int)sample->payload.len, diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index 6bd3d88b7..8c8da8896 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -103,15 +103,17 @@ void wifi_init_sta(void) { void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_str_t keystr; - // TODO(sashacmc): z_query_parameters - // z_keyexpr_to_string(z_query_keyexpr(query), &keystr); - // z_bytes_t pred = z_query_parameters(query); - // printf(">> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)pred.len, - // pred.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_str_data(z_loan(keystr)), (int)z_loan(params)->len, + z_loan(params)->val); z_view_keyexpr_t ke; z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); - z_query_reply(query, z_loan(ke), (const unsigned char *)VALUE, strlen(VALUE), NULL); + z_owned_bytes_t reply_payload; + // TODO(sashacmc): VALUE encoding + z_query_reply(query, z_loan(ke), z_move(reply_payload), NULL); z_drop(z_move(keystr)); } diff --git a/examples/espidf/z_scout.c b/examples/espidf/z_scout.c index 99ca0b026..e569d81ea 100644 --- a/examples/espidf/z_scout.c +++ b/examples/espidf/z_scout.c @@ -109,18 +109,17 @@ 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, "["); - (void)locs; - // TODO(sashacmc): z_str_array missed - // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - // fprintf(stream, "\""); - // fprintf(stream, "%s", *z_str_array_get(locs, i)); - // fprintf(stream, "\""); - // if (i < z_str_array_len(locs) - 1) { - // fprintf(stream, ", "); - // } - //} + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { + fprintf(stream, "\""); + 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_string_array_len(locs) - 1) { + fprintf(stream, ", "); + } + } fprintf(stream, "]"); } diff --git a/examples/espidf/z_sub.c b/examples/espidf/z_sub.c index 76e93927f..45e1b8ed9 100644 --- a/examples/espidf/z_sub.c +++ b/examples/espidf/z_sub.c @@ -101,12 +101,12 @@ void wifi_init_sta(void) { } void data_handler(const z_loaned_sample_t* sample, void* arg) { - z_owned_str_t 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 handler] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len, + printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len, payload->start); - z_str_drop(z_str_move(&keystr)); + z_string_drop(z_string_move(&keystr)); } void app_main() { diff --git a/examples/freertos_plus_tcp/z_get.c b/examples/freertos_plus_tcp/z_get.c index 6e3145748..efe049411 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -40,7 +40,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { (void)(ctx); if (z_reply_is_ok(reply)) { const z_loaned_sample_t *sample = z_reply_ok(reply); - z_owned_str_t 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(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); @@ -84,7 +84,8 @@ void app_main(void) { z_get_options_t opts; z_get_options_default(&opts); if (strcmp(VALUE, "") != 0) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + // TODO(sashacmc): encoding + // opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); } z_owned_closure_reply_t callback; z_closure(&callback, reply_handler, reply_dropper); diff --git a/examples/freertos_plus_tcp/z_pull.c b/examples/freertos_plus_tcp/z_pull.c index 2c6ed1014..6f1273b70 100644 --- a/examples/freertos_plus_tcp/z_pull.c +++ b/examples/freertos_plus_tcp/z_pull.c @@ -31,7 +31,7 @@ // @TODO // void data_handler(const z_loaned_sample_t *sample, void *ctx) { // (void)(ctx); -// z_owned_str_t keystr; +// z_owned_string_t keystr; // z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); // printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)sample->payload.len, // sample->payload.start); diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index 02251adc3..95eb3813c 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -31,19 +31,21 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_query_keyexpr(query), &keystr); - // TODO(sashacmc): z_query_parameters - // 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_str_data(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_view_string_t params; + z_query_parameters(query, ¶ms); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len, + z_loan(params)->val); + const z_loaned_value_t *payload_value = z_query_value(query); + if (payload_value->payload.len > 0) { + printf(" with value '%.*s'\n", (int)payload_value->payload.len, payload_value->payload.start); + } z_query_reply_options_t options; z_query_reply_options_default(&options); - options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); - z_query_reply(query, z_query_keyexpr(query), (const unsigned char *)VALUE, strlen(VALUE), &options); + z_owned_bytes_t reply_payload; + // TODO(sashacmc): VALUE encoding + z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); z_drop(z_move(keystr)); } diff --git a/examples/freertos_plus_tcp/z_scout.c b/examples/freertos_plus_tcp/z_scout.c index 777ef2b2c..4b62c1301 100644 --- a/examples/freertos_plus_tcp/z_scout.c +++ b/examples/freertos_plus_tcp/z_scout.c @@ -43,18 +43,17 @@ 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, "["); - (void)locs; - // TODO(sashacmc): z_str_array missed - // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - // fprintf(stream, "\""); - // fprintf(stream, "%s", *z_str_array_get(locs, i)); - // fprintf(stream, "\""); - // if (i < z_str_array_len(locs) - 1) { - // fprintf(stream, ", "); - // } - //} + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { + fprintf(stream, "\""); + 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_string_array_len(locs) - 1) { + fprintf(stream, ", "); + } + } fprintf(stream, "]"); } diff --git a/examples/freertos_plus_tcp/z_sub.c b/examples/freertos_plus_tcp/z_sub.c index b59d85972..dbf7e79f9 100644 --- a/examples/freertos_plus_tcp/z_sub.c +++ b/examples/freertos_plus_tcp/z_sub.c @@ -30,7 +30,7 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_str_t 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_str_data(z_loan(keystr)), (int)payload->len, payload->start); diff --git a/examples/freertos_plus_tcp/z_sub_st.c b/examples/freertos_plus_tcp/z_sub_st.c index 53f81f2a8..151e078a6 100644 --- a/examples/freertos_plus_tcp/z_sub_st.c +++ b/examples/freertos_plus_tcp/z_sub_st.c @@ -33,7 +33,7 @@ int msg_nb = 0; void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_str_t 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_str_data(z_loan(keystr)), (int)payload->len, payload->start); diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index e16387948..12340b094 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -36,11 +36,11 @@ void reply_dropper(void *ctx) { printf(" >> Received query final notification\n" void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { if (z_reply_is_ok(oreply)) { const z_loaned_sample_t *sample = z_reply_ok(oreply); - z_owned_str_t 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(" >> Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len, payload->start); - z_str_drop(z_str_move(&keystr)); + printf(" >> Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len, payload->start); + z_string_drop(z_string_move(&keystr)); } else { printf(" >> Received an error\n"); } @@ -80,7 +80,8 @@ int main(int argc, char **argv) { z_get_options_t opts; z_get_options_default(&opts); if (strcmp(VALUE, "") != 0) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + // TODO(sashacmc): encoding + // opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); } z_owned_closure_reply_t callback; z_closure_reply(&callback, reply_handler, reply_dropper, NULL); diff --git a/examples/mbed/z_pull.cpp b/examples/mbed/z_pull.cpp index f011a4643..d01f2978c 100644 --- a/examples/mbed/z_pull.cpp +++ b/examples/mbed/z_pull.cpp @@ -32,12 +32,12 @@ // @TODO // void data_handler(const z_loaned_sample_t *sample, void *arg) { -// z_owned_str_t keystr; +// z_owned_string_t keystr; // z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); -// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), +// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_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) { diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index 505da93e5..bf271d92d 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -33,14 +33,17 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_query_keyexpr(query), &keystr); - z_view_str_t pred; + z_view_string_t pred; z_query_parameters(query, &pred); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_str_loan(&keystr)), - (int)z_view_str_loan(&pred)->len, z_view_str_loan(&pred)->val); - z_query_reply(query, z_query_keyexpr(query), (const unsigned char *)VALUE, strlen(VALUE), NULL); - z_str_drop(z_str_move(&keystr)); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_string_loan(&keystr)), + (int)z_view_string_loan(&pred)->len, z_view_string_loan(&pred)->val); + + z_owned_bytes_t reply_payload; + // TODO(sashacmc): VALUE encoding + 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) { diff --git a/examples/mbed/z_scout.cpp b/examples/mbed/z_scout.cpp index 6940f284b..9f78c6be4 100644 --- a/examples/mbed/z_scout.cpp +++ b/examples/mbed/z_scout.cpp @@ -51,18 +51,17 @@ 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, "["); - (void)locs; - // TODO(sashacmc): z_str_array_t - // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - // fprintf(stream, "\""); - // fprintf(stream, "%s", *z_str_array_get(locs, i)); - // fprintf(stream, "\""); - // if (i < z_str_array_len(locs) - 1) { - // fprintf(stream, ", "); - // } - //} + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { + fprintf(stream, "\""); + 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_string_array_len(locs) - 1) { + fprintf(stream, ", "); + } + } fprintf(stream, "]"); } diff --git a/examples/mbed/z_sub.cpp b/examples/mbed/z_sub.cpp index 5b8a56716..537630d6a 100644 --- a/examples/mbed/z_sub.cpp +++ b/examples/mbed/z_sub.cpp @@ -31,12 +31,12 @@ #define KEYEXPR "demo/example/**" void data_handler(const z_loaned_sample_t *sample, void *arg) { - z_owned_str_t 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 handler] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len, + printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len, payload->start); - z_str_drop(z_str_move(&keystr)); + z_string_drop(z_string_move(&keystr)); } int main(int argc, char **argv) { diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index f810adfbf..132f566f2 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -41,7 +41,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { (void)(ctx); if (z_reply_is_ok(reply)) { const z_loaned_sample_t *sample = z_reply_ok(reply); - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len, z_sample_payload(sample)->start); @@ -132,7 +132,8 @@ int main(int argc, char **argv) { 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)); + // TODO(sashacmc): encoding + // opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); } #if Z_FEATURE_ATTACHMENT == 1 z_owned_bytes_map_t map = z_bytes_map_new(); diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index ed96149fc..d2f4c0a41 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -91,7 +91,8 @@ int main(int argc, char **argv) { 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)); + // TODO(sashacmc): encoding + // opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); } z_owned_reply_ring_channel_t channel; z_reply_ring_channel_new(&channel, 1); @@ -105,7 +106,7 @@ int main(int argc, char **argv) { for (z_call(channel.recv, &reply); z_check(reply); z_call(channel.recv, &reply)) { if (z_reply_is_ok(z_loan(reply))) { const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply)); - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len, z_sample_payload(sample)->start); diff --git a/examples/unix/c11/z_pull.c b/examples/unix/c11/z_pull.c index 61a0f283a..5b396c265 100644 --- a/examples/unix/c11/z_pull.c +++ b/examples/unix/c11/z_pull.c @@ -88,7 +88,7 @@ int main(int argc, char **argv) { z_null(&sample); while (true) { for (z_call(channel.try_recv, &sample); z_check(sample); z_call(channel.try_recv, &sample)) { - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr); printf(">> [Subscriber] Pulled ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(z_loan(sample))->len, z_sample_payload(z_loan(sample))->start); diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index 27d1c69d4..08ced0e1f 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -33,15 +33,16 @@ int8_t attachment_handler(z_bytes_t key, z_bytes_t att_value, void *ctx) { void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_query_keyexpr(query), &keystr); - // TODO(sashacmc): z_query_parameters - // 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_str_data(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_view_string_t params; + z_query_parameters(query, ¶ms); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len, + z_loan(params)->val); + const z_loaned_value_t *payload_value = z_query_value(query); + if (payload_value->payload.len > 0) { + printf(" with value '%.*s'\n", (int)payload_value->payload.len, payload_value->payload.start); + } #if Z_FEATURE_ATTACHMENT == 1 z_attachment_t attachment = z_query_attachment(query); if (z_attachment_check(&attachment)) { @@ -61,7 +62,9 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { options.attachment = z_bytes_map_as_attachment(&map); #endif - z_query_reply(query, z_query_keyexpr(query), (const unsigned char *)value, strlen(value), &options); + z_owned_bytes_t reply_payload; + // TODO(sashacmc): value encoding + z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); z_drop(z_move(keystr)); msg_nb++; diff --git a/examples/unix/c11/z_queryable_channel.c b/examples/unix/c11/z_queryable_channel.c index 8487afab6..ce128023a 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -100,19 +100,21 @@ int main(int argc, char **argv) { z_null(&query); for (z_call(channel.recv, &query); z_check(query); z_call(channel.recv, &query)) { const z_loaned_query_t *q = z_loan(query); - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_query_keyexpr(q), &keystr); - // TODO(sashacmc): - // 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_str_data(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_view_string_t params; + z_query_parameters(q, ¶ms); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), + (int)z_loan(params)->len, z_loan(params)->val); + const z_loaned_value_t *payload_value = z_query_value(q); + if (payload_value->payload.len > 0) { + printf(" with value '%.*s'\n", (int)payload_value->payload.len, payload_value->payload.start); + } z_query_reply_options_t options; z_query_reply_options_default(&options); - options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); - z_query_reply(q, z_loan(ke), (const unsigned char *)value, strlen(value), &options); + z_owned_bytes_t reply_payload; + // TODO(sashacmc): value encoding + z_query_reply(q, z_query_keyexpr(q), z_move(reply_payload), &options); z_drop(z_move(keystr)); z_drop(z_move(query)); } diff --git a/examples/unix/c11/z_scout.c b/examples/unix/c11/z_scout.c index beba299ee..a39bd2226 100644 --- a/examples/unix/c11/z_scout.c +++ b/examples/unix/c11/z_scout.c @@ -41,18 +41,17 @@ 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, "["); - (void)locs; - // TODO(sashacmc): - // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - // fprintf(stream, "\""); - // fprintf(stream, "%s", *z_str_array_get(locs, i)); - // fprintf(stream, "\""); - // if (i < z_str_array_len(locs) - 1) { - // fprintf(stream, ", "); - // } - //} + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { + fprintf(stream, "\""); + 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_string_array_len(locs) - 1) { + fprintf(stream, ", "); + } + } fprintf(stream, "]"); } diff --git a/examples/unix/c11/z_sub.c b/examples/unix/c11/z_sub.c index 3b1e0865d..df6bf9e07 100644 --- a/examples/unix/c11/z_sub.c +++ b/examples/unix/c11/z_sub.c @@ -34,7 +34,7 @@ int8_t attachment_handler(z_bytes_t key, z_bytes_t value, void *ctx) { void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_str_t 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_str_data(z_loan(keystr)), (int)payload->len, payload->start); diff --git a/examples/unix/c11/z_sub_channel.c b/examples/unix/c11/z_sub_channel.c index 83985a7c6..1067a475f 100644 --- a/examples/unix/c11/z_sub_channel.c +++ b/examples/unix/c11/z_sub_channel.c @@ -78,7 +78,7 @@ int main(int argc, char **argv) { z_owned_sample_t sample; z_null(&sample); for (z_call(channel.recv, &sample); z_check(sample); z_call(channel.recv, &sample)) { - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr); printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(z_loan(sample))->len, z_sample_payload(z_loan(sample))->start); diff --git a/examples/unix/c11/z_sub_st.c b/examples/unix/c11/z_sub_st.c index 0deee874f..852011459 100644 --- a/examples/unix/c11/z_sub_st.c +++ b/examples/unix/c11/z_sub_st.c @@ -25,7 +25,7 @@ static int msg_nb = 0; void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_str_t 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_str_data(z_loan(keystr)), (int)payload->len, payload->start); diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 457b5ecc4..38a6339c7 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -33,11 +33,11 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { (void)(ctx); if (z_reply_is_ok(reply)) { const z_loaned_sample_t *sample = z_reply_ok(reply); - z_owned_str_t 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(">> Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len, payload->start); - z_str_drop(z_str_move(&keystr)); + printf(">> Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len, payload->start); + z_string_drop(z_string_move(&keystr)); } else { printf(">> Received an error\n"); } @@ -118,7 +118,8 @@ int main(int argc, char **argv) { 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)); + // TODO(sashacmc): encoding + // opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); } z_owned_closure_reply_t callback; z_closure_reply(&callback, reply_handler, reply_dropper, NULL); diff --git a/examples/unix/c99/z_pull.c b/examples/unix/c99/z_pull.c index e93bd9db6..0760264de 100644 --- a/examples/unix/c99/z_pull.c +++ b/examples/unix/c99/z_pull.c @@ -22,11 +22,11 @@ // @TODO // void data_handler(const z_loaned_sample_t *sample, void *ctx) { // (void)(ctx); -// z_owned_str_t keystr; +// z_owned_string_t keystr; // z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); -// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)sample->payload.len, +// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_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) { diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index 7f2b4adc9..df3a8a6e2 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -24,17 +24,19 @@ const char *value = "Queryable from Pico!"; void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_query_keyexpr(query), &keystr); - z_view_str_t pred; - z_query_parameters(query, &pred); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_str_loan(&keystr)), - (int)z_view_str_loan(&pred)->len, z_view_str_loan(&pred)->val); + z_view_string_t params; + z_query_parameters(query, ¶ms); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_string_loan(&keystr)), + (int)z_view_string_loan(¶ms)->len, z_view_string_loan(¶ms)->val); z_query_reply_options_t options; z_query_reply_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); - z_query_reply(query, z_query_keyexpr(query), (const unsigned char *)value, strlen(value), &options); - z_str_drop(z_str_move(&keystr)); + z_owned_bytes_t reply_payload; + // TODO(sashacmc): value encoding + 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) { diff --git a/examples/unix/c99/z_scout.c b/examples/unix/c99/z_scout.c index cd07d9778..d94c278f4 100644 --- a/examples/unix/c99/z_scout.c +++ b/examples/unix/c99/z_scout.c @@ -40,18 +40,17 @@ 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, "["); - (void)locs; - // TODO(sashacmc): z_str_array_t - // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - // fprintf(stream, "\""); - // fprintf(stream, "%s", *z_str_array_get(locs, i)); - // fprintf(stream, "\""); - // if (i < z_str_array_len(locs) - 1) { - // fprintf(stream, ", "); - // } - //} + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { + fprintf(stream, "\""); + 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_string_array_len(locs) - 1) { + fprintf(stream, ", "); + } + } fprintf(stream, "]"); } diff --git a/examples/unix/c99/z_sub.c b/examples/unix/c99/z_sub.c index 4276819ea..fa05fc001 100644 --- a/examples/unix/c99/z_sub.c +++ b/examples/unix/c99/z_sub.c @@ -22,12 +22,12 @@ #if Z_FEATURE_SUBSCRIPTION == 1 void data_handler(const z_loaned_sample_t *sample, void *arg) { (void)(arg); - z_owned_str_t 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_str_data(z_str_loan(&keystr)), (int)payload->len, + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len, payload->start); - z_str_drop(z_str_move(&keystr)); + z_string_drop(z_string_move(&keystr)); } int main(int argc, char **argv) { diff --git a/examples/unix/c99/z_sub_st.c b/examples/unix/c99/z_sub_st.c index 5a3e83e02..f97540233 100644 --- a/examples/unix/c99/z_sub_st.c +++ b/examples/unix/c99/z_sub_st.c @@ -25,12 +25,12 @@ static int msg_nb = 0; void data_handler(const z_loaned_sample_t *sample, void *arg) { (void)(arg); - z_owned_str_t 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_str_data(z_str_loan(&keystr)), (int)payload->len, + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len, payload->start); - z_str_drop(z_str_move(&keystr)); + z_string_drop(z_string_move(&keystr)); msg_nb++; } diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index e614f4f77..405b1a279 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -32,7 +32,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { (void)(ctx); if (z_reply_is_ok(reply)) { const z_loaned_sample_t *sample = z_reply_ok(reply); - z_owned_str_t 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(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); @@ -83,7 +83,8 @@ int main(int argc, char **argv) { 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)); + // TODO(sashacmc): encoding + // opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); } z_owned_closure_reply_t callback; z_closure(&callback, reply_handler, reply_dropper); diff --git a/examples/windows/z_pull.c b/examples/windows/z_pull.c index 5cc5be9ce..5dd66e558 100644 --- a/examples/windows/z_pull.c +++ b/examples/windows/z_pull.c @@ -21,7 +21,7 @@ // @TODO // void data_handler(const z_loaned_sample_t *sample, void *ctx) { // (void)(ctx); -// z_owned_str_t keystr; +// z_owned_string_t keystr; // z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); // printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)sample->payload.len, // sample->payload.start); diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index 0c409b597..d810c0926 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -23,20 +23,24 @@ const char *value = "Queryable from Pico!"; void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_query_keyexpr(query), &keystr); - // TODO(sashacmc): - // 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_str_data(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_view_string_t params; + z_query_parameters(query, ¶ms); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len, + z_loan(params)->val); + const z_loaned_value_t *payload_value = z_query_value(query); + if (payload_value->payload.len > 0) { + printf(" with value '%.*s'\n", (int)payload_value->payload.len, payload_value->payload.start); + } + z_query_reply_options_t options; z_query_reply_options_default(&options); - options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); - z_query_reply(query, z_query_keyexpr(query), (const unsigned char *)value, strlen(value), &options); + z_owned_bytes_t reply_payload; + // TODO(sashacmc): value encoding + z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options); + z_drop(z_move(keystr)); } diff --git a/examples/windows/z_scout.c b/examples/windows/z_scout.c index a89ddd038..dfd993637 100644 --- a/examples/windows/z_scout.c +++ b/examples/windows/z_scout.c @@ -40,18 +40,17 @@ 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, "["); - (void)locs; - // TODO(sashacmc): z_str_array missed - // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - // fprintf(stream, "\""); - // fprintf(stream, "%s", *z_str_array_get(locs, i)); - // fprintf(stream, "\""); - // if (i < z_str_array_len(locs) - 1) { - // fprintf(stream, ", "); - // } - //} + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { + fprintf(stream, "\""); + 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_string_array_len(locs) - 1) { + fprintf(stream, ", "); + } + } fprintf(stream, "]"); } diff --git a/examples/windows/z_sub.c b/examples/windows/z_sub.c index 298d54b4f..e908f2534 100644 --- a/examples/windows/z_sub.c +++ b/examples/windows/z_sub.c @@ -21,7 +21,7 @@ #if Z_FEATURE_SUBSCRIPTION == 1 void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_str_t 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_str_data(z_loan(keystr)), (int)payload->len, payload->start); diff --git a/examples/windows/z_sub_st.c b/examples/windows/z_sub_st.c index 4276a4ecb..fed96e89c 100644 --- a/examples/windows/z_sub_st.c +++ b/examples/windows/z_sub_st.c @@ -24,7 +24,7 @@ int msg_nb = 0; #if Z_FEATURE_SUBSCRIPTION == 1 void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_str_t 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_str_data(z_loan(keystr)), (int)payload->len, payload->start); diff --git a/examples/zephyr/z_get.c b/examples/zephyr/z_get.c index 0a0d8cf1b..58b0d9221 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -36,7 +36,7 @@ void reply_dropper(void *ctx) { printf(" >> Received query final notification\n" void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { if (z_reply_is_ok(oreply)) { const z_loaned_sample_t *sample = z_reply_ok(oreply); - z_owned_str_t 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(" >> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); diff --git a/examples/zephyr/z_pull.c b/examples/zephyr/z_pull.c index cdab228bf..374c6e8d3 100644 --- a/examples/zephyr/z_pull.c +++ b/examples/zephyr/z_pull.c @@ -31,7 +31,7 @@ // @TODO // void data_handler(const z_loaned_sample_t *sample, void *arg) { -// z_owned_str_t keystr; +// z_owned_string_t keystr; // z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); // printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), // (int)sample->payload.len, diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index 3950bc1b8..68fc5d4d8 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -33,13 +33,16 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_query_keyexpr(query), &keystr); - // TODO(sashacmc): z_query_parameters - // z_bytes_t pred = z_query_parameters(query); - // printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)pred.len, - // pred.start); - z_query_reply(query, z_query_keyexpr(query), (const unsigned char *)VALUE, strlen(VALUE), NULL); + z_view_string_t params; + z_query_parameters(query, ¶ms); + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), z_loan(params)->len, + z_loan(params)->val); + + z_owned_bytes_t reply_payload; + // TODO(sashacmc): VALUE encoding + z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); z_drop(z_move(keystr)); } diff --git a/examples/zephyr/z_scout.c b/examples/zephyr/z_scout.c index 3f1c9c93e..06c8277b5 100644 --- a/examples/zephyr/z_scout.c +++ b/examples/zephyr/z_scout.c @@ -37,18 +37,17 @@ 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, "["); - (void)locs; - // TODO(sashacmc): z_str_array missed - // for (unsigned int i = 0; i < z_str_array_len(locs); i++) { - // fprintf(stream, "\""); - // fprintf(stream, "%s", *z_str_array_get(locs, i)); - // fprintf(stream, "\""); - // if (i < z_str_array_len(locs) - 1) { - // fprintf(stream, ", "); - // } - //} + for (unsigned int i = 0; i < z_string_array_len(locs); i++) { + fprintf(stream, "\""); + 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_string_array_len(locs) - 1) { + fprintf(stream, ", "); + } + } fprintf(stream, "]"); } diff --git a/examples/zephyr/z_sub.c b/examples/zephyr/z_sub.c index 362b249a2..81b97ad01 100644 --- a/examples/zephyr/z_sub.c +++ b/examples/zephyr/z_sub.c @@ -30,7 +30,7 @@ #define KEYEXPR "demo/example/**" void data_handler(const z_loaned_sample_t *sample, void *arg) { - z_owned_str_t 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 handler] Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index 18ea8acc4..bf5cf8c96 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -42,11 +42,12 @@ 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_view_str_t : z_view_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 \ )(&x) #define z_loan_mut(x) _Generic((x), \ @@ -57,10 +58,12 @@ 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_str_t : z_str_loan_mut, \ - z_owned_str_array_t : z_str_array_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_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. @@ -78,8 +81,8 @@ 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_closure_sample_t * : z_closure_sample_drop, \ @@ -109,7 +112,7 @@ #define z_check(x) _Generic((x), \ z_owned_keyexpr_t : z_keyexpr_check, \ z_view_keyexpr_t : z_keyexpr_is_initialized, \ - z_value_t : z_value_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, \ @@ -118,8 +121,8 @@ 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_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 \ @@ -161,8 +164,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, \ @@ -172,6 +175,7 @@ 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_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, \ @@ -198,8 +202,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) /** @@ -219,7 +223,7 @@ 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, \ @@ -265,14 +269,14 @@ 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 char* type; }; template<> inline z_session_t z_loan(const z_owned_session_t& x) { return z_session_loan(&x); } template<> inline z_keyexpr_t z_loan(const z_owned_keyexpr_t& x) { return z_keyexpr_loan(&x); } template<> inline z_config_t z_loan(const z_owned_config_t& x) { return z_config_loan(&x); } template<> inline z_publisher_t z_loan(const z_owned_publisher_t& x) { return z_publisher_loan(&x); } template<> inline z_hello_t z_loan(const z_owned_hello_t& x) { return z_hello_loan(&x); } -template<> inline const char* z_loan(const z_owned_str_t& x) { return z_str_loan(&x); } +template<> inline const char* z_loan(const z_owned_string_t& x) { return z_string_loan(&x); } template struct zenoh_drop_type { typedef T type; }; template inline typename zenoh_drop_type::type z_drop(T*); @@ -286,7 +290,7 @@ 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; }; @@ -305,7 +309,7 @@ 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); } @@ -328,7 +332,7 @@ 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_str_t* v) { z_str_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_clusure_owned_sample_t* v) { z_closure_owned_sample_null(v); } inline void z_null(z_owned_closure_query_t* v) { z_closure_query_null(v); } @@ -348,8 +352,8 @@ inline bool z_check(const z_owned_subscriber_t& v) { return z_subscriber_check(& 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_string_t& v) { return z_sample_check(&v); } inline void z_call(const z_owned_closure_sample_t &closure, const z_loaned_sample_t *sample) { z_closure_sample_call(&closure, sample); } diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 622274f5c..6daa41f20 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -45,7 +45,7 @@ extern "C" { * Returns: * The :c:type:`z_string_t` corresponding to the given string. */ -int8_t z_view_str_wrap(z_view_str_t *str, const char *value); +int8_t z_view_str_wrap(z_view_string_t *str, const char *value); /** * Constructs a :c:type:`z_keyexpr_t` departing from a string. @@ -71,9 +71,6 @@ int8_t z_view_keyexpr_from_string(z_view_keyexpr_t *keyexpr, const char *name); * Returns: * The :c:type:`z_keyexpr_t` corresponding to the given string. */ -// TODO(sashacmc): -z_owned_keyexpr_t z_keyexpr_unchecked(const char *name); - int8_t z_view_keyexpr_from_string_unchecked(z_view_keyexpr_t *keyexpr, const char *name); /** @@ -87,9 +84,9 @@ int8_t z_view_keyexpr_from_string_unchecked(z_view_keyexpr_t *keyexpr, const cha * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` * * Returns: - * The :c:type:`z_owned_str_t` containing key expression string representation if it's possible + * The :c:type:`z_owned_string_t` containing key expression string representation if it's possible */ -void z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_str_t *s); +void z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *s); /** * Returns the key expression's internal string by aliasing it. @@ -127,7 +124,7 @@ _Bool zp_keyexpr_was_declared(const z_loaned_keyexpr_t *keyexpr); * Returns: * The string representation of a keyexpr for a given session. */ -int8_t zp_keyexpr_resolve(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_str_t *str); +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. @@ -519,7 +516,7 @@ z_query_consolidation_t z_query_consolidation_none(void); * Returns the value selector wrapped as a :c:type:`z_loaned_bytes_t*`, since value selector is a user-defined * representation. */ -void z_query_parameters(const z_loaned_query_t *query, z_view_str_t *parameters); +void z_query_parameters(const z_loaned_query_t *query, z_view_string_t *parameters); /** * Get a query's payload value by aliasing it. @@ -531,7 +528,7 @@ void z_query_parameters(const z_loaned_query_t *query, z_view_str_t *parameters) * Returns: * Returns the payload wrapped as a :c:type:`z_value_t`, since payload value is a user-defined representation. */ -z_value_t z_query_value(const z_loaned_query_t *query); +const z_loaned_value_t *z_query_value(const z_loaned_query_t *query); #if Z_FEATURE_ATTACHMENT == 1 /** @@ -784,7 +781,7 @@ int8_t z_closure_zid(z_owned_closure_zid_t *closure, z_id_handler_t call, z_drop void z_##name##_drop(ownedtype *obj); \ void z_##name##_null(ownedtype *obj); -_OWNED_FUNCTIONS(z_loaned_str_t, z_owned_str_t, str) +_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) @@ -794,10 +791,11 @@ _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_str_array_t, z_owned_str_array_t, str_array) +_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); \ @@ -820,7 +818,7 @@ _OWNED_FUNCTIONS_CLOSURE(z_owned_closure_zid_t, closure_zid) void z_view_##name##_null(viewtype *name); _VIEW_FUNCTIONS(z_loaned_keyexpr_t, z_view_keyexpr_t, keyexpr) -_VIEW_FUNCTIONS(z_loaned_str_t, z_view_str_t, str) +_VIEW_FUNCTIONS(z_loaned_string_t, z_view_string_t, string) // Gets internal value from refcountered type (e.g. z_loaned_session_t, z_query_t) #define _Z_RC_IN_VAL(arg) ((arg)->in->val) @@ -830,7 +828,7 @@ _VIEW_FUNCTIONS(z_loaned_str_t, z_view_str_t, str) // TODO(sashacmc): comments, docs, etc. const z_loaned_sample_t *z_sample_loan(const z_owned_sample_t *sample); -const char *z_str_data(const z_loaned_str_t *str); +const char *z_str_data(const z_loaned_string_t *str); /************* Primitives **************/ /** @@ -1174,7 +1172,7 @@ void z_get_options_default(z_get_options_t *options); * Returns ``0`` if the put operation is successful, or a ``negative value`` otherwise. */ 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, const z_get_options_t *options); + 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. * @@ -1214,7 +1212,7 @@ const z_loaned_sample_t *z_reply_ok(const z_loaned_reply_t *reply); * Returns: * Returns the :c:type:`z_value_t` wrapped in the query reply. */ -z_value_t z_reply_err(const z_loaned_reply_t *reply); +const z_loaned_value_t *z_reply_err(const z_loaned_reply_t *reply); #endif #if Z_FEATURE_QUERYABLE == 1 @@ -1297,8 +1295,8 @@ void z_query_reply_options_default(z_query_reply_options_t *options); * Returns: * Returns ``0`` if the send query reply operation is successful, or a ``negative value`` otherwise. */ -int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, const uint8_t *payload, - size_t payload_len, const z_query_reply_options_t *options); +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 /** @@ -1407,17 +1405,6 @@ int8_t z_undeclare_subscriber(z_owned_subscriber_t *sub); z_owned_keyexpr_t z_subscriber_keyexpr(z_loaned_subscriber_t *sub); #endif -/** - * Checks if a given value is valid. - * - * Parameters: - * value: A loaned instance of :c:type:`z_value_t` to be checked. - * - * Returns: - * Returns ``true`` if the value is valid, or ``false`` otherwise. - */ -_Bool z_value_is_initialized(z_value_t *value); - /************* Multi Thread Tasks helpers **************/ /** * Constructs the default values for the session read task. diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index a9f4b86de..a25577ada 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -18,6 +18,7 @@ #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/publish.h" #include "zenoh-pico/net/query.h" #include "zenoh-pico/net/reply.h" @@ -87,9 +88,9 @@ typedef _z_id_t z_id_t; * const char *val: A pointer to the string. */ -_OWNED_TYPE_PTR(_z_string_t, str) -_LOANED_TYPE(_z_string_t, str) -_VIEW_TYPE(_z_string_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. @@ -207,8 +208,9 @@ typedef _z_timestamp_t z_timestamp_t; * z_encoding_t encoding: The encoding of the `payload`. * z_loaned_bytes_t* payload: The payload of this zenoh value. */ -// TODO(sashacmc): -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, @@ -329,10 +331,12 @@ typedef struct { * Members: * z_query_target_t target: The queryables that should be targeted by this get. * z_query_consolidation_t consolidation: The replies consolidation strategy to apply on replies. - * z_value_t value: The payload to include in the query. + * z_owned_bytes_t payload: The payload to include in the query. + * z_encoding_t encoding: Payload encoding. */ typedef struct { - z_value_t value; + z_owned_bytes_t *payload; + z_encoding_t encoding; z_query_consolidation_t consolidation; z_query_target_t target; uint32_t timeout_ms; @@ -437,7 +441,7 @@ _LOANED_TYPE(_z_sample_rc_t, sample) * Members: * z_whatami_t whatami: The kind of zenoh entity. * z_loaned_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_string_array_t locators: The locators of the scouted entity. */ _OWNED_TYPE_PTR(_z_hello_t, hello) _LOANED_TYPE(_z_hello_t, hello) @@ -464,21 +468,20 @@ _LOANED_TYPE(_z_reply_rc_t, reply) /** * Represents an array of ``z_str_t``. * - * 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);`` + * - ``char *z_string_array_get(z_loaned_string_array_t *a, size_t k);`` + * - ``size_t z_string_array_len(z_loaned_string_array_t *a);`` + * - ``_Bool z_str_array_array_is_empty(z_loaned_string_array_t *a);`` */ -// TODO(sashacmc): -typedef _z_str_array_t z_str_array_t; -z_owned_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_string_vec_t, string_array) +_LOANED_TYPE(_z_string_vec_t, string_array) +_VIEW_TYPE(_z_string_vec_t, string_array) -_OWNED_TYPE_PTR(z_str_array_t, str_array) -_LOANED_TYPE(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); diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index cfbc33978..827017145 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) @@ -73,6 +70,7 @@ typedef struct { } _z_string_t; _z_string_t _z_string_make(const 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); @@ -85,28 +83,8 @@ _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/protocol/core.h b/include/zenoh-pico/protocol/core.h index 4d9453fc2..fd25c4cb5 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -248,12 +248,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/src/api/api.c b/src/api/api.c index d0177b867..1a24ccc81 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -43,15 +43,18 @@ /********* Data Types Handlers *********/ -int8_t z_view_str_wrap(z_view_str_t *str, const char *value) { +int8_t z_view_str_wrap(z_view_string_t *str, const char *value) { str->_val = _z_string_make(value); return _Z_RES_OK; } -// TODO(sashacmc): to z_slice_array_t ??? -// 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); } +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); +} + +size_t z_string_array_len(const z_loaned_string_array_t *a) { return _z_string_vec_len(a); } + +_Bool z_string_array_is_empty(const z_loaned_string_array_t *a) { return _z_string_vec_is_empty(a); } int8_t z_view_keyexpr_from_string(z_view_keyexpr_t *keyexpr, const char *name) { keyexpr->_val = _z_rname(name); @@ -63,7 +66,7 @@ int8_t z_view_keyexpr_from_string_unchecked(z_view_keyexpr_t *keyexpr, const cha return _Z_RES_OK; } -void z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_str_t *s) { +void z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *s) { if (keyexpr->_id == Z_RESOURCE_ID_NONE) { s->_val = (_z_string_t *)z_malloc(sizeof(_z_string_t)); if (s->_val != NULL) { @@ -90,7 +93,7 @@ _Bool zp_keyexpr_was_declared(const z_keyexpr_t *keyexpr) { return ret; } */ -int8_t zp_keyexpr_resolve(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_str_t *str) { +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 @@ -265,10 +268,6 @@ z_encoding_t z_encoding_default(void) { return z_encoding(Z_ENCODING_PREFIX_DEFA _Bool z_timestamp_check(z_timestamp_t ts) { return _z_timestamp_check(&ts); } -z_value_t z_value(const char *payload, size_t payload_len, z_encoding_t encoding) { - return (z_value_t){.payload = {.start = (const uint8_t *)payload, .len = payload_len}, .encoding = encoding}; -} - z_query_target_t z_query_target_default(void) { return Z_QUERY_TARGET_DEFAULT; } z_query_consolidation_t z_query_consolidation_auto(void) { @@ -289,12 +288,12 @@ z_query_consolidation_t z_query_consolidation_none(void) { z_query_consolidation_t z_query_consolidation_default(void) { return z_query_consolidation_auto(); } -void z_query_parameters(const z_loaned_query_t *query, z_view_str_t *parameters) { - // TODO(sashacmc) - // *parameters = _z_bytes_wrap((uint8_t *)query->in->val._parameters, strlen(query->in->val._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_loaned_query_t *query) { return query->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_loaned_query_t *query) { return query->in->val.attachment; } @@ -302,16 +301,6 @@ z_attachment_t z_query_attachment(const z_loaned_query_t *query) { return query- 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_loaned_sample_t *sample) { if (closure->call != NULL) { (closure->call)(sample, closure->context); @@ -401,18 +390,20 @@ static inline void _z_owner_noop_copy(void *dst, const void *src) { } // TODO(sashacmc): remove -// OWNED_FUNCTIONS_STR(z_str_t, z_owned_str_t, str, _z_str_free, _z_str_n_copy) +// OWNED_FUNCTIONS_STR(z_str_t, z_owned_string_t, str, _z_str_free, _z_str_n_copy) 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, str, _z_string_copy, _z_string_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, str) +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_str_array_t, str_array, _z_owner_noop_copy, _z_str_array_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) OWNED_FUNCTIONS_RC(sample) @@ -597,7 +588,7 @@ z_qos_t z_sample_qos(const z_loaned_sample_t *sample) { return _Z_RC_IN_VAL(samp z_attachment_t z_sample_attachment(const z_loaned_sample_t *sample) { return _Z_RC_IN_VAL(sample).attachment; } #endif -const char *z_str_data(const z_loaned_str_t *str) { return str->val; } +const char *z_str_data(const z_loaned_string_t *str) { return str->val; } #if Z_FEATURE_PUBLICATION == 1 int8_t _z_publisher_drop(_z_publisher_t **pub) { @@ -799,8 +790,8 @@ OWNED_FUNCTIONS_RC(reply) void z_get_options_default(z_get_options_t *options) { options->target = z_query_target_default(); options->consolidation = z_query_consolidation_default(); - options->value.encoding = z_encoding_default(); - options->value.payload = _z_bytes_empty(); + options->encoding = z_encoding_default(); + z_bytes_null(options->payload); #if Z_FEATURE_ATTACHMENT == 1 options->attachment = z_attachment_null(); #endif @@ -808,7 +799,7 @@ void z_get_options_default(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, const z_get_options_t *options) { + z_owned_closure_reply_t *callback, z_get_options_t *options) { int8_t ret = _Z_RES_OK; void *ctx = callback->context; @@ -819,7 +810,8 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co 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 @@ -833,13 +825,16 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co opt.consolidation.mode = Z_CONSOLIDATION_MODE_LATEST; } } - ret = _z_query(&_Z_RC_IN_VAL(zs), *keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, - callback->call, callback->drop, ctx, opt.timeout_ms + z_loaned_bytes_t *payload = z_bytes_loan_mut(opt.payload); + _z_value_t value = {.payload = _z_bytes_wrap(payload->start, payload->len), .encoding = opt.encoding}; + 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 ); + z_bytes_drop(opt.payload); return ret; } @@ -852,9 +847,9 @@ _Bool z_reply_is_ok(const z_loaned_reply_t *reply) { const z_loaned_sample_t *z_reply_ok(const z_loaned_reply_t *reply) { return &reply->in->val.data.sample; } -z_value_t z_reply_err(const z_loaned_reply_t *reply) { +const z_loaned_value_t *z_reply_err(const z_loaned_reply_t *reply) { _ZP_UNUSED(reply); - return (z_value_t){.payload = _z_bytes_empty(), .encoding = z_encoding_default()}; + return NULL; } #endif @@ -906,23 +901,21 @@ int8_t z_undeclare_queryable(z_owned_queryable_t *queryable) { return _z_queryab void z_query_reply_options_default(z_query_reply_options_t *options) { options->encoding = z_encoding_default(); } -int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, const uint8_t *payload, - size_t payload_len, const z_query_reply_options_t *options) { +// TODO(sashacmc): Why z_owned_bytes_t *payload but not z_view_bytes_t, do we really want clean it up after? +int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, z_owned_bytes_t *payload, + const z_query_reply_options_t *options) { z_query_reply_options_t opts; if (options == NULL) { z_query_reply_options_default(&opts); } else { opts = *options; } - _z_value_t value = {.payload = - { - .start = payload, - ._is_alloc = false, - .len = payload_len, - }, + z_loaned_bytes_t *loaned_payload = z_bytes_loan_mut(payload); + _z_value_t value = {.payload = _z_bytes_wrap(loaned_payload->start, loaned_payload->len), .encoding = {.id = opts.encoding.id, .schema = opts.encoding.schema}}; - return _z_send_reply(&query->in->val, *keyexpr, value, Z_SAMPLE_KIND_PUT, opts.attachment); - return _Z_ERR_GENERIC; + int8_t ret = _z_send_reply(&query->in->val, *keyexpr, value, Z_SAMPLE_KIND_PUT, opts.attachment); + z_bytes_drop(payload); + return ret; } #endif diff --git a/src/collections/string.c b/src/collections/string.c index cd55c8546..6985b6baf 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -25,6 +25,13 @@ _z_string_t _z_string_make(const char *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) { @@ -127,59 +134,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/net/memory.c b/src/net/memory.c index dcf095b27..ad2201538 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); } } diff --git a/src/net/session.c b/src/net/session.c index 6c708cd38..3419cabda 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 { @@ -104,15 +104,16 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { 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; diff --git a/src/session/scout.c b/src/session/scout.c index b3a8dffb8..a0713d109 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_string_make_as_ptr(_z_locator_to_str( + &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/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index b95d1b8cc..9050138d4 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -67,7 +67,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { queries++; const z_loaned_keyexpr_t *query_ke = z_query_keyexpr(query); - z_owned_str_t k_str; + z_owned_string_t k_str; z_keyexpr_to_string(query_ke, &k_str); #ifdef ZENOH_PICO if (z_check(k_str) == false) { @@ -75,14 +75,16 @@ void query_handler(const z_loaned_query_t *query, void *arg) { } #endif - z_view_str_t pred; + 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(&_ret_qreply_opt); - z_query_reply(query, query_ke, (const uint8_t *)value, strlen(value), &_ret_qreply_opt); + z_owned_bytes_t reply_payload; + // TODO(sashacmc): value encoding + z_query_reply(query, query_ke, z_move(reply_payload), &_ret_qreply_opt); z_drop(z_move(k_str)); } @@ -95,7 +97,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *arg) { if (z_reply_is_ok(reply)) { const z_loaned_sample_t *sample = z_reply_ok(reply); - z_owned_str_t k_str; + 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) { @@ -104,7 +106,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *arg) { #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); } } @@ -114,7 +116,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { printf("%s\n", __func__); datas++; - z_owned_str_t k_str; + 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) { diff --git a/tests/z_api_null_drop_test.c b/tests/z_api_null_drop_test.c index c958deb4c..5494e283f 100644 --- a/tests/z_api_null_drop_test.c +++ b/tests/z_api_null_drop_test.c @@ -44,8 +44,8 @@ int main(void) { 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_str_t str_null_1; - z_str_null(&str_null_1); + z_owned_string_t str_null_1; + z_string_null(&str_null_1); // // Test that they actually make invalid value (where applicable) @@ -70,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); diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 4015f778a..ccc3432c9 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -52,16 +52,18 @@ void query_handler(const z_loaned_query_t *query, void *arg) { 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_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_view_str_t pred; + 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_query_keyexpr(query), (const uint8_t *)res, strlen(res), NULL); + z_owned_bytes_t reply_payload; + // TODO(sashacmc): res encoding + z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); queries++; z_drop(z_move(k_str)); @@ -76,7 +78,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *arg) { 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_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)); @@ -97,7 +99,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { snprintf(res, 64, "%s%u", uri, *(unsigned int *)arg); printf(">> Received data: %s\t(%u/%u)\n", res, datas, total); - z_owned_str_t k_str; + 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)); diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 463adc5e0..3ba31e71d 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); } diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 01906c594..6808d8f23 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -46,7 +46,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { snprintf(res, 64, "%s%u", uri, *(unsigned int *)arg); printf(">> Received data: %s\t(%u/%u)\n", res, datas, total); - z_owned_str_t k_str; + 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); diff --git a/tests/z_test_fragment_rx.c b/tests/z_test_fragment_rx.c index 81d6b27c2..55bd8bce2 100644 --- a/tests/z_test_fragment_rx.c +++ b/tests/z_test_fragment_rx.c @@ -20,7 +20,7 @@ #if Z_FEATURE_SUBSCRIPTION == 1 void data_handler(const z_loaned_sample_t *sample, void *ctx) { (void)(ctx); - z_owned_str_t keystr; + z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); bool is_valid = true; const z_loaned_bytes_t *payload = z_sample_payload(sample); diff --git a/zenohpico.pc b/zenohpico.pc index 26ddb1280..642e15247 100644 --- a/zenohpico.pc +++ b/zenohpico.pc @@ -3,6 +3,6 @@ prefix=/usr/local Name: zenohpico Description: URL: -Version: 0.11.20240503dev +Version: 0.11.20240517dev Cflags: -I${prefix}/include Libs: -L${prefix}/lib -lzenohpico From 97ba0ed8617f641df0be16f4a61d84e4ae6800b5 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Thu, 23 May 2024 22:32:01 +0200 Subject: [PATCH 14/43] Fix vec clone --- src/collections/vec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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]); } } } From 9b03aa373cbb90d64a2a118fa7e959aed159da75 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Thu, 23 May 2024 22:32:25 +0200 Subject: [PATCH 15/43] Fix payload processing --- src/api/api.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 1a24ccc81..c2f9bb0df 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -791,6 +791,8 @@ void z_get_options_default(z_get_options_t *options) { options->target = z_query_target_default(); options->consolidation = z_query_consolidation_default(); options->encoding = z_encoding_default(); + // TODO(sashacmc): add cleanup or rework to on stack + options->payload = (z_owned_bytes_t *)z_malloc(sizeof(z_owned_bytes_t)); z_bytes_null(options->payload); #if Z_FEATURE_ATTACHMENT == 1 options->attachment = z_attachment_null(); @@ -825,8 +827,10 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co opt.consolidation.mode = Z_CONSOLIDATION_MODE_LATEST; } } - z_loaned_bytes_t *payload = z_bytes_loan_mut(opt.payload); - _z_value_t value = {.payload = _z_bytes_wrap(payload->start, payload->len), .encoding = opt.encoding}; + z_loaned_bytes_t *loaned_payload = z_bytes_loan_mut(opt.payload); + _z_value_t value = { + .payload = loaned_payload ? _z_bytes_wrap(loaned_payload->start, loaned_payload->len) : _z_bytes_empty(), + .encoding = opt.encoding}; 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 @@ -911,8 +915,9 @@ int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *ke opts = *options; } z_loaned_bytes_t *loaned_payload = z_bytes_loan_mut(payload); - _z_value_t value = {.payload = _z_bytes_wrap(loaned_payload->start, loaned_payload->len), - .encoding = {.id = opts.encoding.id, .schema = opts.encoding.schema}}; + _z_value_t value = { + .payload = loaned_payload ? _z_bytes_wrap(loaned_payload->start, loaned_payload->len) : _z_bytes_empty(), + .encoding = {.id = opts.encoding.id, .schema = opts.encoding.schema}}; int8_t ret = _z_send_reply(&query->in->val, *keyexpr, value, Z_SAMPLE_KIND_PUT, opts.attachment); z_bytes_drop(payload); return ret; From e282da0b36a55ea846146386872190136d47e1e6 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 24 May 2024 09:47:38 +0200 Subject: [PATCH 16/43] fix: allow for NULL z_get payload --- src/api/api.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index c2f9bb0df..07059b2bf 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -791,9 +791,7 @@ void z_get_options_default(z_get_options_t *options) { options->target = z_query_target_default(); options->consolidation = z_query_consolidation_default(); options->encoding = z_encoding_default(); - // TODO(sashacmc): add cleanup or rework to on stack - options->payload = (z_owned_bytes_t *)z_malloc(sizeof(z_owned_bytes_t)); - z_bytes_null(options->payload); + options->payload = NULL; #if Z_FEATURE_ATTACHMENT == 1 options->attachment = z_attachment_null(); #endif @@ -827,10 +825,15 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co opt.consolidation.mode = Z_CONSOLIDATION_MODE_LATEST; } } - z_loaned_bytes_t *loaned_payload = z_bytes_loan_mut(opt.payload); - _z_value_t value = { - .payload = loaned_payload ? _z_bytes_wrap(loaned_payload->start, loaned_payload->len) : _z_bytes_empty(), - .encoding = opt.encoding}; + _z_value_t value; + if (opt.payload != NULL) { + z_loaned_bytes_t *loaned_payload = z_bytes_loan_mut(opt.payload); + value.payload = + (loaned_payload != NULL) ? _z_bytes_wrap(loaned_payload->start, loaned_payload->len) : _z_bytes_empty(); + value.encoding = opt.encoding; + } else { + value.payload = _z_bytes_empty(); + } 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 @@ -838,7 +841,9 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co opt.attachment #endif ); - z_bytes_drop(opt.payload); + if (opt.payload != NULL) { + z_bytes_drop(opt.payload); + } return ret; } @@ -914,12 +919,19 @@ int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *ke } else { opts = *options; } - z_loaned_bytes_t *loaned_payload = z_bytes_loan_mut(payload); - _z_value_t value = { - .payload = loaned_payload ? _z_bytes_wrap(loaned_payload->start, loaned_payload->len) : _z_bytes_empty(), - .encoding = {.id = opts.encoding.id, .schema = opts.encoding.schema}}; + _z_value_t value; + if (payload != NULL) { + z_loaned_bytes_t *loaned_payload = z_bytes_loan_mut(payload); + value.payload = + (loaned_payload != NULL) ? _z_bytes_wrap(loaned_payload->start, loaned_payload->len) : _z_bytes_empty(); + value.encoding = opts.encoding; + } else { + value.payload = _z_bytes_empty(); + } int8_t ret = _z_send_reply(&query->in->val, *keyexpr, value, Z_SAMPLE_KIND_PUT, opts.attachment); - z_bytes_drop(payload); + if (payload != NULL) { + z_bytes_drop(payload); + } return ret; } #endif From 28d0f4083146cdf8b9aa5b6e27213a2c40cc592c Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 24 May 2024 09:48:28 +0200 Subject: [PATCH 17/43] fix: add z_locator_to_zstring to avoid memory leak --- include/zenoh-pico/link/endpoint.h | 1 + src/link/endpoint.c | 20 ++++++++++++++++++++ src/session/scout.c | 4 ++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/include/zenoh-pico/link/endpoint.h b/include/zenoh-pico/link/endpoint.h index bd7c8cbc3..5e568a15e 100644 --- a/include/zenoh-pico/link/endpoint.h +++ b/include/zenoh-pico/link/endpoint.h @@ -50,6 +50,7 @@ _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_zstring(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/src/link/endpoint.c b/src/link/endpoint.c index 035e646a6..1cd61611f 100644 --- a/src/link/endpoint.c +++ b/src/link/endpoint.c @@ -263,6 +263,26 @@ char *_z_locator_to_str(const _z_locator_t *l) { return dst; } +/** + * Converts a :c:type:`_z_locator_t` into its _z_string format. + * + * Parameters: + * loc: :c:type:`_z_locator_t` to be converted into its _z_string format. + * + * Returns: + * The pointer to the z_stringified :c:type:`_z_locator_t`. + */ +_z_string_t *_z_locator_to_zstring(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; + } + __z_locator_onto_str(s->val, s->len, loc); + return s; +} + /*------------------ Endpoint ------------------*/ void _z_endpoint_init(_z_endpoint_t *endpoint) { _z_locator_init(&endpoint->_locator); diff --git a/src/session/scout.c b/src/session/scout.c index a0713d109..709251d18 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -87,8 +87,8 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig hello->locators = _z_string_vec_make(n_loc); for (size_t i = 0; i < n_loc; i++) { _z_string_vec_append(&hello->locators, - _z_string_make_as_ptr(_z_locator_to_str( - &s_msg._body._hello._locators._val[i]))); + _z_locator_to_zstring( + &s_msg._body._hello._locators._val[i])); } } else { // @TODO: construct the locator departing from the sock address From a9c49abc7046c321781f4d13c9e471e940f62b51 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 24 May 2024 10:35:11 +0200 Subject: [PATCH 18/43] fix: init value encoding for query/reply --- src/api/api.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 07059b2bf..3ed85b742 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -825,15 +825,14 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co opt.consolidation.mode = Z_CONSOLIDATION_MODE_LATEST; } } - _z_value_t value; + // Set value + _z_value_t value = {.payload = _z_bytes_empty(), .encoding = opt.encoding}; if (opt.payload != NULL) { z_loaned_bytes_t *loaned_payload = z_bytes_loan_mut(opt.payload); value.payload = (loaned_payload != NULL) ? _z_bytes_wrap(loaned_payload->start, loaned_payload->len) : _z_bytes_empty(); - value.encoding = opt.encoding; - } else { - value.payload = _z_bytes_empty(); } + 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 @@ -919,15 +918,14 @@ int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *ke } else { opts = *options; } - _z_value_t value; + // Set value + _z_value_t value = {.payload = _z_bytes_empty(), .encoding = opts.encoding};; if (payload != NULL) { z_loaned_bytes_t *loaned_payload = z_bytes_loan_mut(payload); value.payload = (loaned_payload != NULL) ? _z_bytes_wrap(loaned_payload->start, loaned_payload->len) : _z_bytes_empty(); - value.encoding = opts.encoding; - } else { - value.payload = _z_bytes_empty(); } + int8_t ret = _z_send_reply(&query->in->val, *keyexpr, value, Z_SAMPLE_KIND_PUT, opts.attachment); if (payload != NULL) { z_bytes_drop(payload); From 45fe6f50ad41928663fe850075396fb2fc4409e4 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 24 May 2024 10:50:38 +0200 Subject: [PATCH 19/43] chore: format issues --- src/api/api.c | 4 ++-- src/session/scout.c | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 3ed85b742..ea3269b9f 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -919,13 +919,13 @@ int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *ke opts = *options; } // Set value - _z_value_t value = {.payload = _z_bytes_empty(), .encoding = opts.encoding};; + _z_value_t value = {.payload = _z_bytes_empty(), .encoding = opts.encoding}; if (payload != NULL) { z_loaned_bytes_t *loaned_payload = z_bytes_loan_mut(payload); value.payload = (loaned_payload != NULL) ? _z_bytes_wrap(loaned_payload->start, loaned_payload->len) : _z_bytes_empty(); } - + int8_t ret = _z_send_reply(&query->in->val, *keyexpr, value, Z_SAMPLE_KIND_PUT, opts.attachment); if (payload != NULL) { z_bytes_drop(payload); diff --git a/src/session/scout.c b/src/session/scout.c index 709251d18..675290570 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -86,9 +86,9 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig if (n_loc > 0) { hello->locators = _z_string_vec_make(n_loc); for (size_t i = 0; i < n_loc; i++) { - _z_string_vec_append(&hello->locators, - _z_locator_to_zstring( - &s_msg._body._hello._locators._val[i])); + _z_string_vec_append( + &hello->locators, + _z_locator_to_zstring(&s_msg._body._hello._locators._val[i])); } } else { // @TODO: construct the locator departing from the sock address From 0e551ad80ed8f3b5004184bd88ac7b83c93bc2c3 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 24 May 2024 16:23:52 +0200 Subject: [PATCH 20/43] refactor: rename locator_to_str function --- include/zenoh-pico/link/endpoint.h | 2 +- src/link/endpoint.c | 4 ++-- src/protocol/codec/message.c | 2 +- tests/z_msgcodec_test.c | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/zenoh-pico/link/endpoint.h b/include/zenoh-pico/link/endpoint.h index 5e568a15e..f1212fd42 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); +char *_z_locator_to_string(const _z_locator_t *l); _z_string_t *_z_locator_to_zstring(const _z_locator_t *loc); int8_t _z_locator_from_str(_z_locator_t *lc, const char *s); diff --git a/src/link/endpoint.c b/src/link/endpoint.c index 1cd61611f..53255b319 100644 --- a/src/link/endpoint.c +++ b/src/link/endpoint.c @@ -254,7 +254,7 @@ void __z_locator_onto_str(char *dst, size_t dst_len, const _z_locator_t *loc) { * Returns: * The pointer to the stringified :c:type:`_z_locator_t`. */ -char *_z_locator_to_str(const _z_locator_t *l) { +char *_z_locator_to_string(const _z_locator_t *l) { size_t len = _z_locator_strlen(l) + (size_t)1; char *dst = (char *)z_malloc(len); if (dst != NULL) { @@ -438,7 +438,7 @@ 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); + char *locator = _z_locator_to_string(&endpoint->_locator); if (locator != NULL) { size_t len = 1; // Start with space for the null-terminator len = len + strlen(locator); diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 32fd16299..4edd4d7f1 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -148,7 +148,7 @@ 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]); + char *s = _z_locator_to_string(&la->_val[i]); _Z_RETURN_IF_ERR(_z_str_encode(wbf, s)) z_free(s); } diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 3ba31e71d..0b2d80db8 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -331,8 +331,8 @@ 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); + char *ls = _z_locator_to_string(l); + char *rs = _z_locator_to_string(r); printf("%s:%s", ls, rs); if (i < left->_len - 1) printf(" "); From 90cdc5f156bbd9be22d2e82b10ea313f98577684 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 24 May 2024 16:34:17 +0200 Subject: [PATCH 21/43] feat: add a z_byte_from_owned_byte function --- src/api/api.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index ea3269b9f..316bc74f7 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -406,6 +406,14 @@ OWNED_FUNCTIONS_PTR(_z_string_vec_t, string_array, _z_owner_noop_copy, _z_string 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) // TODO(sashacmc): drop != close OWNED_FUNCTIONS_RC(session) @@ -826,12 +834,7 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co } } // Set value - _z_value_t value = {.payload = _z_bytes_empty(), .encoding = opt.encoding}; - if (opt.payload != NULL) { - z_loaned_bytes_t *loaned_payload = z_bytes_loan_mut(opt.payload); - value.payload = - (loaned_payload != NULL) ? _z_bytes_wrap(loaned_payload->start, loaned_payload->len) : _z_bytes_empty(); - } + _z_value_t value = {.payload = _z_bytes_from_owned_bytes(opt.payload), .encoding = opt.encoding}; ret = _z_query(&_Z_RC_IN_VAL(zs), *keyexpr, parameters, opt.target, opt.consolidation.mode, value, callback->call, callback->drop, ctx, opt.timeout_ms @@ -919,12 +922,7 @@ int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *ke opts = *options; } // Set value - _z_value_t value = {.payload = _z_bytes_empty(), .encoding = opts.encoding}; - if (payload != NULL) { - z_loaned_bytes_t *loaned_payload = z_bytes_loan_mut(payload); - value.payload = - (loaned_payload != NULL) ? _z_bytes_wrap(loaned_payload->start, loaned_payload->len) : _z_bytes_empty(); - } + _z_value_t value = {.payload = _z_bytes_from_owned_bytes(payload), .encoding = opts.encoding}; int8_t ret = _z_send_reply(&query->in->val, *keyexpr, value, Z_SAMPLE_KIND_PUT, opts.attachment); if (payload != NULL) { From 65be6ca5351d1df46ae855c5555b89514144f0b5 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Mon, 27 May 2024 10:34:07 +0200 Subject: [PATCH 22/43] fix: remove char *z_locator_to_string --- include/zenoh-pico/link/endpoint.h | 3 +- include/zenoh-pico/protocol/codec/core.h | 1 + src/link/endpoint.c | 80 +++++++++--------------- src/protocol/codec.c | 7 +++ src/protocol/codec/message.c | 4 +- src/session/scout.c | 2 +- tests/z_msgcodec_test.c | 6 +- 7 files changed, 46 insertions(+), 57 deletions(-) diff --git a/include/zenoh-pico/link/endpoint.h b/include/zenoh-pico/link/endpoint.h index f1212fd42..9512e32e6 100644 --- a/include/zenoh-pico/link/endpoint.h +++ b/include/zenoh-pico/link/endpoint.h @@ -49,8 +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_string(const _z_locator_t *l); -_z_string_t *_z_locator_to_zstring(const _z_locator_t *loc); +_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/protocol/codec/core.h b/include/zenoh-pico/protocol/codec/core.h index 93010a9ba..075111156 100644 --- a/include/zenoh-pico/protocol/codec/core.h +++ b/include/zenoh-pico/protocol/codec/core.h @@ -72,6 +72,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/src/link/endpoint.c b/src/link/endpoint.c index 53255b319..b2464ba10 100644 --- a/src/link/endpoint.c +++ b/src/link/endpoint.c @@ -245,24 +245,6 @@ 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. - * - * Parameters: - * loc: :c:type:`_z_locator_t` to be converted into its string format. - * - * Returns: - * The pointer to the stringified :c:type:`_z_locator_t`. - */ -char *_z_locator_to_string(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); - } - return dst; -} - /** * Converts a :c:type:`_z_locator_t` into its _z_string format. * @@ -272,7 +254,7 @@ char *_z_locator_to_string(const _z_locator_t *l) { * Returns: * The pointer to the z_stringified :c:type:`_z_locator_t`. */ -_z_string_t *_z_locator_to_zstring(const _z_locator_t *loc) { +_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); @@ -437,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_string(&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/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 4edd4d7f1..90f9b3e68 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_string(&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); } diff --git a/src/session/scout.c b/src/session/scout.c index 675290570..956163be8 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -88,7 +88,7 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig for (size_t i = 0; i < n_loc; i++) { _z_string_vec_append( &hello->locators, - _z_locator_to_zstring(&s_msg._body._hello._locators._val[i])); + _z_locator_to_string(&s_msg._body._hello._locators._val[i])); } } else { // @TODO: construct the locator departing from the sock address diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 0b2d80db8..7f515198d 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -331,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_string(l); - char *rs = _z_locator_to_string(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); From 7ae284da13786922e140fdc87e673a43180806f4 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Mon, 27 May 2024 11:24:05 +0200 Subject: [PATCH 23/43] feat: add value encoding function --- examples/unix/c11/z_get.c | 8 ++++++-- examples/unix/c11/z_queryable.c | 6 +++++- include/zenoh-pico/api/primitives.h | 9 +++++++-- include/zenoh-pico/collections/string.h | 1 + src/api/api.c | 14 +++++++++++++- src/collections/string.c | 7 +++++++ 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index 132f566f2..14ab50d40 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -131,9 +131,13 @@ int main(int argc, char **argv) { printf("Sending Query '%s'...\n", keyexpr); z_get_options_t opts; z_get_options_default(&opts); + if (value != NULL) { - // TODO(sashacmc): encoding - // opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + z_view_string_t value_str; + z_view_str_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(); diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index 08ced0e1f..5cc32f236 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -62,8 +62,12 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { options.attachment = z_bytes_map_as_attachment(&map); #endif + // Reply value encoding + z_view_string_t reply_str; + z_view_str_wrap(&reply_str, value); z_owned_bytes_t reply_payload; - // TODO(sashacmc): value encoding + 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++; diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 6daa41f20..eacf11fd0 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -440,6 +440,11 @@ z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix); */ z_encoding_t z_encoding_default(void); +/** + * Encodes string by aliasing. + */ +int8_t z_bytes_encode_from_string(z_owned_bytes_t *buffer, const z_loaned_string_t *s); + /** * * Checks validity of the timestamp @@ -820,10 +825,10 @@ _OWNED_FUNCTIONS_CLOSURE(z_owned_closure_zid_t, closure_zid) _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 refcountered type (e.g. z_loaned_session_t, z_query_t) +// 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 refcountered owned type (e.g. z_owned_session_t, z_owned_query_t) +// 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) // TODO(sashacmc): comments, docs, etc. diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index 827017145..bd0b599b4 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -70,6 +70,7 @@ typedef struct { } _z_string_t; _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); diff --git a/src/api/api.c b/src/api/api.c index 316bc74f7..5e4b465f3 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -44,7 +44,7 @@ /********* Data Types Handlers *********/ int8_t z_view_str_wrap(z_view_string_t *str, const char *value) { - str->_val = _z_string_make(value); + str->_val = _z_string_wrap((char *)value); return _Z_RES_OK; } @@ -266,6 +266,18 @@ z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix) { z_encoding_t z_encoding_default(void) { return z_encoding(Z_ENCODING_PREFIX_DEFAULT, NULL); } +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; } diff --git a/src/collections/string.c b/src/collections/string.c index 6985b6baf..927dc7dab 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -25,6 +25,13 @@ _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); From 28583147f1919e8846e00a2f8854cec13f34fcb8 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Mon, 27 May 2024 12:11:41 +0200 Subject: [PATCH 24/43] fix: update modularity test --- tests/modularity.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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... From 1a7c73e3f6852b674d66955d5f3af1d70497fd96 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Mon, 27 May 2024 17:17:34 +0200 Subject: [PATCH 25/43] Cleanup TODOs --- include/zenoh-pico/api/primitives.h | 25 ------------------------- include/zenoh-pico/api/types.h | 19 +++++-------------- src/api/api.c | 26 +------------------------- 3 files changed, 6 insertions(+), 64 deletions(-) diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index eacf11fd0..19b3feeba 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -88,31 +88,6 @@ int8_t z_view_keyexpr_from_string_unchecked(z_view_keyexpr_t *keyexpr, const cha */ void z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *s); -/** - * Returns the key expression's internal string by aliasing it. - * - * Parameters: - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` - * - * Returns: - * The :c:type:`z_loaned_bytes_t*` pointing to key expression string representation if it's possible - */ -z_loaned_bytes_t *z_keyexpr_as_bytes(z_loaned_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_loaned_keyexpr_t *keyexpr); - /** * Constructs a null-terminated string departing from a :c:type:`z_keyexpr_t` for a given :c:type:`z_loaned_session_t`. * The user is responsible of dropping the returned string using ``z_free``. diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index a25577ada..0e0fa8c40 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -192,13 +192,12 @@ _LOANED_TYPE(_z_query_rc_t, query) * z_encoding_prefix_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. */ -// TODO(sashacmc): +// TODO(sashacmc): z_encoding_t typedef _z_encoding_t z_encoding_t; /* * Represents timestamp value in Zenoh */ -// TODO(sashacmc): typedef _z_timestamp_t z_timestamp_t; /** @@ -453,7 +452,7 @@ _LOANED_TYPE(_z_hello_t, hello) * z_sample_t sample: The :c:type:`_z_sample_t` containing the key and value of the reply. * z_loaned_bytes_t* replier_id: The id of the replier that sent this reply. */ -// TODO(sashacmc): +// TODO(sashacmc): z_reply_data_t typedef _z_reply_data_t z_reply_data_t; /** @@ -497,7 +496,6 @@ typedef _z_data_handler_t z_data_handler_t; * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ -// TODO(sashacmc): typedef struct { void *context; z_data_handler_t call; @@ -517,7 +515,6 @@ void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_loan * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. void *context: a * pointer to an arbitrary state. */ -// TODO(sashacmc): typedef struct { void *context; z_owned_sample_handler_t call; @@ -539,7 +536,6 @@ typedef _z_queryable_handler_t z_queryable_handler_t; * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ -// TODO(sashacmc): typedef struct { void *context; z_queryable_handler_t call; @@ -561,7 +557,6 @@ typedef void (*z_owned_query_handler_t)(z_owned_query_t *query, void *arg); * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. void *context: a * pointer to an arbitrary state. */ -// TODO(sashacmc): typedef struct { void *context; z_owned_query_handler_t call; @@ -584,7 +579,6 @@ typedef _z_reply_handler_t z_reply_handler_t; * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ -// TODO(sashacmc): typedef struct { void *context; z_reply_handler_t call; @@ -604,7 +598,6 @@ void z_closure_reply_call(const z_owned_closure_reply_t *closure, const z_loaned * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ -// TODO(sashacmc): typedef struct { void *context; z_owned_reply_handler_t call; @@ -626,7 +619,6 @@ typedef void (*z_owned_hello_handler_t)(z_owned_hello_t *hello, void *arg); * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ -// TODO(sashacmc): typedef struct { void *context; z_owned_hello_handler_t call; @@ -647,7 +639,6 @@ typedef void (*z_id_handler_t)(const z_id_t *id, void *arg); * z_dropper_handler_t drop: `void *drop(void*)` allows the callback's state to be freed. * void *context: a pointer to an arbitrary state. */ -// TODO(sashacmc): typedef struct { void *context; z_id_handler_t call; @@ -669,7 +660,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): +// 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; @@ -743,12 +734,12 @@ int8_t z_bytes_map_iter(const z_owned_bytes_map_t *this_, z_attachment_iter_body /** * Constructs a new map. */ -// TODO(sashacmc): +// 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` */ -// TODO(sashacmc): +// TODO(sashacmc): z_bytes_map_null for attachment z_owned_bytes_map_t z_bytes_map_null(void); #endif diff --git a/src/api/api.c b/src/api/api.c index 5e4b465f3..90fd0bafb 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -76,23 +76,6 @@ void z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *s) } } -/* -TODO(sashacmc) -z_loaned_bytes_t* z_keyexpr_as_bytes(z_loaned_keyexpr_t *keyexpr) { - if (keyexpr._id == Z_RESOURCE_ID_NONE) { - z_loaned_bytes_t* ret = {.start = (const uint8_t *)keyexpr._suffix, .len = strlen(keyexpr._suffix), ._is_alloc = -false}; return ret; } else { z_loaned_bytes_t* ret = {.start = NULL, .len = 0, ._is_alloc = false}; return ret; - } -} - -_Bool zp_keyexpr_was_declared(const z_keyexpr_t *keyexpr) { - _Bool ret = false; - if (keyexpr->_id != Z_RESOURCE_ID_NONE) { - ret = true; - } - 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 @@ -361,7 +344,6 @@ void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id) } } -// TODO(sashacmc): return value for drop? (queryable, etc) #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; } \ @@ -380,7 +362,7 @@ void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id) } \ } -// TODO(sashacmc): Sould drop clean pinter to allow double call ? +// TODO(sashacmc): Sould drop clean ponter to allow double call ? #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; } \ @@ -401,9 +383,6 @@ static inline void _z_owner_noop_copy(void *dst, const void *src) { (void)(src); } -// TODO(sashacmc): remove -// OWNED_FUNCTIONS_STR(z_str_t, z_owned_string_t, str, _z_str_free, _z_str_n_copy) - 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) @@ -924,7 +903,6 @@ int8_t z_undeclare_queryable(z_owned_queryable_t *queryable) { return _z_queryab void z_query_reply_options_default(z_query_reply_options_t *options) { options->encoding = z_encoding_default(); } -// TODO(sashacmc): Why z_owned_bytes_t *payload but not z_view_bytes_t, do we really want clean it up after? 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; @@ -1264,6 +1242,4 @@ 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; } -// TODO(sashacmc): -// z_loaned_bytes_t *z_bytes_null(void) { return (z_loaned_bytes_t *){.len = 0, ._is_alloc = false, .start = NULL}; } #endif From 7fe58b55d8f6ce5a043a82494d1e0e6598a3666e Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Tue, 28 May 2024 13:11:07 +0200 Subject: [PATCH 26/43] Add RC drop cleanup --- src/api/api.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 90fd0bafb..95db44cc1 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -362,7 +362,6 @@ void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id) } \ } -// TODO(sashacmc): Sould drop clean ponter to allow double call ? #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; } \ @@ -372,7 +371,13 @@ void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id) void z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src) { \ obj->_rc = _z_##name##_rc_clone((z_loaned_##name##_t *)src); \ } \ - void z_##name##_drop(z_owned_##name##_t *val) { _z_##name##_rc_drop(&val->_rc); } + 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; } \ @@ -406,7 +411,6 @@ static _z_bytes_t _z_bytes_from_owned_bytes(z_owned_bytes_t *bytes) { } OWNED_FUNCTIONS_RC(sample) -// TODO(sashacmc): drop != close OWNED_FUNCTIONS_RC(session) #define OWNED_FUNCTIONS_CLOSURE(ownedtype, name, f_call, f_drop) \ From 3a2716f026d621c77c18ddd4849acf87985fb617 Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Tue, 28 May 2024 13:40:11 +0200 Subject: [PATCH 27/43] Fixing CI issues (#5) * fix: init encoding in put options * fix: switch z_keyexpr functions return value to _Bool * fix: z_api_alignment_test issues * fix: z_api_alignment_test memory leak * fix: missin reply encoding in z_client_test * fix: add value encoding in examples * ci: add retry to raweth test --- .github/workflows/build-check.yaml | 14 +- examples/arduino/z_get.ino | 8 +- examples/arduino/z_queryable.ino | 7 +- examples/espidf/z_get.c | 8 +- examples/espidf/z_queryable.c | 6 +- examples/freertos_plus_tcp/z_get.c | 8 +- examples/freertos_plus_tcp/z_queryable.c | 6 +- examples/mbed/z_get.cpp | 8 +- examples/mbed/z_queryable.cpp | 6 +- examples/unix/c11/z_get_channel.c | 8 +- examples/unix/c11/z_queryable_channel.c | 6 +- examples/unix/c99/z_get.c | 8 +- examples/unix/c99/z_queryable.c | 6 +- examples/windows/z_get.c | 8 +- examples/windows/z_queryable.c | 6 +- examples/zephyr/z_get.c | 9 + examples/zephyr/z_queryable.c | 6 +- include/zenoh-pico/api/macros.h | 2 + include/zenoh-pico/api/primitives.h | 34 ++- src/api/api.c | 67 ++---- src/net/primitives.c | 1 + src/protocol/config.c | 2 +- tests/z_api_alignment_test.c | 12 +- tests/z_client_test.c | 7 +- tests/z_keyexpr_test.c | 286 +++++++++++------------ 25 files changed, 296 insertions(+), 243 deletions(-) diff --git a/.github/workflows/build-check.yaml b/.github/workflows/build-check.yaml index b90e5feaf..b81b92831 100644 --- a/.github/workflows/build-check.yaml +++ b/.github/workflows/build-check.yaml @@ -126,11 +126,15 @@ jobs: 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 + uses: nick-fields/retry@v3 + with: + timeout_minutes: 5 + max_attempts: 3 + retry_on: error + command: | + sudo apt install -y ninja-build + CMAKE_GENERATOR=Ninja make + python3 ./build/tests/raweth.py --reth $Z_FEATURE_RAWETH_TRANSPORT env: Z_FEATURE_RAWETH_TRANSPORT: ${{ matrix.feature_reth }} diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index f5853253e..70cc7c328 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -113,9 +113,13 @@ void loop() { Serial.println(" ..."); z_get_options_t opts; z_get_options_default(&opts); + // Value encoding if (strcmp(VALUE, "") != 0) { - // TODO(sashacmc): encoding - // opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + z_view_string_t value_str; + z_view_str_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(&callback, reply_handler, reply_dropper, NULL); diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index c3c022ce6..5f7b69850 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -47,8 +47,13 @@ void query_handler(const z_loaned_query_t *query, void *arg) { z_view_keyexpr_t ke; z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + + // Reply value encoding + z_view_string_t reply_str; + z_view_str_wrap(&reply_str, VALUE); z_owned_bytes_t reply_payload; - // TODO(sashacmc): VALUE encoding + z_bytes_encode_from_string(&reply_payload, z_view_string_loan(&reply_str)); + z_query_reply(query, z_view_keyexpr_loan(&ke), z_bytes_move(&reply_payload), NULL); z_string_drop(z_string_move(&keystr)); diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index d42474e8d..9424c03d3 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -160,9 +160,13 @@ void app_main() { printf("Sending Query '%s'...\n", KEYEXPR); z_get_options_t opts; z_get_options_default(&opts); + // Value encoding if (strcmp(VALUE, "") != 0) { - // TODO(sashacmc): encoding - // opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + z_view_string_t value_str; + z_view_str_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); diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index 8c8da8896..9eafadb57 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -111,8 +111,12 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_loan(params)->val); z_view_keyexpr_t ke; z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + // Reply value encoding + z_view_string_t reply_str; + z_view_str_wrap(&reply_str, VALUE); z_owned_bytes_t reply_payload; - // TODO(sashacmc): VALUE encoding + 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)); } diff --git a/examples/freertos_plus_tcp/z_get.c b/examples/freertos_plus_tcp/z_get.c index efe049411..49744ae6d 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -83,9 +83,13 @@ void app_main(void) { printf("Sending Query '%s'...\n", KEYEXPR); z_get_options_t opts; z_get_options_default(&opts); + // Value encoding if (strcmp(VALUE, "") != 0) { - // TODO(sashacmc): encoding - // opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + z_view_string_t value_str; + z_view_str_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); diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index 95eb3813c..39fbabfad 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -43,8 +43,12 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { } z_query_reply_options_t options; z_query_reply_options_default(&options); + // Reply value encoding + z_view_string_t reply_str; + z_view_str_wrap(&reply_str, VALUE); z_owned_bytes_t reply_payload; - // TODO(sashacmc): VALUE encoding + 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)); } diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index 12340b094..4b912c254 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -79,9 +79,13 @@ int main(int argc, char **argv) { printf("Sending Query '%s'...\n", KEYEXPR); z_get_options_t opts; z_get_options_default(&opts); + // Value encoding if (strcmp(VALUE, "") != 0) { - // TODO(sashacmc): encoding - // opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE)); + z_view_string_t value_str; + z_view_str_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(&callback, reply_handler, reply_dropper, NULL); diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index bf271d92d..5828f920f 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -40,8 +40,12 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_string_loan(&keystr)), (int)z_view_string_loan(&pred)->len, z_view_string_loan(&pred)->val); + // Reply value encoding + z_view_string_t reply_str; + z_view_str_wrap(&reply_str, VALUE); z_owned_bytes_t reply_payload; - // TODO(sashacmc): VALUE encoding + 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)); } diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index d2f4c0a41..e5690e13f 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -90,9 +90,13 @@ int main(int argc, char **argv) { printf("Sending Query '%s'...\n", keyexpr); z_get_options_t opts; z_get_options_default(&opts); + // Value encoding if (value != NULL) { - // TODO(sashacmc): encoding - // opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + z_view_string_t value_str; + z_view_str_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(&channel, 1); diff --git a/examples/unix/c11/z_queryable_channel.c b/examples/unix/c11/z_queryable_channel.c index ce128023a..d5df62bf7 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -112,8 +112,12 @@ int main(int argc, char **argv) { } z_query_reply_options_t options; z_query_reply_options_default(&options); + // Reply value encoding + z_view_string_t reply_str; + z_view_str_wrap(&reply_str, value); z_owned_bytes_t reply_payload; - // TODO(sashacmc): value encoding + 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)); diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 38a6339c7..08d51e440 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -117,9 +117,13 @@ int main(int argc, char **argv) { printf("Sending Query '%s'...\n", keyexpr); z_get_options_t opts; z_get_options_default(&opts); + // Value encoding if (value != NULL) { - // TODO(sashacmc): encoding - // opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + z_view_string_t value_str; + z_view_str_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(&callback, reply_handler, reply_dropper, NULL); diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index df3a8a6e2..8f69c8387 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -33,8 +33,12 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_reply_options_t options; z_query_reply_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + // Reply value encoding + z_view_string_t reply_str; + z_view_str_wrap(&reply_str, value); z_owned_bytes_t reply_payload; - // TODO(sashacmc): value encoding + 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)); } diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index 405b1a279..7d77faf1e 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -82,9 +82,13 @@ int main(int argc, char **argv) { printf("Sending Query '%s'...\n", keyexpr); z_get_options_t opts; z_get_options_default(&opts); + // Value encoding if (value != NULL) { - // TODO(sashacmc): encoding - // opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + z_view_string_t value_str; + z_view_str_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); diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index d810c0926..d4540ec64 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -37,8 +37,12 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_reply_options_t options; z_query_reply_options_default(&options); + // Reply value encoding + z_view_string_t reply_str; + z_view_str_wrap(&reply_str, value); z_owned_bytes_t reply_payload; - // TODO(sashacmc): value encoding + 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)); diff --git a/examples/zephyr/z_get.c b/examples/zephyr/z_get.c index 58b0d9221..fe478deef 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -30,6 +30,7 @@ #endif #define KEYEXPR "demo/example/**" +#define VALUE "" void reply_dropper(void *ctx) { printf(" >> Received query final notification\n"); } @@ -76,6 +77,14 @@ int main(int argc, char **argv) { z_get_options_t opts; z_get_options_default(&opts); opts.target = Z_QUERY_TARGET_ALL; + // Value encoding + if (strcmp(VALUE, "") != 0) { + z_view_string_t value_str; + z_view_str_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; diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index 68fc5d4d8..173191bb5 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -40,8 +40,12 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), z_loan(params)->len, z_loan(params)->val); + // Reply value encoding + z_view_string_t reply_str; + z_view_str_wrap(&reply_str, VALUE); z_owned_bytes_t reply_payload; - // TODO(sashacmc): VALUE encoding + 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)); } diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index bf5cf8c96..a38403125 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -270,6 +270,7 @@ 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 z_loaned_string_t type; }; template<> inline z_session_t z_loan(const z_owned_session_t& x) { return z_session_loan(&x); } template<> inline z_keyexpr_t z_loan(const z_owned_keyexpr_t& x) { return z_keyexpr_loan(&x); } @@ -277,6 +278,7 @@ template<> inline z_config_t z_loan(const z_owned_config_t& x) { return z_config 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_string_t& x) { return z_string_loan(&x); } +template<> inline z_loaned_string_t z_loan(const z_view_string_t& x) { return z_view_string_loan(&x); } template struct zenoh_drop_type { typedef T type; }; template inline typename zenoh_drop_type::type z_drop(T*); diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 19b3feeba..9fe96c38e 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -174,10 +174,10 @@ int8_t zp_keyexpr_canonize_null_terminated(char *start); * r: The second keyexpr. * * 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. + * Returns ``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(const z_loaned_keyexpr_t *l, const z_loaned_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. @@ -189,10 +189,10 @@ int8_t z_keyexpr_includes(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t * 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. + * Returns ``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. @@ -202,10 +202,10 @@ int8_t zp_keyexpr_includes_null_terminated(const char *l, const char *r); * r: The second keyexpr. * * 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. + * Returns ``true`` if the keyexprs intersect, i.e. there exists at least one key which is contained in both of the + * sets defined by ``l`` and ``r``. Otherwise, returns ``false``. */ -int8_t z_keyexpr_intersects(const z_loaned_keyexpr_t *l, const z_loaned_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. @@ -217,10 +217,10 @@ int8_t z_keyexpr_intersects(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_ * 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. + * Returns ``true`` if the keyexprs intersect, i.e. there exists at least one key which is contained in both of the + * sets defined by ``l`` and ``r``. Otherwise, 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. @@ -230,10 +230,9 @@ int8_t zp_keyexpr_intersect_null_terminated(const char *l, const char *r); * r: The second keyexpr. * * Returns: - * Returns ``0`` if both ``l`` and ``r`` are equal. Otherwise, it returns a ``-1``, or other ``negative value`` for - * errors. + * Returns ``true`` if both ``l`` and ``r`` are equal. Otherwise, returns ``false``. */ -int8_t z_keyexpr_equals(const z_loaned_keyexpr_t *l, const z_loaned_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. @@ -245,10 +244,9 @@ int8_t z_keyexpr_equals(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r * rlen: Number of characters in ``r``. * * Returns: - * Returns ``0`` if both ``l`` and ``r`` are equal. Otherwise, it returns a ``-1``, or other ``negative value`` for - * errors. + * Returns ``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); // TODO(sashacmc): update comment /** diff --git a/src/api/api.c b/src/api/api.c index 95db44cc1..c2be288ed 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -113,81 +113,51 @@ int8_t zp_keyexpr_canonize_null_terminated(char *start) { return ret; } -int8_t z_keyexpr_includes(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r) { - int8_t ret = 0; - +_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)) { - ret = zp_keyexpr_includes_null_terminated(l->_suffix, r->_suffix); - } else { - ret = _Z_ERR_GENERIC; + 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(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r) { - int8_t ret = 0; - +_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)) { - ret = zp_keyexpr_intersect_null_terminated(l->_suffix, r->_suffix); - } else { - ret = _Z_ERR_GENERIC; + 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(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t *r) { - int8_t ret = 0; - +_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)) { - ret = zp_keyexpr_equals_null_terminated(l->_suffix, r->_suffix); - } else { - ret = _Z_ERR_GENERIC; + 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; } void z_config_new(z_owned_config_t *config) { config->_val = _z_config_empty(); } @@ -608,6 +578,7 @@ OWNED_FUNCTIONS_PTR(_z_publisher_t, publisher, _z_owner_noop_copy, _z_publisher_ void z_put_options_default(z_put_options_t *options) { options->congestion_control = Z_CONGESTION_CONTROL_DEFAULT; options->priority = Z_PRIORITY_DEFAULT; + options->encoding = z_encoding_default(); #if Z_FEATURE_ATTACHMENT == 1 options->attachment = z_attachment_null(); #endif diff --git a/src/net/primitives.c b/src/net/primitives.c index 8724f25a8..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 ------------------*/ diff --git a/src/protocol/config.c b/src/protocol/config.c index 40c895c96..ad4440fc2 100644 --- a/src/protocol/config.c +++ b/src/protocol/config.c @@ -29,7 +29,7 @@ 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, _z_str_clone(value)); - if (res != value) { + if (strcmp(res, value) != 0) { ret = _Z_ERR_CONFIG_FAILED_INSERT; } diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 9050138d4..d84cae70b 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -57,8 +57,7 @@ void hello_handler(z_owned_hello_t *hello, void *arg) { (void)(arg); printf("%s\n", __func__); hellos++; - z_null(hello); - 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; @@ -82,8 +81,13 @@ void query_handler(const z_loaned_query_t *query, void *arg) { (void)(payload_value); 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_str_wrap(&reply_str, value); z_owned_bytes_t reply_payload; - // TODO(sashacmc): value encoding + 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)); @@ -350,7 +354,7 @@ int main(int argc, char **argv) { z_publisher_options_default(&_ret_pub_opt); _ret_pub_opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK; z_owned_publisher_t _ret_pub; - _ret_int8 = z_declare_publisher(&_ret_pub, z_loan(s1), z_loan(ke), &_ret_pub_opt); + _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"); diff --git a/tests/z_client_test.c b/tests/z_client_test.c index ccc3432c9..0e96d9d26 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -61,10 +61,13 @@ void query_handler(const z_loaned_query_t *query, void *arg) { assert(z_loan(pred)->len == strlen("")); assert(strncmp((const char *)z_loan(pred)->val, "", strlen("")) == 0); + // Reply value encoding + z_view_string_t reply_str; + z_view_str_wrap(&reply_str, res); z_owned_bytes_t reply_payload; - // TODO(sashacmc): res encoding - z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL); + 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); 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; } From b8975a4aef38bdb14dd0489801d83740e6f36396 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Tue, 28 May 2024 14:00:07 +0200 Subject: [PATCH 28/43] Fix sting usage in _zp_*_info_session + Formatting fix --- examples/mbed/z_get.cpp | 10 +++++----- src/transport/multicast.c | 5 +++-- src/transport/unicast.c | 7 ++++--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index 4b912c254..ded6f7e9e 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -81,11 +81,11 @@ int main(int argc, char **argv) { z_get_options_default(&opts); // Value encoding if (strcmp(VALUE, "") != 0) { - z_view_string_t value_str; - z_view_str_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_view_string_t value_str; + z_view_str_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(&callback, reply_handler, reply_dropper, NULL); diff --git a/src/transport/multicast.c b/src/transport/multicast.c index 6b2b83feb..14d0fe4df 100644 --- a/src/transport/multicast.c +++ b/src/transport/multicast.c @@ -46,8 +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)); - // TODO(sashacmc): double check! - _zp_config_insert(ps, Z_INFO_PEER_PID_KEY, _z_string_from_bytes(&remote_zid).val); + _z_string_t remote_zid_str = _z_string_from_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/unicast.c b/src/transport/unicast.c index 75e29a56b..f7d066bb8 100644 --- a/src/transport/unicast.c +++ b/src/transport/unicast.c @@ -39,9 +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)); - // TODO(sashacmc): double check! - _zp_config_insert(ps, Z_INFO_ROUTER_PID_KEY, _z_string_from_bytes(&remote_zidbytes).val); + _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_from_bytes(&remote_zid_bytes); + _zp_config_insert(ps, Z_INFO_ROUTER_PID_KEY, remote_zid_str.val); + _z_string_clear(&remote_zid_str); } #else From d134bc5243916bc01c6cb84c84f41e7e01d1cce6 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Tue, 28 May 2024 15:05:04 +0200 Subject: [PATCH 29/43] Fix OOM error processing --- include/zenoh-pico/api/primitives.h | 16 ++++++------- include/zenoh-pico/session/session.h | 6 ++--- src/api/api.c | 36 +++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 9fe96c38e..2979a9f84 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -86,7 +86,7 @@ int8_t z_view_keyexpr_from_string_unchecked(z_view_keyexpr_t *keyexpr, const cha * Returns: * The :c:type:`z_owned_string_t` containing key expression string representation if it's possible */ -void z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *s); +int8_t z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *s); /** * Constructs a null-terminated string departing from a :c:type:`z_keyexpr_t` for a given :c:type:`z_loaned_session_t`. @@ -750,13 +750,13 @@ int8_t z_closure_hello(z_owned_closure_hello_t *closure, z_owned_hello_handler_t 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(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); \ - void z_##name##_clone(ownedtype *obj, const loanedtype *src); \ - void z_##name##_drop(ownedtype *obj); \ +#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) diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index 21169f1b5..fbfdb5a62 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -80,9 +80,9 @@ typedef struct { uint32_t _id; } _z_publication_t; -// TODO(sashacmc): make in proper way -typedef struct _z_query_rc_t _z_query_rc_t; -typedef _z_query_rc_t z_loaned_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. */ diff --git a/src/api/api.c b/src/api/api.c index c2be288ed..04e19ea18 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -66,14 +66,21 @@ int8_t z_view_keyexpr_from_string_unchecked(z_view_keyexpr_t *keyexpr, const cha return _Z_RES_OK; } -void z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *s) { +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 { + ret = _Z_ERR_GENERIC; } + + return ret; } int8_t zp_keyexpr_resolve(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *str) { @@ -320,11 +327,15 @@ void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id) 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; } \ - void z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src) { \ + 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) { \ @@ -338,8 +349,13 @@ void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id) 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; } \ - void z_##name##_clone(z_owned_##name##_t *obj, const z_loaned_##name##_t *src) { \ + 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) { \ @@ -898,22 +914,30 @@ int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *ke #endif int8_t z_keyexpr_new(z_owned_keyexpr_t *key, const char *name) { + int8_t ret = _Z_RES_OK; + 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 _Z_RES_OK; + return ret; } 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->_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 _Z_RES_OK; + return ret; } int8_t z_undeclare_keyexpr(const z_loaned_session_t *zs, z_owned_keyexpr_t *keyexpr) { @@ -987,7 +1011,7 @@ int8_t z_declare_subscriber(z_owned_subscriber_t *sub, const z_loaned_session_t sub->_val = int_sub; if (int_sub == NULL) { - return _Z_ERR_SYSTEM_OUT_OF_MEMORY; // TODO(sashacmc): shoud we introduce sperate error? + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } else { return _Z_RES_OK; } From c1d56d910f6437b1e4627fd7cad4d2ace9ef3e22 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Tue, 28 May 2024 15:33:03 +0200 Subject: [PATCH 30/43] Remove z_reply_data_t from API --- include/zenoh-pico/api/types.h | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 0e0fa8c40..c77c0305e 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -445,21 +445,8 @@ _LOANED_TYPE(_z_sample_rc_t, sample) _OWNED_TYPE_PTR(_z_hello_t, hello) _LOANED_TYPE(_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_loaned_bytes_t* replier_id: The id of the replier that sent this reply. - */ -// TODO(sashacmc): z_reply_data_t -typedef _z_reply_data_t z_reply_data_t; - /** * Represents the reply to a query. - * - * Members: - * z_reply_data_t data: the content of the reply. */ _OWNED_TYPE_RC(_z_reply_rc_t, reply) _LOANED_TYPE(_z_reply_rc_t, reply) From 46f00205db483a971254cda4531e96df28930136 Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Wed, 29 May 2024 12:39:02 +0200 Subject: [PATCH 31/43] Add decode bytes function (#6) * feat: add zenoh-c decode bytes function * fix: rename string_from_bytes to string_convert_bytes * feat: add _z_string_from_bytes function --- examples/arduino/z_get.ino | 6 ++++-- examples/arduino/z_queryable.ino | 18 ++++++++++++----- examples/espidf/z_get.c | 6 ++++-- examples/espidf/z_queryable.c | 8 ++++++++ examples/freertos_plus_tcp/z_get.c | 7 +++++-- examples/freertos_plus_tcp/z_queryable.c | 10 +++++++--- examples/mbed/z_get.cpp | 7 +++++-- examples/mbed/z_queryable.cpp | 9 ++++++++- examples/unix/c11/z_get.c | 7 +++++-- examples/unix/c11/z_get_channel.c | 7 +++++-- examples/unix/c11/z_queryable.c | 10 +++++++--- examples/unix/c11/z_queryable_channel.c | 9 ++++++--- examples/unix/c99/z_get.c | 7 +++++-- examples/unix/c99/z_queryable.c | 8 ++++++++ examples/windows/z_get.c | 7 +++++-- examples/windows/z_queryable.c | 11 +++++++---- examples/zephyr/z_get.c | 7 +++++-- examples/zephyr/z_queryable.c | 9 ++++++++- include/zenoh-pico/api/primitives.h | 18 +++++++++++++++++ include/zenoh-pico/collections/string.h | 2 ++ src/api/api.c | 16 +++++++++++++++ src/collections/string.c | 25 ++++++++++++++++++++++-- src/net/session.c | 2 +- src/transport/multicast.c | 2 +- src/transport/unicast.c | 2 +- tests/z_client_test.c | 2 +- tests/z_peer_multicast_test.c | 4 ++-- 27 files changed, 180 insertions(+), 46 deletions(-) diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index 70cc7c328..3fc12d587 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -48,15 +48,17 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(oreply); 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); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); Serial.print(" >> [Get listener] Received ("); Serial.print(z_str_data(z_string_loan(&keystr))); Serial.print(", "); - Serial.print(val.c_str()); + Serial.print(z_str_data(z_string_loan(&replystr))); Serial.println(")"); z_string_drop(z_string_move(&keystr)); + z_string_drop(z_string_move(&replystr)); } else { Serial.println(" >> Received an error"); } diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index 5f7b69850..b7f652b7f 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -38,12 +38,20 @@ 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] Replying Data ('"); + Serial.print(" >> [Queryable handler] Received Query '"); Serial.print(z_str_data(z_string_loan(&keystr))); - Serial.print("': '"); - Serial.print(VALUE); - Serial.println("')"); + 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_str_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); diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index 9424c03d3..889e4e1a5 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -108,10 +108,12 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { 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_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len, - z_sample_payload(sample)->start); + printf(" >> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(" >> Received an error\n"); } diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index 9eafadb57..dbf232594 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -109,6 +109,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_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_str_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 diff --git a/examples/freertos_plus_tcp/z_get.c b/examples/freertos_plus_tcp/z_get.c index 49744ae6d..3c7ddbb3b 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -42,9 +42,12 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(reply); 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(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); } diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index 39fbabfad..be8e1672f 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -37,9 +37,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len, z_loan(params)->val); - const z_loaned_value_t *payload_value = z_query_value(query); - if (payload_value->payload.len > 0) { - printf(" with value '%.*s'\n", (int)payload_value->payload.len, payload_value->payload.start); + // 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_str_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); } z_query_reply_options_t options; z_query_reply_options_default(&options); diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index ded6f7e9e..d5f89b09d 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -38,9 +38,12 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(oreply); 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(" >> Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len, payload->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(" >> Received ('%s': '%s')\n", z_str_data(z_string_loan(&keystr)), z_str_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"); } diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index 5828f920f..64f68e9fa 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -39,7 +39,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, &pred); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_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_str_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_str_wrap(&reply_str, VALUE); diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index 14ab50d40..b650556ed 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -43,8 +43,10 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(reply); z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); - printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len, - z_sample_payload(sample)->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(replystr))); #if Z_FEATURE_ATTACHMENT == 1 if (z_attachment_check(&sample.attachment)) { printf("Attachement found\n"); @@ -52,6 +54,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { } #endif z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); } diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index e5690e13f..daefcd25c 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -112,9 +112,12 @@ int main(int argc, char **argv) { 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); - printf(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len, - z_sample_payload(sample)->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); } diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index 5cc32f236..b480c3636 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -39,9 +39,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len, z_loan(params)->val); - const z_loaned_value_t *payload_value = z_query_value(query); - if (payload_value->payload.len > 0) { - printf(" with value '%.*s'\n", (int)payload_value->payload.len, payload_value->payload.start); + // 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_str_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); } #if Z_FEATURE_ATTACHMENT == 1 z_attachment_t attachment = z_query_attachment(query); diff --git a/examples/unix/c11/z_queryable_channel.c b/examples/unix/c11/z_queryable_channel.c index d5df62bf7..562e6cd66 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -106,9 +106,12 @@ int main(int argc, char **argv) { z_query_parameters(q, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len, z_loan(params)->val); - const z_loaned_value_t *payload_value = z_query_value(q); - if (payload_value->payload.len > 0) { - printf(" with value '%.*s'\n", (int)payload_value->payload.len, payload_value->payload.start); + 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_str_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); } z_query_reply_options_t options; z_query_reply_options_default(&options); diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 08d51e440..78a9a6ad4 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -35,9 +35,12 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(reply); 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(">> Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len, payload->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_str_data(z_string_loan(&keystr)), z_str_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"); } diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index 8f69c8387..de03d1c49 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -30,6 +30,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_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_str_data(z_string_loan(&payload_string))); + z_string_drop(z_string_move(&payload_string)); + } z_query_reply_options_t options; z_query_reply_options_default(&options); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index 7d77faf1e..8e0192618 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -34,9 +34,12 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(reply); 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(">> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(">> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(">> Received an error\n"); } diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index d4540ec64..d5c06db56 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -30,11 +30,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len, z_loan(params)->val); - const z_loaned_value_t *payload_value = z_query_value(query); - if (payload_value->payload.len > 0) { - printf(" with value '%.*s'\n", (int)payload_value->payload.len, payload_value->payload.start); + // 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_str_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); } - z_query_reply_options_t options; z_query_reply_options_default(&options); // Reply value encoding diff --git a/examples/zephyr/z_get.c b/examples/zephyr/z_get.c index fe478deef..f4213b96c 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -39,9 +39,12 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { const z_loaned_sample_t *sample = z_reply_ok(oreply); 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(" >> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)payload->len, payload->start); + z_owned_string_t replystr; + z_bytes_decode_into_string(z_sample_payload(sample), &replystr); + + printf(" >> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(replystr))); z_drop(z_move(keystr)); + z_drop(z_move(replystr)); } else { printf(" >> Received an error\n"); } diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index 173191bb5..3cbb4c3bb 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -39,7 +39,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_parameters(query, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_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_str_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); + } // Reply value encoding z_view_string_t reply_str; z_view_str_wrap(&reply_str, VALUE); diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 2979a9f84..6a782b3a4 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -413,6 +413,24 @@ z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix); */ z_encoding_t z_encoding_default(void); +/** + * Returns value payload. + */ +const z_loaned_bytes_t *z_value_payload(const z_loaned_value_t *value); + +/** + * Returns total number of bytes in the payload. + */ +size_t z_bytes_len(const z_loaned_bytes_t *bytes); + +/** + * Decodes data into an owned null-terminated string. + * + * @param this_: Data to decode. + * @param dst: An unitialized memory location where to construct a decoded string. + */ +int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *s); + /** * Encodes string by aliasing. */ diff --git a/include/zenoh-pico/collections/string.h b/include/zenoh-pico/collections/string.h index bd0b599b4..2c7f95bdd 100644 --- a/include/zenoh-pico/collections/string.h +++ b/include/zenoh-pico/collections/string.h @@ -69,6 +69,7 @@ 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); @@ -80,6 +81,7 @@ 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) diff --git a/src/api/api.c b/src/api/api.c index 04e19ea18..64a517adb 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -226,6 +226,22 @@ z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix) { z_encoding_t z_encoding_default(void) { return z_encoding(Z_ENCODING_PREFIX_DEFAULT, NULL); } +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; +} + 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); diff --git a/src/collections/string.c b/src/collections/string.c index 927dc7dab..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); @@ -83,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)); @@ -105,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; } diff --git a/src/net/session.c b/src/net/session.c index 3419cabda..509cb294b 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -157,7 +157,7 @@ _z_config_t *_z_info(const _z_session_t *zn) { _z_bytes_t local_zid = _z_bytes_wrap(zn->_local_zid.id, _z_id_len(zn->_local_zid)); // TODO(sasahcmc): is it zero terminated??? // rework it!!! - _zp_config_insert(ps, Z_INFO_PID_KEY, _z_string_from_bytes(&local_zid).val); + _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/transport/multicast.c b/src/transport/multicast.c index 14d0fe4df..db76bb01a 100644 --- a/src/transport/multicast.c +++ b/src/transport/multicast.c @@ -46,7 +46,7 @@ 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)); - _z_string_t remote_zid_str = _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); diff --git a/src/transport/unicast.c b/src/transport/unicast.c index f7d066bb8..650a99615 100644 --- a/src/transport/unicast.c +++ b/src/transport/unicast.c @@ -40,7 +40,7 @@ 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_zid_bytes = _z_bytes_wrap(remote_zid.id, _z_id_len(remote_zid)); - _z_string_t remote_zid_str = _z_string_from_bytes(&remote_zid_bytes); + _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); } diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 0e96d9d26..f23d1f8ea 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -115,7 +115,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { _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) { diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 6808d8f23..898eeb508 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -79,7 +79,7 @@ int main(int argc, char **argv) { assert(z_check(s1)); _z_bytes_t 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_from_bytes(&id_as_bytes); + _z_string_t zid1 = _z_string_convert_bytes(&id_as_bytes); printf("Session 1 with PID: %s\n", zid1.val); _z_string_clear(&zid1); @@ -97,7 +97,7 @@ int main(int argc, char **argv) { z_open(&s2, z_move(config)); assert(z_check(s2)); 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_from_bytes(&id_as_bytes); + _z_string_t zid2 = _z_string_convert_bytes(&id_as_bytes); printf("Session 2 with PID: %s\n", zid2.val); _z_string_clear(&zid2); From 385261bc7470b22cdb4d5f2d22d2f40ae3040ac4 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 29 May 2024 18:03:19 +0200 Subject: [PATCH 32/43] Implement correct c++ support in macros.h --- include/zenoh-pico/api/handlers.h | 2 +- include/zenoh-pico/api/macros.h | 120 +++++++++++++++++++++--------- tests/z_channels_test.c | 1 + 3 files changed, 87 insertions(+), 36 deletions(-) diff --git a/include/zenoh-pico/api/handlers.h b/include/zenoh-pico/api/handlers.h index ae1c8cf6d..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" diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index a38403125..6f4d5cd70 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -256,29 +256,81 @@ */ #define z_closure(...) _z_closure_overloader(__VA_ARGS__, 0, 0, 0) -#else - -// TODO(sashacmc): z_loan_mut +#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 z_loaned_string_t type; }; - -template<> inline z_session_t z_loan(const z_owned_session_t& x) { return z_session_loan(&x); } -template<> inline z_keyexpr_t z_loan(const z_owned_keyexpr_t& x) { return z_keyexpr_loan(&x); } -template<> inline z_config_t z_loan(const z_owned_config_t& x) { return z_config_loan(&x); } -template<> inline z_publisher_t z_loan(const z_owned_publisher_t& x) { return z_publisher_loan(&x); } -template<> inline z_hello_t z_loan(const z_owned_hello_t& x) { return z_hello_loan(&x); } -template<> inline const char* z_loan(const z_owned_string_t& x) { return z_string_loan(&x); } -template<> inline z_loaned_string_t z_loan(const z_view_string_t& x) { return z_view_string_loan(&x); } +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 <> 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*); @@ -294,13 +346,16 @@ 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); } @@ -313,17 +368,18 @@ template<> inline void z_drop(z_owned_reply_t* v) { z_reply_drop(v); } template<> inline void z_drop(z_owned_hello_t* v) { z_hello_drop(v); } template<> inline void z_drop(z_owned_string_t* v) { z_string_drop(v); } template<> inline void z_drop(z_owned_closure_sample_t* v) { z_closure_sample_drop(v); } -template<> inline void z_drop(z_owned_closure_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); } +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); } @@ -336,9 +392,7 @@ 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_clusure_owned_sample_t* v) { z_closure_owned_sample_null(v); } inline void z_null(z_owned_closure_query_t* v) { z_closure_query_null(v); } -inline void z_null(z_owned_clusure_owned_query_t* v) { z_closure_owned_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); } @@ -346,25 +400,21 @@ 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_string_t& v) { return z_string_check(&v); } -inline bool z_check(const z_owned_string_t& v) { return z_sample_check(&v); } +inline bool z_check(const z_owned_sample_t& v) { return z_sample_check(&v); } + +// 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_owned_sample_t *sample) - { z_closure_owned_sample_call(&closure, sample); } 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_owned_query_t *query) - { z_closure_owned_query_call(&closure, query); } 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) diff --git a/tests/z_channels_test.c b/tests/z_channels_test.c index 9c151dcab..086fd7684 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 From 0b9017f6f78122a4b5f5d7d76d24d22210fbf889 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 29 May 2024 19:26:48 +0200 Subject: [PATCH 33/43] Change keyexpr processing in z_declare_subscriber --- src/api/api.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 64a517adb..fe358e755 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -994,24 +994,25 @@ int8_t z_declare_subscriber(z_owned_subscriber_t *sub, const z_loaned_session_t if (r == NULL) { 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; - // TODO(sashacmc): Why we modify it? Rework to remove cast - ((z_loaned_keyexpr_t *)keyexpr)->_suffix = suffix; - _z_keyexpr_set_owns_suffix((z_loaned_keyexpr_t *)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(&_Z_RC_IN_VAL(zs), *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); } } From a8c236dc073675dbf740ce73fd281b1aaafdf385 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Thu, 30 May 2024 19:58:36 +0200 Subject: [PATCH 34/43] Rename z_str_data to z_string_data --- examples/arduino/z_get.ino | 4 ++-- examples/arduino/z_pull.ino | 2 +- examples/arduino/z_queryable.ino | 4 ++-- examples/arduino/z_sub.ino | 2 +- examples/espidf/z_get.c | 2 +- examples/espidf/z_pull.c | 2 +- examples/espidf/z_queryable.c | 4 ++-- examples/espidf/z_sub.c | 4 ++-- examples/freertos_plus_tcp/z_get.c | 2 +- examples/freertos_plus_tcp/z_pull.c | 2 +- examples/freertos_plus_tcp/z_queryable.c | 4 ++-- examples/freertos_plus_tcp/z_sub.c | 3 ++- examples/freertos_plus_tcp/z_sub_st.c | 3 ++- examples/mbed/z_get.cpp | 3 ++- examples/mbed/z_pull.cpp | 2 +- examples/mbed/z_queryable.cpp | 4 ++-- examples/mbed/z_sub.cpp | 4 ++-- examples/unix/c11/z_get.c | 2 +- examples/unix/c11/z_get_channel.c | 2 +- examples/unix/c11/z_pull.c | 2 +- examples/unix/c11/z_queryable.c | 4 ++-- examples/unix/c11/z_queryable_channel.c | 4 ++-- examples/unix/c11/z_sub.c | 3 ++- examples/unix/c11/z_sub_channel.c | 2 +- examples/unix/c11/z_sub_st.c | 3 ++- examples/unix/c99/z_get.c | 3 ++- examples/unix/c99/z_pull.c | 3 ++- examples/unix/c99/z_queryable.c | 4 ++-- examples/unix/c99/z_sub.c | 2 +- examples/unix/c99/z_sub_st.c | 2 +- examples/windows/z_get.c | 2 +- examples/windows/z_pull.c | 2 +- examples/windows/z_queryable.c | 4 ++-- examples/windows/z_sub.c | 3 ++- examples/windows/z_sub_st.c | 3 ++- examples/zephyr/z_get.c | 2 +- examples/zephyr/z_pull.c | 2 +- examples/zephyr/z_queryable.c | 4 ++-- examples/zephyr/z_sub.c | 2 +- include/zenoh-pico/api/primitives.h | 2 +- src/api/api.c | 2 +- tests/z_test_fragment_rx.c | 2 +- 42 files changed, 63 insertions(+), 54 deletions(-) diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index 3fc12d587..2e172996f 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -52,9 +52,9 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { z_bytes_decode_into_string(z_sample_payload(sample), &replystr); Serial.print(" >> [Get listener] Received ("); - Serial.print(z_str_data(z_string_loan(&keystr))); + Serial.print(z_string_data(z_string_loan(&keystr))); Serial.print(", "); - Serial.print(z_str_data(z_string_loan(&replystr))); + Serial.print(z_string_data(z_string_loan(&replystr))); Serial.println(")"); z_string_drop(z_string_move(&keystr)); diff --git a/examples/arduino/z_pull.ino b/examples/arduino/z_pull.ino index dd8a271db..d785a1faf 100644 --- a/examples/arduino/z_pull.ino +++ b/examples/arduino/z_pull.ino @@ -44,7 +44,7 @@ // std::string val((const char *)sample->payload.start, sample->payload.len); // Serial.print(" >> [Subscription listener] Received ("); -// Serial.print(z_str_data(z_string_loan(&keystr))); +// Serial.print(z_string_data(z_string_loan(&keystr))); // Serial.print(", "); // Serial.print(val.c_str()); // Serial.println(")"); diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index b7f652b7f..d4751dab5 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -39,7 +39,7 @@ 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_str_data(z_string_loan(&keystr))); + Serial.print(z_string_data(z_string_loan(&keystr))); Serial.println("'"); // Process value @@ -48,7 +48,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { z_owned_string_t payload_string; z_bytes_decode_into_string(payload, &payload_string); Serial.print(" with value '"); - Serial.print(z_str_data(z_string_loan(&payload_string))); + Serial.print(z_string_data(z_string_loan(&payload_string))); Serial.println("'"); z_string_drop(z_string_move(&payload_string)); } diff --git a/examples/arduino/z_sub.ino b/examples/arduino/z_sub.ino index 5336932e1..990ab7580 100644 --- a/examples/arduino/z_sub.ino +++ b/examples/arduino/z_sub.ino @@ -40,7 +40,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { std::string val((const char *)z_sample_payload(sample)->start, z_sample_payload(sample)->len); Serial.print(" >> [Subscription listener] Received ("); - Serial.print(z_str_data(z_string_loan(&keystr))); + Serial.print(z_string_data(z_string_loan(&keystr))); Serial.print(", "); Serial.print(val.c_str()); Serial.println(")"); diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index 889e4e1a5..eb06f20f1 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -111,7 +111,7 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { z_owned_string_t replystr; z_bytes_decode_into_string(z_sample_payload(sample), &replystr); - printf(" >> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(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 { diff --git a/examples/espidf/z_pull.c b/examples/espidf/z_pull.c index 8efd04a29..9647f4ef2 100644 --- a/examples/espidf/z_pull.c +++ b/examples/espidf/z_pull.c @@ -104,7 +104,7 @@ void wifi_init_sta(void) { // 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_str_data(z_loan(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)); diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index dbf232594..48b09ff8b 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -107,14 +107,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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_str_data(z_loan(keystr)), (int)z_loan(params)->len, + 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_str_data(z_loan(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; diff --git a/examples/espidf/z_sub.c b/examples/espidf/z_sub.c index 45e1b8ed9..d496734e6 100644 --- a/examples/espidf/z_sub.c +++ b/examples/espidf/z_sub.c @@ -104,8 +104,8 @@ 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_str_data(z_string_loan(&keystr)), (int)payload->len, - payload->start); + 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)); } diff --git a/examples/freertos_plus_tcp/z_get.c b/examples/freertos_plus_tcp/z_get.c index 3c7ddbb3b..1d53dab41 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -45,7 +45,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { z_owned_string_t replystr; z_bytes_decode_into_string(z_sample_payload(sample), &replystr); - printf(">> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(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 { diff --git a/examples/freertos_plus_tcp/z_pull.c b/examples/freertos_plus_tcp/z_pull.c index 6f1273b70..8d2ef7f8f 100644 --- a/examples/freertos_plus_tcp/z_pull.c +++ b/examples/freertos_plus_tcp/z_pull.c @@ -33,7 +33,7 @@ // (void)(ctx); // z_owned_string_t keystr; // z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); -// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)sample->payload.len, +// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)sample->payload.len, // sample->payload.start); // z_drop(z_move(keystr)); // } diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index be8e1672f..20d72f7eb 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -35,14 +35,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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_str_data(z_loan(keystr)), (int)z_loan(params)->len, + 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_str_data(z_loan(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; diff --git a/examples/freertos_plus_tcp/z_sub.c b/examples/freertos_plus_tcp/z_sub.c index dbf7e79f9..d0afc19ad 100644 --- a/examples/freertos_plus_tcp/z_sub.c +++ b/examples/freertos_plus_tcp/z_sub.c @@ -33,7 +33,8 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { 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_str_data(z_loan(keystr)), (int)payload->len, payload->start); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)payload->len, + payload->start); z_drop(z_move(keystr)); } diff --git a/examples/freertos_plus_tcp/z_sub_st.c b/examples/freertos_plus_tcp/z_sub_st.c index 151e078a6..aa24456ee 100644 --- a/examples/freertos_plus_tcp/z_sub_st.c +++ b/examples/freertos_plus_tcp/z_sub_st.c @@ -36,7 +36,8 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { 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_str_data(z_loan(keystr)), (int)payload->len, payload->start); + 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++; diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index d5f89b09d..699186612 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -41,7 +41,8 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { z_owned_string_t replystr; z_bytes_decode_into_string(z_sample_payload(sample), &replystr); - printf(" >> Received ('%s': '%s')\n", z_str_data(z_string_loan(&keystr)), z_str_data(z_string_loan(&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 { diff --git a/examples/mbed/z_pull.cpp b/examples/mbed/z_pull.cpp index d01f2978c..52a4f9be0 100644 --- a/examples/mbed/z_pull.cpp +++ b/examples/mbed/z_pull.cpp @@ -34,7 +34,7 @@ // 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_str_data(z_string_loan(&keystr)), +// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)), // (int)sample->payload.len, // sample->payload.start); // z_string_drop(z_string_move(&keystr)); diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index 64f68e9fa..5c09bf79f 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -37,14 +37,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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_str_data(z_string_loan(&keystr)), + 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_str_data(z_string_loan(&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 diff --git a/examples/mbed/z_sub.cpp b/examples/mbed/z_sub.cpp index 537630d6a..20ea106d4 100644 --- a/examples/mbed/z_sub.cpp +++ b/examples/mbed/z_sub.cpp @@ -34,8 +34,8 @@ 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_str_data(z_string_loan(&keystr)), (int)payload->len, - payload->start); + 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)); } diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index b650556ed..f7677e3b3 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -46,7 +46,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { z_owned_string_t replystr; z_bytes_decode_into_string(z_sample_payload(sample), &replystr); - printf(">> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(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"); diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index daefcd25c..4f74b1848 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -115,7 +115,7 @@ int main(int argc, char **argv) { z_owned_string_t replystr; z_bytes_decode_into_string(z_sample_payload(sample), &replystr); - printf(">> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(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 { diff --git a/examples/unix/c11/z_pull.c b/examples/unix/c11/z_pull.c index 5b396c265..6e7506663 100644 --- a/examples/unix/c11/z_pull.c +++ b/examples/unix/c11/z_pull.c @@ -90,7 +90,7 @@ int main(int argc, char **argv) { for (z_call(channel.try_recv, &sample); z_check(sample); z_call(channel.try_recv, &sample)) { z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr); - printf(">> [Subscriber] Pulled ('%s': '%.*s')\n", z_str_data(z_loan(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)); diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index b480c3636..3fd4b415a 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -37,14 +37,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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_str_data(z_loan(keystr)), (int)z_loan(params)->len, + 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_str_data(z_loan(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 diff --git a/examples/unix/c11/z_queryable_channel.c b/examples/unix/c11/z_queryable_channel.c index 562e6cd66..9038790b2 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -104,13 +104,13 @@ int main(int argc, char **argv) { 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_str_data(z_loan(keystr)), + 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_str_data(z_loan(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; diff --git a/examples/unix/c11/z_sub.c b/examples/unix/c11/z_sub.c index df6bf9e07..a91133fa6 100644 --- a/examples/unix/c11/z_sub.c +++ b/examples/unix/c11/z_sub.c @@ -37,7 +37,8 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { 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_str_data(z_loan(keystr)), (int)payload->len, payload->start); + 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)) { diff --git a/examples/unix/c11/z_sub_channel.c b/examples/unix/c11/z_sub_channel.c index 1067a475f..02a793961 100644 --- a/examples/unix/c11/z_sub_channel.c +++ b/examples/unix/c11/z_sub_channel.c @@ -80,7 +80,7 @@ int main(int argc, char **argv) { for (z_call(channel.recv, &sample); z_check(sample); z_call(channel.recv, &sample)) { z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_loan(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)); diff --git a/examples/unix/c11/z_sub_st.c b/examples/unix/c11/z_sub_st.c index 852011459..97bcacfc0 100644 --- a/examples/unix/c11/z_sub_st.c +++ b/examples/unix/c11/z_sub_st.c @@ -28,7 +28,8 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { 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_str_data(z_loan(keystr)), (int)payload->len, payload->start); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)payload->len, + payload->start); z_drop(z_move(keystr)); msg_nb++; } diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 78a9a6ad4..12c5bc3f6 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -38,7 +38,8 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { z_owned_string_t replystr; z_bytes_decode_into_string(z_sample_payload(sample), &replystr); - printf(">> Received ('%s': '%s')\n", z_str_data(z_string_loan(&keystr)), z_str_data(z_string_loan(&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 { diff --git a/examples/unix/c99/z_pull.c b/examples/unix/c99/z_pull.c index 0760264de..460389878 100644 --- a/examples/unix/c99/z_pull.c +++ b/examples/unix/c99/z_pull.c @@ -24,7 +24,8 @@ // (void)(ctx); // z_owned_string_t keystr; // z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); -// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)sample->payload.len, +// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)), +// (int)sample->payload.len, // sample->payload.start); // z_string_drop(z_string_move(&keystr)); // } diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index de03d1c49..f4b7b4156 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -28,14 +28,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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_str_data(z_string_loan(&keystr)), + 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_str_data(z_string_loan(&payload_string))); + printf(" with value '%s'\n", z_string_data(z_string_loan(&payload_string))); z_string_drop(z_string_move(&payload_string)); } z_query_reply_options_t options; diff --git a/examples/unix/c99/z_sub.c b/examples/unix/c99/z_sub.c index fa05fc001..3495d167a 100644 --- a/examples/unix/c99/z_sub.c +++ b/examples/unix/c99/z_sub.c @@ -25,7 +25,7 @@ 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] Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len, + 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)); } diff --git a/examples/unix/c99/z_sub_st.c b/examples/unix/c99/z_sub_st.c index f97540233..c83d059ba 100644 --- a/examples/unix/c99/z_sub_st.c +++ b/examples/unix/c99/z_sub_st.c @@ -28,7 +28,7 @@ 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] Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len, + 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++; diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index 8e0192618..9292ebdb1 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -37,7 +37,7 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) { z_owned_string_t replystr; z_bytes_decode_into_string(z_sample_payload(sample), &replystr); - printf(">> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(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 { diff --git a/examples/windows/z_pull.c b/examples/windows/z_pull.c index 5dd66e558..9256cf4d9 100644 --- a/examples/windows/z_pull.c +++ b/examples/windows/z_pull.c @@ -23,7 +23,7 @@ // (void)(ctx); // z_owned_string_t keystr; // z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); -// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)sample->payload.len, +// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)sample->payload.len, // sample->payload.start); // z_drop(z_move(keystr)); // } diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index d5c06db56..6ccae4b3a 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -28,14 +28,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_view_string_t params; z_query_parameters(query, ¶ms); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len, + 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_str_data(z_loan(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; diff --git a/examples/windows/z_sub.c b/examples/windows/z_sub.c index e908f2534..5ca826633 100644 --- a/examples/windows/z_sub.c +++ b/examples/windows/z_sub.c @@ -24,7 +24,8 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { 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_str_data(z_loan(keystr)), (int)payload->len, payload->start); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)payload->len, + payload->start); z_drop(z_move(keystr)); } diff --git a/examples/windows/z_sub_st.c b/examples/windows/z_sub_st.c index fed96e89c..5aed70bdc 100644 --- a/examples/windows/z_sub_st.c +++ b/examples/windows/z_sub_st.c @@ -27,7 +27,8 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { 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_str_data(z_loan(keystr)), (int)payload->len, payload->start); + printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)payload->len, + payload->start); z_drop(z_move(keystr)); msg_nb++; diff --git a/examples/zephyr/z_get.c b/examples/zephyr/z_get.c index f4213b96c..2635eb1ef 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -42,7 +42,7 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) { z_owned_string_t replystr; z_bytes_decode_into_string(z_sample_payload(sample), &replystr); - printf(" >> Received ('%s': '%s')\n", z_str_data(z_loan(keystr)), z_str_data(z_loan(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 { diff --git a/examples/zephyr/z_pull.c b/examples/zephyr/z_pull.c index 374c6e8d3..007b20ba5 100644 --- a/examples/zephyr/z_pull.c +++ b/examples/zephyr/z_pull.c @@ -33,7 +33,7 @@ // 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_str_data(z_loan(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)); diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index 3cbb4c3bb..61e8710f8 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -37,14 +37,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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_str_data(z_loan(keystr)), z_loan(params)->len, + 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_str_data(z_loan(payload_string))); + printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); z_drop(z_move(payload_string)); } // Reply value encoding diff --git a/examples/zephyr/z_sub.c b/examples/zephyr/z_sub.c index 81b97ad01..bda16ff13 100644 --- a/examples/zephyr/z_sub.c +++ b/examples/zephyr/z_sub.c @@ -33,7 +33,7 @@ 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_str_data(z_loan(keystr)), (int)payload->len, + printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)payload->len, payload->start); z_drop(z_move(keystr)); } diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 6a782b3a4..0999b306b 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -824,7 +824,7 @@ _VIEW_FUNCTIONS(z_loaned_string_t, z_view_string_t, string) // TODO(sashacmc): comments, docs, etc. const z_loaned_sample_t *z_sample_loan(const z_owned_sample_t *sample); -const char *z_str_data(const z_loaned_string_t *str); +const char *z_string_data(const z_loaned_string_t *str); /************* Primitives **************/ /** diff --git a/src/api/api.c b/src/api/api.c index fe358e755..1d7aa356c 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -593,7 +593,7 @@ z_qos_t z_sample_qos(const z_loaned_sample_t *sample) { return _Z_RC_IN_VAL(samp z_attachment_t z_sample_attachment(const z_loaned_sample_t *sample) { return _Z_RC_IN_VAL(sample).attachment; } #endif -const char *z_str_data(const z_loaned_string_t *str) { return str->val; } +const char *z_string_data(const z_loaned_string_t *str) { return str->val; } #if Z_FEATURE_PUBLICATION == 1 int8_t _z_publisher_drop(_z_publisher_t **pub) { diff --git a/tests/z_test_fragment_rx.c b/tests/z_test_fragment_rx.c index 55bd8bce2..8f1c8b95f 100644 --- a/tests/z_test_fragment_rx.c +++ b/tests/z_test_fragment_rx.c @@ -31,7 +31,7 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { break; } } - printf("[rx]: Received packet on %s, len: %d, validity: %d\n", z_str_data(z_loan(keystr)), + 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)); } From 506a66804901e7e6b3f6b0e51ef6eef02a1250cf Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Tue, 4 Jun 2024 14:11:15 +0200 Subject: [PATCH 35/43] z_encoding API rework (#7) * refactor: reworking encoding prefix enum * feat: create net/encoding module * fix: missing include in logging module * fix: update files with encoding module * feat: rework z_encoding api * fix: remove encoding from freertos examples * feat: add encoding_loan_check function * feat: update tests & examples with new encoding * fix: bad size schema allocation * feat: add _z_encoding clear and check functions * feat: add _z_encoding copy and move functions * fix: use _z_encoding_check for errors * fix: set encoding id value * fix: missing end of file newline * fix: deactivate faulty workflow * doc: improve encoding documentation * fix: missing new encoding functions * fix: pr corrections * fix: update examples * fix: remove z_encoding_loan_check * feat: rework _z_encoding make/wrap functions * fix: update alignment test * fix: missing return statement * fix: create encoding in loop as it's moved * feat: add z_encoding_move function * fix: compilation/test issue * fix: missing z_move to alignment test * fix: remove default encoding struct --- .github/workflows/build-check.yaml | 35 ++---- docs/api.rst | 4 +- examples/freertos_plus_tcp/z_pub.c | 5 +- examples/freertos_plus_tcp/z_put.c | 1 - examples/unix/c11/z_pub.c | 19 +-- examples/unix/c11/z_put.c | 6 +- examples/unix/c11/z_queryable.c | 6 +- examples/unix/c99/z_pub.c | 8 +- examples/unix/c99/z_put.c | 6 +- examples/unix/c99/z_queryable.c | 6 +- examples/windows/z_pub.c | 15 +-- examples/windows/z_put.c | 5 +- examples/windows/z_queryable.c | 5 + include/zenoh-pico/api/constants.h | 116 +++++++++++------- include/zenoh-pico/api/macros.h | 11 +- include/zenoh-pico/api/primitives.h | 68 +++++++--- include/zenoh-pico/api/types.h | 27 ++-- include/zenoh-pico/net/encoding.h | 36 ++++++ include/zenoh-pico/net/primitives.h | 1 + include/zenoh-pico/net/reply.h | 1 + include/zenoh-pico/net/sample.h | 1 + include/zenoh-pico/protocol/codec/core.h | 1 + include/zenoh-pico/protocol/core.h | 9 +- .../zenoh-pico/protocol/definitions/message.h | 1 + include/zenoh-pico/session/subscription.h | 1 + include/zenoh-pico/utils/logging.h | 2 + src/api/api.c | 78 +++++++++--- src/net/encoding.c | 59 +++++++++ src/net/memory.c | 2 +- src/net/sample.c | 16 +-- src/protocol/codec/message.c | 5 +- src/protocol/core.c | 5 +- src/protocol/definitions/message.c | 7 +- src/session/push.c | 2 +- src/session/rx.c | 2 +- src/session/subscription.c | 2 +- tests/z_api_alignment_test.c | 7 +- tests/z_channels_test.c | 2 +- 38 files changed, 399 insertions(+), 184 deletions(-) create mode 100644 include/zenoh-pico/net/encoding.h create mode 100644 src/net/encoding.c diff --git a/.github/workflows/build-check.yaml b/.github/workflows/build-check.yaml index b81b92831..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 @@ -115,29 +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 - uses: nick-fields/retry@v3 - with: - timeout_minutes: 5 - max_attempts: 3 - retry_on: error - command: | - sudo apt install -y ninja-build - CMAKE_GENERATOR=Ninja make - python3 ./build/tests/raweth.py --reth $Z_FEATURE_RAWETH_TRANSPORT - 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/docs/api.rst b/docs/api.rst index 08aaa7a95..72f3a3e24 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 @@ -192,7 +192,7 @@ 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_default .. autocfunction:: primitives.h::z_query_target_default .. autocfunction:: primitives.h::z_query_consolidation_auto .. autocfunction:: primitives.h::z_query_consolidation_default diff --git a/examples/freertos_plus_tcp/z_pub.c b/examples/freertos_plus_tcp/z_pub.c index ab13b8796..f19ddc23e 100644 --- a/examples/freertos_plus_tcp/z_pub.c +++ b/examples/freertos_plus_tcp/z_pub.c @@ -99,16 +99,13 @@ void app_main(void) { z_publisher_put_options_t options; z_publisher_put_options_default(&options); - options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); 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_mut(s)); zp_stop_lease_task(z_loan_mut(s)); - z_close(z_move(s)); } #else diff --git a/examples/freertos_plus_tcp/z_put.c b/examples/freertos_plus_tcp/z_put.c index b057bb88f..092ed503b 100644 --- a/examples/freertos_plus_tcp/z_put.c +++ b/examples/freertos_plus_tcp/z_put.c @@ -66,7 +66,6 @@ void app_main(void) { printf("Putting Data ('%s': '%s')...\n", KEYEXPR, VALUE); z_put_options_t options; z_put_options_default(&options); - options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)VALUE, strlen(VALUE), &options) < 0) { printf("Oh no! Put has failed...\n"); } diff --git a/examples/unix/c11/z_pub.c b/examples/unix/c11/z_pub.c index 8c78dc328..576edf915 100644 --- a/examples/unix/c11/z_pub.c +++ b/examples/unix/c11/z_pub.c @@ -86,7 +86,7 @@ int main(int argc, char **argv) { // Start read and lease tasks for zenoh-pico 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 @@ -94,6 +94,7 @@ 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_view_keyexpr_t ke; @@ -102,27 +103,29 @@ int main(int argc, char **argv) { 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); + // 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_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + 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_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_put.c b/examples/unix/c11/z_put.c index 2f13733b3..d6ac6cc87 100644 --- a/examples/unix/c11/z_put.c +++ b/examples/unix/c11/z_put.c @@ -97,10 +97,14 @@ int main(int argc, char **argv) { 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); - options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + 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")); diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index 3fd4b415a..17263e5d8 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -55,9 +55,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { } #endif + // 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(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + options.encoding = z_move(encoding); #if Z_FEATURE_ATTACHMENT == 1 // Add attachment diff --git a/examples/unix/c99/z_pub.c b/examples/unix/c99/z_pub.c index 6714a6e06..91e5799c2 100644 --- a/examples/unix/c99/z_pub.c +++ b/examples/unix/c99/z_pub.c @@ -98,13 +98,17 @@ 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); + // 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(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + options.encoding = z_encoding_move(&encoding); + z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), &options); } // Clean up diff --git a/examples/unix/c99/z_put.c b/examples/unix/c99/z_put.c index eb9e6ebf3..701432fa6 100644 --- a/examples/unix/c99/z_put.c +++ b/examples/unix/c99/z_put.c @@ -93,10 +93,14 @@ int main(int argc, char **argv) { 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); - options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + 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"); } diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index f4b7b4156..08af246ab 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -38,9 +38,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + options.encoding = z_encoding_move(&encoding); // Reply value encoding z_view_string_t reply_str; z_view_str_wrap(&reply_str, value); diff --git a/examples/windows/z_pub.c b/examples/windows/z_pub.c index 5ef1da47a..231edeb66 100644 --- a/examples/windows/z_pub.c +++ b/examples/windows/z_pub.c @@ -44,7 +44,7 @@ int main(int argc, char **argv) { // Start read and lease tasks for zenoh-pico 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; } @@ -60,24 +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); + // 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(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + 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_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_put.c b/examples/windows/z_put.c index d009968a3..e3c9b5982 100644 --- a/examples/windows/z_put.c +++ b/examples/windows/z_put.c @@ -59,11 +59,14 @@ int main(int argc, char **argv) { 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); - options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); + 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"); } diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index 6ccae4b3a..91c9c8117 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -38,8 +38,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); z_drop(z_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_move(encoding); // Reply value encoding z_view_string_t reply_str; z_view_str_wrap(&reply_str, value); 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/macros.h b/include/zenoh-pico/api/macros.h index 6f4d5cd70..58287f5fa 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -47,7 +47,8 @@ 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_bytes_t : z_bytes_loan \ + z_owned_bytes_t : z_bytes_loan, \ + z_owned_encoding_t : z_encoding_loan \ )(&x) #define z_loan_mut(x) _Generic((x), \ @@ -85,6 +86,7 @@ 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, \ @@ -125,7 +127,8 @@ 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) /** @@ -176,6 +179,7 @@ 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, \ @@ -231,7 +235,8 @@ 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 diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 0999b306b..95cd1084b 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -394,24 +394,59 @@ const char *zp_scouting_config_get(const z_loaned_scouting_config_t *config, uin int8_t zp_scouting_config_insert(z_loaned_scouting_config_t *config, uint8_t key, const char *value); /** - * Constructs a :c:type:`z_encoding_t`. + * Constructs a :c:type:`z_owned_encoding_t`. * * Parameters: - * prefix: A known :c:type:`z_encoding_prefix_t`. - * suffix: A custom suffix to be appended to the prefix. + * encoding: a reference to an uninitialized :c:type:`z_owned_encoding_t` + * id: A known :c:type:`z_encoding_id_t`. + * schema: A custom schema string value. * * Returns: - * Returns the constructed :c:type:`z_encoding_t`. + * Returns ``0`` if construction is successful, or a ``negative value`` otherwise. */ -z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix); +int8_t zp_encoding_make(z_owned_encoding_t *encoding, z_encoding_id_t id, const char *schema); /** - * Constructs a default encoding. + * Constructs a :c:type:`z_owned_encoding_t` with default value. + * + * Parameters: + * encoding: a reference to an uninitialized :c:type:`z_owned_encoding_t` + * + * Returns: + * Returns ``0`` if construction is successful, or a ``negative value`` otherwise. + */ +int8_t zp_encoding_default(z_owned_encoding_t *encoding); + +/** + * Returns ``true`` if encoding is in non-default state, ``false`` otherwise. + */ +_Bool z_encoding_check(const z_owned_encoding_t *encoding); + +/** + * Frees the memory of a :c:type:`z_owned_encoding_t`. + */ +void z_encoding_drop(z_owned_encoding_t *encoding); + +/** + * Returns a loaned :c:type:`z_loaned_encoding_t`. + */ +const z_loaned_encoding_t *z_encoding_loan(const z_owned_encoding_t *encoding); + +/** + * Moves a owned :c:type:`z_owned_encoding_t`. + */ +z_owned_encoding_t *z_encoding_move(z_owned_encoding_t *encoding); + +/** + * Constructs a :c:type:`z_owned_encoding_t` with default value. + * + * Parameters: + * encoding: a reference to an uninitialized :c:type:`z_owned_encoding_t` * * Returns: - * Returns the constructed :c:type:`z_encoding_t`. + * Returns ``0`` if construction is successful, or a ``negative value`` otherwise. */ -z_encoding_t z_encoding_default(void); +int8_t z_encoding_null(z_owned_encoding_t *encoding); /** * Returns value payload. @@ -424,15 +459,20 @@ const z_loaned_bytes_t *z_value_payload(const z_loaned_value_t *value); size_t z_bytes_len(const z_loaned_bytes_t *bytes); /** - * Decodes data into an owned null-terminated string. + * Decodes data into a :c:type:`z_owned_string_t` * - * @param this_: Data to decode. - * @param dst: An unitialized memory location where to construct a decoded string. + * Parameters: + * bytes: Data to decode. + * s: An uninitialized memory location where to construct a decoded string. */ int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *s); /** - * Encodes string by aliasing. + * Encodes string into a :c:type:`z_owned_bytes_t` + * + * Parameters: + * buffer: An uninitialized memory location where to encode the string. + * s: The string to encode. */ int8_t z_bytes_encode_from_string(z_owned_bytes_t *buffer, const z_loaned_string_t *s); @@ -963,9 +1003,9 @@ z_timestamp_t z_sample_timestamp(const z_loaned_sample_t *sample); * sample: Pointer to the sample to get the encoding from. * * Returns: - * Returns the encoding wrapped as a :c:type:`z_encoding_t`. + * Returns the encoding wrapped as a :c:type:`z_loaned_encoding_t*`. */ -z_encoding_t z_sample_encoding(const z_loaned_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. diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index c77c0305e..beefb4eba 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -19,6 +19,7 @@ #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" @@ -189,11 +190,11 @@ _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_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. */ -// TODO(sashacmc): z_encoding_t -typedef _z_encoding_t z_encoding_t; +_OWNED_TYPE_PTR(_z_encoding_t, encoding) +_LOANED_TYPE(_z_encoding_t, encoding) /* * Represents timestamp value in Zenoh @@ -204,7 +205,7 @@ typedef _z_timestamp_t z_timestamp_t; * Represents a Zenoh value. * * Members: - * z_encoding_t encoding: The encoding of the `payload`. + * z_loaned_encoding_t encoding: The encoding of the `payload`. * z_loaned_bytes_t* payload: The payload of this zenoh value. */ @@ -262,11 +263,11 @@ typedef struct { * 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; @@ -275,12 +276,12 @@ typedef struct { * whenever issued 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 @@ -306,10 +307,10 @@ typedef struct { * whenever issued 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 @@ -331,11 +332,11 @@ typedef struct { * 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_owned_bytes_t payload: The payload to include in the query. - * z_encoding_t encoding: Payload encoding. + * z_owned_encoding_t *encoding: Payload encoding. */ typedef struct { z_owned_bytes_t *payload; - z_encoding_t encoding; + z_owned_encoding_t *encoding; z_query_consolidation_t consolidation; z_query_target_t target; uint32_t timeout_ms; @@ -426,7 +427,7 @@ static inline z_qos_t z_qos_default(void) { return _Z_N_QOS_DEFAULT; } * Members: * z_keyexpr_t keyexpr: The keyexpr of this data sample. * z_loaned_bytes_t* payload: The value of this data sample. - * z_encoding_t encoding: The encoding of 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. 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 9812db2a7..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" diff --git a/include/zenoh-pico/net/reply.h b/include/zenoh-pico/net/reply.h index a01ecf9ad..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" 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 075111156..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" diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index fd25c4cb5..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. */ 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/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/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 1d7aa356c..9cce5ef36 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -219,12 +219,44 @@ int8_t zp_scouting_config_insert(z_loaned_scouting_config_t *sc, uint8_t key, co 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_owned_encoding_t *z_encoding_move(z_owned_encoding_t *encoding) { return encoding; } + +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); } -z_encoding_t z_encoding_default(void) { return z_encoding(Z_ENCODING_PREFIX_DEFAULT, NULL); } +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; } @@ -587,7 +619,7 @@ const z_loaned_keyexpr_t *z_sample_keyexpr(const z_loaned_sample_t *sample) { re 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; } -z_encoding_t z_sample_encoding(const z_loaned_sample_t *sample) { return _Z_RC_IN_VAL(sample).encoding; } +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_loaned_sample_t *sample) { return _Z_RC_IN_VAL(sample).attachment; } @@ -610,7 +642,7 @@ OWNED_FUNCTIONS_PTR(_z_publisher_t, publisher, _z_owner_noop_copy, _z_publisher_ void z_put_options_default(z_put_options_t *options) { options->congestion_control = Z_CONGESTION_CONTROL_DEFAULT; options->priority = Z_PRIORITY_DEFAULT; - options->encoding = z_encoding_default(); + options->encoding = NULL; #if Z_FEATURE_ATTACHMENT == 1 options->attachment = z_attachment_null(); #endif @@ -635,8 +667,8 @@ int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co opt.attachment = options->attachment; #endif } - ret = _z_write(&_Z_RC_IN_VAL(zs), *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 @@ -654,7 +686,8 @@ int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co att ); - + // Clean-up + z_encoding_drop(opt.encoding); return ret; } @@ -667,7 +700,7 @@ int8_t z_delete(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, opt.congestion_control = options->congestion_control; opt.priority = options->priority; } - ret = _z_write(&_Z_RC_IN_VAL(zs), *keyexpr, NULL, 0, z_encoding_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 , @@ -728,7 +761,7 @@ int8_t z_declare_publisher(z_owned_publisher_t *pub, const z_loaned_session_t *z int8_t z_undeclare_publisher(z_owned_publisher_t *pub) { return _z_publisher_drop(&pub->_val); } void z_publisher_put_options_default(z_publisher_put_options_t *options) { - options->encoding = z_encoding_default(); + options->encoding = NULL; #if Z_FEATURE_ATTACHMENT == 1 options->attachment = z_attachment_null(); #endif @@ -751,8 +784,8 @@ int8_t z_publisher_put(const z_loaned_publisher_t *pub, const uint8_t *payload, // Check if write filter is active before writing if (!_z_write_filter_active(pub)) { // Write value - ret = _z_write(&pub->_zn.in->val, pub->_key, payload, len, opt.encoding, Z_SAMPLE_KIND_PUT, - pub->_congestion_control, pub->_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 @@ -767,12 +800,15 @@ int8_t z_publisher_put(const z_loaned_publisher_t *pub, const uint8_t *payload, #endif // Trigger local subscriptions _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_loaned_publisher_t *pub, const z_publisher_delete_options_t *options) { (void)(options); - return _z_write(&pub->_zn.in->val, pub->_key, NULL, 0, z_encoding_default(), Z_SAMPLE_KIND_DELETE, + 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 , @@ -796,7 +832,7 @@ OWNED_FUNCTIONS_RC(reply) void z_get_options_default(z_get_options_t *options) { options->target = z_query_target_default(); options->consolidation = z_query_consolidation_default(); - options->encoding = z_encoding_default(); + options->encoding = NULL; options->payload = NULL; #if Z_FEATURE_ATTACHMENT == 1 options->attachment = z_attachment_null(); @@ -832,7 +868,8 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co } } // Set value - _z_value_t value = {.payload = _z_bytes_from_owned_bytes(opt.payload), .encoding = opt.encoding}; + _z_value_t value = {.payload = _z_bytes_from_owned_bytes(opt.payload), + .encoding = _z_encoding_from_owned(opt.encoding)}; ret = _z_query(&_Z_RC_IN_VAL(zs), *keyexpr, parameters, opt.target, opt.consolidation.mode, value, callback->call, callback->drop, ctx, opt.timeout_ms @@ -844,6 +881,8 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co if (opt.payload != NULL) { z_bytes_drop(opt.payload); } + // Clean-up + z_encoding_drop(opt.encoding); return ret; } @@ -908,7 +947,7 @@ int8_t z_declare_queryable(z_owned_queryable_t *queryable, const z_loaned_sessio 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 = z_encoding_default(); } +void z_query_reply_options_default(z_query_reply_options_t *options) { options->encoding = NULL; } int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *keyexpr, z_owned_bytes_t *payload, const z_query_reply_options_t *options) { @@ -919,12 +958,15 @@ int8_t z_query_reply(const z_loaned_query_t *query, const z_loaned_keyexpr_t *ke opts = *options; } // Set value - _z_value_t value = {.payload = _z_bytes_from_owned_bytes(payload), .encoding = opts.encoding}; + _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 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 ad2201538..1d60c338f 100644 --- a/src/net/memory.c +++ b/src/net/memory.c @@ -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/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/protocol/codec/message.c b/src/protocol/codec/message.c index 90f9b3e68..8c4c27cc7 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -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; } @@ -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/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/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/subscription.c b/src/session/subscription.c index c76bb6b87..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; diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index d84cae70b..30e9ad42e 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -320,10 +320,9 @@ int main(int argc, char **argv) { 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"); diff --git a/tests/z_channels_test.c b/tests/z_channels_test.c index 086fd7684..7ca5d3358 100644 --- a/tests/z_channels_test.c +++ b/tests/z_channels_test.c @@ -28,7 +28,7 @@ _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_loaned_sample_t sample = _z_sample_rc_new_from_val(s); \ From 3d0595b119351727deb0876c5cd0bca35df374b0 Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Thu, 6 Jun 2024 16:42:45 +0200 Subject: [PATCH 36/43] Update doc comments for API functions (#8) * doc: update doc comment for api functions * feat: rework z_sub_keyexpr * doc: update comments * doc: update type doc * fix: revert doc changes --- include/zenoh-pico/api/primitives.h | 1188 ++++++++++++--------------- include/zenoh-pico/api/types.h | 119 ++- src/api/api.c | 36 +- 3 files changed, 577 insertions(+), 766 deletions(-) diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 95cd1084b..eb2a62e84 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -34,70 +34,73 @@ extern "C" { /********* Data Types Handlers *********/ #define z_bytes_wrap _z_bytes_wrap -// TODO(sashacmc): docs /** - * 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. */ int8_t z_view_str_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. */ 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. */ 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_string_t` containing key expression string representation if it's possible + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. */ -int8_t z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *s); +int8_t z_keyexpr_to_string(const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *str); /** - * Constructs a null-terminated string departing from a :c:type:`z_keyexpr_t` for a given :c:type:`z_loaned_session_t`. - * The user is responsible of dropping the returned string using ``z_free``. + * Builds a null-terminated string from a :c:type:`z_loaned_keyexpr_t` for a given + * :c:type:`z_loaned_session_t`. * * Parameters: - * zs: A loaned instance of the the :c:type:`z_loaned_session_t` to resolve the keyexpr. - * keyexpr: A loaned instance of :c:type:`z_keyexpr_t` to be resolved. + * 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 string representation of a keyexpr for a given session. + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. */ int8_t zp_keyexpr_resolve(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_string_t *str); @@ -105,82 +108,82 @@ int8_t zp_keyexpr_resolve(const z_loaned_session_t *zs, const z_loaned_keyexpr_t * 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_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 ``true`` if ``l`` includes ``r``, i.e. the set defined by ``l`` contains every key belonging to the set + * 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``. */ _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. @@ -188,27 +191,27 @@ _Bool z_keyexpr_includes(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t * * r: Pointer to the keyexpr in its string representation as a null terminated string. * rlen: Number of characters in ``r``. * - * Returns: - * Returns ``true`` if ``l`` includes ``r``, i.e. the set defined by ``l`` contains every key belonging to the set + * 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``. */ _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 ``true`` if the keyexprs intersect, i.e. there exists at least one key which is contained in both of the + * 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``. */ _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. @@ -216,26 +219,26 @@ _Bool z_keyexpr_intersects(const z_loaned_keyexpr_t *l, const z_loaned_keyexpr_t * r: Pointer to the keyexpr in its string representation as a null terminated string. * rlen: Number of characters in ``r``. * - * Returns: - * Returns ``true`` if the keyexprs intersect, i.e. there exists at least one key which is contained in both of the + * 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``. */ _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 ``true`` if both ``l`` and ``r`` are equal. Otherwise, returns ``false``. + * Return: + * ``true`` if both ``l`` and ``r`` are equal. Otherwise, returns ``false``. */ _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. @@ -243,54 +246,26 @@ _Bool z_keyexpr_equals(const z_loaned_keyexpr_t *l, const z_loaned_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 ``true`` if both ``l`` and ``r`` are equal. Otherwise, it returns ``false``. + * Return: + * ``true`` if both ``l`` and ``r`` are equal. Otherwise, it returns ``false``. */ _Bool zp_keyexpr_equals_null_terminated(const char *l, const char *r); -// TODO(sashacmc): update comment /** - * 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)``. - * - * 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_config_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. - * - * Returns: - * Returns a new, zenoh-allocated, empty configuration. + * Parameters: + * config: Pointer to uninitialized :c:type:`z_owned_config_t`. */ void z_config_new(z_owned_config_t *config); -// TODO(sashacmc): update comment /** - * Return a new, zenoh-allocated, default configuration. + * Builds a new, zenoh-allocated, default configuration. * It consists in a default 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)``. - * - * 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_config_check(&val)`` or ``z_check(val)`` if your compiler - * supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. - * - * Returns: - * Returns a new, zenoh-allocated, default configuration. + * Parameters: + * config: Pointer to uninitialized :c:type:`z_owned_config_t`. */ void z_config_default(z_owned_config_t *config); @@ -298,11 +273,11 @@ void z_config_default(z_owned_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`. - * key: Integer key for the requested property. + * config: Pointer to a :c:type:`z_loaned_config_t` to get the property from. + * key: Integer key of 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(const z_loaned_config_t *config, uint8_t key); @@ -310,61 +285,32 @@ const char *zp_config_get(const z_loaned_config_t *config, uint8_t key); * Inserts or replaces the property with the given integer key in the configuration. * * Parameters: - * config: A loaned instance of :c:type:`z_owned_config_t`. - * key: Integer key for the property to be inserted. + * 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. * - * 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_loaned_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. - * - * 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)``. + * Builds a new :c:type:`z_owned_scouting_config_t` with a default set of properties for scouting 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. - * - * 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. - * - * Returns: - * Returns a new, zenoh-allocated, default scouting configuration. + * Parameters: + * sc: Pointer to an uninitialized :c:type:`z_owned_scouting_config_t`. */ void z_scouting_config_default(z_owned_scouting_config_t *sc); /** - * 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. - * - * 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)``. - * - * 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_scouting_config_check(&val)`` or ``z_check(val)`` if your - * compiler supports ``_Generic``, which will return ``true`` if ``val`` is valid, or ``false`` otherwise. + * Builds a new :c:type:`z_owned_scouting_config_t` with values from a :c:type:`z_loaned_config_t`. * * Parameters: - * config: A loaned instance of :c:type:`z_owned_config_t`. + * 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 scouting configuration. + * Return: + * ``0`` if build successful, ``negative value`` otherwise. */ int8_t z_scouting_config_from(z_owned_scouting_config_t *sc, const z_loaned_config_t *config); @@ -372,89 +318,116 @@ int8_t z_scouting_config_from(z_owned_scouting_config_t *sc, const z_loaned_conf * Gets the property with the given integer key from the configuration. * * Parameters: - * config: A loaned instance of :c:type:`z_owned_scouting_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_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_scouting_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_scouting_config_insert(z_loaned_scouting_config_t *config, uint8_t key, const char *value); /** - * Constructs a :c:type:`z_owned_encoding_t`. + * Builds a new :c:type:`z_owned_encoding_t`. * * Parameters: - * encoding: a reference to an uninitialized :c:type:`z_owned_encoding_t` - * id: A known :c:type:`z_encoding_id_t`. - * schema: A custom schema string value. + * 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. * - * Returns: - * Returns ``0`` if construction is successful, or a ``negative value`` otherwise. + * 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); /** - * Constructs a :c:type:`z_owned_encoding_t` with default value. + * Builds a new a :c:type:`z_owned_encoding_t` with default value. * * Parameters: - * encoding: a reference to an uninitialized :c:type:`z_owned_encoding_t` + * encoding: Pointer to an uninitialized :c:type:`z_owned_encoding_t`. * - * Returns: - * Returns ``0`` if construction is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if creation successful, ``negative value`` otherwise. */ int8_t zp_encoding_default(z_owned_encoding_t *encoding); /** - * Returns ``true`` if encoding is in non-default state, ``false`` otherwise. + * Checks if a :c:type:`z_owned_encoding_t` has non-default values. + * + * Return: + * ``true`` if encoding is in non-default state, ``false`` otherwise. */ _Bool z_encoding_check(const z_owned_encoding_t *encoding); /** - * Frees the memory of a :c:type:`z_owned_encoding_t`. + * Free the memory of a :c:type:`z_owned_encoding_t`. */ void z_encoding_drop(z_owned_encoding_t *encoding); /** - * Returns a loaned :c:type:`z_loaned_encoding_t`. + * Gets a loaned version of a :c:type:`z_owned_encoding_t`. + * + * Parameters: + * encoding: Pointer to a :c:type:`z_owned_encoding_t` to loan. + * + * Return: + * Pointer to the loaned version. */ const z_loaned_encoding_t *z_encoding_loan(const z_owned_encoding_t *encoding); /** - * Moves a owned :c:type:`z_owned_encoding_t`. + * Gets a moved version of a :c:type:`z_owned_encoding_t`. + * + * Parameters: + * encoding: Pointer to a :c:type:`z_owned_encoding_t` to move. + * + * Return: + * Pointer to the moved version. */ z_owned_encoding_t *z_encoding_move(z_owned_encoding_t *encoding); /** - * Constructs a :c:type:`z_owned_encoding_t` with default value. + * Builds a :c:type:`z_owned_encoding_t` with default value. * * Parameters: - * encoding: a reference to an uninitialized :c:type:`z_owned_encoding_t` + * encoding: Pointer to an uninitialized :c:type:`z_owned_encoding_t`. * - * Returns: - * Returns ``0`` if construction is successful, or a ``negative value`` otherwise. + * Return: + * ``0`` if creation successful,``negative value`` otherwise. */ int8_t z_encoding_null(z_owned_encoding_t *encoding); /** - * Returns value payload. + * Gets the bytes data from a value payload by aliasing it. + * + * Parameters: + * 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); /** - * Returns total number of bytes in the payload. + * Gets total number of bytes in a bytes array. + * + * Parameters: + * bytes: Pointer to a :c:type:`z_loaned_bytes_t` to get length from. + * + * Return: + * The number of bytes. */ size_t z_bytes_len(const z_loaned_bytes_t *bytes); @@ -462,348 +435,256 @@ size_t z_bytes_len(const z_loaned_bytes_t *bytes); * Decodes data into a :c:type:`z_owned_string_t` * * Parameters: - * bytes: Data to decode. - * s: An uninitialized memory location where to construct a decoded string. + * 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. + * + * Return: + * ``0`` if decode successful, or a ``negative value`` otherwise. */ -int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *s); +int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *str); /** - * Encodes string into a :c:type:`z_owned_bytes_t` + * Encodes a string into a :c:type:`z_owned_bytes_t` * * Parameters: - * buffer: An uninitialized memory location where to encode the string. - * s: The string to encode. + * 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. */ -int8_t z_bytes_encode_from_string(z_owned_bytes_t *buffer, const z_loaned_string_t *s); +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_loaned_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. */ 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`. */ 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_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`. */ const z_loaned_keyexpr_t *z_query_keyexpr(const z_loaned_query_t *query); -// TODO(sashacmc): update comment for all "owned" /** - * 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. */ 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. */ 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. */ 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. */ 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. */ 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 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. */ 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 hello 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_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 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); /** - * Return a new zid closure. + * Builds a new zid 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. - * - * 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. - * * 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. */ int8_t z_closure_zid(z_owned_closure_zid_t *closure, z_id_handler_t call, z_dropper_handler_t drop, void *context); @@ -862,45 +743,50 @@ _VIEW_FUNCTIONS(z_loaned_string_t, z_view_string_t, string) // 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) -// TODO(sashacmc): comments, docs, etc. +/** + * Loans a :c:type:`z_owned_sample_t`. + * + * Parameters: + * sample: Pointer to a :c:type:`z_owned_sample_t` to loan. + * + * Return: + * Pointer to the loaned sample as a :c:type:`z_loaned_sample_t`. + */ const z_loaned_sample_t *z_sample_loan(const z_owned_sample_t *sample); + +/** + * Gets data from a :c:type:`z_loaned_string_t`. + * + * Parameters: + * str: Pointer to a :c:type:`z_loaned_string_t` to get data from. + * + * Return: + * Pointer to the string data. + */ const char *z_string_data(const z_loaned_string_t *str); /************* Primitives **************/ /** - * Looks for other Zenoh-enabled entities like routers and/or peers. + * Scouts for other Zenoh entities like routers and/or peers. * * 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. + * 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. * - * Returns: - * Returns ``0`` if the scouting is successful triggered, or a ``negative value`` otherwise. + * 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. * - * 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)``. - * - * 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_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: 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. */ int8_t z_open(z_owned_session_t *zs, z_owned_config_t *config); @@ -908,157 +794,149 @@ 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_loaned_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_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_loaned_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_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_loaned_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_loaned_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_loaned_session_t`. + * Return: + * The local Zenoh ID of the session as :c:type:`z_id_t`. */ 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_loaned_keyexpr_t`. + * Return: + * The keyexpr wrapped as a :c:type:`z_loaned_keyexpr_t`. */ 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_loaned_bytes_t`. + * Return: + * The payload wrapped as a :c:type:`z_loaned_bytes_t`. */ 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_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_loaned_encoding_t*`. + * Return: + * The encoding wrapped as a :c:type:`z_loaned_encoding_t*`. */ 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_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_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_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`. */ 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`. */ void z_delete_options_default(z_delete_options_t *options); @@ -1066,249 +944,215 @@ 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_loaned_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(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_loaned_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(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`. */ 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_loaned_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. */ 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_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`. */ 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`. */ 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_loaned_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_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_loaned_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_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`. */ 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_loaned_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(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_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`. */ 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`. */ 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`. */ 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_loaned_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,_loaned ``z_check(val)`` ing the returned value will return ``false``. + * Return: + * ``0`` if declare operation successful, ``negative value`` otherwise. */ 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`. */ void z_query_reply_options_default(z_query_reply_options_t *options); @@ -1321,266 +1165,240 @@ void z_query_reply_options_default(z_query_reply_options_t *options); * 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_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. */ -int8_t z_keyexpr_new(z_owned_keyexpr_t *key, 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_loaned_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. */ -int8_t z_declare_keyexpr(z_owned_keyexpr_t *key, const z_loaned_session_t *zs, const z_loaned_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_loaned_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. */ // 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`. */ 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_loaned_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. */ 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 + * Copies the keyexpr of a subscriber * + * Parameters: + * 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. + * + * Return: + * ``0`` if copy successful, ``negative value`` otherwise. */ -z_owned_keyexpr_t z_subscriber_keyexpr(z_loaned_subscriber_t *sub); +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`. */ 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_loaned_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_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_loaned_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_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`. */ 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_loaned_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_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_loaned_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_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`. */ 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_loaned_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(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`. */ 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_loaned_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(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`. */ 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_loaned_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(const z_loaned_session_t *zs, const zp_send_join_options_t *options); diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index beefb4eba..c6ae6a95e 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -67,7 +67,6 @@ typedef _z_zint_t z_zint_t; * 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) @@ -88,7 +87,6 @@ typedef _z_id_t z_id_t; * size_t len: The length of the string. * const char *val: A pointer to the string. */ - _OWNED_TYPE_PTR(_z_string_t, string) _LOANED_TYPE(_z_string_t, string) _VIEW_TYPE(_z_string_t, string) @@ -96,9 +94,9 @@ _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` @@ -108,11 +106,9 @@ _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` @@ -123,11 +119,9 @@ _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` @@ -138,16 +132,15 @@ _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. */ - _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` @@ -158,7 +151,7 @@ _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` @@ -171,7 +164,7 @@ _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` @@ -180,7 +173,7 @@ _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. * */ _OWNED_TYPE_RC(_z_query_rc_t, query) @@ -208,34 +201,31 @@ typedef _z_timestamp_t z_timestamp_t; * z_loaned_encoding_t encoding: The encoding of the `payload`. * z_loaned_bytes_t* payload: The payload of this zenoh value. */ - _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 @@ -248,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. @@ -259,8 +248,7 @@ 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_owned_encoding_t *encoding: The encoding of the payload. @@ -272,8 +260,7 @@ typedef struct { } 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_owned_encoding_t *encoding: The encoding of the payload. @@ -290,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. @@ -303,8 +289,8 @@ 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_owned_encoding_t *encoding: The encoding of the payload. @@ -317,16 +303,15 @@ typedef struct { } 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. @@ -346,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 @@ -358,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 @@ -370,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. */ @@ -404,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. */ @@ -422,7 +406,7 @@ 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. @@ -453,15 +437,14 @@ _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 ``z_string_t``. * * Operations over :c:type:`z_loaned_string_array_t` must be done using the provided functions: * - * - ``char *z_string_array_get(z_loaned_string_array_t *a, size_t k);`` - * - ``size_t z_string_array_len(z_loaned_string_array_t *a);`` - * - ``_Bool z_str_array_array_is_empty(z_loaned_string_array_t *a);`` + * - :c:func:`z_string_array_get` + * - :c:func:`z_string_array_len` + * - :c:func:`z_str_array_array_is_empty` */ - _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) @@ -657,29 +640,34 @@ 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. @@ -687,6 +675,7 @@ z_owned_bytes_map_t z_bytes_map_from_attachment_aliasing(z_attachment_t this_); */ 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. * @@ -697,6 +686,7 @@ z_loaned_bytes_t *z_bytes_map_get(const z_owned_bytes_map_t *this_, z_loaned_byt */ 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. @@ -705,6 +695,7 @@ void z_bytes_map_insert_by_alias(const z_owned_bytes_map_t *this_, z_loaned_byte */ 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. * @@ -719,13 +710,15 @@ void z_bytes_map_insert_by_copy(const z_owned_bytes_map_t *this_, z_loaned_bytes */ 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); diff --git a/src/api/api.c b/src/api/api.c index 9cce5ef36..0a6224bc5 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -1078,27 +1078,27 @@ int8_t z_declare_subscriber(z_owned_subscriber_t *sub, const z_loaned_session_t int8_t z_undeclare_subscriber(z_owned_subscriber_t *sub) { return _z_subscriber_drop(&sub->_val); } -z_owned_keyexpr_t z_subscriber_keyexpr(z_loaned_subscriber_t *sub) { - z_owned_keyexpr_t ret; - z_keyexpr_null(&ret); - if (sub != NULL) { - uint32_t lookup = sub->_entity_id; - _z_subscription_rc_list_t *tail = sub->_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){._val = z_malloc(sizeof(_z_keyexpr_t))}; - if (ret._val != NULL) { - *ret._val = key; - } else { - _z_keyexpr_clear(&key); - } +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 From bff998e3b6b2575c70d52663fbd722c0d3dc2077 Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Thu, 6 Jun 2024 17:59:04 +0200 Subject: [PATCH 37/43] feat: update doc (#9) --- docs/api.rst | 176 +++++++++++++++++---------------- include/zenoh-pico/api/types.h | 6 +- 2 files changed, 94 insertions(+), 88 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 72f3a3e24..47dc2ed2e 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -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,66 @@ 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_keep_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. - -.. c:type:: z_owned_bytes_t - - A zenoh-allocated :c:type:`z_bytes_t`. - -.. c:type:: z_owned_str_t - - A zenoh-allocated :c:type:`char *`. - -.. c:type:: z_owned_keyexpr_t - - A zenoh-allocated :c:type:`z_keyexpr_t`. - -.. c:type:: z_owned_config_t - - A zenoh-allocated :c:type:`z_config_t`. - -.. c:type:: z_owned_session_t - - A zenoh-allocated :c:type:`z_session_t`. - -.. c:type:: z_owned_subscriber_t - - A zenoh-allocated :c:type:`z_subscriber_t`. - -.. c:type:: z_owned_publisher_t - - A zenoh-allocated :c:type:`z_publisher_t`. - -.. c:type:: z_owned_queryable_t - - A zenoh-allocated :c:type:`z_queryable_t`. - -.. c:type:: z_owned_reply_t +TODO: owned type description + +.. autoctype:: types.h::z_owned_bytes_t +.. autoctype:: types.h::z_owned_string_t +.. autoctype:: types.h::z_owned_keyexpr_t +.. autoctype:: types.h::z_owned_config_t +.. autoctype:: types.h::z_owned_scouting_config_t +.. autoctype:: types.h::z_owned_session_t +.. autoctype:: types.h::z_owned_subscriber_t +.. autoctype:: types.h::z_owned_publisher_t +.. autoctype:: types.h::z_owned_queryable_t +.. autoctype:: types.h::z_owned_query_t +.. autoctype:: types.h::z_owned_encoding_t +.. autoctype:: types.h::z_owned_value_t +.. autoctype:: types.h::z_owned_sample_t +.. autoctype:: types.h::z_owned_hello_t +.. autoctype:: types.h::z_owned_reply_t +.. autoctype:: types.h::z_owned_string_array_t +.. autoctype:: types.h::z_owned_bytes_map_t + +Loaned Types +~~~~~~~~~~~ - A zenoh-allocated :c:type:`z_reply_t`. +TODO: loaned type description + +.. autoctype:: types.h::z_loaned_bytes_t +.. autoctype:: types.h::z_loaned_string_t +.. autoctype:: types.h::z_loaned_keyexpr_t +.. autoctype:: types.h::z_loaned_config_t +.. autoctype:: types.h::z_loaned_scouting_config_t +.. autoctype:: types.h::z_loaned_session_t +.. autoctype:: types.h::z_loaned_subscriber_t +.. autoctype:: types.h::z_loaned_publisher_t +.. autoctype:: types.h::z_loaned_queryable_t +.. autoctype:: types.h::z_loaned_query_t +.. autoctype:: types.h::z_loaned_encoding_t +.. autoctype:: types.h::z_loaned_value_t +.. autoctype:: types.h::z_loaned_sample_t +.. autoctype:: types.h::z_loaned_hello_t +.. autoctype:: types.h::z_loaned_reply_t +.. autoctype:: types.h::z_loaned_string_array_t + +View Types +~~~~~~~~~~~ -.. c:type:: z_owned_str_array_t +TODO: view type description - A zenoh-allocated :c:type:`z_str_array_t`. +.. autoctype:: types.h::z_view_string_t +.. autoctype:: types.h::z_view_keyexpr_t +.. autoctype:: types.h::z_view_string_array_t Closures ~~~~~~~~ @@ -170,7 +149,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 +173,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::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 +192,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 +226,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 +252,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 \ No newline at end of file diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index c6ae6a95e..55add9cc3 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -277,7 +277,7 @@ typedef struct { } z_put_options_t; /** - * Represents the configuration used to configure a delete operation sent 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. @@ -304,14 +304,14 @@ typedef struct { /** * Represents the configuration used to configure a delete operation by a previously declared publisher, - * sent via :c:func:`z_publisher_delete`, + * 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 configuration used to configure a get operation sent 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. From 0c720ddd5f0121d542374090e35f4691667061ff Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Thu, 6 Jun 2024 19:21:09 +0200 Subject: [PATCH 38/43] fix: bad type in doc --- docs/api.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 47dc2ed2e..33d8a20ea 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -53,7 +53,7 @@ Data Structures .. autoctype:: types.h::zp_task_lease_options_t .. autoctype:: types.h::zp_read_options_t .. autoctype:: types.h::zp_send_keep_alive_options_t -.. autoctype:: types.h::zp_send_keep_join_options_t +.. autoctype:: types.h::zp_send_join_options_t .. autoctype:: types.h::z_qos_t Owned Types @@ -254,4 +254,4 @@ Primitives .. autocfunction:: primitives.h::zp_send_keep_alive_options_default .. autocfunction:: primitives.h::zp_send_keep_alive .. autocfunction:: primitives.h::zp_send_join_options_default -.. autocfunction:: primitives.h::zp_send_join \ No newline at end of file +.. autocfunction:: primitives.h::zp_send_join From 9d5f0d6764bc4a2f6255f017a4b9c4f00c816dd8 Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Fri, 7 Jun 2024 09:18:54 +0200 Subject: [PATCH 39/43] doc: missing member doc for closure types --- include/zenoh-pico/api/types.h | 43 ++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 55add9cc3..70c19885e 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -60,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. - */ -_OWNED_TYPE_PTR(_z_bytes_t, bytes) -_LOANED_TYPE(_z_bytes_t, bytes) - /** * Represents a Zenoh ID. * @@ -80,6 +70,21 @@ _LOANED_TYPE(_z_bytes_t, bytes) */ 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. * @@ -173,7 +178,7 @@ _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. * */ _OWNED_TYPE_RC(_z_query_rc_t, query) @@ -189,11 +194,6 @@ _LOANED_TYPE(_z_query_rc_t, query) _OWNED_TYPE_PTR(_z_encoding_t, encoding) _LOANED_TYPE(_z_encoding_t, encoding) -/* - * Represents timestamp value in Zenoh - */ -typedef _z_timestamp_t z_timestamp_t; - /** * Represents a Zenoh value. * @@ -437,7 +437,7 @@ _OWNED_TYPE_RC(_z_reply_rc_t, reply) _LOANED_TYPE(_z_reply_rc_t, reply) /** - * Represents an array of ``z_string_t``. + * Represents an array of non null-terminated string. * * Operations over :c:type:`z_loaned_string_array_t` must be done using the provided functions: * @@ -463,9 +463,9 @@ typedef _z_data_handler_t z_data_handler_t; * 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_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. */ typedef struct { void *context; @@ -481,6 +481,7 @@ void z_closure_sample_call(const z_owned_closure_sample_t *closure, const z_loan * 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_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 @@ -502,6 +503,7 @@ typedef _z_queryable_handler_t z_queryable_handler_t; * 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. @@ -523,6 +525,7 @@ 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: + * 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 @@ -545,6 +548,7 @@ typedef _z_reply_handler_t z_reply_handler_t; * 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. @@ -564,6 +568,7 @@ void z_closure_reply_call(const z_owned_closure_reply_t *closure, const z_loaned * 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. @@ -585,6 +590,7 @@ 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. @@ -606,6 +612,7 @@ 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. * void *context: a pointer to an arbitrary state. From eada7877bee0189a6c90d5a9c68e3b6855433639 Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Fri, 7 Jun 2024 09:22:19 +0200 Subject: [PATCH 40/43] fix: declare manually macro-defined types --- docs/api.rst | 172 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 137 insertions(+), 35 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 33d8a20ea..32dcf583d 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -61,22 +61,69 @@ Owned Types TODO: owned type description -.. autoctype:: types.h::z_owned_bytes_t -.. autoctype:: types.h::z_owned_string_t -.. autoctype:: types.h::z_owned_keyexpr_t -.. autoctype:: types.h::z_owned_config_t -.. autoctype:: types.h::z_owned_scouting_config_t -.. autoctype:: types.h::z_owned_session_t -.. autoctype:: types.h::z_owned_subscriber_t -.. autoctype:: types.h::z_owned_publisher_t -.. autoctype:: types.h::z_owned_queryable_t -.. autoctype:: types.h::z_owned_query_t -.. autoctype:: types.h::z_owned_encoding_t -.. autoctype:: types.h::z_owned_value_t -.. autoctype:: types.h::z_owned_sample_t -.. autoctype:: types.h::z_owned_hello_t -.. autoctype:: types.h::z_owned_reply_t -.. autoctype:: types.h::z_owned_string_array_t +.. c:type:: z_owned_bytes_t + + Represents an array of bytes. + +.. c:type:: z_owned_string_t + + Represents a string without null-terminator. + +.. c:type:: z_owned_keyexpr_t + + Represents a key expression in Zenoh. + +.. c:type:: z_owned_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 + + Represents a Zenoh Session. + +.. c:type:: z_owned_subscriber_t + + Represents a Zenoh Subscriber entity. + +.. c:type:: z_owned_publisher_t + Represents a Zenoh Publisher entity. + +.. c:type:: z_owned_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 + + Represents the reply to a query. + +.. c:type:: z_owned_string_array_t + + Represents an array of non null-terminated string. + .. autoctype:: types.h::z_owned_bytes_map_t Loaned Types @@ -84,31 +131,86 @@ Loaned Types TODO: loaned type description -.. autoctype:: types.h::z_loaned_bytes_t -.. autoctype:: types.h::z_loaned_string_t -.. autoctype:: types.h::z_loaned_keyexpr_t -.. autoctype:: types.h::z_loaned_config_t -.. autoctype:: types.h::z_loaned_scouting_config_t -.. autoctype:: types.h::z_loaned_session_t -.. autoctype:: types.h::z_loaned_subscriber_t -.. autoctype:: types.h::z_loaned_publisher_t -.. autoctype:: types.h::z_loaned_queryable_t -.. autoctype:: types.h::z_loaned_query_t -.. autoctype:: types.h::z_loaned_encoding_t -.. autoctype:: types.h::z_loaned_value_t -.. autoctype:: types.h::z_loaned_sample_t -.. autoctype:: types.h::z_loaned_hello_t -.. autoctype:: types.h::z_loaned_reply_t -.. autoctype:: types.h::z_loaned_string_array_t +.. 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 + + Represents a Zenoh Query entity, received by Zenoh queryable entities. + +.. 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 -.. autoctype:: types.h::z_view_string_t -.. autoctype:: types.h::z_view_keyexpr_t -.. autoctype:: types.h::z_view_string_array_t +.. 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 ~~~~~~~~ From f8c12a242b14ae95eed7b18bc30ab5f15ebd64ad Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Fri, 7 Jun 2024 09:25:58 +0200 Subject: [PATCH 41/43] fix: remove ignore type and fix missing new line --- docs/api.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 32dcf583d..b0591a4b8 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -90,6 +90,7 @@ TODO: owned type description Represents a Zenoh Subscriber entity. .. c:type:: z_owned_publisher_t + Represents a Zenoh Publisher entity. .. c:type:: z_owned_queryable_t @@ -124,8 +125,6 @@ TODO: owned type description Represents an array of non null-terminated string. -.. autoctype:: types.h::z_owned_bytes_map_t - Loaned Types ~~~~~~~~~~~ From bf495cb6acda8278f30c306292c09c0ca4f9dc0c Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Fri, 7 Jun 2024 09:37:17 +0200 Subject: [PATCH 42/43] fix: format conf.py and add attachment feature --- docs/conf.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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 From dac018294e7351db694a6ef4bbd7fcf508b21a32 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Mon, 10 Jun 2024 12:35:05 +0200 Subject: [PATCH 43/43] Rename z_view_str_wrap to z_view_string_wrap --- examples/arduino/z_get.ino | 2 +- examples/arduino/z_queryable.ino | 2 +- examples/espidf/z_get.c | 2 +- examples/espidf/z_queryable.c | 2 +- examples/freertos_plus_tcp/z_get.c | 2 +- examples/freertos_plus_tcp/z_queryable.c | 2 +- examples/mbed/z_get.cpp | 2 +- examples/mbed/z_queryable.cpp | 2 +- examples/unix/c11/z_get.c | 2 +- examples/unix/c11/z_get_channel.c | 2 +- examples/unix/c11/z_queryable.c | 2 +- examples/unix/c11/z_queryable_channel.c | 2 +- examples/unix/c99/z_get.c | 2 +- examples/unix/c99/z_queryable.c | 2 +- examples/windows/z_get.c | 2 +- examples/windows/z_queryable.c | 2 +- examples/zephyr/z_get.c | 2 +- examples/zephyr/z_queryable.c | 2 +- include/zenoh-pico/api/primitives.h | 2 +- src/api/api.c | 2 +- tests/z_api_alignment_test.c | 2 +- tests/z_client_test.c | 2 +- 22 files changed, 22 insertions(+), 22 deletions(-) diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index 2e172996f..a4a5a9f2d 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -118,7 +118,7 @@ void loop() { // Value encoding if (strcmp(VALUE, "") != 0) { z_view_string_t value_str; - z_view_str_wrap(&value_str, VALUE); + 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; diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index d4751dab5..da225a613 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -58,7 +58,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { // Reply value encoding z_view_string_t reply_str; - z_view_str_wrap(&reply_str, VALUE); + 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)); diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index eb06f20f1..7c53b86b5 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -165,7 +165,7 @@ void app_main() { // Value encoding if (strcmp(VALUE, "") != 0) { z_view_string_t value_str; - z_view_str_wrap(&value_str, VALUE); + 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; diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index 48b09ff8b..d10e25f57 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -121,7 +121,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); // Reply value encoding z_view_string_t reply_str; - z_view_str_wrap(&reply_str, VALUE); + z_view_string_wrap(&reply_str, VALUE); z_owned_bytes_t reply_payload; z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); diff --git a/examples/freertos_plus_tcp/z_get.c b/examples/freertos_plus_tcp/z_get.c index 1d53dab41..d91bb6065 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -89,7 +89,7 @@ void app_main(void) { // Value encoding if (strcmp(VALUE, "") != 0) { z_view_string_t value_str; - z_view_str_wrap(&value_str, VALUE); + 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; diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index 20d72f7eb..ca3444034 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -49,7 +49,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { z_query_reply_options_default(&options); // Reply value encoding z_view_string_t reply_str; - z_view_str_wrap(&reply_str, VALUE); + z_view_string_wrap(&reply_str, VALUE); z_owned_bytes_t reply_payload; z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index 699186612..ec2f2cef5 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -86,7 +86,7 @@ int main(int argc, char **argv) { // Value encoding if (strcmp(VALUE, "") != 0) { z_view_string_t value_str; - z_view_str_wrap(&value_str, VALUE); + 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; diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index 5c09bf79f..7be7f79da 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -49,7 +49,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { } // Reply value encoding z_view_string_t reply_str; - z_view_str_wrap(&reply_str, VALUE); + 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)); diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index f7677e3b3..6066ac2e7 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -137,7 +137,7 @@ int main(int argc, char **argv) { if (value != NULL) { z_view_string_t value_str; - z_view_str_wrap(&value_str, value); + 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; diff --git a/examples/unix/c11/z_get_channel.c b/examples/unix/c11/z_get_channel.c index 4f74b1848..e2eef163c 100644 --- a/examples/unix/c11/z_get_channel.c +++ b/examples/unix/c11/z_get_channel.c @@ -93,7 +93,7 @@ int main(int argc, char **argv) { // Value encoding if (value != NULL) { z_view_string_t value_str; - z_view_str_wrap(&value_str, value); + 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; diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index 17263e5d8..73ee65683 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -72,7 +72,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { // Reply value encoding z_view_string_t reply_str; - z_view_str_wrap(&reply_str, value); + z_view_string_wrap(&reply_str, value); z_owned_bytes_t reply_payload; z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); diff --git a/examples/unix/c11/z_queryable_channel.c b/examples/unix/c11/z_queryable_channel.c index 9038790b2..05091d334 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -117,7 +117,7 @@ int main(int argc, char **argv) { z_query_reply_options_default(&options); // Reply value encoding z_view_string_t reply_str; - z_view_str_wrap(&reply_str, value); + z_view_string_wrap(&reply_str, value); z_owned_bytes_t reply_payload; z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index 12c5bc3f6..e11827800 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -124,7 +124,7 @@ int main(int argc, char **argv) { // Value encoding if (value != NULL) { z_view_string_t value_str; - z_view_str_wrap(&value_str, value); + 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; diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index 08af246ab..9bdb4a910 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -47,7 +47,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { options.encoding = z_encoding_move(&encoding); // Reply value encoding z_view_string_t reply_str; - z_view_str_wrap(&reply_str, value); + 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)); diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index 9292ebdb1..42fdfea55 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -88,7 +88,7 @@ int main(int argc, char **argv) { // Value encoding if (value != NULL) { z_view_string_t value_str; - z_view_str_wrap(&value_str, value); + 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; diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index 91c9c8117..a6130c3b7 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -47,7 +47,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { options.encoding = z_move(encoding); // Reply value encoding z_view_string_t reply_str; - z_view_str_wrap(&reply_str, value); + z_view_string_wrap(&reply_str, value); z_owned_bytes_t reply_payload; z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); diff --git a/examples/zephyr/z_get.c b/examples/zephyr/z_get.c index 2635eb1ef..397b3d340 100644 --- a/examples/zephyr/z_get.c +++ b/examples/zephyr/z_get.c @@ -83,7 +83,7 @@ int main(int argc, char **argv) { // Value encoding if (strcmp(VALUE, "") != 0) { z_view_string_t value_str; - z_view_str_wrap(&value_str, VALUE); + 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; diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index 61e8710f8..f210cf1b3 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -49,7 +49,7 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { } // Reply value encoding z_view_string_t reply_str; - z_view_str_wrap(&reply_str, VALUE); + z_view_string_wrap(&reply_str, VALUE); z_owned_bytes_t reply_payload; z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index eb2a62e84..0653ffab6 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -44,7 +44,7 @@ extern "C" { * Return: * ``0`` if creation successful, ``negative value`` otherwise. */ -int8_t z_view_str_wrap(z_view_string_t *str, const char *value); +int8_t z_view_string_wrap(z_view_string_t *str, const char *value); /** * Builds a :c:type:`z_keyexpr_t` from a null-terminated string. diff --git a/src/api/api.c b/src/api/api.c index 0a6224bc5..f2bed7e36 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -43,7 +43,7 @@ /********* Data Types Handlers *********/ -int8_t z_view_str_wrap(z_view_string_t *str, const char *value) { +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; } diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 30e9ad42e..1204092f4 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -84,7 +84,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { // Reply value encoding z_view_string_t reply_str; - z_view_str_wrap(&reply_str, value); + z_view_string_wrap(&reply_str, value); z_owned_bytes_t reply_payload; z_bytes_encode_from_string(&reply_payload, z_loan(reply_str)); diff --git a/tests/z_client_test.c b/tests/z_client_test.c index f23d1f8ea..087ff18f7 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -63,7 +63,7 @@ void query_handler(const z_loaned_query_t *query, void *arg) { // Reply value encoding z_view_string_t reply_str; - z_view_str_wrap(&reply_str, res); + z_view_string_wrap(&reply_str, res); z_owned_bytes_t reply_payload; z_bytes_encode_from_string(&reply_payload, z_loan(reply_str));