From 37592dc50a1221f4d97082e0636d163d2b67ee32 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 22 May 2024 12:42:44 +0200 Subject: [PATCH] Strings/bytes queries/reply rework --- examples/arduino/z_get.ino | 3 +- examples/arduino/z_queryable.ino | 4 +- examples/espidf/z_get.c | 3 +- examples/espidf/z_queryable.c | 14 +++--- examples/espidf/z_sub.c | 4 +- examples/freertos_plus_tcp/z_get.c | 3 +- examples/freertos_plus_tcp/z_queryable.c | 20 ++++---- examples/mbed/z_get.cpp | 4 +- examples/mbed/z_pull.cpp | 4 +- examples/mbed/z_queryable.cpp | 9 ++-- examples/mbed/z_sub.cpp | 4 +- examples/unix/c11/z_get.c | 3 +- examples/unix/c11/z_get_channel.c | 3 +- examples/unix/c11/z_queryable.c | 19 ++++---- examples/unix/c11/z_queryable_channel.c | 20 ++++---- examples/unix/c99/z_get.c | 7 +-- examples/unix/c99/z_pull.c | 4 +- examples/unix/c99/z_queryable.c | 14 +++--- examples/unix/c99/z_sub.c | 4 +- examples/unix/c99/z_sub_st.c | 4 +- examples/windows/z_get.c | 3 +- examples/windows/z_queryable.c | 24 ++++++---- examples/zephyr/z_queryable.c | 13 ++++-- include/zenoh-pico/api/macros.h | 10 ++-- include/zenoh-pico/api/primitives.h | 22 +++------ include/zenoh-pico/api/types.h | 11 +++-- src/api/api.c | 59 ++++++++++-------------- 27 files changed, 154 insertions(+), 138 deletions(-) diff --git a/examples/arduino/z_get.ino b/examples/arduino/z_get.ino index caf054c69..f83fc6ab5 100644 --- a/examples/arduino/z_get.ino +++ b/examples/arduino/z_get.ino @@ -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_queryable.ino b/examples/arduino/z_queryable.ino index 69ee0d9ec..f7fadeec2 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -47,7 +47,9 @@ void query_handler(const z_loaned_query_t *query, void *arg) { 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_move(reply_payload), NULL); z_str_drop(z_str_move(&keystr)); } diff --git a/examples/espidf/z_get.c b/examples/espidf/z_get.c index 5e0282843..d42474e8d 100644 --- a/examples/espidf/z_get.c +++ b/examples/espidf/z_get.c @@ -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_queryable.c b/examples/espidf/z_queryable.c index 6e8ee8270..8c8da8896 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -104,14 +104,16 @@ void wifi_init_sta(void) { void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); z_owned_string_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_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_sub.c b/examples/espidf/z_sub.c index 3f2a0ec88..45e1b8ed9 100644 --- a/examples/espidf/z_sub.c +++ b/examples/espidf/z_sub.c @@ -104,9 +104,9 @@ 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_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 056431660..efe049411 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -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_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index 87c0c6751..95eb3813c 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -33,17 +33,19 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); 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/mbed/z_get.cpp b/examples/mbed/z_get.cpp index 3995105d4..407535775 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -39,8 +39,8 @@ void reply_handler(const z_loaned_reply_t *oreply, 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(" >> 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"); } diff --git a/examples/mbed/z_pull.cpp b/examples/mbed/z_pull.cpp index 249a8fa8d..d01f2978c 100644 --- a/examples/mbed/z_pull.cpp +++ b/examples/mbed/z_pull.cpp @@ -34,10 +34,10 @@ // 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_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 2ac42bb79..133081993 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -37,10 +37,13 @@ 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_str_loan(&keystr)), + printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_string_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)); + + 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_sub.cpp b/examples/mbed/z_sub.cpp index 4c7fcea91..537630d6a 100644 --- a/examples/mbed/z_sub.cpp +++ b/examples/mbed/z_sub.cpp @@ -34,9 +34,9 @@ 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_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 cba56fe00..132f566f2 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -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 07c6c11a6..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); diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index 0e5babb98..08ced0e1f 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -35,13 +35,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); 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 81667b8f3..ce128023a 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -102,17 +102,19 @@ int main(int argc, char **argv) { const z_loaned_query_t *q = z_loan(query); 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/c99/z_get.c b/examples/unix/c99/z_get.c index d06ee6036..38a6339c7 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -36,8 +36,8 @@ void reply_handler(const z_loaned_reply_t *reply, 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(">> 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 44b356b6c..0760264de 100644 --- a/examples/unix/c99/z_pull.c +++ b/examples/unix/c99/z_pull.c @@ -24,9 +24,9 @@ // (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_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 02d38d7f3..df3a8a6e2 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -26,15 +26,17 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); z_owned_string_t keystr; z_keyexpr_to_string(z_query_keyexpr(query), &keystr); - z_view_string_t pred; - z_query_parameters(query, &pred); - printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_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_sub.c b/examples/unix/c99/z_sub.c index 4ba6e9c8b..fa05fc001 100644 --- a/examples/unix/c99/z_sub.c +++ b/examples/unix/c99/z_sub.c @@ -25,9 +25,9 @@ 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_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 8b047fc37..f97540233 100644 --- a/examples/unix/c99/z_sub_st.c +++ b/examples/unix/c99/z_sub_st.c @@ -28,9 +28,9 @@ 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_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 51edb6f2d..405b1a279 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -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_queryable.c b/examples/windows/z_queryable.c index 064bd2501..d810c0926 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -25,18 +25,22 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); 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/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index 430a783fe..8cb203b0b 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -35,11 +35,14 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { (void)(ctx); 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_string_view_t params; + z_query_parameters(query, params); + 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/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index a12cacffd..bf5cf8c96 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -46,7 +46,8 @@ 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), \ @@ -58,9 +59,11 @@ z_owned_reply_t : z_reply_loan_mut, \ z_owned_hello_t : z_hello_loan_mut, \ z_owned_string_t : z_string_loan_mut, \ + z_view_string_t : z_view_string_loan_mut, \ z_owned_string_array_t : z_string_array_loan_mut, \ z_owned_sample_t : z_sample_loan_mut, \ - z_owned_query_t : z_query_loan_mut \ + z_owned_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. @@ -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, \ @@ -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, \ diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index ad96cb5d6..6daa41f20 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -528,7 +528,7 @@ void z_query_parameters(const z_loaned_query_t *query, z_view_string_t *paramete * 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 /** @@ -795,6 +795,7 @@ _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); \ @@ -1171,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. * @@ -1211,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 @@ -1294,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 /** @@ -1404,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 6518ec9a2..a25577ada 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -208,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, @@ -330,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; diff --git a/src/api/api.c b/src/api/api.c index aef214fe1..1a24ccc81 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -268,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) { @@ -293,11 +289,11 @@ 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_string_t *parameters) { - // TODO(sashacmc) - // *parameters = _z_bytes_wrap((uint8_t *)query->in->val._parameters, strlen(query->in->val._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; } @@ -305,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); @@ -409,6 +395,7 @@ static inline void _z_owner_noop_copy(void *dst, const void *src) { OWNED_FUNCTIONS_PTR(_z_config_t, config, _z_owner_noop_copy, _z_config_free) OWNED_FUNCTIONS_PTR(_z_scouting_config_t, scouting_config, _z_owner_noop_copy, _z_scouting_config_free) OWNED_FUNCTIONS_PTR(_z_string_t, string, _z_string_copy, _z_string_free) +OWNED_FUNCTIONS_PTR(_z_value_t, value, _z_value_copy, _z_value_free) OWNED_FUNCTIONS_PTR(_z_keyexpr_t, keyexpr, _z_keyexpr_copy, _z_keyexpr_free) VIEW_FUNCTIONS_PTR(_z_keyexpr_t, keyexpr) @@ -803,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 @@ -812,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; @@ -823,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 @@ -837,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; } @@ -856,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 @@ -910,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