From 9d54437fbdbed7fded1b92d0fe7771dc861a301a Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 12 Jun 2024 16:05:06 +0200 Subject: [PATCH] fix: update tests & examples --- examples/arduino/z_queryable.ino | 9 ++++--- examples/arduino/z_sub.ino | 7 +++--- examples/espidf/z_queryable.c | 11 +++++---- examples/espidf/z_sub.c | 8 ++++--- examples/freertos_plus_tcp/z_queryable.c | 10 ++++---- examples/freertos_plus_tcp/z_sub.c | 7 +++--- examples/freertos_plus_tcp/z_sub_st.c | 8 +++---- examples/mbed/z_queryable.cpp | 10 ++++---- examples/mbed/z_sub.cpp | 8 ++++--- examples/unix/c11/z_pong.c | 6 +++-- examples/unix/c11/z_pull.c | 8 ++++--- examples/unix/c11/z_queryable.c | 10 ++++---- examples/unix/c11/z_queryable_channel.c | 11 +++++---- examples/unix/c11/z_sub.c | 7 +++--- examples/unix/c11/z_sub_channel.c | 7 +++--- examples/unix/c11/z_sub_st.c | 7 +++--- examples/unix/c99/z_pong.c | 6 +++-- examples/unix/c99/z_queryable.c | 10 ++++---- examples/unix/c99/z_sub.c | 8 ++++--- examples/unix/c99/z_sub_st.c | 8 ++++--- examples/windows/z_pong.c | 6 +++-- examples/windows/z_queryable.c | 10 ++++---- examples/windows/z_sub.c | 7 +++--- examples/windows/z_sub_st.c | 8 +++---- examples/zephyr/z_queryable.c | 10 ++++---- examples/zephyr/z_sub.c | 8 ++++--- tests/z_channels_test.c | 30 +++++++++++++----------- tests/z_client_test.c | 14 +++++++---- tests/z_peer_multicast_test.c | 6 +++-- tests/z_perf_rx.c | 6 +++-- tests/z_test_fragment_rx.c | 8 ++++--- 31 files changed, 158 insertions(+), 121 deletions(-) diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index 1a62641e3..ec15cdd19 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -43,15 +43,14 @@ void query_handler(const z_loaned_query_t *query, void *arg) { 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); + z_owned_string_t payload_string; + z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string); + if (z_string_len(z_string_loan(payload_string)) > 1) { Serial.print(" with value '"); Serial.print(z_string_data(z_string_loan(&payload_string))); Serial.println("'"); - z_string_drop(z_string_move(&payload_string)); } + 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/arduino/z_sub.ino b/examples/arduino/z_sub.ino index 8104e574e..163368ed7 100644 --- a/examples/arduino/z_sub.ino +++ b/examples/arduino/z_sub.ino @@ -37,16 +37,17 @@ 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); - std::string val((const char *)z_bytes_data(payload), z_bytes_len(payload)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(sample), &value); Serial.print(" >> [Subscription listener] Received ("); Serial.print(z_string_data(z_string_loan(&keystr))); Serial.print(", "); - Serial.print(val.c_str()); + Serial.print(z_string_data(z_string_loan(&value))); Serial.println(")"); z_string_drop(z_string_move(&keystr)); + z_string_drop(z_string_move(&value)); } void setup() { diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index 22c673704..160d0d475 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -110,15 +110,16 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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); + z_owned_string_t payload_string; + z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string); + if (z_string_len(z_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); - z_drop(z_move(payload_string)); } + z_drop(z_move(payload_string)); + z_view_keyexpr_t ke; z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR); + // Reply value encoding z_owned_bytes_t reply_payload; z_bytes_encode_from_string(&reply_payload, VALUE); diff --git a/examples/espidf/z_sub.c b/examples/espidf/z_sub.c index 07e2efec8..278dff3c1 100644 --- a/examples/espidf/z_sub.c +++ b/examples/espidf/z_sub.c @@ -103,10 +103,12 @@ 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); - const z_loaned_bytes_t* payload = z_sample_payload(sample); - printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)), - (int)z_bytes_len(payload), z_bytes_data(payload)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(sample), &value); + printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)), + z_string_data(z_string_loan(&value))); z_string_drop(z_string_move(&keystr)); + z_string_drop(z_string_move(&value)); } void app_main() { diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index 46b62888f..923fef662 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -38,13 +38,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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); + z_owned_string_t payload_string; + z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string); + if (z_string_len(z_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); - z_drop(z_move(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/freertos_plus_tcp/z_sub.c b/examples/freertos_plus_tcp/z_sub.c index e44b27f14..ed923ef4d 100644 --- a/examples/freertos_plus_tcp/z_sub.c +++ b/examples/freertos_plus_tcp/z_sub.c @@ -32,10 +32,11 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { (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_string_data(z_loan(keystr)), (int)z_bytes_len(payload), - z_bytes_data(payload)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(sample), &value); + printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); z_drop(z_move(keystr)); + z_drop(z_move(value)); } void app_main(void) { diff --git a/examples/freertos_plus_tcp/z_sub_st.c b/examples/freertos_plus_tcp/z_sub_st.c index 13212ff1c..40c0e613f 100644 --- a/examples/freertos_plus_tcp/z_sub_st.c +++ b/examples/freertos_plus_tcp/z_sub_st.c @@ -35,11 +35,11 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { (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_string_data(z_loan(keystr)), (int)z_bytes_len(payload), - z_bytes_data(payload)); - z_drop(z_move(keystr)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(sample), &value); + printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); z_drop(z_move(keystr)); + z_drop(z_move(value)); msg_nb++; } diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index 3a243e9ca..701ac48b6 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -40,13 +40,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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); + z_owned_string_t payload_string; + z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string); + if (z_string_len(z_string_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_string_loan(&payload_string))); - z_string_drop(z_string_move(&payload_string)); } + z_string_drop(z_string_move(&payload_string)); + // Reply value encoding z_owned_bytes_t reply_payload; z_bytes_encode_from_string(&reply_payload, VALUE); diff --git a/examples/mbed/z_sub.cpp b/examples/mbed/z_sub.cpp index 99325653e..4cb1e9b89 100644 --- a/examples/mbed/z_sub.cpp +++ b/examples/mbed/z_sub.cpp @@ -33,10 +33,12 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); - const z_loaned_bytes_t *payload = z_sample_payload(sample); - printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)), - (int)z_bytes_len(payload), z_bytes_data(payload)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(sample), &value); + printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)), + z_string_data(z_string_loan(&value))); z_string_drop(z_string_move(&keystr)); + z_string_drop(z_string_move(&value)); } int main(int argc, char **argv) { diff --git a/examples/unix/c11/z_pong.c b/examples/unix/c11/z_pong.c index 62b083e14..6b7514bc4 100644 --- a/examples/unix/c11/z_pong.c +++ b/examples/unix/c11/z_pong.c @@ -18,8 +18,10 @@ #if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1 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, z_bytes_data(payload), z_bytes_len(payload), NULL); + z_owned_slice_t value; + z_bytes_decode_into_slice(z_sample_payload(sample), &value); + z_publisher_put(pub, z_slice_data(z_loan(value)), z_slice_len(z_loan(value)), NULL); + z_drop(z_move(value)); } void drop(void* context) { z_owned_publisher_t* pub = (z_owned_publisher_t*)context; diff --git a/examples/unix/c11/z_pull.c b/examples/unix/c11/z_pull.c index 29bb3f1ef..7dab57c57 100644 --- a/examples/unix/c11/z_pull.c +++ b/examples/unix/c11/z_pull.c @@ -90,10 +90,12 @@ 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); - const z_loaned_bytes_t *payload = z_sample_payload(z_loan(sample)); - printf(">> [Subscriber] Pulled ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)z_bytes_len(payload), - z_bytes_data(payload)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(z_loan(sample)), &value); + printf(">> [Subscriber] Pulled ('%s': '%s')\n", z_string_data(z_loan(keystr)), + z_string_data(z_loan(value))); z_drop(z_move(keystr)); + z_drop(z_move(value)); z_drop(z_move(sample)); } printf(">> [Subscriber] Nothing to pull... sleep for %zu ms\n", interval); diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index 002059421..cc65c3956 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -40,13 +40,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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); + z_owned_string_t payload_string; + z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string); + if (z_string_len(z_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); - z_drop(z_move(payload_string)); } + z_drop(z_move(payload_string)); + #if Z_FEATURE_ATTACHMENT == 1 z_attachment_t attachment = z_query_attachment(query); if (z_attachment_check(&attachment)) { diff --git a/examples/unix/c11/z_queryable_channel.c b/examples/unix/c11/z_queryable_channel.c index cd69bc8bf..22888c46f 100644 --- a/examples/unix/c11/z_queryable_channel.c +++ b/examples/unix/c11/z_queryable_channel.c @@ -106,13 +106,14 @@ int main(int argc, char **argv) { z_query_parameters(q, ¶ms); printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len, z_loan(params)->val); - const z_loaned_bytes_t *payload = z_value_payload(z_query_value(z_loan(query))); - if (z_bytes_len(payload) > 0) { - z_owned_string_t payload_string; - z_bytes_decode_into_string(payload, &payload_string); + // Process value + z_owned_string_t payload_string; + z_bytes_decode_into_string(z_value_payload(z_query_value(z_loan(query))), &payload_string); + if (z_string_len(z_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); - z_drop(z_move(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/unix/c11/z_sub.c b/examples/unix/c11/z_sub.c index bfa3838c3..3f245eefb 100644 --- a/examples/unix/c11/z_sub.c +++ b/examples/unix/c11/z_sub.c @@ -36,9 +36,9 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { (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_string_data(z_loan(keystr)), (int)z_bytes_len(payload), - z_bytes_data(payload)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(sample), &value); + printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); #if Z_FEATURE_ATTACHMENT == 1 z_attachment_t attachment = z_sample_attachment(sample); if (z_attachment_check(&attachment)) { @@ -47,6 +47,7 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { } #endif z_drop(z_move(keystr)); + z_drop(z_move(value)); msg_nb++; } diff --git a/examples/unix/c11/z_sub_channel.c b/examples/unix/c11/z_sub_channel.c index 3dcc96b11..94e341eec 100644 --- a/examples/unix/c11/z_sub_channel.c +++ b/examples/unix/c11/z_sub_channel.c @@ -80,10 +80,11 @@ 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); - const z_loaned_bytes_t *payload = z_sample_payload(z_loan(sample)); - printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)z_bytes_len(payload), - z_bytes_data(payload)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(z_loan(sample)), &value); + printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); z_drop(z_move(keystr)); + z_drop(z_move(value)); z_drop(z_move(sample)); z_null(&sample); } diff --git a/examples/unix/c11/z_sub_st.c b/examples/unix/c11/z_sub_st.c index 168bd20df..004cd1b39 100644 --- a/examples/unix/c11/z_sub_st.c +++ b/examples/unix/c11/z_sub_st.c @@ -27,10 +27,11 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { (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_string_data(z_loan(keystr)), (int)z_bytes_len(payload), - z_bytes_data(payload)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(sample), &value); + printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); z_drop(z_move(keystr)); + z_drop(z_move(value)); msg_nb++; } diff --git a/examples/unix/c99/z_pong.c b/examples/unix/c99/z_pong.c index 45cbfaa9f..6f390bdf4 100644 --- a/examples/unix/c99/z_pong.c +++ b/examples/unix/c99/z_pong.c @@ -19,8 +19,10 @@ #if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1 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, z_bytes_data(payload), z_bytes_len(payload), NULL); + z_owned_slice_t value; + z_bytes_decode_into_slice(z_sample_payload(sample), &value); + z_publisher_put(pub, z_slice_data(z_string_loan(value)), z_slice_len(z_string_loan(value)), NULL); + z_slice_drop(z_slice_move(&value)); } void drop(void* context) { z_owned_publisher_t* pub = (z_owned_publisher_t*)context; diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index 910b66cf9..35e7d50eb 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -31,13 +31,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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); + z_owned_string_t payload_string; + z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string); + if (z_string_len(z_string_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_string_loan(&payload_string))); - z_string_drop(z_string_move(&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); diff --git a/examples/unix/c99/z_sub.c b/examples/unix/c99/z_sub.c index 992f39219..f354857aa 100644 --- a/examples/unix/c99/z_sub.c +++ b/examples/unix/c99/z_sub.c @@ -24,10 +24,12 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { (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_string_data(z_string_loan(&keystr)), - (int)z_bytes_len(payload), z_bytes_data(payload)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(sample), &value); + printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)), + z_string_data(z_string_loan(&value))); z_string_drop(z_string_move(&keystr)); + z_string_drop(z_string_move(&value)); } 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 745bee1a5..548f7a9c6 100644 --- a/examples/unix/c99/z_sub_st.c +++ b/examples/unix/c99/z_sub_st.c @@ -27,10 +27,12 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { (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_string_data(z_string_loan(&keystr)), - (int)z_bytes_len(payload), z_bytes_data(payload)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(sample), &value); + printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)), + z_string_data(z_string_loan(&value))); z_string_drop(z_string_move(&keystr)); + z_string_drop(z_string_move(&value)); msg_nb++; } diff --git a/examples/windows/z_pong.c b/examples/windows/z_pong.c index 62b083e14..6b7514bc4 100644 --- a/examples/windows/z_pong.c +++ b/examples/windows/z_pong.c @@ -18,8 +18,10 @@ #if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1 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, z_bytes_data(payload), z_bytes_len(payload), NULL); + z_owned_slice_t value; + z_bytes_decode_into_slice(z_sample_payload(sample), &value); + z_publisher_put(pub, z_slice_data(z_loan(value)), z_slice_len(z_loan(value)), NULL); + z_drop(z_move(value)); } void drop(void* context) { z_owned_publisher_t* pub = (z_owned_publisher_t*)context; diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index 446543033..c87bcd8a3 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -31,13 +31,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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); + z_owned_string_t payload_string; + z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string); + if (z_string_len(z_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); - z_drop(z_move(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); diff --git a/examples/windows/z_sub.c b/examples/windows/z_sub.c index a6b854f2f..1472a8982 100644 --- a/examples/windows/z_sub.c +++ b/examples/windows/z_sub.c @@ -23,10 +23,11 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { (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_string_data(z_loan(keystr)), (int)z_bytes_len(payload), - z_bytes_data(payload)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(sample), &value); + printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); z_drop(z_move(keystr)); + z_drop(z_move(value)); } int main(int argc, char **argv) { diff --git a/examples/windows/z_sub_st.c b/examples/windows/z_sub_st.c index 76e8ab22a..a122b6771 100644 --- a/examples/windows/z_sub_st.c +++ b/examples/windows/z_sub_st.c @@ -26,11 +26,11 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { (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_string_data(z_loan(keystr)), (int)z_bytes_len(payload), - z_bytes_data(payload)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(sample), &value); + printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value))); z_drop(z_move(keystr)); - + z_drop(z_move(value)); msg_nb++; } diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index e2a494e3a..60a2e0261 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -40,13 +40,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) { 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); + z_owned_string_t payload_string; + z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string); + if (z_string_len(z_loan(payload_string)) > 1) { printf(" with value '%s'\n", z_string_data(z_loan(payload_string))); - z_drop(z_move(payload_string)); } + z_drop(z_move(payload_string)); + // Reply value encoding z_owned_bytes_t reply_payload; z_bytes_encode_from_string(&reply_payload, VALUE); diff --git a/examples/zephyr/z_sub.c b/examples/zephyr/z_sub.c index ea447d7c3..33e1a8236 100644 --- a/examples/zephyr/z_sub.c +++ b/examples/zephyr/z_sub.c @@ -32,10 +32,12 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { z_owned_string_t keystr; z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr); - const z_loaned_bytes_t *payload = z_sample_payload(sample); - printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), - (int)z_bytes_len(payload), z_bytes_data(payload)); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(sample), &value); + printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), + z_string_data(z_loan(value))); z_drop(z_move(keystr)); + z_drop(z_move(value)); } int main(int argc, char **argv) { diff --git a/tests/z_channels_test.c b/tests/z_channels_test.c index a749fe163..da794fd9f 100644 --- a/tests/z_channels_test.c +++ b/tests/z_channels_test.c @@ -35,20 +35,22 @@ z_call(channel.send, &sample); \ } while (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)); \ - size_t payload_len = z_bytes_len(payload); \ - strncpy(buf, (const char *)z_bytes_data(payload), 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)) { \ + z_owned_slice_t value; \ + z_bytes_decode_into_slice(z_sample_payload(z_loan(sample)), &value); \ + size_t value_len = z_slice_len(z_loan(value)); \ + strncpy(buf, (const char *)z_slice_data(z_loan(value)), value_len); \ + buf[value_len] = '\0'; \ + z_drop(z_move(sample)); \ + z_drop(z_move(value)); \ + } else { \ + buf[0] = '\0'; \ + } \ } while (0); #define RECV(channel, buf) _RECV(channel, recv, buf) diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 0ec5da514..c82bd25c4 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -81,13 +81,15 @@ void reply_handler(const z_loaned_reply_t *reply, void *arg) { 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(z_bytes_len(payload) == strlen(res)); - assert(strncmp(res, (const char *)z_bytes_data(payload), strlen(res)) == 0); + z_owned_string_t value; + z_bytes_decode_into_string(z_sample_payload(sample), &value); + assert(z_string_len(z_loan(value)) == strlen(res) + 1); + assert(strncmp(res, z_string_data(z_loan(value)), strlen(res)) == 0); assert(_z_str_eq(z_loan(k_str)->val, res) == true); replies++; z_drop(z_move(k_str)); + z_drop(z_move(value)); } else { printf(">> Received an error\n"); } @@ -102,13 +104,15 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { 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); - size_t payload_len = z_bytes_len(payload); + z_owned_slice_t value; + z_bytes_decode_into_slice(z_sample_payload(sample), &value); + size_t payload_len = z_slice_len(z_loan(value)); 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)); + z_drop(z_move(value)); free(res); } diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index 7ead51b64..93a17c10a 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -48,14 +48,16 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { 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(z_bytes_len(payload) == MSG_LEN); + z_owned_slice_t value; + z_bytes_decode_into_slice(z_sample_payload(sample), &value); + assert(z_slice_len(z_loan(value)) == MSG_LEN); assert(z_loan(k_str)->len == strlen(res)); assert(strncmp(res, z_loan(k_str)->val, strlen(res)) == 0); (void)(sample); datas++; z_drop(z_move(k_str)); + z_drop(z_move(value)); free(res); } diff --git a/tests/z_perf_rx.c b/tests/z_perf_rx.c index e9ac3ebdd..65511a4b4 100644 --- a/tests/z_perf_rx.c +++ b/tests/z_perf_rx.c @@ -41,8 +41,9 @@ void z_stats_stop(z_stats_t *stats) { void on_sample(const z_loaned_sample_t *sample, void *context) { z_stats_t *stats = (z_stats_t *)context; - const z_loaned_bytes_t *payload = z_sample_payload(sample); - unsigned long data_len = (unsigned long)z_bytes_len(payload); + z_owned_slice_t value; + z_bytes_decode_into_slice(z_sample_payload(sample), &value); + unsigned long data_len = (unsigned long)z_slice_len(z_loan(value)); if (stats->curr_len != data_len) { // End previous measurement @@ -57,6 +58,7 @@ void on_sample(const z_loaned_sample_t *sample, void *context) { printf("Starting test for pkt len: %lu\n", stats->curr_len); stats->start = z_clock_now(); } + z_drop(z_move(value)); stats->count++; } diff --git a/tests/z_test_fragment_rx.c b/tests/z_test_fragment_rx.c index f4b2d0395..06fafd4ae 100644 --- a/tests/z_test_fragment_rx.c +++ b/tests/z_test_fragment_rx.c @@ -23,9 +23,10 @@ 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); bool is_valid = true; - const z_loaned_bytes_t *payload = z_sample_payload(sample); - const uint8_t *data = z_bytes_data(payload); - size_t data_len = z_bytes_len(payload); + z_owned_slice_t value; + z_bytes_decode_into_slice(z_sample_payload(sample), &value); + const uint8_t *data = z_slice_data(z_loan(value)); + size_t data_len = z_slice_len(z_loan(value)); for (size_t i = 0; i < data_len; i++) { if (data[i] != (uint8_t)i) { is_valid = false; @@ -35,6 +36,7 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) { printf("[rx]: Received packet on %s, len: %d, validity: %d\n", z_string_data(z_loan(keystr)), (int)data_len, is_valid); z_drop(z_move(keystr)); + z_drop(z_move(value)); } int main(int argc, char **argv) {